123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526 |
- #pragma once
- #include "Iterator.tlh"
- #pragma warning (disable:4267)
- // warning C4267 : “参数”: 从“size_t”转换到“int”,可能丢失数据
- #include "WString.hpp"
- #include "FastJsonEncoder.tlh"
- //-----------------------------------------------------------------------------
- // FastJsonEncoder
- //-----------------------------------------------------------------------------
- template <typename T>
- FastJsonEncoder <T>::FastJsonEncoder ()
- {
- }
- template <typename T>
- FastJsonEncoder <T>::FastJsonEncoder (FastJsonEncoder && Encoder)
- {
- Swap (Encoder);
- }
- template <typename T>
- FastJsonEncoder <T>::FastJsonEncoder (const FastJsonEncoder & Encoder)
- {
- Encoder.CopyTo (*this);
- }
- template <typename T>
- FastJsonEncoder <T>::~FastJsonEncoder ()
- {
- }
- // 以下代码处理转义字符
- /*
- else if (*b == Ch('\b')) result += Ch('\\'), result += Ch('b');
- else if (*b == Ch('\f')) result += Ch('\\'), result += Ch('f');
- else if (*b == Ch('\n')) result += Ch('\\'), result += Ch('n');
- else if (*b == Ch('\r')) result += Ch('\\'), result += Ch('r');
- else if (*b == Ch('\t')) result += Ch('\\'), result += Ch('t');
- else if (*b == Ch('/')) result += Ch('\\'), result += Ch('/');
- else if (*b == Ch('"')) result += Ch('\\'), result += Ch('"');
- else if (*b == Ch('\\')) result += Ch('\\'), result += Ch('\\');
- */
- static inline DString ESC_JSON (PCTSTR strValue, int length)
- {
- const char * pc = strValue;
- DString toValue;
- char * pt = toValue.GetBuffer (length + length); // 预分配双倍的长度
- char * ps = pt;
- for (; *pc; pc++)
- {
- char ch = *pc;
-
- if (ch == '\\') *pt++ = '\\', *pt++ = '\\';
- else if (ch == '"') *pt++ = '\\', *pt++ = '"';
- else if (ch == '/') *pt++ = '\\', *pt++ = '/';
- else if (ch == '\b') *pt++ = '\\', *pt++ = 'b';
- else if (ch == '\f') *pt++ = '\\', *pt++ = 'f';
- else if (ch == '\n') *pt++ = '\\', *pt++ = 'n';
- else if (ch == '\r') *pt++ = '\\', *pt++ = 'r';
- else if (ch == '\t') *pt++ = '\\', *pt++ = 't';
- else *pt++ = ch;
- }
- int len = (int) (pt - ps);
- toValue.ReleaseBuffer (len);
- return toValue;
- }
- static inline WString ESC_JSON (PCWSTR strValue, int length)
- {
- const wchar_t * pc = strValue;
- WString toValue;
- wchar_t * pt = toValue.GetBuffer (length + length); // 预分配双倍的长度
- wchar_t * ps = pt;
- for (; *pc; pc++)
- {
- wchar_t ch = *pc;
- if (ch == '\\') *pt++ = '\\', *pt++ = '\\';
- else if (ch == '"') *pt++ = '\\', *pt++ = '"';
- else if (ch == '/') *pt++ = '\\', *pt++ = '/';
- else if (ch == '\b') *pt++ = '\\', *pt++ = 'b';
- else if (ch == '\f') *pt++ = '\\', *pt++ = 'f';
- else if (ch == '\n') *pt++ = '\\', *pt++ = 'n';
- else if (ch == '\r') *pt++ = '\\', *pt++ = 'r';
- else if (ch == '\t') *pt++ = '\\', *pt++ = 't';
- else *pt++ = ch;
- }
- int len = (int) (pt - ps);
- toValue.ReleaseBuffer (len);
- return toValue;
- }
- template <typename T>
- void FastJsonEncoder <T>::Set (tPCTSTR Key, LPCSTR strValue)
- {
- static_assert (std::is_base_of <DString, T>::value, "T must be derived from DString");
- auto Value = ESC_JSON (strValue, int (strlen (strValue)));
- T Msg; Msg.GetBuffer (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);
- }
- template <typename T>
- void FastJsonEncoder <T>::Set (tPCTSTR Key, PCWSTR strValue)
- {
- static_assert (std::is_base_of <WString, T>::value, "T must be derived from WString");
- auto Value = ESC_JSON (strValue, int (wcslen (strValue)));
- T Msg; Msg.GetBuffer (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);
- }
- template <typename T>
- void FastJsonEncoder <T>::Set (tPCTSTR Key, const DString & strTmp)
- {
- // static_assert (std::is_base_of <DString, T>::value, "T must be derived from DString");
- auto Value = ESC_JSON (strTmp, strTmp.GetLength ());
- T Msg; Msg.GetBuffer (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);
- }
- template <typename T>
- void FastJsonEncoder <T>::Set (tPCTSTR Key, const WString & strTmp)
- {
- static_assert (std::is_base_of <WString, T>::value, "T must be derived from WString");
- auto Value = ESC_JSON (strTmp, strTmp.GetLength ());
- T Msg; Msg.GetBuffer (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);
- }
- template <typename T>
- void FastJsonEncoder <T>::Set (tPCTSTR Key, UINT_PTR Value)
- {
- T Msg; Msg.GetBuffer (64);
- Msg.Append ('\r').Append ('\n').Append (' ').Append (' ');
- Msg.Append ('"').Append (Key).Append ('"');
- Msg.Append (' ').Append (':').Append (' ');
- Msg.Append (T::From (Value));
- m_arMessage.push_back (Msg);
- }
- template <typename T>
- void FastJsonEncoder <T>::Set (tPCTSTR Key, __int64 Value)
- {
- T Msg; Msg.GetBuffer (64);
- Msg.Append ('\r').Append ('\n').Append (' ').Append (' ');
- Msg.Append ('"').Append (Key).Append ('"');
- Msg.Append (' ').Append (':').Append (' ');
- Msg.Append (T::From (Value));
- m_arMessage.push_back (Msg);
- }
- template <typename T>
- void FastJsonEncoder <T>::Set (tPCTSTR Key, DWORD Value)
- {
- T Msg; Msg.GetBuffer (64);
- Msg.Append ('\r').Append ('\n').Append (' ').Append (' ');
- Msg.Append ('"').Append (Key).Append ('"');
- Msg.Append (' ').Append (':').Append (' ');
- Msg.Append (T::From (Value));
- m_arMessage.push_back (Msg);
- }
- template <typename T>
- void FastJsonEncoder <T>::Set (tPCTSTR Key, int Value)
- {
- T Msg; Msg.GetBuffer (64);
- Msg.Append ('\r').Append ('\n').Append (' ').Append (' ');
- Msg.Append ('"').Append (Key).Append ('"');
- Msg.Append (' ').Append (':').Append (' ');
- Msg.Append (T::From (Value));
- m_arMessage.push_back (Msg);
- }
- template <typename T>
- void FastJsonEncoder <T>::Set (tPCTSTR Key, bool Value)
- {
- T Msg; Msg.GetBuffer (64);
- Msg.Append ('\r').Append ('\n').Append (' ').Append (' ');
- Msg.Append ('"').Append (Key).Append ('"');
- Msg.Append (' ').Append (':').Append (' ');
- Msg.Append (T::From (Value));
- m_arMessage.push_back (Msg);
- }
- template <typename T>
- void FastJsonEncoder <T>::Set (tPCTSTR Key, void * ptr)
- {
- return Set (Key, (UINT_PTR) ptr);
- }
- template <typename T>
- void FastJsonEncoder <T>::Set (tPCTSTR Key, float Value)
- {
- T Msg; Msg.GetBuffer (64);
- Msg.Append ('\r').Append ('\n').Append (' ').Append (' ');
- Msg.Append ('"').Append (Key).Append ('"');
- Msg.Append (' ').Append (':').Append (' ');
- Msg.Append (T::From (Value));
- m_arMessage.push_back (Msg);
- }
- template <typename T>
- void FastJsonEncoder <T>::Set (tPCTSTR Key, double Value)
- {
- T Msg; Msg.GetBuffer (64);
- Msg.Append ('\r').Append ('\n').Append (' ').Append (' ');
- Msg.Append ('"').Append (Key).Append ('"');
- Msg.Append (' ').Append (':').Append (' ');
- Msg.Append (T::From (Value));
- m_arMessage.push_back (Msg);
- }
- template <typename T>
- void FastJsonEncoder <T>::Set (tPCTSTR Key, const std::vector <int> & arValue)
- {
- T tValue; tValue.GetBuffer (64 * arValue.size ());
- for (const auto iValue : arValue)
- {
- T Value = T::From (iValue);
- if (tValue.GetLength () > 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);
- }
- T Message; Message.GetBuffer (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);
- }
- template <typename T>
- void FastJsonEncoder <T>::Set (tPCTSTR Key, const std::vector <T> & arValue)
- {
- T tValue; tValue.GetBuffer (64 * arValue.size ());
- for (auto & strValue : arValue)
- {
- auto Value = ESC_JSON (strValue, strValue.GetLength ());
- if (tValue.GetLength () > 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 ('"');
- }
- T Message; Message.GetBuffer (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);
- }
- template <typename T>
- void FastJsonEncoder <T>::Set (tPCTSTR Key, const std::vector <FastJsonEncoder> & arValue)
- {
- T tValue; tValue.GetBuffer (64 * arValue.size ());
- for (auto & Encoder : arValue)
- {
- auto Value = Encoder.ToString ();
- if (tValue.GetLength () > 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);
- }
- T Message; Message.GetBuffer (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);
- }
- template <typename T>
- T FastJsonEncoder <T>::ToStringOf (const std::vector <FastJsonEncoder> & arValue)
- {
- T tValue; tValue.GetBuffer (64 * arValue.size ());
- for (auto & Encoder : arValue)
- {
- auto Value = Encoder.ToString ();
- if (tValue.GetLength () > 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);
- }
- T Message; Message.GetBuffer (64 * arValue.size ());
- Message.Append ('\r').Append ('\n').Append (' ').Append (' ');
- Message.Append ('[').Append (tValue).Append ('\r').Append ('\n').Append (' ').Append (' ').Append (']');
- return Message;
- }
- template <typename T>
- bool FastJsonEncoder <T>::Recode (LPCSTR String)
- {
- static_assert (std::is_base_of <DString, T>::value, "T must be derived from DString");
- T Msg (String);
- int first = Msg.Find ('{');
- if (first >= 0)
- Msg.SetAt (first, ' ');
- int last = Msg.ReverseFind ('}');
- if (last >= 0)
- Msg.SetAt (last, ' ');
- Msg.TrimLeft (); Msg.TrimRight ();
- // 空串不要加入了
- if (Msg.GetLength () > 0)
- m_arMessage.push_back (Msg);
- return true;
- }
- template <typename T>
- bool FastJsonEncoder <T>::Recode (PCWSTR String)
- {
- static_assert (std::is_base_of <WString, T>::value, "T must be derived from WString");
- T Msg (String);
- int first = Msg.Find ('{');
- if (first >= 0)
- Msg.SetAt (first, ' ');
- int last = Msg.ReverseFind ('}');
- if (last >= 0)
- Msg.SetAt (last, ' ');
- Msg.TrimLeft (); Msg.TrimRight ();
- // 空串不要加入了
- if (Msg.GetLength () > 0)
- m_arMessage.push_back (Msg);
- return true;
- }
- template <typename T>
- T FastJsonEncoder <T>::ToStringOfElement () const
- {
- T Msg; Msg.GetBuffer (10240);
- for (auto Iter = Iterator::From (m_arMessage); Iter; Iter++)
- {
- const auto & msg = Iter ();
- if (Iter.Index ())
- Msg.Append (',');
- Msg.Append (msg);
- }
- return Msg;
- }
- template <typename T>
- T FastJsonEncoder <T>::ToString () const
- {
- T Msg; Msg.GetBuffer (10240);
- Msg.Append ("{");
- Msg.Append (ToStringOfElement ());
- Msg.Append ("\r\n}");
- //#ifdef _DEBUG
- #if 0
- OFileBuffer OFB;
- OFB.OpenForWrite ("C:\\FastJsonEncoder.JSON");
- OFB.Write (Msg, Msg.GetLength ());
- OFB.Close ();
- #endif
- return Msg;
- }
- template <typename T>
- void FastJsonEncoder <T>::Empty ()
- {
- m_arMessage.clear ();
- }
- template <typename T>
- void FastJsonEncoder <T>::CopyTo (FastJsonEncoder & toEncoder) const
- {
- // m_arMessage.CopyTo (toEncoder.m_arMessage);
- for (const auto msg : m_arMessage)
- toEncoder.m_arMessage.push_back (msg);
- }
- template <typename T>
- void FastJsonEncoder <T>::Swap (FastJsonEncoder & toEncoder)
- {
- m_arMessage.swap (toEncoder.m_arMessage);
- }
- template <typename T>
- void FastJsonEncoder <T>::operator = (FastJsonEncoder && Encoder)
- {
- Swap (Encoder);
- }
- template <typename T>
- void FastJsonEncoder <T>::operator = (const FastJsonEncoder & Encoder)
- {
- Encoder.CopyTo (*this);
- }
|