-
Notifications
You must be signed in to change notification settings - Fork 262
/
Copy pathsimplejsontest.cpp
175 lines (144 loc) · 4.82 KB
/
simplejsontest.cpp
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
#include "../test.h"
#ifdef _WIN32
#define WIN32
#endif
#include "simplejson/src/JSON.cpp"
#include "simplejson/src/JSONValue.cpp"
#include <clocale>
static void GenStat(Stat& stat, const JSONValue& v) {
if (v.IsArray()) {
const JSONArray& a = v.AsArray();
for (JSONArray::const_iterator itr = a.begin(); itr != a.end(); ++itr)
GenStat(stat, **itr);
stat.arrayCount++;
stat.elementCount += a.size();
}
else if (v.IsObject()) {
const JSONObject& o = v.AsObject();
for (JSONObject::const_iterator itr = o.begin(); itr != o.end(); ++itr) {
GenStat(stat, *itr->second);
stat.stringLength += itr->first.size();
}
stat.objectCount++;
stat.memberCount += o.size();
stat.stringCount += o.size();
}
else if (v.IsString()) {
stat.stringCount++;
stat.stringLength += v.AsString().size();
}
else if (v.IsNumber())
stat.numberCount++;
else if (v.IsBool()) {
if (v.AsBool())
stat.trueCount++;
else
stat.falseCount++;
}
else if (v.IsNull())
stat.nullCount++;
}
class SimplejsonParseResult : public ParseResultBase {
public:
SimplejsonParseResult() : root() {}
~SimplejsonParseResult() { delete root; }
JSONValue* root;
};
class SimplejsonStringResult : public StringResultBase {
public:
SimplejsonStringResult() : s() {}
~SimplejsonStringResult() { free(s); }
virtual const char* c_str() const { return s; }
char* s;
};
class SimplejsonTest : public TestBase {
public:
#if TEST_INFO
virtual const char* GetName() const { return "SimpleJSON (C++)"; }
virtual const char* GetFilename() const { return __FILE__; }
#endif
#if TEST_PARSE
virtual ParseResultBase* Parse(const char* json, size_t length) const {
(void)length;
SimplejsonParseResult* pr = new SimplejsonParseResult;
char* backupLocale = std::setlocale(LC_ALL, 0);
std::setlocale(LC_ALL, "en_US.UTF-8");
pr->root = JSON::Parse(json); // Use mcbstowcs() internally
std::setlocale(LC_ALL, backupLocale);
if (!pr->root) {
delete pr;
pr = 0;
}
return pr;
}
#endif
#if TEST_STRINGIFY
virtual StringResultBase* Stringify(const ParseResultBase* parseResult) const {
const SimplejsonParseResult* pr = static_cast<const SimplejsonParseResult*>(parseResult);
SimplejsonStringResult* sr = new SimplejsonStringResult;
std::wstring s = JSON::Stringify(pr->root);
// Doing reverse conversion as in JSON::Prase(const char*) with UTF8 locale
char* backupLocale = std::setlocale(LC_ALL, 0);
std::setlocale(LC_ALL, "en_US.UTF-8");
size_t length = s.size() * 2 + 1;
sr->s = (char*)malloc(length);
if (wcstombs(sr->s, s.c_str(), length) == (size_t)-1) {
delete sr;
sr = 0;
}
std::setlocale(LC_ALL, backupLocale);
return sr;
}
#endif
#if TEST_STATISTICS
virtual bool Statistics(const ParseResultBase* parseResult, Stat* stat) const {
const SimplejsonParseResult* pr = static_cast<const SimplejsonParseResult*>(parseResult);
memset(stat, 0, sizeof(Stat));
GenStat(*stat, *pr->root);
return true;
}
#endif
#if TEST_CONFORMANCE
virtual bool ParseDouble(const char* json, double* d) const {
SimplejsonParseResult pr;
char* backupLocale = std::setlocale(LC_ALL, 0);
std::setlocale(LC_ALL, "en_US.UTF-8");
pr.root = JSON::Parse(json); // Use mcbstowcs() internally
std::setlocale(LC_ALL, backupLocale);
if (pr.root &&
pr.root->IsArray() &&
pr.root->AsArray().size() == 1 &&
pr.root->AsArray()[0]->IsNumber())
{
*d = pr.root->AsArray()[0]->AsNumber();
return true;
}
else
return false;
}
virtual bool ParseString(const char* json, std::string& s) const {
SimplejsonParseResult pr;
char* backupLocale = std::setlocale(LC_ALL, 0);
std::setlocale(LC_ALL, "en_US.UTF-8");
pr.root = JSON::Parse(json); // Use mcbstowcs() internally
bool ret = false;
if (pr.root &&
pr.root->IsArray() &&
pr.root->AsArray().size() == 1 &&
pr.root->AsArray()[0]->IsString())
{
std::wstring ss = pr.root->AsArray()[0]->AsString();
size_t length = ss.size() * 2 + 1;
char *u = (char*)malloc(length);
if (wcstombs(u, ss.c_str(), length) != (size_t)-1) {
s = u;
ret = true;
}
free(u);
}
std::setlocale(LC_ALL, backupLocale);
return ret;
}
#endif
};
REGISTER_TEST(SimplejsonTest);