JSON.cpp 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639
  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(
  604. RCArray<JSONValidationResult>* removedPartsValidationResults)
  605. {
  606. if (reason)
  607. {
  608. RCArray<JSONValidationResult> temporaryInvalidParts;
  609. JSONValue* valid = reason->getValidPart(&temporaryInvalidParts);
  610. Text* p = reason->getPath().getTeilText(path.getLength());
  611. if (foundValue->getType() == JSONType::ARRAY)
  612. {
  613. if (!valid
  614. && (!expected->hasAttribute("removeInvalidEntries")
  615. || !expected->getAttributeValue("removeInvalidEntries")
  616. .istGleich("true")))
  617. {
  618. if (removedPartsValidationResults)
  619. {
  620. removedPartsValidationResults->add(
  621. dynamic_cast<JSONValidationResult*>(getThis()));
  622. }
  623. p->release();
  624. return 0;
  625. }
  626. if (p->hatAt(0, "[") && p->hatAt(p->getLength() - 1, "]"))
  627. {
  628. Text* it = p->getTeilText(1, p->getLength() - 1);
  629. int i = (int)*it;
  630. it->release();
  631. if (i >= 0 && foundValue->asArray()->getLength() > i)
  632. {
  633. if (removedPartsValidationResults)
  634. {
  635. for (JSONValidationResult* res : temporaryInvalidParts)
  636. {
  637. removedPartsValidationResults->add(
  638. dynamic_cast<JSONValidationResult*>(
  639. res->getThis()));
  640. }
  641. }
  642. JSONValue* tmp = foundValue->clone();
  643. if (valid)
  644. tmp->asArray()->setValue(i, valid);
  645. else
  646. tmp->asArray()->removeValue(i);
  647. JSONValidationResult* res = JSONValidator(
  648. dynamic_cast<XML::Element*>(expected->getThis()))
  649. .validate(tmp);
  650. if (res->isValid())
  651. {
  652. res->release();
  653. p->release();
  654. return tmp;
  655. }
  656. else if (res->isDifferent(this, path) || !valid)
  657. {
  658. p->release();
  659. tmp->release();
  660. JSONValue* result
  661. = res->getValidPart(removedPartsValidationResults);
  662. res->release();
  663. return result;
  664. }
  665. tmp->release();
  666. res->release();
  667. }
  668. }
  669. }
  670. else if (foundValue->getType() == JSONType::OBJECT)
  671. {
  672. if (!valid
  673. && (!expected->hasAttribute("removeInvalidEntries")
  674. || !expected->getAttributeValue("removeInvalidEntries")
  675. .istGleich("true")))
  676. {
  677. if (removedPartsValidationResults)
  678. {
  679. removedPartsValidationResults->add(
  680. dynamic_cast<JSONValidationResult*>(getThis()));
  681. }
  682. p->release();
  683. return 0;
  684. }
  685. if (p->hatAt(0, "."))
  686. {
  687. if (removedPartsValidationResults)
  688. {
  689. for (JSONValidationResult* res : temporaryInvalidParts)
  690. {
  691. removedPartsValidationResults->add(
  692. dynamic_cast<JSONValidationResult*>(
  693. res->getThis()));
  694. }
  695. }
  696. Text* at = p->getTeilText(1);
  697. Text attr = *at;
  698. at->release();
  699. JSONValue* tmp = foundValue->clone();
  700. tmp->asObject()->removeValue(attr);
  701. if (valid) tmp->asObject()->addValue(attr, valid);
  702. JSONValidationResult* res = JSONValidator(
  703. dynamic_cast<XML::Element*>(expected->getThis()))
  704. .validate(tmp);
  705. if (res->isValid())
  706. {
  707. res->release();
  708. p->release();
  709. return tmp;
  710. }
  711. else if (res->isDifferent(this, path) || !valid)
  712. {
  713. p->release();
  714. tmp->release();
  715. JSONValue* result
  716. = res->getValidPart(removedPartsValidationResults);
  717. res->release();
  718. return result;
  719. }
  720. tmp->release();
  721. res->release();
  722. }
  723. }
  724. p->release();
  725. if (valid) valid->release();
  726. }
  727. if (removedPartsValidationResults)
  728. {
  729. removedPartsValidationResults->add(
  730. dynamic_cast<JSONValidationResult*>(getThis()));
  731. }
  732. return 0;
  733. }
  734. Text JSONTypeMissmatch::getPath() const
  735. {
  736. return path;
  737. }
  738. bool JSONTypeMissmatch::isDifferent(
  739. const JSONValidationResult* zResult, Text additionalPath) const
  740. {
  741. const JSONTypeMissmatch* casted
  742. = dynamic_cast<const JSONTypeMissmatch*>(zResult);
  743. if (casted == 0) return 1;
  744. if (!casted->getPath().istGleich(additionalPath + path)) return 1;
  745. return reason->isDifferent(casted->reason, path);
  746. }
  747. #pragma endregion Content
  748. #pragma region JSONUnknownValue
  749. JSONUnknownValue::JSONUnknownValue(Text path, JSONValue* foundValue)
  750. : JSONValidationResult()
  751. {
  752. this->path = path;
  753. this->foundValue = foundValue;
  754. }
  755. JSONUnknownValue::~JSONUnknownValue()
  756. {
  757. foundValue->release();
  758. }
  759. bool JSONUnknownValue::isValid() const
  760. {
  761. return 0;
  762. }
  763. void JSONUnknownValue::printInvalidInfo(int indent) const
  764. {
  765. Text ind = "";
  766. ind.fillText(' ', indent);
  767. std::cout << ind.getText() << "Unknown Value at '" << path.getText()
  768. << "'. Value found:\n"
  769. << ind.getText() << foundValue->toString().getText() << "\n";
  770. }
  771. JSONValue* JSONUnknownValue::getValidPart(
  772. RCArray<JSONValidationResult>* removedPartsValidationResults)
  773. {
  774. if (removedPartsValidationResults)
  775. {
  776. removedPartsValidationResults->add(
  777. dynamic_cast<JSONValidationResult*>(getThis()));
  778. }
  779. return 0;
  780. }
  781. Text JSONUnknownValue::getPath() const
  782. {
  783. return path;
  784. }
  785. bool JSONUnknownValue::isDifferent(
  786. const JSONValidationResult* zResult, Text additionalPath) const
  787. {
  788. const JSONUnknownValue* casted
  789. = dynamic_cast<const JSONUnknownValue*>(zResult);
  790. if (casted == 0) return 1;
  791. if (!casted->getPath().istGleich(additionalPath + path)) return 1;
  792. return 0;
  793. }
  794. #pragma endregion Content
  795. #pragma region JSONMissingValue
  796. JSONMissingValue::JSONMissingValue(Text path, XML::Element* expected)
  797. : JSONValidationResult()
  798. {
  799. this->path = path;
  800. this->expected = expected;
  801. }
  802. JSONMissingValue::~JSONMissingValue()
  803. {
  804. expected->release();
  805. }
  806. bool JSONMissingValue::isValid() const
  807. {
  808. return 0;
  809. }
  810. void JSONMissingValue::printInvalidInfo(int indent) const
  811. {
  812. Text ind = "";
  813. ind.fillText(' ', indent);
  814. std::cout << ind.getText() << "Missing Value at '" << path.getText()
  815. << "'. Expected type:\n"
  816. << ind.getText() << expected->toString().getText() << "\n";
  817. }
  818. JSONValue* JSONMissingValue::getValidPart(
  819. RCArray<JSONValidationResult>* removedPartsValidationResults)
  820. {
  821. if (expected->hasAttribute("default"))
  822. {
  823. JSONValue* def
  824. = Parser::getValue(expected->getAttributeValue("default"));
  825. JSONValidationResult* res
  826. = JSONValidator(dynamic_cast<XML::Element*>(expected->getThis()))
  827. .validate(def);
  828. if (res->isValid())
  829. {
  830. res->release();
  831. return def;
  832. }
  833. else if (res->isDifferent(this, path))
  834. {
  835. def->release();
  836. JSONValue* result
  837. = res->getValidPart(removedPartsValidationResults);
  838. res->release();
  839. return result;
  840. }
  841. def->release();
  842. }
  843. if (removedPartsValidationResults)
  844. {
  845. removedPartsValidationResults->add(
  846. dynamic_cast<JSONValidationResult*>(getThis()));
  847. }
  848. return 0;
  849. }
  850. Text JSONMissingValue::getPath() const
  851. {
  852. return path;
  853. }
  854. bool JSONMissingValue::isDifferent(
  855. const JSONValidationResult* zResult, Text additionalPath) const
  856. {
  857. const JSONMissingValue* casted
  858. = dynamic_cast<const JSONMissingValue*>(zResult);
  859. if (casted == 0) return 1;
  860. if (!casted->getPath().istGleich(additionalPath + path)) return 1;
  861. return 0;
  862. }
  863. #pragma endregion Content
  864. #pragma region JSONMissingOneOf
  865. JSONMissingOneOf::JSONMissingOneOf(Text path, XML::Editor expected)
  866. : JSONValidationResult()
  867. {
  868. this->path = path;
  869. for (XML::Element* e : expected)
  870. this->expected.add(dynamic_cast<XML::Element*>(e->getThis()));
  871. }
  872. JSONMissingOneOf::~JSONMissingOneOf() {}
  873. bool JSONMissingOneOf::isValid() const
  874. {
  875. return 0;
  876. }
  877. void JSONMissingOneOf::printInvalidInfo(int indent) const
  878. {
  879. Text ind = "";
  880. ind.fillText(' ', indent);
  881. std::cout << ind.getText() << "Missing Value at '" << path.getText()
  882. << "'. The value should have one of the following types:\n";
  883. ind += " ";
  884. for (XML::Element* e : expected)
  885. {
  886. std::cout << ind.getText() << e->toString().getText() << "\n";
  887. }
  888. }
  889. JSONValue* JSONMissingOneOf::getValidPart(
  890. RCArray<JSONValidationResult>* removedPartsValidationResults)
  891. {
  892. if (removedPartsValidationResults)
  893. {
  894. removedPartsValidationResults->add(
  895. dynamic_cast<JSONValidationResult*>(getThis()));
  896. }
  897. // multiple possibilities are undecidable
  898. return 0;
  899. }
  900. Text JSONMissingOneOf::getPath() const
  901. {
  902. return path;
  903. }
  904. bool JSONMissingOneOf::isDifferent(
  905. const JSONValidationResult* zResult, Text additionalPath) const
  906. {
  907. const JSONMissingOneOf* casted
  908. = dynamic_cast<const JSONMissingOneOf*>(zResult);
  909. if (casted == 0) return 1;
  910. if (!casted->getPath().istGleich(additionalPath + path)) return 1;
  911. return 0;
  912. }
  913. #pragma endregion Content
  914. #pragma region JSONNoTypeMatching
  915. JSONNoTypeMatching::JSONNoTypeMatching(Text path,
  916. JSONValue* foundValue,
  917. RCArray<XML::Element>& expected,
  918. RCArray<JSONValidationResult>& reasons)
  919. : JSONValidationResult()
  920. {
  921. this->path = path;
  922. this->foundValue = foundValue;
  923. this->expected = expected;
  924. this->reasons = reasons;
  925. }
  926. JSONNoTypeMatching::~JSONNoTypeMatching()
  927. {
  928. foundValue->release();
  929. }
  930. bool JSONNoTypeMatching::isValid() const
  931. {
  932. return 0;
  933. }
  934. void JSONNoTypeMatching::printInvalidInfo(int indent) const
  935. {
  936. Text ind = "";
  937. ind.fillText(' ', indent);
  938. std::cout << ind.getText() << "Found Value at '" << path.getText()
  939. << "' did not match any of the given possible types:\n";
  940. Text ind2 = ind + " ";
  941. for (XML::Element* element : expected)
  942. {
  943. std::cout << ind2.getText() << element->toString().getText() << "\n";
  944. }
  945. std::cout << ind.getText() << "Reasons:\n";
  946. for (JSONValidationResult* reason : reasons)
  947. {
  948. reason->printInvalidInfo(indent + 4);
  949. }
  950. }
  951. JSONValue* JSONNoTypeMatching::getValidPart(
  952. RCArray<JSONValidationResult>* removedPartsValidationResults)
  953. {
  954. if (removedPartsValidationResults)
  955. {
  956. removedPartsValidationResults->add(
  957. dynamic_cast<JSONValidationResult*>(getThis()));
  958. }
  959. // multiple possibilities are undecidable
  960. return 0;
  961. }
  962. Text JSONNoTypeMatching::getPath() const
  963. {
  964. return path;
  965. }
  966. bool JSONNoTypeMatching::isDifferent(
  967. const JSONValidationResult* zResult, Text additionalPath) const
  968. {
  969. const JSONNoTypeMatching* casted
  970. = dynamic_cast<const JSONNoTypeMatching*>(zResult);
  971. if (casted == 0) return 1;
  972. if (!casted->getPath().istGleich(additionalPath + path)) return 1;
  973. for (int i = 0; i < reasons.getEintragAnzahl(); i++)
  974. {
  975. if (reasons.z(i)->isDifferent(casted->reasons.z(i), additionalPath))
  976. return 1;
  977. }
  978. return 0;
  979. }
  980. #pragma endregion Content
  981. #pragma region JSONValidValue
  982. JSONValidValue::JSONValidValue(Text path, JSONValue* value)
  983. : JSONValidationResult()
  984. {
  985. this->path = path;
  986. this->value = value;
  987. }
  988. JSONValidValue::~JSONValidValue()
  989. {
  990. value->release();
  991. }
  992. bool JSONValidValue::isValid() const
  993. {
  994. return 1;
  995. }
  996. void JSONValidValue::printInvalidInfo(int indent) const {}
  997. JSONValue* JSONValidValue::getValidPart(
  998. RCArray<JSONValidationResult>* removedPartsValidationResults)
  999. {
  1000. return value->clone();
  1001. }
  1002. Text JSONValidValue::getPath() const
  1003. {
  1004. return path;
  1005. }
  1006. bool JSONValidValue::isDifferent(
  1007. const JSONValidationResult* zResult, Text additionalPath) const
  1008. {
  1009. const JSONValidValue* casted = dynamic_cast<const JSONValidValue*>(zResult);
  1010. if (casted == 0) return 1;
  1011. if (!casted->getPath().istGleich(additionalPath + path)) return 1;
  1012. return 0;
  1013. }
  1014. #pragma endregion Content
  1015. #pragma endregion Content
  1016. #pragma region JSONValidator
  1017. JSONValidator::JSONValidator(XML::Element* constraints)
  1018. : ReferenceCounter(),
  1019. constraints(constraints)
  1020. {}
  1021. JSONValidator::~JSONValidator()
  1022. {
  1023. constraints->release();
  1024. }
  1025. JSONValidationResult* JSONValidator::validate(JSONValue* zValue) const
  1026. {
  1027. return validate(zValue, constraints, "");
  1028. }
  1029. bool JSONValidator::isValid(JSONValue* zValue) const
  1030. {
  1031. JSONValidationResult* res = validate(zValue);
  1032. if (res->isValid())
  1033. {
  1034. res->release();
  1035. return 1;
  1036. }
  1037. res->release();
  1038. return 0;
  1039. }
  1040. JSONValue* JSONValidator::getValidParts(JSONValue* zValue,
  1041. RCArray<JSONValidationResult>* removedPartsValidationResults) const
  1042. {
  1043. JSONValidationResult* res = validate(zValue);
  1044. if (res->isValid())
  1045. {
  1046. res->release();
  1047. return zValue->clone();
  1048. }
  1049. JSONValue* valid = res->getValidPart(removedPartsValidationResults);
  1050. res->release();
  1051. return valid;
  1052. }
  1053. XML::Element* JSONValidator::zConstraints()
  1054. {
  1055. return constraints;
  1056. }
  1057. JSONValidationResult* JSONValidator::validate(
  1058. JSONValue* zValue, XML::Element* zConstraints, Text path) const
  1059. {
  1060. if (zConstraints->getName().istGleich("oneOf"))
  1061. {
  1062. return validateMultipleTypes(zValue, zConstraints, path);
  1063. }
  1064. switch (zValue->getType())
  1065. {
  1066. case JSONType::NULL_:
  1067. if (!zConstraints->hasAttribute("nullable")
  1068. || !zConstraints->getAttributeValue("nullable").istGleich("true"))
  1069. {
  1070. return new JSONTypeMissmatch(path,
  1071. dynamic_cast<JSONValue*>(zValue->getThis()),
  1072. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1073. 0);
  1074. }
  1075. break;
  1076. case JSONType::BOOLEAN:
  1077. if (!zConstraints->getName().istGleich("bool"))
  1078. {
  1079. return new JSONTypeMissmatch(path,
  1080. dynamic_cast<JSONValue*>(zValue->getThis()),
  1081. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1082. 0);
  1083. }
  1084. else if (zConstraints->hasAttribute("equals"))
  1085. {
  1086. if (!zConstraints->getAttributeValue("equals").istGleich("true")
  1087. == !zValue->asBool()->getBool())
  1088. {
  1089. return new JSONTypeMissmatch(path,
  1090. dynamic_cast<JSONValue*>(zValue->getThis()),
  1091. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1092. 0);
  1093. }
  1094. }
  1095. break;
  1096. case JSONType::NUMBER:
  1097. if (!zConstraints->getName().istGleich("number"))
  1098. {
  1099. return new JSONTypeMissmatch(path,
  1100. dynamic_cast<JSONValue*>(zValue->getThis()),
  1101. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1102. 0);
  1103. }
  1104. else
  1105. {
  1106. if (zConstraints->hasAttribute("equals")
  1107. && (double)zConstraints->getAttributeValue("equals")
  1108. != zValue->asNumber()->getNumber())
  1109. {
  1110. return new JSONTypeMissmatch(path,
  1111. dynamic_cast<JSONValue*>(zValue->getThis()),
  1112. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1113. 0);
  1114. }
  1115. if (zConstraints->hasAttribute("lessOrEqual")
  1116. && zValue->asNumber()->getNumber()
  1117. > (double)zConstraints->getAttributeValue("lessOrEqual"))
  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("greaterOrEqual")
  1125. && zValue->asNumber()->getNumber()
  1126. < (double)zConstraints->getAttributeValue(
  1127. "greaterOrEqual"))
  1128. {
  1129. return new JSONTypeMissmatch(path,
  1130. dynamic_cast<JSONValue*>(zValue->getThis()),
  1131. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1132. 0);
  1133. }
  1134. if (zConstraints->hasAttribute("less")
  1135. && zValue->asNumber()->getNumber()
  1136. >= (double)zConstraints->getAttributeValue("less"))
  1137. {
  1138. return new JSONTypeMissmatch(path,
  1139. dynamic_cast<JSONValue*>(zValue->getThis()),
  1140. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1141. 0);
  1142. }
  1143. if (zConstraints->hasAttribute("greater")
  1144. && zValue->asNumber()->getNumber()
  1145. <= (double)zConstraints->getAttributeValue("greater"))
  1146. {
  1147. return new JSONTypeMissmatch(path,
  1148. dynamic_cast<JSONValue*>(zValue->getThis()),
  1149. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1150. 0);
  1151. }
  1152. }
  1153. break;
  1154. case JSONType::STRING:
  1155. if (!zConstraints->getName().istGleich("string"))
  1156. {
  1157. return new JSONTypeMissmatch(path,
  1158. dynamic_cast<JSONValue*>(zValue->getThis()),
  1159. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1160. 0);
  1161. }
  1162. else
  1163. {
  1164. if (zConstraints->hasAttribute("equals")
  1165. && !zConstraints->getAttributeValue("equals").istGleich(
  1166. zValue->asString()->getString()))
  1167. {
  1168. return new JSONTypeMissmatch(path,
  1169. dynamic_cast<JSONValue*>(zValue->getThis()),
  1170. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1171. 0);
  1172. }
  1173. if (zConstraints->hasAttribute("contains")
  1174. && zValue->asString()->getString().hat(
  1175. zConstraints->getAttributeValue("contains").getText()))
  1176. {
  1177. return new JSONTypeMissmatch(path,
  1178. dynamic_cast<JSONValue*>(zValue->getThis()),
  1179. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1180. 0);
  1181. }
  1182. if (zConstraints->hasAttribute("startsWith")
  1183. && zValue->asString()->getString().positionVon(
  1184. zConstraints->getAttributeValue("startsWith").getText())
  1185. == 0)
  1186. {
  1187. return new JSONTypeMissmatch(path,
  1188. dynamic_cast<JSONValue*>(zValue->getThis()),
  1189. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1190. 0);
  1191. }
  1192. if (zConstraints->hasAttribute("endsWith")
  1193. && zValue->asString()->getString().positionVon(
  1194. zConstraints->getAttributeValue("endsWith").getText())
  1195. == zValue->asString()->getString().getLength()
  1196. - zConstraints->getAttributeValue("endsWith")
  1197. .getLength())
  1198. {
  1199. return new JSONTypeMissmatch(path,
  1200. dynamic_cast<JSONValue*>(zValue->getThis()),
  1201. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1202. 0);
  1203. }
  1204. if (zConstraints->hasAttribute("oneOf"))
  1205. {
  1206. JSONArray* array
  1207. = Parser::getValue(zConstraints->getAttributeValue("oneOf"))
  1208. ->asArray();
  1209. bool ok = 0;
  1210. for (JSONValue* v : *array)
  1211. {
  1212. if (v->asString()->getString().istGleich(
  1213. zValue->asString()->getString()))
  1214. {
  1215. ok = 1;
  1216. break;
  1217. }
  1218. }
  1219. array->release();
  1220. if (!ok)
  1221. {
  1222. return new JSONTypeMissmatch(path,
  1223. dynamic_cast<JSONValue*>(zValue->getThis()),
  1224. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1225. 0);
  1226. }
  1227. }
  1228. }
  1229. break;
  1230. case JSONType::ARRAY:
  1231. if (!zConstraints->getName().istGleich("array"))
  1232. {
  1233. return new JSONTypeMissmatch(path,
  1234. dynamic_cast<JSONValue*>(zValue->getThis()),
  1235. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1236. 0);
  1237. }
  1238. else
  1239. {
  1240. int index = 0;
  1241. for (JSON::JSONValue* value : *zValue->asArray())
  1242. {
  1243. Text p = path;
  1244. p += "[";
  1245. p += index;
  1246. p += "]";
  1247. JSONValidationResult* res
  1248. = validateMultipleTypes(value, zConstraints, p);
  1249. if (!res->isValid())
  1250. {
  1251. return new JSONTypeMissmatch(path,
  1252. dynamic_cast<JSONValue*>(zValue->getThis()),
  1253. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1254. res);
  1255. }
  1256. res->release();
  1257. index++;
  1258. }
  1259. }
  1260. break;
  1261. case JSONType::OBJECT:
  1262. if (zConstraints->getName().istGleich("objectRef"))
  1263. {
  1264. Text id = zConstraints->getAttributeValue("ref");
  1265. XML::Editor editor = constraints->select()
  1266. .selectAllElements()
  1267. .whereNameEquals("object")
  1268. .whereAttributeEquals("id", id);
  1269. if (editor.getSize())
  1270. {
  1271. zConstraints = editor.begin().val();
  1272. }
  1273. }
  1274. if (!zConstraints->getName().istGleich("object"))
  1275. {
  1276. return new JSONTypeMissmatch(path,
  1277. dynamic_cast<JSONValue*>(zValue->getThis()),
  1278. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1279. 0);
  1280. }
  1281. else
  1282. {
  1283. JSON::JSONObject* obj = zValue->asObject();
  1284. for (auto i = obj->getFields(); i; i++)
  1285. {
  1286. Text p = path;
  1287. p += ".";
  1288. p += i.val();
  1289. if (!zConstraints->selectChildsByAttribute("name", i.val())
  1290. .exists())
  1291. {
  1292. return new JSONTypeMissmatch(path,
  1293. dynamic_cast<JSONValue*>(zValue->getThis()),
  1294. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1295. new JSONUnknownValue(
  1296. p, zValue->asObject()->getValue(i.val())));
  1297. }
  1298. else
  1299. {
  1300. XML::Editor tmp = zConstraints->selectChildsByAttribute(
  1301. "name", i.val());
  1302. JSONValidationResult* res = validateMultipleTypes(
  1303. zValue->asObject()->zValue(i.val()),
  1304. tmp.begin().val(),
  1305. p);
  1306. if (!res->isValid())
  1307. {
  1308. return new JSONTypeMissmatch(path,
  1309. dynamic_cast<JSONValue*>(zValue->getThis()),
  1310. dynamic_cast<XML::Element*>(
  1311. zConstraints->getThis()),
  1312. res);
  1313. }
  1314. res->release();
  1315. }
  1316. }
  1317. for (XML::Element* constraint : zConstraints->selectChildren())
  1318. {
  1319. if (!zValue->asObject()->hasValue(
  1320. constraint->getAttributeValue("name")))
  1321. {
  1322. XML::Editor tmp = constraint->selectChildren();
  1323. bool optional = true;
  1324. std::function<void(XML::Element*)> checkOptional;
  1325. checkOptional = [&optional, &checkOptional](
  1326. XML::Element* zElement) {
  1327. if (zElement->getName().istGleich("oneOf"))
  1328. {
  1329. XML::Editor tmp = zElement->selectChildren();
  1330. tmp.forEach(checkOptional);
  1331. }
  1332. else
  1333. {
  1334. optional &= zElement->hasAttribute("optional")
  1335. && zElement->getAttributeValue("optional")
  1336. .istGleich("true");
  1337. }
  1338. };
  1339. tmp.forEach(checkOptional);
  1340. if (!optional)
  1341. {
  1342. Text p = path;
  1343. p += ".";
  1344. p += constraint->getAttributeValue("name");
  1345. if (constraint->getChildCount() != 1)
  1346. return new JSONTypeMissmatch(path,
  1347. dynamic_cast<JSONValue*>(zValue->getThis()),
  1348. dynamic_cast<XML::Element*>(
  1349. zConstraints->getThis()),
  1350. new JSONMissingOneOf(p, tmp));
  1351. return new JSONTypeMissmatch(path,
  1352. dynamic_cast<JSONValue*>(zValue->getThis()),
  1353. dynamic_cast<XML::Element*>(
  1354. zConstraints->getThis()),
  1355. new JSONMissingValue(p,
  1356. dynamic_cast<XML::Element*>(
  1357. tmp.begin()->getThis())));
  1358. }
  1359. }
  1360. }
  1361. }
  1362. break;
  1363. }
  1364. return new JSONValidValue(
  1365. path, dynamic_cast<JSONValue*>(zValue->getThis()));
  1366. }
  1367. JSONValidationResult* JSONValidator::validateMultipleTypes(
  1368. JSONValue* zChildValue,
  1369. XML::Element* zPossibleChildConstraints,
  1370. Text childPath) const
  1371. {
  1372. if (zPossibleChildConstraints->getChildCount() == 1
  1373. && !zPossibleChildConstraints->hasAttribute("typeSpecifiedBy"))
  1374. {
  1375. XML::Editor children = zPossibleChildConstraints->selectChildren();
  1376. return validate(zChildValue,
  1377. children.begin().val(),
  1378. childPath); // only one type is possible
  1379. }
  1380. bool hasTypeAttr = 0;
  1381. RCArray<XML::Element> possibleConstraints;
  1382. if (zPossibleChildConstraints->hasAttribute("typeSpecifiedBy"))
  1383. { // try to find the correct constraints based on the type attribute
  1384. hasTypeAttr = 1;
  1385. Text typeAttr
  1386. = zPossibleChildConstraints->getAttributeValue("typeSpecifiedBy");
  1387. if (zChildValue->getType() == JSONType::OBJECT)
  1388. {
  1389. if (zChildValue->asObject()->hasValue(typeAttr))
  1390. {
  1391. JSONValue* typeV = zChildValue->asObject()->zValue(typeAttr);
  1392. for (XML::Element* constraint :
  1393. zPossibleChildConstraints->selectChildsByName("object")
  1394. .whereChildWithAttributeExists("name", typeAttr))
  1395. {
  1396. XML::Editor nameChildren
  1397. = constraint->selectChildsByAttribute("name", typeAttr);
  1398. XML::Element* typeAttrContraints
  1399. = nameChildren.begin().val();
  1400. JSONValidationResult* res = validateMultipleTypes(
  1401. typeV, typeAttrContraints, childPath + "." + typeAttr);
  1402. if (res->isValid())
  1403. possibleConstraints.add(
  1404. dynamic_cast<XML::Element*>(constraint->getThis()));
  1405. res->release();
  1406. }
  1407. }
  1408. }
  1409. }
  1410. if (hasTypeAttr && possibleConstraints.getEintragAnzahl() == 1)
  1411. return validate(zChildValue,
  1412. possibleConstraints.begin().val(),
  1413. childPath); // if the type is clear
  1414. else if (hasTypeAttr && possibleConstraints.getEintragAnzahl() > 0)
  1415. { // more then one type is possible
  1416. RCArray<JSONValidationResult> invalidResults;
  1417. for (XML::Element* constraint : possibleConstraints)
  1418. {
  1419. JSONValidationResult* res
  1420. = validate(zChildValue, constraint, childPath);
  1421. invalidResults.add(res);
  1422. if (res->isValid())
  1423. return new JSONValidValue(childPath,
  1424. dynamic_cast<JSONValue*>(zChildValue->getThis()));
  1425. }
  1426. return new JSONNoTypeMatching(childPath,
  1427. dynamic_cast<JSONValue*>(zChildValue->getThis()),
  1428. possibleConstraints,
  1429. invalidResults);
  1430. }
  1431. // try all types
  1432. possibleConstraints.leeren();
  1433. RCArray<JSONValidationResult> invalidResults;
  1434. for (XML::Element* constraint : zPossibleChildConstraints->selectChildren())
  1435. {
  1436. JSONValidationResult* res
  1437. = validate(zChildValue, constraint, childPath);
  1438. invalidResults.add(res);
  1439. if (res->isValid())
  1440. return new JSONValidValue(
  1441. childPath, dynamic_cast<JSONValue*>(zChildValue->getThis()));
  1442. possibleConstraints.add(
  1443. dynamic_cast<XML::Element*>(constraint->getThis()));
  1444. }
  1445. return new JSONNoTypeMatching(childPath,
  1446. dynamic_cast<JSONValue*>(zChildValue->getThis()),
  1447. possibleConstraints,
  1448. invalidResults);
  1449. }
  1450. StringValidationBuilder<JSONValidator>* JSONValidator::buildForString()
  1451. {
  1452. return new StringValidationBuilder<JSONValidator>(
  1453. [](XML::Element& e) { return new JSONValidator(e.dublicate()); });
  1454. }
  1455. NumberValidationBuilder<JSONValidator>* JSONValidator::buildForNumber()
  1456. {
  1457. return new NumberValidationBuilder<JSONValidator>(
  1458. [](XML::Element& e) { return new JSONValidator(e.dublicate()); });
  1459. }
  1460. BoolValidationBuilder<JSONValidator>* JSONValidator::buildForBool()
  1461. {
  1462. return new BoolValidationBuilder<JSONValidator>(
  1463. [](XML::Element& e) { return new JSONValidator(e.dublicate()); });
  1464. }
  1465. ObjectValidationBuilder<JSONValidator>* JSONValidator::buildForObject()
  1466. {
  1467. return new ObjectValidationBuilder<JSONValidator>(
  1468. [](XML::Element& e) { return new JSONValidator(e.dublicate()); });
  1469. }
  1470. ArrayValidationBuilder<JSONValidator>* JSONValidator::buildForArray()
  1471. {
  1472. return new ArrayValidationBuilder<JSONValidator>(
  1473. [](XML::Element& e) { return new JSONValidator(e.dublicate()); });
  1474. }
  1475. JSONValidator* JSONValidator::buildForObjectReference(Text objectId)
  1476. {
  1477. return new JSONValidator(
  1478. new XML::Element(Text("<objectRef ref=\"") + objectId + Text("\"/>")));
  1479. }
  1480. OneOfValidationBuilder<JSONValidator>* JSONValidator::buildForOneOf()
  1481. {
  1482. return new OneOfValidationBuilder<JSONValidator>(
  1483. [](XML::Element& e) { return new JSONValidator(e.dublicate()); });
  1484. }
  1485. #pragma endregion Content