IoTeX client
json_helper.h
1#ifndef JSON_HELPER_H
2#define JSON_HELPER_H
3
4#include "extern/cjson/cJSON.h"
5#include "helpers/client_helper.h"
6#include "helpers/reflection_helper.h"
7#include "IoTeXResultCodes.h"
8#include <string.h>
9#include <string>
10
11namespace iotex
12{
13namespace json
14{
16{
17 const reflection::CppType type;
18 const IotexString name;
19
20 JsonProp(reflection::CppType type, IotexString name) :
21 type(type), name(name), _initialized(false)
22 {
23 }
24
25 // Move constructor
26 JsonProp(JsonProp&& orig) noexcept : type(orig.type), name(orig.name)
27 {
28 _initialized = orig._initialized;
29 _bytesCount = orig._bytesCount;
30 if(_initialized)
31 {
32 _value.object = orig._value.object;
33 orig._value.object = nullptr;
34 }
35 }
36
37 /* Setters */
38 void setValue(const char* value)
39 {
40 if(_initialized && type == reflection::CppType::C_STRING)
41 {
42 free(_value.c_string);
43 }
44 _value.c_string = (char*)malloc(strlen(value));
45 memcpy(_value.c_string, value, strlen(value));
46 _initialized = true;
47 }
48
49 void setValue(const IotexString& value)
50 {
51 if(_initialized && type == reflection::CppType::STRING)
52 {
53 free(_value.string);
54 }
55 _value.string = new IotexString(value);
56 _initialized = true;
57 }
58
59 void setValue(uint64_t value)
60 {
61 _initialized = true;
62 _value.uint64 = value;
63 }
64
65 void setValue(bool value)
66 {
67 _initialized = true;
68 _value.boolean = value;
69 }
70
71 /* Getters */
72 const char* getValueCString()
73 {
74 return _initialized ? _value.c_string : nullptr;
75 }
76
77 const IotexString* getValueString()
78 {
79 return _initialized ? _value.string : nullptr;
80 }
81
82 uint64_t getValueUint64()
83 {
84 return _value.uint64;
85 }
86
87 bool getValueBool()
88 {
89 return _value.boolean;
90 }
91
92 const uint8_t* getValueBytes()
93 {
94 return _value.bytes;
95 }
96
97 ~JsonProp()
98 {
99 if(_initialized && isHeapAllocated())
100 {
101 free(_value.object);
102 }
103 }
104
105 uint32_t getBytesCount()
106 {
107 return _bytesCount;
108 }
109
110 void ToString(IotexString& out, bool enclose = false)
111 {
112 out = "";
113 if(enclose)
114 out += "{";
115 // Name
116 out += "\"" + name + "\":";
117 // Value
118 appendValueToString(out);
119 if(enclose)
120 out += "}";
121 }
122
123 protected:
124 bool _initialized;
125 union
126 {
127 uint8_t uint8;
128 uint16_t uint16;
129 uint32_t uint32;
130 uint64_t uint64;
131 bool boolean;
132 char* c_string;
133 IotexString* string;
134 void* object;
135 uint8_t* bytes;
136 } _value;
137
138 uint32_t _bytesCount; // bytes count, only used for bytes type
139
140 bool isHeapAllocated()
141 {
142 if(type == reflection::CppType::C_STRING || type == reflection::CppType::STRING ||
143 type == reflection::CppType::OBJECT || type == reflection::CppType::BYTES)
144 {
145 return true;
146 }
147 else
148 return false;
149 }
150
151 virtual void appendValueToString(IotexString& out)
152 {
153 // TODO Make pure abstract
154 }
155};
156
162{
163 StringJsonProp(IotexString name) : JsonProp(reflection::CppType::STRING, name)
164 {
165 }
166
167 StringJsonProp(StringJsonProp&& orig) noexcept : JsonProp(std::move(orig))
168 {
169 }
170
171 const IotexString* getValue()
172 {
173 return _value.string;
174 }
175
176 private:
177 void appendValueToString(IotexString& out) override
178 {
179 out += "\"";
180 out += _value.string->c_str();
181 out += "\"";
182 }
183};
184
186{
187 Uint8JsonProp(IotexString name) : JsonProp(reflection::CppType::UINT8, name)
188 {
189 }
190
191 Uint8JsonProp(Uint8JsonProp&& orig) noexcept : JsonProp(std::move(orig))
192 {
193 }
194
195 uint8_t getValue()
196 {
197 return _value.uint8;
198 }
199
200 private:
201 void appendValueToString(IotexString& out) override
202 {
203 char buf[4] = {0};
204 sprintf(buf, "%u", _value.uint8);
205 out += buf;
206 }
207};
208
210{
211 Uint64JsonProp(IotexString name) : JsonProp(reflection::CppType::UINT64, name)
212 {
213 }
214
215 Uint64JsonProp(Uint64JsonProp&& orig) noexcept : JsonProp(std::move(orig))
216 {
217 }
218
219 uint64_t getValue()
220 {
221 return _value.uint64;
222 }
223
224 private:
225 void appendValueToString(IotexString& out) override
226 {
227 char buf[21] = {0};
228 sprintf(buf, "%lu", _value.uint64);
229 out += buf;
230 }
231};
232
234{
235 BytesJsonProp(IotexString name) : JsonProp(reflection::CppType::BYTES, name)
236 {
237 }
238
239 const uint8_t* getValue()
240 {
241 return _value.bytes;
242 }
243};
244
246{
247 BoolJsonProp(IotexString name) : JsonProp(reflection::CppType::BOOLEAN, name)
248 {
249 }
250
251 BoolJsonProp(BoolJsonProp&& orig) noexcept : JsonProp(std::move(orig))
252 {
253 }
254
255 bool getValue()
256 {
257 return _value.boolean;
258 }
259
260 private:
261 void appendValueToString(IotexString& out)
262 {
263 out += _value.boolean ? "true" : "false";
264 }
265};
266
268{
269 ArrayJsonProp(IotexString name) : JsonProp(reflection::CppType::ARRAY, name)
270 {
271 }
272};
273
274iotex::ResultCode SetValueFromJsonObject(const cJSON* json, reflection::CppType type, void* pData,
275 size_t max_size = 0);
276} // namespace json
277}; // namespace iotex
278
279#endif
Definition: abi.h:12
Definition: json_helper.h:268
Definition: json_helper.h:246
Definition: json_helper.h:234
Definition: json_helper.h:16
Definition: json_helper.h:162
Definition: json_helper.h:210
Definition: json_helper.h:186