JSON.h 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. #pragma once
  2. #include <functional>
  3. #include <source_location>
  4. #include "Array.h"
  5. #include "ReferenceCounter.h"
  6. #include "Text.h"
  7. #include "Trie.h"
  8. #include "Writer.h"
  9. #include "XML.h"
  10. namespace Framework
  11. {
  12. namespace JSON
  13. {
  14. enum class JSONType
  15. {
  16. NULL_,
  17. BOOLEAN,
  18. NUMBER,
  19. STRING,
  20. ARRAY,
  21. OBJECT
  22. };
  23. class JSONArray;
  24. class JSONObject;
  25. class JSONBool;
  26. class JSONNumber;
  27. class JSONString;
  28. class JSONValue : public virtual ReferenceCounter
  29. {
  30. private:
  31. JSONType type;
  32. protected:
  33. __declspec(dllexport) JSONValue(JSONType type);
  34. public:
  35. __declspec(dllexport) JSONValue();
  36. __declspec(dllexport) virtual ~JSONValue();
  37. __declspec(dllexport) JSONType getType() const;
  38. __declspec(dllexport) virtual Text toString() const;
  39. __declspec(dllexport) virtual JSONValue* clone() const;
  40. __declspec(dllexport) JSONBool* asBool() const;
  41. __declspec(dllexport) JSONNumber* asNumber() const;
  42. __declspec(dllexport) JSONString* asString() const;
  43. __declspec(dllexport) JSONArray* asArray() const;
  44. __declspec(dllexport) JSONObject* asObject() const;
  45. };
  46. class JSONBool : public JSONValue
  47. {
  48. private:
  49. bool b;
  50. public:
  51. __declspec(dllexport) JSONBool(bool b);
  52. __declspec(dllexport) bool getBool() const;
  53. __declspec(dllexport) Text toString() const override;
  54. __declspec(dllexport) JSONValue* clone() const override;
  55. };
  56. class JSONNumber : public JSONValue
  57. {
  58. private:
  59. double number;
  60. public:
  61. __declspec(dllexport) JSONNumber(double num);
  62. __declspec(dllexport) double getNumber() const;
  63. __declspec(dllexport) Text toString() const override;
  64. __declspec(dllexport) JSONValue* clone() const override;
  65. };
  66. class JSONString : public JSONValue
  67. {
  68. private:
  69. Text string;
  70. public:
  71. __declspec(dllexport) JSONString(Text string);
  72. __declspec(dllexport) Text getString() const;
  73. __declspec(dllexport) Text toString() const override;
  74. __declspec(dllexport) JSONValue* clone() const override;
  75. };
  76. class JSONArray : public JSONValue
  77. {
  78. private:
  79. RCArray<JSONValue>* array;
  80. public:
  81. __declspec(dllexport) JSONArray();
  82. __declspec(dllexport) JSONArray(Text string);
  83. __declspec(dllexport) JSONArray(const JSONArray& arr);
  84. __declspec(dllexport) ~JSONArray();
  85. __declspec(dllexport) JSONArray& operator=(const JSONArray& arr);
  86. __declspec(dllexport) void addValue(JSONValue* value);
  87. __declspec(dllexport) void setValue(int i, JSONValue* value);
  88. __declspec(dllexport) void removeValue(int i);
  89. __declspec(dllexport) JSONValue* getValue(int i) const;
  90. __declspec(dllexport) JSONValue* zValue(int i) const;
  91. __declspec(dllexport) int getLength() const;
  92. __declspec(dllexport) bool isValueOfType(
  93. int i, JSONType type) const;
  94. //! Gibt einen Iterator zurück.
  95. //! Mit ++ kann durch die Liste iteriert werden
  96. __declspec(dllexport) ArrayIterator<JSONValue*> begin() const;
  97. __declspec(dllexport) ArrayIterator<JSONValue*> end() const;
  98. template<class T>
  99. RCArray<T>* toRCArray(std::function<T*(JSONValue&)> map) const
  100. {
  101. return toRCArray([](JSONValue& v) { return 1; }, map);
  102. }
  103. template<class T>
  104. RCArray<T>* toRCArray(std::function<bool(JSONValue&)> filter,
  105. std::function<T*(JSONValue&)> map) const
  106. {
  107. RCArray<T>* result = new RCArray<T>();
  108. for (auto v : *array)
  109. {
  110. if (filter(*v))
  111. {
  112. result->add(map(*v));
  113. }
  114. }
  115. return result;
  116. }
  117. template<class T>
  118. Array<T>* toArray(std::function<T(JSONValue&)> map) const
  119. {
  120. return toArray([](JSONValue& v) { return 1; }, map);
  121. ;
  122. }
  123. template<class T>
  124. Array<T>* toArray(std::function<bool(JSONValue&)> filter,
  125. std::function<T(JSONValue&)> map) const
  126. {
  127. Array<T>* result = new Array<T>();
  128. for (auto v : *array)
  129. {
  130. if (filter(*v))
  131. {
  132. result->add(map(*v));
  133. }
  134. }
  135. return result;
  136. }
  137. __declspec(dllexport) Text toString() const override;
  138. __declspec(dllexport) JSONValue* clone() const override;
  139. template<class T> static JSONArray* fromRCArray(
  140. Framework::RCArray<T>& arr, std::function<JSONValue*(T&)> map)
  141. {
  142. JSONArray* array = new JSONArray();
  143. for (T* v : arr)
  144. {
  145. array->addValue(map(*v));
  146. }
  147. return array;
  148. }
  149. template<class T> static JSONArray* fromArray(
  150. Framework::Array<T>& arr, std::function<JSONValue*(T)> map)
  151. {
  152. JSONArray* array = new JSONArray();
  153. for (T v : arr)
  154. {
  155. array->addValue(map(v));
  156. }
  157. return array;
  158. }
  159. };
  160. class JSONObject : public JSONValue
  161. {
  162. private:
  163. Array<Text>* fields;
  164. RCArray<JSONValue>* values;
  165. public:
  166. __declspec(dllexport) JSONObject();
  167. __declspec(dllexport) JSONObject(Text string);
  168. __declspec(dllexport) JSONObject(const JSONObject& obj);
  169. __declspec(dllexport) ~JSONObject();
  170. __declspec(dllexport) JSONObject& operator=(const JSONObject& obj);
  171. __declspec(dllexport) bool addValue(Text field, JSONValue* value);
  172. __declspec(dllexport) bool removeValue(Text field);
  173. __declspec(dllexport) bool hasValue(Text field);
  174. __declspec(dllexport) JSONValue* getValue(Text field);
  175. __declspec(dllexport) JSONValue* zValue(Text field);
  176. __declspec(dllexport) ArrayIterator<Text> getFields();
  177. __declspec(dllexport) ArrayIterator<JSONValue*> getValues();
  178. __declspec(dllexport) int getFieldCount() const;
  179. __declspec(dllexport) bool isValueOfType(
  180. Text field, JSONType type) const;
  181. template<class T> T* parseTo(T* initialState,
  182. std::function<void(
  183. T* obj, Text fieldName, JSONValue& fieldValue)> parser)
  184. const
  185. {
  186. auto fieldsI = fields->begin();
  187. auto valuesI = values->begin();
  188. while (fieldsI && valuesI)
  189. {
  190. parser(initialState, fieldsI, *(JSONValue*)valuesI);
  191. fieldsI++;
  192. valuesI++;
  193. }
  194. return initialState;
  195. }
  196. __declspec(dllexport) Text toString() const override;
  197. __declspec(dllexport) JSONValue* clone() const override;
  198. };
  199. __declspec(dllexport) JSONValue* loadJSONFromFile(Text path);
  200. namespace Parser
  201. {
  202. __declspec(dllexport) int findObjectEndInArray(const char* str);
  203. __declspec(dllexport) Text removeWhitespace(const char* str);
  204. __declspec(dllexport) JSONValue* getValue(const char* str);
  205. __declspec(dllexport) int findFieldEndInObject(const char* str);
  206. __declspec(dllexport) int findValueEndInObject(const char* str);
  207. }; // namespace Parser
  208. namespace Validator
  209. {
  210. class JSONValidationResult : public Framework::ReferenceCounter
  211. {
  212. public:
  213. __declspec(dllexport) JSONValidationResult();
  214. __declspec(dllexport) virtual ~JSONValidationResult();
  215. virtual bool isValid() const = 0;
  216. __declspec(dllexport) void logInvalidInfo(
  217. std::source_location location
  218. = std::source_location::current()) const;
  219. virtual Text getInvalidInfo(int indent = 0) const = 0;
  220. virtual JSONValue* getValidPart(RCArray<JSONValidationResult>*
  221. zRemovedPartsValidationResults)
  222. = 0;
  223. virtual Text getPath() const = 0;
  224. virtual void addBasePath(Text basePath) = 0;
  225. virtual bool isDifferent(
  226. const JSONValidationResult* zResult) const
  227. = 0;
  228. };
  229. class JSONTypeMissmatch : public JSONValidationResult
  230. {
  231. private:
  232. Text path;
  233. JSONValue* foundValue;
  234. XML::Element* expected;
  235. JSONValidationResult* reason;
  236. public:
  237. __declspec(dllexport) JSONTypeMissmatch(Text path,
  238. JSONValue* foundValue,
  239. XML::Element* expected,
  240. JSONValidationResult* reason);
  241. __declspec(dllexport) ~JSONTypeMissmatch();
  242. __declspec(dllexport) bool isValid() const override;
  243. __declspec(dllexport) Text
  244. getInvalidInfo(int indent) const override;
  245. protected:
  246. __declspec(dllexport) JSONValue* getValidPart(
  247. RCArray<JSONValidationResult>*
  248. zRemovedPartsValidationResults) override;
  249. __declspec(dllexport) Text getPath() const override;
  250. __declspec(dllexport) bool isDifferent(
  251. const JSONValidationResult* zResult) const override;
  252. __declspec(dllexport) void addBasePath(Text basePath) override;
  253. };
  254. class JSONUnknownValue : public JSONValidationResult
  255. {
  256. private:
  257. Text path;
  258. JSONValue* foundValue;
  259. public:
  260. __declspec(dllexport)
  261. JSONUnknownValue(Text path, JSONValue* foundValue);
  262. __declspec(dllexport) ~JSONUnknownValue();
  263. __declspec(dllexport) bool isValid() const override;
  264. __declspec(dllexport) Text
  265. getInvalidInfo(int indent) const override;
  266. __declspec(dllexport) JSONValue* getValidPart(
  267. RCArray<JSONValidationResult>*
  268. zRemovedPartsValidationResults) override;
  269. __declspec(dllexport) Text getPath() const override;
  270. __declspec(dllexport) bool isDifferent(
  271. const JSONValidationResult* zResult) const override;
  272. __declspec(dllexport) void addBasePath(Text basePath) override;
  273. };
  274. class JSONMissingValue : public JSONValidationResult
  275. {
  276. private:
  277. Text path;
  278. XML::Element* expected;
  279. public:
  280. __declspec(dllexport)
  281. JSONMissingValue(Text path, XML::Element* expected);
  282. __declspec(dllexport) ~JSONMissingValue();
  283. __declspec(dllexport) bool isValid() const override;
  284. __declspec(dllexport) Text
  285. getInvalidInfo(int indent) const override;
  286. __declspec(dllexport) JSONValue* getValidPart(
  287. RCArray<JSONValidationResult>*
  288. zRemovedPartsValidationResults) override;
  289. __declspec(dllexport) Text getPath() const override;
  290. __declspec(dllexport) bool isDifferent(
  291. const JSONValidationResult* zResult) const override;
  292. __declspec(dllexport) void addBasePath(Text basePath) override;
  293. };
  294. class JSONMissingOneOf : public JSONValidationResult
  295. {
  296. private:
  297. Text path;
  298. RCArray<XML::Element> expected;
  299. public:
  300. __declspec(dllexport)
  301. JSONMissingOneOf(Text path, XML::Editor expected);
  302. __declspec(dllexport) ~JSONMissingOneOf();
  303. __declspec(dllexport) bool isValid() const override;
  304. __declspec(dllexport) Text
  305. getInvalidInfo(int indent) const override;
  306. __declspec(dllexport) JSONValue* getValidPart(
  307. RCArray<JSONValidationResult>*
  308. zRemovedPartsValidationResults) override;
  309. __declspec(dllexport) Text getPath() const override;
  310. __declspec(dllexport) bool isDifferent(
  311. const JSONValidationResult* zResult) const override;
  312. __declspec(dllexport) void addBasePath(Text basePath) override;
  313. };
  314. class JSONNoTypeMatching : public JSONValidationResult
  315. {
  316. private:
  317. Text path;
  318. JSONValue* foundValue;
  319. RCArray<XML::Element> expected;
  320. RCArray<JSONValidationResult> reasons;
  321. public:
  322. __declspec(dllexport) JSONNoTypeMatching(Text path,
  323. JSONValue* foundValue,
  324. RCArray<XML::Element>& expected,
  325. RCArray<JSONValidationResult>& reasons);
  326. __declspec(dllexport) ~JSONNoTypeMatching();
  327. __declspec(dllexport) bool isValid() const override;
  328. __declspec(dllexport) Text
  329. getInvalidInfo(int indent) const override;
  330. __declspec(dllexport) JSONValue* getValidPart(
  331. RCArray<JSONValidationResult>*
  332. zRemovedPartsValidationResults) override;
  333. __declspec(dllexport) Text getPath() const override;
  334. __declspec(dllexport) bool isDifferent(
  335. const JSONValidationResult* zResult) const override;
  336. __declspec(dllexport) void addBasePath(Text basePath) override;
  337. };
  338. class JSONValidValue : public JSONValidationResult
  339. {
  340. private:
  341. Text path;
  342. JSONValue* value;
  343. public:
  344. __declspec(dllexport)
  345. JSONValidValue(Text path, JSONValue* value);
  346. __declspec(dllexport) ~JSONValidValue();
  347. __declspec(dllexport) bool isValid() const override;
  348. __declspec(dllexport) Text
  349. getInvalidInfo(int indent) const override;
  350. __declspec(dllexport) JSONValue* getValidPart(
  351. RCArray<JSONValidationResult>*
  352. zRemovedPartsValidationResults) override;
  353. __declspec(dllexport) Text getPath() const override;
  354. __declspec(dllexport) bool isDifferent(
  355. const JSONValidationResult* zResult) const override;
  356. __declspec(dllexport) void addBasePath(Text basePath) override;
  357. };
  358. template<typename T> class StringValidationBuilder;
  359. template<typename T> class NumberValidationBuilder;
  360. template<typename T> class BoolValidationBuilder;
  361. template<typename T> class ObjectValidationBuilder;
  362. template<typename T> class ArrayValidationBuilder;
  363. template<typename T> class OneOfValidationBuilder;
  364. class JSONValidator : public Framework::ReferenceCounter
  365. {
  366. private:
  367. XML::Element* constraints;
  368. Trie<XML::Element*> typeConstraints;
  369. public:
  370. __declspec(dllexport) JSONValidator(XML::Element* constraints);
  371. __declspec(dllexport) ~JSONValidator();
  372. __declspec(dllexport) JSONValidationResult* validate(
  373. JSONValue* zValue) const;
  374. __declspec(dllexport) bool isValid(JSONValue* zValue) const;
  375. /**
  376. * returns the valid part of the json by performing the
  377. * following operations in the specified order untill no further
  378. * changes are made:
  379. * - invalid or unknown object properties are removed
  380. * - missing object properties with default values are added if
  381. * missing
  382. * - invalid array elements are removed
  383. *
  384. * @param zValue the json value to to extract the valid part
  385. * without increased reference counter
  386. * @return the valid part or 0 if no valid part exists
  387. */
  388. __declspec(dllexport) JSONValue* getValidParts(
  389. JSONValue* zValue,
  390. RCArray<JSONValidationResult>*
  391. zRemovedPartsValidationResults) const;
  392. __declspec(dllexport) XML::Element* zConstraints();
  393. private:
  394. __declspec(dllexport) JSONValidationResult* validate(
  395. JSONValue* zValue,
  396. XML::Element* zConstraints,
  397. Text path) const;
  398. __declspec(dllexport)
  399. JSONValidationResult* validateMultipleTypes(
  400. JSONValue* zChildValue,
  401. XML::Element* zPossibleChildConstraints,
  402. Text childPath) const;
  403. public:
  404. __declspec(dllexport) static StringValidationBuilder<
  405. JSONValidator>* buildForString();
  406. __declspec(dllexport) static NumberValidationBuilder<
  407. JSONValidator>* buildForNumber();
  408. __declspec(dllexport) static BoolValidationBuilder<
  409. JSONValidator>* buildForBool();
  410. __declspec(dllexport) static ObjectValidationBuilder<
  411. JSONValidator>* buildForObject();
  412. __declspec(dllexport) static ArrayValidationBuilder<
  413. JSONValidator>* buildForArray();
  414. __declspec(dllexport) static OneOfValidationBuilder<
  415. JSONValidator>* buildForOneOf();
  416. __declspec(
  417. dllexport) static JSONValidator* buildForObjectReference(Text
  418. objectId);
  419. };
  420. template<typename T> class StringValidationBuilder
  421. {
  422. private:
  423. XML::Element element;
  424. std::function<T*(XML::Element& element)> builder;
  425. public:
  426. StringValidationBuilder(
  427. std::function<T*(XML::Element& element)> builder)
  428. : element("<string/>"),
  429. builder(builder)
  430. {}
  431. StringValidationBuilder<T>* withExactMatch(Text value)
  432. {
  433. element.setAttribute("equals", value);
  434. return this;
  435. }
  436. StringValidationBuilder<T>* whichContainsMatch(Text value)
  437. {
  438. element.setAttribute("contains", value);
  439. return this;
  440. }
  441. StringValidationBuilder<T>* whichStartsWithMatch(Text value)
  442. {
  443. element.setAttribute("startsWith", value);
  444. return this;
  445. }
  446. StringValidationBuilder<T>* whichEndsWithMatch(Text value)
  447. {
  448. element.setAttribute("endsWith", value);
  449. return this;
  450. }
  451. StringValidationBuilder<T>* whichIsOneOf(RCArray<Text> values)
  452. {
  453. JSONArray arr;
  454. for (Text* str : values)
  455. arr.addValue(new JSONString(str->getText()));
  456. element.setAttribute("oneOf", arr.toString());
  457. return this;
  458. }
  459. StringValidationBuilder<T>* whichIsOneOf(
  460. std::initializer_list<Text> values)
  461. {
  462. JSONArray arr;
  463. for (Text str : values)
  464. arr.addValue(new JSONString(str));
  465. element.setAttribute("oneOf", arr.toString());
  466. return this;
  467. }
  468. StringValidationBuilder<T>* whichIs(Text value)
  469. {
  470. JSONArray arr;
  471. arr.addValue(new JSONString(value));
  472. element.setAttribute("oneOf", arr.toString());
  473. return this;
  474. }
  475. StringValidationBuilder<T>* withDefault(Text value)
  476. {
  477. element.setAttribute(
  478. "default", JSONString(value).toString());
  479. return this;
  480. }
  481. StringValidationBuilder<T>* withDefaultNull()
  482. {
  483. element.setAttribute("default", "null");
  484. return this;
  485. }
  486. StringValidationBuilder<T>* whichCanBeNull()
  487. {
  488. element.setAttribute("nullable", "true");
  489. return this;
  490. }
  491. StringValidationBuilder<T>* whichIsOptional()
  492. {
  493. element.setAttribute("optional", "true");
  494. return this;
  495. }
  496. T* finishString()
  497. {
  498. T* result = builder(element);
  499. delete this;
  500. return result;
  501. }
  502. };
  503. template<typename T> class NumberValidationBuilder
  504. {
  505. private:
  506. XML::Element element;
  507. std::function<T*(XML::Element& element)> builder;
  508. public:
  509. NumberValidationBuilder(
  510. std::function<T*(XML::Element& element)> builder)
  511. : element("<number/>"),
  512. builder(builder)
  513. {}
  514. NumberValidationBuilder<T>* whichIs(double value)
  515. {
  516. element.setAttribute("equals", Text(value));
  517. return this;
  518. }
  519. NumberValidationBuilder<T>* whichIsLessOrEqual(double value)
  520. {
  521. element.setAttribute("lessOrEqual", Text(value));
  522. return this;
  523. }
  524. NumberValidationBuilder<T>* whichIsGreaterOrEqual(double value)
  525. {
  526. element.setAttribute("greaterOrEqual", Text(value));
  527. return this;
  528. }
  529. NumberValidationBuilder<T>* whichIsLessThen(double value)
  530. {
  531. element.setAttribute("less", Text(value));
  532. return this;
  533. }
  534. NumberValidationBuilder<T>* whichIsGreaterThen(double value)
  535. {
  536. element.setAttribute("greater", Text(value));
  537. return this;
  538. }
  539. NumberValidationBuilder<T>* withDefault(double value)
  540. {
  541. element.setAttribute(
  542. "default", JSONNumber(value).toString());
  543. return this;
  544. }
  545. NumberValidationBuilder<T>* withDefaultNull()
  546. {
  547. element.setAttribute("default", "null");
  548. return this;
  549. }
  550. NumberValidationBuilder<T>* whichCanBeNull()
  551. {
  552. element.setAttribute("nullable", "true");
  553. return this;
  554. }
  555. NumberValidationBuilder<T>* whichIsOptional()
  556. {
  557. element.setAttribute("optional", "true");
  558. return this;
  559. }
  560. T* finishNumber()
  561. {
  562. T* result = builder(element);
  563. delete this;
  564. return result;
  565. }
  566. };
  567. template<typename T> class BoolValidationBuilder
  568. {
  569. private:
  570. XML::Element element;
  571. std::function<T*(XML::Element& element)> builder;
  572. public:
  573. BoolValidationBuilder(
  574. std::function<T*(XML::Element& element)> builder)
  575. : element("<bool/>"),
  576. builder(builder)
  577. {}
  578. BoolValidationBuilder<T>* whichIs(bool value)
  579. {
  580. element.setAttribute("equals", value ? "true" : "false");
  581. return this;
  582. }
  583. BoolValidationBuilder<T>* withDefault(bool value)
  584. {
  585. element.setAttribute("default", JSONBool(value).toString());
  586. return this;
  587. }
  588. BoolValidationBuilder<T>* withDefaultNull()
  589. {
  590. element.setAttribute("default", "null");
  591. return this;
  592. }
  593. BoolValidationBuilder<T>* whichCanBeNull()
  594. {
  595. element.setAttribute("nullable", "true");
  596. return this;
  597. }
  598. BoolValidationBuilder<T>* whichIsOptional()
  599. {
  600. element.setAttribute("optional", "true");
  601. return this;
  602. }
  603. T* finishBool()
  604. {
  605. T* result = builder(element);
  606. delete this;
  607. return result;
  608. }
  609. };
  610. template<typename T> class ArrayValidationBuilder;
  611. template<typename T> class ObjectValidationBuilder
  612. {
  613. private:
  614. XML::Element element;
  615. std::function<T*(XML::Element& element)> builder;
  616. public:
  617. ObjectValidationBuilder(
  618. std::function<T*(XML::Element& element)> builder)
  619. : element("<object></object>"),
  620. builder(builder)
  621. {}
  622. ObjectValidationBuilder<T>* setObjectReferenceId(Text id)
  623. {
  624. element.setAttribute("id", id);
  625. return this;
  626. }
  627. NumberValidationBuilder<ObjectValidationBuilder<T>>*
  628. withRequiredNumber(Text name)
  629. {
  630. return new NumberValidationBuilder<
  631. ObjectValidationBuilder<T>>(
  632. [this, name](XML::Element& e) {
  633. if (!element.selectChildsByAttribute("name", name)
  634. .exists())
  635. {
  636. XML::Element* attr
  637. = new XML::Element("<value></value>");
  638. attr->setAttribute("name", name);
  639. element.addChild(attr);
  640. }
  641. element.selectChildsByAttribute("name", name)
  642. .addChild(e.dublicate());
  643. return this;
  644. });
  645. }
  646. StringValidationBuilder<ObjectValidationBuilder<T>>*
  647. withRequiredString(Text name)
  648. {
  649. return new StringValidationBuilder<
  650. ObjectValidationBuilder<T>>(
  651. [this, name](XML::Element& e) {
  652. if (!element.selectChildsByAttribute("name", name)
  653. .exists())
  654. {
  655. XML::Element* attr
  656. = new XML::Element("<value></value>");
  657. attr->setAttribute("name", name);
  658. element.addChild(attr);
  659. }
  660. element.selectChildsByAttribute("name", name)
  661. .addChild(e.dublicate());
  662. return this;
  663. });
  664. }
  665. BoolValidationBuilder<ObjectValidationBuilder<T>>*
  666. withRequiredBool(Text name)
  667. {
  668. return new BoolValidationBuilder<
  669. ObjectValidationBuilder<T>>(
  670. [this, name](XML::Element& e) {
  671. if (!element.selectChildsByAttribute("name", name)
  672. .exists())
  673. {
  674. XML::Element* attr
  675. = new XML::Element("<value></value>");
  676. attr->setAttribute("name", name);
  677. element.addChild(attr);
  678. }
  679. element.selectChildsByAttribute("name", name)
  680. .addChild(e.dublicate());
  681. return this;
  682. });
  683. }
  684. ArrayValidationBuilder<ObjectValidationBuilder<T>>*
  685. withRequiredArray(Text name)
  686. {
  687. return new ArrayValidationBuilder<
  688. ObjectValidationBuilder<T>>(
  689. [this, name](XML::Element& e) {
  690. if (!element.selectChildsByAttribute("name", name)
  691. .exists())
  692. {
  693. XML::Element* attr
  694. = new XML::Element("<value></value>");
  695. attr->setAttribute("name", name);
  696. element.addChild(attr);
  697. }
  698. element.selectChildsByAttribute("name", name)
  699. .addChild(e.dublicate());
  700. return this;
  701. });
  702. }
  703. ObjectValidationBuilder<ObjectValidationBuilder<T>>*
  704. withRequiredObject(Text name)
  705. {
  706. return new ObjectValidationBuilder<
  707. ObjectValidationBuilder<T>>(
  708. [this, name](XML::Element& e) {
  709. if (!element.selectChildsByAttribute("name", name)
  710. .exists())
  711. {
  712. XML::Element* attr
  713. = new XML::Element("<value></value>");
  714. attr->setAttribute("name", name);
  715. element.addChild(attr);
  716. }
  717. element.selectChildsByAttribute("name", name)
  718. .addChild(e.dublicate());
  719. return this;
  720. });
  721. }
  722. ObjectValidationBuilder<T>* withRequiredAttribute(
  723. Text name, JSONValidator* validator)
  724. {
  725. if (!element.selectChildsByAttribute("name", name).exists())
  726. {
  727. XML::Element* attr
  728. = new XML::Element("<value></value>");
  729. attr->setAttribute("name", name);
  730. element.addChild(attr);
  731. }
  732. element.selectChildsByAttribute("name", name)
  733. .addChild(validator->zConstraints()->dublicate());
  734. validator->release();
  735. return this;
  736. }
  737. ObjectValidationBuilder<T>* withDefault(JSONObject* obj)
  738. {
  739. element.setAttribute("default", obj->toString());
  740. obj->release();
  741. return this;
  742. }
  743. ObjectValidationBuilder<T>* withDefaultNull()
  744. {
  745. element.setAttribute("default", "null");
  746. return this;
  747. }
  748. ObjectValidationBuilder<T>* whichCanBeNull()
  749. {
  750. element.setAttribute("nullable", "true");
  751. return this;
  752. }
  753. ObjectValidationBuilder<T>* whichIsOptional()
  754. {
  755. element.setAttribute("optional", "true");
  756. return this;
  757. }
  758. ArrayValidationBuilder<T>* typeOfValuesSpecifiedByAttribute(
  759. Text valueName, Text attributeName)
  760. {
  761. if (!element.selectChildsByAttribute("name", valueName)
  762. .exists())
  763. {
  764. XML::Element* attr
  765. = new XML::Element("<value></value>");
  766. attr->setAttribute("name", valueName);
  767. element.addChild(attr);
  768. }
  769. element.selectChildsByAttribute("name", valueName)
  770. .setAttribute("typeSpecifiedBy", attributeName);
  771. return this;
  772. }
  773. ObjectValidationBuilder<T>* removeInvalidEntries()
  774. {
  775. element.setAttribute("removeInvalidEntries", "true");
  776. return this;
  777. }
  778. ObjectValidationBuilder<T>* allowAdditionalAttriutes()
  779. {
  780. element.setAttribute("allowAdditionalAttributes", "true");
  781. return this;
  782. }
  783. T* finishObject()
  784. {
  785. T* result = builder(element);
  786. delete this;
  787. return result;
  788. }
  789. };
  790. template<typename T> class ArrayValidationBuilder
  791. {
  792. private:
  793. XML::Element element;
  794. std::function<T*(XML::Element& element)> builder;
  795. public:
  796. ArrayValidationBuilder(
  797. std::function<T*(XML::Element& element)> builder)
  798. : element("<array></array>"),
  799. builder(builder)
  800. {}
  801. StringValidationBuilder<ArrayValidationBuilder<T>>*
  802. addAcceptedStringInArray()
  803. {
  804. return new StringValidationBuilder<
  805. ArrayValidationBuilder<T>>([this](XML::Element& e) {
  806. element.addChild(e.dublicate());
  807. return this;
  808. });
  809. }
  810. NumberValidationBuilder<ArrayValidationBuilder<T>>*
  811. addAcceptedNumberInArray()
  812. {
  813. return new NumberValidationBuilder<
  814. ArrayValidationBuilder<T>>([this](XML::Element& e) {
  815. element.addChild(e.dublicate());
  816. return this;
  817. });
  818. }
  819. BoolValidationBuilder<ArrayValidationBuilder<T>>*
  820. addAcceptedBooleanInArray()
  821. {
  822. return new BoolValidationBuilder<ArrayValidationBuilder<T>>(
  823. [this](XML::Element& e) {
  824. element.addChild(e.dublicate());
  825. return this;
  826. });
  827. }
  828. ObjectValidationBuilder<ArrayValidationBuilder<T>>*
  829. addAcceptedObjectInArray()
  830. {
  831. return new ObjectValidationBuilder<
  832. ArrayValidationBuilder<T>>([this](XML::Element& e) {
  833. element.addChild(e.dublicate());
  834. return this;
  835. });
  836. }
  837. ArrayValidationBuilder<ArrayValidationBuilder<T>>*
  838. addAcceptedArrayInArray()
  839. {
  840. return new ArrayValidationBuilder<
  841. ArrayValidationBuilder<T>>([this](XML::Element& e) {
  842. element.addChild(e.dublicate());
  843. return this;
  844. });
  845. }
  846. ArrayValidationBuilder<T>* addAcceptedTypeInArray(
  847. JSONValidator* validator)
  848. {
  849. element.addChild(validator->zConstraints()->dublicate());
  850. validator->release();
  851. return this;
  852. }
  853. ArrayValidationBuilder<T>* acceptNullsInArray()
  854. {
  855. element.setAttribute("nullsEnabled", "true");
  856. return this;
  857. }
  858. ArrayValidationBuilder<T>* withDefault(JSONArray* array)
  859. {
  860. element.setAttribute("default", array->toString());
  861. array->release();
  862. return this;
  863. }
  864. ArrayValidationBuilder<T>* withDefaultNull()
  865. {
  866. element.setAttribute("default", "null");
  867. return this;
  868. }
  869. ArrayValidationBuilder<T>* whichCanBeNull()
  870. {
  871. element.setAttribute("nullable", "true");
  872. return this;
  873. }
  874. ArrayValidationBuilder<T>* whichIsOptional()
  875. {
  876. element.setAttribute("optional", "true");
  877. return this;
  878. }
  879. ArrayValidationBuilder<T>* typeSpecifiedByAttribute(Text name)
  880. {
  881. element.setAttribute("typeSpecifiedBy", name);
  882. return this;
  883. }
  884. ArrayValidationBuilder<T>* removeInvalidEntries()
  885. {
  886. element.setAttribute("removeInvalidEntries", "true");
  887. return this;
  888. }
  889. T* finishArray()
  890. {
  891. T* result = builder(element);
  892. delete this;
  893. return result;
  894. }
  895. };
  896. template<typename T> class OneOfValidationBuilder
  897. {
  898. private:
  899. XML::Element element;
  900. std::function<T*(XML::Element& element)> builder;
  901. public:
  902. OneOfValidationBuilder(
  903. std::function<T*(XML::Element& element)> builder)
  904. : element("<oneOf></oneOf>"),
  905. builder(builder)
  906. {}
  907. StringValidationBuilder<OneOfValidationBuilder<T>>*
  908. addAcceptedStringInArray()
  909. {
  910. return new StringValidationBuilder<
  911. OneOfValidationBuilder<T>>([this](XML::Element& e) {
  912. element.addChild(e.dublicate());
  913. return this;
  914. });
  915. }
  916. NumberValidationBuilder<OneOfValidationBuilder<T>>*
  917. addAcceptedNumberInArray()
  918. {
  919. return new NumberValidationBuilder<
  920. OneOfValidationBuilder<T>>([this](XML::Element& e) {
  921. element.addChild(e.dublicate());
  922. return this;
  923. });
  924. }
  925. BoolValidationBuilder<OneOfValidationBuilder<T>>*
  926. addAcceptedBooleanInArray()
  927. {
  928. return new BoolValidationBuilder<OneOfValidationBuilder<T>>(
  929. [this](XML::Element& e) {
  930. element.addChild(e.dublicate());
  931. return this;
  932. });
  933. }
  934. ObjectValidationBuilder<OneOfValidationBuilder<T>>*
  935. addAcceptedObjectInArray()
  936. {
  937. return new ObjectValidationBuilder<
  938. OneOfValidationBuilder<T>>([this](XML::Element& e) {
  939. element.addChild(e.dublicate());
  940. return this;
  941. });
  942. }
  943. ArrayValidationBuilder<OneOfValidationBuilder<T>>*
  944. addAcceptedArrayInArray()
  945. {
  946. return new ArrayValidationBuilder<
  947. OneOfValidationBuilder<T>>([this](XML::Element& e) {
  948. element.addChild(e.dublicate());
  949. return this;
  950. });
  951. }
  952. OneOfValidationBuilder<T>* addAcceptedType(
  953. JSONValidator* validator)
  954. {
  955. element.addChild(validator->zConstraints()->dublicate());
  956. validator->release();
  957. return this;
  958. }
  959. OneOfValidationBuilder<T>* typeSpecifiedByAttribute(Text name)
  960. {
  961. element.setAttribute("typeSpecifiedBy", name);
  962. return this;
  963. }
  964. T* finishOneOf()
  965. {
  966. T* result = builder(element);
  967. delete this;
  968. return result;
  969. }
  970. };
  971. } // namespace Validator
  972. } // namespace JSON
  973. } // namespace Framework