JSON.cpp 50 KB

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