JSON.cpp 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745
  1. #include "JSON.h"
  2. #include "Datei.h"
  3. using namespace Framework;
  4. using namespace JSON;
  5. #pragma region JSONValue
  6. JSONValue::JSONValue()
  7. : ReferenceCounter()
  8. {
  9. this->type = JSONType::NULL_;
  10. }
  11. JSONValue::~JSONValue() {}
  12. JSONValue::JSONValue(JSONType type)
  13. : ReferenceCounter()
  14. {
  15. this->type = type;
  16. }
  17. JSONType JSONValue::getType() const
  18. {
  19. return type;
  20. }
  21. Text JSONValue::toString() const
  22. {
  23. return Text("null");
  24. }
  25. JSONValue* JSONValue::clone() const
  26. {
  27. return new JSONValue();
  28. }
  29. JSONBool* JSONValue::asBool() const
  30. {
  31. return (JSONBool*)this;
  32. }
  33. JSONNumber* JSONValue::asNumber() const
  34. {
  35. return (JSONNumber*)this;
  36. }
  37. JSONString* JSONValue::asString() const
  38. {
  39. return (JSONString*)this;
  40. }
  41. JSONArray* JSONValue::asArray() const
  42. {
  43. return (JSONArray*)this;
  44. }
  45. JSONObject* JSONValue::asObject() const
  46. {
  47. return (JSONObject*)this;
  48. }
  49. #pragma endregion Content
  50. #pragma region JSONBool
  51. JSONBool::JSONBool(bool b)
  52. : JSONValue(JSONType::BOOLEAN)
  53. {
  54. this->b = b;
  55. }
  56. bool JSONBool::getBool() const
  57. {
  58. return b;
  59. }
  60. Text JSONBool::toString() const
  61. {
  62. if (b)
  63. return Text("true");
  64. else
  65. return Text("false");
  66. }
  67. JSONValue* JSONBool::clone() const
  68. {
  69. return new JSONBool(b);
  70. }
  71. #pragma endregion Content
  72. #pragma region JSONNumber
  73. JSONNumber::JSONNumber(double num)
  74. : JSONValue(JSONType::NUMBER)
  75. {
  76. number = num;
  77. }
  78. double JSONNumber::getNumber() const
  79. {
  80. return number;
  81. }
  82. Text JSONNumber::toString() const
  83. {
  84. return Text(number);
  85. }
  86. JSONValue* JSONNumber::clone() const
  87. {
  88. return new JSONNumber(number);
  89. }
  90. #pragma endregion Content
  91. #pragma region JSONString
  92. JSONString::JSONString(Text string)
  93. : JSONValue(JSONType::STRING)
  94. {
  95. this->string = string;
  96. this->string.ersetzen("\\\"", "\"");
  97. this->string.ersetzen("\\n", "\n");
  98. }
  99. Text JSONString::getString() const
  100. {
  101. return string;
  102. }
  103. Text JSONString::toString() const
  104. {
  105. Text esc = string;
  106. esc.ersetzen("\"", "\\\"");
  107. esc.ersetzen("\n", "\\n");
  108. return Text(Text("\"") += esc.getText()) += "\"";
  109. }
  110. JSONValue* JSONString::clone() const
  111. {
  112. Text esc = string;
  113. esc.ersetzen("\"", "\\\"");
  114. esc.ersetzen("\n", "\\n");
  115. return new JSONString(esc);
  116. }
  117. #pragma endregion Content
  118. #pragma region JSONArray
  119. JSONArray::JSONArray()
  120. : JSONValue(JSONType::ARRAY)
  121. {
  122. array = new RCArray<JSONValue>();
  123. }
  124. JSONArray::JSONArray(Text string)
  125. : JSONValue(JSONType::ARRAY)
  126. {
  127. array = new RCArray<JSONValue>();
  128. string = Parser::removeWhitespace(string);
  129. if (string.getText()[0] == '['
  130. && string.getText()[string.getLength() - 1] == ']')
  131. {
  132. string.remove(0, 1);
  133. string.remove(string.getLength() - 1, string.getLength());
  134. while (string.getLength())
  135. {
  136. int end = Parser::findObjectEndInArray(string);
  137. Text* objStr = string.getTeilText(0, end);
  138. string.remove(0, end + 1);
  139. array->add(Parser::getValue(*objStr));
  140. objStr->release();
  141. }
  142. }
  143. }
  144. JSONArray::JSONArray(const JSONArray& arr)
  145. : JSONValue(JSONType::ARRAY)
  146. {
  147. array = dynamic_cast<RCArray<JSONValue>*>(arr.array->getThis());
  148. }
  149. JSONArray::~JSONArray()
  150. {
  151. array->release();
  152. }
  153. JSONArray& JSONArray::operator=(const JSONArray& arr)
  154. {
  155. array->release();
  156. array = dynamic_cast<RCArray<JSONValue>*>(arr.array->getThis());
  157. return *this;
  158. }
  159. void JSONArray::addValue(JSONValue* value)
  160. {
  161. array->add(value);
  162. }
  163. void JSONArray::setValue(int i, JSONValue* value)
  164. {
  165. array->set(value, i);
  166. }
  167. void JSONArray::removeValue(int i)
  168. {
  169. array->remove(i);
  170. }
  171. JSONValue* JSONArray::getValue(int i) const
  172. {
  173. return array->get(i);
  174. }
  175. JSONValue* JSONArray::zValue(int i) const
  176. {
  177. return array->z(i);
  178. }
  179. int JSONArray::getLength() const
  180. {
  181. return array->getEintragAnzahl();
  182. }
  183. bool JSONArray::isValueOfType(int i, JSONType type) const
  184. {
  185. return i >= 0 && i < array->getEintragAnzahl()
  186. && array->z(i)->getType() == type;
  187. }
  188. Iterator<JSONValue*> JSONArray::begin() const
  189. {
  190. return array->begin();
  191. }
  192. Iterator<JSONValue*> JSONArray::end() const
  193. {
  194. return array->end();
  195. }
  196. Text JSONArray::toString() const
  197. {
  198. Text str = "[";
  199. for (auto i = array->begin(); i; i++)
  200. {
  201. str += i->toString();
  202. if (i.hasNext()) str += ",";
  203. }
  204. str += "]";
  205. return str;
  206. }
  207. JSONValue* JSONArray::clone() const
  208. {
  209. 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. Iterator<Text> JSONObject::getFields()
  317. {
  318. return fields->begin();
  319. }
  320. Iterator<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. Iterator<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. if (removedPartsValidationResults)
  960. {
  961. removedPartsValidationResults->add(
  962. dynamic_cast<JSONValidationResult*>(getThis()));
  963. }
  964. // multiple possibilities are undecidable
  965. return 0;
  966. }
  967. Text JSONMissingOneOf::getPath() const
  968. {
  969. return path;
  970. }
  971. bool JSONMissingOneOf::isDifferent(const JSONValidationResult* zResult) const
  972. {
  973. const JSONMissingOneOf* casted
  974. = dynamic_cast<const JSONMissingOneOf*>(zResult);
  975. if (casted == 0) return 1;
  976. if (!casted->getPath().istGleich(path)) return 1;
  977. return 0;
  978. }
  979. void Framework::JSON::Validator::JSONMissingOneOf::addBasePath(Text basePath)
  980. {
  981. path = basePath + path;
  982. }
  983. #pragma endregion Content
  984. #pragma region JSONNoTypeMatching
  985. JSONNoTypeMatching::JSONNoTypeMatching(Text path,
  986. JSONValue* foundValue,
  987. RCArray<XML::Element>& expected,
  988. RCArray<JSONValidationResult>& reasons)
  989. : JSONValidationResult()
  990. {
  991. this->path = path;
  992. this->foundValue = foundValue;
  993. this->expected = expected;
  994. this->reasons = reasons;
  995. }
  996. JSONNoTypeMatching::~JSONNoTypeMatching()
  997. {
  998. foundValue->release();
  999. }
  1000. bool JSONNoTypeMatching::isValid() const
  1001. {
  1002. return 0;
  1003. }
  1004. void JSONNoTypeMatching::printInvalidInfo(int indent) const
  1005. {
  1006. Text ind = "";
  1007. ind.fillText(' ', indent);
  1008. std::cout << ind.getText() << "Found Value at '" << path.getText()
  1009. << "' did not match any of the specified types\n";
  1010. Text ind2 = ind + " ";
  1011. std::cout << ind.getText() << "Reasons:\n";
  1012. for (JSONValidationResult* reason : reasons)
  1013. {
  1014. reason->printInvalidInfo(indent + 4);
  1015. }
  1016. }
  1017. JSONValue* JSONNoTypeMatching::getValidPart(
  1018. RCArray<JSONValidationResult>* removedPartsValidationResults)
  1019. {
  1020. RCArray<JSONValidationResult> tempErrors;
  1021. for (JSONValidationResult* reason : reasons)
  1022. {
  1023. JSONValue* result = reason->getValidPart(&tempErrors);
  1024. if (result)
  1025. {
  1026. return result;
  1027. }
  1028. }
  1029. if (removedPartsValidationResults)
  1030. {
  1031. removedPartsValidationResults->add(
  1032. dynamic_cast<JSONValidationResult*>(getThis()));
  1033. reasons.leeren();
  1034. for (JSONValidationResult* error : tempErrors)
  1035. {
  1036. reasons.add(dynamic_cast<JSONValidationResult*>(error->getThis()));
  1037. }
  1038. }
  1039. // multiple possibilities are undecidable
  1040. return 0;
  1041. }
  1042. Text JSONNoTypeMatching::getPath() const
  1043. {
  1044. return path;
  1045. }
  1046. bool JSONNoTypeMatching::isDifferent(const JSONValidationResult* zResult) const
  1047. {
  1048. const JSONNoTypeMatching* casted
  1049. = dynamic_cast<const JSONNoTypeMatching*>(zResult);
  1050. if (casted == 0) return 1;
  1051. if (!casted->getPath().istGleich(path)) return 1;
  1052. for (int i = 0; i < reasons.getEintragAnzahl(); i++)
  1053. {
  1054. if (reasons.z(i)->isDifferent(casted->reasons.z(i))) return 1;
  1055. }
  1056. return 0;
  1057. }
  1058. void Framework::JSON::Validator::JSONNoTypeMatching::addBasePath(Text basePath)
  1059. {
  1060. path = basePath + path;
  1061. for (JSONValidationResult* reason : reasons)
  1062. {
  1063. reason->addBasePath(basePath);
  1064. }
  1065. }
  1066. #pragma endregion Content
  1067. #pragma region JSONValidValue
  1068. JSONValidValue::JSONValidValue(Text path, JSONValue* value)
  1069. : JSONValidationResult()
  1070. {
  1071. this->path = path;
  1072. this->value = value;
  1073. }
  1074. JSONValidValue::~JSONValidValue()
  1075. {
  1076. value->release();
  1077. }
  1078. bool JSONValidValue::isValid() const
  1079. {
  1080. return 1;
  1081. }
  1082. void JSONValidValue::printInvalidInfo(int indent) const {}
  1083. JSONValue* JSONValidValue::getValidPart(
  1084. RCArray<JSONValidationResult>* removedPartsValidationResults)
  1085. {
  1086. return value->clone();
  1087. }
  1088. Text JSONValidValue::getPath() const
  1089. {
  1090. return path;
  1091. }
  1092. bool JSONValidValue::isDifferent(const JSONValidationResult* zResult) const
  1093. {
  1094. const JSONValidValue* casted = dynamic_cast<const JSONValidValue*>(zResult);
  1095. if (casted == 0) return 1;
  1096. if (!casted->getPath().istGleich(path)) return 1;
  1097. return 0;
  1098. }
  1099. void Framework::JSON::Validator::JSONValidValue::addBasePath(Text basePath)
  1100. {
  1101. path = basePath + path;
  1102. }
  1103. #pragma endregion Content
  1104. #pragma endregion Content
  1105. #pragma region JSONValidator
  1106. JSONValidator::JSONValidator(XML::Element* constraints)
  1107. : ReferenceCounter(),
  1108. constraints(constraints)
  1109. {
  1110. for (XML::Element* e : constraints->select()
  1111. .selectAllElements()
  1112. .whereNameEquals("object")
  1113. .whereAttributeExists("id"))
  1114. {
  1115. Framework::Text id = e->getAttributeValue("id");
  1116. typeConstraints.set(id, id.getLength(), e);
  1117. }
  1118. }
  1119. JSONValidator::~JSONValidator()
  1120. {
  1121. constraints->release();
  1122. }
  1123. JSONValidationResult* JSONValidator::validate(JSONValue* zValue) const
  1124. {
  1125. return validate(zValue, constraints, "");
  1126. }
  1127. bool JSONValidator::isValid(JSONValue* zValue) const
  1128. {
  1129. JSONValidationResult* res = validate(zValue);
  1130. if (res->isValid())
  1131. {
  1132. res->release();
  1133. return 1;
  1134. }
  1135. res->release();
  1136. return 0;
  1137. }
  1138. JSONValue* JSONValidator::getValidParts(JSONValue* zValue,
  1139. RCArray<JSONValidationResult>* removedPartsValidationResults) const
  1140. {
  1141. JSONValidationResult* res = validate(zValue);
  1142. if (res->isValid())
  1143. {
  1144. res->release();
  1145. return zValue->clone();
  1146. }
  1147. JSONValue* valid = res->getValidPart(removedPartsValidationResults);
  1148. res->release();
  1149. return valid;
  1150. }
  1151. XML::Element* JSONValidator::zConstraints()
  1152. {
  1153. return constraints;
  1154. }
  1155. JSONValidationResult* JSONValidator::validate(
  1156. JSONValue* zValue, XML::Element* zConstraints, Text path) const
  1157. {
  1158. if (zConstraints->getName().istGleich("oneOf"))
  1159. {
  1160. return validateMultipleTypes(zValue, zConstraints, path);
  1161. }
  1162. switch (zValue->getType())
  1163. {
  1164. case JSONType::NULL_:
  1165. if (!zConstraints->hasAttribute("nullable")
  1166. || !zConstraints->getAttributeValue("nullable").istGleich("true"))
  1167. {
  1168. return new JSONTypeMissmatch(path,
  1169. dynamic_cast<JSONValue*>(zValue->getThis()),
  1170. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1171. 0);
  1172. }
  1173. break;
  1174. case JSONType::BOOLEAN:
  1175. if (!zConstraints->getName().istGleich("bool"))
  1176. {
  1177. return new JSONTypeMissmatch(path,
  1178. dynamic_cast<JSONValue*>(zValue->getThis()),
  1179. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1180. 0);
  1181. }
  1182. else if (zConstraints->hasAttribute("equals"))
  1183. {
  1184. if (!zConstraints->getAttributeValue("equals").istGleich("true")
  1185. == !zValue->asBool()->getBool())
  1186. {
  1187. return new JSONTypeMissmatch(path,
  1188. dynamic_cast<JSONValue*>(zValue->getThis()),
  1189. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1190. 0);
  1191. }
  1192. }
  1193. break;
  1194. case JSONType::NUMBER:
  1195. if (!zConstraints->getName().istGleich("number"))
  1196. {
  1197. return new JSONTypeMissmatch(path,
  1198. dynamic_cast<JSONValue*>(zValue->getThis()),
  1199. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1200. 0);
  1201. }
  1202. else
  1203. {
  1204. if (zConstraints->hasAttribute("equals")
  1205. && (double)zConstraints->getAttributeValue("equals")
  1206. != zValue->asNumber()->getNumber())
  1207. {
  1208. return new JSONTypeMissmatch(path,
  1209. dynamic_cast<JSONValue*>(zValue->getThis()),
  1210. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1211. 0);
  1212. }
  1213. if (zConstraints->hasAttribute("lessOrEqual")
  1214. && zValue->asNumber()->getNumber()
  1215. > (double)zConstraints->getAttributeValue("lessOrEqual"))
  1216. {
  1217. return new JSONTypeMissmatch(path,
  1218. dynamic_cast<JSONValue*>(zValue->getThis()),
  1219. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1220. 0);
  1221. }
  1222. if (zConstraints->hasAttribute("greaterOrEqual")
  1223. && zValue->asNumber()->getNumber()
  1224. < (double)zConstraints->getAttributeValue(
  1225. "greaterOrEqual"))
  1226. {
  1227. return new JSONTypeMissmatch(path,
  1228. dynamic_cast<JSONValue*>(zValue->getThis()),
  1229. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1230. 0);
  1231. }
  1232. if (zConstraints->hasAttribute("less")
  1233. && zValue->asNumber()->getNumber()
  1234. >= (double)zConstraints->getAttributeValue("less"))
  1235. {
  1236. return new JSONTypeMissmatch(path,
  1237. dynamic_cast<JSONValue*>(zValue->getThis()),
  1238. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1239. 0);
  1240. }
  1241. if (zConstraints->hasAttribute("greater")
  1242. && zValue->asNumber()->getNumber()
  1243. <= (double)zConstraints->getAttributeValue("greater"))
  1244. {
  1245. return new JSONTypeMissmatch(path,
  1246. dynamic_cast<JSONValue*>(zValue->getThis()),
  1247. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1248. 0);
  1249. }
  1250. }
  1251. break;
  1252. case JSONType::STRING:
  1253. if (!zConstraints->getName().istGleich("string"))
  1254. {
  1255. return new JSONTypeMissmatch(path,
  1256. dynamic_cast<JSONValue*>(zValue->getThis()),
  1257. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1258. 0);
  1259. }
  1260. else
  1261. {
  1262. if (zConstraints->hasAttribute("equals")
  1263. && !zConstraints->getAttributeValue("equals").istGleich(
  1264. zValue->asString()->getString()))
  1265. {
  1266. return new JSONTypeMissmatch(path,
  1267. dynamic_cast<JSONValue*>(zValue->getThis()),
  1268. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1269. 0);
  1270. }
  1271. if (zConstraints->hasAttribute("contains")
  1272. && zValue->asString()->getString().hat(
  1273. zConstraints->getAttributeValue("contains").getText()))
  1274. {
  1275. return new JSONTypeMissmatch(path,
  1276. dynamic_cast<JSONValue*>(zValue->getThis()),
  1277. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1278. 0);
  1279. }
  1280. if (zConstraints->hasAttribute("startsWith")
  1281. && zValue->asString()->getString().positionVon(
  1282. zConstraints->getAttributeValue("startsWith").getText())
  1283. == 0)
  1284. {
  1285. return new JSONTypeMissmatch(path,
  1286. dynamic_cast<JSONValue*>(zValue->getThis()),
  1287. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1288. 0);
  1289. }
  1290. if (zConstraints->hasAttribute("endsWith")
  1291. && zValue->asString()->getString().positionVon(
  1292. zConstraints->getAttributeValue("endsWith").getText())
  1293. == zValue->asString()->getString().getLength()
  1294. - zConstraints->getAttributeValue("endsWith")
  1295. .getLength())
  1296. {
  1297. return new JSONTypeMissmatch(path,
  1298. dynamic_cast<JSONValue*>(zValue->getThis()),
  1299. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1300. 0);
  1301. }
  1302. if (zConstraints->hasAttribute("oneOf"))
  1303. {
  1304. JSONArray* array
  1305. = Parser::getValue(zConstraints->getAttributeValue("oneOf"))
  1306. ->asArray();
  1307. bool ok = 0;
  1308. for (JSONValue* v : *array)
  1309. {
  1310. if (v->asString()->getString().istGleich(
  1311. zValue->asString()->getString()))
  1312. {
  1313. ok = 1;
  1314. break;
  1315. }
  1316. }
  1317. array->release();
  1318. if (!ok)
  1319. {
  1320. return new JSONTypeMissmatch(path,
  1321. dynamic_cast<JSONValue*>(zValue->getThis()),
  1322. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1323. 0);
  1324. }
  1325. }
  1326. }
  1327. break;
  1328. case JSONType::ARRAY:
  1329. if (!zConstraints->getName().istGleich("array"))
  1330. {
  1331. return new JSONTypeMissmatch(path,
  1332. dynamic_cast<JSONValue*>(zValue->getThis()),
  1333. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1334. 0);
  1335. }
  1336. else
  1337. {
  1338. int index = 0;
  1339. for (JSON::JSONValue* value : *zValue->asArray())
  1340. {
  1341. Text p = path;
  1342. p += "[";
  1343. p += index;
  1344. p += "]";
  1345. JSONValidationResult* res
  1346. = validateMultipleTypes(value, zConstraints, p);
  1347. if (!res->isValid())
  1348. {
  1349. return new JSONTypeMissmatch(path,
  1350. dynamic_cast<JSONValue*>(zValue->getThis()),
  1351. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1352. res);
  1353. }
  1354. res->release();
  1355. index++;
  1356. }
  1357. }
  1358. break;
  1359. case JSONType::OBJECT:
  1360. if (zConstraints->getName().istGleich("objectRef"))
  1361. {
  1362. Text id = zConstraints->getAttributeValue("ref");
  1363. XML::Element* e = typeConstraints.get(id, id.getLength());
  1364. if (e)
  1365. {
  1366. zConstraints = e;
  1367. }
  1368. }
  1369. if (!zConstraints->getName().istGleich("object"))
  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. JSON::JSONObject* obj = zValue->asObject();
  1379. for (auto i = obj->getFields(); i; i++)
  1380. {
  1381. Text p = path;
  1382. p += ".";
  1383. p += i.val();
  1384. if (!zConstraints->selectChildsByAttribute("name", i.val())
  1385. .exists())
  1386. {
  1387. if (!zConstraints
  1388. ->getAttributeValue("allowAdditionalAttributes")
  1389. .istGleich("true"))
  1390. {
  1391. return new JSONTypeMissmatch(path,
  1392. dynamic_cast<JSONValue*>(zValue->getThis()),
  1393. dynamic_cast<XML::Element*>(
  1394. zConstraints->getThis()),
  1395. new JSONUnknownValue(
  1396. p, zValue->asObject()->getValue(i.val())));
  1397. }
  1398. }
  1399. else
  1400. {
  1401. XML::Editor tmp = zConstraints->selectChildsByAttribute(
  1402. "name", i.val());
  1403. JSONValidationResult* res = validateMultipleTypes(
  1404. zValue->asObject()->zValue(i.val()),
  1405. tmp.begin().val(),
  1406. p);
  1407. if (!res->isValid())
  1408. {
  1409. return new JSONTypeMissmatch(path,
  1410. dynamic_cast<JSONValue*>(zValue->getThis()),
  1411. dynamic_cast<XML::Element*>(
  1412. zConstraints->getThis()),
  1413. res);
  1414. }
  1415. res->release();
  1416. }
  1417. }
  1418. for (XML::Element* constraint : zConstraints->selectChildren())
  1419. {
  1420. if (!zValue->asObject()->hasValue(
  1421. constraint->getAttributeValue("name")))
  1422. {
  1423. XML::Editor tmp = constraint->selectChildren();
  1424. bool optional = true;
  1425. std::function<void(XML::Element*)> checkOptional;
  1426. checkOptional = [&optional, &checkOptional](
  1427. XML::Element* zElement) {
  1428. if (zElement->getName().istGleich("oneOf"))
  1429. {
  1430. XML::Editor tmp = zElement->selectChildren();
  1431. tmp.forEach(checkOptional);
  1432. }
  1433. else
  1434. {
  1435. optional &= zElement->hasAttribute("optional")
  1436. && zElement->getAttributeValue("optional")
  1437. .istGleich("true");
  1438. }
  1439. };
  1440. tmp.forEach(checkOptional);
  1441. if (!optional)
  1442. {
  1443. Text p = path;
  1444. p += ".";
  1445. p += constraint->getAttributeValue("name");
  1446. if (constraint->getChildCount() != 1)
  1447. return new JSONTypeMissmatch(path,
  1448. dynamic_cast<JSONValue*>(zValue->getThis()),
  1449. dynamic_cast<XML::Element*>(
  1450. zConstraints->getThis()),
  1451. new JSONMissingOneOf(p, tmp));
  1452. return new JSONTypeMissmatch(path,
  1453. dynamic_cast<JSONValue*>(zValue->getThis()),
  1454. dynamic_cast<XML::Element*>(
  1455. zConstraints->getThis()),
  1456. new JSONMissingValue(p,
  1457. dynamic_cast<XML::Element*>(
  1458. tmp.begin()->getThis())));
  1459. }
  1460. }
  1461. }
  1462. }
  1463. break;
  1464. }
  1465. return new JSONValidValue(
  1466. path, dynamic_cast<JSONValue*>(zValue->getThis()));
  1467. }
  1468. JSONValidationResult* JSONValidator::validateMultipleTypes(
  1469. JSONValue* zChildValue,
  1470. XML::Element* zPossibleChildConstraints,
  1471. Text childPath) const
  1472. {
  1473. if (zPossibleChildConstraints->getChildCount() == 1
  1474. && !zPossibleChildConstraints->hasAttribute("typeSpecifiedBy"))
  1475. {
  1476. XML::Editor children = zPossibleChildConstraints->selectChildren();
  1477. return validate(zChildValue,
  1478. children.begin().val(),
  1479. childPath); // only one type is possible
  1480. }
  1481. bool hasTypeAttr = 0;
  1482. RCArray<XML::Element> possibleConstraints;
  1483. if (zPossibleChildConstraints->hasAttribute("typeSpecifiedBy"))
  1484. { // try to find the correct constraints based on the type attribute
  1485. hasTypeAttr = 1;
  1486. Text typeAttr
  1487. = zPossibleChildConstraints->getAttributeValue("typeSpecifiedBy");
  1488. if (zChildValue->getType() == JSONType::OBJECT)
  1489. {
  1490. if (zChildValue->asObject()->hasValue(typeAttr))
  1491. {
  1492. JSONValue* typeV = zChildValue->asObject()->zValue(typeAttr);
  1493. for (XML::Element* constraint :
  1494. zPossibleChildConstraints->selectChildsByName("object")
  1495. .whereChildWithAttributeExists("name", typeAttr))
  1496. {
  1497. XML::Editor nameChildren
  1498. = constraint->selectChildsByAttribute("name", typeAttr);
  1499. XML::Element* typeAttrContraints
  1500. = nameChildren.begin().val();
  1501. JSONValidationResult* res = validateMultipleTypes(
  1502. typeV, typeAttrContraints, childPath + "." + typeAttr);
  1503. if (res->isValid())
  1504. possibleConstraints.add(
  1505. dynamic_cast<XML::Element*>(constraint->getThis()));
  1506. res->release();
  1507. }
  1508. }
  1509. }
  1510. }
  1511. if (hasTypeAttr && possibleConstraints.getEintragAnzahl() == 1)
  1512. return validate(zChildValue,
  1513. possibleConstraints.begin().val(),
  1514. childPath); // if the type is clear
  1515. else if (hasTypeAttr && possibleConstraints.getEintragAnzahl() > 0)
  1516. { // more then one type is possible
  1517. RCArray<JSONValidationResult> invalidResults;
  1518. for (XML::Element* constraint : possibleConstraints)
  1519. {
  1520. JSONValidationResult* res
  1521. = validate(zChildValue, constraint, childPath);
  1522. invalidResults.add(res);
  1523. if (res->isValid())
  1524. return new JSONValidValue(childPath,
  1525. dynamic_cast<JSONValue*>(zChildValue->getThis()));
  1526. }
  1527. return new JSONNoTypeMatching(childPath,
  1528. dynamic_cast<JSONValue*>(zChildValue->getThis()),
  1529. possibleConstraints,
  1530. invalidResults);
  1531. }
  1532. // try all types
  1533. possibleConstraints.leeren();
  1534. RCArray<JSONValidationResult> invalidResults;
  1535. for (XML::Element* constraint : zPossibleChildConstraints->selectChildren())
  1536. {
  1537. JSONValidationResult* res
  1538. = validate(zChildValue, constraint, childPath);
  1539. invalidResults.add(res);
  1540. if (res->isValid())
  1541. return new JSONValidValue(
  1542. childPath, dynamic_cast<JSONValue*>(zChildValue->getThis()));
  1543. possibleConstraints.add(
  1544. dynamic_cast<XML::Element*>(constraint->getThis()));
  1545. }
  1546. return new JSONNoTypeMatching(childPath,
  1547. dynamic_cast<JSONValue*>(zChildValue->getThis()),
  1548. possibleConstraints,
  1549. invalidResults);
  1550. }
  1551. StringValidationBuilder<JSONValidator>* JSONValidator::buildForString()
  1552. {
  1553. return new StringValidationBuilder<JSONValidator>(
  1554. [](XML::Element& e) { return new JSONValidator(e.dublicate()); });
  1555. }
  1556. NumberValidationBuilder<JSONValidator>* JSONValidator::buildForNumber()
  1557. {
  1558. return new NumberValidationBuilder<JSONValidator>(
  1559. [](XML::Element& e) { return new JSONValidator(e.dublicate()); });
  1560. }
  1561. BoolValidationBuilder<JSONValidator>* JSONValidator::buildForBool()
  1562. {
  1563. return new BoolValidationBuilder<JSONValidator>(
  1564. [](XML::Element& e) { return new JSONValidator(e.dublicate()); });
  1565. }
  1566. ObjectValidationBuilder<JSONValidator>* JSONValidator::buildForObject()
  1567. {
  1568. return new ObjectValidationBuilder<JSONValidator>(
  1569. [](XML::Element& e) { return new JSONValidator(e.dublicate()); });
  1570. }
  1571. ArrayValidationBuilder<JSONValidator>* JSONValidator::buildForArray()
  1572. {
  1573. return new ArrayValidationBuilder<JSONValidator>(
  1574. [](XML::Element& e) { return new JSONValidator(e.dublicate()); });
  1575. }
  1576. JSONValidator* JSONValidator::buildForObjectReference(Text objectId)
  1577. {
  1578. return new JSONValidator(
  1579. new XML::Element(Text("<objectRef ref=\"") + objectId + Text("\"/>")));
  1580. }
  1581. OneOfValidationBuilder<JSONValidator>* JSONValidator::buildForOneOf()
  1582. {
  1583. return new OneOfValidationBuilder<JSONValidator>(
  1584. [](XML::Element& e) { return new JSONValidator(e.dublicate()); });
  1585. }
  1586. #pragma endregion Content