JSON.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #pragma once
  2. #include "Text.h"
  3. #include "Array.h"
  4. #include "ReferenceCounter.h"
  5. #include <functional>
  6. namespace Framework
  7. {
  8. namespace JSON
  9. {
  10. enum class JSONType
  11. {
  12. NULL_,
  13. BOOLEAN,
  14. NUMBER,
  15. STRING,
  16. ARRAY,
  17. OBJECT
  18. };
  19. class JSONArray;
  20. class JSONObject;
  21. class JSONBool;
  22. class JSONNumber;
  23. class JSONString;
  24. class JSONValue : public virtual ReferenceCounter
  25. {
  26. private:
  27. JSONType type;
  28. protected:
  29. __declspec(dllexport) JSONValue(JSONType type);
  30. public:
  31. __declspec(dllexport) JSONValue();
  32. __declspec(dllexport) virtual ~JSONValue();
  33. __declspec(dllexport) JSONType getType() const;
  34. __declspec(dllexport) virtual Text toString() const;
  35. __declspec(dllexport) JSONBool* asBool() const;
  36. __declspec(dllexport) JSONNumber* asNumber() const;
  37. __declspec(dllexport) JSONString* asString() const;
  38. __declspec(dllexport) JSONArray* asArray() const;
  39. __declspec(dllexport) JSONObject* asObject() const;
  40. };
  41. class JSONBool : public JSONValue
  42. {
  43. private:
  44. bool b;
  45. public:
  46. __declspec(dllexport) JSONBool(bool b);
  47. __declspec(dllexport) bool getBool() const;
  48. __declspec(dllexport) Text toString() const override;
  49. };
  50. class JSONNumber : public JSONValue
  51. {
  52. private:
  53. double number;
  54. public:
  55. __declspec(dllexport) JSONNumber(double num);
  56. __declspec(dllexport) double getNumber() const;
  57. __declspec(dllexport) Text toString() const override;
  58. };
  59. class JSONString : public JSONValue
  60. {
  61. private:
  62. Text string;
  63. public:
  64. __declspec(dllexport) JSONString(Text string);
  65. __declspec(dllexport) Text getString() const;
  66. __declspec(dllexport) Text toString() const override;
  67. };
  68. class JSONArray : public JSONValue
  69. {
  70. private:
  71. RCArray< JSONValue >* array;
  72. public:
  73. __declspec(dllexport) JSONArray();
  74. __declspec(dllexport) JSONArray(Text string);
  75. __declspec(dllexport) JSONArray(const JSONArray& arr);
  76. __declspec(dllexport) ~JSONArray();
  77. __declspec(dllexport) JSONArray& operator=(const JSONArray& arr);
  78. __declspec(dllexport) void addValue(JSONValue* value);
  79. __declspec(dllexport) JSONValue* getValue(int i) const;
  80. __declspec(dllexport) int getLength() const;
  81. __declspec(dllexport) bool isValueOfType(int i, JSONType type) const;
  82. template<class T>
  83. RCArray<T>* toRCArray(std::function<T* (JSONValue&)> map) const
  84. {
  85. return toRCArray([](JSONValue& v) { return 1; }, map);
  86. }
  87. template<class T>
  88. RCArray<T>* toRCArray(std::function<bool(JSONValue&)> filter, std::function<T* (JSONValue&)> map) const
  89. {
  90. RCArray<T>* result = new RCArray<T>();
  91. for (auto v : *array)
  92. {
  93. if (filter(*v))
  94. {
  95. result->add(map(*v));
  96. }
  97. }
  98. return result;
  99. }
  100. template<class T>
  101. Array<T>* toArray(std::function<T(JSONValue&)> map) const
  102. {
  103. return toArray([](JSONValue& v) { return 1; }, map);;
  104. }
  105. template<class T>
  106. Array<T>* toArray(std::function<bool(JSONValue&)> filter, std::function<T(JSONValue&)> map) const
  107. {
  108. Array<T>* result = new Array<T>();
  109. for (auto v : *array)
  110. {
  111. if (filter(*v))
  112. {
  113. result->add(map(*v));
  114. }
  115. }
  116. return result;
  117. }
  118. __declspec(dllexport) Text toString() const override;
  119. template<class T>
  120. static JSONArray* fromRCArray(Framework::RCArray<T>& arr, std::function<JSONValue* (T&)> map)
  121. {
  122. JSONArray* array = new JSONArray();
  123. for (T* v : arr)
  124. {
  125. array->addValue(map(*v));
  126. }
  127. return array;
  128. }
  129. template<class T>
  130. static JSONArray* fromArray(Framework::Array<T>& arr, std::function<JSONValue* (T)> map)
  131. {
  132. JSONArray* array = new JSONArray();
  133. for (T v : arr)
  134. {
  135. array->addValue(map(v));
  136. }
  137. return array;
  138. }
  139. };
  140. class JSONObject : public JSONValue
  141. {
  142. private:
  143. Array< Text >* fields;
  144. RCArray< JSONValue >* values;
  145. public:
  146. __declspec(dllexport) JSONObject();
  147. __declspec(dllexport) JSONObject(Text string);
  148. __declspec(dllexport) JSONObject(const JSONObject& obj);
  149. __declspec(dllexport) ~JSONObject();
  150. __declspec(dllexport) JSONObject& operator=(const JSONObject& obj);
  151. __declspec(dllexport) bool addValue(Text field, JSONValue* value);
  152. __declspec(dllexport) bool removeValue(Text field);
  153. __declspec(dllexport) bool hasValue(Text field);
  154. __declspec(dllexport) JSONValue* getValue(Text field);
  155. __declspec(dllexport) Iterator< Text > getFields();
  156. __declspec(dllexport) Iterator< JSONValue* > getValues();
  157. __declspec(dllexport) int getFieldCount() const;
  158. __declspec(dllexport) bool isValueOfType(Text field, JSONType type) const;
  159. template<class T>
  160. T* parseTo(T* initialState, std::function<void(T* obj, Text fieldName, JSONValue& fieldValue)> parser) const
  161. {
  162. auto fieldsI = fields->begin();
  163. auto valuesI = values->begin();
  164. while (fieldsI && valuesI)
  165. {
  166. parser(initialState, fieldsI, *(JSONValue*)valuesI);
  167. fieldsI++;
  168. valuesI++;
  169. }
  170. return initialState;
  171. }
  172. __declspec(dllexport) Text toString() const override;
  173. };
  174. __declspec(dllexport) JSONValue* loadJSONFromFile(Text path);
  175. namespace Parser
  176. {
  177. __declspec(dllexport) int findObjectEndInArray(const char* str);
  178. __declspec(dllexport) Text removeWhitespace(const char* str);
  179. __declspec(dllexport) JSONValue* getValue(const char* str);
  180. __declspec(dllexport) int findFieldEndInObject(const char* str);
  181. __declspec(dllexport) int findValueEndInObject(const char* str);
  182. };
  183. }
  184. }