JSON.h 25 KB

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