-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
executable file
·337 lines (266 loc) · 9.51 KB
/
Copy pathtest.cpp
File metadata and controls
executable file
·337 lines (266 loc) · 9.51 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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include <iostream>
#include "jsonw.hpp"
using namespace octillion;
// show how to read json from buffer contains utf8 data
void read_json_from_utf8_data();
// show how to read json from buffer contains usc (wchar_t) data
void read_json_from_usc_data();
// show how to read json from utf8 file
void read_json_from_utf8_file();
// show how to create json as below programatically
// {
// "txt1": "some text1",
// "num1": 123,
// "array":
// [
// true,
// {
// "null": null,
// "num2": 0.456
// }
// ]
// }
void create_json_programatically();
// show how to get the value from json text and work with it
void how_to_work_with_value();
// show how to work with object
void how_to_work_with_object();
// show how to work with array
void how_to_work_with_array();
// memory management - avoiding deep copy to save memory
void how_to_avoid_deep_copy();
int main()
{
read_json_from_utf8_data();
read_json_from_usc_data();
read_json_from_utf8_file();
create_json_programatically();
how_to_work_with_value();
how_to_work_with_object();
how_to_work_with_array();
how_to_avoid_deep_copy();
// see README.md for the memory leak detection
#ifdef OCTILLION_JSONW_ENABLE_MEMORY_LEAK_DETECTION
octillion::JsonW::memory_leak_detect_result();
#endif
return 0;
}
void read_json_from_utf8_data()
{
// literal 'u8' is not necessary if compiler uses utf8 as default
char jsondata[] = u8"{\"name\":\"meowyih\",\"age\":123}";
// initialize JsonTextW object using jsondata
JsonW json(jsondata);
// json in utf8
std::cout << json << std::endl;
}
void read_json_from_usc_data()
{
// literal 'u8' is not necessary if compiler uses utf8 as default
wchar_t jsondata[] = L"{\"name\":\"meowyih\",\"age\":123}";
// initialize JsonTextW object using jsondata
JsonW json(jsondata);
// json in utf8
std::cout << json << std::endl;
}
void read_json_from_utf8_file()
{
// 'sample.json' must utf-8 format without BOM
std::string jsonfile = "sample.json";
// initialize JsonTextW object using ifstream
std::ifstream fin(jsonfile);
if (!fin.good())
{
std::wcout << L"bad ifstream" << std::endl;
return;
}
// initialize JsonTextW object using fin
JsonW json(fin);
// json in utf8
std::cout << json << std::endl;
}
// show how to create json as below programatically
// {
// "txt1": "some text1",
// "num1": 123,
// "array":
// [
// true,
// {
// "txt2": null,
// "num2": 0.456
// }
// ]
// }
void create_json_programatically()
{
// create two JsonW object
JsonW jobject1, jobject2;
jobject1[u8"txt1"] = "some text1";
jobject1[u8"num1"] = 123;
jobject2[u8"null"]; // default JsonW value is NULL
// do no use 'json = NULL' this macro
// '#define NULL 0' is standard in compiler
jobject2[u8"num2"] = 0.456;
// create the array
JsonW jarray;
jarray[0] = true;
jarray[1] = jobject2;
// put array in jobject1
jobject1[u8"array"] = jarray;
// display the json text in utf8
std::cout << jobject1 << std::endl;
}
void how_to_work_with_value()
{
// jvalue is a null as default
JsonW jvalue;
// jvalue is a number
jvalue = (short)123;
jvalue = 123;
jvalue = (long)123;
jvalue = (long long)123;
jvalue = (float)4.56;
jvalue = 4.56;
jvalue = (long double)4.56;
// jvalue is a string
jvalue = "utf8 c-string";
jvalue = L"ucs c-string";
jvalue = std::string("utf8 string");
jvalue = std::wstring(L"utf8 string");
// jvalue is a boolean
jvalue = true;
// jvalue is an json object, which is also another JsonW object
JsonW jobject(u8"{\"name\":\"meowyih\",\"age\":123}");
jvalue = jobject;
// jvalue is a json array, which is also another JsonW object
JsonW jarray(u8"[1,2,3,4,5]");
jvalue = jarray;
// check jvalue's type and retrieve the data
switch (jvalue.type())
{
// error type
case JsonW::BAD:
std::cout << "jvalue is an BAD type" << std::endl;
break;
// jvalue is an json object, check "working with object" for detail
case JsonW::OBJECT:
std::cout << "jvalue is an json object" << std::endl;
break;
// jvalue is an json array, check "working with array" for detail
case JsonW::ARRAY:
std::cout << "jvalue is an json array" << std::endl;
break;
// jvalue is an integer number
case JsonW::INTEGER:
std::cout << "jvalue is an integer " << jvalue.integer() << std::endl;
break;
// jvalue is an floating point number
case JsonW::FLOAT:
std::cout << "jvalue is an integer " << jvalue.frac() << std::endl;
break;
// jvalue is a string, JsonW provides both utf8 and ucs string format
case JsonW::STRING:
std::cout << "jvalue is an string, utf8:" << jvalue.str() << std::endl;
std::wcout << L"jvalue is an string, ucs:" << jvalue.wstr() << std::endl;
break;
// jvalue is a boolean
case JsonW::BOOLEAN:
std::cout << "jvalue is a boolean " << jvalue.boolean() << std::endl;
break;
case JsonW::NULLVALUE:
std::cout << "jvalue is null" << std::endl;
}
// get json format string
std::string json_utf8 = jvalue.text();
std::wstring json_ucs = jvalue.wtext();
// overloading ostream << operator
std::cout << "json format:" << jvalue << std::endl;
}
void how_to_work_with_object()
{
// create a test json text using ascii (utf8) string
octillion::JsonW jobject(u8"{\"last\":\"Lee\",\"first\":\"Peter\"}");
// check if json is valid
if (jobject.valid() == false)
{
std::cout << "error: invalid json" << std::endl;
return;
}
// check if json has object
if (jobject.type() != octillion::JsonW::OBJECT)
{
std::cout << "error: json is not json object" << std::endl;
return;
}
std::cout << "json object contains " << jobject.size()
<< " name-pair value(s)" << std::endl;
// list all keys in object in std::wstring type and std::string type
std::vector<std::string> keys;
jobject.keys(keys);
// display name/value pair in ucs (wchar_t), normally programmer should
// check the value type before handling it, but for demo purpose, we
// 'magically' knows it is string type since it was created by our own.
for (size_t i = 0; i < keys.size(); i++)
{
std::string key = keys.at(i);
std::string value = jobject[key].str();
std::cout << "key-" << i << ":" << key << " value:" << value << std::endl;
}
// earse some value and see the result
jobject.erase(u8"last");
std::cout << jobject << std::endl;
}
void how_to_work_with_array()
{
// create a test json text using ascii (utf8) string
octillion::JsonW jarray(u8"[12,13,-42,20]");
std::cout << "json array contains " << jarray.size() << " value(s) in it" << std::endl;
// list each value (again, for demo purpose, we 'magically' know all value in are integer)
for (size_t i = 0; i < jarray.size(); i++)
{
std::cout << "item-" << i << ":" << jarray[i].integer() << std::endl;
}
// erase some value and see the result
jarray.erase(0); // be careful, after erase, the size of jarray became 3
jarray.erase(1); // be careful, after erase, the size of jarray became 2
std::cout << jarray << std::endl;
}
// memory management - avoiding deep copy to save memory
void how_to_avoid_deep_copy()
{
JsonW json, jobject, jarray;
jarray[1] = 10; // JsonW automatically assign NULL to jarray[0]
jobject["data"] = "data";
// deep copy jarray into jobject
jobject["array"] = jarray;
// deep copy jobject (as well as jarray) into json
json["object"] = jobject;
std::cout << "json:" << json << std::endl;
// it is not a problem when data size is small, however,
// if JsonW contains huge data, says 100MB. Deep copy might
// be a problem if the memory usage is a concern.
// If caller has such concern, do not use assignment
// operaotr (i.e. '='). Use size(JsonW::OBJECT)/get()/add()/keys() for json object
// and size(JsonW::ARRAY)/get()/add() for json array
JsonW *p_json, *p_object, *p_jarray;
p_jarray = new JsonW();
// null is a standard json string, see README.md for detail
p_jarray->add(new JsonW("null"));
// 10 is a standard json string, see README.md for detail
p_jarray->add(new JsonW("10"));
p_object = new JsonW();
// "data" is a standard json string, see README.md for detail
p_object->add("data", new JsonW("\"data\""));
// assign p_jarray into p_object, it is not deep copy.
// p_object just copy the address of p_jarray
p_object->add("array", p_jarray);
p_json = new JsonW();
// assign p_object into p_json, it is not deep copy.
// p_json just copy the address of p_object
p_json->add("object", p_object);
std::cout << "p_json:" << p_json->text() << std::endl;
// when delete the p_json, all the JsonW objects in it
// would be deleted, includes p_jobject and p_jarray.
delete p_json;
}