25 JSONUtil::JSONUtil() {
30 JSONUtil::~JSONUtil() {
34 void JSONUtil::array(
string& json,
JSONElement* element)
36 while(json.find(
"[")!=string::npos && json.find(
"]")!=string::npos)
38 element->setType(JSONElement::JSON_ARRAY);
39 StringUtil::replaceFirst(json,
"[",
"");
40 StringUtil::replaceLast(json,
"]",
"");
41 readJSON(json,
true,element);
45 void JSONUtil::object(
string& json,
JSONElement* element)
47 while(json.find(
"{")!=string::npos && json.find(
"}")!=string::npos)
49 element->setType(JSONElement::JSON_OBJECT);
50 StringUtil::replaceFirst(json,
"{",
"");
51 StringUtil::replaceLast(json,
"}",
"");
52 readJSON(json,
false,element);
56 void JSONUtil::arrayOrObject(
string& json,
JSONElement* element)
58 while((json.find(
"{")!=string::npos && json.find(
"}")!=string::npos)
59 || (json.find(
"[")!=string::npos && json.find(
"]")!=string::npos))
61 int arrst = json.find(
"[");
62 int objst = json.find(
"{");
63 if(json.find(
"{")!=string::npos && json.find(
"}")!=string::npos && (objst<arrst || arrst==(int)string::npos))
65 element->setType(JSONElement::JSON_OBJECT);
66 StringUtil::replaceFirst(json,
"{",
"");
67 StringUtil::replaceLast(json,
"}",
"");
68 readJSON(json,
false,element);
70 else if(json.find(
"[")!=string::npos && json.find(
"]")!=string::npos)
72 element->setType(JSONElement::JSON_ARRAY);
73 StringUtil::replaceFirst(json,
"[",
"");
74 StringUtil::replaceLast(json,
"]",
"");
75 readJSON(json,
true,element);
80 void JSONUtil::readJSON(
string& json,
bool isarray,
JSONElement *par)
87 size_t stn = json.find(
"\"");
88 while(stn!=string::npos && stn>0 && json.at(stn-1)==
'\\')
90 stn = json.find(
"\"", stn+1);
97 throw (
"invalid json - no start '\"' found for name parameter");
98 size_t enn = json.find(
"\"", stn+1);
99 while(enn!=string::npos && enn>0 && json.at(enn-1)==
'\\')
101 enn = json.find(
"\"", enn+1);
107 if(enn==string::npos)
108 throw (
"invalid json - no end '\"' found for name parameter");
110 name = json.substr(stn+1, (enn-stn-1));
112 json = json.substr(enn+1);
113 StringUtil::trim(json);
114 size_t vst = json.find(
":");
115 if(vst==string::npos)
116 throw (
"invalid json - no ':' found");
118 throw (
"invalid json - invalid json - invalid string before ':' found");
119 json = json.substr(vst+1);
122 element->setName(name);
124 StringUtil::trim(json);
125 size_t env = json.find(
",");
126 size_t obs = json.find(
"{");
127 size_t ars = json.find(
"[");
130 size_t sss = json.find(
"{", obs+1);
131 size_t eee = json.find(
"}", obs);
137 sss = json.find(
"{", sss+1);
138 if(sss!=string::npos)
140 eee = json.find(
"}", sss);
142 if(eee!=string::npos)
145 if(eee!=string::npos && json.find(
"}", eee+1)!=string::npos)
146 obe = json.find(
"}", eee+1);
148 value = json.substr(obs, obe-obs+1);
149 json = json.substr(obe+1);
150 element->setType(JSONElement::JSON_OBJECT);
154 size_t sss = json.find(
"[", ars+1);
155 size_t eee = json.find(
"]", ars);
161 sss = json.find(
"[", sss+1);
162 if(sss!=string::npos)
164 eee = json.find(
"]", sss);
166 if(eee!=string::npos)
169 if(eee!=string::npos && json.find(
"]", eee+1)!=string::npos)
170 are = json.find(
"]", eee+1);
172 value = json.substr(ars, are-ars+1);
173 json = json.substr(are+1);
174 element->setType(JSONElement::JSON_ARRAY);
176 else if(env==string::npos)
180 element->setType(JSONElement::JSON_STRING);
184 if(obs!=string::npos && env==0 && (obs<ars || ars==string::npos))
186 size_t sss = json.find(
"{", obs+1);
187 size_t eee = json.find(
"}", obs);
193 sss = json.find(
"{", sss+1);
194 if(sss!=string::npos)
196 eee = json.find(
"}", sss);
198 if(eee!=string::npos)
201 if(eee!=string::npos && json.find(
"}", eee+1)!=string::npos)
202 obe = json.find(
"}", eee+1);
204 value = json.substr(obs, obe-obs+1);
205 json = json.substr(obe+1);
206 element->setType(JSONElement::JSON_OBJECT);
208 else if(ars!=string::npos && env==0 && (ars<obs || obs==string::npos))
210 size_t sss = json.find(
"[", ars+1);
211 size_t eee = json.find(
"]", ars);
217 sss = json.find(
"[", sss+1);
218 if(sss!=string::npos)
220 eee = json.find(
"]", sss);
222 if(eee!=string::npos)
225 if(eee!=string::npos && json.find(
"]", eee+1)!=string::npos)
226 are = json.find(
"]", eee+1);
228 value = json.substr(ars, are-ars+1);
229 json = json.substr(are+1);
230 element->setType(JSONElement::JSON_ARRAY);
232 else if(obs!=string::npos && obs<env && (obs<ars || ars==string::npos))
234 size_t sss = json.find(
"{", obs+1);
235 size_t eee = json.find(
"}", obs);
241 sss = json.find(
"{", sss+1);
242 if(sss!=string::npos)
244 eee = json.find(
"}", sss);
246 if(eee!=string::npos)
249 if(eee!=string::npos && json.find(
"}", eee+1)!=string::npos)
250 obe = json.find(
"}", eee+1);
252 value = json.substr(obs, obe-obs+1);
253 json = json.substr(obe+1);
254 element->setType(JSONElement::JSON_OBJECT);
256 else if(ars!=string::npos && ars<env && (ars<obs || obs==string::npos))
258 size_t sss = json.find(
"[", ars+1);
259 size_t eee = json.find(
"]", ars);
265 sss = json.find(
"[", sss+1);
266 if(sss!=string::npos)
268 eee = json.find(
"]", sss);
270 if(eee!=string::npos)
273 if(eee!=string::npos && json.find(
"]", eee+1)!=string::npos)
274 are = json.find(
"]", eee+1);
276 value = json.substr(ars, are-ars+1);
277 json = json.substr(are+1);
278 element->setType(JSONElement::JSON_ARRAY);
282 value = json.substr(0, env);
283 json = json.substr(env+1);
284 element->setType(JSONElement::JSON_STRING);
289 string ex =
"invalid json - no value object found for name "+ name;
292 if(element->getType()!=JSONElement::JSON_OBJECT && element->getType()!=JSONElement::JSON_ARRAY)
294 validateSetValue(element, value);
296 par->addChild(element);
297 if(element->getType()==JSONElement::JSON_OBJECT)
298 object(value, element);
299 else if(element->getType()==JSONElement::JSON_ARRAY)
300 array(value, element);
301 readJSON(json,isarray,par);
304 void JSONUtil::validateSetValue(
JSONElement* element,
string value)
306 StringUtil::trim(value);
307 size_t stn = value.find(
"\"");
308 if(stn!=string::npos)
310 while(stn!=string::npos && stn>0 && value.at(stn-1)==
'\\')
312 stn = value.find(
"\"", stn+1);
318 size_t enn = value.find(
"\"", stn+1);
319 while(enn!=string::npos && enn>0 && value.at(enn-1)==
'\\')
321 enn = value.find(
"\"", enn+1);
327 if(enn==string::npos)
329 string ex =
"invalid json - invalid string object '"+value+
"' found for name "+ element->getName();
332 else if(enn!=value.length()-1)
334 string ex =
"invalid json - invalid literal found after string object '"+value+
"' for name "+ element->getName();
339 value = value.substr(stn+1, (enn-stn-1));
345 element->setType(JSONElement::JSON_STRING);
347 else if(StringUtil::toLowerCopy(value)==
"true" || StringUtil::toLowerCopy(value)==
"false")
349 value = StringUtil::toLowerCopy(value);
350 element->setType(JSONElement::JSON_BOOL);
352 else if(value.find(
".")!=string::npos)
356 CastUtil::lexical_cast<
double>(value);
357 }
catch (
const char* ex) {
358 string exp =
"invalid json - invalid double value "+value+
" found for name "+ element->getName();
361 element->setType(JSONElement::JSON_FLOAT);
367 CastUtil::lexical_cast<
int>(value);
368 }
catch (
const char* ex) {
371 CastUtil::lexical_cast<
long>(value);
372 }
catch (
const char* ex) {
375 CastUtil::lexical_cast<
long long>(value);
376 }
catch (
const char* ex) {
377 string exp =
"invalid json - invalid numeric value "+value+
" found for name "+ element->getName();
382 element->setType(JSONElement::JSON_NUMBER);
384 element->setValue(value);
387 JSONElement JSONUtil::getDocument(
const string& jsonTxt)
389 string json(jsonTxt);
391 root.setType(JSONElement::JSON_OBJECT);
392 root.setName(
"_JSON_ROOT");
393 int arrst = json.find(
"[");
394 int objst = json.find(
"{");
395 if(json.find(
"{")!=string::npos && json.find(
"}")!=string::npos && (objst<arrst || arrst==(int)string::npos))
397 root.setType(JSONElement::JSON_OBJECT);
398 StringUtil::replaceFirst(json,
"{",
"");
399 StringUtil::replaceLast(json,
"}",
"");
400 readJSON(json,
false,&root);
402 else if(json.find(
"[")!=string::npos && json.find(
"]")!=string::npos)
404 root.setType(JSONElement::JSON_ARRAY);
405 StringUtil::replaceFirst(json,
"[",
"");
406 StringUtil::replaceLast(json,
"]",
"");
407 readJSON(json,
true,&root);
415 if(doc.getType()==JSONElement::JSON_OBJECT)
419 if(doc.hasChildren())
421 for (
int var = 0; var < (int)doc.getChildren().size(); ++var) {
423 if(doc.getType()==JSONElement::JSON_OBJECT)
424 jsonText +=
"\"" + child->getName() +
"\":";
425 if(child->getType()==JSONElement::JSON_OBJECT || child->getType()==JSONElement::JSON_ARRAY)
427 jsonText += getDocumentStr(*child);
431 if(child->getType()==JSONElement::JSON_STRING)
432 jsonText +=
"\"" + child->getValue() +
"\"";
434 jsonText += child->getValue();
436 if(var!=(
int)doc.getChildren().size()-1)
442 if(doc.getType()==JSONElement::JSON_OBJECT)