JSON.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #pragma once
  2. #include <functional>
  3. #include "AbstractElement.h"
  4. #include "Array.h"
  5. #include "ReferenceCounter.h"
  6. #include "Text.h"
  7. namespace Framework
  8. {
  9. namespace JSON
  10. {
  11. class JSONArray;
  12. class JSONObject;
  13. class JSONBool;
  14. class JSONNumber;
  15. class JSONString;
  16. class JSONValue : public virtual ReferenceCounter,
  17. public virtual AbstractElement
  18. {
  19. private:
  20. AbstractType type;
  21. protected:
  22. __declspec(dllexport) JSONValue(AbstractType type);
  23. public:
  24. __declspec(dllexport) JSONValue();
  25. __declspec(dllexport) virtual ~JSONValue();
  26. __declspec(dllexport) AbstractType getType() const override;
  27. __declspec(dllexport) virtual Text toString() const override;
  28. __declspec(dllexport) virtual JSONValue* clone() const;
  29. __declspec(dllexport) JSONBool* asBool() const;
  30. __declspec(dllexport) JSONNumber* asNumber() const;
  31. __declspec(dllexport) JSONString* asString() const;
  32. __declspec(dllexport) JSONArray* asArray() const;
  33. __declspec(dllexport) JSONObject* asObject() const;
  34. __declspec(dllexport) const AbstractBool*
  35. asAbstractBool() const override;
  36. __declspec(dllexport) const AbstractNumber*
  37. asAbstractNumber() const override;
  38. __declspec(dllexport) const AbstractString*
  39. asAbstractString() const override;
  40. __declspec(dllexport) const AbstractArray*
  41. asAbstractArray() const override;
  42. __declspec(dllexport) const AbstractObject*
  43. asAbstractObject() const override;
  44. };
  45. #ifdef WIN32
  46. # pragma warning(push)
  47. # pragma warning(disable : 4250)
  48. #endif
  49. class JSONBool : public AbstractBool,
  50. public JSONValue
  51. {
  52. private:
  53. bool b;
  54. public:
  55. __declspec(dllexport) JSONBool(bool b);
  56. __declspec(dllexport) bool getBool() const override;
  57. __declspec(dllexport) Text toString() const override;
  58. __declspec(dllexport) JSONValue* clone() const override;
  59. };
  60. class JSONNumber : public JSONValue,
  61. public AbstractNumber
  62. {
  63. private:
  64. double number;
  65. public:
  66. __declspec(dllexport) JSONNumber(double num);
  67. __declspec(dllexport) double getNumber() const override;
  68. __declspec(dllexport) Text toString() const override;
  69. __declspec(dllexport) JSONValue* clone() const override;
  70. };
  71. class JSONString : public JSONValue,
  72. public AbstractString
  73. {
  74. private:
  75. Text string;
  76. public:
  77. __declspec(dllexport) JSONString(Text string);
  78. __declspec(dllexport) Text getString() const override;
  79. __declspec(dllexport) Text toString() const override;
  80. __declspec(dllexport) JSONValue* clone() const override;
  81. };
  82. class JSONArray : public JSONValue,
  83. public AbstractArray
  84. {
  85. private:
  86. RCArray<JSONValue>* array;
  87. public:
  88. __declspec(dllexport) JSONArray();
  89. __declspec(dllexport) JSONArray(Text string);
  90. __declspec(dllexport) JSONArray(const JSONArray& arr);
  91. __declspec(dllexport) ~JSONArray();
  92. __declspec(dllexport) JSONArray& operator=(const JSONArray& arr);
  93. __declspec(dllexport) void addValue(JSONValue* value);
  94. __declspec(dllexport) void setValue(int i, JSONValue* value);
  95. __declspec(dllexport) void removeValue(int i);
  96. __declspec(dllexport) JSONValue* getValue(int i) const;
  97. __declspec(dllexport) JSONValue* zValue(int i) const;
  98. __declspec(dllexport) AbstractElement* zAbstractValue(
  99. int i) const override;
  100. __declspec(dllexport) int getLength() const override;
  101. __declspec(dllexport) bool isValueOfType(
  102. int i, AbstractType type) const;
  103. //! Returns an iterator.
  104. //! Use ++ to iterate through the list
  105. __declspec(dllexport) ArrayIterator<JSONValue*> begin() const;
  106. __declspec(dllexport) ArrayIterator<JSONValue*> end() const;
  107. template<class T>
  108. RCArray<T>* toRCArray(std::function<T*(JSONValue&)> map) const
  109. {
  110. return toRCArray([](JSONValue& v) { return 1; }, map);
  111. }
  112. template<class T>
  113. RCArray<T>* toRCArray(std::function<bool(JSONValue&)> filter,
  114. std::function<T*(JSONValue&)> map) const
  115. {
  116. RCArray<T>* result = new RCArray<T>();
  117. for (auto v : *array)
  118. {
  119. if (filter(*v))
  120. {
  121. result->add(map(*v));
  122. }
  123. }
  124. return result;
  125. }
  126. template<class T>
  127. Array<T>* toArray(std::function<T(JSONValue&)> map) const
  128. {
  129. return toArray([](JSONValue& v) { return 1; }, map);
  130. ;
  131. }
  132. template<class T>
  133. Array<T>* toArray(std::function<bool(JSONValue&)> filter,
  134. std::function<T(JSONValue&)> map) const
  135. {
  136. Array<T>* result = new Array<T>();
  137. for (auto v : *array)
  138. {
  139. if (filter(*v))
  140. {
  141. result->add(map(*v));
  142. }
  143. }
  144. return result;
  145. }
  146. __declspec(dllexport) Text toString() const override;
  147. __declspec(dllexport) JSONValue* clone() const override;
  148. template<class T> static JSONArray* fromRCArray(
  149. Framework::RCArray<T>& arr, std::function<JSONValue*(T&)> map)
  150. {
  151. JSONArray* array = new JSONArray();
  152. for (T* v : arr)
  153. {
  154. array->addValue(map(*v));
  155. }
  156. return array;
  157. }
  158. template<class T> static JSONArray* fromArray(
  159. Framework::Array<T>& arr, std::function<JSONValue*(T)> map)
  160. {
  161. JSONArray* array = new JSONArray();
  162. for (T v : arr)
  163. {
  164. array->addValue(map(v));
  165. }
  166. return array;
  167. }
  168. };
  169. class JSONObject : public JSONValue,
  170. public AbstractObject
  171. {
  172. private:
  173. Array<Text>* fields;
  174. RCArray<JSONValue>* values;
  175. public:
  176. __declspec(dllexport) JSONObject();
  177. __declspec(dllexport) JSONObject(Text string);
  178. __declspec(dllexport) JSONObject(const JSONObject& obj);
  179. __declspec(dllexport) ~JSONObject();
  180. __declspec(dllexport) JSONObject& operator=(const JSONObject& obj);
  181. __declspec(dllexport) bool addValue(Text field, JSONValue* value);
  182. __declspec(dllexport) bool removeValue(Text field);
  183. __declspec(dllexport) bool hasValue(Text field) const override;
  184. __declspec(dllexport) JSONValue* getValue(Text field) const;
  185. __declspec(dllexport) JSONValue* zValue(Text field) const;
  186. __declspec(dllexport) AbstractElement* zAbstractValue(
  187. Text field) const override;
  188. __declspec(dllexport) ArrayIterator<Text> getFields();
  189. __declspec(dllexport) ArrayIterator<JSONValue*> getValues();
  190. __declspec(dllexport) Text getFieldKey(int i) const override;
  191. __declspec(dllexport) AbstractElement* zAbstractValue(
  192. int i) const override;
  193. __declspec(dllexport) int getFieldCount() const override;
  194. __declspec(dllexport) bool isValueOfType(
  195. Text field, AbstractType type) const;
  196. template<class T> T* parseTo(T* initialState,
  197. std::function<void(
  198. T* obj, Text fieldName, JSONValue& fieldValue)> parser)
  199. const
  200. {
  201. auto fieldsI = fields->begin();
  202. auto valuesI = values->begin();
  203. while (fieldsI && valuesI)
  204. {
  205. parser(initialState, fieldsI, *(JSONValue*)valuesI);
  206. fieldsI++;
  207. valuesI++;
  208. }
  209. return initialState;
  210. }
  211. __declspec(dllexport) Text toString() const override;
  212. __declspec(dllexport) JSONValue* clone() const override;
  213. };
  214. #ifdef WIN32
  215. # pragma warning(pop)
  216. #endif
  217. __declspec(dllexport) JSONValue* loadJSONFromFile(Text path);
  218. namespace Parser
  219. {
  220. __declspec(dllexport) int findObjectEndInArray(const char* str);
  221. __declspec(dllexport) Text removeWhitespace(const char* str);
  222. __declspec(dllexport) JSONValue* getValue(const char* str);
  223. __declspec(dllexport) int findFieldEndInObject(const char* str);
  224. __declspec(dllexport) int findValueEndInObject(const char* str);
  225. }; // namespace Parser
  226. } // namespace JSON
  227. } // namespace Framework