JSON.h 41 KB

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