JSON.cpp 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  1. #include "JSON.h"
  2. #include "Datei.h"
  3. using namespace Framework;
  4. using namespace JSON;
  5. JSONValue::JSONValue()
  6. : ReferenceCounter()
  7. {
  8. this->type = JSONType::NULL_;
  9. }
  10. JSONValue::~JSONValue()
  11. {}
  12. JSONValue::JSONValue(JSONType type)
  13. : ReferenceCounter()
  14. {
  15. this->type = type;
  16. }
  17. JSONType JSONValue::getType() const
  18. {
  19. return type;
  20. }
  21. Text JSONValue::toString() const
  22. {
  23. return Text("null");
  24. }
  25. JSONValue* JSONValue::clone() const
  26. {
  27. return new JSONValue();
  28. }
  29. JSONBool* JSONValue::asBool() const
  30. {
  31. return (JSONBool*)this;
  32. }
  33. JSONNumber* JSONValue::asNumber() const
  34. {
  35. return (JSONNumber*)this;
  36. }
  37. JSONString* JSONValue::asString() const
  38. {
  39. return (JSONString*)this;
  40. }
  41. JSONArray* JSONValue::asArray() const
  42. {
  43. return (JSONArray*)this;
  44. }
  45. JSONObject* JSONValue::asObject() const
  46. {
  47. return (JSONObject*)this;
  48. }
  49. JSONBool::JSONBool(bool b)
  50. : JSONValue(JSONType::BOOLEAN)
  51. {
  52. this->b = b;
  53. }
  54. bool JSONBool::getBool() const
  55. {
  56. return b;
  57. }
  58. Text JSONBool::toString() const
  59. {
  60. if (b)
  61. return Text("true");
  62. else
  63. return Text("false");
  64. }
  65. JSONValue* JSONBool::clone() const
  66. {
  67. return new JSONBool(b);
  68. }
  69. JSONNumber::JSONNumber(double num)
  70. : JSONValue(JSONType::NUMBER)
  71. {
  72. number = num;
  73. }
  74. double JSONNumber::getNumber() const
  75. {
  76. return number;
  77. }
  78. Text JSONNumber::toString() const
  79. {
  80. return Text(number);
  81. }
  82. JSONValue* JSONNumber::clone() const
  83. {
  84. return new JSONNumber(number);
  85. }
  86. JSONString::JSONString(Text string)
  87. : JSONValue(JSONType::STRING)
  88. {
  89. this->string = string;
  90. string.ersetzen("\\\"", "\"");
  91. }
  92. Text JSONString::getString() const
  93. {
  94. return string;
  95. }
  96. Text JSONString::toString() const
  97. {
  98. Text esc = string;
  99. esc.ersetzen("\"", "\\\"");
  100. return Text(Text("\"") += esc.getText()) += "\"";
  101. }
  102. JSONValue* JSONString::clone() const
  103. {
  104. return new JSONString(string);
  105. }
  106. JSONArray::JSONArray()
  107. : JSONValue(JSONType::ARRAY)
  108. {
  109. array = new RCArray< JSONValue >();
  110. }
  111. JSONArray::JSONArray(Text string)
  112. : JSONValue(JSONType::ARRAY)
  113. {
  114. array = new RCArray< JSONValue >();
  115. string = Parser::removeWhitespace(string);
  116. if (string.getText()[0] == '[' && string.getText()[string.getLength() - 1] == ']')
  117. {
  118. string.remove(0, 1);
  119. string.remove(string.getLength() - 1, string.getLength());
  120. while (string.getLength())
  121. {
  122. int end = Parser::findObjectEndInArray(string);
  123. Text* objStr = string.getTeilText(0, end);
  124. string.remove(0, end + 1);
  125. array->add(Parser::getValue(*objStr));
  126. objStr->release();
  127. }
  128. }
  129. }
  130. JSONArray::JSONArray(const JSONArray& arr)
  131. : JSONValue(JSONType::ARRAY)
  132. {
  133. array = dynamic_cast<RCArray<JSONValue> *>(arr.array->getThis());
  134. }
  135. JSONArray::~JSONArray()
  136. {
  137. array->release();
  138. }
  139. JSONArray& JSONArray::operator=(const JSONArray& arr)
  140. {
  141. array->release();
  142. array = dynamic_cast<RCArray<JSONValue> *>(arr.array->getThis());
  143. return *this;
  144. }
  145. void JSONArray::addValue(JSONValue* value)
  146. {
  147. array->add(value);
  148. }
  149. void JSONArray::setValue(int i, JSONValue* value)
  150. {
  151. array->set(value, i);
  152. }
  153. void JSONArray::removeValue(int i)
  154. {
  155. array->remove(i);
  156. }
  157. JSONValue* JSONArray::getValue(int i) const
  158. {
  159. return array->get(i);
  160. }
  161. JSONValue* JSONArray::zValue(int i) const
  162. {
  163. return array->z(i);
  164. }
  165. int JSONArray::getLength() const
  166. {
  167. return array->getEintragAnzahl();
  168. }
  169. bool JSONArray::isValueOfType(int i, JSONType type) const
  170. {
  171. return i >= 0 && i < array->getEintragAnzahl() && array->z(i)->getType() == type;
  172. }
  173. Iterator< JSONValue* > JSONArray::begin() const
  174. {
  175. return array->begin();
  176. }
  177. Iterator< JSONValue* > JSONArray::end() const
  178. {
  179. return array->end();
  180. }
  181. Text JSONArray::toString() const
  182. {
  183. Text str = "[";
  184. for (auto i = array->begin(); i; i++)
  185. {
  186. str += i->toString();
  187. if (i.hasNext())
  188. str += ",";
  189. }
  190. str += "]";
  191. return str;
  192. }
  193. JSONValue* JSONArray::clone() const
  194. {
  195. return new JSONArray(toString());
  196. }
  197. JSONObject::JSONObject()
  198. : JSONValue(JSONType::OBJECT)
  199. {
  200. fields = new Array< Text >();
  201. values = new RCArray< JSONValue >();
  202. }
  203. JSONObject::JSONObject(Text string)
  204. : JSONValue(JSONType::OBJECT)
  205. {
  206. fields = new Array< Text >();
  207. values = new RCArray< JSONValue >();
  208. string = Parser::removeWhitespace(string);
  209. if (string.getText()[0] == '{' && string.getText()[string.getLength() - 1] == '}')
  210. {
  211. string.remove(0, 1);
  212. string.remove(string.getLength() - 1, string.getLength());
  213. while (string.getLength())
  214. {
  215. int endField = Parser::findFieldEndInObject(string);
  216. Text* fieldName = string.getTeilText(0, endField);
  217. string.remove(0, endField + 1);
  218. fieldName->remove(0, 1);
  219. fieldName->remove(fieldName->getLength() - 1, fieldName->getLength());
  220. int endValue = Parser::findValueEndInObject(string);
  221. Text* value = string.getTeilText(0, endValue);
  222. string.remove(0, endValue + 1);
  223. fields->add(Text(fieldName->getText()));
  224. values->add(Parser::getValue(*value));
  225. fieldName->release();
  226. value->release();
  227. }
  228. }
  229. }
  230. JSONObject::JSONObject(const JSONObject& obj)
  231. : JSONValue(JSONType::OBJECT)
  232. {
  233. fields = dynamic_cast<Array<Text> *>(obj.fields->getThis());
  234. values = dynamic_cast<RCArray<JSONValue> *>(obj.values->getThis());
  235. }
  236. JSONObject::~JSONObject()
  237. {
  238. fields->release();
  239. values->release();
  240. }
  241. JSONObject& JSONObject::operator=(const JSONObject& obj)
  242. {
  243. fields->release();
  244. values->release();
  245. fields = dynamic_cast<Array<Text> *>(obj.fields->getThis());
  246. values = dynamic_cast<RCArray<JSONValue> *>(obj.values->getThis());
  247. return *this;
  248. }
  249. bool JSONObject::addValue(Text field, JSONValue* value)
  250. {
  251. if (hasValue(field))
  252. return 0;
  253. fields->add(field);
  254. values->add(value);
  255. return 1;
  256. }
  257. bool JSONObject::removeValue(Text field)
  258. {
  259. for (int i = 0; i < fields->getEintragAnzahl(); i++)
  260. {
  261. if (fields->get(i).istGleich(field))
  262. {
  263. fields->remove(i);
  264. values->remove(i);
  265. return 1;
  266. }
  267. }
  268. return 0;
  269. }
  270. bool JSONObject::hasValue(Text field)
  271. {
  272. for (int i = 0; i < fields->getEintragAnzahl(); i++)
  273. {
  274. if (fields->get(i).istGleich(field))
  275. return 1;
  276. }
  277. return 0;
  278. }
  279. JSONValue* JSONObject::getValue(Text field)
  280. {
  281. for (int i = 0; i < fields->getEintragAnzahl(); i++)
  282. {
  283. if (fields->get(i).istGleich(field))
  284. return values->get(i);
  285. }
  286. return 0;
  287. }
  288. JSONValue* JSONObject::zValue(Text field)
  289. {
  290. for (int i = 0; i < fields->getEintragAnzahl(); i++)
  291. {
  292. if (fields->get(i).istGleich(field))
  293. return values->z(i);
  294. }
  295. return 0;
  296. }
  297. Iterator< Text > JSONObject::getFields()
  298. {
  299. return fields->begin();
  300. }
  301. Iterator< JSONValue* > JSONObject::getValues()
  302. {
  303. return values->begin();
  304. }
  305. int JSONObject::getFieldCount() const
  306. {
  307. return fields->getEintragAnzahl();
  308. }
  309. bool JSONObject::isValueOfType(Text field, JSONType type) const
  310. {
  311. for (int i = 0; i < fields->getEintragAnzahl(); i++)
  312. {
  313. if (fields->get(i).istGleich(field))
  314. return values->z(i)->getType() == type;
  315. }
  316. return 0;
  317. }
  318. Text JSONObject::toString() const
  319. {
  320. Text str = "{";
  321. Iterator< Text > k = fields->begin();
  322. for (auto v = values->begin(); k && v; k++, v++)
  323. {
  324. str += "\"";
  325. str += k._.getText();
  326. str += "\":";
  327. str += v->toString().getText();
  328. if (v.hasNext())
  329. str += ",";
  330. }
  331. str += "}";
  332. return str;
  333. }
  334. JSONValue* JSONObject::clone() const
  335. {
  336. return new JSONObject(toString());
  337. }
  338. JSONValue* JSON::loadJSONFromFile(Text path)
  339. {
  340. Datei d;
  341. d.setDatei(path);
  342. d.open(Datei::Style::lesen);
  343. int size = (int)d.getSize();
  344. char* buffer = new char[size + 1];
  345. buffer[size] = 0;
  346. d.lese(buffer, size);
  347. d.close();
  348. JSONValue* result = Parser::getValue(buffer);
  349. delete[] buffer;
  350. return result;
  351. }
  352. int Parser::findObjectEndInArray(const char* str)
  353. {
  354. return findValueEndInObject(str);
  355. }
  356. Text Parser::removeWhitespace(const char* str)
  357. {
  358. int wsc = 0;
  359. int i = 0;
  360. bool esc = 0;
  361. bool strO = 0;
  362. for (; str[i]; i++)
  363. {
  364. switch (str[i])
  365. {
  366. case '\\':
  367. if (strO)
  368. esc = !esc;
  369. else
  370. esc = 0;
  371. break;
  372. case '"':
  373. if (!esc)
  374. strO = !strO;
  375. esc = 0;
  376. break;
  377. case ' ':
  378. case '\n':
  379. case '\t':
  380. case '\r':
  381. if (!strO)
  382. wsc++;
  383. esc = 0;
  384. break;
  385. default:
  386. esc = 0;
  387. break;
  388. }
  389. }
  390. Text ret;
  391. ret.fillText(' ', i - wsc);
  392. i = 0;
  393. esc = 0;
  394. strO = 0;
  395. int index = 0;
  396. for (; str[i]; i++)
  397. {
  398. switch (str[i])
  399. {
  400. case '\\':
  401. if (strO)
  402. esc = !esc;
  403. else
  404. esc = 0;
  405. ret.getText()[index++] = str[i];
  406. break;
  407. case '"':
  408. if (!esc)
  409. strO = !strO;
  410. esc = 0;
  411. ret.getText()[index++] = str[i];
  412. break;
  413. case ' ':
  414. case '\n':
  415. case '\t':
  416. case '\r':
  417. if (strO)
  418. ret.getText()[index++] = str[i];
  419. esc = 0;
  420. break;
  421. default:
  422. ret.getText()[index++] = str[i];
  423. esc = 0;
  424. break;
  425. }
  426. }
  427. return ret;
  428. }
  429. JSONValue* Parser::getValue(const char* str)
  430. {
  431. Text string = Parser::removeWhitespace(str);
  432. if (string.istGleich("true"))
  433. return new JSONBool(1);
  434. if (string.istGleich("false"))
  435. return new JSONBool(0);
  436. if (string.getText()[0] == '"')
  437. {
  438. string.remove(0, 1);
  439. string.remove(string.getLength() - 1, string.getLength());
  440. return new JSONString(string);
  441. }
  442. if (string.getText()[0] == '[')
  443. return new JSONArray(string);
  444. if (string.getText()[0] == '{')
  445. return new JSONObject(string);
  446. if (Text((int)string).istGleich(string.getText()))
  447. return new JSONNumber(string);
  448. if (string.anzahlVon('.') == 1)
  449. {
  450. bool isNumber = 1;
  451. for (char* c = (*string.getText() == '-') ? string.getText() + 1 : string.getText(); *c; c++)
  452. isNumber &= (*c >= '0' && *c <= '9') || *c == '.';
  453. if (isNumber)
  454. return new JSONNumber(string);
  455. }
  456. return new JSONValue();
  457. }
  458. int Parser::findFieldEndInObject(const char* str)
  459. {
  460. int i = 0;
  461. bool esc = 0;
  462. bool strO = 0;
  463. int objOc = 0;
  464. int arrayOc = 0;
  465. for (; str[i]; i++)
  466. {
  467. switch (str[i])
  468. {
  469. case '\\':
  470. if (strO)
  471. esc = !esc;
  472. else
  473. esc = 0;
  474. break;
  475. case '"':
  476. if (!esc)
  477. strO = !strO;
  478. esc = 0;
  479. break;
  480. case '[':
  481. if (!strO)
  482. arrayOc++;
  483. esc = 0;
  484. break;
  485. case ']':
  486. if (!strO)
  487. arrayOc--;
  488. esc = 0;
  489. break;
  490. case '{':
  491. if (!strO)
  492. objOc++;
  493. esc = 0;
  494. break;
  495. case '}':
  496. if (!strO)
  497. objOc--;
  498. esc = 0;
  499. break;
  500. case ':':
  501. if (!strO && objOc == 0 && arrayOc == 0)
  502. return i;
  503. esc = 0;
  504. break;
  505. default:
  506. esc = 0;
  507. break;
  508. }
  509. }
  510. return i;
  511. }
  512. int Parser::findValueEndInObject(const char* str)
  513. {
  514. int i = 0;
  515. bool esc = 0;
  516. bool strO = 0;
  517. int objOc = 0;
  518. int arrayOc = 0;
  519. for (; str[i]; i++)
  520. {
  521. switch (str[i])
  522. {
  523. case '\\':
  524. if (strO)
  525. esc = !esc;
  526. else
  527. esc = 0;
  528. break;
  529. case '"':
  530. if (!esc)
  531. strO = !strO;
  532. esc = 0;
  533. break;
  534. case '[':
  535. if (!strO)
  536. arrayOc++;
  537. esc = 0;
  538. break;
  539. case ']':
  540. if (!strO)
  541. arrayOc--;
  542. esc = 0;
  543. break;
  544. case '{':
  545. if (!strO)
  546. objOc++;
  547. esc = 0;
  548. break;
  549. case '}':
  550. if (!strO)
  551. objOc--;
  552. esc = 0;
  553. break;
  554. case ',':
  555. if (!strO && objOc == 0 && arrayOc == 0)
  556. return i;
  557. esc = 0;
  558. break;
  559. default:
  560. esc = 0;
  561. break;
  562. }
  563. }
  564. return i;
  565. }
  566. using namespace Validator;
  567. JSONValidationResult::JSONValidationResult()
  568. : ReferenceCounter()
  569. {}
  570. JSONValidationResult::~JSONValidationResult() {}
  571. JSONTypeMissmatch::JSONTypeMissmatch(Text path, JSONValue* foundValue, XML::Element* expected, JSONValidationResult* reason)
  572. : JSONValidationResult()
  573. {
  574. this->path = path;
  575. this->foundValue = foundValue;
  576. this->expected = expected;
  577. this->reason = reason;
  578. }
  579. JSONTypeMissmatch::~JSONTypeMissmatch()
  580. {
  581. foundValue->release();
  582. expected->release();
  583. if (reason)
  584. reason->release();
  585. }
  586. bool JSONTypeMissmatch::isValid() const
  587. {
  588. return 0;
  589. }
  590. void JSONTypeMissmatch::printInvalidInfo(int indent) const
  591. {
  592. Text ind = "";
  593. ind.fillText(' ', indent);
  594. std::cout << ind.getText() << "Type missmatch at path '" << path.getText() << "'. JSON Value\n" << ind.getText() << foundValue->toString().getText() << "\n" << ind.getText() << "does not match type\n" << ind.getText() << expected->toString().getText() << "\n";
  595. if (reason)
  596. {
  597. std::cout << ind.getText() << "Reason for type mismatch:\n";
  598. reason->printInvalidInfo(indent + 4);
  599. }
  600. }
  601. JSONValue* JSONTypeMissmatch::getValidPart() const
  602. {
  603. if (reason)
  604. {
  605. JSONValue* valid = reason->getValidPart();
  606. Text* p = reason->getPath().getTeilText(path.getLength());
  607. if (foundValue->getType() == JSONType::ARRAY)
  608. {
  609. if (p->hatAt(0, "[") && p->hatAt(p->getLength() - 1, "]"))
  610. {
  611. Text* it = p->getTeilText(1, p->getLength() - 1);
  612. int i = *it;
  613. it->release();
  614. if (i >= 0 && foundValue->asArray()->getLength() > i)
  615. {
  616. JSONValue* tmp = foundValue->clone();
  617. if (valid)
  618. tmp->asArray()->setValue(i, valid);
  619. else
  620. tmp->asArray()->removeValue(i);
  621. JSONValidationResult* res = JSONValidator(dynamic_cast<XML::Element*>(expected->getThis())).validate(tmp);
  622. if (res->isValid())
  623. {
  624. res->release();
  625. p->release();
  626. return tmp;
  627. }
  628. else if (res->isDifferent(this, path) || !valid)
  629. {
  630. p->release();
  631. tmp->release();
  632. JSONValue* result = res->getValidPart();
  633. res->release();
  634. return result;
  635. }
  636. tmp->release();
  637. res->release();
  638. }
  639. }
  640. }
  641. else if (foundValue->getType() == JSONType::OBJECT)
  642. {
  643. if (p->hatAt(0, "."))
  644. {
  645. Text* at = p->getTeilText(1);
  646. Text attr = *at;
  647. at->release();
  648. if (foundValue->asObject()->hasValue(attr))
  649. {
  650. JSONValue* tmp = foundValue->clone();
  651. tmp->asObject()->removeValue(attr);
  652. if (valid)
  653. tmp->asObject()->addValue(attr, valid);
  654. JSONValidationResult* res = JSONValidator(dynamic_cast<XML::Element*>(expected->getThis())).validate(tmp);
  655. if (res->isValid())
  656. {
  657. res->release();
  658. p->release();
  659. return tmp;
  660. }
  661. else if (res->isDifferent(this, path) || !valid)
  662. {
  663. p->release();
  664. tmp->release();
  665. JSONValue* result = res->getValidPart();
  666. res->release();
  667. return result;
  668. }
  669. tmp->release();
  670. res->release();
  671. }
  672. }
  673. }
  674. p->release();
  675. if (valid)
  676. valid->release();
  677. }
  678. if (expected->hasAttribute("default"))
  679. {
  680. JSONValue* def = Parser::getValue(expected->getAttributeValue("default"));
  681. JSONValidationResult* res = JSONValidator(dynamic_cast<XML::Element*>(expected->getThis())).validate(def);
  682. if (res->isValid())
  683. {
  684. res->release();
  685. return def;
  686. }
  687. else if (res->isDifferent(this, path))
  688. {
  689. def->release();
  690. JSONValue* result = res->getValidPart();
  691. res->release();
  692. return result;
  693. }
  694. def->release();
  695. }
  696. return 0;
  697. }
  698. Text JSONTypeMissmatch::getPath() const
  699. {
  700. return path;
  701. }
  702. bool JSONTypeMissmatch::isDifferent(const JSONValidationResult* zResult, Text additionalPath) const
  703. {
  704. const JSONTypeMissmatch* casted = dynamic_cast<const JSONTypeMissmatch*>(zResult);
  705. if (casted == 0)
  706. return 1;
  707. if (!casted->getPath().istGleich(additionalPath + path))
  708. return 1;
  709. return reason->isDifferent(casted->reason, path);
  710. }
  711. JSONUnknownValue::JSONUnknownValue(Text path, JSONValue* foundValue)
  712. : JSONValidationResult()
  713. {
  714. this->path = path;
  715. this->foundValue = foundValue;
  716. }
  717. JSONUnknownValue::~JSONUnknownValue()
  718. {
  719. foundValue->release();
  720. }
  721. bool JSONUnknownValue::isValid() const
  722. {
  723. return 0;
  724. }
  725. void JSONUnknownValue::printInvalidInfo(int indent) const
  726. {
  727. Text ind = "";
  728. ind.fillText(' ', indent);
  729. std::cout << ind.getText() << "Unknown Value at '" << path.getText() << "'. Value found:\n" << foundValue->toString().getText() << "\n";
  730. }
  731. JSONValue* JSONUnknownValue::getValidPart() const
  732. {
  733. return 0;
  734. }
  735. Text JSONUnknownValue::getPath() const
  736. {
  737. return path;
  738. }
  739. bool JSONUnknownValue::isDifferent(const JSONValidationResult* zResult, Text additionalPath) const
  740. {
  741. const JSONUnknownValue* casted = dynamic_cast<const JSONUnknownValue*>(zResult);
  742. if (casted == 0)
  743. return 1;
  744. if (!casted->getPath().istGleich(additionalPath + path))
  745. return 1;
  746. return 0;
  747. }
  748. JSONMissingValue::JSONMissingValue(Text path, XML::Element* expected)
  749. : JSONValidationResult()
  750. {
  751. this->path = path;
  752. this->expected = expected;
  753. }
  754. JSONMissingValue::~JSONMissingValue()
  755. {
  756. expected->release();
  757. }
  758. bool JSONMissingValue::isValid() const
  759. {
  760. return 0;
  761. }
  762. void JSONMissingValue::printInvalidInfo(int indent) const
  763. {
  764. Text ind = "";
  765. ind.fillText(' ', indent);
  766. std::cout << ind.getText() << "Missing Value at '" << path.getText() << "'. Expected type:\n" << expected->toString().getText() << "\n";
  767. }
  768. JSONValue* JSONMissingValue::getValidPart() const
  769. {
  770. if (expected->hasAttribute("default"))
  771. {
  772. JSONValue* def = Parser::getValue(expected->getAttributeValue("default"));
  773. JSONValidationResult* res = JSONValidator(dynamic_cast<XML::Element*>(expected->getThis())).validate(def);
  774. if (res->isValid())
  775. {
  776. res->release();
  777. return def;
  778. }
  779. else if (res->isDifferent(this, path))
  780. {
  781. def->release();
  782. JSONValue* result = res->getValidPart();
  783. res->release();
  784. return result;
  785. }
  786. def->release();
  787. }
  788. return 0;
  789. }
  790. Text JSONMissingValue::getPath() const
  791. {
  792. return path;
  793. }
  794. bool JSONMissingValue::isDifferent(const JSONValidationResult* zResult, Text additionalPath) const
  795. {
  796. const JSONMissingValue* casted = dynamic_cast<const JSONMissingValue*>(zResult);
  797. if (casted == 0)
  798. return 1;
  799. if (!casted->getPath().istGleich(additionalPath + path))
  800. return 1;
  801. return 0;
  802. }
  803. JSONMissingOneOf::JSONMissingOneOf(Text path, XML::Editor expected)
  804. : JSONValidationResult()
  805. {
  806. this->path = path;
  807. for (XML::Element* e : expected)
  808. this->expected.add(dynamic_cast<XML::Element*>(e->getThis()));
  809. }
  810. JSONMissingOneOf::~JSONMissingOneOf()
  811. {}
  812. bool JSONMissingOneOf::isValid() const
  813. {
  814. return 0;
  815. }
  816. void JSONMissingOneOf::printInvalidInfo(int indent) const
  817. {
  818. Text ind = "";
  819. ind.fillText(' ', indent);
  820. std::cout << ind.getText() << "Missing Value at '" << path.getText() << "'. The value should have one of the following types:\n";
  821. ind += " ";
  822. for (XML::Element* e : expected)
  823. {
  824. std::cout << ind.getText() << e->toString().getText() << "\n";
  825. }
  826. }
  827. JSONValue* JSONMissingOneOf::getValidPart() const
  828. {
  829. // multiple possibilities are undecidable
  830. return 0;
  831. }
  832. Text JSONMissingOneOf::getPath() const
  833. {
  834. return path;
  835. }
  836. bool JSONMissingOneOf::isDifferent(const JSONValidationResult* zResult, Text additionalPath) const
  837. {
  838. const JSONMissingOneOf* casted = dynamic_cast<const JSONMissingOneOf*>(zResult);
  839. if (casted == 0)
  840. return 1;
  841. if (!casted->getPath().istGleich(additionalPath + path))
  842. return 1;
  843. return 0;
  844. }
  845. JSONNoTypeMatching::JSONNoTypeMatching(Text path, JSONValue* foundValue, RCArray<XML::Element>& expected, RCArray<JSONValidationResult>& reasons)
  846. : JSONValidationResult()
  847. {
  848. this->path = path;
  849. this->foundValue = foundValue;
  850. this->expected = expected;
  851. this->reasons = reasons;
  852. }
  853. JSONNoTypeMatching::~JSONNoTypeMatching()
  854. {
  855. foundValue->release();
  856. }
  857. bool JSONNoTypeMatching::isValid() const
  858. {
  859. return 0;
  860. }
  861. void JSONNoTypeMatching::printInvalidInfo(int indent) const
  862. {
  863. Text ind = "";
  864. ind.fillText(' ', indent);
  865. std::cout << ind.getText() << "Found Value at '" << path.getText() << "' did not match any of the given possible types:\n";
  866. Text ind2 = ind + " ";
  867. for (XML::Element* element : expected)
  868. {
  869. std::cout << ind2.getText() << element->toString().getText() << "\n";
  870. }
  871. std::cout << ind.getText() << "Reasons:\n";
  872. for (JSONValidationResult* reason : reasons)
  873. {
  874. reason->printInvalidInfo(indent + 4);
  875. }
  876. }
  877. JSONValue* JSONNoTypeMatching::getValidPart() const
  878. {
  879. // multiple possibilities are undecidable
  880. return 0;
  881. }
  882. Text JSONNoTypeMatching::getPath() const
  883. {
  884. return path;
  885. }
  886. bool JSONNoTypeMatching::isDifferent(const JSONValidationResult* zResult, Text additionalPath) const
  887. {
  888. const JSONNoTypeMatching* casted = dynamic_cast<const JSONNoTypeMatching*>(zResult);
  889. if (casted == 0)
  890. return 1;
  891. if (!casted->getPath().istGleich(additionalPath + path))
  892. return 1;
  893. for (int i = 0; i < reasons.getEintragAnzahl(); i++)
  894. {
  895. if (reasons.z(i)->isDifferent(casted->reasons.z(i), additionalPath))
  896. return 1;
  897. }
  898. return 0;
  899. }
  900. JSONValidValue::JSONValidValue(Text path, JSONValue* value)
  901. : JSONValidationResult()
  902. {
  903. this->path = path;
  904. this->value = value;
  905. }
  906. JSONValidValue::~JSONValidValue()
  907. {
  908. value->release();
  909. }
  910. bool JSONValidValue::isValid() const
  911. {
  912. return 1;
  913. }
  914. void JSONValidValue::printInvalidInfo(int indent) const
  915. {}
  916. JSONValue* JSONValidValue::getValidPart() const
  917. {
  918. return value->clone();
  919. }
  920. Text JSONValidValue::getPath() const
  921. {
  922. return path;
  923. }
  924. bool JSONValidValue::isDifferent(const JSONValidationResult* zResult, Text additionalPath) const
  925. {
  926. const JSONValidValue* casted = dynamic_cast<const JSONValidValue*>(zResult);
  927. if (casted == 0)
  928. return 1;
  929. if (!casted->getPath().istGleich(additionalPath + path))
  930. return 1;
  931. return 0;
  932. }
  933. JSONValidator::JSONValidator(XML::Element* constraints)
  934. : ReferenceCounter(),
  935. constraints(constraints)
  936. {}
  937. JSONValidator::~JSONValidator()
  938. {
  939. constraints->release();
  940. }
  941. JSONValidationResult* JSONValidator::validate(JSONValue* zValue) const
  942. {
  943. return validate(zValue, constraints, "");
  944. }
  945. bool JSONValidator::isValid(JSONValue* zValue) const
  946. {
  947. JSONValidationResult* res = validate(zValue);
  948. if (res->isValid())
  949. {
  950. res->release();
  951. return 1;
  952. }
  953. res->release();
  954. return 0;
  955. }
  956. JSONValue* JSONValidator::getValidParts(JSONValue* zValue) const
  957. {
  958. JSONValidationResult* res = validate(zValue);
  959. if (res->isValid())
  960. {
  961. res->release();
  962. return zValue->clone();
  963. }
  964. JSONValue* valid = res->getValidPart();
  965. res->release();
  966. return valid;
  967. }
  968. XML::Element* JSONValidator::zConstraints()
  969. {
  970. return constraints;
  971. }
  972. JSONValidationResult* JSONValidator::validate(JSONValue* zValue, XML::Element* zConstraints, Text path) const
  973. {
  974. switch (zValue->getType())
  975. {
  976. case JSONType::NULL_:
  977. if (!zConstraints->hasAttribute("nullable") || !zConstraints->getAttributeValue("nullable").istGleich("true"))
  978. {
  979. return new JSONTypeMissmatch(path, dynamic_cast<JSONValue*>(zValue->getThis()), dynamic_cast<XML::Element*>(zConstraints->getThis()), 0);
  980. }
  981. break;
  982. case JSONType::BOOLEAN:
  983. if (!zConstraints->getName().istGleich("bool"))
  984. {
  985. return new JSONTypeMissmatch(path, dynamic_cast<JSONValue*>(zValue->getThis()), dynamic_cast<XML::Element*>(zConstraints->getThis()), 0);
  986. }
  987. else if (zConstraints->hasAttribute("equals"))
  988. {
  989. if (!zConstraints->getAttributeValue("equals").istGleich("true") == !zValue->asBool()->getBool())
  990. {
  991. return new JSONTypeMissmatch(path, dynamic_cast<JSONValue*>(zValue->getThis()), dynamic_cast<XML::Element*>(zConstraints->getThis()), 0);
  992. }
  993. }
  994. break;
  995. case JSONType::NUMBER:
  996. if (!zConstraints->getName().istGleich("number"))
  997. {
  998. return new JSONTypeMissmatch(path, dynamic_cast<JSONValue*>(zValue->getThis()), dynamic_cast<XML::Element*>(zConstraints->getThis()), 0);
  999. }
  1000. else
  1001. {
  1002. if (zConstraints->hasAttribute("equals") && (double)zConstraints->getAttributeValue("equals") != zValue->asNumber()->getNumber())
  1003. {
  1004. return new JSONTypeMissmatch(path, dynamic_cast<JSONValue*>(zValue->getThis()), dynamic_cast<XML::Element*>(zConstraints->getThis()), 0);
  1005. }
  1006. else if (zConstraints->hasAttribute("lessOrEqual") && zValue->asNumber()->getNumber() > (double)zConstraints->getAttributeValue("lessOrEqual"))
  1007. {
  1008. return new JSONTypeMissmatch(path, dynamic_cast<JSONValue*>(zValue->getThis()), dynamic_cast<XML::Element*>(zConstraints->getThis()), 0);
  1009. }
  1010. else if (zConstraints->hasAttribute("greaterOrEqual") && zValue->asNumber()->getNumber() < (double)zConstraints->getAttributeValue("greaterOrEqual"))
  1011. {
  1012. return new JSONTypeMissmatch(path, dynamic_cast<JSONValue*>(zValue->getThis()), dynamic_cast<XML::Element*>(zConstraints->getThis()), 0);
  1013. }
  1014. else if (zConstraints->hasAttribute("less") && zValue->asNumber()->getNumber() >= (double)zConstraints->getAttributeValue("less"))
  1015. {
  1016. return new JSONTypeMissmatch(path, dynamic_cast<JSONValue*>(zValue->getThis()), dynamic_cast<XML::Element*>(zConstraints->getThis()), 0);
  1017. }
  1018. else if (zConstraints->hasAttribute("greater") && zValue->asNumber()->getNumber() <= (double)zConstraints->getAttributeValue("greater"))
  1019. {
  1020. return new JSONTypeMissmatch(path, dynamic_cast<JSONValue*>(zValue->getThis()), dynamic_cast<XML::Element*>(zConstraints->getThis()), 0);
  1021. }
  1022. }
  1023. break;
  1024. case JSONType::STRING:
  1025. if (!zConstraints->getName().istGleich("string"))
  1026. {
  1027. return new JSONTypeMissmatch(path, dynamic_cast<JSONValue*>(zValue->getThis()), dynamic_cast<XML::Element*>(zConstraints->getThis()), 0);
  1028. }
  1029. else
  1030. {
  1031. if (zConstraints->hasAttribute("equals") && !zConstraints->getAttributeValue("equals").istGleich(zValue->asString()->getString()))
  1032. {
  1033. return new JSONTypeMissmatch(path, dynamic_cast<JSONValue*>(zValue->getThis()), dynamic_cast<XML::Element*>(zConstraints->getThis()), 0);
  1034. }
  1035. else if (zConstraints->hasAttribute("contains") && zValue->asString()->getString().hat(zConstraints->getAttributeValue("contains")))
  1036. {
  1037. return new JSONTypeMissmatch(path, dynamic_cast<JSONValue*>(zValue->getThis()), dynamic_cast<XML::Element*>(zConstraints->getThis()), 0);
  1038. }
  1039. else if (zConstraints->hasAttribute("startsWith") && zValue->asString()->getString().positionVon(zConstraints->getAttributeValue("startsWith")) == 0)
  1040. {
  1041. return new JSONTypeMissmatch(path, dynamic_cast<JSONValue*>(zValue->getThis()), dynamic_cast<XML::Element*>(zConstraints->getThis()), 0);
  1042. }
  1043. else if (zConstraints->hasAttribute("endsWith") && zValue->asString()->getString().positionVon(zConstraints->getAttributeValue("endsWith")) == zValue->asString()->getString().getLength() - zConstraints->getAttributeValue("endsWith").getLength())
  1044. {
  1045. return new JSONTypeMissmatch(path, dynamic_cast<JSONValue*>(zValue->getThis()), dynamic_cast<XML::Element*>(zConstraints->getThis()), 0);
  1046. }
  1047. }
  1048. break;
  1049. case JSONType::ARRAY:
  1050. if (!zConstraints->getName().istGleich("array"))
  1051. {
  1052. return new JSONTypeMissmatch(path, dynamic_cast<JSONValue*>(zValue->getThis()), dynamic_cast<XML::Element*>(zConstraints->getThis()), 0);
  1053. }
  1054. else
  1055. {
  1056. bool valid = 1;
  1057. int index = 0;
  1058. for (JSON::JSONValue* value : *zValue->asArray())
  1059. {
  1060. Text p = path;
  1061. p += "[";
  1062. p += index;
  1063. p += "]";
  1064. JSONValidationResult* res = validateMultipleTypes(value, zConstraints, p);
  1065. if (!res->isValid())
  1066. {
  1067. return new JSONTypeMissmatch(path, dynamic_cast<JSONValue*>(zValue->getThis()), dynamic_cast<XML::Element*>(zConstraints->getThis()), res);
  1068. }
  1069. res->release();
  1070. index++;
  1071. }
  1072. }
  1073. break;
  1074. case JSONType::OBJECT:
  1075. if (!zConstraints->getName().istGleich("object"))
  1076. {
  1077. return new JSONTypeMissmatch(path, dynamic_cast<JSONValue*>(zValue->getThis()), dynamic_cast<XML::Element*>(zConstraints->getThis()), 0);
  1078. }
  1079. else
  1080. {
  1081. bool valid = 1;
  1082. JSON::JSONObject* obj = zValue->asObject();
  1083. for (auto i = obj->getFields(); i; i++)
  1084. {
  1085. Text p = path;
  1086. p += ".";
  1087. p += i.val();
  1088. if (!zConstraints->selectChildsByAttribute("name", i.val()).exists())
  1089. {
  1090. return new JSONUnknownValue(p, zValue->asObject()->getValue(i.val()));
  1091. }
  1092. else
  1093. {
  1094. bool isValueValid = 0;
  1095. JSONValidationResult* res = validateMultipleTypes(zValue->asObject()->zValue(i.val()), zConstraints->selectChildsByAttribute("name", i.val()).begin().val(), p);
  1096. if (!res->isValid())
  1097. {
  1098. return new JSONTypeMissmatch(path, dynamic_cast<JSONValue*>(zValue->getThis()), dynamic_cast<XML::Element*>(zConstraints->getThis()), res);
  1099. }
  1100. res->release();
  1101. }
  1102. }
  1103. for (XML::Element* constraint : zConstraints->selectChildren())
  1104. {
  1105. if (!zValue->asObject()->hasValue(constraint->getAttributeValue("name")))
  1106. {
  1107. Text p = path;
  1108. p += ".";
  1109. p += constraint->getAttributeValue("name");
  1110. if (constraint->getChildCount() != 1)
  1111. return new JSONMissingOneOf(p, constraint->selectChildren());
  1112. return new JSONMissingValue(path, dynamic_cast<XML::Element*>(constraint->selectChildren().begin().val()->getThis()));
  1113. }
  1114. }
  1115. }
  1116. break;
  1117. }
  1118. return new JSONValidValue(path, dynamic_cast<JSONValue*>(zValue->getThis()));
  1119. }
  1120. JSONValidationResult* JSONValidator::validateMultipleTypes(JSONValue* zChildValue, XML::Element* zPossibleChildConstraints, Text childPath) const
  1121. {
  1122. if (zPossibleChildConstraints->getChildCount() == 1)
  1123. return validate(zChildValue, zPossibleChildConstraints->selectChildren().begin().val(), childPath); // only one type is possible
  1124. bool hasTypeAttr = 0;
  1125. RCArray<XML::Element> possibleConstraints;
  1126. if (zPossibleChildConstraints->hasAttribute("typeSpecifiedBy"))
  1127. { // try to find the correct constraints based on the type attribute
  1128. hasTypeAttr = 1;
  1129. Text typeAttr = zPossibleChildConstraints->getAttributeValue("typeSpecifiedBy");
  1130. if (zChildValue->getType() == JSONType::OBJECT)
  1131. {
  1132. if (zChildValue->asObject()->hasValue(typeAttr))
  1133. {
  1134. JSONValue* typeV = zChildValue->asObject()->zValue(typeAttr);
  1135. for (XML::Element* constraint : zPossibleChildConstraints->selectChildsByName("object").whereChildWithAttributeExists("name", typeAttr))
  1136. {
  1137. XML::Element* typeAttrContraints = constraint->selectChildsByAttribute("name", typeAttr).begin().val();
  1138. JSONValidationResult* res = validateMultipleTypes(typeV, typeAttrContraints, childPath + "." + typeAttr);
  1139. if (res->isValid())
  1140. possibleConstraints.add(dynamic_cast<XML::Element*>(constraint->getThis()));
  1141. res->release();
  1142. }
  1143. }
  1144. }
  1145. }
  1146. if (hasTypeAttr && possibleConstraints.getEintragAnzahl() == 1)
  1147. return validate(zChildValue, possibleConstraints.begin().val(), childPath); // if the type is clear
  1148. else if (hasTypeAttr && possibleConstraints.getEintragAnzahl() > 0)
  1149. { // more then one type is possible
  1150. RCArray< JSONValidationResult> invalidResults;
  1151. for (XML::Element* constraint : possibleConstraints)
  1152. {
  1153. JSONValidationResult* res = validate(zChildValue, constraint, childPath);
  1154. invalidResults.add(res);
  1155. if (res->isValid())
  1156. return new JSONValidValue(childPath, dynamic_cast<JSONValue*>(zChildValue->getThis()));
  1157. }
  1158. return new JSONNoTypeMatching(childPath, dynamic_cast<JSONValue*>(zChildValue->getThis()), possibleConstraints, invalidResults);
  1159. }
  1160. // try all types
  1161. possibleConstraints.leeren();
  1162. RCArray< JSONValidationResult> invalidResults;
  1163. for (XML::Element* constraint : zPossibleChildConstraints->selectChildren())
  1164. {
  1165. JSONValidationResult* res = validate(zChildValue, constraint, childPath);
  1166. invalidResults.add(res);
  1167. if (res->isValid())
  1168. return new JSONValidValue(childPath, dynamic_cast<JSONValue*>(zChildValue->getThis()));
  1169. possibleConstraints.add(dynamic_cast<XML::Element*>(constraint->getThis()));
  1170. }
  1171. return new JSONNoTypeMatching(childPath, dynamic_cast<JSONValue*>(zChildValue->getThis()), possibleConstraints, invalidResults);
  1172. }
  1173. StringValidationBuilder<JSONValidator>* JSONValidator::buildForString()
  1174. {
  1175. return new StringValidationBuilder<JSONValidator>([](XML::Element& e)
  1176. {
  1177. return new JSONValidator(e.dublicate());
  1178. });
  1179. }
  1180. NumberValidationBuilder<JSONValidator>* JSONValidator::buildForNumber()
  1181. {
  1182. return new NumberValidationBuilder<JSONValidator>([](XML::Element& e)
  1183. {
  1184. return new JSONValidator(e.dublicate());
  1185. });
  1186. }
  1187. BoolValidationBuilder<JSONValidator>* JSONValidator::buildForBool()
  1188. {
  1189. return new BoolValidationBuilder<JSONValidator>([](XML::Element& e)
  1190. {
  1191. return new JSONValidator(e.dublicate());
  1192. });
  1193. }
  1194. ObjectValidationBuilder<JSONValidator>* JSONValidator::buildForObject()
  1195. {
  1196. return new ObjectValidationBuilder<JSONValidator>([](XML::Element& e)
  1197. {
  1198. return new JSONValidator(e.dublicate());
  1199. });
  1200. }
  1201. ArrayValidationBuilder<JSONValidator>* JSONValidator::buildForArray()
  1202. {
  1203. return new ArrayValidationBuilder<JSONValidator>([](XML::Element& e)
  1204. {
  1205. return new JSONValidator(e.dublicate());
  1206. });
  1207. }