JSON.cpp 49 KB

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