-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
109 lines (77 loc) · 2.03 KB
/
test.cpp
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//
// Created by mklong on 15/12/14.
//
#include "rapidjson2struct.h"
#include <vector>
#include <string>
#include <cstdio>
using namespace std;
using namespace rapidjson2struct;
struct student :public base{
student():id(0),male(true){ }
int id;
string name;
bool male;
void print(){
printf("student \nid: %d\nname : %s\nmale : %d\n"
,id,name.c_str(),male);
}
virtual void parser_add_members(){
add_number_member(id,"id",true);
add_string_member(name,"name",true);
add_bool_member(male,"male");
}
};
class grade : public base
{
public:
grade(){}
~grade(){
for(int i = 0 ;i < students.size();i ++)
delete students[i];
}
student monitor;
vector<student* > students;
bool rocket;
void print(){
printf("monitor : \n");
monitor.print();
printf("students : \n");
for (int i = 0; i < students.size(); ++i) {
students[i]->print();
}
printf("rocket : %d\n",rocket);
}
virtual void parser_add_members(){
add_struct_member(&monitor,"monitor",true);
add_array_struct_member(&students,"students",alloc<student>,true);
add_bool_member(rocket,"rocket");
}
private:
grade(const grade &);
grade &operator= (const grade &);
};
static const char * json = "{\"monitor\":{\"id\":110,\"name\":\"711\"},"
"\"students\":[{\"id\":111,\"name\":\"langke\",\"male\":true},{\"id\":112,\"name\":\"guozu\"}],"
"\"rocket\":true}";
int main(){
int ret = 0;
rapidjson::Document doc;
doc.Parse(json);
if(doc.HasParseError()){
ret = doc.GetParseError();
printf("errno : %d\n",ret);
return -1;
}
grade g;
g.parser_add_members();
ret = g.parse(doc);
if (ret){
printf("errno : %d ,name : %s\n",ret,g.get_parse_error_name().c_str());
return ret;
}
g.print();
printf("debug info: \n");
g.debug_print();
return 0;
}