JSON.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  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. __declspec(dllexport) static JSONValidator* buildForObjectReference(Text objectId);
  388. };
  389. template<typename T> class StringValidationBuilder
  390. {
  391. private:
  392. XML::Element element;
  393. std::function<T*(XML::Element& element)> builder;
  394. public:
  395. StringValidationBuilder(
  396. std::function<T*(XML::Element& element)> builder)
  397. : element("<string/>"),
  398. builder(builder)
  399. {}
  400. StringValidationBuilder<T>* withExactMatch(Text value)
  401. {
  402. element.setAttribute("equals", value);
  403. return this;
  404. }
  405. StringValidationBuilder<T>* whichContainsMatch(Text value)
  406. {
  407. element.setAttribute("contains", value);
  408. return this;
  409. }
  410. StringValidationBuilder<T>* whichStartsWithMatch(Text value)
  411. {
  412. element.setAttribute("startsWith", value);
  413. return this;
  414. }
  415. StringValidationBuilder<T>* whichEndsWithMatch(Text value)
  416. {
  417. element.setAttribute("endsWith", value);
  418. return this;
  419. }
  420. StringValidationBuilder<T>* whichIsOneOf(RCArray<Text>& values)
  421. {
  422. JSONArray arr;
  423. for (Text* str : values)
  424. arr.addValue(new JSONString(str->getText()));
  425. element.setAttribute("oneOf", arr.toString());
  426. return this;
  427. }
  428. StringValidationBuilder<T>* withDefault(Text value)
  429. {
  430. element.setAttribute(
  431. "default", JSONString(value).toString());
  432. return this;
  433. }
  434. StringValidationBuilder<T>* withDefaultNull()
  435. {
  436. element.setAttribute("default", "null");
  437. return this;
  438. }
  439. StringValidationBuilder<T>* whichCanBeNull()
  440. {
  441. element.setAttribute("nullable", "true");
  442. return this;
  443. }
  444. StringValidationBuilder<T>* whichIsOptional()
  445. {
  446. element.setAttribute("optional", "true");
  447. return this;
  448. }
  449. T* finishString()
  450. {
  451. T* result = builder(element);
  452. delete this;
  453. return result;
  454. }
  455. };
  456. template<typename T> class NumberValidationBuilder
  457. {
  458. private:
  459. XML::Element element;
  460. std::function<T*(XML::Element& element)> builder;
  461. public:
  462. NumberValidationBuilder(
  463. std::function<T*(XML::Element& element)> builder)
  464. : element("<number/>"),
  465. builder(builder)
  466. {}
  467. NumberValidationBuilder<T>* whichIs(double value)
  468. {
  469. element.setAttribute("equals", Text(value));
  470. return this;
  471. }
  472. NumberValidationBuilder<T>* whichIsLessOrEqual(double value)
  473. {
  474. element.setAttribute("lessOrEqual", Text(value));
  475. return this;
  476. }
  477. NumberValidationBuilder<T>* whichIsGreaterOrEqual(double value)
  478. {
  479. element.setAttribute("greaterOrEqual", Text(value));
  480. return this;
  481. }
  482. NumberValidationBuilder<T>* whichIsLessThen(double value)
  483. {
  484. element.setAttribute("less", Text(value));
  485. return this;
  486. }
  487. NumberValidationBuilder<T>* whichIsGreaterThen(double value)
  488. {
  489. element.setAttribute("greater", Text(value));
  490. return this;
  491. }
  492. NumberValidationBuilder<T>* withDefault(double value)
  493. {
  494. element.setAttribute(
  495. "default", JSONNumber(value).toString());
  496. return this;
  497. }
  498. NumberValidationBuilder<T>* withDefaultNull()
  499. {
  500. element.setAttribute("default", "null");
  501. return this;
  502. }
  503. NumberValidationBuilder<T>* whichCanBeNull()
  504. {
  505. element.setAttribute("nullable", "true");
  506. return this;
  507. }
  508. NumberValidationBuilder<T>* whichIsOptional()
  509. {
  510. element.setAttribute("optional", "true");
  511. return this;
  512. }
  513. T* finishNumber()
  514. {
  515. T* result = builder(element);
  516. delete this;
  517. return result;
  518. }
  519. };
  520. template<typename T> class BoolValidationBuilder
  521. {
  522. private:
  523. XML::Element element;
  524. std::function<T*(XML::Element& element)> builder;
  525. public:
  526. BoolValidationBuilder(
  527. std::function<T*(XML::Element& element)> builder)
  528. : element("<bool/>"),
  529. builder(builder)
  530. {}
  531. BoolValidationBuilder<T>* whichIs(bool value)
  532. {
  533. element.setAttribute("equals", value ? "true" : "false");
  534. return this;
  535. }
  536. BoolValidationBuilder<T>* withDefault(bool value)
  537. {
  538. element.setAttribute("default", JSONBool(value).toString());
  539. return this;
  540. }
  541. BoolValidationBuilder<T>* withDefaultNull()
  542. {
  543. element.setAttribute("default", "null");
  544. return this;
  545. }
  546. BoolValidationBuilder<T>* whichCanBeNull()
  547. {
  548. element.setAttribute("nullable", "true");
  549. return this;
  550. }
  551. BoolValidationBuilder<T>* whichIsOptional()
  552. {
  553. element.setAttribute("optional", "true");
  554. return this;
  555. }
  556. T* finishBool()
  557. {
  558. T* result = builder(element);
  559. delete this;
  560. return result;
  561. }
  562. };
  563. template<typename T> class ArrayValidationBuilder;
  564. template<typename T> class ObjectValidationBuilder
  565. {
  566. private:
  567. XML::Element element;
  568. std::function<T*(XML::Element& element)> builder;
  569. public:
  570. ObjectValidationBuilder(
  571. std::function<T*(XML::Element& element)> builder)
  572. : element("<object></object>"),
  573. builder(builder)
  574. {}
  575. ObjectValidationBuilder<T>* setObjectReferenceId(
  576. Text id)
  577. {
  578. element.setAttribute("id", id);
  579. return this;
  580. }
  581. NumberValidationBuilder<ObjectValidationBuilder<T>>*
  582. withRequiredNumber(Text name)
  583. {
  584. return new NumberValidationBuilder<
  585. ObjectValidationBuilder<T>>(
  586. [this, name](XML::Element& e) {
  587. if (!element.selectChildsByAttribute("name", name)
  588. .exists())
  589. {
  590. XML::Element* attr
  591. = new XML::Element("<value></value>");
  592. attr->setAttribute("name", name);
  593. element.addChild(attr);
  594. }
  595. element.selectChildsByAttribute("name", name)
  596. .addChild(e.dublicate());
  597. return this;
  598. });
  599. }
  600. StringValidationBuilder<ObjectValidationBuilder<T>>*
  601. withRequiredString(Text name)
  602. {
  603. return new StringValidationBuilder<
  604. ObjectValidationBuilder<T>>(
  605. [this, name](XML::Element& e) {
  606. if (!element.selectChildsByAttribute("name", name)
  607. .exists())
  608. {
  609. XML::Element* attr
  610. = new XML::Element("<value></value>");
  611. attr->setAttribute("name", name);
  612. element.addChild(attr);
  613. }
  614. element.selectChildsByAttribute("name", name)
  615. .addChild(e.dublicate());
  616. return this;
  617. });
  618. }
  619. BoolValidationBuilder<ObjectValidationBuilder<T>>*
  620. withRequiredBool(Text name)
  621. {
  622. return new BoolValidationBuilder<
  623. ObjectValidationBuilder<T>>(
  624. [this, name](XML::Element& e) {
  625. if (!element.selectChildsByAttribute("name", name)
  626. .exists())
  627. {
  628. XML::Element* attr
  629. = new XML::Element("<value></value>");
  630. attr->setAttribute("name", name);
  631. element.addChild(attr);
  632. }
  633. element.selectChildsByAttribute("name", name)
  634. .addChild(e.dublicate());
  635. return this;
  636. });
  637. }
  638. ArrayValidationBuilder<ObjectValidationBuilder<T>>*
  639. withRequiredArray(Text name)
  640. {
  641. return new ArrayValidationBuilder<
  642. ObjectValidationBuilder<T>>(
  643. [this, name](XML::Element& e) {
  644. if (!element.selectChildsByAttribute("name", name)
  645. .exists())
  646. {
  647. XML::Element* attr
  648. = new XML::Element("<value></value>");
  649. attr->setAttribute("name", name);
  650. element.addChild(attr);
  651. }
  652. element.selectChildsByAttribute("name", name)
  653. .addChild(e.dublicate());
  654. return this;
  655. });
  656. }
  657. ObjectValidationBuilder<ObjectValidationBuilder<T>>*
  658. withRequiredObject(Text name)
  659. {
  660. return new ObjectValidationBuilder<
  661. ObjectValidationBuilder<T>>(
  662. [this, name](XML::Element& e) {
  663. if (!element.selectChildsByAttribute("name", name)
  664. .exists())
  665. {
  666. XML::Element* attr
  667. = new XML::Element("<value></value>");
  668. attr->setAttribute("name", name);
  669. element.addChild(attr);
  670. }
  671. element.selectChildsByAttribute("name", name)
  672. .addChild(e.dublicate());
  673. return this;
  674. });
  675. }
  676. ObjectValidationBuilder<T>* withRequiredAttribute(
  677. Text name, JSONValidator* validator)
  678. {
  679. if (!element.selectChildsByAttribute("name", name).exists())
  680. {
  681. XML::Element* attr
  682. = new XML::Element("<value></value>");
  683. attr->setAttribute("name", name);
  684. element.addChild(attr);
  685. }
  686. element.selectChildsByAttribute("name", name)
  687. .addChild(validator->zConstraints()->dublicate());
  688. validator->release();
  689. return this;
  690. }
  691. ObjectValidationBuilder<T>* withDefault(JSONObject* obj)
  692. {
  693. element.setAttribute("default", obj->toString());
  694. obj->release();
  695. return this;
  696. }
  697. ObjectValidationBuilder<T>* withDefaultNull()
  698. {
  699. element.setAttribute("default", "null");
  700. return this;
  701. }
  702. ObjectValidationBuilder<T>* whichCanBeNull()
  703. {
  704. element.setAttribute("nullable", "true");
  705. return this;
  706. }
  707. ObjectValidationBuilder<T>* whichIsOptional()
  708. {
  709. element.setAttribute("optional", "true");
  710. return this;
  711. }
  712. ArrayValidationBuilder<T>* typeOfValuesSpecifiedByAttribute(
  713. Text valueName, Text attributeName)
  714. {
  715. if (!element.selectChildsByAttribute("name", valueName)
  716. .exists())
  717. {
  718. XML::Element* attr
  719. = new XML::Element("<value></value>");
  720. attr->setAttribute("name", valueName);
  721. element.addChild(attr);
  722. }
  723. element.selectChildsByAttribute("name", valueName)
  724. .setAttribute("typeSpecifiedBy", attributeName);
  725. return this;
  726. }
  727. ObjectValidationBuilder<T>* removeInvalidEntries()
  728. {
  729. element.setAttribute("removeInvalidEntries", "true");
  730. return this;
  731. }
  732. T* finishObject()
  733. {
  734. T* result = builder(element);
  735. delete this;
  736. return result;
  737. }
  738. };
  739. template<typename T> class ArrayValidationBuilder
  740. {
  741. private:
  742. XML::Element element;
  743. std::function<T*(XML::Element& element)> builder;
  744. public:
  745. ArrayValidationBuilder(
  746. std::function<T*(XML::Element& element)> builder)
  747. : element("<array></array>"),
  748. builder(builder)
  749. {}
  750. StringValidationBuilder<ArrayValidationBuilder<T>>*
  751. addAcceptedStringInArray()
  752. {
  753. return new StringValidationBuilder<
  754. ArrayValidationBuilder<T>>([this](XML::Element& e) {
  755. element.addChild(e.dublicate());
  756. return this;
  757. });
  758. }
  759. NumberValidationBuilder<ArrayValidationBuilder<T>>*
  760. addAcceptedNumberInArray()
  761. {
  762. return new NumberValidationBuilder<
  763. ArrayValidationBuilder<T>>([this](XML::Element& e) {
  764. element.addChild(e.dublicate());
  765. return this;
  766. });
  767. }
  768. BoolValidationBuilder<ArrayValidationBuilder<T>>*
  769. addAcceptedBooleanInArray()
  770. {
  771. return new BoolValidationBuilder<ArrayValidationBuilder<T>>(
  772. [this](XML::Element& e) {
  773. element.addChild(e.dublicate());
  774. return this;
  775. });
  776. }
  777. ObjectValidationBuilder<ArrayValidationBuilder<T>>*
  778. addAcceptedObjectInArray()
  779. {
  780. return new ObjectValidationBuilder<
  781. ArrayValidationBuilder<T>>([this](XML::Element& e) {
  782. element.addChild(e.dublicate());
  783. return this;
  784. });
  785. }
  786. ArrayValidationBuilder<ArrayValidationBuilder<T>>*
  787. addAcceptedArrayInArray()
  788. {
  789. return new ArrayValidationBuilder<
  790. ArrayValidationBuilder<T>>([this](XML::Element& e) {
  791. element.addChild(e.dublicate());
  792. return this;
  793. });
  794. }
  795. ArrayValidationBuilder<T>* addAcceptedTypeInArray(
  796. JSONValidator* validator)
  797. {
  798. element.addChild(validator->zConstraints()->dublicate());
  799. validator->release();
  800. return this;
  801. }
  802. ArrayValidationBuilder<T>* acceptNullsInArray()
  803. {
  804. element.setAttribute("nullsEnabled", "true");
  805. return this;
  806. }
  807. ArrayValidationBuilder<T>* withDefault(JSONArray* array)
  808. {
  809. element.setAttribute("default", array->toString());
  810. array->release();
  811. return this;
  812. }
  813. ArrayValidationBuilder<T>* withDefaultNull()
  814. {
  815. element.setAttribute("default", "null");
  816. return this;
  817. }
  818. ArrayValidationBuilder<T>* whichCanBeNull()
  819. {
  820. element.setAttribute("nullable", "true");
  821. return this;
  822. }
  823. ArrayValidationBuilder<T>* whichIsOptional()
  824. {
  825. element.setAttribute("optional", "true");
  826. return this;
  827. }
  828. ArrayValidationBuilder<T>* typeSpecifiedByAttribute(Text name)
  829. {
  830. element.setAttribute("typeSpecifiedBy", name);
  831. return this;
  832. }
  833. ArrayValidationBuilder<T>* removeInvalidEntries()
  834. {
  835. element.setAttribute("removeInvalidEntries", "true");
  836. return this;
  837. }
  838. T* finishArray()
  839. {
  840. T* result = builder(element);
  841. delete this;
  842. return result;
  843. }
  844. };
  845. } // namespace Validator
  846. } // namespace JSON
  847. } // namespace Framework