-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
82 lines (68 loc) · 1.78 KB
/
Copy pathmain.cpp
File metadata and controls
82 lines (68 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include "JObject.h"
#include "Parser.h"
#include <fstream>
using namespace json;
struct Base
{
int pp;
string qq;
START_FROM_JSON
pp = from("pp", int);
qq = from("qq", string);
END_FROM_JSON
START_TO_JSON
to("pp") = pp;
to("qq") = qq;
END_TO_JSON
};
struct Mytest
{
int id;
std::string name;
Base q;
START_TO_JSON
to_struct("base", q);
to("id") = id;
to("name") = name;
END_TO_JSON
START_FROM_JSON
id = from("id", int);
name = from("name", string);
from_struct("base", q);
END_FROM_JSON
};
// void test_class_serialization()
// {
// Mytest test{.id = 32, .name = "fda"};
// auto item = Parser::FromJSON<Mytest>(R"({"base":{"pp":0,"qq":""},"id":32,"name":"fda"} )"); // serialization
// std::cout << Parser::ToJSON(item); // deserialization
// }
void test_string_parser()
{
std::ifstream fin(R"(../test_source/test.json)");
if (!fin) {
std::cout << "read file error";
return;
}
// 提示:
// std::istreambuf_iterator<char>(fin):
// 这是一个输入流迭代器,使用 fin 流(文件输入流)作为输入源,
// char 表示它是字符流迭代器,每次迭代都会读取一个字符。
// std::istreambuf_iterator<char>():
// 这是一个默认构造的、标记为结束的输入流迭代器,表示流的末尾。
std::string text((std::istreambuf_iterator<char>(fin)), std::istreambuf_iterator<char>());
{
auto object = json::Parser::FromString(text);
std::cout << (object["[markdown]"].to_string()) << "\n";
}
}
enum T
{
MYSD,
sf
};
int main()
{
test_string_parser();
}