EasyJSONEncoder.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #pragma once
  2. #include <vector>
  3. #include <string>
  4. class EasyJSONEncoder
  5. {
  6. using LPCTSTR = const char *;
  7. public:
  8. EasyJSONEncoder ();
  9. EasyJSONEncoder (const EasyJSONEncoder & Encoder);
  10. virtual ~EasyJSONEncoder ();
  11. void Set (std::string Key, LPCTSTR Value);
  12. void Set (std::string Key, const std::string & Value);
  13. void Set (std::string Key, __int64 Value);
  14. void Set (std::string Key, UINT_PTR Value);
  15. void Set (std::string Key, DWORD Value);
  16. void Set (std::string Key, int Value);
  17. void Set (std::string Key, bool Value);
  18. void Set (std::string Key, float Value);
  19. void Set (std::string Key, double Value);
  20. void Set (std::string Key, const std::vector <std::string> & arValue);
  21. void Set (std::string Key, const std::vector <int> & arValue);
  22. void Set (std::string Key, const std::vector <EasyJSONEncoder> & arValue);
  23. bool Recode (std::string String);
  24. std::string ToString () const;
  25. std::string ToStringOfElement () const;
  26. operator std::string () const { return ToString (); }
  27. void operator = (const EasyJSONEncoder & Encoder);
  28. void CopyTo (EasyJSONEncoder & toEncoder) const;
  29. void Empty ();
  30. template <typename T>
  31. EasyJSONEncoder & Encode (LPCTSTR Key, T value)
  32. {
  33. Set (Key, value);
  34. return (*this);
  35. }
  36. protected:
  37. std::vector <std::string> m_arMessage;
  38. protected:
  39. class Proxy
  40. {
  41. public:
  42. inline Proxy (EasyJSONEncoder & Encoder, LPCTSTR Key) :
  43. m_Encoder (Encoder),
  44. m_Key (Key)
  45. {
  46. }
  47. inline Proxy & operator = (LPCTSTR Value)
  48. {
  49. m_Encoder.Set (m_Key, Value);
  50. return (*this);
  51. }
  52. inline Proxy & operator = (const std::string & Value)
  53. {
  54. m_Encoder.Set (m_Key, Value);
  55. return (*this);
  56. }
  57. inline Proxy & operator = (__int64 Value)
  58. {
  59. m_Encoder.Set (m_Key, Value);
  60. return (*this);
  61. }
  62. inline Proxy & operator = (UINT_PTR Value)
  63. {
  64. m_Encoder.Set (m_Key, Value);
  65. return (*this);
  66. }
  67. inline Proxy & operator = (DWORD Value)
  68. {
  69. m_Encoder.Set (m_Key, Value);
  70. return (*this);
  71. }
  72. inline Proxy & operator = (int Value)
  73. {
  74. m_Encoder.Set (m_Key, Value);
  75. return (*this);
  76. }
  77. inline Proxy & operator = (bool Value)
  78. {
  79. m_Encoder.Set (m_Key, Value);
  80. return (*this);
  81. }
  82. inline Proxy & operator = (float Value)
  83. {
  84. m_Encoder.Set (m_Key, Value);
  85. return (*this);
  86. }
  87. inline Proxy & operator = (double Value)
  88. {
  89. m_Encoder.Set (m_Key, Value);
  90. return (*this);
  91. }
  92. inline Proxy & operator = (std::vector <std::string> & arValue)
  93. {
  94. m_Encoder.Set (m_Key, arValue);
  95. return (*this);
  96. }
  97. inline Proxy & operator = (std::vector <int> & arValue)
  98. {
  99. m_Encoder.Set (m_Key, arValue);
  100. return (*this);
  101. }
  102. inline Proxy & operator = (std::vector <EasyJSONEncoder> & arValue)
  103. {
  104. m_Encoder.Set (m_Key, arValue);
  105. return (*this);
  106. }
  107. protected:
  108. LPCTSTR m_Key;
  109. EasyJSONEncoder & m_Encoder;
  110. };
  111. public:
  112. Proxy operator [] (LPCTSTR Key)
  113. {
  114. return Proxy (*this, Key);
  115. }
  116. Proxy operator () (LPCTSTR Key)
  117. {
  118. return Proxy (*this, Key);
  119. }
  120. };
  121. /*//
  122. #define JSON EasyJSONEncoder ()
  123. arg->SetAck (JSON
  124. .Encode ("A", "a")
  125. .Encode ("B", 22)
  126. .Encode ("C", true)
  127. .ToString ());
  128. *///