JSON.h 41 KB

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