mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-15 11:36:36 +02:00
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:
@ -7,8 +7,7 @@
|
||||
#include <string>
|
||||
|
||||
TEST_CASE("JsonObject::set()") {
|
||||
DynamicJsonBuffer jb;
|
||||
JsonObject& _object = jb.createObject();
|
||||
DynamicJsonObject _object;
|
||||
|
||||
SECTION("int") {
|
||||
_object.set("hello", 123);
|
||||
@ -43,7 +42,7 @@ TEST_CASE("JsonObject::set()") {
|
||||
}
|
||||
|
||||
SECTION("nested array") {
|
||||
JsonArray& arr = jb.createArray();
|
||||
DynamicJsonArray arr;
|
||||
|
||||
_object.set("hello", arr);
|
||||
|
||||
@ -53,7 +52,7 @@ TEST_CASE("JsonObject::set()") {
|
||||
}
|
||||
|
||||
SECTION("nested object") {
|
||||
JsonObject& obj = jb.createObject();
|
||||
DynamicJsonObject obj;
|
||||
|
||||
_object.set("hello", obj);
|
||||
|
||||
@ -63,7 +62,7 @@ TEST_CASE("JsonObject::set()") {
|
||||
}
|
||||
|
||||
SECTION("array subscript") {
|
||||
JsonArray& arr = jb.createArray();
|
||||
DynamicJsonArray arr;
|
||||
arr.add(42);
|
||||
|
||||
_object.set("a", arr[0]);
|
||||
@ -72,7 +71,7 @@ TEST_CASE("JsonObject::set()") {
|
||||
}
|
||||
|
||||
SECTION("object subscript") {
|
||||
JsonObject& obj = jb.createObject();
|
||||
DynamicJsonObject obj;
|
||||
obj.set("x", 42);
|
||||
|
||||
_object.set("a", obj["x"]);
|
||||
@ -81,15 +80,13 @@ TEST_CASE("JsonObject::set()") {
|
||||
}
|
||||
|
||||
SECTION("returns true when allocation succeeds") {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1) + 15> jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
StaticJsonObject<JSON_OBJECT_SIZE(1) + 15> obj;
|
||||
|
||||
REQUIRE(true == obj.set(std::string("hello"), std::string("world")));
|
||||
}
|
||||
|
||||
SECTION("returns false when allocation fails") {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1) + 10> jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
StaticJsonObject<JSON_OBJECT_SIZE(1) + 10> obj;
|
||||
|
||||
REQUIRE(false == obj.set(std::string("hello"), std::string("world")));
|
||||
}
|
||||
@ -97,42 +94,42 @@ TEST_CASE("JsonObject::set()") {
|
||||
SECTION("should not duplicate const char*") {
|
||||
_object.set("hello", "world");
|
||||
const size_t expectedSize = JSON_OBJECT_SIZE(1);
|
||||
REQUIRE(expectedSize == jb.size());
|
||||
REQUIRE(expectedSize == _object.memoryUsage());
|
||||
}
|
||||
|
||||
SECTION("should duplicate char* value") {
|
||||
_object.set("hello", const_cast<char*>("world"));
|
||||
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
|
||||
REQUIRE(expectedSize == jb.size());
|
||||
REQUIRE(expectedSize == _object.memoryUsage());
|
||||
}
|
||||
|
||||
SECTION("should duplicate char* key") {
|
||||
_object.set(const_cast<char*>("hello"), "world");
|
||||
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
|
||||
REQUIRE(expectedSize == jb.size());
|
||||
REQUIRE(expectedSize == _object.memoryUsage());
|
||||
}
|
||||
|
||||
SECTION("should duplicate char* key&value") {
|
||||
_object.set(const_cast<char*>("hello"), const_cast<char*>("world"));
|
||||
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 12;
|
||||
REQUIRE(expectedSize <= jb.size());
|
||||
REQUIRE(expectedSize <= _object.memoryUsage());
|
||||
}
|
||||
|
||||
SECTION("should duplicate std::string value") {
|
||||
_object.set("hello", std::string("world"));
|
||||
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
|
||||
REQUIRE(expectedSize == jb.size());
|
||||
REQUIRE(expectedSize == _object.memoryUsage());
|
||||
}
|
||||
|
||||
SECTION("should duplicate std::string key") {
|
||||
_object.set(std::string("hello"), "world");
|
||||
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
|
||||
REQUIRE(expectedSize == jb.size());
|
||||
REQUIRE(expectedSize == _object.memoryUsage());
|
||||
}
|
||||
|
||||
SECTION("should duplicate std::string key&value") {
|
||||
_object.set(std::string("hello"), std::string("world"));
|
||||
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 12;
|
||||
REQUIRE(expectedSize <= jb.size());
|
||||
REQUIRE(expectedSize <= _object.memoryUsage());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user