JSON.cpp 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763
  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. for (XML::Element* e : expected)
  960. {
  961. if (e->hasAttribute("default"))
  962. {
  963. JSONValue *default = Parser::getValue(e->getAttributeValue("default"));
  964. if (default)
  965. {
  966. JSONValue *valid = JSONValidator(
  967. dynamic_cast<XML::Element*>(e->getThis()))
  968. .getValidParts(default, removedPartsValidationResults);
  969. default->release();
  970. if (valid)
  971. {
  972. return valid;
  973. }
  974. }
  975. }
  976. }
  977. if (removedPartsValidationResults)
  978. {
  979. removedPartsValidationResults->add(
  980. dynamic_cast<JSONValidationResult*>(getThis()));
  981. }
  982. // multiple possibilities are undecidable
  983. return 0;
  984. }
  985. Text JSONMissingOneOf::getPath() const
  986. {
  987. return path;
  988. }
  989. bool JSONMissingOneOf::isDifferent(const JSONValidationResult* zResult) const
  990. {
  991. const JSONMissingOneOf* casted
  992. = dynamic_cast<const JSONMissingOneOf*>(zResult);
  993. if (casted == 0) return 1;
  994. if (!casted->getPath().istGleich(path)) return 1;
  995. return 0;
  996. }
  997. void Framework::JSON::Validator::JSONMissingOneOf::addBasePath(Text basePath)
  998. {
  999. path = basePath + path;
  1000. }
  1001. #pragma endregion Content
  1002. #pragma region JSONNoTypeMatching
  1003. JSONNoTypeMatching::JSONNoTypeMatching(Text path,
  1004. JSONValue* foundValue,
  1005. RCArray<XML::Element>& expected,
  1006. RCArray<JSONValidationResult>& reasons)
  1007. : JSONValidationResult()
  1008. {
  1009. this->path = path;
  1010. this->foundValue = foundValue;
  1011. this->expected = expected;
  1012. this->reasons = reasons;
  1013. }
  1014. JSONNoTypeMatching::~JSONNoTypeMatching()
  1015. {
  1016. foundValue->release();
  1017. }
  1018. bool JSONNoTypeMatching::isValid() const
  1019. {
  1020. return 0;
  1021. }
  1022. void JSONNoTypeMatching::printInvalidInfo(int indent) const
  1023. {
  1024. Text ind = "";
  1025. ind.fillText(' ', indent);
  1026. std::cout << ind.getText() << "Found Value at '" << path.getText()
  1027. << "' did not match any of the specified types\n";
  1028. Text ind2 = ind + " ";
  1029. std::cout << ind.getText() << "Reasons:\n";
  1030. for (JSONValidationResult* reason : reasons)
  1031. {
  1032. reason->printInvalidInfo(indent + 4);
  1033. }
  1034. }
  1035. JSONValue* JSONNoTypeMatching::getValidPart(
  1036. RCArray<JSONValidationResult>* removedPartsValidationResults)
  1037. {
  1038. RCArray<JSONValidationResult> tempErrors;
  1039. for (JSONValidationResult* reason : reasons)
  1040. {
  1041. JSONValue* result = reason->getValidPart(&tempErrors);
  1042. if (result)
  1043. {
  1044. return result;
  1045. }
  1046. }
  1047. if (removedPartsValidationResults)
  1048. {
  1049. removedPartsValidationResults->add(
  1050. dynamic_cast<JSONValidationResult*>(getThis()));
  1051. reasons.leeren();
  1052. for (JSONValidationResult* error : tempErrors)
  1053. {
  1054. reasons.add(dynamic_cast<JSONValidationResult*>(error->getThis()));
  1055. }
  1056. }
  1057. // multiple possibilities are undecidable
  1058. return 0;
  1059. }
  1060. Text JSONNoTypeMatching::getPath() const
  1061. {
  1062. return path;
  1063. }
  1064. bool JSONNoTypeMatching::isDifferent(const JSONValidationResult* zResult) const
  1065. {
  1066. const JSONNoTypeMatching* casted
  1067. = dynamic_cast<const JSONNoTypeMatching*>(zResult);
  1068. if (casted == 0) return 1;
  1069. if (!casted->getPath().istGleich(path)) return 1;
  1070. for (int i = 0; i < reasons.getEintragAnzahl(); i++)
  1071. {
  1072. if (reasons.z(i)->isDifferent(casted->reasons.z(i))) return 1;
  1073. }
  1074. return 0;
  1075. }
  1076. void Framework::JSON::Validator::JSONNoTypeMatching::addBasePath(Text basePath)
  1077. {
  1078. path = basePath + path;
  1079. for (JSONValidationResult* reason : reasons)
  1080. {
  1081. reason->addBasePath(basePath);
  1082. }
  1083. }
  1084. #pragma endregion Content
  1085. #pragma region JSONValidValue
  1086. JSONValidValue::JSONValidValue(Text path, JSONValue* value)
  1087. : JSONValidationResult()
  1088. {
  1089. this->path = path;
  1090. this->value = value;
  1091. }
  1092. JSONValidValue::~JSONValidValue()
  1093. {
  1094. value->release();
  1095. }
  1096. bool JSONValidValue::isValid() const
  1097. {
  1098. return 1;
  1099. }
  1100. void JSONValidValue::printInvalidInfo(int indent) const {}
  1101. JSONValue* JSONValidValue::getValidPart(
  1102. RCArray<JSONValidationResult>* removedPartsValidationResults)
  1103. {
  1104. return value->clone();
  1105. }
  1106. Text JSONValidValue::getPath() const
  1107. {
  1108. return path;
  1109. }
  1110. bool JSONValidValue::isDifferent(const JSONValidationResult* zResult) const
  1111. {
  1112. const JSONValidValue* casted = dynamic_cast<const JSONValidValue*>(zResult);
  1113. if (casted == 0) return 1;
  1114. if (!casted->getPath().istGleich(path)) return 1;
  1115. return 0;
  1116. }
  1117. void Framework::JSON::Validator::JSONValidValue::addBasePath(Text basePath)
  1118. {
  1119. path = basePath + path;
  1120. }
  1121. #pragma endregion Content
  1122. #pragma endregion Content
  1123. #pragma region JSONValidator
  1124. JSONValidator::JSONValidator(XML::Element* constraints)
  1125. : ReferenceCounter(),
  1126. constraints(constraints)
  1127. {
  1128. for (XML::Element* e : constraints->select()
  1129. .selectAllElements()
  1130. .whereNameEquals("object")
  1131. .whereAttributeExists("id"))
  1132. {
  1133. Framework::Text id = e->getAttributeValue("id");
  1134. typeConstraints.set(id, id.getLength(), e);
  1135. }
  1136. }
  1137. JSONValidator::~JSONValidator()
  1138. {
  1139. constraints->release();
  1140. }
  1141. JSONValidationResult* JSONValidator::validate(JSONValue* zValue) const
  1142. {
  1143. return validate(zValue, constraints, "");
  1144. }
  1145. bool JSONValidator::isValid(JSONValue* zValue) const
  1146. {
  1147. JSONValidationResult* res = validate(zValue);
  1148. if (res->isValid())
  1149. {
  1150. res->release();
  1151. return 1;
  1152. }
  1153. res->release();
  1154. return 0;
  1155. }
  1156. JSONValue* JSONValidator::getValidParts(JSONValue* zValue,
  1157. RCArray<JSONValidationResult>* removedPartsValidationResults) const
  1158. {
  1159. JSONValidationResult* res = validate(zValue);
  1160. if (res->isValid())
  1161. {
  1162. res->release();
  1163. return zValue->clone();
  1164. }
  1165. JSONValue* valid = res->getValidPart(removedPartsValidationResults);
  1166. res->release();
  1167. return valid;
  1168. }
  1169. XML::Element* JSONValidator::zConstraints()
  1170. {
  1171. return constraints;
  1172. }
  1173. JSONValidationResult* JSONValidator::validate(
  1174. JSONValue* zValue, XML::Element* zConstraints, Text path) const
  1175. {
  1176. if (zConstraints->getName().istGleich("oneOf"))
  1177. {
  1178. return validateMultipleTypes(zValue, zConstraints, path);
  1179. }
  1180. switch (zValue->getType())
  1181. {
  1182. case JSONType::NULL_:
  1183. if (!zConstraints->hasAttribute("nullable")
  1184. || !zConstraints->getAttributeValue("nullable").istGleich("true"))
  1185. {
  1186. return new JSONTypeMissmatch(path,
  1187. dynamic_cast<JSONValue*>(zValue->getThis()),
  1188. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1189. 0);
  1190. }
  1191. break;
  1192. case JSONType::BOOLEAN:
  1193. if (!zConstraints->getName().istGleich("bool"))
  1194. {
  1195. return new JSONTypeMissmatch(path,
  1196. dynamic_cast<JSONValue*>(zValue->getThis()),
  1197. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1198. 0);
  1199. }
  1200. else if (zConstraints->hasAttribute("equals"))
  1201. {
  1202. if (!zConstraints->getAttributeValue("equals").istGleich("true")
  1203. == !zValue->asBool()->getBool())
  1204. {
  1205. return new JSONTypeMissmatch(path,
  1206. dynamic_cast<JSONValue*>(zValue->getThis()),
  1207. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1208. 0);
  1209. }
  1210. }
  1211. break;
  1212. case JSONType::NUMBER:
  1213. if (!zConstraints->getName().istGleich("number"))
  1214. {
  1215. return new JSONTypeMissmatch(path,
  1216. dynamic_cast<JSONValue*>(zValue->getThis()),
  1217. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1218. 0);
  1219. }
  1220. else
  1221. {
  1222. if (zConstraints->hasAttribute("equals")
  1223. && (double)zConstraints->getAttributeValue("equals")
  1224. != zValue->asNumber()->getNumber())
  1225. {
  1226. return new JSONTypeMissmatch(path,
  1227. dynamic_cast<JSONValue*>(zValue->getThis()),
  1228. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1229. 0);
  1230. }
  1231. if (zConstraints->hasAttribute("lessOrEqual")
  1232. && zValue->asNumber()->getNumber()
  1233. > (double)zConstraints->getAttributeValue("lessOrEqual"))
  1234. {
  1235. return new JSONTypeMissmatch(path,
  1236. dynamic_cast<JSONValue*>(zValue->getThis()),
  1237. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1238. 0);
  1239. }
  1240. if (zConstraints->hasAttribute("greaterOrEqual")
  1241. && zValue->asNumber()->getNumber()
  1242. < (double)zConstraints->getAttributeValue(
  1243. "greaterOrEqual"))
  1244. {
  1245. return new JSONTypeMissmatch(path,
  1246. dynamic_cast<JSONValue*>(zValue->getThis()),
  1247. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1248. 0);
  1249. }
  1250. if (zConstraints->hasAttribute("less")
  1251. && zValue->asNumber()->getNumber()
  1252. >= (double)zConstraints->getAttributeValue("less"))
  1253. {
  1254. return new JSONTypeMissmatch(path,
  1255. dynamic_cast<JSONValue*>(zValue->getThis()),
  1256. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1257. 0);
  1258. }
  1259. if (zConstraints->hasAttribute("greater")
  1260. && zValue->asNumber()->getNumber()
  1261. <= (double)zConstraints->getAttributeValue("greater"))
  1262. {
  1263. return new JSONTypeMissmatch(path,
  1264. dynamic_cast<JSONValue*>(zValue->getThis()),
  1265. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1266. 0);
  1267. }
  1268. }
  1269. break;
  1270. case JSONType::STRING:
  1271. if (!zConstraints->getName().istGleich("string"))
  1272. {
  1273. return new JSONTypeMissmatch(path,
  1274. dynamic_cast<JSONValue*>(zValue->getThis()),
  1275. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1276. 0);
  1277. }
  1278. else
  1279. {
  1280. if (zConstraints->hasAttribute("equals")
  1281. && !zConstraints->getAttributeValue("equals").istGleich(
  1282. zValue->asString()->getString()))
  1283. {
  1284. return new JSONTypeMissmatch(path,
  1285. dynamic_cast<JSONValue*>(zValue->getThis()),
  1286. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1287. 0);
  1288. }
  1289. if (zConstraints->hasAttribute("contains")
  1290. && zValue->asString()->getString().hat(
  1291. zConstraints->getAttributeValue("contains").getText()))
  1292. {
  1293. return new JSONTypeMissmatch(path,
  1294. dynamic_cast<JSONValue*>(zValue->getThis()),
  1295. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1296. 0);
  1297. }
  1298. if (zConstraints->hasAttribute("startsWith")
  1299. && zValue->asString()->getString().positionVon(
  1300. zConstraints->getAttributeValue("startsWith").getText())
  1301. == 0)
  1302. {
  1303. return new JSONTypeMissmatch(path,
  1304. dynamic_cast<JSONValue*>(zValue->getThis()),
  1305. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1306. 0);
  1307. }
  1308. if (zConstraints->hasAttribute("endsWith")
  1309. && zValue->asString()->getString().positionVon(
  1310. zConstraints->getAttributeValue("endsWith").getText())
  1311. == zValue->asString()->getString().getLength()
  1312. - zConstraints->getAttributeValue("endsWith")
  1313. .getLength())
  1314. {
  1315. return new JSONTypeMissmatch(path,
  1316. dynamic_cast<JSONValue*>(zValue->getThis()),
  1317. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1318. 0);
  1319. }
  1320. if (zConstraints->hasAttribute("oneOf"))
  1321. {
  1322. JSONArray* array
  1323. = Parser::getValue(zConstraints->getAttributeValue("oneOf"))
  1324. ->asArray();
  1325. bool ok = 0;
  1326. for (JSONValue* v : *array)
  1327. {
  1328. if (v->asString()->getString().istGleich(
  1329. zValue->asString()->getString()))
  1330. {
  1331. ok = 1;
  1332. break;
  1333. }
  1334. }
  1335. array->release();
  1336. if (!ok)
  1337. {
  1338. return new JSONTypeMissmatch(path,
  1339. dynamic_cast<JSONValue*>(zValue->getThis()),
  1340. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1341. 0);
  1342. }
  1343. }
  1344. }
  1345. break;
  1346. case JSONType::ARRAY:
  1347. if (!zConstraints->getName().istGleich("array"))
  1348. {
  1349. return new JSONTypeMissmatch(path,
  1350. dynamic_cast<JSONValue*>(zValue->getThis()),
  1351. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1352. 0);
  1353. }
  1354. else
  1355. {
  1356. int index = 0;
  1357. for (JSON::JSONValue* value : *zValue->asArray())
  1358. {
  1359. Text p = path;
  1360. p += "[";
  1361. p += index;
  1362. p += "]";
  1363. JSONValidationResult* res
  1364. = validateMultipleTypes(value, zConstraints, p);
  1365. if (!res->isValid())
  1366. {
  1367. return new JSONTypeMissmatch(path,
  1368. dynamic_cast<JSONValue*>(zValue->getThis()),
  1369. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1370. res);
  1371. }
  1372. res->release();
  1373. index++;
  1374. }
  1375. }
  1376. break;
  1377. case JSONType::OBJECT:
  1378. if (zConstraints->getName().istGleich("objectRef"))
  1379. {
  1380. Text id = zConstraints->getAttributeValue("ref");
  1381. XML::Element* e = typeConstraints.get(id, id.getLength());
  1382. if (e)
  1383. {
  1384. zConstraints = e;
  1385. }
  1386. }
  1387. if (!zConstraints->getName().istGleich("object"))
  1388. {
  1389. return new JSONTypeMissmatch(path,
  1390. dynamic_cast<JSONValue*>(zValue->getThis()),
  1391. dynamic_cast<XML::Element*>(zConstraints->getThis()),
  1392. 0);
  1393. }
  1394. else
  1395. {
  1396. JSON::JSONObject* obj = zValue->asObject();
  1397. for (auto i = obj->getFields(); i; i++)
  1398. {
  1399. Text p = path;
  1400. p += ".";
  1401. p += i.val();
  1402. if (!zConstraints->selectChildsByAttribute("name", i.val())
  1403. .exists())
  1404. {
  1405. if (!zConstraints
  1406. ->getAttributeValue("allowAdditionalAttributes")
  1407. .istGleich("true"))
  1408. {
  1409. return new JSONTypeMissmatch(path,
  1410. dynamic_cast<JSONValue*>(zValue->getThis()),
  1411. dynamic_cast<XML::Element*>(
  1412. zConstraints->getThis()),
  1413. new JSONUnknownValue(
  1414. p, zValue->asObject()->getValue(i.val())));
  1415. }
  1416. }
  1417. else
  1418. {
  1419. XML::Editor tmp = zConstraints->selectChildsByAttribute(
  1420. "name", i.val());
  1421. JSONValidationResult* res = validateMultipleTypes(
  1422. zValue->asObject()->zValue(i.val()),
  1423. tmp.begin().val(),
  1424. p);
  1425. if (!res->isValid())
  1426. {
  1427. return new JSONTypeMissmatch(path,
  1428. dynamic_cast<JSONValue*>(zValue->getThis()),
  1429. dynamic_cast<XML::Element*>(
  1430. zConstraints->getThis()),
  1431. res);
  1432. }
  1433. res->release();
  1434. }
  1435. }
  1436. for (XML::Element* constraint : zConstraints->selectChildren())
  1437. {
  1438. if (!zValue->asObject()->hasValue(
  1439. constraint->getAttributeValue("name")))
  1440. {
  1441. XML::Editor tmp = constraint->selectChildren();
  1442. bool optional = true;
  1443. std::function<void(XML::Element*)> checkOptional;
  1444. checkOptional = [&optional, &checkOptional](
  1445. XML::Element* zElement) {
  1446. if (zElement->getName().istGleich("oneOf"))
  1447. {
  1448. XML::Editor tmp = zElement->selectChildren();
  1449. tmp.forEach(checkOptional);
  1450. }
  1451. else
  1452. {
  1453. optional &= zElement->hasAttribute("optional")
  1454. && zElement->getAttributeValue("optional")
  1455. .istGleich("true");
  1456. }
  1457. };
  1458. tmp.forEach(checkOptional);
  1459. if (!optional)
  1460. {
  1461. Text p = path;
  1462. p += ".";
  1463. p += constraint->getAttributeValue("name");
  1464. if (constraint->getChildCount() != 1)
  1465. return new JSONTypeMissmatch(path,
  1466. dynamic_cast<JSONValue*>(zValue->getThis()),
  1467. dynamic_cast<XML::Element*>(
  1468. zConstraints->getThis()),
  1469. new JSONMissingOneOf(p, tmp));
  1470. return new JSONTypeMissmatch(path,
  1471. dynamic_cast<JSONValue*>(zValue->getThis()),
  1472. dynamic_cast<XML::Element*>(
  1473. zConstraints->getThis()),
  1474. new JSONMissingValue(p,
  1475. dynamic_cast<XML::Element*>(
  1476. tmp.begin()->getThis())));
  1477. }
  1478. }
  1479. }
  1480. }
  1481. break;
  1482. }
  1483. return new JSONValidValue(
  1484. path, dynamic_cast<JSONValue*>(zValue->getThis()));
  1485. }
  1486. JSONValidationResult* JSONValidator::validateMultipleTypes(
  1487. JSONValue* zChildValue,
  1488. XML::Element* zPossibleChildConstraints,
  1489. Text childPath) const
  1490. {
  1491. if (zPossibleChildConstraints->getChildCount() == 1
  1492. && !zPossibleChildConstraints->hasAttribute("typeSpecifiedBy"))
  1493. {
  1494. XML::Editor children = zPossibleChildConstraints->selectChildren();
  1495. return validate(zChildValue,
  1496. children.begin().val(),
  1497. childPath); // only one type is possible
  1498. }
  1499. bool hasTypeAttr = 0;
  1500. RCArray<XML::Element> possibleConstraints;
  1501. if (zPossibleChildConstraints->hasAttribute("typeSpecifiedBy"))
  1502. { // try to find the correct constraints based on the type attribute
  1503. hasTypeAttr = 1;
  1504. Text typeAttr
  1505. = zPossibleChildConstraints->getAttributeValue("typeSpecifiedBy");
  1506. if (zChildValue->getType() == JSONType::OBJECT)
  1507. {
  1508. if (zChildValue->asObject()->hasValue(typeAttr))
  1509. {
  1510. JSONValue* typeV = zChildValue->asObject()->zValue(typeAttr);
  1511. for (XML::Element* constraint :
  1512. zPossibleChildConstraints->selectChildsByName("object")
  1513. .whereChildWithAttributeExists("name", typeAttr))
  1514. {
  1515. XML::Editor nameChildren
  1516. = constraint->selectChildsByAttribute("name", typeAttr);
  1517. XML::Element* typeAttrContraints
  1518. = nameChildren.begin().val();
  1519. JSONValidationResult* res = validateMultipleTypes(
  1520. typeV, typeAttrContraints, childPath + "." + typeAttr);
  1521. if (res->isValid())
  1522. possibleConstraints.add(
  1523. dynamic_cast<XML::Element*>(constraint->getThis()));
  1524. res->release();
  1525. }
  1526. }
  1527. }
  1528. }
  1529. if (hasTypeAttr && possibleConstraints.getEintragAnzahl() == 1)
  1530. return validate(zChildValue,
  1531. possibleConstraints.begin().val(),
  1532. childPath); // if the type is clear
  1533. else if (hasTypeAttr && possibleConstraints.getEintragAnzahl() > 0)
  1534. { // more then one type is possible
  1535. RCArray<JSONValidationResult> invalidResults;
  1536. for (XML::Element* constraint : possibleConstraints)
  1537. {
  1538. JSONValidationResult* res
  1539. = validate(zChildValue, constraint, childPath);
  1540. invalidResults.add(res);
  1541. if (res->isValid())
  1542. return new JSONValidValue(childPath,
  1543. dynamic_cast<JSONValue*>(zChildValue->getThis()));
  1544. }
  1545. return new JSONNoTypeMatching(childPath,
  1546. dynamic_cast<JSONValue*>(zChildValue->getThis()),
  1547. possibleConstraints,
  1548. invalidResults);
  1549. }
  1550. // try all types
  1551. possibleConstraints.leeren();
  1552. RCArray<JSONValidationResult> invalidResults;
  1553. for (XML::Element* constraint : zPossibleChildConstraints->selectChildren())
  1554. {
  1555. JSONValidationResult* res
  1556. = validate(zChildValue, constraint, childPath);
  1557. invalidResults.add(res);
  1558. if (res->isValid())
  1559. return new JSONValidValue(
  1560. childPath, dynamic_cast<JSONValue*>(zChildValue->getThis()));
  1561. possibleConstraints.add(
  1562. dynamic_cast<XML::Element*>(constraint->getThis()));
  1563. }
  1564. return new JSONNoTypeMatching(childPath,
  1565. dynamic_cast<JSONValue*>(zChildValue->getThis()),
  1566. possibleConstraints,
  1567. invalidResults);
  1568. }
  1569. StringValidationBuilder<JSONValidator>* JSONValidator::buildForString()
  1570. {
  1571. return new StringValidationBuilder<JSONValidator>(
  1572. [](XML::Element& e) { return new JSONValidator(e.dublicate()); });
  1573. }
  1574. NumberValidationBuilder<JSONValidator>* JSONValidator::buildForNumber()
  1575. {
  1576. return new NumberValidationBuilder<JSONValidator>(
  1577. [](XML::Element& e) { return new JSONValidator(e.dublicate()); });
  1578. }
  1579. BoolValidationBuilder<JSONValidator>* JSONValidator::buildForBool()
  1580. {
  1581. return new BoolValidationBuilder<JSONValidator>(
  1582. [](XML::Element& e) { return new JSONValidator(e.dublicate()); });
  1583. }
  1584. ObjectValidationBuilder<JSONValidator>* JSONValidator::buildForObject()
  1585. {
  1586. return new ObjectValidationBuilder<JSONValidator>(
  1587. [](XML::Element& e) { return new JSONValidator(e.dublicate()); });
  1588. }
  1589. ArrayValidationBuilder<JSONValidator>* JSONValidator::buildForArray()
  1590. {
  1591. return new ArrayValidationBuilder<JSONValidator>(
  1592. [](XML::Element& e) { return new JSONValidator(e.dublicate()); });
  1593. }
  1594. JSONValidator* JSONValidator::buildForObjectReference(Text objectId)
  1595. {
  1596. return new JSONValidator(
  1597. new XML::Element(Text("<objectRef ref=\"") + objectId + Text("\"/>")));
  1598. }
  1599. OneOfValidationBuilder<JSONValidator>* JSONValidator::buildForOneOf()
  1600. {
  1601. return new OneOfValidationBuilder<JSONValidator>(
  1602. [](XML::Element& e) { return new JSONValidator(e.dublicate()); });
  1603. }
  1604. #pragma endregion Content