Add overload JsonObjectSuscript::set(value, decimals) (issue #143)

This commit is contained in:
Benoit Blanchon
2015-10-30 22:29:47 +01:00
parent b9e3255c9e
commit 9f3ce18f06
6 changed files with 48 additions and 18 deletions

View File

@ -1,6 +1,11 @@
ArduinoJson: change log ArduinoJson: change log
======================= =======================
v5.0.5
------
* Add overload `JsonObjectSuscript::set(value, decimals)` (issue #143)
v5.0.4 v5.0.4
------ ------

View File

@ -34,32 +34,36 @@ Quick start
#### Decoding / Parsing #### 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"]; const char* sensor = root["sensor"];
long time = root["time"]; long time = root["time"];
double latitude = root["data"][0]; double latitude = root["data"][0];
double longitude = root["data"][1]; double longitude = root["data"][1];
```
#### Encoding / Generating #### Encoding / Generating
StaticJsonBuffer<200> jsonBuffer; ```c++
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject(); JsonObject& root = jsonBuffer.createObject();
root["sensor"] = "gps"; root["sensor"] = "gps";
root["time"] = 1351824120; root["time"] = 1351824120;
JsonArray& data = root.createNestedArray("data"); JsonArray& data = root.createNestedArray("data");
data.add(48.756080, 6); // 6 is the number of decimals to print 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 data.add(2.302038, 6); // if not specified, 2 digits are printed
root.printTo(Serial); root.printTo(Serial);
// This prints: // This prints:
// {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]} // {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
``
Documentation Documentation

View File

@ -54,6 +54,11 @@ class JsonObjectSubscript
return _object.set(_key, value); return _object.set(_key, value);
} }
template <typename TValue>
FORCE_INLINE bool set(TValue value, uint8_t decimals) {
return _object.set(_key, value, decimals);
}
FORCE_INLINE JsonVariant get() { return _object.get(_key); } FORCE_INLINE JsonVariant get() { return _object.get(_key); }
void writeTo(Internals::JsonWriter& writer) const { void writeTo(Internals::JsonWriter& writer) const {

View File

@ -1,5 +1,5 @@
name=ArduinoJson name=ArduinoJson
version=5.0.4 version=5.0.5
author=Benoit Blanchon <blog.benoitblanchon.fr> author=Benoit Blanchon <blog.benoitblanchon.fr>
maintainer=Benoit Blanchon <blog.benoitblanchon.fr> maintainer=Benoit Blanchon <blog.benoitblanchon.fr>
sentence=An efficient and elegant JSON library for Arduino. sentence=An efficient and elegant JSON library for Arduino.

View File

@ -46,6 +46,14 @@ TEST_(StoreDouble) {
EXPECT_FALSE(_object["hello"].is<long>()); EXPECT_FALSE(_object["hello"].is<long>());
} }
TEST_(StoreDoubleWithDigits) {
_object.set("hello", 123.45, 2);
EXPECT_EQ(123.45, _object["hello"].as<double>());
EXPECT_TRUE(_object["hello"].is<double>());
EXPECT_FALSE(_object["hello"].is<long>());
}
TEST_(StoreBoolean) { TEST_(StoreBoolean) {
_object.set("hello", true); _object.set("hello", true);

View File

@ -46,6 +46,14 @@ TEST_(StoreDouble) {
EXPECT_EQ(123.45, _object["hello"].as<double>()); EXPECT_EQ(123.45, _object["hello"].as<double>());
} }
TEST_(StoreDoubleWithDigits) {
_object["hello"].set(123.45, 2);
EXPECT_TRUE(_object["hello"].is<double>());
EXPECT_FALSE(_object["hello"].is<long>());
EXPECT_EQ(123.45, _object["hello"].as<double>());
}
TEST_(StoreBoolean) { TEST_(StoreBoolean) {
_object["hello"] = true; _object["hello"] = true;