JSON.cpp 46 KB

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