#pragma once #include "Iterator.tlh" #pragma warning (disable:4267) // warning C4267 : “参数”: 从“size_t”转换到“int”,可能丢失数据 #include "WString.hpp" #include "FastJsonEncoder.tlh" //----------------------------------------------------------------------------- // FastJsonEncoder //----------------------------------------------------------------------------- template FastJsonEncoder ::FastJsonEncoder () { } template FastJsonEncoder ::FastJsonEncoder (FastJsonEncoder && Encoder) { Swap (Encoder); } template FastJsonEncoder ::FastJsonEncoder (const FastJsonEncoder & Encoder) { Encoder.CopyTo (*this); } template FastJsonEncoder ::~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 void FastJsonEncoder ::Set (tPCTSTR Key, LPCSTR strValue) { static_assert (std::is_base_of ::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 void FastJsonEncoder ::Set (tPCTSTR Key, PCWSTR strValue) { static_assert (std::is_base_of ::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 void FastJsonEncoder ::Set (tPCTSTR Key, const DString & strTmp) { // static_assert (std::is_base_of ::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 void FastJsonEncoder ::Set (tPCTSTR Key, const WString & strTmp) { static_assert (std::is_base_of ::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 void FastJsonEncoder ::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 void FastJsonEncoder ::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 void FastJsonEncoder ::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 void FastJsonEncoder ::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 void FastJsonEncoder ::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 void FastJsonEncoder ::Set (tPCTSTR Key, void * ptr) { return Set (Key, (UINT_PTR) ptr); } template void FastJsonEncoder ::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 void FastJsonEncoder ::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 void FastJsonEncoder ::Set (tPCTSTR Key, const std::vector & 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 void FastJsonEncoder ::Set (tPCTSTR Key, const std::vector & 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 void FastJsonEncoder ::Set (tPCTSTR Key, const std::vector & 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 T FastJsonEncoder ::ToStringOf (const std::vector & 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 bool FastJsonEncoder ::Recode (LPCSTR String) { static_assert (std::is_base_of ::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 bool FastJsonEncoder ::Recode (PCWSTR String) { static_assert (std::is_base_of ::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 T FastJsonEncoder ::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 T FastJsonEncoder ::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 void FastJsonEncoder ::Empty () { m_arMessage.clear (); } template void FastJsonEncoder ::CopyTo (FastJsonEncoder & toEncoder) const { // m_arMessage.CopyTo (toEncoder.m_arMessage); for (const auto msg : m_arMessage) toEncoder.m_arMessage.push_back (msg); } template void FastJsonEncoder ::Swap (FastJsonEncoder & toEncoder) { m_arMessage.swap (toEncoder.m_arMessage); } template void FastJsonEncoder ::operator = (FastJsonEncoder && Encoder) { Swap (Encoder); } template void FastJsonEncoder ::operator = (const FastJsonEncoder & Encoder) { Encoder.CopyTo (*this); }