diff --git a/examples/JSONObject/JSONObject.ino b/examples/JSONObject/JSONObject.ino index 786185a..157ab8d 100644 --- a/examples/JSONObject/JSONObject.ino +++ b/examples/JSONObject/JSONObject.ino @@ -8,6 +8,7 @@ */ #include +#include const char input[] = "{\"result\":true,\"count\":42,\"foo\":\"bar\"}"; @@ -78,7 +79,19 @@ void demoCreation() { myObject["hello"] = "world"; myObject["true"] = true; - myObject["x"] = 42; + + myObject["x1"] = (int) 42; + myObject["x2"] = (long) 42; + myObject["x3"] = (unsigned long) 42; + + int x1 = myObject["x1"]; + assert(x1 == 42); + + long x2 = myObject["x2"]; + assert(x2 == 42); + + unsigned long x3 = myObject["x3"]; + assert(x3 == 42); Serial.print("myObject.keys() = "); Serial.println(myObject.keys()); diff --git a/src/JSONVar.cpp b/src/JSONVar.cpp index 247fcc3..8adc085 100644 --- a/src/JSONVar.cpp +++ b/src/JSONVar.cpp @@ -142,6 +142,11 @@ 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 { return cJSON_IsNumber(_json) ? _json->valuedouble : NAN; diff --git a/src/JSONVar.h b/src/JSONVar.h index 58a7213..0bf71f0 100644 --- a/src/JSONVar.h +++ b/src/JSONVar.h @@ -49,6 +49,7 @@ class JSONVar : public Printable { operator bool() const; operator int() const; operator long() const; + operator unsigned long() const; operator double() const; operator const char*() const;