JSON.cpp 49 KB

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