From 07033185ad8c16e6c0b275cd2efec90e04ddcd15 Mon Sep 17 00:00:00 2001 From: Dirk-Willem van Gulik Date: Thu, 27 Oct 2022 16:31:24 +0200 Subject: [PATCH 1/3] Add unsigned long support; addresses issues #39 --- examples/JSONObject/JSONObject.ino | 9 ++++++++- src/JSONVar.cpp | 5 +++++ src/JSONVar.h | 1 + 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/examples/JSONObject/JSONObject.ino b/examples/JSONObject/JSONObject.ino index 786185a..9291588 100644 --- a/examples/JSONObject/JSONObject.ino +++ b/examples/JSONObject/JSONObject.ino @@ -78,7 +78,14 @@ 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"]; + long x2 = myObject["x2"]; + unsigned long x3 = myObject["x3"]; 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; From 0bb22497cf13c27ba29ca8075e0be64c1c524bf9 Mon Sep 17 00:00:00 2001 From: Dirk-Willem van Gulik Date: Thu, 27 Oct 2022 16:33:39 +0200 Subject: [PATCH 2/3] Better test case added --- examples/JSONObject/JSONObject.ino | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/examples/JSONObject/JSONObject.ino b/examples/JSONObject/JSONObject.ino index 9291588..6298727 100644 --- a/examples/JSONObject/JSONObject.ino +++ b/examples/JSONObject/JSONObject.ino @@ -84,8 +84,13 @@ void demoCreation() { 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()); @@ -127,4 +132,4 @@ void demoCreation() { Serial.print("myObject = "); Serial.println(myObject); -} +} \ No newline at end of file From e8f2289a75a0b90ce31cfde658ff457ca15082c6 Mon Sep 17 00:00:00 2001 From: Dirk-Willem van Gulik Date: Thu, 27 Oct 2022 16:38:01 +0200 Subject: [PATCH 3/3] Nano platform needs assert.h explicitly. Added. --- examples/JSONObject/JSONObject.ino | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/JSONObject/JSONObject.ino b/examples/JSONObject/JSONObject.ino index 6298727..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\"}"; @@ -132,4 +133,4 @@ void demoCreation() { Serial.print("myObject = "); Serial.println(myObject); -} \ No newline at end of file +}