Skip to content

Commit ce54b3e

Browse files
committed
Add ujson4c
1 parent 0888f39 commit ce54b3e

File tree

7 files changed

+110
-3
lines changed

7 files changed

+110
-3
lines changed

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@
2222
[submodule "thirdparty/casablanca"]
2323
path = thirdparty/casablanca
2424
url = https://git01.codeplex.com/casablanca
25+
[submodule "thirdparty/ujson4c"]
26+
path = thirdparty/ujson4c
27+
url = https://github.com/esnme/ujson4c.git

build/premake4.lua

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ solution "benchmark"
7575
includedirs {
7676
"../thirdparty/",
7777
"../thirdparty/include/",
78+
"../thirdparty/ujson4c/3rdparty/",
7879
}
7980

8081
files {

src/cjsonlibs/ujson4c.c

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

src/cjsonlibs/ultrajsondec.c

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

src/main.cpp

+11-3
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,23 @@ static void Verify(const TestBase& test, const TestJsonList& testJsons) {
180180
printf("\nNot support Statistics\n", itr->filename);
181181
failed = true;
182182
delete dom1;
183-
break;
183+
continue;
184184
}
185185

186186
StringResultBase* json1 = test.Stringify(dom1);
187187
delete dom1;
188188

189189
if (!json1) {
190-
printf("\nFailed to strinify '%s'\n", itr->filename);
191-
failed = true;
190+
// Some libraries may not support stringify, but still check statistics
191+
if (memcmp(&stat1, &itr->stat, sizeof(Stat)) != 0) {
192+
printf("\nStatistics of '%s' is different from reference.\n\n", itr->filename);
193+
printf("Reference\n---------\n");
194+
PrintStat(itr->stat);
195+
printf("\nStat 1\n--------\n");
196+
PrintStat(stat1);
197+
printf("\n");
198+
failed = true;
199+
}
192200
continue;
193201
}
194202

src/tests/ujson4c.cpp

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#include "../test.h"
2+
3+
#include "ujson4c/src/ujdecode.h"
4+
5+
extern "C" {
6+
7+
static void GenStat(Stat* s, UJObject v) {
8+
switch (UJGetType(v)) {
9+
case UJT_Object:
10+
{
11+
void* iter = UJBeginObject(v);
12+
UJString key;
13+
UJObject value;
14+
while (UJIterObject(&iter, &key, &value)) {
15+
s->memberCount++;
16+
s->stringCount++;
17+
s->stringLength += key.cchLen;
18+
GenStat(s, value);
19+
}
20+
s->objectCount++;
21+
}
22+
break;
23+
24+
case UJT_Array:
25+
{
26+
void* iter = UJBeginArray(v);
27+
UJObject value;
28+
while (UJIterArray(&iter, &value)) {
29+
GenStat(s, value);
30+
s->elementCount++;
31+
}
32+
s->arrayCount++;
33+
}
34+
break;
35+
36+
case UJT_String:
37+
{
38+
size_t length;
39+
UJReadString(v, &length);
40+
s->stringCount++;
41+
s->stringLength += length;
42+
}
43+
break;
44+
45+
case UJT_Long:
46+
case UJT_LongLong:
47+
case UJT_Double:
48+
s->numberCount++; break;
49+
50+
case UJT_True: s->trueCount++; break;
51+
case UJT_False: s->falseCount++; break;
52+
case UJT_Null: s->nullCount++; break;
53+
default:;
54+
}
55+
}
56+
57+
} // extern "C"
58+
59+
class Ujson4cParseResult : public ParseResultBase {
60+
public:
61+
Ujson4cParseResult() : root(), state() {}
62+
~Ujson4cParseResult() { UJFree(state); }
63+
64+
UJObject root;
65+
void* state;
66+
};
67+
68+
class Ujson4c : public TestBase {
69+
public:
70+
Ujson4c() : TestBase("Ujson4c") {
71+
}
72+
73+
virtual ParseResultBase* Parse(const char* json, size_t length) const {
74+
(void)length;
75+
Ujson4cParseResult* pr = new Ujson4cParseResult;
76+
pr->root = UJDecode(json, length, NULL, &pr->state);
77+
return pr;
78+
}
79+
80+
virtual bool Statistics(const ParseResultBase* parseResult, Stat* stat) const {
81+
const Ujson4cParseResult* pr = static_cast<const Ujson4cParseResult*>(parseResult);
82+
memset(stat, 0, sizeof(Stat));
83+
GenStat(stat, pr->root);
84+
return true;
85+
}
86+
};
87+
88+
REGISTER_TEST(Ujson4c);

thirdparty/ujson4c

Submodule ujson4c added at e14f3fd

0 commit comments

Comments
 (0)