FastJsonEncoder.tli 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. #pragma once
  2. #include "Iterator.tlh"
  3. #pragma warning (disable:4267)
  4. // warning C4267 : “参数”: 从“size_t”转换到“int”,可能丢失数据
  5. #include "WString.hpp"
  6. #include "FastJsonEncoder.tlh"
  7. //-----------------------------------------------------------------------------
  8. // FastJsonEncoder
  9. //-----------------------------------------------------------------------------
  10. template <typename T>
  11. FastJsonEncoder <T>::FastJsonEncoder ()
  12. {
  13. }
  14. template <typename T>
  15. FastJsonEncoder <T>::FastJsonEncoder (FastJsonEncoder && Encoder)
  16. {
  17. Swap (Encoder);
  18. }
  19. template <typename T>
  20. FastJsonEncoder <T>::FastJsonEncoder (const FastJsonEncoder & Encoder)
  21. {
  22. Encoder.CopyTo (*this);
  23. }
  24. template <typename T>
  25. FastJsonEncoder <T>::~FastJsonEncoder ()
  26. {
  27. }
  28. // 以下代码处理转义字符
  29. /*
  30. else if (*b == Ch('\b')) result += Ch('\\'), result += Ch('b');
  31. else if (*b == Ch('\f')) result += Ch('\\'), result += Ch('f');
  32. else if (*b == Ch('\n')) result += Ch('\\'), result += Ch('n');
  33. else if (*b == Ch('\r')) result += Ch('\\'), result += Ch('r');
  34. else if (*b == Ch('\t')) result += Ch('\\'), result += Ch('t');
  35. else if (*b == Ch('/')) result += Ch('\\'), result += Ch('/');
  36. else if (*b == Ch('"')) result += Ch('\\'), result += Ch('"');
  37. else if (*b == Ch('\\')) result += Ch('\\'), result += Ch('\\');
  38. */
  39. static inline DString ESC_JSON (PCTSTR strValue, int length)
  40. {
  41. const char * pc = strValue;
  42. DString toValue;
  43. char * pt = toValue.GetBuffer (length + length); // 预分配双倍的长度
  44. char * ps = pt;
  45. for (; *pc; pc++)
  46. {
  47. char ch = *pc;
  48. if (ch == '\\') *pt++ = '\\', *pt++ = '\\';
  49. else if (ch == '"') *pt++ = '\\', *pt++ = '"';
  50. else if (ch == '/') *pt++ = '\\', *pt++ = '/';
  51. else if (ch == '\b') *pt++ = '\\', *pt++ = 'b';
  52. else if (ch == '\f') *pt++ = '\\', *pt++ = 'f';
  53. else if (ch == '\n') *pt++ = '\\', *pt++ = 'n';
  54. else if (ch == '\r') *pt++ = '\\', *pt++ = 'r';
  55. else if (ch == '\t') *pt++ = '\\', *pt++ = 't';
  56. else *pt++ = ch;
  57. }
  58. int len = (int) (pt - ps);
  59. toValue.ReleaseBuffer (len);
  60. return toValue;
  61. }
  62. static inline WString ESC_JSON (PCWSTR strValue, int length)
  63. {
  64. const wchar_t * pc = strValue;
  65. WString toValue;
  66. wchar_t * pt = toValue.GetBuffer (length + length); // 预分配双倍的长度
  67. wchar_t * ps = pt;
  68. for (; *pc; pc++)
  69. {
  70. wchar_t ch = *pc;
  71. if (ch == '\\') *pt++ = '\\', *pt++ = '\\';
  72. else if (ch == '"') *pt++ = '\\', *pt++ = '"';
  73. else if (ch == '/') *pt++ = '\\', *pt++ = '/';
  74. else if (ch == '\b') *pt++ = '\\', *pt++ = 'b';
  75. else if (ch == '\f') *pt++ = '\\', *pt++ = 'f';
  76. else if (ch == '\n') *pt++ = '\\', *pt++ = 'n';
  77. else if (ch == '\r') *pt++ = '\\', *pt++ = 'r';
  78. else if (ch == '\t') *pt++ = '\\', *pt++ = 't';
  79. else *pt++ = ch;
  80. }
  81. int len = (int) (pt - ps);
  82. toValue.ReleaseBuffer (len);
  83. return toValue;
  84. }
  85. template <typename T>
  86. void FastJsonEncoder <T>::Set (tPCTSTR Key, LPCSTR strValue)
  87. {
  88. static_assert (std::is_base_of <DString, T>::value, "T must be derived from DString");
  89. auto Value = ESC_JSON (strValue, int (strlen (strValue)));
  90. T Msg; Msg.GetBuffer (64);
  91. Msg.Append ('\r').Append ('\n').Append (' ').Append (' ');
  92. Msg.Append ('"').Append (Key).Append ('"');
  93. Msg.Append (' ').Append (':').Append (' ');
  94. Msg.Append ('"').Append (Value).Append ('"');
  95. m_arMessage.push_back (Msg);
  96. }
  97. template <typename T>
  98. void FastJsonEncoder <T>::Set (tPCTSTR Key, PCWSTR strValue)
  99. {
  100. static_assert (std::is_base_of <WString, T>::value, "T must be derived from WString");
  101. auto Value = ESC_JSON (strValue, int (wcslen (strValue)));
  102. T Msg; Msg.GetBuffer (64);
  103. Msg.Append ('\r').Append ('\n').Append (' ').Append (' ');
  104. Msg.Append ('"').Append (Key).Append ('"');
  105. Msg.Append (' ').Append (':').Append (' ');
  106. Msg.Append ('"').Append (Value).Append ('"');
  107. m_arMessage.push_back (Msg);
  108. }
  109. template <typename T>
  110. void FastJsonEncoder <T>::Set (tPCTSTR Key, const DString & strTmp)
  111. {
  112. // static_assert (std::is_base_of <DString, T>::value, "T must be derived from DString");
  113. auto Value = ESC_JSON (strTmp, strTmp.GetLength ());
  114. T Msg; Msg.GetBuffer (64);
  115. Msg.Append ('\r').Append ('\n').Append (' ').Append (' ');
  116. Msg.Append ('"').Append (Key).Append ('"');
  117. Msg.Append (' ').Append (':').Append (' ');
  118. Msg.Append ('"').Append (Value).Append ('"');
  119. m_arMessage.push_back (Msg);
  120. }
  121. template <typename T>
  122. void FastJsonEncoder <T>::Set (tPCTSTR Key, const WString & strTmp)
  123. {
  124. static_assert (std::is_base_of <WString, T>::value, "T must be derived from WString");
  125. auto Value = ESC_JSON (strTmp, strTmp.GetLength ());
  126. T Msg; Msg.GetBuffer (64);
  127. Msg.Append ('\r').Append ('\n').Append (' ').Append (' ');
  128. Msg.Append ('"').Append (Key).Append ('"');
  129. Msg.Append (' ').Append (':').Append (' ');
  130. Msg.Append ('"').Append (Value).Append ('"');
  131. m_arMessage.push_back (Msg);
  132. }
  133. template <typename T>
  134. void FastJsonEncoder <T>::Set (tPCTSTR Key, UINT_PTR Value)
  135. {
  136. T Msg; Msg.GetBuffer (64);
  137. Msg.Append ('\r').Append ('\n').Append (' ').Append (' ');
  138. Msg.Append ('"').Append (Key).Append ('"');
  139. Msg.Append (' ').Append (':').Append (' ');
  140. Msg.Append (T::From (Value));
  141. m_arMessage.push_back (Msg);
  142. }
  143. template <typename T>
  144. void FastJsonEncoder <T>::Set (tPCTSTR Key, __int64 Value)
  145. {
  146. T Msg; Msg.GetBuffer (64);
  147. Msg.Append ('\r').Append ('\n').Append (' ').Append (' ');
  148. Msg.Append ('"').Append (Key).Append ('"');
  149. Msg.Append (' ').Append (':').Append (' ');
  150. Msg.Append (T::From (Value));
  151. m_arMessage.push_back (Msg);
  152. }
  153. template <typename T>
  154. void FastJsonEncoder <T>::Set (tPCTSTR Key, DWORD Value)
  155. {
  156. T Msg; Msg.GetBuffer (64);
  157. Msg.Append ('\r').Append ('\n').Append (' ').Append (' ');
  158. Msg.Append ('"').Append (Key).Append ('"');
  159. Msg.Append (' ').Append (':').Append (' ');
  160. Msg.Append (T::From (Value));
  161. m_arMessage.push_back (Msg);
  162. }
  163. template <typename T>
  164. void FastJsonEncoder <T>::Set (tPCTSTR Key, int Value)
  165. {
  166. T Msg; Msg.GetBuffer (64);
  167. Msg.Append ('\r').Append ('\n').Append (' ').Append (' ');
  168. Msg.Append ('"').Append (Key).Append ('"');
  169. Msg.Append (' ').Append (':').Append (' ');
  170. Msg.Append (T::From (Value));
  171. m_arMessage.push_back (Msg);
  172. }
  173. template <typename T>
  174. void FastJsonEncoder <T>::Set (tPCTSTR Key, bool Value)
  175. {
  176. T Msg; Msg.GetBuffer (64);
  177. Msg.Append ('\r').Append ('\n').Append (' ').Append (' ');
  178. Msg.Append ('"').Append (Key).Append ('"');
  179. Msg.Append (' ').Append (':').Append (' ');
  180. Msg.Append (T::From (Value));
  181. m_arMessage.push_back (Msg);
  182. }
  183. template <typename T>
  184. void FastJsonEncoder <T>::Set (tPCTSTR Key, void * ptr)
  185. {
  186. return Set (Key, (UINT_PTR) ptr);
  187. }
  188. template <typename T>
  189. void FastJsonEncoder <T>::Set (tPCTSTR Key, float Value)
  190. {
  191. T Msg; Msg.GetBuffer (64);
  192. Msg.Append ('\r').Append ('\n').Append (' ').Append (' ');
  193. Msg.Append ('"').Append (Key).Append ('"');
  194. Msg.Append (' ').Append (':').Append (' ');
  195. Msg.Append (T::From (Value));
  196. m_arMessage.push_back (Msg);
  197. }
  198. template <typename T>
  199. void FastJsonEncoder <T>::Set (tPCTSTR Key, double Value)
  200. {
  201. T Msg; Msg.GetBuffer (64);
  202. Msg.Append ('\r').Append ('\n').Append (' ').Append (' ');
  203. Msg.Append ('"').Append (Key).Append ('"');
  204. Msg.Append (' ').Append (':').Append (' ');
  205. Msg.Append (T::From (Value));
  206. m_arMessage.push_back (Msg);
  207. }
  208. template <typename T>
  209. void FastJsonEncoder <T>::Set (tPCTSTR Key, const std::vector <int> & arValue)
  210. {
  211. T tValue; tValue.GetBuffer (64 * arValue.size ());
  212. for (const auto iValue : arValue)
  213. {
  214. T Value = T::From (iValue);
  215. if (tValue.GetLength () > 0)
  216. {
  217. tValue.Append ('\r').Append ('\n').Append (' ').Append (' ');
  218. tValue.Append (',').Append (' ');
  219. }
  220. else
  221. {
  222. tValue.Append ('\r').Append ('\n').Append (' ').Append (' ');
  223. tValue.Append (' ').Append (' ');
  224. }
  225. tValue.Append (Value);
  226. }
  227. T Message; Message.GetBuffer (64 * arValue.size ());
  228. Message.Append ('\r').Append ('\n').Append (' ').Append (' ');
  229. Message.Append ('"').Append (Key).Append ('"');
  230. Message.Append (' ').Append (':').Append (' ');
  231. Message.Append ('[').Append (tValue).Append ('\r').Append ('\n').Append (' ').Append (' ').Append (']');
  232. m_arMessage.push_back (Message);
  233. }
  234. template <typename T>
  235. void FastJsonEncoder <T>::Set (tPCTSTR Key, const std::vector <T> & arValue)
  236. {
  237. T tValue; tValue.GetBuffer (64 * arValue.size ());
  238. for (auto & strValue : arValue)
  239. {
  240. auto Value = ESC_JSON (strValue, strValue.GetLength ());
  241. if (tValue.GetLength () > 0)
  242. {
  243. tValue.Append ('\r').Append ('\n').Append (' ').Append (' ');
  244. tValue.Append (',').Append (' ');
  245. }
  246. else
  247. {
  248. tValue.Append ('\r').Append ('\n').Append (' ').Append (' ');
  249. tValue.Append (' ').Append (' ');
  250. }
  251. tValue.Append ('\"').Append (Value).Append ('"');
  252. }
  253. T Message; Message.GetBuffer (64 * arValue.size ());
  254. Message.Append ('\r').Append ('\n').Append (' ').Append (' ');
  255. Message.Append ('"').Append (Key).Append ('"');
  256. Message.Append (' ').Append (':').Append (' ');
  257. Message.Append ('[').Append (tValue).Append ('\r').Append ('\n').Append (' ').Append (' ').Append (']');
  258. m_arMessage.push_back (Message);
  259. }
  260. template <typename T>
  261. void FastJsonEncoder <T>::Set (tPCTSTR Key, const std::vector <FastJsonEncoder> & arValue)
  262. {
  263. T tValue; tValue.GetBuffer (64 * arValue.size ());
  264. for (auto & Encoder : arValue)
  265. {
  266. auto Value = Encoder.ToString ();
  267. if (tValue.GetLength () > 0)
  268. {
  269. tValue.Append ('\r').Append ('\n').Append (' ').Append (' ');
  270. tValue.Append (',').Append (' ');
  271. }
  272. else
  273. {
  274. tValue.Append ('\r').Append ('\n').Append (' ').Append (' ');
  275. tValue.Append (' ').Append (' ');
  276. }
  277. tValue.Append (Value);
  278. }
  279. T Message; Message.GetBuffer (64 * arValue.size ());
  280. Message.Append ('\r').Append ('\n').Append (' ').Append (' ');
  281. Message.Append ('"').Append (Key).Append ('"');
  282. Message.Append (' ').Append (':').Append (' ');
  283. Message.Append ('[').Append (tValue).Append ('\r').Append ('\n').Append (' ').Append (' ').Append (']');
  284. m_arMessage.push_back (Message);
  285. }
  286. template <typename T>
  287. T FastJsonEncoder <T>::ToStringOf (const std::vector <FastJsonEncoder> & arValue)
  288. {
  289. T tValue; tValue.GetBuffer (64 * arValue.size ());
  290. for (auto & Encoder : arValue)
  291. {
  292. auto Value = Encoder.ToString ();
  293. if (tValue.GetLength () > 0)
  294. {
  295. tValue.Append ('\r').Append ('\n').Append (' ').Append (' ');
  296. tValue.Append (',').Append (' ');
  297. }
  298. else
  299. {
  300. tValue.Append ('\r').Append ('\n').Append (' ').Append (' ');
  301. tValue.Append (' ').Append (' ');
  302. }
  303. tValue.Append (Value);
  304. }
  305. T Message; Message.GetBuffer (64 * arValue.size ());
  306. Message.Append ('\r').Append ('\n').Append (' ').Append (' ');
  307. Message.Append ('[').Append (tValue).Append ('\r').Append ('\n').Append (' ').Append (' ').Append (']');
  308. return Message;
  309. }
  310. template <typename T>
  311. bool FastJsonEncoder <T>::Recode (LPCSTR String)
  312. {
  313. static_assert (std::is_base_of <DString, T>::value, "T must be derived from DString");
  314. T Msg (String);
  315. int first = Msg.Find ('{');
  316. if (first >= 0)
  317. Msg.SetAt (first, ' ');
  318. int last = Msg.ReverseFind ('}');
  319. if (last >= 0)
  320. Msg.SetAt (last, ' ');
  321. Msg.TrimLeft (); Msg.TrimRight ();
  322. // 空串不要加入了
  323. if (Msg.GetLength () > 0)
  324. m_arMessage.push_back (Msg);
  325. return true;
  326. }
  327. template <typename T>
  328. bool FastJsonEncoder <T>::Recode (PCWSTR String)
  329. {
  330. static_assert (std::is_base_of <WString, T>::value, "T must be derived from WString");
  331. T Msg (String);
  332. int first = Msg.Find ('{');
  333. if (first >= 0)
  334. Msg.SetAt (first, ' ');
  335. int last = Msg.ReverseFind ('}');
  336. if (last >= 0)
  337. Msg.SetAt (last, ' ');
  338. Msg.TrimLeft (); Msg.TrimRight ();
  339. // 空串不要加入了
  340. if (Msg.GetLength () > 0)
  341. m_arMessage.push_back (Msg);
  342. return true;
  343. }
  344. template <typename T>
  345. T FastJsonEncoder <T>::ToStringOfElement () const
  346. {
  347. T Msg; Msg.GetBuffer (10240);
  348. for (auto Iter = Iterator::From (m_arMessage); Iter; Iter++)
  349. {
  350. const auto & msg = Iter ();
  351. if (Iter.Index ())
  352. Msg.Append (',');
  353. Msg.Append (msg);
  354. }
  355. return Msg;
  356. }
  357. template <typename T>
  358. T FastJsonEncoder <T>::ToString () const
  359. {
  360. T Msg; Msg.GetBuffer (10240);
  361. Msg.Append ("{");
  362. Msg.Append (ToStringOfElement ());
  363. Msg.Append ("\r\n}");
  364. //#ifdef _DEBUG
  365. #if 0
  366. OFileBuffer OFB;
  367. OFB.OpenForWrite ("C:\\FastJsonEncoder.JSON");
  368. OFB.Write (Msg, Msg.GetLength ());
  369. OFB.Close ();
  370. #endif
  371. return Msg;
  372. }
  373. template <typename T>
  374. void FastJsonEncoder <T>::Empty ()
  375. {
  376. m_arMessage.clear ();
  377. }
  378. template <typename T>
  379. void FastJsonEncoder <T>::CopyTo (FastJsonEncoder & toEncoder) const
  380. {
  381. // m_arMessage.CopyTo (toEncoder.m_arMessage);
  382. for (const auto msg : m_arMessage)
  383. toEncoder.m_arMessage.push_back (msg);
  384. }
  385. template <typename T>
  386. void FastJsonEncoder <T>::Swap (FastJsonEncoder & toEncoder)
  387. {
  388. m_arMessage.swap (toEncoder.m_arMessage);
  389. }
  390. template <typename T>
  391. void FastJsonEncoder <T>::operator = (FastJsonEncoder && Encoder)
  392. {
  393. Swap (Encoder);
  394. }
  395. template <typename T>
  396. void FastJsonEncoder <T>::operator = (const FastJsonEncoder & Encoder)
  397. {
  398. Encoder.CopyTo (*this);
  399. }