JSON.h 41 KB

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