JSON.cpp 46 KB

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