JSON.cpp 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617
  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. switch (zValue->getType())
  1061. {
  1062. case JSONType::NULL_:
  1063. if (!zConstraints->hasAttribute("nullable")
  1064. || !zConstraints->getAttributeValue("nullable").istGleich("true"))
  1065. {
  1066. return new JSONTypeMissmatch(path,
  1067. dynamic_cast<JSONValue*>(zValue->getThis()),
  1068. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1069. 0);
  1070. }
  1071. break;
  1072. case JSONType::BOOLEAN:
  1073. if (!zConstraints->getName().istGleich("bool"))
  1074. {
  1075. return new JSONTypeMissmatch(path,
  1076. dynamic_cast<JSONValue*>(zValue->getThis()),
  1077. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1078. 0);
  1079. }
  1080. else if (zConstraints->hasAttribute("equals"))
  1081. {
  1082. if (!zConstraints->getAttributeValue("equals").istGleich("true")
  1083. == !zValue->asBool()->getBool())
  1084. {
  1085. return new JSONTypeMissmatch(path,
  1086. dynamic_cast<JSONValue*>(zValue->getThis()),
  1087. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1088. 0);
  1089. }
  1090. }
  1091. break;
  1092. case JSONType::NUMBER:
  1093. if (!zConstraints->getName().istGleich("number"))
  1094. {
  1095. return new JSONTypeMissmatch(path,
  1096. dynamic_cast<JSONValue*>(zValue->getThis()),
  1097. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1098. 0);
  1099. }
  1100. else
  1101. {
  1102. if (zConstraints->hasAttribute("equals")
  1103. && (double)zConstraints->getAttributeValue("equals")
  1104. != zValue->asNumber()->getNumber())
  1105. {
  1106. return new JSONTypeMissmatch(path,
  1107. dynamic_cast<JSONValue*>(zValue->getThis()),
  1108. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1109. 0);
  1110. }
  1111. if (zConstraints->hasAttribute("lessOrEqual")
  1112. && zValue->asNumber()->getNumber()
  1113. > (double)zConstraints->getAttributeValue("lessOrEqual"))
  1114. {
  1115. return new JSONTypeMissmatch(path,
  1116. dynamic_cast<JSONValue*>(zValue->getThis()),
  1117. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1118. 0);
  1119. }
  1120. if (zConstraints->hasAttribute("greaterOrEqual")
  1121. && zValue->asNumber()->getNumber()
  1122. < (double)zConstraints->getAttributeValue(
  1123. "greaterOrEqual"))
  1124. {
  1125. return new JSONTypeMissmatch(path,
  1126. dynamic_cast<JSONValue*>(zValue->getThis()),
  1127. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1128. 0);
  1129. }
  1130. if (zConstraints->hasAttribute("less")
  1131. && zValue->asNumber()->getNumber()
  1132. >= (double)zConstraints->getAttributeValue("less"))
  1133. {
  1134. return new JSONTypeMissmatch(path,
  1135. dynamic_cast<JSONValue*>(zValue->getThis()),
  1136. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1137. 0);
  1138. }
  1139. if (zConstraints->hasAttribute("greater")
  1140. && zValue->asNumber()->getNumber()
  1141. <= (double)zConstraints->getAttributeValue("greater"))
  1142. {
  1143. return new JSONTypeMissmatch(path,
  1144. dynamic_cast<JSONValue*>(zValue->getThis()),
  1145. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1146. 0);
  1147. }
  1148. }
  1149. break;
  1150. case JSONType::STRING:
  1151. if (!zConstraints->getName().istGleich("string"))
  1152. {
  1153. return new JSONTypeMissmatch(path,
  1154. dynamic_cast<JSONValue*>(zValue->getThis()),
  1155. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1156. 0);
  1157. }
  1158. else
  1159. {
  1160. if (zConstraints->hasAttribute("equals")
  1161. && !zConstraints->getAttributeValue("equals").istGleich(
  1162. zValue->asString()->getString()))
  1163. {
  1164. return new JSONTypeMissmatch(path,
  1165. dynamic_cast<JSONValue*>(zValue->getThis()),
  1166. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1167. 0);
  1168. }
  1169. if (zConstraints->hasAttribute("contains")
  1170. && zValue->asString()->getString().hat(
  1171. zConstraints->getAttributeValue("contains").getText()))
  1172. {
  1173. return new JSONTypeMissmatch(path,
  1174. dynamic_cast<JSONValue*>(zValue->getThis()),
  1175. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1176. 0);
  1177. }
  1178. if (zConstraints->hasAttribute("startsWith")
  1179. && zValue->asString()->getString().positionVon(
  1180. zConstraints->getAttributeValue("startsWith").getText())
  1181. == 0)
  1182. {
  1183. return new JSONTypeMissmatch(path,
  1184. dynamic_cast<JSONValue*>(zValue->getThis()),
  1185. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1186. 0);
  1187. }
  1188. if (zConstraints->hasAttribute("endsWith")
  1189. && zValue->asString()->getString().positionVon(
  1190. zConstraints->getAttributeValue("endsWith").getText())
  1191. == zValue->asString()->getString().getLength()
  1192. - zConstraints->getAttributeValue("endsWith")
  1193. .getLength())
  1194. {
  1195. return new JSONTypeMissmatch(path,
  1196. dynamic_cast<JSONValue*>(zValue->getThis()),
  1197. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1198. 0);
  1199. }
  1200. if (zConstraints->hasAttribute("oneOf"))
  1201. {
  1202. JSONArray* array
  1203. = Parser::getValue(zConstraints->getAttributeValue("oneOf"))
  1204. ->asArray();
  1205. bool ok = 0;
  1206. for (JSONValue* v : *array)
  1207. {
  1208. if (v->asString()->getString().istGleich(
  1209. zValue->asString()->getString()))
  1210. {
  1211. ok = 1;
  1212. break;
  1213. }
  1214. }
  1215. array->release();
  1216. if (!ok)
  1217. {
  1218. return new JSONTypeMissmatch(path,
  1219. dynamic_cast<JSONValue*>(zValue->getThis()),
  1220. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1221. 0);
  1222. }
  1223. }
  1224. }
  1225. break;
  1226. case JSONType::ARRAY:
  1227. if (!zConstraints->getName().istGleich("array"))
  1228. {
  1229. return new JSONTypeMissmatch(path,
  1230. dynamic_cast<JSONValue*>(zValue->getThis()),
  1231. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1232. 0);
  1233. }
  1234. else
  1235. {
  1236. int index = 0;
  1237. for (JSON::JSONValue* value : *zValue->asArray())
  1238. {
  1239. Text p = path;
  1240. p += "[";
  1241. p += index;
  1242. p += "]";
  1243. JSONValidationResult* res
  1244. = validateMultipleTypes(value, zConstraints, p);
  1245. if (!res->isValid())
  1246. {
  1247. return new JSONTypeMissmatch(path,
  1248. dynamic_cast<JSONValue*>(zValue->getThis()),
  1249. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1250. res);
  1251. }
  1252. res->release();
  1253. index++;
  1254. }
  1255. }
  1256. break;
  1257. case JSONType::OBJECT:
  1258. if (zConstraints->getName().istGleich("objectRef"))
  1259. {
  1260. Text id = zConstraints->getAttributeValue("ref");
  1261. XML::Editor editor = constraints->select()
  1262. .selectAllElements()
  1263. .whereNameEquals("object")
  1264. .whereAttributeEquals("id", id);
  1265. if (editor.getSize())
  1266. {
  1267. zConstraints = editor.begin().val();
  1268. }
  1269. }
  1270. if (!zConstraints->getName().istGleich("object"))
  1271. {
  1272. return new JSONTypeMissmatch(path,
  1273. dynamic_cast<JSONValue*>(zValue->getThis()),
  1274. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1275. 0);
  1276. }
  1277. else
  1278. {
  1279. JSON::JSONObject* obj = zValue->asObject();
  1280. for (auto i = obj->getFields(); i; i++)
  1281. {
  1282. Text p = path;
  1283. p += ".";
  1284. p += i.val();
  1285. if (!zConstraints->selectChildsByAttribute("name", i.val())
  1286. .exists())
  1287. {
  1288. return new JSONTypeMissmatch(path,
  1289. dynamic_cast<JSONValue*>(zValue->getThis()),
  1290. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1291. new JSONUnknownValue(
  1292. p, zValue->asObject()->getValue(i.val())));
  1293. }
  1294. else
  1295. {
  1296. XML::Editor tmp = zConstraints->selectChildsByAttribute(
  1297. "name", i.val());
  1298. JSONValidationResult* res = validateMultipleTypes(
  1299. zValue->asObject()->zValue(i.val()),
  1300. tmp.begin().val(),
  1301. p);
  1302. if (!res->isValid())
  1303. {
  1304. return new JSONTypeMissmatch(path,
  1305. dynamic_cast<JSONValue*>(zValue->getThis()),
  1306. dynamic_cast<XML::Element*>(
  1307. zConstraints->getThis()),
  1308. res);
  1309. }
  1310. res->release();
  1311. }
  1312. }
  1313. for (XML::Element* constraint : zConstraints->selectChildren())
  1314. {
  1315. if (!zValue->asObject()->hasValue(
  1316. constraint->getAttributeValue("name")))
  1317. {
  1318. XML::Editor tmp = constraint->selectChildren();
  1319. bool optional = true;
  1320. tmp.forEach([&optional](XML::Element* zElement) {
  1321. optional &= zElement->hasAttribute("optional")
  1322. && zElement->getAttributeValue("optional")
  1323. .istGleich("true");
  1324. });
  1325. if (!optional)
  1326. {
  1327. Text p = path;
  1328. p += ".";
  1329. p += constraint->getAttributeValue("name");
  1330. if (constraint->getChildCount() != 1)
  1331. return new JSONTypeMissmatch(path,
  1332. dynamic_cast<JSONValue*>(zValue->getThis()),
  1333. dynamic_cast<XML::Element*>(
  1334. zConstraints->getThis()),
  1335. new JSONMissingOneOf(p, tmp));
  1336. return new JSONTypeMissmatch(path,
  1337. dynamic_cast<JSONValue*>(zValue->getThis()),
  1338. dynamic_cast<XML::Element*>(
  1339. zConstraints->getThis()),
  1340. new JSONMissingValue(p,
  1341. dynamic_cast<XML::Element*>(
  1342. tmp.begin()->getThis())));
  1343. }
  1344. }
  1345. }
  1346. }
  1347. break;
  1348. }
  1349. return new JSONValidValue(
  1350. path, dynamic_cast<JSONValue*>(zValue->getThis()));
  1351. }
  1352. JSONValidationResult* JSONValidator::validateMultipleTypes(
  1353. JSONValue* zChildValue,
  1354. XML::Element* zPossibleChildConstraints,
  1355. Text childPath) const
  1356. {
  1357. if (zPossibleChildConstraints->getChildCount() == 1)
  1358. {
  1359. XML::Editor children = zPossibleChildConstraints->selectChildren();
  1360. return validate(zChildValue,
  1361. children.begin().val(),
  1362. childPath); // only one type is possible
  1363. }
  1364. bool hasTypeAttr = 0;
  1365. RCArray<XML::Element> possibleConstraints;
  1366. if (zPossibleChildConstraints->hasAttribute("typeSpecifiedBy"))
  1367. { // try to find the correct constraints based on the type attribute
  1368. hasTypeAttr = 1;
  1369. Text typeAttr
  1370. = zPossibleChildConstraints->getAttributeValue("typeSpecifiedBy");
  1371. if (zChildValue->getType() == JSONType::OBJECT)
  1372. {
  1373. if (zChildValue->asObject()->hasValue(typeAttr))
  1374. {
  1375. JSONValue* typeV = zChildValue->asObject()->zValue(typeAttr);
  1376. for (XML::Element* constraint :
  1377. zPossibleChildConstraints->selectChildsByName("object")
  1378. .whereChildWithAttributeExists("name", typeAttr))
  1379. {
  1380. XML::Editor nameChildren
  1381. = constraint->selectChildsByAttribute("name", typeAttr);
  1382. XML::Element* typeAttrContraints
  1383. = nameChildren.begin().val();
  1384. JSONValidationResult* res = validateMultipleTypes(
  1385. typeV, typeAttrContraints, childPath + "." + typeAttr);
  1386. if (res->isValid())
  1387. possibleConstraints.add(
  1388. dynamic_cast<XML::Element*>(constraint->getThis()));
  1389. res->release();
  1390. }
  1391. }
  1392. }
  1393. }
  1394. if (hasTypeAttr && possibleConstraints.getEintragAnzahl() == 1)
  1395. return validate(zChildValue,
  1396. possibleConstraints.begin().val(),
  1397. childPath); // if the type is clear
  1398. else if (hasTypeAttr && possibleConstraints.getEintragAnzahl() > 0)
  1399. { // more then one type is possible
  1400. RCArray<JSONValidationResult> invalidResults;
  1401. for (XML::Element* constraint : possibleConstraints)
  1402. {
  1403. JSONValidationResult* res
  1404. = validate(zChildValue, constraint, childPath);
  1405. invalidResults.add(res);
  1406. if (res->isValid())
  1407. return new JSONValidValue(childPath,
  1408. dynamic_cast<JSONValue*>(zChildValue->getThis()));
  1409. }
  1410. return new JSONNoTypeMatching(childPath,
  1411. dynamic_cast<JSONValue*>(zChildValue->getThis()),
  1412. possibleConstraints,
  1413. invalidResults);
  1414. }
  1415. // try all types
  1416. possibleConstraints.leeren();
  1417. RCArray<JSONValidationResult> invalidResults;
  1418. for (XML::Element* constraint : zPossibleChildConstraints->selectChildren())
  1419. {
  1420. JSONValidationResult* res
  1421. = validate(zChildValue, constraint, childPath);
  1422. invalidResults.add(res);
  1423. if (res->isValid())
  1424. return new JSONValidValue(
  1425. childPath, dynamic_cast<JSONValue*>(zChildValue->getThis()));
  1426. possibleConstraints.add(
  1427. dynamic_cast<XML::Element*>(constraint->getThis()));
  1428. }
  1429. return new JSONNoTypeMatching(childPath,
  1430. dynamic_cast<JSONValue*>(zChildValue->getThis()),
  1431. possibleConstraints,
  1432. invalidResults);
  1433. }
  1434. StringValidationBuilder<JSONValidator>* JSONValidator::buildForString()
  1435. {
  1436. return new StringValidationBuilder<JSONValidator>(
  1437. [](XML::Element& e) { return new JSONValidator(e.dublicate()); });
  1438. }
  1439. NumberValidationBuilder<JSONValidator>* JSONValidator::buildForNumber()
  1440. {
  1441. return new NumberValidationBuilder<JSONValidator>(
  1442. [](XML::Element& e) { return new JSONValidator(e.dublicate()); });
  1443. }
  1444. BoolValidationBuilder<JSONValidator>* JSONValidator::buildForBool()
  1445. {
  1446. return new BoolValidationBuilder<JSONValidator>(
  1447. [](XML::Element& e) { return new JSONValidator(e.dublicate()); });
  1448. }
  1449. ObjectValidationBuilder<JSONValidator>* JSONValidator::buildForObject()
  1450. {
  1451. return new ObjectValidationBuilder<JSONValidator>(
  1452. [](XML::Element& e) { return new JSONValidator(e.dublicate()); });
  1453. }
  1454. ArrayValidationBuilder<JSONValidator>* JSONValidator::buildForArray()
  1455. {
  1456. return new ArrayValidationBuilder<JSONValidator>(
  1457. [](XML::Element& e) { return new JSONValidator(e.dublicate()); });
  1458. }
  1459. JSONValidator* JSONValidator::buildForObjectReference(Text objectId)
  1460. {
  1461. return new JSONValidator(
  1462. new XML::Element(Text("<objectRef ref=\"") + objectId + Text("\"/>")));
  1463. }
  1464. #pragma endregion Content