JSON.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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>* withDefault(Text value)
  372. {
  373. element.setAttribute("default", JSONString(value).toString());
  374. return this;
  375. }
  376. StringValidationBuilder<T>* withDefaultNull()
  377. {
  378. element.setAttribute("default", "null");
  379. return this;
  380. }
  381. StringValidationBuilder<T>* whichCanBeNull()
  382. {
  383. element.setAttribute("nullable", "true");
  384. return this;
  385. }
  386. StringValidationBuilder<T>* whichIsOptional()
  387. {
  388. element.setAttribute("optional", "true");
  389. return this;
  390. }
  391. T* finishString()
  392. {
  393. T* result = builder(element);
  394. delete this;
  395. return result;
  396. }
  397. };
  398. template<typename T>
  399. class NumberValidationBuilder
  400. {
  401. private:
  402. XML::Element element;
  403. std::function<T* (XML::Element& element)> builder;
  404. public:
  405. NumberValidationBuilder(std::function<T* (XML::Element& element)> builder)
  406. : element("<number/>"),
  407. builder(builder)
  408. {}
  409. NumberValidationBuilder<T>* whichIs(double value)
  410. {
  411. element.setAttribute("equals", Text(value));
  412. return this;
  413. }
  414. NumberValidationBuilder<T>* whichIsLessOrEqual(double value)
  415. {
  416. element.setAttribute("lessOrEqual", Text(value));
  417. return this;
  418. }
  419. NumberValidationBuilder<T>* whichIsGreaterOrEqual(double value)
  420. {
  421. element.setAttribute("greaterOrEqual", Text(value));
  422. return this;
  423. }
  424. NumberValidationBuilder<T>* whichIsLessThen(double value)
  425. {
  426. element.setAttribute("less", Text(value));
  427. return this;
  428. }
  429. NumberValidationBuilder<T>* whichIsGreaterThen(double value)
  430. {
  431. element.setAttribute("greater", Text(value));
  432. return this;
  433. }
  434. NumberValidationBuilder<T>* withDefault(double value)
  435. {
  436. element.setAttribute("default", JSONNumber(value).toString());
  437. return this;
  438. }
  439. NumberValidationBuilder<T>* withDefaultNull()
  440. {
  441. element.setAttribute("default", "null");
  442. return this;
  443. }
  444. NumberValidationBuilder<T>* whichCanBeNull()
  445. {
  446. element.setAttribute("nullable", "true");
  447. return this;
  448. }
  449. NumberValidationBuilder<T>* whichIsOptional()
  450. {
  451. element.setAttribute("optional", "true");
  452. return this;
  453. }
  454. T* finishNumber()
  455. {
  456. T* result = builder(element);
  457. delete this;
  458. return result;
  459. }
  460. };
  461. template<typename T>
  462. class BoolValidationBuilder
  463. {
  464. private:
  465. XML::Element element;
  466. std::function<T* (XML::Element& element)> builder;
  467. public:
  468. BoolValidationBuilder(std::function<T* (XML::Element& element)> builder)
  469. : element("<bool/>"),
  470. builder(builder)
  471. {}
  472. BoolValidationBuilder<T>* whichIs(bool value)
  473. {
  474. element.setAttribute("equals", value ? "true" : "false");
  475. return this;
  476. }
  477. BoolValidationBuilder<T>* withDefault(bool value)
  478. {
  479. element.setAttribute("default", JSONBool(value).toString());
  480. return this;
  481. }
  482. BoolValidationBuilder<T>* withDefaultNull()
  483. {
  484. element.setAttribute("default", "null");
  485. return this;
  486. }
  487. BoolValidationBuilder<T>* whichCanBeNull()
  488. {
  489. element.setAttribute("nullable", "true");
  490. return this;
  491. }
  492. BoolValidationBuilder<T>* whichIsOptional()
  493. {
  494. element.setAttribute("optional", "true");
  495. return this;
  496. }
  497. T* finishBool()
  498. {
  499. T* result = builder(element);
  500. delete this;
  501. return result;
  502. }
  503. };
  504. template<typename T>
  505. class ArrayValidationBuilder;
  506. template<typename T>
  507. class ObjectValidationBuilder
  508. {
  509. private:
  510. XML::Element element;
  511. std::function<T* (XML::Element& element)> builder;
  512. public:
  513. ObjectValidationBuilder(std::function<T* (XML::Element& element)> builder)
  514. : element("<object></object>"),
  515. builder(builder)
  516. {}
  517. NumberValidationBuilder<ObjectValidationBuilder<T>>* withRequiredNumber(Text name)
  518. {
  519. return new NumberValidationBuilder<ObjectValidationBuilder<T>>([this, name](XML::Element& e)
  520. {
  521. if (!element.selectChildsByAttribute("name", name).exists())
  522. {
  523. XML::Element* attr = new XML::Element("<value></value>");
  524. attr->setAttribute("name", name);
  525. element.addChild(attr);
  526. }
  527. element.selectChildsByAttribute("name", name).addChild(e.dublicate());
  528. return this;
  529. });
  530. }
  531. StringValidationBuilder<ObjectValidationBuilder<T>>* withRequiredString(Text name)
  532. {
  533. return new StringValidationBuilder<ObjectValidationBuilder<T>>([this, name](XML::Element& e)
  534. {
  535. if (!element.selectChildsByAttribute("name", name).exists())
  536. {
  537. XML::Element* attr = new XML::Element("<value></value>");
  538. attr->setAttribute("name", name);
  539. element.addChild(attr);
  540. }
  541. element.selectChildsByAttribute("name", name).addChild(e.dublicate());
  542. return this;
  543. });
  544. }
  545. BoolValidationBuilder<ObjectValidationBuilder<T>>* withRequiredBool(Text name)
  546. {
  547. return new BoolValidationBuilder<ObjectValidationBuilder<T>>([this, name](XML::Element& e)
  548. {
  549. if (!element.selectChildsByAttribute("name", name).exists())
  550. {
  551. XML::Element* attr = new XML::Element("<value></value>");
  552. attr->setAttribute("name", name);
  553. element.addChild(attr);
  554. }
  555. element.selectChildsByAttribute("name", name).addChild(e.dublicate());
  556. return this;
  557. });
  558. }
  559. ArrayValidationBuilder<ObjectValidationBuilder<T>>* withRequiredArray(Text name)
  560. {
  561. return new ArrayValidationBuilder<ObjectValidationBuilder<T>>([this, name](XML::Element& e)
  562. {
  563. if (!element.selectChildsByAttribute("name", name).exists())
  564. {
  565. XML::Element* attr = new XML::Element("<value></value>");
  566. attr->setAttribute("name", name);
  567. element.addChild(attr);
  568. }
  569. element.selectChildsByAttribute("name", name).addChild(e.dublicate());
  570. return this;
  571. });
  572. }
  573. ObjectValidationBuilder<ObjectValidationBuilder<T>>* withRequiredObject(Text name)
  574. {
  575. return new ObjectValidationBuilder<ObjectValidationBuilder<T>>([this, name](XML::Element& e)
  576. {
  577. if (!element.selectChildsByAttribute("name", name).exists())
  578. {
  579. XML::Element* attr = new XML::Element("<value></value>");
  580. attr->setAttribute("name", name);
  581. element.addChild(attr);
  582. }
  583. element.selectChildsByAttribute("name", name).addChild(e.dublicate());
  584. return this;
  585. });
  586. }
  587. ObjectValidationBuilder<T>* withRequiredAttribute(Text name, JSONValidator* validator)
  588. {
  589. if (!element.selectChildsByAttribute("name", name).exists())
  590. {
  591. XML::Element* attr = new XML::Element("<value></value>");
  592. attr->setAttribute("name", name);
  593. element.addChild(attr);
  594. }
  595. element.selectChildsByAttribute("name", name).addChild(validator->zConstraints()->dublicate());
  596. validator->release();
  597. return this;
  598. }
  599. ObjectValidationBuilder<T>* withDefault(JSONObject* obj)
  600. {
  601. element.setAttribute("default", obj->toString()); obj
  602. obj->release();
  603. return this;
  604. }
  605. ObjectValidationBuilder<T>* withDefaultNull()
  606. {
  607. element.setAttribute("default", "null");
  608. return this;
  609. }
  610. ObjectValidationBuilder<T>* whichCanBeNull()
  611. {
  612. element.setAttribute("nullable", "true");
  613. return this;
  614. }
  615. ObjectValidationBuilder<T>* whichIsOptional()
  616. {
  617. element.setAttribute("optional", "true");
  618. return this;
  619. }
  620. ArrayValidationBuilder<T>* typeOfValuesSpecifiedByAttribute(Text valueName, Text attributeName)
  621. {
  622. if (!element.selectChildsByAttribute("name", valueName).exists())
  623. {
  624. XML::Element* attr = new XML::Element("<value></value>");
  625. attr->setAttribute("name", valueName);
  626. element.addChild(attr);
  627. }
  628. element.selectChildsByAttribute("name", valueName).setAttribute("typeSpecifiedBy", attributeName);
  629. return this;
  630. }
  631. ObjectValidationBuilder<T>* removeInvalidEntries()
  632. {
  633. element.setAttribute("removeInvalidEntries", "true");
  634. return this;
  635. }
  636. T* finishObject()
  637. {
  638. T* result = builder(element);
  639. delete this;
  640. return result;
  641. }
  642. };
  643. template<typename T>
  644. class ArrayValidationBuilder
  645. {
  646. private:
  647. XML::Element element;
  648. std::function<T* (XML::Element& element)> builder;
  649. public:
  650. ArrayValidationBuilder(std::function<T* (XML::Element& element)> builder)
  651. : element("<array></array>"),
  652. builder(builder)
  653. {}
  654. StringValidationBuilder<ArrayValidationBuilder<T>>* addAcceptedStringInArray()
  655. {
  656. return new StringValidationBuilder<ArrayValidationBuilder<T>>([this](XML::Element& e)
  657. {
  658. element.addChild(e.dublicate());
  659. return this;
  660. });
  661. }
  662. NumberValidationBuilder<ArrayValidationBuilder<T>>* addAcceptedNumberInArray()
  663. {
  664. return new NumberValidationBuilder<ArrayValidationBuilder<T>>([this](XML::Element& e)
  665. {
  666. element.addChild(e.dublicate());
  667. return this;
  668. });
  669. }
  670. BoolValidationBuilder<ArrayValidationBuilder<T>>* addAcceptedBooleanInArray()
  671. {
  672. return new BoolValidationBuilder<ArrayValidationBuilder<T>>([this](XML::Element& e)
  673. {
  674. element.addChild(e.dublicate());
  675. return this;
  676. });
  677. }
  678. ObjectValidationBuilder<ArrayValidationBuilder<T>>* addAcceptedObjectInArray()
  679. {
  680. return new ObjectValidationBuilder<ArrayValidationBuilder<T>>([this](XML::Element& e)
  681. {
  682. element.addChild(e.dublicate());
  683. return this;
  684. });
  685. }
  686. ArrayValidationBuilder<ArrayValidationBuilder<T>>* addAcceptedArrayInArray()
  687. {
  688. return new ArrayValidationBuilder<ArrayValidationBuilder<T>>([this](XML::Element& e)
  689. {
  690. element.addChild(e.dublicate());
  691. return this;
  692. });
  693. }
  694. ArrayValidationBuilder<T>* addAcceptedTypeInArray(JSONValidator* validator)
  695. {
  696. element.addChild(validator->zConstraints()->dublicate());
  697. validator->release();
  698. return this;
  699. }
  700. ArrayValidationBuilder<T>* acceptNullsInArray()
  701. {
  702. element.setAttribute("nullsEnabled", "true");
  703. return this;
  704. }
  705. ArrayValidationBuilder<T>* withDefault(JSONArray* array)
  706. {
  707. element.setAttribute("default", array->toString());
  708. array->release();
  709. return this;
  710. }
  711. ArrayValidationBuilder<T>* withDefaultNull()
  712. {
  713. element.setAttribute("default", "null");
  714. return this;
  715. }
  716. ArrayValidationBuilder<T>* whichCanBeNull()
  717. {
  718. element.setAttribute("nullable", "true");
  719. return this;
  720. }
  721. ArrayValidationBuilder<T>* whichIsOptional()
  722. {
  723. element.setAttribute("optional", "true");
  724. return this;
  725. }
  726. ArrayValidationBuilder<T>* typeSpecifiedByAttribute(Text name)
  727. {
  728. element.setAttribute("typeSpecifiedBy", name);
  729. return this;
  730. }
  731. ArrayValidationBuilder<T>* removeInvalidEntries()
  732. {
  733. element.setAttribute("removeInvalidEntries", "true");
  734. return this;
  735. }
  736. T* finishArray()
  737. {
  738. T* result = builder(element);
  739. delete this;
  740. return result;
  741. }
  742. };
  743. }
  744. }
  745. }