JSON.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552
  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. 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. #pragma endregion Content
  50. #pragma region JSONBool
  51. JSONBool::JSONBool(bool b)
  52. : JSONValue(JSONType::BOOLEAN)
  53. {
  54. this->b = b;
  55. }
  56. bool JSONBool::getBool() const
  57. {
  58. return b;
  59. }
  60. Text JSONBool::toString() const
  61. {
  62. if (b)
  63. return Text("true");
  64. else
  65. return Text("false");
  66. }
  67. JSONValue* JSONBool::clone() const
  68. {
  69. return new JSONBool(b);
  70. }
  71. #pragma endregion Content
  72. #pragma region JSONNumber
  73. JSONNumber::JSONNumber(double num)
  74. : JSONValue(JSONType::NUMBER)
  75. {
  76. number = num;
  77. }
  78. double JSONNumber::getNumber() const
  79. {
  80. return number;
  81. }
  82. Text JSONNumber::toString() const
  83. {
  84. return Text(number);
  85. }
  86. JSONValue* JSONNumber::clone() const
  87. {
  88. return new JSONNumber(number);
  89. }
  90. #pragma endregion Content
  91. #pragma region JSONString
  92. JSONString::JSONString(Text string)
  93. : JSONValue(JSONType::STRING)
  94. {
  95. this->string = string;
  96. string.ersetzen("\\\"", "\"");
  97. }
  98. Text JSONString::getString() const
  99. {
  100. return string;
  101. }
  102. Text JSONString::toString() const
  103. {
  104. Text esc = string;
  105. esc.ersetzen("\"", "\\\"");
  106. return Text(Text("\"") += esc.getText()) += "\"";
  107. }
  108. JSONValue* JSONString::clone() const
  109. {
  110. return new JSONString(string);
  111. }
  112. #pragma endregion Content
  113. #pragma region JSONArray
  114. JSONArray::JSONArray()
  115. : JSONValue(JSONType::ARRAY)
  116. {
  117. array = new RCArray<JSONValue>();
  118. }
  119. JSONArray::JSONArray(Text string)
  120. : JSONValue(JSONType::ARRAY)
  121. {
  122. array = new RCArray<JSONValue>();
  123. string = Parser::removeWhitespace(string);
  124. if (string.getText()[0] == '['
  125. && 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()
  181. && array->z(i)->getType() == type;
  182. }
  183. Iterator<JSONValue*> JSONArray::begin() const
  184. {
  185. return array->begin();
  186. }
  187. Iterator<JSONValue*> JSONArray::end() const
  188. {
  189. return array->end();
  190. }
  191. Text JSONArray::toString() const
  192. {
  193. Text str = "[";
  194. for (auto i = array->begin(); i; i++)
  195. {
  196. str += i->toString();
  197. if (i.hasNext()) 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] == '{'
  221. && string.getText()[string.getLength() - 1] == '}')
  222. {
  223. string.remove(0, 1);
  224. string.remove(string.getLength() - 1, string.getLength());
  225. while (string.getLength())
  226. {
  227. int endField = Parser::findFieldEndInObject(string);
  228. Text* fieldName = string.getTeilText(0, endField);
  229. string.remove(0, endField + 1);
  230. fieldName->remove(0, 1);
  231. fieldName->remove(
  232. fieldName->getLength() - 1, fieldName->getLength());
  233. int endValue = Parser::findValueEndInObject(string);
  234. Text* value = string.getTeilText(0, endValue);
  235. string.remove(0, endValue + 1);
  236. fields->add(Text(fieldName->getText()));
  237. values->add(Parser::getValue(*value));
  238. fieldName->release();
  239. value->release();
  240. }
  241. }
  242. }
  243. JSONObject::JSONObject(const JSONObject& obj)
  244. : JSONValue(JSONType::OBJECT)
  245. {
  246. fields = dynamic_cast<Array<Text>*>(obj.fields->getThis());
  247. values = dynamic_cast<RCArray<JSONValue>*>(obj.values->getThis());
  248. }
  249. JSONObject::~JSONObject()
  250. {
  251. fields->release();
  252. values->release();
  253. }
  254. JSONObject& JSONObject::operator=(const JSONObject& obj)
  255. {
  256. fields->release();
  257. values->release();
  258. fields = dynamic_cast<Array<Text>*>(obj.fields->getThis());
  259. values = dynamic_cast<RCArray<JSONValue>*>(obj.values->getThis());
  260. return *this;
  261. }
  262. bool JSONObject::addValue(Text field, JSONValue* value)
  263. {
  264. if (hasValue(field)) return 0;
  265. fields->add(field);
  266. values->add(value);
  267. return 1;
  268. }
  269. bool JSONObject::removeValue(Text field)
  270. {
  271. for (int i = 0; i < fields->getEintragAnzahl(); i++)
  272. {
  273. if (fields->get(i).istGleich(field))
  274. {
  275. fields->remove(i);
  276. values->remove(i);
  277. return 1;
  278. }
  279. }
  280. return 0;
  281. }
  282. bool JSONObject::hasValue(Text field)
  283. {
  284. for (int i = 0; i < fields->getEintragAnzahl(); i++)
  285. {
  286. if (fields->get(i).istGleich(field)) 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)) return values->get(i);
  295. }
  296. return 0;
  297. }
  298. JSONValue* JSONObject::zValue(Text field)
  299. {
  300. for (int i = 0; i < fields->getEintragAnzahl(); i++)
  301. {
  302. if (fields->get(i).istGleich(field)) return values->z(i);
  303. }
  304. return 0;
  305. }
  306. Iterator<Text> JSONObject::getFields()
  307. {
  308. return fields->begin();
  309. }
  310. Iterator<JSONValue*> JSONObject::getValues()
  311. {
  312. return values->begin();
  313. }
  314. int JSONObject::getFieldCount() const
  315. {
  316. return fields->getEintragAnzahl();
  317. }
  318. bool JSONObject::isValueOfType(Text field, JSONType type) const
  319. {
  320. for (int i = 0; i < fields->getEintragAnzahl(); i++)
  321. {
  322. if (fields->get(i).istGleich(field))
  323. return values->z(i)->getType() == type;
  324. }
  325. return 0;
  326. }
  327. Text JSONObject::toString() const
  328. {
  329. Text str = "{";
  330. Iterator<Text> k = fields->begin();
  331. for (auto v = values->begin(); k && v; k++, v++)
  332. {
  333. str += "\"";
  334. str += k._.getText();
  335. str += "\":";
  336. str += v->toString().getText();
  337. if (v.hasNext()) str += ",";
  338. }
  339. str += "}";
  340. return str;
  341. }
  342. JSONValue* JSONObject::clone() const
  343. {
  344. return new JSONObject(toString());
  345. }
  346. #pragma endregion Content
  347. JSONValue* JSON::loadJSONFromFile(Text path)
  348. {
  349. Datei d;
  350. d.setDatei(path);
  351. if (!d.open(Datei::Style::lesen))
  352. {
  353. return new JSONValue();
  354. }
  355. int size = (int)d.getSize();
  356. char* buffer = new char[size + 1];
  357. buffer[size] = 0;
  358. d.lese(buffer, size);
  359. d.close();
  360. JSONValue* result = Parser::getValue(buffer);
  361. delete[] buffer;
  362. return result;
  363. }
  364. #pragma region Parser
  365. int Parser::findObjectEndInArray(const char* str)
  366. {
  367. return findValueEndInObject(str);
  368. }
  369. Text Parser::removeWhitespace(const char* str)
  370. {
  371. int wsc = 0;
  372. int i = 0;
  373. bool esc = 0;
  374. bool strO = 0;
  375. for (; str[i]; i++)
  376. {
  377. switch (str[i])
  378. {
  379. case '\\':
  380. if (strO)
  381. esc = !esc;
  382. else
  383. esc = 0;
  384. break;
  385. case '"':
  386. if (!esc) strO = !strO;
  387. esc = 0;
  388. break;
  389. case ' ':
  390. case '\n':
  391. case '\t':
  392. case '\r':
  393. if (!strO) wsc++;
  394. esc = 0;
  395. break;
  396. default:
  397. esc = 0;
  398. break;
  399. }
  400. }
  401. Text ret;
  402. i = 0;
  403. esc = 0;
  404. strO = 0;
  405. for (; str[i]; i++)
  406. {
  407. switch (str[i])
  408. {
  409. case '\\':
  410. if (strO)
  411. esc = !esc;
  412. else
  413. esc = 0;
  414. ret.append(str[i]);
  415. break;
  416. case '"':
  417. if (!esc) strO = !strO;
  418. esc = 0;
  419. ret.append(str[i]);
  420. break;
  421. case ' ':
  422. case '\n':
  423. case '\t':
  424. case '\r':
  425. if (strO) ret.append(str[i]);
  426. esc = 0;
  427. break;
  428. default:
  429. ret.append(str[i]);
  430. esc = 0;
  431. break;
  432. }
  433. }
  434. return ret;
  435. }
  436. JSONValue* Parser::getValue(const char* str)
  437. {
  438. Text string = Parser::removeWhitespace(str);
  439. if (string.istGleich("true")) return new JSONBool(1);
  440. if (string.istGleich("false")) return new JSONBool(0);
  441. if (string.getText()[0] == '"')
  442. {
  443. string.remove(0, 1);
  444. string.remove(string.getLength() - 1, string.getLength());
  445. return new JSONString(string);
  446. }
  447. if (string.getText()[0] == '[') return new JSONArray(string);
  448. if (string.getText()[0] == '{') return new JSONObject(string);
  449. if (Text((int)string).istGleich(string.getText()))
  450. return new JSONNumber((double)string);
  451. if (string.anzahlVon('.') == 1)
  452. {
  453. bool isNumber = 1;
  454. for (const char* c = (*string.getText() == '-') ? string.getText() + 1
  455. : string.getText();
  456. *c;
  457. c++)
  458. isNumber &= (*c >= '0' && *c <= '9') || *c == '.';
  459. if (isNumber) return new JSONNumber((double)string);
  460. }
  461. return new JSONValue();
  462. }
  463. int Parser::findFieldEndInObject(const char* str)
  464. {
  465. int i = 0;
  466. bool esc = 0;
  467. bool strO = 0;
  468. int objOc = 0;
  469. int arrayOc = 0;
  470. for (; str[i]; i++)
  471. {
  472. switch (str[i])
  473. {
  474. case '\\':
  475. if (strO)
  476. esc = !esc;
  477. else
  478. esc = 0;
  479. break;
  480. case '"':
  481. if (!esc) strO = !strO;
  482. esc = 0;
  483. break;
  484. case '[':
  485. if (!strO) arrayOc++;
  486. esc = 0;
  487. break;
  488. case ']':
  489. if (!strO) arrayOc--;
  490. esc = 0;
  491. break;
  492. case '{':
  493. if (!strO) objOc++;
  494. esc = 0;
  495. break;
  496. case '}':
  497. if (!strO) objOc--;
  498. esc = 0;
  499. break;
  500. case ':':
  501. if (!strO && objOc == 0 && arrayOc == 0) return i;
  502. esc = 0;
  503. break;
  504. default:
  505. esc = 0;
  506. break;
  507. }
  508. }
  509. return i;
  510. }
  511. int Parser::findValueEndInObject(const char* str)
  512. {
  513. int i = 0;
  514. bool esc = 0;
  515. bool strO = 0;
  516. int objOc = 0;
  517. int arrayOc = 0;
  518. for (; str[i]; i++)
  519. {
  520. switch (str[i])
  521. {
  522. case '\\':
  523. if (strO)
  524. esc = !esc;
  525. else
  526. esc = 0;
  527. break;
  528. case '"':
  529. if (!esc) strO = !strO;
  530. esc = 0;
  531. break;
  532. case '[':
  533. if (!strO) arrayOc++;
  534. esc = 0;
  535. break;
  536. case ']':
  537. if (!strO) arrayOc--;
  538. esc = 0;
  539. break;
  540. case '{':
  541. if (!strO) objOc++;
  542. esc = 0;
  543. break;
  544. case '}':
  545. if (!strO) objOc--;
  546. esc = 0;
  547. break;
  548. case ',':
  549. if (!strO && objOc == 0 && arrayOc == 0) return i;
  550. esc = 0;
  551. break;
  552. default:
  553. esc = 0;
  554. break;
  555. }
  556. }
  557. return i;
  558. }
  559. #pragma endregion Content
  560. using namespace Validator;
  561. #pragma region JSONValidationResult
  562. JSONValidationResult::JSONValidationResult()
  563. : ReferenceCounter()
  564. {}
  565. JSONValidationResult::~JSONValidationResult() {}
  566. #pragma region JSONTypeMissmatch
  567. JSONTypeMissmatch::JSONTypeMissmatch(Text path,
  568. JSONValue* foundValue,
  569. XML::Element* expected,
  570. JSONValidationResult* reason)
  571. : JSONValidationResult()
  572. {
  573. this->path = path;
  574. this->foundValue = foundValue;
  575. this->expected = expected;
  576. this->reason = reason;
  577. }
  578. JSONTypeMissmatch::~JSONTypeMissmatch()
  579. {
  580. foundValue->release();
  581. expected->release();
  582. if (reason) reason->release();
  583. }
  584. bool JSONTypeMissmatch::isValid() const
  585. {
  586. return 0;
  587. }
  588. void JSONTypeMissmatch::printInvalidInfo(int indent) const
  589. {
  590. Text ind = "";
  591. ind.fillText(' ', indent);
  592. std::cout << ind.getText() << "Type missmatch at path '" << path.getText()
  593. << "'. JSON Value\n"
  594. << ind.getText() << foundValue->toString().getText() << "\n"
  595. << ind.getText() << "does not match type\n"
  596. << ind.getText() << expected->toString().getText() << "\n";
  597. if (reason)
  598. {
  599. std::cout << ind.getText() << "Reason for type mismatch:\n";
  600. reason->printInvalidInfo(indent + 4);
  601. }
  602. }
  603. JSONValue* JSONTypeMissmatch::getValidPart() const
  604. {
  605. if (reason)
  606. {
  607. JSONValue* valid = reason->getValidPart();
  608. Text* p = reason->getPath().getTeilText(path.getLength());
  609. if (foundValue->getType() == JSONType::ARRAY)
  610. {
  611. if (!valid
  612. && (!expected->hasAttribute("removeInvalidEntries")
  613. || !expected->getAttributeValue("removeInvalidEntries")
  614. .istGleich("true")))
  615. {
  616. p->release();
  617. return 0;
  618. }
  619. if (p->hatAt(0, "[") && p->hatAt(p->getLength() - 1, "]"))
  620. {
  621. Text* it = p->getTeilText(1, p->getLength() - 1);
  622. int i = (int)*it;
  623. it->release();
  624. if (i >= 0 && foundValue->asArray()->getLength() > i)
  625. {
  626. JSONValue* tmp = foundValue->clone();
  627. if (valid)
  628. tmp->asArray()->setValue(i, valid);
  629. else
  630. tmp->asArray()->removeValue(i);
  631. JSONValidationResult* res = JSONValidator(
  632. dynamic_cast<XML::Element*>(expected->getThis()))
  633. .validate(tmp);
  634. if (res->isValid())
  635. {
  636. res->release();
  637. p->release();
  638. return tmp;
  639. }
  640. else if (res->isDifferent(this, path) || !valid)
  641. {
  642. p->release();
  643. tmp->release();
  644. JSONValue* result = res->getValidPart();
  645. res->release();
  646. return result;
  647. }
  648. tmp->release();
  649. res->release();
  650. }
  651. }
  652. }
  653. else if (foundValue->getType() == JSONType::OBJECT)
  654. {
  655. if (!valid
  656. && (!expected->hasAttribute("removeInvalidEntries")
  657. || !expected->getAttributeValue("removeInvalidEntries")
  658. .istGleich("true")))
  659. {
  660. p->release();
  661. return 0;
  662. }
  663. if (p->hatAt(0, "."))
  664. {
  665. Text* at = p->getTeilText(1);
  666. Text attr = *at;
  667. at->release();
  668. JSONValue* tmp = foundValue->clone();
  669. tmp->asObject()->removeValue(attr);
  670. if (valid) tmp->asObject()->addValue(attr, valid);
  671. JSONValidationResult* res = JSONValidator(
  672. dynamic_cast<XML::Element*>(expected->getThis()))
  673. .validate(tmp);
  674. if (res->isValid())
  675. {
  676. res->release();
  677. p->release();
  678. return tmp;
  679. }
  680. else if (res->isDifferent(this, path) || !valid)
  681. {
  682. p->release();
  683. tmp->release();
  684. JSONValue* result = res->getValidPart();
  685. res->release();
  686. return result;
  687. }
  688. tmp->release();
  689. res->release();
  690. }
  691. }
  692. p->release();
  693. if (valid) valid->release();
  694. }
  695. return 0;
  696. }
  697. Text JSONTypeMissmatch::getPath() const
  698. {
  699. return path;
  700. }
  701. bool JSONTypeMissmatch::isDifferent(
  702. const JSONValidationResult* zResult, Text additionalPath) const
  703. {
  704. const JSONTypeMissmatch* casted
  705. = dynamic_cast<const JSONTypeMissmatch*>(zResult);
  706. if (casted == 0) return 1;
  707. if (!casted->getPath().istGleich(additionalPath + path)) return 1;
  708. return reason->isDifferent(casted->reason, path);
  709. }
  710. #pragma endregion Content
  711. #pragma region JSONUnknownValue
  712. JSONUnknownValue::JSONUnknownValue(Text path, JSONValue* foundValue)
  713. : JSONValidationResult()
  714. {
  715. this->path = path;
  716. this->foundValue = foundValue;
  717. }
  718. JSONUnknownValue::~JSONUnknownValue()
  719. {
  720. foundValue->release();
  721. }
  722. bool JSONUnknownValue::isValid() const
  723. {
  724. return 0;
  725. }
  726. void JSONUnknownValue::printInvalidInfo(int indent) const
  727. {
  728. Text ind = "";
  729. ind.fillText(' ', indent);
  730. std::cout << ind.getText() << "Unknown Value at '" << path.getText()
  731. << "'. Value found:\n"
  732. << ind.getText() << foundValue->toString().getText() << "\n";
  733. }
  734. JSONValue* JSONUnknownValue::getValidPart() const
  735. {
  736. return 0;
  737. }
  738. Text JSONUnknownValue::getPath() const
  739. {
  740. return path;
  741. }
  742. bool JSONUnknownValue::isDifferent(
  743. const JSONValidationResult* zResult, Text additionalPath) const
  744. {
  745. const JSONUnknownValue* casted
  746. = dynamic_cast<const JSONUnknownValue*>(zResult);
  747. if (casted == 0) return 1;
  748. if (!casted->getPath().istGleich(additionalPath + path)) return 1;
  749. return 0;
  750. }
  751. #pragma endregion Content
  752. #pragma region JSONMissingValue
  753. JSONMissingValue::JSONMissingValue(Text path, XML::Element* expected)
  754. : JSONValidationResult()
  755. {
  756. this->path = path;
  757. this->expected = expected;
  758. }
  759. JSONMissingValue::~JSONMissingValue()
  760. {
  761. expected->release();
  762. }
  763. bool JSONMissingValue::isValid() const
  764. {
  765. return 0;
  766. }
  767. void JSONMissingValue::printInvalidInfo(int indent) const
  768. {
  769. Text ind = "";
  770. ind.fillText(' ', indent);
  771. std::cout << ind.getText() << "Missing Value at '" << path.getText()
  772. << "'. Expected type:\n"
  773. << ind.getText() << expected->toString().getText() << "\n";
  774. }
  775. JSONValue* JSONMissingValue::getValidPart() const
  776. {
  777. if (expected->hasAttribute("default"))
  778. {
  779. JSONValue* def
  780. = Parser::getValue(expected->getAttributeValue("default"));
  781. JSONValidationResult* res
  782. = JSONValidator(dynamic_cast<XML::Element*>(expected->getThis()))
  783. .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(
  805. const JSONValidationResult* zResult, Text additionalPath) const
  806. {
  807. const JSONMissingValue* casted
  808. = dynamic_cast<const JSONMissingValue*>(zResult);
  809. if (casted == 0) return 1;
  810. if (!casted->getPath().istGleich(additionalPath + path)) 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. bool JSONMissingOneOf::isValid() const
  824. {
  825. return 0;
  826. }
  827. void JSONMissingOneOf::printInvalidInfo(int indent) const
  828. {
  829. Text ind = "";
  830. ind.fillText(' ', indent);
  831. std::cout << ind.getText() << "Missing Value at '" << path.getText()
  832. << "'. 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(
  849. const JSONValidationResult* zResult, Text additionalPath) const
  850. {
  851. const JSONMissingOneOf* casted
  852. = dynamic_cast<const JSONMissingOneOf*>(zResult);
  853. if (casted == 0) return 1;
  854. if (!casted->getPath().istGleich(additionalPath + path)) return 1;
  855. return 0;
  856. }
  857. #pragma endregion Content
  858. #pragma region JSONNoTypeMatching
  859. JSONNoTypeMatching::JSONNoTypeMatching(Text path,
  860. JSONValue* foundValue,
  861. RCArray<XML::Element>& expected,
  862. RCArray<JSONValidationResult>& reasons)
  863. : JSONValidationResult()
  864. {
  865. this->path = path;
  866. this->foundValue = foundValue;
  867. this->expected = expected;
  868. this->reasons = reasons;
  869. }
  870. JSONNoTypeMatching::~JSONNoTypeMatching()
  871. {
  872. foundValue->release();
  873. }
  874. bool JSONNoTypeMatching::isValid() const
  875. {
  876. return 0;
  877. }
  878. void JSONNoTypeMatching::printInvalidInfo(int indent) const
  879. {
  880. Text ind = "";
  881. ind.fillText(' ', indent);
  882. std::cout << ind.getText() << "Found Value at '" << path.getText()
  883. << "' did not match any of the given possible types:\n";
  884. Text ind2 = ind + " ";
  885. for (XML::Element* element : expected)
  886. {
  887. std::cout << ind2.getText() << element->toString().getText() << "\n";
  888. }
  889. std::cout << ind.getText() << "Reasons:\n";
  890. for (JSONValidationResult* reason : reasons)
  891. {
  892. reason->printInvalidInfo(indent + 4);
  893. }
  894. }
  895. JSONValue* JSONNoTypeMatching::getValidPart() const
  896. {
  897. // multiple possibilities are undecidable
  898. return 0;
  899. }
  900. Text JSONNoTypeMatching::getPath() const
  901. {
  902. return path;
  903. }
  904. bool JSONNoTypeMatching::isDifferent(
  905. const JSONValidationResult* zResult, Text additionalPath) const
  906. {
  907. const JSONNoTypeMatching* casted
  908. = dynamic_cast<const JSONNoTypeMatching*>(zResult);
  909. if (casted == 0) return 1;
  910. if (!casted->getPath().istGleich(additionalPath + path)) return 1;
  911. for (int i = 0; i < reasons.getEintragAnzahl(); i++)
  912. {
  913. if (reasons.z(i)->isDifferent(casted->reasons.z(i), additionalPath))
  914. return 1;
  915. }
  916. return 0;
  917. }
  918. #pragma endregion Content
  919. #pragma region JSONValidValue
  920. JSONValidValue::JSONValidValue(Text path, JSONValue* value)
  921. : JSONValidationResult()
  922. {
  923. this->path = path;
  924. this->value = value;
  925. }
  926. JSONValidValue::~JSONValidValue()
  927. {
  928. value->release();
  929. }
  930. bool JSONValidValue::isValid() const
  931. {
  932. return 1;
  933. }
  934. void JSONValidValue::printInvalidInfo(int indent) const {}
  935. JSONValue* JSONValidValue::getValidPart() const
  936. {
  937. return value->clone();
  938. }
  939. Text JSONValidValue::getPath() const
  940. {
  941. return path;
  942. }
  943. bool JSONValidValue::isDifferent(
  944. const JSONValidationResult* zResult, Text additionalPath) const
  945. {
  946. const JSONValidValue* casted = dynamic_cast<const JSONValidValue*>(zResult);
  947. if (casted == 0) return 1;
  948. if (!casted->getPath().istGleich(additionalPath + path)) return 1;
  949. return 0;
  950. }
  951. #pragma endregion Content
  952. #pragma endregion Content
  953. #pragma region JSONValidator
  954. JSONValidator::JSONValidator(XML::Element* constraints)
  955. : ReferenceCounter(),
  956. constraints(constraints)
  957. {}
  958. JSONValidator::~JSONValidator()
  959. {
  960. constraints->release();
  961. }
  962. JSONValidationResult* JSONValidator::validate(JSONValue* zValue) const
  963. {
  964. return validate(zValue, constraints, "");
  965. }
  966. bool JSONValidator::isValid(JSONValue* zValue) const
  967. {
  968. JSONValidationResult* res = validate(zValue);
  969. if (res->isValid())
  970. {
  971. res->release();
  972. return 1;
  973. }
  974. res->release();
  975. return 0;
  976. }
  977. JSONValue* JSONValidator::getValidParts(JSONValue* zValue) const
  978. {
  979. JSONValidationResult* res = validate(zValue);
  980. if (res->isValid())
  981. {
  982. res->release();
  983. return zValue->clone();
  984. }
  985. JSONValue* valid = res->getValidPart();
  986. res->release();
  987. return valid;
  988. }
  989. XML::Element* JSONValidator::zConstraints()
  990. {
  991. return constraints;
  992. }
  993. JSONValidationResult* JSONValidator::validate(
  994. JSONValue* zValue, XML::Element* zConstraints, Text path) const
  995. {
  996. switch (zValue->getType())
  997. {
  998. case JSONType::NULL_:
  999. if (!zConstraints->hasAttribute("nullable")
  1000. || !zConstraints->getAttributeValue("nullable").istGleich("true"))
  1001. {
  1002. return new JSONTypeMissmatch(path,
  1003. dynamic_cast<JSONValue*>(zValue->getThis()),
  1004. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1005. 0);
  1006. }
  1007. break;
  1008. case JSONType::BOOLEAN:
  1009. if (!zConstraints->getName().istGleich("bool"))
  1010. {
  1011. return new JSONTypeMissmatch(path,
  1012. dynamic_cast<JSONValue*>(zValue->getThis()),
  1013. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1014. 0);
  1015. }
  1016. else if (zConstraints->hasAttribute("equals"))
  1017. {
  1018. if (!zConstraints->getAttributeValue("equals").istGleich("true")
  1019. == !zValue->asBool()->getBool())
  1020. {
  1021. return new JSONTypeMissmatch(path,
  1022. dynamic_cast<JSONValue*>(zValue->getThis()),
  1023. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1024. 0);
  1025. }
  1026. }
  1027. break;
  1028. case JSONType::NUMBER:
  1029. if (!zConstraints->getName().istGleich("number"))
  1030. {
  1031. return new JSONTypeMissmatch(path,
  1032. dynamic_cast<JSONValue*>(zValue->getThis()),
  1033. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1034. 0);
  1035. }
  1036. else
  1037. {
  1038. if (zConstraints->hasAttribute("equals")
  1039. && (double)zConstraints->getAttributeValue("equals")
  1040. != zValue->asNumber()->getNumber())
  1041. {
  1042. return new JSONTypeMissmatch(path,
  1043. dynamic_cast<JSONValue*>(zValue->getThis()),
  1044. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1045. 0);
  1046. }
  1047. if (zConstraints->hasAttribute("lessOrEqual")
  1048. && zValue->asNumber()->getNumber()
  1049. > (double)zConstraints->getAttributeValue("lessOrEqual"))
  1050. {
  1051. return new JSONTypeMissmatch(path,
  1052. dynamic_cast<JSONValue*>(zValue->getThis()),
  1053. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1054. 0);
  1055. }
  1056. if (zConstraints->hasAttribute("greaterOrEqual")
  1057. && zValue->asNumber()->getNumber()
  1058. < (double)zConstraints->getAttributeValue(
  1059. "greaterOrEqual"))
  1060. {
  1061. return new JSONTypeMissmatch(path,
  1062. dynamic_cast<JSONValue*>(zValue->getThis()),
  1063. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1064. 0);
  1065. }
  1066. if (zConstraints->hasAttribute("less")
  1067. && zValue->asNumber()->getNumber()
  1068. >= (double)zConstraints->getAttributeValue("less"))
  1069. {
  1070. return new JSONTypeMissmatch(path,
  1071. dynamic_cast<JSONValue*>(zValue->getThis()),
  1072. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1073. 0);
  1074. }
  1075. if (zConstraints->hasAttribute("greater")
  1076. && zValue->asNumber()->getNumber()
  1077. <= (double)zConstraints->getAttributeValue("greater"))
  1078. {
  1079. return new JSONTypeMissmatch(path,
  1080. dynamic_cast<JSONValue*>(zValue->getThis()),
  1081. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1082. 0);
  1083. }
  1084. }
  1085. break;
  1086. case JSONType::STRING:
  1087. if (!zConstraints->getName().istGleich("string"))
  1088. {
  1089. return new JSONTypeMissmatch(path,
  1090. dynamic_cast<JSONValue*>(zValue->getThis()),
  1091. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1092. 0);
  1093. }
  1094. else
  1095. {
  1096. if (zConstraints->hasAttribute("equals")
  1097. && !zConstraints->getAttributeValue("equals").istGleich(
  1098. zValue->asString()->getString()))
  1099. {
  1100. return new JSONTypeMissmatch(path,
  1101. dynamic_cast<JSONValue*>(zValue->getThis()),
  1102. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1103. 0);
  1104. }
  1105. if (zConstraints->hasAttribute("contains")
  1106. && zValue->asString()->getString().hat(
  1107. zConstraints->getAttributeValue("contains").getText()))
  1108. {
  1109. return new JSONTypeMissmatch(path,
  1110. dynamic_cast<JSONValue*>(zValue->getThis()),
  1111. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1112. 0);
  1113. }
  1114. if (zConstraints->hasAttribute("startsWith")
  1115. && zValue->asString()->getString().positionVon(
  1116. zConstraints->getAttributeValue("startsWith").getText())
  1117. == 0)
  1118. {
  1119. return new JSONTypeMissmatch(path,
  1120. dynamic_cast<JSONValue*>(zValue->getThis()),
  1121. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1122. 0);
  1123. }
  1124. if (zConstraints->hasAttribute("endsWith")
  1125. && zValue->asString()->getString().positionVon(
  1126. zConstraints->getAttributeValue("endsWith").getText())
  1127. == zValue->asString()->getString().getLength()
  1128. - zConstraints->getAttributeValue("endsWith")
  1129. .getLength())
  1130. {
  1131. return new JSONTypeMissmatch(path,
  1132. dynamic_cast<JSONValue*>(zValue->getThis()),
  1133. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1134. 0);
  1135. }
  1136. if (zConstraints->hasAttribute("oneOf"))
  1137. {
  1138. JSONArray* array
  1139. = Parser::getValue(zConstraints->getAttributeValue("oneOf"))
  1140. ->asArray();
  1141. bool ok = 0;
  1142. for (JSONValue* v : *array)
  1143. {
  1144. if (v->asString()->getString().istGleich(
  1145. zValue->asString()->getString()))
  1146. {
  1147. ok = 1;
  1148. break;
  1149. }
  1150. }
  1151. array->release();
  1152. if (!ok)
  1153. {
  1154. return new JSONTypeMissmatch(path,
  1155. dynamic_cast<JSONValue*>(zValue->getThis()),
  1156. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1157. 0);
  1158. }
  1159. }
  1160. }
  1161. break;
  1162. case JSONType::ARRAY:
  1163. if (!zConstraints->getName().istGleich("array"))
  1164. {
  1165. return new JSONTypeMissmatch(path,
  1166. dynamic_cast<JSONValue*>(zValue->getThis()),
  1167. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1168. 0);
  1169. }
  1170. else
  1171. {
  1172. int index = 0;
  1173. for (JSON::JSONValue* value : *zValue->asArray())
  1174. {
  1175. Text p = path;
  1176. p += "[";
  1177. p += index;
  1178. p += "]";
  1179. JSONValidationResult* res
  1180. = validateMultipleTypes(value, zConstraints, p);
  1181. if (!res->isValid())
  1182. {
  1183. return new JSONTypeMissmatch(path,
  1184. dynamic_cast<JSONValue*>(zValue->getThis()),
  1185. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1186. res);
  1187. }
  1188. res->release();
  1189. index++;
  1190. }
  1191. }
  1192. break;
  1193. case JSONType::OBJECT:
  1194. if (zConstraints->getName().istGleich("objectRef"))
  1195. {
  1196. Text id = zConstraints->getAttributeValue("ref");
  1197. XML::Editor editor = constraints->select()
  1198. .selectAllElements()
  1199. .whereNameEquals("object")
  1200. .whereAttributeEquals("id", id);
  1201. if (editor.getSize())
  1202. {
  1203. zConstraints = editor.begin().val();
  1204. }
  1205. }
  1206. if (!zConstraints->getName().istGleich("object"))
  1207. {
  1208. return new JSONTypeMissmatch(path,
  1209. dynamic_cast<JSONValue*>(zValue->getThis()),
  1210. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1211. 0);
  1212. }
  1213. else
  1214. {
  1215. JSON::JSONObject* obj = zValue->asObject();
  1216. for (auto i = obj->getFields(); i; i++)
  1217. {
  1218. Text p = path;
  1219. p += ".";
  1220. p += i.val();
  1221. if (!zConstraints->selectChildsByAttribute("name", i.val())
  1222. .exists())
  1223. {
  1224. return new JSONTypeMissmatch(path,
  1225. dynamic_cast<JSONValue*>(zValue->getThis()),
  1226. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1227. new JSONUnknownValue(
  1228. p, zValue->asObject()->getValue(i.val())));
  1229. }
  1230. else
  1231. {
  1232. XML::Editor tmp = zConstraints->selectChildsByAttribute(
  1233. "name", i.val());
  1234. JSONValidationResult* res = validateMultipleTypes(
  1235. zValue->asObject()->zValue(i.val()),
  1236. tmp.begin().val(),
  1237. p);
  1238. if (!res->isValid())
  1239. {
  1240. return new JSONTypeMissmatch(path,
  1241. dynamic_cast<JSONValue*>(zValue->getThis()),
  1242. dynamic_cast<XML::Element*>(
  1243. zConstraints->getThis()),
  1244. res);
  1245. }
  1246. res->release();
  1247. }
  1248. }
  1249. for (XML::Element* constraint : zConstraints->selectChildren())
  1250. {
  1251. if (!zValue->asObject()->hasValue(
  1252. constraint->getAttributeValue("name")))
  1253. {
  1254. XML::Editor tmp = constraint->selectChildren();
  1255. bool optional = true;
  1256. tmp.forEach([&optional](XML::Element* zElement) {
  1257. optional &= zElement->hasAttribute("optional")
  1258. && zElement->getAttributeValue("optional")
  1259. .istGleich("true");
  1260. });
  1261. if (!optional)
  1262. {
  1263. Text p = path;
  1264. p += ".";
  1265. p += constraint->getAttributeValue("name");
  1266. if (constraint->getChildCount() != 1)
  1267. return new JSONTypeMissmatch(path,
  1268. dynamic_cast<JSONValue*>(zValue->getThis()),
  1269. dynamic_cast<XML::Element*>(
  1270. zConstraints->getThis()),
  1271. new JSONMissingOneOf(p, tmp));
  1272. return new JSONTypeMissmatch(path,
  1273. dynamic_cast<JSONValue*>(zValue->getThis()),
  1274. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1275. new JSONMissingValue(p,
  1276. dynamic_cast<XML::Element*>(
  1277. tmp.begin()->getThis())));
  1278. }
  1279. }
  1280. }
  1281. }
  1282. break;
  1283. }
  1284. return new JSONValidValue(
  1285. path, dynamic_cast<JSONValue*>(zValue->getThis()));
  1286. }
  1287. JSONValidationResult* JSONValidator::validateMultipleTypes(
  1288. JSONValue* zChildValue,
  1289. XML::Element* zPossibleChildConstraints,
  1290. Text childPath) const
  1291. {
  1292. if (zPossibleChildConstraints->getChildCount() == 1)
  1293. {
  1294. XML::Editor children = zPossibleChildConstraints->selectChildren();
  1295. return validate(zChildValue,
  1296. children.begin().val(),
  1297. childPath); // only one type is possible
  1298. }
  1299. bool hasTypeAttr = 0;
  1300. RCArray<XML::Element> possibleConstraints;
  1301. if (zPossibleChildConstraints->hasAttribute("typeSpecifiedBy"))
  1302. { // try to find the correct constraints based on the type attribute
  1303. hasTypeAttr = 1;
  1304. Text typeAttr
  1305. = zPossibleChildConstraints->getAttributeValue("typeSpecifiedBy");
  1306. if (zChildValue->getType() == JSONType::OBJECT)
  1307. {
  1308. if (zChildValue->asObject()->hasValue(typeAttr))
  1309. {
  1310. JSONValue* typeV = zChildValue->asObject()->zValue(typeAttr);
  1311. for (XML::Element* constraint :
  1312. zPossibleChildConstraints->selectChildsByName("object")
  1313. .whereChildWithAttributeExists("name", typeAttr))
  1314. {
  1315. XML::Editor nameChildren
  1316. = constraint->selectChildsByAttribute("name", typeAttr);
  1317. XML::Element* typeAttrContraints
  1318. = nameChildren.begin().val();
  1319. JSONValidationResult* res = validateMultipleTypes(
  1320. typeV, typeAttrContraints, childPath + "." + typeAttr);
  1321. if (res->isValid())
  1322. possibleConstraints.add(
  1323. dynamic_cast<XML::Element*>(constraint->getThis()));
  1324. res->release();
  1325. }
  1326. }
  1327. }
  1328. }
  1329. if (hasTypeAttr && possibleConstraints.getEintragAnzahl() == 1)
  1330. return validate(zChildValue,
  1331. possibleConstraints.begin().val(),
  1332. childPath); // if the type is clear
  1333. else if (hasTypeAttr && possibleConstraints.getEintragAnzahl() > 0)
  1334. { // more then one type is possible
  1335. RCArray<JSONValidationResult> invalidResults;
  1336. for (XML::Element* constraint : possibleConstraints)
  1337. {
  1338. JSONValidationResult* res
  1339. = validate(zChildValue, constraint, childPath);
  1340. invalidResults.add(res);
  1341. if (res->isValid())
  1342. return new JSONValidValue(childPath,
  1343. dynamic_cast<JSONValue*>(zChildValue->getThis()));
  1344. }
  1345. return new JSONNoTypeMatching(childPath,
  1346. dynamic_cast<JSONValue*>(zChildValue->getThis()),
  1347. possibleConstraints,
  1348. invalidResults);
  1349. }
  1350. // try all types
  1351. possibleConstraints.leeren();
  1352. RCArray<JSONValidationResult> invalidResults;
  1353. for (XML::Element* constraint : zPossibleChildConstraints->selectChildren())
  1354. {
  1355. JSONValidationResult* res
  1356. = validate(zChildValue, constraint, childPath);
  1357. invalidResults.add(res);
  1358. if (res->isValid())
  1359. return new JSONValidValue(
  1360. childPath, dynamic_cast<JSONValue*>(zChildValue->getThis()));
  1361. possibleConstraints.add(
  1362. dynamic_cast<XML::Element*>(constraint->getThis()));
  1363. }
  1364. return new JSONNoTypeMatching(childPath,
  1365. dynamic_cast<JSONValue*>(zChildValue->getThis()),
  1366. possibleConstraints,
  1367. invalidResults);
  1368. }
  1369. StringValidationBuilder<JSONValidator>* JSONValidator::buildForString()
  1370. {
  1371. return new StringValidationBuilder<JSONValidator>(
  1372. [](XML::Element& e) { return new JSONValidator(e.dublicate()); });
  1373. }
  1374. NumberValidationBuilder<JSONValidator>* JSONValidator::buildForNumber()
  1375. {
  1376. return new NumberValidationBuilder<JSONValidator>(
  1377. [](XML::Element& e) { return new JSONValidator(e.dublicate()); });
  1378. }
  1379. BoolValidationBuilder<JSONValidator>* JSONValidator::buildForBool()
  1380. {
  1381. return new BoolValidationBuilder<JSONValidator>(
  1382. [](XML::Element& e) { return new JSONValidator(e.dublicate()); });
  1383. }
  1384. ObjectValidationBuilder<JSONValidator>* JSONValidator::buildForObject()
  1385. {
  1386. return new ObjectValidationBuilder<JSONValidator>(
  1387. [](XML::Element& e) { return new JSONValidator(e.dublicate()); });
  1388. }
  1389. ArrayValidationBuilder<JSONValidator>* JSONValidator::buildForArray()
  1390. {
  1391. return new ArrayValidationBuilder<JSONValidator>(
  1392. [](XML::Element& e) { return new JSONValidator(e.dublicate()); });
  1393. }
  1394. JSONValidator* JSONValidator::buildForObjectReference(Text objectId)
  1395. {
  1396. return new JSONValidator(
  1397. new XML::Element(Text("<objectRef ref=\"") + objectId + Text("\"/>")));
  1398. }
  1399. #pragma endregion Content