JSON.h 9.3 KB

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