JSON.cpp 48 KB

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