JSONEncoder.tlh 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. #pragma once
  2. #include <string>
  3. using namespace std::literals;
  4. #include "BSTTree.hpp"
  5. template <typename T>
  6. class JSONEncoder
  7. {
  8. using CV_String = const std::string_view;
  9. using tKeyType = const std::string_view &;
  10. public:
  11. JSONEncoder ();
  12. virtual ~JSONEncoder ();
  13. public:
  14. void Set (tKeyType Key, const T & Value);
  15. void Set (tKeyType Key, LPCSTR Value);
  16. void Set (tKeyType Key, CV_String Value);
  17. void Set (tKeyType Key, __int64 Value);
  18. void Set (tKeyType Key, unsigned __int64 Value);
  19. void Set (tKeyType Key, int Value);
  20. void Set (tKeyType Key, unsigned int Value);
  21. void Set (tKeyType Key, unsigned long Value);
  22. void Set (tKeyType Key, bool Value);
  23. void Set (tKeyType Key, float Value);
  24. void Set (tKeyType Key, double Value);
  25. void Set (tKeyType Key, const JSONEncoder & Element); // 作为嵌入对象
  26. public:
  27. template <typename TV>
  28. JSONEncoder & Set (tKeyType Key, const std::vector <TV> & arValue);
  29. template <typename TV>
  30. JSONEncoder & Set (tKeyType Key, const std::list <TV> & arValue);
  31. template <typename TV>
  32. JSONEncoder & Set (tKeyType Key, const std::initializer_list <TV> & arValue);
  33. public:
  34. template <typename TV>
  35. JSONEncoder & Encode (tKeyType Key, const TV & value)
  36. {
  37. Set (Key, value);
  38. return (*this);
  39. }
  40. template <typename TV>
  41. JSONEncoder & Encode (tKeyType Key, const std::initializer_list <TV> & value)
  42. {
  43. Set (Key, value);
  44. return (*this);
  45. }
  46. public:
  47. bool Recode (CV_String String);
  48. T ToString () const;
  49. operator T () const { return ToString (); }
  50. // 我没有拷贝构造函数
  51. private:
  52. JSONEncoder (const JSONEncoder &);
  53. JSONEncoder & operator = (JSONEncoder & Value);
  54. protected:
  55. ECOM::Utility::BSTTree * m_JsonRoot;
  56. protected:
  57. class Proxy
  58. {
  59. public:
  60. inline Proxy (JSONEncoder & Encoder, tKeyType Key) :
  61. m_Encoder (Encoder),
  62. m_Key (Key)
  63. {
  64. }
  65. inline Proxy & operator = (const T & Value)
  66. {
  67. m_Encoder.Set (m_Key, Value);
  68. return (*this);
  69. }
  70. inline Proxy & operator = (LPCSTR Value)
  71. {
  72. m_Encoder.Set (m_Key, Value);
  73. return (*this);
  74. }
  75. inline Proxy & operator = (CV_String Value)
  76. {
  77. m_Encoder.Set (m_Key, Value);
  78. return (*this);
  79. }
  80. inline Proxy & operator = (__int64 Value)
  81. {
  82. m_Encoder.Set (m_Key, Value);
  83. return (*this);
  84. }
  85. inline Proxy & operator = (unsigned __int64 Value)
  86. {
  87. m_Encoder.Set (m_Key, Value);
  88. return (*this);
  89. }
  90. inline Proxy & operator = (int Value)
  91. {
  92. m_Encoder.Set (m_Key, Value);
  93. return (*this);
  94. }
  95. inline Proxy & operator = (unsigned int Value)
  96. {
  97. m_Encoder.Set (m_Key, Value);
  98. return (*this);
  99. }
  100. inline Proxy & operator = (unsigned long Value)
  101. {
  102. m_Encoder.Set (m_Key, Value);
  103. return (*this);
  104. }
  105. inline Proxy & operator = (bool Value)
  106. {
  107. m_Encoder.Set (m_Key, Value);
  108. return (*this);
  109. }
  110. inline Proxy & operator = (float Value)
  111. {
  112. m_Encoder.Set (m_Key, Value);
  113. return (*this);
  114. }
  115. inline Proxy & operator = (double Value)
  116. {
  117. m_Encoder.Set (m_Key, Value);
  118. return (*this);
  119. }
  120. template <typename TV>
  121. inline Proxy & operator = (const std::vector <TV> & arValue)
  122. {
  123. m_Encoder.Set (m_Key, arValue);
  124. return (*this);
  125. }
  126. template <typename TV>
  127. inline Proxy & operator = (const std::list <TV> & arValue)
  128. {
  129. m_Encoder.Set (m_Key, arValue);
  130. return (*this);
  131. }
  132. template <typename TV>
  133. inline Proxy & operator = (const std::initializer_list <TV> & arValue)
  134. {
  135. m_Encoder.Set (m_Key, arValue);
  136. return (*this);
  137. }
  138. inline Proxy & operator = (JSONEncoder & Value)
  139. {
  140. m_Encoder.Set (m_Key, Value);
  141. return (*this);
  142. }
  143. protected:
  144. tKeyType m_Key;
  145. JSONEncoder & m_Encoder;
  146. };
  147. public:
  148. Proxy operator [] (CV_String & Key)
  149. {
  150. return Proxy (*this, Key);
  151. }
  152. Proxy operator () (CV_String & Key)
  153. {
  154. return Proxy (*this, Key);
  155. }
  156. };
  157. template <typename T>
  158. JSONEncoder <T>::JSONEncoder ()
  159. {
  160. m_JsonRoot = new ECOM::Utility::BSTTree;
  161. }
  162. template <typename T>
  163. JSONEncoder <T>::~JSONEncoder ()
  164. {
  165. delete m_JsonRoot;
  166. }
  167. template <typename T>
  168. void JSONEncoder <T>::Set (tKeyType Key, const T & Value)
  169. {
  170. m_JsonRoot->Put (Key, std::string_view {Value});
  171. }
  172. template <typename T>
  173. void JSONEncoder <T>::Set (tKeyType Key, CV_String Value)
  174. {
  175. m_JsonRoot->Put (Key, Value);
  176. }
  177. template <typename T>
  178. void JSONEncoder <T>::Set (tKeyType Key, LPCTSTR Value)
  179. {
  180. m_JsonRoot->Put (Key, Value);
  181. }
  182. template <typename T>
  183. void JSONEncoder <T>::Set (tKeyType Key, __int64 Value)
  184. {
  185. m_JsonRoot->Put (Key, Value);
  186. }
  187. template <typename T>
  188. void JSONEncoder <T>::Set (tKeyType Key, unsigned __int64 Value)
  189. {
  190. m_JsonRoot->Put (Key, Value);
  191. }
  192. template <typename T>
  193. void JSONEncoder <T>::Set (tKeyType Key, int Value)
  194. {
  195. m_JsonRoot->Put (Key, Value);
  196. }
  197. template <typename T>
  198. void JSONEncoder <T>::Set (tKeyType Key, unsigned int Value)
  199. {
  200. m_JsonRoot->Put (Key, Value);
  201. }
  202. template <typename T>
  203. void JSONEncoder <T>::Set (tKeyType Key, unsigned long Value)
  204. {
  205. m_JsonRoot->Put (Key, Value);
  206. }
  207. template <typename T>
  208. void JSONEncoder <T>::Set (tKeyType Key, float Value)
  209. {
  210. m_JsonRoot->Put (Key, Value);
  211. }
  212. template <typename T>
  213. void JSONEncoder <T>::Set (tKeyType Key, double Value)
  214. {
  215. m_JsonRoot->Put (Key, Value);
  216. }
  217. template <typename T>
  218. void JSONEncoder <T>::Set (tKeyType Key, bool Value)
  219. {
  220. m_JsonRoot->Put (Key, Value);
  221. }
  222. template <typename T>
  223. void JSONEncoder <T>::Set (tKeyType Key, const JSONEncoder & child)
  224. {
  225. const auto & tree { *child.m_JsonRoot };
  226. m_JsonRoot->Add (Key, tree);
  227. }
  228. template <typename T>
  229. bool JSONEncoder <T>::Recode (CV_String String)
  230. {
  231. delete m_JsonRoot;
  232. m_JsonRoot = new ECOM::Utility::BSTTree (std::move (ECOM::Utility::BSTJSON::FromString (String)));
  233. return ! m_JsonRoot->IsEmpty ();
  234. }
  235. template <typename T>
  236. T JSONEncoder <T>::ToString () const
  237. {
  238. return T { ECOM::Utility::BSTJSON::ToString (*m_JsonRoot)};
  239. }
  240. template <typename T>
  241. template <typename TV>
  242. JSONEncoder <T> & JSONEncoder <T>::Set (tKeyType Key, const std::vector <TV> & arValue)
  243. {
  244. T emp;
  245. auto CHArray = m_JsonRoot->AddChildArray (Key);
  246. for (const auto & Item : arValue)
  247. {
  248. ECOM::Utility::BSTTree CH;
  249. CH.Put (emp, Item);
  250. CHArray.Add (std::move (CH));
  251. }
  252. return (*this);
  253. }
  254. template <typename T>
  255. template <typename TV>
  256. JSONEncoder <T> & JSONEncoder <T>::Set (tKeyType Key, const std::list <TV> & arValue)
  257. {
  258. T emp;
  259. auto CHArray = m_JsonRoot->AddChildArray (Key);
  260. for (const auto & Item : arValue)
  261. {
  262. ECOM::Utility::BSTTree CH;
  263. CH.Put (emp, Item);
  264. CHArray.Add (std::move (CH));
  265. }
  266. return (*this);
  267. }
  268. template <typename T>
  269. template <typename TV>
  270. JSONEncoder <T> & JSONEncoder <T>::Set (tKeyType Key, const std::initializer_list <TV> & arValue)
  271. {
  272. T emp;
  273. auto CHArray = m_JsonRoot->AddChildArray (Key);
  274. for (const auto & Item : arValue)
  275. {
  276. ECOM::Utility::BSTTree CH;
  277. CH.Put (emp, Item);
  278. CHArray.Add (std::move (CH));
  279. }
  280. return (*this);
  281. }