JSON.cpp 32 KB

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