EasyJSONEncoder.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. #include "stdafx.h"
  2. #pragma warning (disable:4267)
  3. // warning C4267 : “参数”: 从“size_t”转换到“int”,可能丢失数据
  4. #include "EasyJSONEncoder.hpp"
  5. //-----------------------------------------------------------------------------
  6. // EasyJSONEncoder
  7. //-----------------------------------------------------------------------------
  8. EasyJSONEncoder::EasyJSONEncoder ()
  9. {
  10. }
  11. EasyJSONEncoder::EasyJSONEncoder (const EasyJSONEncoder & Encoder)
  12. {
  13. Encoder.CopyTo (*this);
  14. }
  15. EasyJSONEncoder::~EasyJSONEncoder ()
  16. {
  17. }
  18. // 以下代码处理转义字符, \\ " /
  19. static inline std::string ESC_JSON (LPCTSTR strValue, int length)
  20. {
  21. LPCTSTR pc = strValue;
  22. std::string toValue;
  23. toValue.resize (length + length); // 预分配双倍的长度
  24. char * pt = (char *) toValue.data ();
  25. char * ps = pt;
  26. for (; *pc; pc++)
  27. {
  28. char ch = *pc;
  29. if (ch == '\\')
  30. {
  31. *pt = ch;
  32. pt ++;
  33. *pt = ch;
  34. pt ++;
  35. }
  36. else
  37. if (ch == '"')
  38. {
  39. *pt = '\\';
  40. pt ++;
  41. *pt = ch;
  42. pt ++;
  43. }
  44. else
  45. if (ch == '/')
  46. {
  47. *pt = '\\';
  48. pt ++;
  49. *pt = ch;
  50. pt ++;
  51. }
  52. else
  53. {
  54. *pt = ch;
  55. pt ++;
  56. }
  57. }
  58. int len = (int) (pt-ps);
  59. toValue.resize (len);
  60. return toValue;
  61. }
  62. void EasyJSONEncoder::Set (std::string Key, LPCTSTR strValue)
  63. {
  64. std::string Value = strValue;// ESC_JSON(strValue, int(strlen(strValue)));
  65. std::string Msg; Msg.reserve (64);
  66. // 值是否为Object
  67. bool bObject = false;
  68. for (size_t i=0; i<Value.size(); i++)
  69. {
  70. char cValue = Value[i];
  71. if (cValue != ' ' && cValue != '\t' &&
  72. cValue != '\r' && cValue != '\n')
  73. {
  74. if ((cValue == '{' || cValue == '[')&& Value.find(':') != std::string::npos)
  75. {
  76. bObject = true;
  77. }
  78. break;
  79. }
  80. }
  81. if (bObject)
  82. {
  83. // Object不转义
  84. Msg.append("\r").append("\n").append(" ").append(" ");
  85. Msg.append("\"").append(Key).append("\"");
  86. Msg.append(" ").append(":").append(" ");
  87. Msg.append(Value);
  88. }
  89. else
  90. {
  91. // 其他则转义
  92. Value = ESC_JSON(strValue, int(strlen(strValue)));
  93. Msg.append("\r").append("\n").append(" ").append(" ");
  94. Msg.append("\"").append(Key).append("\"");
  95. Msg.append(" ").append(":").append(" ");
  96. Msg.append("\"").append(Value).append("\"");
  97. }
  98. m_arMessage.push_back (Msg);
  99. }
  100. void EasyJSONEncoder::Set (std::string Key, const std::string & strTmp)
  101. {
  102. const char * strValue = strTmp.c_str ();
  103. std::string Value = ESC_JSON (strValue, strTmp.length ());
  104. std::string Msg; Msg.reserve (64);
  105. Msg.append ("\r").append ("\n").append (" ").append (" ");
  106. Msg.append ("\"").append (Key).append ("\"");
  107. Msg.append (" ").append (":").append (" ");
  108. Msg.append ("\"").append (Value).append ("\"");
  109. m_arMessage.push_back (Msg);
  110. }
  111. void EasyJSONEncoder::Set (std::string Key, UINT_PTR Value)
  112. {
  113. std::string Msg; Msg.reserve (64);
  114. Msg.append ("\r").append ("\n").append (" ").append (" ");
  115. Msg.append ("\"").append (Key).append ("\"");
  116. Msg.append (" ").append (":").append (" ");
  117. Msg.append (std::to_string ((unsigned int) Value));
  118. m_arMessage.push_back (Msg);
  119. }
  120. void EasyJSONEncoder::Set (std::string Key, __int64 Value)
  121. {
  122. std::string Msg; Msg.reserve (64);
  123. Msg.append ("\r").append ("\n").append (" ").append (" ");
  124. Msg.append ("\"").append (Key).append ("\"");
  125. Msg.append (" ").append (":").append (" ");
  126. Msg.append (std::to_string ((unsigned int)Value));
  127. m_arMessage.push_back (Msg);
  128. }
  129. void EasyJSONEncoder::Set (std::string Key, DWORD Value)
  130. {
  131. std::string Msg; Msg.reserve (64);
  132. Msg.append ("\r").append ("\n").append (" ").append (" ");
  133. Msg.append ("\"").append (Key).append ("\"");
  134. Msg.append (" ").append (":").append (" ");
  135. Msg.append (std::to_string (Value));
  136. m_arMessage.push_back (Msg);
  137. }
  138. void EasyJSONEncoder::Set (std::string Key, int Value)
  139. {
  140. std::string Msg; Msg.reserve (64);
  141. Msg.append ("\r").append ("\n").append (" ").append (" ");
  142. Msg.append ("\"").append (Key).append ("\"");
  143. Msg.append (" ").append (":").append (" ");
  144. Msg.append (std::to_string (Value));
  145. m_arMessage.push_back (Msg);
  146. }
  147. void EasyJSONEncoder::Set (std::string Key, bool Value)
  148. {
  149. std::string Msg; Msg.reserve (64);
  150. Msg.append ("\r").append ("\n").append (" ").append (" ");
  151. Msg.append ("\"").append (Key).append ("\"");
  152. Msg.append (" ").append (":").append (" ");
  153. Msg.append (std::to_string (Value));
  154. m_arMessage.push_back (Msg);
  155. }
  156. /*
  157. void EasyJSONEncoder::Set (std::string Key, void * ptr)
  158. {
  159. std::string Msg; Msg.reserve (64);
  160. Msg.append ("\r").append ("\n").append (" ").append (" ");
  161. Msg.append ("\"").append (Key).append ("\"");
  162. Msg.append (" ").append (":").append (" ");
  163. Msg.AppendFormat ("\"0x%p\"", ptr);
  164. m_arMessage.push_back (Msg);
  165. }
  166. */
  167. void EasyJSONEncoder::Set (std::string Key, float Value)
  168. {
  169. std::string Msg; Msg.reserve (64);
  170. Msg.append ("\r").append ("\n").append (" ").append (" ");
  171. Msg.append ("\"").append (Key).append ("\"");
  172. Msg.append (" ").append (":").append (" ");
  173. Msg.append (std::to_string (Value));
  174. m_arMessage.push_back (Msg);
  175. }
  176. void EasyJSONEncoder::Set (std::string Key, double Value)
  177. {
  178. std::string Msg; Msg.reserve (64);
  179. Msg.append ("\r").append ("\n").append (" ").append (" ");
  180. Msg.append ("\"").append (Key).append ("\"");
  181. Msg.append (" ").append (":").append (" ");
  182. Msg.append (std::to_string (Value));
  183. m_arMessage.push_back (Msg);
  184. }
  185. void EasyJSONEncoder::Set (std::string Key, const std::vector <int> & arValue)
  186. {
  187. std::string tValue; tValue.reserve (64 * arValue.size ());
  188. for (const auto iValue : arValue)
  189. {
  190. std::string Value = std::to_string (iValue);
  191. if (tValue.length () > 0)
  192. {
  193. tValue.append ("\r").append ("\n").append (" ").append (" ");
  194. tValue.append (",").append (" ");
  195. }
  196. else
  197. {
  198. tValue.append ("\r").append ("\n").append (" ").append (" ");
  199. tValue.append (" ").append (" ");
  200. }
  201. tValue.append (Value);
  202. }
  203. std::string Message; Message.reserve (64 * arValue.size ());
  204. Message.append ("\r").append ("\n").append (" ").append (" ");
  205. Message.append ("\"").append (Key).append ("\"");
  206. Message.append (" ").append (":").append (" ");
  207. Message.append ("[").append (tValue).append ("\r").append ("\n").append (" ").append (" ").append ("]");
  208. m_arMessage.push_back (Message);
  209. }
  210. void EasyJSONEncoder::Set (std::string Key, const std::vector <std::string> & arValue)
  211. {
  212. std::string tValue; tValue.reserve (64 * arValue.size ());
  213. for (auto & strValue : arValue)
  214. {
  215. std::string Value = ESC_JSON (strValue.c_str (), strValue.length ());
  216. if (tValue.length () > 0)
  217. {
  218. tValue.append ("\r").append ("\n").append (" ").append (" ");
  219. tValue.append (",").append (" ");
  220. }
  221. else
  222. {
  223. tValue.append ("\r").append ("\n").append (" ").append (" ");
  224. tValue.append (" ").append (" ");
  225. }
  226. tValue.append ("\"").append (Value).append ("\"");
  227. }
  228. std::string Message; Message.reserve (64 * arValue.size ());
  229. Message.append ("\r").append ("\n").append (" ").append (" ");
  230. Message.append ("\"").append (Key).append ("\"");
  231. Message.append (" ").append (":").append (" ");
  232. Message.append ("[").append (tValue).append ("\r").append ("\n").append (" ").append (" ").append ("]");
  233. m_arMessage.push_back (Message);
  234. }
  235. void EasyJSONEncoder::Set (std::string Key, const std::vector <EasyJSONEncoder> & arValue)
  236. {
  237. std::string tValue; tValue.reserve (64 * arValue.size ());
  238. for (auto & Encoder : arValue)
  239. {
  240. std::string Value = Encoder.ToString ();
  241. if (tValue.length () > 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 (Value);
  252. }
  253. std::string Message; Message.reserve (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. bool EasyJSONEncoder::Recode (std::string String)
  261. {
  262. std::string Msg (String);
  263. auto first = Msg.find ('{');
  264. if (first != std::string::npos)
  265. {
  266. char * pc = (char *) Msg.data ();
  267. pc [first] = ' ';
  268. }
  269. auto last = Msg.rfind ('}');
  270. if (first != std::string::npos)
  271. {
  272. char * pc = (char *)Msg.data ();
  273. pc [last] = ' ';
  274. }
  275. // Msg.TrimLeft (); Msg.TrimRight ();
  276. // 空串不要加入了
  277. if (Msg.length () > 0)
  278. m_arMessage.push_back (Msg);
  279. return true;
  280. }
  281. std::string EasyJSONEncoder::ToStringOfElement () const
  282. {
  283. std::string Msg; Msg.reserve (10240);
  284. for (const auto Item : m_arMessage)
  285. {
  286. if (Msg.length ())
  287. Msg.append (",");
  288. Msg.append (Item);
  289. }
  290. return Msg;
  291. }
  292. std::string EasyJSONEncoder::ToString () const
  293. {
  294. std::string Msg; Msg.reserve (10240);
  295. Msg.append ("\r\n{");
  296. Msg.append (ToStringOfElement ());
  297. Msg.append ("\r\n}\r\n");
  298. //#ifdef _DEBUG
  299. #if 0
  300. OFileBuffer OFB;
  301. OFB.OpenForWrite ("C:\\EasyJSONEncoder.JSON");
  302. OFB.Write (Msg, Msg.GetLength ());
  303. OFB.Close ();
  304. #endif
  305. return Msg;
  306. }
  307. void EasyJSONEncoder::Empty ()
  308. {
  309. m_arMessage.clear ();
  310. }
  311. void EasyJSONEncoder::CopyTo (EasyJSONEncoder & toEncoder) const
  312. {
  313. // m_arMessage.CopyTo (toEncoder.m_arMessage);
  314. for (const auto msg : m_arMessage)
  315. toEncoder.m_arMessage.push_back (msg);
  316. }
  317. void EasyJSONEncoder::operator = (const EasyJSONEncoder & Encoder)
  318. {
  319. Encoder.CopyTo (*this);
  320. }