EasyJSONEncoder.cpp 9.2 KB

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