123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402 |
- #include "EasyJSONEncoder.hpp"
- #include <cstring>
- //-----------------------------------------------------------------------------
- // 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<Value.size(); i++)
- {
- char cValue = Value[i];
- if (cValue != ' ' && cValue != '\t' &&
- cValue != '\r' && cValue != '\n')
- {
- if ((cValue == '{' || cValue == '[')&& Value.find(':') != std::string::npos)
- {
- bObject = true;
- }
- break;
- }
- }
- if (bObject)
- {
- // Object不转义
- Msg.append("\r").append("\n").append(" ").append(" ");
- Msg.append("\"").append(Key).append("\"");
- Msg.append(" ").append(":").append(" ");
- Msg.append(Value);
- }
- else
- {
- // 其他则转义
- Value = ESC_JSON(strValue, int(strlen(strValue)));
- Msg.append("\r").append("\n").append(" ").append(" ");
- Msg.append("\"").append(Key).append("\"");
- Msg.append(" ").append(":").append(" ");
- Msg.append("\"").append(Value).append("\"");
- }
- m_arMessage.push_back (Msg);
- }
- void EasyJSONEncoder::Set (std::string Key, const std::string & strTmp)
- {
- const char * strValue = strTmp.c_str ();
- std::string Value = ESC_JSON (strValue, strTmp.length ());
- std::string Msg; Msg.reserve (64);
- Msg.append ("\r").append ("\n").append (" ").append (" ");
- Msg.append ("\"").append (Key).append ("\"");
- Msg.append (" ").append (":").append (" ");
- Msg.append ("\"").append (Value).append ("\"");
- m_arMessage.push_back (Msg);
- }
- void EasyJSONEncoder::Set (std::string Key, int64_t Value)
- {
- std::string Msg; Msg.reserve (64);
- Msg.append ("\r").append ("\n").append (" ").append (" ");
- Msg.append ("\"").append (Key).append ("\"");
- Msg.append (" ").append (":").append (" ");
- Msg.append (std::to_string ((unsigned int) Value));
- m_arMessage.push_back (Msg);
- }
- void EasyJSONEncoder::Set (std::string Key, uintptr_t Value)
- {
- std::string Msg; Msg.reserve (64);
- Msg.append ("\r").append ("\n").append (" ").append (" ");
- Msg.append ("\"").append (Key).append ("\"");
- Msg.append (" ").append (":").append (" ");
- Msg.append (std::to_string ((unsigned int)Value));
- m_arMessage.push_back (Msg);
- }
- void EasyJSONEncoder::Set (std::string Key, uint32_t Value)
- {
- std::string Msg; Msg.reserve (64);
- Msg.append ("\r").append ("\n").append (" ").append (" ");
- Msg.append ("\"").append (Key).append ("\"");
- Msg.append (" ").append (":").append (" ");
- Msg.append (std::to_string (Value));
- m_arMessage.push_back (Msg);
- }
- void EasyJSONEncoder::Set (std::string Key, int Value)
- {
- std::string Msg; Msg.reserve (64);
- Msg.append ("\r").append ("\n").append (" ").append (" ");
- Msg.append ("\"").append (Key).append ("\"");
- Msg.append (" ").append (":").append (" ");
- Msg.append (std::to_string (Value));
- m_arMessage.push_back (Msg);
- }
- void EasyJSONEncoder::Set (std::string Key, bool Value)
- {
- std::string Msg; Msg.reserve (64);
- Msg.append ("\r").append ("\n").append (" ").append (" ");
- Msg.append ("\"").append (Key).append ("\"");
- Msg.append (" ").append (":").append (" ");
- Msg.append (std::to_string (Value));
- m_arMessage.push_back (Msg);
- }
- /*
- void EasyJSONEncoder::Set (std::string Key, void * ptr)
- {
- std::string Msg; Msg.reserve (64);
- Msg.append ("\r").append ("\n").append (" ").append (" ");
- Msg.append ("\"").append (Key).append ("\"");
- Msg.append (" ").append (":").append (" ");
- Msg.AppendFormat ("\"0x%p\"", ptr);
- m_arMessage.push_back (Msg);
- }
- */
- void EasyJSONEncoder::Set (std::string Key, float Value)
- {
- std::string Msg; Msg.reserve (64);
- Msg.append ("\r").append ("\n").append (" ").append (" ");
- Msg.append ("\"").append (Key).append ("\"");
- Msg.append (" ").append (":").append (" ");
- Msg.append (std::to_string (Value));
- m_arMessage.push_back (Msg);
- }
- void EasyJSONEncoder::Set (std::string Key, double Value)
- {
- std::string Msg; Msg.reserve (64);
- Msg.append ("\r").append ("\n").append (" ").append (" ");
- Msg.append ("\"").append (Key).append ("\"");
- Msg.append (" ").append (":").append (" ");
- Msg.append (std::to_string (Value));
- m_arMessage.push_back (Msg);
- }
- void EasyJSONEncoder::Set (std::string Key, const std::vector <int> & 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 <std::string> & 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 <EasyJSONEncoder> & 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);
- }
|