FastJsonEncoder.tlh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #pragma once
  2. #include <vector>
  3. class DString;
  4. class WString;
  5. //-----------------------------------------------------------------------------
  6. // FastJsonEncoder
  7. //-----------------------------------------------------------------------------
  8. template <typename T>
  9. class FastJsonEncoder
  10. {
  11. protected:
  12. typedef const char * tPCTSTR;
  13. public:
  14. FastJsonEncoder ();
  15. FastJsonEncoder (const FastJsonEncoder & Encoder);
  16. FastJsonEncoder (FastJsonEncoder && Encoder);
  17. virtual ~FastJsonEncoder ();
  18. public:
  19. void Set (tPCTSTR Key, LPCSTR strValue);
  20. void Set (tPCTSTR Key, PCWSTR strValue);
  21. void Set (tPCTSTR Key, const DString & Value);
  22. void Set (tPCTSTR Key, const WString & Value);
  23. void Set (tPCTSTR Key, __int64 Value);
  24. void Set (tPCTSTR Key, UINT_PTR Value);
  25. void Set (tPCTSTR Key, DWORD Value);
  26. void Set (tPCTSTR Key, int Value);
  27. void Set (tPCTSTR Key, bool Value);
  28. void Set (tPCTSTR Key, void * ptr);
  29. void Set (tPCTSTR Key, float Value);
  30. void Set (tPCTSTR Key, double Value);
  31. void Set (tPCTSTR Key, const std::vector <T> & arValue);
  32. void Set (tPCTSTR Key, const std::vector <int> & arValue);
  33. void Set (tPCTSTR Key, const std::vector <FastJsonEncoder> & arValue);
  34. public:
  35. // 直接把数组转换成 JSON, 格式为 [ {...}, {...} ]
  36. static T ToStringOf (const std::vector <FastJsonEncoder> & arValue);
  37. public:
  38. bool Recode (LPCSTR String);
  39. bool Recode (PCWSTR String);
  40. public:
  41. T ToString () const;
  42. T ToStringOfElement () const;
  43. operator T () const { return ToString (); }
  44. void operator = (FastJsonEncoder && Encoder);
  45. void operator = (const FastJsonEncoder & Encoder);
  46. void CopyTo (FastJsonEncoder & toEncoder) const;
  47. void Swap (FastJsonEncoder & toEncoder);
  48. public:
  49. void Empty ();
  50. public:
  51. FastJsonEncoder & Encode (tPCTSTR Key, const T & value)
  52. {
  53. Set (Key, value);
  54. return (*this);
  55. }
  56. template <typename TT>
  57. FastJsonEncoder & Encode (tPCTSTR Key, TT value)
  58. {
  59. Set (Key, value);
  60. return (*this);
  61. }
  62. protected:
  63. std::vector <T> m_arMessage;
  64. protected:
  65. class Proxy
  66. {
  67. public:
  68. inline Proxy (FastJsonEncoder & Encoder, tPCTSTR Key) :
  69. m_Encoder (Encoder),
  70. m_Key (Key)
  71. {
  72. }
  73. inline Proxy & operator = (LPCSTR Value)
  74. {
  75. static_assert (std::is_base_of <DString, T>::value, "T must be derived from DString");
  76. m_Encoder.Set (m_Key, Value);
  77. return (*this);
  78. }
  79. inline Proxy & operator = (PCWSTR Value)
  80. {
  81. static_assert (std::is_base_of <WString, T>::value, "T must be derived from WString");
  82. m_Encoder.Set (m_Key, Value);
  83. return (*this);
  84. }
  85. inline Proxy & operator = (const DString & Value)
  86. {
  87. m_Encoder.Set (m_Key, Value);
  88. return (*this);
  89. }
  90. inline Proxy & operator = (const WString & Value)
  91. {
  92. m_Encoder.Set (m_Key, Value);
  93. return (*this);
  94. }
  95. inline Proxy & operator = (__int64 Value)
  96. {
  97. m_Encoder.Set (m_Key, Value);
  98. return (*this);
  99. }
  100. inline Proxy & operator = (UINT_PTR Value)
  101. {
  102. m_Encoder.Set (m_Key, Value);
  103. return (*this);
  104. }
  105. inline Proxy & operator = (DWORD Value)
  106. {
  107. m_Encoder.Set (m_Key, Value);
  108. return (*this);
  109. }
  110. inline Proxy & operator = (int Value)
  111. {
  112. m_Encoder.Set (m_Key, Value);
  113. return (*this);
  114. }
  115. inline Proxy & operator = (bool Value)
  116. {
  117. m_Encoder.Set (m_Key, Value);
  118. return (*this);
  119. }
  120. inline Proxy & operator = (void * ptr)
  121. {
  122. m_Encoder.Set (m_Key, ptr);
  123. return (*this);
  124. }
  125. inline Proxy & operator = (float Value)
  126. {
  127. m_Encoder.Set (m_Key, Value);
  128. return (*this);
  129. }
  130. inline Proxy & operator = (double Value)
  131. {
  132. m_Encoder.Set (m_Key, Value);
  133. return (*this);
  134. }
  135. inline Proxy & operator = (std::vector <T> & arValue)
  136. {
  137. m_Encoder.Set (m_Key, arValue);
  138. return (*this);
  139. }
  140. inline Proxy & operator = (std::vector <int> & arValue)
  141. {
  142. m_Encoder.Set (m_Key, arValue);
  143. return (*this);
  144. }
  145. inline Proxy & operator = (std::vector <FastJsonEncoder> & arValue)
  146. {
  147. m_Encoder.Set (m_Key, arValue);
  148. return (*this);
  149. }
  150. protected:
  151. tPCTSTR m_Key;
  152. FastJsonEncoder & m_Encoder;
  153. };
  154. public:
  155. Proxy operator [] (tPCTSTR Key)
  156. {
  157. return Proxy (*this, Key);
  158. }
  159. Proxy operator () (tPCTSTR Key)
  160. {
  161. return Proxy (*this, Key);
  162. }
  163. };