-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
230 lines (200 loc) · 7.51 KB
/
main.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "JsonWrapper.h"
void JsonUIntField_WriteTo_Test() {
JsonFieldsContainer container;
JsonValue<unsigned int> testable1(&container, "test", 19);
JsonValue<uint32_t> testable2(&container, "test", 20);
JsonValue<uint16_t> testable3(&container, "test", 21);
JsonValue<uint8_t> testable4(&container, "test", 22);
{
rapidjson::Document doc;
doc.SetObject();
testable1.WriteToDoc(&doc);
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
doc.Accept(writer);
if (strcmp(buffer.GetString(), "{\"test\":19}") == 0) { printf("111\n"); }
}
{
rapidjson::Document doc;
doc.SetObject();
testable2.WriteToDoc(&doc);
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
doc.Accept(writer);
if (strcmp(buffer.GetString(), "{\"test\":20}") == 0) { printf("222\n"); }
}
{
rapidjson::Document doc;
doc.SetObject();
testable3.WriteToDoc(&doc);
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
doc.Accept(writer);
if (strcmp(buffer.GetString(), "{\"test\":21}") == 0) { printf("333\n"); }
}
{
rapidjson::Document doc;
doc.SetObject();
testable4.WriteToDoc(&doc);
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
doc.Accept(writer);
if (strcmp(buffer.GetString(), "{\"test\":22}") == 0) { printf("444\n"); }
}
}
void JsonUIntCommonField_WriteTo_Test() {
JsonFieldsContainer container;
JsonCommonValue<unsigned int> testable1(&container, "test", 19);
JsonCommonValue<uint32_t> testable2(&container, "test", 20);
JsonCommonValue<uint16_t> testable3(&container, "test", 21);
JsonCommonValue<uint8_t> testable4(&container, "test", 22);
{
rapidjson::Document doc;
doc.SetObject();
testable1.WriteToDoc(&doc);
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
doc.Accept(writer);
if (strcmp(buffer.GetString(), "{\"test\":19}") == 0) { printf("111\n"); }
}
{
rapidjson::Document doc;
doc.SetObject();
testable2.WriteToDoc(&doc);
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
doc.Accept(writer);
if (strcmp(buffer.GetString(), "{\"test\":20}") == 0) { printf("222\n"); }
}
{
rapidjson::Document doc;
doc.SetObject();
testable3.WriteToDoc(&doc);
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
doc.Accept(writer);
if (strcmp(buffer.GetString(), "{\"test\":21}") == 0) { printf("333\n"); }
}
{
rapidjson::Document doc;
doc.SetObject();
testable4.WriteToDoc(&doc);
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
doc.Accept(writer);
if (strcmp(buffer.GetString(), "{\"test\":22}") == 0) { printf("444\n"); }
}
}
void JsonFloatField_Equals_Test() {
JsonFieldsContainer container;
JsonValue<float> testable1(&container, "test", 100.5);
JsonValue<float> testable01(&container, "test", 100.5);
JsonValue<double> testable2(&container, "test", 101.75);
JsonValue<double> testable02(&container, "test", 101.75);
if (testable1.Equals(&testable01)) { printf("111\n"); }
if (testable1 == testable01) { printf("111\n"); }
if (!(testable1 != testable01)) { printf("222\n"); }
testable01.Set(testable01.Get() + 1);
if (testable1 != testable01) { printf("333\n"); }
if (!(testable1 == testable01)) { printf("444\n"); }
if (testable2 == testable02) { printf("555\n"); }
if (!(testable2 != testable02)) { printf("777\n"); }
testable02.Set(testable02.Get() + 1);
if (testable2 != testable02) { printf("888\n"); }
if (!(testable2 == testable02)) { printf("999\n"); }
}
void JsonFloatCommonField_Equals_Test() {
JsonFieldsContainer container;
JsonCommonValue<float> testable1(&container, "test", 100.5);
JsonCommonValue<float> testable01(&container, "test", 100.5);
JsonCommonValue<double> testable2(&container, "test", 101.75);
JsonCommonValue<double> testable02(&container, "test", 101.75);
if (testable1.Equals(&testable01)) { printf("111\n"); }
if (testable1 == testable01) { printf("111\n"); }
if (!(testable1 != testable01)) { printf("222\n"); }
testable01.Set(testable01.Get() + 1);
if (testable1 != testable01) { printf("333\n"); }
if (!(testable1 == testable01)) { printf("444\n"); }
if (testable2 == testable02) { printf("555\n"); }
if (!(testable2 != testable02)) { printf("777\n"); }
testable02.Set(testable02.Get() + 1);
if (testable2 != testable02) { printf("888\n"); }
if (!(testable2 == testable02)) { printf("999\n"); }
}
void JsonStringValue_CloneTo_Test() {
JsonFieldsContainer container;
JsonValue<char *> testable1(&container, "test", "0123456789");
JsonValue<char *> clone1(&container, "test");
testable1.CloneTo((JsonValueBase *)&clone1);
testable1.Set("check the full data buffer is cloned");
if (clone1.Get() == "0123456789") { printf("1000\n"); }
}
void JsonStringCommonValue_CloneTo_Test() {
JsonFieldsContainer container;
JsonCommonValue<char *> testable1(&container, "test", "0123456789");
JsonCommonValue<char *> clone1(&container, "test");
testable1.CloneTo((JsonValueBase *)&clone1);
testable1.Set("check the full data buffer is cloned");
if (clone1.Get() == "0123456789") { printf("1000\n"); }
}
int main(const int argc, const char *argv[]) {
fprintf(stdout, "json to class object\n");
// {
JsonFieldsContainer container;
JsonValue<int> intObj(&container, "test", 100);
JsonCommonValue<int> intObj1(&container, "test", 100);
rapidjson::Document doc;
doc.Parse("{\"test\":153000}");
intObj.TryParse(&doc);
intObj1.TryParse(&doc);
JsonValue<int8_t> int8Obj(&container, "test", 100);
JsonCommonValue<int8_t> int8Obj1(&container, "test", 100);
rapidjson::Document doc8;
doc8.Parse("{\"test\":153000}");
int8Obj.TryParse(&doc8);
int8Obj1.TryParse(&doc8);
JsonValue<uint8_t> uint8Obj(&container, "test", 100);
JsonCommonValue<uint8_t> uint8Obj1(&container, "test", 100);
rapidjson::Document docu8;
docu8.Parse("{\"test\":153000}");
uint8Obj.TryParse(&doc8);
uint8Obj1.TryParse(&doc8);
JsonUIntField_WriteTo_Test();
JsonUIntCommonField_WriteTo_Test();
JsonFloatField_Equals_Test();
JsonFloatCommonField_Equals_Test();
JsonStringValue_CloneTo_Test();
JsonStringCommonValue_CloneTo_Test();
// auto res = intObj.TryStringParse("{\"test\":19}");
// printf("intObj.TryParse res:%d, val:%d\n", res, (int)intObj.Get());
// rapidjson::Document doc;
// doc.SetObject();
// intObj.WriteToDoc(&doc);
// rapidjson::StringBuffer buffer;
// rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
// doc.Accept(writer);
// const char *jsonStr = buffer.GetString();
// }
// {
// JsonFieldsContainer container;
// JsonValue<char *, false> strObj(&container, "testStr", "hello json");
// auto res = strObj.TryStringParse("{\"testStr\":\"0123456 abcdef\"}");
// printf("strObj.TryParse res:%d, val:%s\n", res, strObj.Get());
// rapidjson::Document doc;
// doc.SetObject();
// strObj.WriteToDoc(&doc);
// rapidjson::StringBuffer buffer;
// rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
// doc.Accept(writer);
// const char *jsonStr = buffer.GetString();
// }
// JsonStringField<(const char *)"testName", false, 256> testable1("test");
// JsonStringField testableString("testName", "test1");
// JsonNumericField<uint32_t> testableUint("testName", 100);
// std::cout << testableString.Name << " " << testableString.Get() << std::endl;
// std::cout << testableUint.Name << " " << testableUint.Get() << std::endl;
return EXIT_SUCCESS;
}