#pragma once #include class DString; class WString; //----------------------------------------------------------------------------- // FastJsonEncoder //----------------------------------------------------------------------------- template class FastJsonEncoder { protected: typedef const char * tPCTSTR; public: FastJsonEncoder (); FastJsonEncoder (const FastJsonEncoder & Encoder); FastJsonEncoder (FastJsonEncoder && Encoder); virtual ~FastJsonEncoder (); public: void Set (tPCTSTR Key, LPCSTR strValue); void Set (tPCTSTR Key, PCWSTR strValue); void Set (tPCTSTR Key, const DString & Value); void Set (tPCTSTR Key, const WString & Value); void Set (tPCTSTR Key, __int64 Value); void Set (tPCTSTR Key, UINT_PTR Value); void Set (tPCTSTR Key, DWORD Value); void Set (tPCTSTR Key, int Value); void Set (tPCTSTR Key, bool Value); void Set (tPCTSTR Key, void * ptr); void Set (tPCTSTR Key, float Value); void Set (tPCTSTR Key, double Value); void Set (tPCTSTR Key, const std::vector & arValue); void Set (tPCTSTR Key, const std::vector & arValue); void Set (tPCTSTR Key, const std::vector & arValue); public: // 直接把数组转换成 JSON, 格式为 [ {...}, {...} ] static T ToStringOf (const std::vector & arValue); public: bool Recode (LPCSTR String); bool Recode (PCWSTR String); public: T ToString () const; T ToStringOfElement () const; operator T () const { return ToString (); } void operator = (FastJsonEncoder && Encoder); void operator = (const FastJsonEncoder & Encoder); void CopyTo (FastJsonEncoder & toEncoder) const; void Swap (FastJsonEncoder & toEncoder); public: void Empty (); public: FastJsonEncoder & Encode (tPCTSTR Key, const T & value) { Set (Key, value); return (*this); } template FastJsonEncoder & Encode (tPCTSTR Key, TT value) { Set (Key, value); return (*this); } protected: std::vector m_arMessage; protected: class Proxy { public: inline Proxy (FastJsonEncoder & Encoder, tPCTSTR Key) : m_Encoder (Encoder), m_Key (Key) { } inline Proxy & operator = (LPCSTR Value) { static_assert (std::is_base_of ::value, "T must be derived from DString"); m_Encoder.Set (m_Key, Value); return (*this); } inline Proxy & operator = (PCWSTR Value) { static_assert (std::is_base_of ::value, "T must be derived from WString"); m_Encoder.Set (m_Key, Value); return (*this); } inline Proxy & operator = (const DString & Value) { m_Encoder.Set (m_Key, Value); return (*this); } inline Proxy & operator = (const WString & Value) { m_Encoder.Set (m_Key, Value); return (*this); } inline Proxy & operator = (__int64 Value) { m_Encoder.Set (m_Key, Value); return (*this); } inline Proxy & operator = (UINT_PTR Value) { m_Encoder.Set (m_Key, Value); return (*this); } inline Proxy & operator = (DWORD Value) { m_Encoder.Set (m_Key, Value); return (*this); } inline Proxy & operator = (int Value) { m_Encoder.Set (m_Key, Value); return (*this); } inline Proxy & operator = (bool Value) { m_Encoder.Set (m_Key, Value); return (*this); } inline Proxy & operator = (void * ptr) { m_Encoder.Set (m_Key, ptr); return (*this); } inline Proxy & operator = (float Value) { m_Encoder.Set (m_Key, Value); return (*this); } inline Proxy & operator = (double Value) { m_Encoder.Set (m_Key, Value); return (*this); } inline Proxy & operator = (std::vector & arValue) { m_Encoder.Set (m_Key, arValue); return (*this); } inline Proxy & operator = (std::vector & arValue) { m_Encoder.Set (m_Key, arValue); return (*this); } inline Proxy & operator = (std::vector & arValue) { m_Encoder.Set (m_Key, arValue); return (*this); } protected: tPCTSTR m_Key; FastJsonEncoder & m_Encoder; }; public: Proxy operator [] (tPCTSTR Key) { return Proxy (*this, Key); } Proxy operator () (tPCTSTR Key) { return Proxy (*this, Key); } };