forked from microsoft/cpprestsdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSON.cpp
More file actions
559 lines (506 loc) · 13.1 KB
/
JSON.cpp
File metadata and controls
559 lines (506 loc) · 13.1 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
// JSON.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include "File.h"
#include <filesystem>
#include <iomanip>
#include <iostream>
#include <nlohmann/json.hpp>
// for convenience
using json = nlohmann::json;
#include <iostream>
//创建json对象
/*
{
"pi": 3.141,
"happy": true,
"name": "Niels",
"nothing": null,
"answer": {
"everything": 42
},
"list": [1, 0, 2],
"object": {
"currency": "USD",
"value": 42.99
}
}
*/
void createJson()
{
/*
{
"answer": {
"everyting": 42
},
"happy": true,
"list": [
1,
0,
2
],
"name": "Niels",
"nothing": null,
"object": {
"currency": "USD",
"value": 42.99
},
"pi": 3.141
}
*/
// empty structure null
json j;
// add a number that is stored as double (note the implicit conversion of j to an object)
j["pi"] = 3.141;
// add a boolean that is stored as bool
j["happy"] = true;
// add a string that is stored as std::string
j["name"] = "Niels";
// add another null object by passing nullptr
j["nothing"] = nullptr;
// add another null object by passing nullptr
j["nothing"] = nullptr;
// add an object inside the object
j["answer"]["everyting"] = 42;
// add an array that is stored as std::vector (using an initializer list)
j["list"] = {1, 0, 2};
// add another object ,using an initializer list of pairs
j["object"] = {{"currency", "USD"}, {"value", 42.99}};
/////////////////////////j2和j3都跟j不一样吧TODO
// https://blog.csdn.net/forrid/article/details/78867679
// instead ,you could also write (which looks very similar to the json above)
json j2 = {{"pi", 3.143},
{"happy", true},
{"NAME", "CHLL"},
{"NOTHING", nullptr},
{"ANSWER", {"EVERYTHINS"}, 42},
{"list", {1, 0, 2}},
{"object", {{"currency", "USD"}, {"value", 42.99}}}};
std::cout << std::setw(4) << j << std::endl;
std::cout << "=============================" << std::endl;
std::cout << std::setw(4) << j2 << std::endl;
json j3 = {"pi",
3.143,
"happy",
true,
"NAME",
"CHLL",
"NOTHING",
nullptr,
{"ANSWER", {"EVERYTHINS"}, 42},
"list",
{1, 0, 2},
"object",
{"currency", "USD", "value", 42.99}};
std::cout << "=============================" << std::endl;
std::cout << std::setw(4) << j3 << std::endl;
////////////序列化
// 从字符串字面量创建对象
/*
j4 {
"happy": true,
"pi": 3.141
}
*/
json j4 = "{\"happy\": true, \"pi\": 3.141}"_json;
// 最好是使用原始字符串字面量
/*
j5{
"happy": true,
"pi": 3.141
}
*/
auto j5 = R"({"happy": true, "pi": 3.141})"_json;
// 显式地分析
auto j6 = json::parse("{\"happy\": true, \"pi\": 3.141}");
std::cout << "j4 " << std::setw(4) << j4 << std::endl;
std::cout << "j5" << std::setw(4) << j5 << std::endl;
std::cout << "j6" << std::setw(4) << j6 << std::endl;
// 显式地转换至字符串
std::string s = j6.dump();
// 传入缩进空格数,使用良好的打印形式进行序列化
std::cout << j6.dump(4) << std::endl;
}
/*https://www.json.cn/
{
"project": "client-logs",
"region": "henzhen",
"logStore": "com-web",
"source": "127.0.0.1",
"encode" : 2,
"content":[{
"key1" : "%E5%86%85%E5%AE%B91",
"key2" : "%E5%86%AE%B91",
"key3" : "内容3"
},{
"key1" : "内容1",
"key2" : "内容2",
"key3" : "%E5%86%85%E5%AE%B93"
}]
}
*/
void create100()
{
std::cout << "=============================" << std::endl;
// json j = json::parse("{ \ "project \" : \ "client-logs \",\ "region \" : \"henzhen \",\"logStore \" :
// \"com-web \",\"source \" : \"127.0.0.1 \",\ "encode" : 2,}");
// auto j5 = R"({"happy": true, "pi": 3.141})"_json;
json raw = R"({
"project": "client-logs",
"region": "shenzhen",
"logStore": "com-web",
"source": "127.0.0.1",
"encode" : 2,
"content":[0,1,2]
})"_json;
// std::cout <<"comj:"<< std::setw(4) << j << std::endl;
std::cout << std::setw(4) << raw << std::endl;
}
void createarray()
{
std::cout << "=============================" << std::endl;
//创建一个空数组
json empty_array_explicit = json::array();
// 创建一个空对象的两种方式
json empty_object_implicit = json({});
json empty_object_explicit = json::object();
// array是一个数组,可以用数字直接下标访问。
json array = {"a", 6, "xin", 8};
std::cout << "array[0]" << std::setw(4) << array[0] << std::endl;
/*
// add an object inside the object
j["answer"]["everyting"] = 42;
// add an array that is stored as std::vector (using an initializer list)
j["list"] = {1, 0, 2};
// add another object ,using an initializer list of pairs
j["object"] = {{"currency", "USD"}, {"value", 42.99}};
*/
json j;
// add a number that is stored as double (note the implicit conversion of j to an object)
//会覆盖啊 TODO
/*
"content": {
"5": "5",
"6": 42.99,
"7": "8"
},
*/
j["content"] = {{"currency", "USD"}, {"value", 42.99}, {"nihao", "home"}};
j["content"] = {{"1", "2"}, {"3", 42.99}, {"4", "5"}};
j["content"] = {{"5", "5"}, {"6", 42.99}, {"7", "8"}};
j["module"] = "china";
std::cout << std::setw(4) << j << std::endl;
std::cout << "=============================" << std::endl;
//怎么变为了这个样子: "1content": " [ \"one\", \"two\", \"three\" ]", ??????TODO
j["1_content"] = R"( [ "one", "two", "three" ])";
/*
"2_content": "[\n { \"op\": \"replace\", \"path\": \"/baz\", \"value\": \"boo\" },\n { \"op\": \"add\",
\"path\": \"/hello\", \"value\": [\"world\"] },\n { \"op\": \"remove\", \"path\": \"/foo\"} \n]",
*/
j["2_content"] = R"([
{ "op": "replace", "path": "/baz", "value": "boo" },
{ "op": "add", "path": "/hello", "value": ["world"] },
{ "op": "remove", "path": "/foo"}
])";
/*
3 是这样的,看起来这个就符合要求
"3_content": [
{
"op": "replace",
"path": "/baz",
"value": "boo"
},
{
"op": "add",
"path": "/hello",
"value": [
"world"
]
},
{
"op": "remove",
"path": "/foo"
}
],
*/
//这个也符号要求,j["2_content"] 的方法可以直接转为json的字符串
j["3_content"] = json::parse(
"[ { \"op\": \"replace\", \"path\": \"/baz\", \"value\": \"boo\" },\n { \"op\": \"add\", \"path\": "
"\"/hello\", \"value\": [\"world\"] },\n { \"op\": \"remove\", \"path\": \"/foo\"} ]");
/*
GOOD!
厉害了,这个符合要求
"4_content": [
{
"currency": "USD",
"nihao": "home",
"value": 42.99
},
{
"5": "5",
"6": 42.99,
"7": "8"
},
{
"5": "5",
"6": 42.99,
"7": "8"
}
],
*/
json m1 = {{"currency", "USD"}, {"value", 42.99}, {"nihao", "home"}};
json m2 = {{"1", "2"}, {"3", 42.99}, {"4", "5"}};
json m3 = {{"5", "5"}, {"6", 42.99}, {"7", "8"}};
//这样符合要求
j["4_content"] = {m1, m3, m3};
std::cout << std::setw(4) << j << std::endl;
}
void creatPatch()
{
// a JSON value
json j_original = R"({
"baz": ["one", "two", "three"],
"foo": "bar"
})"_json;
// https://www.jianshu.com/p/69e57f2af904
// a JSON patch (RFC 6902)
json j_patch = R"([
{ "op": "replace", "path": "/baz", "value": "boo" },
{ "op": "add", "path": "/hello", "value": ["world"] },
{ "op": "remove", "path": "/foo"}
])"_json;
}
class Component
{
public:
Component(std::string type) { name = type; }
/*
{
"TIME": "china_2",
"Type": "1_china"
},
*/
void Serialize(json& outJson)
{
outJson["Type"] = std::to_string(++id) + "_" + name;
outJson["TIME"] = name + "_" + std::to_string(++id);
}
static int id;
private:
std::string name;
};
int Component::id = 0;
/*
d
[
{
"Components": [
{
"Type": "1_china"
},
{
"Type": "1_usa"
},
{
"Type": "1_english"
}
],
"Name": "zhangbin"
}
]
*/
void createVecor(json& d)
{
json thing;
thing["Name"] = "zhangbin";
json& componentsJson = thing["Components"];
std::vector<Component> sss;
sss.push_back(Component("china"));
sss.push_back(Component("usa"));
sss.push_back(Component("english"));
auto comps = sss;
for (auto comp : comps)
{
json compJson;
comp.Serialize(compJson);
/*
从vector来自动变为数组
[
{
"TIME": "china_2",
"Type": "1_china"
},
{
"TIME": "usa_4",
"Type": "3_usa"
},
{
"TIME": "english_6",
"Type": "5_english"
}
],
*/
componentsJson.push_back(compJson);
}
std::cout << "===========thing==================" << std::endl;
/*
//{
//"Components": [
//{
// "Type": "1_china"
//},
//{
// "Type": "2_usa"
//},
//{
// "Type": "3_english"
//}
//],
//"Name": "zhangbin"
//}
*/
std::cout << std::setw(4) << thing << std::endl;
d.push_back(thing);
d.push_back(thing);
}
void jsonR_parse(std::string json_string)
{
// json j_patch = R"([
// { "op": "replace", "path": "/baz", "value": "boo" },
// { "op": "add", "path": "/hello", "value": ["world"] },
// { "op": "remove", "path": "/foo"}
//])"_json;
auto jj = json::parse(json_string);
std::cout << std::setw(4) << jj << std::endl;
}
Path FilePath;
void save(std::string fileName, json& world)
{
// void Scene::Save(std::string fileName, Transform * root)
{
FilePath = Path(fileName);
File worldFile(FilePath);
worldFile.Write(world.dump(4));
std::cout << world.dump(4) << std::endl;
}
}
void strdump(std::string str) { std::cout << "#####:" << std::setw(4) << str << std::endl; }
void push(json& m)
{
json up;
up["project"] = "1111";
up["region"] = "222222";
up["logStore"] = "44444";
// TODO IP 地址获取
up["source"] = "127.0.0.1";
up["encode"] = "3333";
up["content"].push_back(m);
std::cout << std::setw(4) << up << std::endl;
strdump(up.dump());
}
void ELK(const std::string& jsonstr)
{
auto j = json::parse(jsonstr);
push(j);
}
#include "show_mac.h"
int main()
{
std::cout << "Hello World!\n";
createJson();
create100();
createarray();
std::cout << "=============================" << std::endl;
/*
[
{
"Components": [
{
"Type": "1_china"
},
{
"Type": "1_usa"
},
{
"Type": "1_english"
}
],
"Name": "zhangbin"
},
{
"Components": [
{
"Type": "1_china"
},
{
"Type": "1_usa"
},
{
"Type": "1_english"
}
],
"Name": "zhangbin"
}
]
*/
json ddd;
createVecor(ddd);
std::cout << std::setw(4) << ddd << std::endl;
json d;
d["content"] = ddd;
std::cout << "=============================" << std::endl;
/*
{
"content": [
{
"Components": [
{
"Type": "1_china"
},
{
"Type": "1_usa"
},
{
"Type": "1_english"
}
],
"Name": "zhangbin"
},
{
"Components": [
{
"Type": "1_china"
},
{
"Type": "1_usa"
},
{
"Type": "1_english"
}
],
"Name": "zhangbin"
}
]
}
*/
std::cout << std::setw(4) << d << std::endl;
// E:\netttttt\http\zhangbincpprestsdk\WIN32\Release下面,跟exe同目录
save("ssave.txt", d);
/////
std::cout << "show mac..............." << std::endl;
// showmac();
std::string str = R"({"traceId":"client_test_str", "spanId":"client_test_spanId"})";
jsonR_parse(str);
json m1 = {{"currency", "USD"}, {"value", 42.99}, {"nihao", "home"}};
ELK(m1.dump());
system("pause");
return 0;
}
// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单
// 入门使用技巧:
// 1. 使用解决方案资源管理器窗口添加/管理文件
// 2. 使用团队资源管理器窗口连接到源代码管理
// 3. 使用输出窗口查看生成输出和其他消息
// 4. 使用错误列表窗口查看错误
// 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
// 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件