JSON.h 41 KB

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