JSON.h 36 KB

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