Don't use JsonBuffer to create or parse objects and arrays.

* Added DynamicJsonArray and StaticJsonArray
* Added DynamicJsonObject and StaticJsonObject
* Added DynamicJsonVariant and StaticJsonVariant
* Added deserializeJson()
* Removed JsonBuffer::parseArray(), parseObject() and parse()
* Removed JsonBuffer::createArray() and createObject()
This commit is contained in:
Benoit Blanchon
2018-02-26 16:05:16 +01:00
parent baf5adcf33
commit 7a2a64803a
89 changed files with 1612 additions and 1691 deletions

View File

@ -4,10 +4,6 @@
add_executable(StaticJsonBufferTests
alloc.cpp
createArray.cpp
createObject.cpp
parseArray.cpp
parseObject.cpp
size.cpp
startString.cpp
)

View File

@ -1,45 +0,0 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("StaticJsonBuffer::createArray()") {
SECTION("GrowsWithArray") {
StaticJsonBuffer<JSON_ARRAY_SIZE(2)> json;
JsonArray &array = json.createArray();
REQUIRE(JSON_ARRAY_SIZE(0) == json.size());
array.add("hello");
REQUIRE(JSON_ARRAY_SIZE(1) == json.size());
array.add("world");
REQUIRE(JSON_ARRAY_SIZE(2) == json.size());
}
SECTION("SucceedWhenBigEnough") {
StaticJsonBuffer<JSON_ARRAY_SIZE(0)> json;
JsonArray &array = json.createArray();
REQUIRE(array.success());
}
SECTION("FailsWhenTooSmall") {
StaticJsonBuffer<JSON_ARRAY_SIZE(0) - 1> json;
JsonArray &array = json.createArray();
REQUIRE_FALSE(array.success());
}
SECTION("ArrayDoesntGrowWhenFull") {
StaticJsonBuffer<JSON_ARRAY_SIZE(1)> json;
JsonArray &array = json.createArray();
array.add("hello");
array.add("world");
REQUIRE(1 == array.size());
}
}

View File

@ -1,56 +0,0 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("StaticJsonBuffer::createObject()") {
SECTION("GrowsWithObject") {
StaticJsonBuffer<JSON_OBJECT_SIZE(3)> buffer;
JsonObject &obj = buffer.createObject();
REQUIRE(JSON_OBJECT_SIZE(0) == buffer.size());
obj["hello"];
REQUIRE(JSON_OBJECT_SIZE(0) == buffer.size());
obj["hello"] = 1;
REQUIRE(JSON_OBJECT_SIZE(1) == buffer.size());
obj["world"] = 2;
REQUIRE(JSON_OBJECT_SIZE(2) == buffer.size());
obj["world"] = 3; // <- same key, should not grow
REQUIRE(JSON_OBJECT_SIZE(2) == buffer.size());
}
SECTION("SucceedWhenBigEnough") {
StaticJsonBuffer<JSON_OBJECT_SIZE(0)> buffer;
JsonObject &object = buffer.createObject();
REQUIRE(object.success());
}
SECTION("FailsWhenTooSmall") {
StaticJsonBuffer<JSON_OBJECT_SIZE(0) - 1> buffer;
JsonObject &object = buffer.createObject();
REQUIRE_FALSE(object.success());
}
SECTION("ObjectDoesntGrowWhenFull") {
StaticJsonBuffer<JSON_OBJECT_SIZE(1)> buffer;
JsonObject &obj = buffer.createObject();
obj["hello"] = 1;
obj["world"] = 2;
REQUIRE(JSON_OBJECT_SIZE(1) == buffer.size());
REQUIRE(1 == obj.size());
char json[64];
obj.printTo(json, sizeof(json));
REQUIRE(std::string("{\"hello\":1}") == json);
}
}

View File

