JSON.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. #pragma once
  2. #include "Text.h"
  3. #include "Array.h"
  4. #include "ReferenceCounter.h"
  5. #include "XML.h"
  6. #include "Writer.h"
  7. #include <functional>
  8. namespace Framework
  9. {
  10. namespace JSON
  11. {
  12. enum class JSONType
  13. {
  14. NULL_,
  15. BOOLEAN,
  16. NUMBER,
  17. STRING,
  18. ARRAY,
  19. OBJECT
  20. };
  21. class JSONArray;
  22. class JSONObject;
  23. class JSONBool;
  24. class JSONNumber;
  25. class JSONString;
  26. class JSONValue : public virtual ReferenceCounter
  27. {
  28. private:
  29. JSONType type;
  30. protected:
  31. __declspec(dllexport) JSONValue(JSONType type);
  32. public:
  33. __declspec(dllexport) JSONValue();
  34. __declspec(dllexport) virtual ~JSONValue();
  35. __declspec(dllexport) JSONType getType() const;
  36. __declspec(dllexport) virtual Text toString() const;
  37. __declspec(dllexport) virtual JSONValue* clone() const;
  38. __declspec(dllexport) JSONBool* asBool() const;
  39. __declspec(dllexport) JSONNumber* asNumber() const;
  40. __declspec(dllexport) JSONString* asString() const;
  41. __declspec(dllexport) JSONArray* asArray() const;
  42. __declspec(dllexport) JSONObject* asObject() const;
  43. };
  44. class JSONBool : public JSONValue
  45. {
  46. private:
  47. bool b;
  48. public:
  49. __declspec(dllexport) JSONBool(bool b);
  50. __declspec(dllexport) bool getBool() const;
  51. __declspec(dllexport) Text toString() const override;
  52. __declspec(dllexport) JSONValue* clone() const override;
  53. };
  54. class JSONNumber : public JSONValue
  55. {
  56. private:
  57. double number;
  58. public:
  59. __declspec(dllexport) JSONNumber(double num);
  60. __declspec(dllexport) double getNumber() const;
  61. __declspec(dllexport) Text toString() const override;
  62. __declspec(dllexport) JSONValue* clone() const override;
  63. };
  64. class JSONString : public JSONValue
  65. {
  66. private:
  67. Text string;
  68. public:
  69. __declspec(dllexport) JSONString(Text string);
  70. __declspec(dllexport) Text getString() const;
  71. __declspec(dllexport) Text toString() const override;
  72. __declspec(dllexport) JSONValue* clone() const override;
  73. };
  74. class JSONArray : public JSONValue
  75. {
  76. private:
  77. RCArray< JSONValue >* array;
  78. public:
  79. __declspec(dllexport) JSONArray();
  80. __declspec(dllexport) JSONArray(Text string);
  81. __declspec(dllexport) JSONArray(const JSONArray& arr);
  82. __declspec(dllexport) ~JSONArray();
  83. __declspec(dllexport) JSONArray& operator=(const JSONArray& arr);
  84. __declspec(dllexport) void addValue(JSONValue* value);
  85. __declspec(dllexport) void setValue(int i, JSONValue* value);
  86. __declspec(dllexport) void removeValue(int i);
  87. __declspec(dllexport) JSONValue* getValue(int i) const;
  88. __declspec(dllexport) JSONValue* zValue(int i) const;
  89. __declspec(dllexport) int getLength() const;
  90. __declspec(dllexport) bool isValueOfType(int i, JSONType type) const;
  91. //! Gibt einen Iterator zurück.
  92. //! Mit ++ kann durch die Liste iteriert werden
  93. __declspec(dllexport) Iterator< JSONValue* > begin() const;
  94. __declspec(dllexport) Iterator< JSONValue* > end() const;
  95. template<class T>
  96. RCArray<T>* toRCArray(std::function<T* (JSONValue&)> map) const
  97. {
  98. return toRCArray([](JSONValue& v) { return 1; }, map);
  99. }
  100. template<class T>
  101. RCArray<T>* toRCArray(std::function<bool(JSONValue&)> filter, std::function<T* (JSONValue&)> map) const
  102. {
  103. RCArray<T>* result = new RCArray<T>();
  104. for (auto v : *array)
  105. {
  106. if (filter(*v))
  107. {
  108. result->add(map(*v));
  109. }
  110. }
  111. return result;
  112. }
  113. template<class T>
  114. Array<T>* toArray(std::function<T(JSONValue&)> map) const
  115. {
  116. return toArray([](JSONValue& v) { return 1; }, map);;
  117. }
  118. template<class T>
  119. Array<T>* toArray(std::function<bool(JSONValue&)> filter, std::function<T(JSONValue&)> map) const
  120. {
  121. Array<T>* result = new Array<T>();
  122. for (auto v : *array)
  123. {
  124. if (filter(*v))
  125. {
  126. result->add(map(*v));
  127. }
  128. }
  129. return result;
  130. }
  131. __declspec(dllexport) Text toString() const override;
  132. __declspec(dllexport) JSONValue* clone() const override;
  133. template<class T>
  134. static JSONArray* fromRCArray(Framework::RCArray<T>& arr, std::function<JSONValue* (T&)> map)
  135. {
  136. JSONArray* array = new JSONArray();
  137. for (T* v : arr)
  138. {
  139. array->addValue(map(*v));
  140. }
  141. return array;
  142. }
  143. template<class T>
  144. static JSONArray* fromArray(Framework::Array<T>& arr, std::function<JSONValue* (T)> map)
  145. {
  146. JSONArray* array = new JSONArray();
  147. for (T v : arr)
  148. {
  149. array->addValue(map(v));
  150. }
  151. return array;
  152. }
  153. };
  154. class JSONObject : public JSONValue
  155. {
  156. private:
  157. Array< Text >* fields;
  158. RCArray< JSONValue >* values;
  159. public:
  160. __declspec(dllexport) JSONObject();
  161. __declspec(dllexport) JSONObject(Text string);
  162. __declspec(dllexport) JSONObject(const JSONObject& obj);
  163. __declspec(dllexport) ~JSONObject();
  164. __declspec(dllexport) JSONObject& operator=(const JSONObject& obj);
  165. __declspec(dllexport) bool addValue(Text field, JSONValue* value);
  166. __declspec(dllexport) bool removeValue(Text field);
  167. __declspec(dllexport) bool hasValue(Text field);
  168. __declspec(dllexport) JSONValue* getValue(Text field);
  169. __declspec(dllexport) JSONValue* zValue(Text field);
  170. __declspec(dllexport) Iterator< Text > getFields();
  171. __declspec(dllexport) Iterator< JSONValue* > getValues();
  172. __declspec(dllexport) int getFieldCount() const;
  173. __declspec(dllexport) bool isValueOfType(Text field, JSONType type) const;
  174. template<class T>
  175. T* parseTo(T* initialState, std::function<void(T* obj, Text fieldName, JSONValue& fieldValue)> parser) const
  176. {
  177. auto fieldsI = fields->begin();
  178. auto valuesI = values->begin();
  179. while (fieldsI && valuesI)
  180. {
  181. parser(initialState, fieldsI, *(JSONValue*)valuesI);
  182. fieldsI++;
  183. valuesI++;
  184. }
  185. return initialState;
  186. }
  187. __declspec(dllexport) Text toString() const override;
  188. __declspec(dllexport) JSONValue* clone() const override;
  189. };
  190. __declspec(dllexport) JSONValue* loadJSONFromFile(Text path);
  191. namespace Parser
  192. {
  193. __declspec(dllexport) int findObjectEndInArray(const char* str);
  194. __declspec(dllexport) Text removeWhitespace(const char* str);
  195. __declspec(dllexport) JSONValue* getValue(const char* str);
  196. __declspec(dllexport) int findFieldEndInObject(const char* str);
  197. __declspec(dllexport) int findValueEndInObject(const char* str);
  198. };
  199. namespace Validator
  200. {
  201. template<typename T>
  202. class StringValidationBuilder
  203. {
  204. private:
  205. XML::Element element;
  206. std::function<T* (XML::Element& element)> builder;
  207. public:
  208. StringValidationBuilder(std::function<T* (XML::Element& element)> builder)
  209. : element("<string/>"),
  210. builder(builder)
  211. {}
  212. StringValidationBuilder<T>* withExactMatch(Text value)
  213. {
  214. element.setAttribute("equals", value);
  215. return this;
  216. }
  217. StringValidationBuilder<T>* whichContainsMatch(Text value)
  218. {
  219. element.setAttribute("contains", value);
  220. return this;
  221. }
  222. StringValidationBuilder<T>* whichStartsWithMatch(Text value)
  223. {
  224. element.setAttribute("startsWith", value);
  225. return this;
  226. }
  227. StringValidationBuilder<T>* whichEndsWithMatch(Text value)
  228. {
  229. element.setAttribute("endsWith", value);
  230. return this;
  231. }
  232. StringValidationBuilder<T>* withDefault(Text value)
  233. {
  234. element.setAttribute("default", JSONString(value).toString());
  235. return this;
  236. }
  237. StringValidationBuilder<T>* withDefaultNull()
  238. {
  239. element.setAttribute("default", "null");
  240. return this;
  241. }
  242. StringValidationBuilder<T>* whichCanBeNull()
  243. {
  244. element.setAttribute("nullable", "true");
  245. return this;
  246. }
  247. StringValidationBuilder<T>* whichIsOptional()
  248. {
  249. element.setAttribute("optional", "true");
  250. return this;
  251. }
  252. T* finishString()
  253. {
  254. T* result = builder(element);
  255. delete this;
  256. return result;
  257. }
  258. };
  259. template<typename T>
  260. class NumberValidationBuilder
  261. {
  262. private:
  263. XML::Element element;
  264. std::function<T* (XML::Element& element)> builder;
  265. public:
  266. NumberValidationBuilder(std::function<T* (XML::Element& element)> builder)
  267. : element("<number/>"),
  268. builder(builder)
  269. {}
  270. NumberValidationBuilder<T>* whichIs(double value)
  271. {
  272. element.setAttribute("equals", Text(value));
  273. return this;
  274. }
  275. NumberValidationBuilder<T>* whichIsLessOrEqual(double value)
  276. {
  277. element.setAttribute("lessOrEqual", Text(value));
  278. return this;
  279. }
  280. NumberValidationBuilder<T>* whichIsGreaterOrEqual(double value)
  281. {
  282. element.setAttribute("greaterOrEqual", Text(value));
  283. return this;
  284. }
  285. NumberValidationBuilder<T>* whichIsLessThen(double value)
  286. {
  287. element.setAttribute("less", Text(value));
  288. return this;
  289. }
  290. NumberValidationBuilder<T>* whichIsGreaterThen(double value)
  291. {
  292. element.setAttribute("greater", Text(value));
  293. return this;
  294. }
  295. NumberValidationBuilder<T>* withDefault(double value)
  296. {
  297. element.setAttribute("default", JSONNumber(value).toString());
  298. return this;
  299. }
  300. NumberValidationBuilder<T>* withDefaultNull()
  301. {
  302. element.setAttribute("default", "null");
  303. return this;
  304. }
  305. NumberValidationBuilder<T>* whichCanBeNull()
  306. {
  307. element.setAttribute("nullable", "true");
  308. return this;
  309. }
  310. NumberValidationBuilder<T>* whichIsOptional()
  311. {
  312. element.setAttribute("optional", "true");
  313. return this;
  314. }
  315. T* finishNumber()
  316. {
  317. T* result = builder(element);
  318. delete this;
  319. return result;
  320. }
  321. };
  322. template<typename T>
  323. class BoolValidationBuilder
  324. {
  325. private:
  326. XML::Element element;
  327. std::function<T* (XML::Element& element)> builder;
  328. public:
  329. BoolValidationBuilder(std::function<T* (XML::Element& element)> builder)
  330. : element("<bool/>"),
  331. builder(builder)
  332. {}
  333. BoolValidationBuilder<T>* whichIs(bool value)
  334. {
  335. element.setAttribute("equals", value ? "true" : "false");
  336. return this;
  337. }
  338. BoolValidationBuilder<T>* withDefault(bool value)
  339. {
  340. element.setAttribute("default", JSONBool(value).toString());
  341. return this;
  342. }
  343. BoolValidationBuilder<T>* withDefaultNull()
  344. {
  345. element.setAttribute("default", "null");
  346. return this;
  347. }
  348. BoolValidationBuilder<T>* whichCanBeNull()
  349. {
  350. element.setAttribute("nullable", "true");
  351. return this;
  352. }
  353. BoolValidationBuilder<T>* whichIsOptional()
  354. {
  355. element.setAttribute("optional", "true");
  356. return this;
  357. }
  358. T* finishBool()
  359. {
  360. T* result = builder(element);
  361. delete this;
  362. return result;
  363. }
  364. };
  365. template<typename T>
  366. class ArrayValidationBuilder;
  367. template<typename T>
  368. class ObjectValidationBuilder
  369. {
  370. private:
  371. XML::Element element;
  372. std::function<T* (XML::Element& element)> builder;
  373. public:
  374. ObjectValidationBuilder(std::function<T* (XML::Element& element)> builder)
  375. : element("<object></object>"),
  376. builder(builder)
  377. {}
  378. NumberValidationBuilder<ObjectValidationBuilder<T>>* withRequiredNumber(Text name)
  379. {
  380. return new NumberValidationBuilder<ObjectValidationBuilder<T>>([this, name](XML::Element& e)
  381. {
  382. if (!element.selectChildsByAttribute("name", name).exists())
  383. {
  384. XML::Element* attr = new XML::Element("<value></value>");
  385. attr->setAttribute("name", name);
  386. element.addChild(attr);
  387. }
  388. element.selectChildsByAttribute("name", name).addChild(e.dublicate());
  389. return this;
  390. });
  391. }
  392. StringValidationBuilder<ObjectValidationBuilder<T>>* withRequiredString(Text name)
  393. {
  394. return new StringValidationBuilder<ObjectValidationBuilder<T>>([this, name](XML::Element& e)
  395. {
  396. if (!element.selectChildsByAttribute("name", name).exists())
  397. {
  398. XML::Element* attr = new XML::Element("<value></value>");
  399. attr->setAttribute("name", name);
  400. element.addChild(attr);
  401. }
  402. element.selectChildsByAttribute("name", name).addChild(e.dublicate());
  403. return this;
  404. });
  405. }
  406. BoolValidationBuilder<ObjectValidationBuilder<T>>* withRequiredBool(Text name)
  407. {
  408. return new BoolValidationBuilder<ObjectValidationBuilder<T>>([this, name](XML::Element& e)
  409. {
  410. if (!element.selectChildsByAttribute("name", name).exists())
  411. {
  412. XML::Element* attr = new XML::Element("<value></value>");
  413. attr->setAttribute("name", name);
  414. element.addChild(attr);
  415. }
  416. element.selectChildsByAttribute("name", name).addChild(e.dublicate());
  417. return this;
  418. });
  419. }
  420. ArrayValidationBuilder<ObjectValidationBuilder<T>>* withRequiredArray(Text name)
  421. {
  422. return new ArrayValidationBuilder<ObjectValidationBuilder<T>>([this, name](XML::Element& e)
  423. {
  424. if (!element.selectChildsByAttribute("name", name).exists())
  425. {
  426. XML::Element* attr = new XML::Element("<value></value>");
  427. attr->setAttribute("name", name);
  428. element.addChild(attr);
  429. }
  430. element.selectChildsByAttribute("name", name).addChild(e.dublicate());
  431. return this;
  432. });
  433. }
  434. ObjectValidationBuilder<ObjectValidationBuilder<T>>* withRequiredObject(Text name)
  435. {
  436. return new ObjectValidationBuilder<ObjectValidationBuilder<T>>([this, name](XML::Element& e)
  437. {
  438. if (!element.selectChildsByAttribute("name", name).exists())
  439. {
  440. XML::Element* attr = new XML::Element("<value></value>");
  441. attr->setAttribute("name", name);
  442. element.addChild(attr);
  443. }
  444. element.selectChildsByAttribute("name", name).addChild(e.dublicate());
  445. return this;
  446. });
  447. }
  448. ObjectValidationBuilder<T>* withDefault(JSONObject* zObj)
  449. {
  450. element.setAttribute("default", zObj->toString());
  451. return this;
  452. }
  453. ObjectValidationBuilder<T>* withDefaultNull()
  454. {
  455. element.setAttribute("default", "null");
  456. return this;
  457. }
  458. ObjectValidationBuilder<T>* whichCanBeNull()
  459. {
  460. element.setAttribute("nullable", "true");
  461. return this;
  462. }
  463. ObjectValidationBuilder<T>* whichIsOptional()
  464. {
  465. element.setAttribute("optional", "true");
  466. return this;
  467. }
  468. ArrayValidationBuilder<T>* typeOfValuesSpecifiedByAttribute(Text valueName, Text attributeName)
  469. {
  470. if (!element.selectChildsByAttribute("name", valueName).exists())
  471. {
  472. XML::Element* attr = new XML::Element("<value></value>");
  473. attr->setAttribute("name", valueName);
  474. element.addChild(attr);
  475. }
  476. element.selectChildsByAttribute("name", valueName).setAttribute("typeSpecifiedBy", attributeName);
  477. return this;
  478. }
  479. T* finishObject()
  480. {
  481. T* result = builder(element);
  482. delete this;
  483. return result;
  484. }
  485. };
  486. template<typename T>
  487. class ArrayValidationBuilder
  488. {
  489. private:
  490. XML::Element element;
  491. std::function<T* (XML::Element& element)> builder;
  492. public:
  493. ArrayValidationBuilder(std::function<T* (XML::Element& element)> builder)
  494. : element("<array></array>"),
  495. builder(builder)
  496. {}
  497. StringValidationBuilder<ArrayValidationBuilder<T>>* addAcceptedStringInArray()
  498. {
  499. return new StringValidationBuilder<ArrayValidationBuilder<T>>([this](XML::Element& e)
  500. {
  501. element.addChild(e.dublicate());
  502. return this;
  503. });
  504. }
  505. NumberValidationBuilder<ArrayValidationBuilder<T>>* addAcceptedNumberInArray()
  506. {
  507. return new NumberValidationBuilder<ArrayValidationBuilder<T>>([this](XML::Element& e)
  508. {
  509. element.addChild(e.dublicate());
  510. return this;
  511. });
  512. }
  513. BoolValidationBuilder<ArrayValidationBuilder<T>>* addAcceptedBooleanInArray()
  514. {
  515. return new BoolValidationBuilder<ArrayValidationBuilder<T>>([this](XML::Element& e)
  516. {
  517. element.addChild(e.dublicate());
  518. return this;
  519. });
  520. }
  521. ObjectValidationBuilder<ArrayValidationBuilder<T>>* addAcceptedObjectInArray()
  522. {
  523. return new ObjectValidationBuilder<ArrayValidationBuilder<T>>([this](XML::Element& e)
  524. {
  525. element.addChild(e.dublicate());
  526. return this;
  527. });
  528. }
  529. ArrayValidationBuilder<ArrayValidationBuilder<T>>* addAcceptedArrayInArray()
  530. {
  531. return new ArrayValidationBuilder<ArrayValidationBuilder<T>>([this](XML::Element& e)
  532. {
  533. element.addChild(e.dublicate());
  534. return this;
  535. });
  536. }
  537. ArrayValidationBuilder<T>* acceptNullsInArray()
  538. {
  539. element.setAttribute("nullsEnabled", "true");
  540. return this;
  541. }
  542. ArrayValidationBuilder<T>* withDefault(JSONArray* zArray)
  543. {
  544. element.setAttribute("default", zArray->toString());
  545. }
  546. ArrayValidationBuilder<T>* withDefaultNull()
  547. {
  548. element.setAttribute("default", "null");
  549. }
  550. ArrayValidationBuilder<T>* whichCanBeNull()
  551. {
  552. element.setAttribute("nullable", "true");
  553. return this;
  554. }
  555. ArrayValidationBuilder<T>* whichIsOptional()
  556. {
  557. element.setAttribute("optional", "true");
  558. return this;
  559. }
  560. ArrayValidationBuilder<T>* typeSpecifiedByAttribute(Text name)
  561. {
  562. element.setAttribute("typeSpecifiedBy", name);
  563. return this;
  564. }
  565. T* finishArray()
  566. {
  567. T* result = builder(element);
  568. delete this;
  569. return result;
  570. }
  571. };
  572. class JSONValidationResult : public Framework::ReferenceCounter
  573. {
  574. public:
  575. JSONValidationResult();
  576. virtual ~JSONValidationResult();
  577. virtual bool isValid() const = 0;
  578. virtual void printInvalidInfo(int indent = 0) const = 0;
  579. virtual JSONValue* getValidPart() const = 0;
  580. virtual Text getPath() const = 0;
  581. virtual bool isDifferent(const JSONValidationResult* zResult, Text additionalPath) const = 0;
  582. };
  583. class JSONTypeMissmatch : public JSONValidationResult
  584. {
  585. private:
  586. Text path;
  587. JSONValue* foundValue;
  588. XML::Element* expected;
  589. JSONValidationResult* reason;
  590. public:
  591. JSONTypeMissmatch(Text path, JSONValue* foundValue, XML::Element* expected, JSONValidationResult* reason);
  592. ~JSONTypeMissmatch();
  593. bool isValid() const override;
  594. void printInvalidInfo(int indent) const override;
  595. JSONValue* getValidPart() const override;
  596. Text getPath() const override;
  597. bool isDifferent(const JSONValidationResult* zResult, Text additionalPath) const override;
  598. };
  599. class JSONUnknownValue : public JSONValidationResult
  600. {
  601. private:
  602. Text path;
  603. JSONValue* foundValue;
  604. public:
  605. JSONUnknownValue(Text path, JSONValue* foundValue);
  606. ~JSONUnknownValue();
  607. bool isValid() const override;
  608. void printInvalidInfo(int indent) const override;
  609. JSONValue* getValidPart() const override;
  610. Text getPath() const override;
  611. bool isDifferent(const JSONValidationResult* zResult, Text additionalPath) const override;
  612. };
  613. class JSONMissingValue : public JSONValidationResult
  614. {
  615. private:
  616. Text path;
  617. XML::Element* expected;
  618. public:
  619. JSONMissingValue(Text path, XML::Element* expected);
  620. ~JSONMissingValue();
  621. bool isValid() const override;
  622. void printInvalidInfo(int indent) const override;
  623. JSONValue* getValidPart() const override;
  624. Text getPath() const override;
  625. bool isDifferent(const JSONValidationResult* zResult, Text additionalPath) const override;
  626. };
  627. class JSONMissingOneOf : public JSONValidationResult
  628. {
  629. private:
  630. Text path;
  631. RCArray<XML::Element> expected;
  632. public:
  633. JSONMissingOneOf(Text path, XML::Editor expected);
  634. ~JSONMissingOneOf();
  635. bool isValid() const override;
  636. void printInvalidInfo(int indent) const override;
  637. JSONValue* getValidPart() const override;
  638. Text getPath() const override;
  639. bool isDifferent(const JSONValidationResult* zResult, Text additionalPath) const override;
  640. };
  641. class JSONNoTypeMatching : public JSONValidationResult
  642. {
  643. private:
  644. Text path;
  645. JSONValue* foundValue;
  646. RCArray<XML::Element> expected;
  647. RCArray<JSONValidationResult> reasons;
  648. public:
  649. JSONNoTypeMatching(Text path, JSONValue* foundValue, RCArray<XML::Element>& expected, RCArray<JSONValidationResult>& reasons);
  650. ~JSONNoTypeMatching();
  651. bool isValid() const override;
  652. void printInvalidInfo(int indent) const override;
  653. JSONValue* getValidPart() const override;
  654. Text getPath() const override;
  655. bool isDifferent(const JSONValidationResult* zResult, Text additionalPath) const override;
  656. };
  657. class JSONValidValue : public JSONValidationResult
  658. {
  659. private:
  660. Text path;
  661. JSONValue* value;
  662. public:
  663. JSONValidValue(Text path, JSONValue* value);
  664. ~JSONValidValue();
  665. bool isValid() const override;
  666. void printInvalidInfo(int indent) const override;
  667. JSONValue* getValidPart() const override;
  668. Text getPath() const override;
  669. bool isDifferent(const JSONValidationResult* zResult, Text additionalPath) const override;
  670. };
  671. class JSONValidator : public Framework::ReferenceCounter
  672. {
  673. private:
  674. XML::Element* constraints;
  675. public:
  676. __declspec(dllexport) JSONValidator(XML::Element* constraints);
  677. __declspec(dllexport) ~JSONValidator();
  678. __declspec(dllexport) JSONValidationResult* validate(JSONValue* zValue) const;
  679. __declspec(dllexport) bool isValid(JSONValue* zValue) const;
  680. /**
  681. * returns the valid part of the json by performing the following operations in the specified order untill no further changes are made:
  682. * - invalid or unknown object properties are removed
  683. * - missing object properties with default values are added if missing
  684. * - invalid array elements are removed
  685. *
  686. * @param zValue the json value to to extract the valid part without increased reference counter
  687. * @return the valid part or 0 if no valid part exists
  688. */
  689. __declspec(dllexport) JSONValue* getValidParts(JSONValue* zValue) const;
  690. __declspec(dllexport) XML::Element* zConstraints();
  691. private:
  692. __declspec(dllexport) JSONValidationResult* validate(JSONValue* zValue, XML::Element* zConstraints, Text path) const;
  693. __declspec(dllexport) JSONValidationResult* validateMultipleTypes(JSONValue* zChildValue, XML::Element* zPossibleChildConstraints, Text childPath) const;
  694. public:
  695. __declspec(dllexport) static StringValidationBuilder<JSONValidator>* buildForString();
  696. __declspec(dllexport) static NumberValidationBuilder<JSONValidator>* buildForNumber();
  697. __declspec(dllexport) static BoolValidationBuilder<JSONValidator>* buildForBool();
  698. __declspec(dllexport) static ObjectValidationBuilder<JSONValidator>* buildForObject();
  699. __declspec(dllexport) static ArrayValidationBuilder<JSONValidator>* buildForArray();
  700. };
  701. }
  702. }
  703. }