From fcbe4634e00d16f2e2437511f10b98785801e8dc Mon Sep 17 00:00:00 2001 From: Martin Mueller Date: Tue, 25 May 2021 07:47:51 -0400 Subject: [PATCH] Added support for char, unsigned char, short, unsigned short and unsigned int. --- src/JSONVar.cpp | 104 +++++++++++++++++++++++++++++++++++++++++++++--- src/JSONVar.h | 21 +++++++++- 2 files changed, 118 insertions(+), 7 deletions(-) diff --git a/src/JSONVar.cpp b/src/JSONVar.cpp index 247fcc3..4f5ce68 100644 --- a/src/JSONVar.cpp +++ b/src/JSONVar.cpp @@ -33,12 +33,42 @@ JSONVar::JSONVar(bool b) : *this = b; } +JSONVar::JSONVar (char i) : + JSONVar () +{ + *this = i; +} + +JSONVar::JSONVar (unsigned char i) : + JSONVar () +{ + *this = i; +} + +JSONVar::JSONVar (short i) : + JSONVar () +{ + *this = i; +} + +JSONVar::JSONVar (unsigned short i) : + JSONVar () +{ + *this = i; +} + JSONVar::JSONVar(int i) : JSONVar() { *this = i; } +JSONVar::JSONVar (unsigned int i) : + JSONVar () +{ + *this = i; +} + JSONVar::JSONVar(long l) : JSONVar() { @@ -132,14 +162,44 @@ JSONVar::operator bool() const return cJSON_IsBool(_json) && cJSON_IsTrue(_json); } -JSONVar::operator int() const +JSONVar::operator char () const +{ + return cJSON_IsNumber (_json) ? _json->valueint : 0; +} + +JSONVar::operator unsigned char () const +{ + return cJSON_IsNumber (_json) ? _json->valueint : 0; +} + +JSONVar::operator short () const +{ + return cJSON_IsNumber (_json) ? _json->valueint : 0; +} + +JSONVar::operator unsigned short () const +{ + return cJSON_IsNumber (_json) ? _json->valueint : 0; +} + +JSONVar::operator int () const { - return cJSON_IsNumber(_json) ? _json->valueint : 0; + return cJSON_IsNumber (_json) ? _json->valueint : 0; } -JSONVar::operator long() const +JSONVar::operator unsigned int () const { - return cJSON_IsNumber(_json) ? _json->valueint : 0; + return cJSON_IsNumber (_json) ? _json->valueint : 0; +} + +JSONVar::operator long () const +{ + return cJSON_IsNumber (_json) ? _json->valueint : 0; +} + +JSONVar::operator unsigned long () const +{ + return cJSON_IsNumber (_json) ? _json->valueint : 0; } JSONVar::operator double() const @@ -156,6 +216,15 @@ JSONVar::operator const char*() const return NULL; } +JSONVar::operator const String () const +{ + if (cJSON_IsString (_json)) { + return String(_json->valuestring); + } + + return String(); +} + void JSONVar::operator=(const JSONVar& v) { if (&v == &undefined) { @@ -196,9 +265,34 @@ void JSONVar::operator=(bool b) replaceJson(b ? cJSON_CreateTrue() : cJSON_CreateFalse()); } +void JSONVar::operator=(char i) +{ + replaceJson (cJSON_CreateNumber (i)); +} + +void JSONVar::operator=(unsigned char i) +{ + replaceJson (cJSON_CreateNumber (i)); +} + +void JSONVar::operator=(short i) +{ + replaceJson (cJSON_CreateNumber (i)); +} + +void JSONVar::operator=(unsigned short i) +{ + replaceJson (cJSON_CreateNumber (i)); +} + void JSONVar::operator=(int i) { - replaceJson(cJSON_CreateNumber(i)); + replaceJson (cJSON_CreateNumber (i)); +} + +void JSONVar::operator=(unsigned int i) +{ + replaceJson (cJSON_CreateNumber (i)); } void JSONVar::operator=(long l) diff --git a/src/JSONVar.h b/src/JSONVar.h index 58a7213..c240f7d 100644 --- a/src/JSONVar.h +++ b/src/JSONVar.h @@ -31,7 +31,12 @@ class JSONVar : public Printable { public: JSONVar(); JSONVar(bool b); + JSONVar(char i); + JSONVar(unsigned char i); + JSONVar(short i); + JSONVar(unsigned short i); JSONVar(int i); + JSONVar(unsigned int i); JSONVar(long l); JSONVar(unsigned long ul); JSONVar(double d); @@ -47,17 +52,29 @@ class JSONVar : public Printable { virtual size_t printTo(Print& p) const; operator bool() const; + operator char() const; + operator unsigned char() const; + operator short() const; + operator unsigned short() const; operator int() const; - operator long() const; + operator unsigned int() const; + operator long () const; + operator unsigned long () const; operator double() const; - operator const char*() const; + operator const char* () const; + operator const String () const; void operator=(const JSONVar& v); #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) JSONVar& operator=(JSONVar&& v); #endif void operator=(bool b); + void operator=(char i); + void operator=(unsigned char i); + void operator=(short i); + void operator=(unsigned short i); void operator=(int i); + void operator=(unsigned int i); void operator=(long l); void operator=(unsigned long ul); void operator=(double d);