Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit fcbe463

Browse files
committedMay 25, 2021
Added support for char, unsigned char, short, unsigned short and unsigned int.
1 parent ab80771 commit fcbe463

File tree

2 files changed

+118
-7
lines changed

2 files changed

+118
-7
lines changed
 

‎src/JSONVar.cpp

Lines changed: 99 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,42 @@ JSONVar::JSONVar(bool b) :
3333
*this = b;
3434
}
3535

36+
JSONVar::JSONVar (char i) :
37+
JSONVar ()
38+
{
39+
*this = i;
40+
}
41+
42+
JSONVar::JSONVar (unsigned char i) :
43+
JSONVar ()
44+
{
45+
*this = i;
46+
}
47+
48+
JSONVar::JSONVar (short i) :
49+
JSONVar ()
50+
{
51+
*this = i;
52+
}
53+
54+
JSONVar::JSONVar (unsigned short i) :
55+
JSONVar ()
56+
{
57+
*this = i;
58+
}
59+
3660
JSONVar::JSONVar(int i) :
3761
JSONVar()
3862
{
3963
*this = i;
4064
}
4165

66+
JSONVar::JSONVar (unsigned int i) :
67+
JSONVar ()
68+
{
69+
*this = i;
70+
}
71+
4272
JSONVar::JSONVar(long l) :
4373
JSONVar()
4474
{
@@ -132,14 +162,44 @@ JSONVar::operator bool() const
132162
return cJSON_IsBool(_json) && cJSON_IsTrue(_json);
133163
}
134164

135-
JSONVar::operator int() const
165+
JSONVar::operator char () const
166+
{
167+
return cJSON_IsNumber (_json) ? _json->valueint : 0;
168+
}
169+
170+
JSONVar::operator unsigned char () const
171+
{
172+
return cJSON_IsNumber (_json) ? _json->valueint : 0;
173+
}
174+
175+
JSONVar::operator short () const
176+
{
177+
return cJSON_IsNumber (_json) ? _json->valueint : 0;
178+
}
179+
180+
JSONVar::operator unsigned short () const
181+
{
182+
return cJSON_IsNumber (_json) ? _json->valueint : 0;
183+
}
184+
185+
JSONVar::operator int () const
136186
{
137-
return cJSON_IsNumber(_json) ? _json->valueint : 0;
187+
return cJSON_IsNumber (_json) ? _json->valueint : 0;
138188
}
139189

140-
JSONVar::operator long() const
190+
JSONVar::operator unsigned int () const
141191
{
142-
return cJSON_IsNumber(_json) ? _json->valueint : 0;
192+
return cJSON_IsNumber (_json) ? _json->valueint : 0;
193+
}
194+
195+
JSONVar::operator long () const
196+
{
197+
return cJSON_IsNumber (_json) ? _json->valueint : 0;
198+
}
199+
200+
JSONVar::operator unsigned long () const
201+
{
202+
return cJSON_IsNumber (_json) ? _json->valueint : 0;
143203
}
144204

145205
JSONVar::operator double() const
@@ -156,6 +216,15 @@ JSONVar::operator const char*() const
156216
return NULL;
157217
}
158218

219+
JSONVar::operator const String () const
220+
{
221+
if (cJSON_IsString (_json)) {
222+
return String(_json->valuestring);
223+
}
224+
225+
return String();
226+
}
227+
159228
void JSONVar::operator=(const JSONVar& v)
160229
{
161230
if (&v == &undefined) {
@@ -196,9 +265,34 @@ void JSONVar::operator=(bool b)
196265
replaceJson(b ? cJSON_CreateTrue() : cJSON_CreateFalse());
197266
}
198267

268+
void JSONVar::operator=(char i)
269+
{
270+
replaceJson (cJSON_CreateNumber (i));
271+
}
272+
273+
void JSONVar::operator=(unsigned char i)
274+
{
275+
replaceJson (cJSON_CreateNumber (i));
276+
}
277+
278+
void JSONVar::operator=(short i)
279+
{
280+
replaceJson (cJSON_CreateNumber (i));
281+
}
282+
283+
void JSONVar::operator=(unsigned short i)
284+
{
285+
replaceJson (cJSON_CreateNumber (i));
286+
}
287+
199288
void JSONVar::operator=(int i)
200289
{
201-
replaceJson(cJSON_CreateNumber(i));
290+
replaceJson (cJSON_CreateNumber (i));
291+
}
292+
293+
void JSONVar::operator=(unsigned int i)
294+
{
295+
replaceJson (cJSON_CreateNumber (i));
202296
}
203297

204298
void JSONVar::operator=(long l)

‎src/JSONVar.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ class JSONVar : public Printable {
3131
public:
3232
JSONVar();
3333
JSONVar(bool b);
34+
JSONVar(char i);
35+
JSONVar(unsigned char i);
36+
JSONVar(short i);
37+
JSONVar(unsigned short i);
3438
JSONVar(int i);
39+
JSONVar(unsigned int i);
3540
JSONVar(long l);
3641
JSONVar(unsigned long ul);
3742
JSONVar(double d);
@@ -47,17 +52,29 @@ class JSONVar : public Printable {
4752
virtual size_t printTo(Print& p) const;
4853

4954
operator bool() const;
55+
operator char() const;
56+
operator unsigned char() const;
57+
operator short() const;
58+
operator unsigned short() const;
5059
operator int() const;
51-
operator long() const;
60+
operator unsigned int() const;
61+
operator long () const;
62+
operator unsigned long () const;
5263
operator double() const;
53-
operator const char*() const;
64+
operator const char* () const;
65+
operator const String () const;
5466

5567
void operator=(const JSONVar& v);
5668
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
5769
JSONVar& operator=(JSONVar&& v);
5870
#endif
5971
void operator=(bool b);
72+
void operator=(char i);
73+
void operator=(unsigned char i);
74+
void operator=(short i);
75+
void operator=(unsigned short i);
6076
void operator=(int i);
77+
void operator=(unsigned int i);
6178
void operator=(long l);
6279
void operator=(unsigned long ul);
6380
void operator=(double d);

0 commit comments

Comments
 (0)
Please sign in to comment.