@ -1,71 +0,0 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("StaticJsonBuffer::parseArray()") {
SECTION("TooSmallBufferForEmptyArray") {
StaticJsonBuffer<JSON_ARRAY_SIZE(0) - 1> bufferTooSmall;
char input[] = "[]";
JsonArray& arr = bufferTooSmall.parseArray(input);
REQUIRE_FALSE(arr.success());
}
SECTION("BufferOfTheRightSizeForEmptyArray") {
StaticJsonBuffer<JSON_ARRAY_SIZE(0)> bufferOfRightSize;
char input[] = "[]";
JsonArray& arr = bufferOfRightSize.parseArray(input);
REQUIRE(arr.success());
}
SECTION("TooSmallBufferForArrayWithOneValue") {
StaticJsonBuffer<JSON_ARRAY_SIZE(1) - 1> bufferTooSmall;
char input[] = "[1]";
JsonArray& arr = bufferTooSmall.parseArray(input);
REQUIRE_FALSE(arr.success());
}
SECTION("BufferOfTheRightSizeForArrayWithOneValue") {
StaticJsonBuffer<JSON_ARRAY_SIZE(1)> bufferOfRightSize;
char input[] = "[1]";
JsonArray& arr = bufferOfRightSize.parseArray(input);
REQUIRE(arr.success());
}
SECTION("TooSmallBufferForArrayWithNestedObject") {
StaticJsonBuffer<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0) - 1>
bufferTooSmall;
char input[] = "[{}]";
JsonArray& arr = bufferTooSmall.parseArray(input);
REQUIRE_FALSE(arr.success());
}
SECTION("BufferOfTheRightSizeForArrayWithNestedObject") {
StaticJsonBuffer<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0)>
bufferOfRightSize;
char input[] = "[{}]";
JsonArray& arr = bufferOfRightSize.parseArray(input);
REQUIRE(arr.success());
}
SECTION("CharPtrNull") {
REQUIRE_FALSE(
StaticJsonBuffer<100>().parseArray(static_cast<char*>(0)).success());
}
SECTION("ConstCharPtrNull") {
REQUIRE_FALSE(StaticJsonBuffer<100>()
.parseArray(static_cast<const char*>(0))
.success());
}
SECTION("CopyStringNotSpaces") {
StaticJsonBuffer<100> jsonBuffer;
jsonBuffer.parseArray(" [ \"1234567\" ] ");
REQUIRE(JSON_ARRAY_SIZE(1) + sizeof("1234567") == jsonBuffer.size());
// note we use a string of 8 bytes to be sure that the StaticJsonBuffer
// will not insert bytes to enforce alignement
}
}

View File

@ -1,62 +0,0 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("StaticJsonBuffer::parseObject()") {
SECTION("TooSmallBufferForEmptyObject") {
StaticJsonBuffer<JSON_OBJECT_SIZE(0) - 1> bufferTooSmall;
char input[] = "{}";
JsonObject& obj = bufferTooSmall.parseObject(input);
REQUIRE_FALSE(obj.success());
}
SECTION("BufferOfTheRightSizeForEmptyObject") {
StaticJsonBuffer<JSON_OBJECT_SIZE(0)> bufferOfRightSize;
char input[] = "{}";
JsonObject& obj = bufferOfRightSize.parseObject(input);
REQUIRE(obj.success());
}
SECTION("TooSmallBufferForObjectWithOneValue") {
StaticJsonBuffer<JSON_OBJECT_SIZE(1) - 1> bufferTooSmall;
char input[] = "{\"a\":1}";
JsonObject& obj = bufferTooSmall.parseObject(input);
REQUIRE_FALSE(obj.success());
}
SECTION("BufferOfTheRightSizeForObjectWithOneValue") {
StaticJsonBuffer<JSON_OBJECT_SIZE(1)> bufferOfRightSize;
char input[] = "{\"a\":1}";
JsonObject& obj = bufferOfRightSize.parseObject(input);
REQUIRE(obj.success());
}
SECTION("TooSmallBufferForObjectWithNestedObject") {
StaticJsonBuffer<JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(0) - 1>
bufferTooSmall;
char input[] = "{\"a\":[]}";
JsonObject& obj = bufferTooSmall.parseObject(input);
REQUIRE_FALSE(obj.success());
}
SECTION("BufferOfTheRightSizeForObjectWithNestedObject") {
StaticJsonBuffer<JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(0)>
bufferOfRightSize;
char input[] = "{\"a\":[]}";
JsonObject& obj = bufferOfRightSize.parseObject(input);
REQUIRE(obj.success());
}
SECTION("CharPtrNull") {
REQUIRE_FALSE(
StaticJsonBuffer<100>().parseObject(static_cast<char*>(0)).success());
}
SECTION("ConstCharPtrNull") {
REQUIRE_FALSE(StaticJsonBuffer<100>()
.parseObject(static_cast<const char*>(0))
.success());
}
}