Skip to content

Commit d399be3

Browse files
committed
add json-parser
1 parent 3403828 commit d399be3

File tree

5 files changed

+158
-1
lines changed

5 files changed

+158
-1
lines changed

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,6 @@
109109
[submodule "thirdparty/boost"]
110110
path = thirdparty/boost
111111
url = https://github.com/boostorg/boost.git
112+
[submodule "thirdparty/json-parser"]
113+
path = thirdparty/json-parser
114+
url = https://github.com/json-parser/json-parser.git

src/cjsonlibs/json_parser_all.c

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include "../memorystat.h"
2+
3+
#include "json-parser/json.c"

src/tests/jsonparsertest.cpp

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
#include "../test.h"
2+
#include "json-parser/json.h"
3+
4+
#include <cassert>
5+
6+
7+
static void GenStat(Stat* s, const json_value* v) {
8+
printf("================");
9+
switch (v->type) {
10+
case json_object:
11+
// for (int i = 0; i< v->u.object.length; i++){
12+
13+
// }
14+
// for (json_value* child = v->u.object.values->value; child != 0; child = child->parent) {
15+
// GenStat(s, child);
16+
// ++s->memberCount;
17+
// }
18+
// ++s->objectCount;
19+
s->objectCount = v->u.object.length;
20+
break;
21+
22+
case json_array:
23+
for (int i = 0; i < v->u.array.length; ++i)
24+
GenStat(s, v->u.array.values[i]);
25+
s->elementCount += v->u.array.length;
26+
++s->arrayCount;
27+
break;
28+
29+
case json_string:
30+
++s->stringCount;
31+
s->stringLength += v->u.string.length;
32+
break;
33+
34+
case json_integer:
35+
case json_double:
36+
++s->numberCount;
37+
break;
38+
39+
case json_boolean:
40+
v->u.boolean ? ++s->trueCount: ++s->falseCount;
41+
break;
42+
43+
case json_null:
44+
++s->nullCount;
45+
break;
46+
47+
default:
48+
assert(false);
49+
}
50+
}
51+
52+
class JsonParserParseResult : public ParseResultBase {
53+
public:
54+
JsonParserParseResult() : value() {}
55+
~JsonParserParseResult() { json_value_free(value); }
56+
57+
json_value* value;
58+
};
59+
60+
class JsonParserStringResult : public StringResultBase {
61+
public:
62+
JsonParserStringResult() : s() {}
63+
~JsonParserStringResult() { free(s); }
64+
65+
virtual const char* c_str() const { return s; }
66+
67+
char* s;
68+
};
69+
70+
class JsonParserTest : public TestBase {
71+
public:
72+
#if TEST_INFO
73+
virtual const char* GetName() const { return "json-parser (C)"; }
74+
virtual const char* GetFilename() const { return __FILE__; }
75+
#endif
76+
77+
#if TEST_PARSE
78+
virtual ParseResultBase* Parse(const char* json, size_t length) const {
79+
(void)length;
80+
JsonParserParseResult* pr = new JsonParserParseResult;
81+
pr->value = json_parse(json, length);
82+
return pr;
83+
}
84+
#endif
85+
86+
87+
#ifdef TEST_STRINGIFY
88+
# undef TEST_STRINGIFY
89+
#endif
90+
91+
#define TEST_STRINGIFY 0
92+
#if TEST_STRINGIFY
93+
virtual StringResultBase* Stringify(const ParseResultBase* parseResult) const {
94+
const JsonParserParseResult* pr = static_cast<const JsonParserParseResult*>(parseResult);
95+
JsonParserStringResult* sr = new JsonParserStringResult;
96+
sr->s = cJSON_PrintUnformatted(pr->root);
97+
return sr;
98+
}
99+
#endif
100+
101+
#ifdef TEST_PRETTIFY
102+
# undef TEST_PRETTIFY
103+
#endif
104+
105+
#define TEST_PRETTIFY 0
106+
#if TEST_PRETTIFY
107+
virtual StringResultBase* Prettify(const ParseResultBase* parseResult) const {
108+
const JsonParserParseResult* pr = static_cast<const JsonParserParseResult*>(parseResult);
109+
JusonStringResult* sr = new JusonStringResult;
110+
sr->s = cJSON_Print(pr->root);
111+
return sr;
112+
}
113+
#endif
114+
115+
#if TEST_STATISTICS
116+
virtual bool Statistics(const ParseResultBase* parseResult, Stat* stat) const {
117+
const JsonParserParseResult* pr = static_cast<const JsonParserParseResult*>(parseResult);
118+
memset(stat, 0, sizeof(Stat));
119+
GenStat(stat, pr->value);
120+
return true;
121+
}
122+
#endif
123+
124+
#if TEST_CONFORMANCE
125+
virtual bool ParseDouble(const char* json, double* d) const {
126+
JsonParserParseResult pr;
127+
json_value* root = json_parse(json, strlen(json));
128+
if (root && root->type == json_array && root->u.array.length == 1 &&
129+
(root->u.array.values[0]->type == json_double)) {
130+
if(root->u.array.values[0]->type == json_double)
131+
*d = root->u.array.values[0]->u.dbl;
132+
return true;
133+
}
134+
else
135+
return false;
136+
}
137+
138+
virtual bool ParseString(const char* json, std::string& s) const {
139+
JsonParserParseResult pr;
140+
json_value* root = json_parse(json, strlen(json));
141+
if (root && root->type == json_array && root->u.array.length == 1 && root->u.array.values[0]->type == json_string) {
142+
s = std::string(root->u.array.values[0]->u.string.ptr, root->u.array.values[0]->u.string.length);
143+
return true;
144+
}
145+
else
146+
return false;
147+
}
148+
#endif
149+
};
150+
151+
REGISTER_TEST(JsonParserTest);

thirdparty/fastjson

-1
This file was deleted.

thirdparty/json-parser

Submodule json-parser added at 90f94ae

0 commit comments

Comments
 (0)