#include "EasyJSONEncoder.hpp" #include //----------------------------------------------------------------------------- // EasyJSONEncoder //----------------------------------------------------------------------------- EasyJSONEncoder::EasyJSONEncoder () { } EasyJSONEncoder::EasyJSONEncoder (const EasyJSONEncoder & Encoder) { Encoder.CopyTo (*this); } EasyJSONEncoder::~EasyJSONEncoder () { } // 以下代码处理转义字符, \\ " / static inline std::string ESC_JSON (const char* strValue, int length) { const char* pc = strValue; std::string toValue; toValue.resize (length + length); // 预分配双倍的长度 char * pt = (char *) toValue.data (); char * ps = pt; for (; *pc; pc++) { char ch = *pc; if (ch == '\\') { *pt = ch; pt ++; *pt = ch; pt ++; } else if (ch == '"') { *pt = '\\'; pt ++; *pt = ch; pt ++; } else if (ch == '/') { *pt = '\\'; pt ++; *pt = ch; pt ++; } else { *pt = ch; pt ++; } } int len = (int) (pt-ps); toValue.resize (len); return toValue; } void EasyJSONEncoder::Set (std::string Key, LPCTSTR strValue) { std::string Value = strValue;// ESC_JSON(strValue, int(strlen(strValue))); std::string Msg; Msg.reserve (64); // 值是否为Object bool bObject = false; for (size_t i=0; i & arValue) { std::string tValue; tValue.reserve (64 * arValue.size ()); for (const auto iValue : arValue) { std::string Value = std::to_string (iValue); if (tValue.length () > 0) { tValue.append ("\r").append ("\n").append (" ").append (" "); tValue.append (",").append (" "); } else { tValue.append ("\r").append ("\n").append (" ").append (" "); tValue.append (" ").append (" "); } tValue.append (Value); } std::string Message; Message.reserve (64 * arValue.size ()); Message.append ("\r").append ("\n").append (" ").append (" "); Message.append ("\"").append (Key).append ("\""); Message.append (" ").append (":").append (" "); Message.append ("[").append (tValue).append ("\r").append ("\n").append (" ").append (" ").append ("]"); m_arMessage.push_back (Message); } void EasyJSONEncoder::Set (std::string Key, const std::vector & arValue) { std::string tValue; tValue.reserve (64 * arValue.size ()); for (auto & strValue : arValue) { std::string Value = ESC_JSON (strValue.c_str (), strValue.length ()); if (tValue.length () > 0) { tValue.append ("\r").append ("\n").append (" ").append (" "); tValue.append (",").append (" "); } else { tValue.append ("\r").append ("\n").append (" ").append (" "); tValue.append (" ").append (" "); } tValue.append ("\"").append (Value).append ("\""); } std::string Message; Message.reserve (64 * arValue.size ()); Message.append ("\r").append ("\n").append (" ").append (" "); Message.append ("\"").append (Key).append ("\""); Message.append (" ").append (":").append (" "); Message.append ("[").append (tValue).append ("\r").append ("\n").append (" ").append (" ").append ("]"); m_arMessage.push_back (Message); } void EasyJSONEncoder::Set (std::string Key, const std::vector & arValue) { std::string tValue; tValue.reserve (64 * arValue.size ()); for (auto & Encoder : arValue) { std::string Value = Encoder.ToString (); if (tValue.length () > 0) { tValue.append ("\r").append ("\n").append (" ").append (" "); tValue.append (",").append (" "); } else { tValue.append ("\r").append ("\n").append (" ").append (" "); tValue.append (" ").append (" "); } tValue.append (Value); } std::string Message; Message.reserve (64 * arValue.size ()); Message.append ("\r").append ("\n").append (" ").append (" "); Message.append ("\"").append (Key).append ("\""); Message.append (" ").append (":").append (" "); Message.append ("[").append (tValue).append ("\r").append ("\n").append (" ").append (" ").append ("]"); m_arMessage.push_back (Message); } bool EasyJSONEncoder::Recode (std::string String) { std::string Msg (String); auto first = Msg.find ('{'); if (first != std::string::npos) { char * pc = (char *) Msg.data (); pc [first] = ' '; } auto last = Msg.rfind ('}'); if (first != std::string::npos) { char * pc = (char *)Msg.data (); pc [last] = ' '; } // Msg.TrimLeft (); Msg.TrimRight (); // 空串不要加入了 if (Msg.length () > 0) m_arMessage.push_back (Msg); return true; } std::string EasyJSONEncoder::ToStringOfElement () const { std::string Msg; Msg.reserve (10240); for (const auto Item : m_arMessage) { if (Msg.length ()) Msg.append (","); Msg.append (Item); } return Msg; } std::string EasyJSONEncoder::ToString () const { std::string Msg; Msg.reserve (10240); Msg.append ("\r\n{"); Msg.append (ToStringOfElement ()); Msg.append ("\r\n}\r\n"); //#ifdef _DEBUG #if 0 OFileBuffer OFB; OFB.OpenForWrite ("C:\\EasyJSONEncoder.JSON"); OFB.Write (Msg, Msg.GetLength ()); OFB.Close (); #endif return Msg; } void EasyJSONEncoder::Empty () { m_arMessage.clear (); } void EasyJSONEncoder::CopyTo (EasyJSONEncoder & toEncoder) const { // m_arMessage.CopyTo (toEncoder.m_arMessage); for (const auto msg : m_arMessage) toEncoder.m_arMessage.push_back (msg); } void EasyJSONEncoder::operator = (const EasyJSONEncoder & Encoder) { Encoder.CopyTo (*this); }