forked from dongyusheng/csdn-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsoncpp_speed.cpp
More file actions
356 lines (300 loc) · 9.26 KB
/
Copy pathjsoncpp_speed.cpp
File metadata and controls
356 lines (300 loc) · 9.26 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
//https://github.com/dongyusheng/csdn-code/tree/master/jsoncpp
//测试jsoncpp的使用方法和性能
#include <memory>
#include <sstream>
#include <iostream>
#include <stdint.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/wait.h>
#include "jsoncpp/json/json.h"
#include "jsoncpp/json/value.h"
#include "jsoncpp/json/reader.h"
#include "jsoncpp/json/writer.h"
/*
{
"name": "milo",
"age": 80,
"languages": ["C++", "C"],
"phone": {
"number": "186****3143",
"type": "home"
},
"books":[
{
"name": "Linux kernel development",
"price":7.7
},
{
"name": "Linux server development",
"price": 8.0
}
],
"vip":true,
"address": null
}
*/
static uint64_t getNowTime()
{
struct timeval tval;
uint64_t nowTime;
gettimeofday(&tval, NULL);
nowTime = tval.tv_sec * 1000L + tval.tv_usec / 1000L;
return nowTime;
}
std::string JsoncppEncodeNew()
{
std::string jsonStr;
// 一个value是可以包含多个键值对
Json::Value root, languages, phone, book, books;
// 姓名
root["name"] = "milo";
// 年龄
root["age"] = 80;
// 语言
languages[0] = "C++";
languages[1] = "Java";
root["languages"] = languages;
// 电话
phone["number"] = "186****3143";
phone["type"] = "home";
root["phone"] = phone;
// 书籍
book["name"] = "Linux kernel development";
book["price"] = 7.7;
books[0] = book; // 深拷贝
book["name"] = "Linux server development";
book["price"] = 8.0;
books[1] = book; // 深拷贝
root["books"] = books;
// 是否为vip
root["vip"] = true;
// address信息空null
root["address"] = "yageguoji";
Json::StreamWriterBuilder writerBuilder;
std::ostringstream os;
std::unique_ptr<Json::StreamWriter> jsonWriter(writerBuilder.newStreamWriter());
jsonWriter->write(root, &os);
jsonStr = os.str();
// std::cout << "Json:\n" << jsonStr << std::endl;
return jsonStr;
}
std::string JsoncppEncodeOld()
{
std::string jsonStr;
Json::Value root, languages, phone, book, books;
// 姓名
root["name"] = "milo";
//root["name"] = Json::nullValue;
// 年龄
root["age"] = 80;
// 语言
languages[0] = "C++";
languages[1] = "Java";
root["languages"] = languages;
// 电话
phone["number"] = "186****3143";
phone["type"] = "home";
root["phone"] = phone;
// 书籍
book["name"] = "Linux kernel development";
book["price"] = (float)7.7;
books[0] = book;
book["name"] = "Linux server development";
book["price"] = (float)8.0;
books[1] = book;
root["books"] = books;
// 是否为vip
root["vip"] = true;
// address信息空null
root["address"] = "yageguoji"; //;// Json::nullValue; // 如果是null,则要用自定义的Json::nullValue,不能用NULL
Json::FastWriter writer;
jsonStr = writer.write(root);
// std::cout << "Json:\n" << jsonStr << std::endl;
return jsonStr;
}
// 不能返回引用
Json::Value JsoncppEncodeOldGet()
{
Json::Value root;
Json::Value languages, phone, book, books;
// 姓名
root["name"] = "milo";
//root["name"] = Json::nullValue;
// 年龄
root["age"] = 80;
// 语言
languages[0] = "C++";
languages[1] = "Java";
root["languages"] = languages;
// 电话
phone["number"] = "186****3143";
phone["type"] = "home";
root["phone"] = phone;
// 书籍
book["name"] = "Linux kernel development";
book["price"] = (float)7.7;
books[0] = book;
book["name"] = "Linux server development";
book["price"] = (float)8.0;
books[1] = book;
root["books"] = books;
// 是否为vip
root["vip"] = true;
// address信息空null
root["address"] = Json::nullValue; //"yageguoji";// Json::nullValue; // 如果是null,则要用自定义的Json::nullValue,不能用NULL
std::cout << "JsoncppEncodeOldGet:\n" << std::endl;
return root;
}
void printJsoncpp(Json::Value &root)
{
if(root["name"].isNull())
{
std::cout << "name null\n";
}
std::cout << "name: " << root["name"].asString() << std::endl;
std::cout << "age: " << root["age"].asInt() << std::endl;
Json::Value &languages = root["languages"];
std::cout << "languages: ";
for (uint32_t i = 0; i < languages.size(); ++i)
{
if (i != 0)
{
std::cout << ", ";
}
std::cout << languages[i].asString();
}
std::cout << std::endl;
Json::Value &phone = root["phone"];
std::cout << "phone number: " << phone["number"].asString() << ", type: " << phone["type"].asString() << std::endl;
Json::Value &books = root["books"];
for (uint32_t i = 0; i < books.size(); ++i)
{
Json::Value &book = books[i];
std::cout << "book name: " << book["name"].asString() << ", price: " << book["price"].asFloat() << std::endl;
}
std::cout << "vip: " << root["vip"].asBool() << std::endl;
if (!root["address"].isNull())
{
std::cout << "address: " << root["address"].asString() << std::endl;
}
else
{
std::cout << "address is null" << std::endl;
}
}
bool JsoncppDecodeNew(const std::string &info)
{
if (info.empty())
return false;
bool res;
JSONCPP_STRING errs;
Json::Value root;
Json::CharReaderBuilder readerBuilder;
std::unique_ptr<Json::CharReader> const jsonReader(readerBuilder.newCharReader());
res = jsonReader->parse(info.c_str(), info.c_str() + info.length(), &root, &errs);
if (!res || !errs.empty())
{
std::cout << "parseJson err. " << errs << std::endl;
return false;
}
// printJsoncpp(root);
return true;
}
bool JsoncppDecodeOld(const std::string &strJson)
{
if (strJson.empty())
return false;
bool res;
Json::Value root;
Json::Reader jsonReader;
res = jsonReader.parse(strJson, root);
if (!res)
{
std::cout << "jsonReader.parse err. " << std::endl;
return false;
}
// printJsoncpp(root);
return true;
}
const char *strCjson = "{ \
\"name\": \"milo\", \
\"age\": 80, \
\"languages\": [\"C++\", \"C\"],\
\"phone\": { \
\"number\": \"186****3143\", \
\"type\": \"home\" \
}, \
\"books\": [{ \
\"name\": \"Linux kernel development\", \
\"price\": 7.700000 \
}, { \
\"name\": \"Linux server development\", \
\"price\": 8 \
}], \
\"vip\": true, \
\"address\": null \
} \
";
#define TEST_COUNT 100000
int main()
{
std::string jsonStrNew;
std::string jsonStrOld;
jsonStrNew = JsoncppEncodeNew();
// JsoncppEncodeNew size: 337
std::cout << "JsoncppEncodeNew size: " << jsonStrNew.size() << std::endl;
std::cout << "JsoncppEncodeNew string: " << jsonStrNew << std::endl;
JsoncppDecodeNew(jsonStrNew);
jsonStrOld = JsoncppEncodeOld();
// JsoncppEncodeOld size: 248
std::cout << "\n\nJsoncppEncodeOld size: " << jsonStrOld.size() << std::endl;
std::cout << "JsoncppEncodeOld string: " << jsonStrOld << std::endl;
JsoncppDecodeOld(jsonStrOld);
Json::Value root = JsoncppEncodeOldGet();
Json::FastWriter writer;
std::cout << "writer:\n" << std::endl;
std::string jsonStr = writer.write(root);
std::cout << "\n\njsonStr string: " << jsonStrOld << std::endl;
#if 1
uint64_t startTime;
uint64_t nowTime;
startTime = getNowTime();
std::cout << "jsoncpp encode time testing" << std::endl;
for(int i = 0; i < TEST_COUNT; i++)
{
JsoncppEncodeNew();
}
nowTime = getNowTime();
std::cout << "jsoncpp encode " << TEST_COUNT << " time, need time: "
<< nowTime-startTime << "ms" << std::endl;
startTime = getNowTime();
std::cout << "\njsoncpp encode time testing" << std::endl;
for(int i = 0; i < TEST_COUNT; i++)
{
JsoncppEncodeOld();
}
nowTime = getNowTime();
std::cout << "jsoncpp encode " << TEST_COUNT << " time, need time: "
<< nowTime-startTime << "ms" << std::endl;
startTime = getNowTime();
std::cout << "\njsoncpp decode time testing" << std::endl;
for(int i = 0; i < TEST_COUNT; i++)
{
JsoncppDecodeNew(jsonStrNew);
}
nowTime = getNowTime();
std::cout << "jsoncpp decode " << TEST_COUNT << " time, need time: "
<< nowTime-startTime << "ms" << std::endl;
startTime = getNowTime();
std::cout << "\njsoncpp decode time testing" << std::endl;
for(int i = 0; i < TEST_COUNT; i++)
{
JsoncppDecodeOld(jsonStrNew);
}
nowTime = getNowTime();
std::cout << "jsoncpp decode " << TEST_COUNT << " time, need time: "
<< nowTime-startTime << "ms" << std::endl;
#endif
return 0;
}