EasyJSONEncoder.hpp 3.0 KB

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