From 9f3ce18f0627460925ba0807003ae5903cedf6b8 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Fri, 30 Oct 2015 22:29:47 +0100 Subject: [PATCH] Add overload `JsonObjectSuscript::set(value, decimals)` (issue #143) --- CHANGELOG.md | 5 +++ README.md | 38 ++++++++++++--------- include/ArduinoJson/JsonObjectSubscript.hpp | 5 +++ library.properties | 2 +- test/JsonObject_Set_Tests.cpp | 8 +++++ test/JsonObject_Subscript_Tests.cpp | 8 +++++ 6 files changed, 48 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f230af94..713ff4b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ ArduinoJson: change log ======================= +v5.0.5 +------ + +* Add overload `JsonObjectSuscript::set(value, decimals)` (issue #143) + v5.0.4 ------ diff --git a/README.md b/README.md index b4e72663..ea42e8a9 100644 --- a/README.md +++ b/README.md @@ -34,32 +34,36 @@ Quick start #### Decoding / Parsing - char json[] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}"; +```c++ +char json[] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}"; - StaticJsonBuffer<200> jsonBuffer; +StaticJsonBuffer<200> jsonBuffer; - JsonObject& root = jsonBuffer.parseObject(json); +JsonObject& root = jsonBuffer.parseObject(json); - const char* sensor = root["sensor"]; - long time = root["time"]; - double latitude = root["data"][0]; - double longitude = root["data"][1]; +const char* sensor = root["sensor"]; +long time = root["time"]; +double latitude = root["data"][0]; +double longitude = root["data"][1]; +``` #### Encoding / Generating - StaticJsonBuffer<200> jsonBuffer; +```c++ +StaticJsonBuffer<200> jsonBuffer; - JsonObject& root = jsonBuffer.createObject(); - root["sensor"] = "gps"; - root["time"] = 1351824120; +JsonObject& root = jsonBuffer.createObject(); +root["sensor"] = "gps"; +root["time"] = 1351824120; - JsonArray& data = root.createNestedArray("data"); - data.add(48.756080, 6); // 6 is the number of decimals to print - data.add(2.302038, 6); // if not specified, 2 digits are printed +JsonArray& data = root.createNestedArray("data"); +data.add(48.756080, 6); // 6 is the number of decimals to print +data.add(2.302038, 6); // if not specified, 2 digits are printed - root.printTo(Serial); - // This prints: - // {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]} +root.printTo(Serial); +// This prints: +// {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]} +`` Documentation diff --git a/include/ArduinoJson/JsonObjectSubscript.hpp b/include/ArduinoJson/JsonObjectSubscript.hpp index d135c217..88c5b942 100644 --- a/include/ArduinoJson/JsonObjectSubscript.hpp +++ b/include/ArduinoJson/JsonObjectSubscript.hpp @@ -54,6 +54,11 @@ class JsonObjectSubscript return _object.set(_key, value); } + template + FORCE_INLINE bool set(TValue value, uint8_t decimals) { + return _object.set(_key, value, decimals); + } + FORCE_INLINE JsonVariant get() { return _object.get(_key); } void writeTo(Internals::JsonWriter& writer) const { diff --git a/library.properties b/library.properties index 32edc006..066d1c26 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=ArduinoJson -version=5.0.4 +version=5.0.5 author=Benoit Blanchon maintainer=Benoit Blanchon sentence=An efficient and elegant JSON library for Arduino. diff --git a/test/JsonObject_Set_Tests.cpp b/test/JsonObject_Set_Tests.cpp index c2c617ca..cb08c9ef 100644 --- a/test/JsonObject_Set_Tests.cpp +++ b/test/JsonObject_Set_Tests.cpp @@ -46,6 +46,14 @@ TEST_(StoreDouble) { EXPECT_FALSE(_object["hello"].is()); } +TEST_(StoreDoubleWithDigits) { + _object.set("hello", 123.45, 2); + + EXPECT_EQ(123.45, _object["hello"].as()); + EXPECT_TRUE(_object["hello"].is()); + EXPECT_FALSE(_object["hello"].is()); +} + TEST_(StoreBoolean) { _object.set("hello", true); diff --git a/test/JsonObject_Subscript_Tests.cpp b/test/JsonObject_Subscript_Tests.cpp index 46dc016b..b9abfa6f 100644 --- a/test/JsonObject_Subscript_Tests.cpp +++ b/test/JsonObject_Subscript_Tests.cpp @@ -46,6 +46,14 @@ TEST_(StoreDouble) { EXPECT_EQ(123.45, _object["hello"].as()); } +TEST_(StoreDoubleWithDigits) { + _object["hello"].set(123.45, 2); + + EXPECT_TRUE(_object["hello"].is()); + EXPECT_FALSE(_object["hello"].is()); + EXPECT_EQ(123.45, _object["hello"].as()); +} + TEST_(StoreBoolean) { _object["hello"] = true;