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

@ -6,8 +6,7 @@
#include <catch.hpp>
TEST_CASE("JsonObject basics") {
DynamicJsonBuffer _jsonBuffer;
JsonObject& _object = _jsonBuffer.createObject();
DynamicJsonObject _object;
SECTION("InitialSizeIsZero") {
REQUIRE(0 == _object.size());

View File

@ -6,8 +6,7 @@
#include <catch.hpp>
TEST_CASE("JsonObject::containsKey()") {
DynamicJsonBuffer _jsonBuffer;
JsonObject& _object = _jsonBuffer.createObject();
DynamicJsonObject _object;
SECTION("ContainsKeyReturnsFalseForNonExistingKey") {
_object.set("hello", 42);

View File

@ -8,8 +8,7 @@
using namespace Catch::Matchers;
TEST_CASE("JsonObject::get()") {
DynamicJsonBuffer jb;
JsonObject& obj = jb.createObject();
DynamicJsonObject obj;
SECTION("GetConstCharPointer_GivenStringLiteral") {
obj.set("hello", "world");

View File

@ -8,8 +8,7 @@
using namespace Catch::Matchers;
TEST_CASE("JsonObject::begin()/end()") {
StaticJsonBuffer<JSON_OBJECT_SIZE(2)> jb;
JsonObject& obj = jb.createObject();
StaticJsonObject<JSON_OBJECT_SIZE(2)> obj;
obj["ab"] = 12;
obj["cd"] = 34;

View File

@ -18,8 +18,7 @@ void check(const JsonObject &obj, const std::string expected) {
}
TEST_CASE("JsonObject::prettyPrintTo()") {
DynamicJsonBuffer jb;
JsonObject &obj = jb.createObject();
DynamicJsonObject obj;
SECTION("EmptyObject") {
check(obj, "{}");

View File

@ -16,8 +16,7 @@ void check(const JsonObject &obj, const std::string &expected) {
REQUIRE(expected.size() == measuredLen);
}
TEST_CASE("JsonObject::printTo()") {
DynamicJsonBuffer _jsonBuffer;
JsonObject &obj = _jsonBuffer.createObject();
DynamicJsonObject obj;
SECTION("EmptyObject") {
check(obj, "{}");
@ -92,17 +91,22 @@ TEST_CASE("JsonObject::printTo()") {
}
SECTION("ThreeNestedArrays") {
DynamicJsonArray b, c;
obj.createNestedArray("a");
obj["b"] = _jsonBuffer.createArray();
obj.set("c", _jsonBuffer.createArray());
obj["b"] = b;
obj.set("c", c);
check(obj, "{\"a\":[],\"b\":[],\"c\":[]}");
}
SECTION("ThreeNestedObjects") {
DynamicJsonObject b;
DynamicJsonObject c;
obj.createNestedObject("a");
obj["b"] = _jsonBuffer.createObject();
obj.set("c", _jsonBuffer.createObject());
obj["b"] = b;
obj.set("c", c);
check(obj, "{\"a\":{},\"b\":{},\"c\":{}}");
}

View File

@ -7,10 +7,9 @@
#include <string>
TEST_CASE("JsonObject::remove()") {
DynamicJsonBuffer jb;
DynamicJsonObject obj;
SECTION("SizeDecreased_WhenValuesAreRemoved") {
JsonObject& obj = jb.createObject();
obj["hello"] = 1;
obj.remove("hello");
@ -19,7 +18,6 @@ TEST_CASE("JsonObject::remove()") {
}
SECTION("SizeUntouched_WhenRemoveIsCalledWithAWrongKey") {
JsonObject& obj = jb.createObject();
obj["hello"] = 1;
obj.remove("world");
@ -28,7 +26,7 @@ TEST_CASE("JsonObject::remove()") {
}
SECTION("RemoveByIterator") {
JsonObject& obj = jb.parseObject("{\"a\":0,\"b\":1,\"c\":2}");
deserializeJson(obj, "{\"a\":0,\"b\":1,\"c\":2}");
for (JsonObject::iterator it = obj.begin(); it != obj.end(); ++it) {
if (it->value == 1) obj.remove(it);

View File

@ -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());
}
}

View File

@ -7,8 +7,7 @@
#include <string>
TEST_CASE("JsonObject::size()") {
DynamicJsonBuffer jb;
JsonObject& _object = jb.createObject();
DynamicJsonObject _object;
SECTION("increases when values are added") {
_object.set("hello", 42);

View File

@ -6,8 +6,7 @@
#include <catch.hpp>
TEST_CASE("JsonObject::operator[]") {
DynamicJsonBuffer _jsonBuffer;
JsonObject& _object = _jsonBuffer.createObject();
DynamicJsonObject _object;
SECTION("int") {
_object["hello"] = 123;
@ -53,7 +52,7 @@ TEST_CASE("JsonObject::operator[]") {
}
SECTION("array") {
JsonArray& arr = _jsonBuffer.createArray();
DynamicJsonArray arr;
_object["hello"] = arr;
@ -69,7 +68,7 @@ TEST_CASE("JsonObject::operator[]") {
}
SECTION("object") {
JsonObject& obj = _jsonBuffer.createObject();
DynamicJsonObject obj;
_object["hello"] = obj;
@ -85,7 +84,7 @@ TEST_CASE("JsonObject::operator[]") {
}
SECTION("array subscript") {
JsonArray& arr = _jsonBuffer.createArray();
DynamicJsonArray arr;
arr.add(42);
_object["a"] = arr[0];
@ -94,7 +93,7 @@ TEST_CASE("JsonObject::operator[]") {
}
SECTION("object subscript") {
JsonObject& obj = _jsonBuffer.createObject();
DynamicJsonObject obj;
obj.set("x", 42);
_object["a"] = obj["x"];
@ -111,42 +110,42 @@ TEST_CASE("JsonObject::operator[]") {
SECTION("should not duplicate const char*") {
_object["hello"] = "world";
const size_t expectedSize = JSON_OBJECT_SIZE(1);
REQUIRE(expectedSize == _jsonBuffer.size());
REQUIRE(expectedSize == _object.memoryUsage());
}
SECTION("should duplicate char* value") {
_object["hello"] = const_cast<char*>("world");
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
REQUIRE(expectedSize == _jsonBuffer.size());
REQUIRE(expectedSize == _object.memoryUsage());
}
SECTION("should duplicate char* key") {
_object[const_cast<char*>("hello")] = "world";
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
REQUIRE(expectedSize == _jsonBuffer.size());
REQUIRE(expectedSize == _object.memoryUsage());
}
SECTION("should duplicate char* key&value") {
_object[const_cast<char*>("hello")] = const_cast<char*>("world");
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 12;
REQUIRE(expectedSize <= _jsonBuffer.size());
REQUIRE(expectedSize <= _object.memoryUsage());
}
SECTION("should duplicate std::string value") {
_object["hello"] = std::string("world");
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
REQUIRE(expectedSize == _jsonBuffer.size());
REQUIRE(expectedSize == _object.memoryUsage());
}
SECTION("should duplicate std::string key") {
_object[std::string("hello")] = "world";
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
REQUIRE(expectedSize == _jsonBuffer.size());
REQUIRE(expectedSize == _object.memoryUsage());
}
SECTION("should duplicate std::string key&value") {
_object[std::string("hello")] = std::string("world");
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 12;
REQUIRE(expectedSize <= _jsonBuffer.size());
REQUIRE(expectedSize <= _object.memoryUsage());
}
}