Added StaticJsonDocument and DynamicJsonDocument.

Removed StaticJsonArray and DynamicJsonArray.
Removed StaticJsonObject and DynamicJsonObject.
Removed StaticJsonVariant and DynamicJsonVariant.
This commit is contained in:
Benoit Blanchon
2018-04-17 21:27:45 +02:00
parent a13b9e8bdc
commit 1feb92679d
100 changed files with 1696 additions and 1844 deletions

View File

@ -6,13 +6,10 @@
#include <catch.hpp>
TEST_CASE("JsonObject basics") {
DynamicJsonObject _object;
SECTION("InitialSizeIsZero") {
REQUIRE(0 == _object.size());
}
DynamicJsonDocument doc;
JsonObject& obj = doc.to<JsonObject>();
SECTION("SuccessIsTrue") {
REQUIRE(_object.success());
REQUIRE(obj.success());
}
}

View File

@ -6,24 +6,25 @@
#include <catch.hpp>
TEST_CASE("JsonObject::containsKey()") {
DynamicJsonObject _object;
DynamicJsonDocument doc;
JsonObject& obj = doc.to<JsonObject>();
SECTION("ContainsKeyReturnsFalseForNonExistingKey") {
_object.set("hello", 42);
obj.set("hello", 42);
REQUIRE(false == _object.containsKey("world"));
REQUIRE(false == obj.containsKey("world"));
}
SECTION("ContainsKeyReturnsTrueForDefinedValue") {
_object.set("hello", 42);
obj.set("hello", 42);
REQUIRE(true == _object.containsKey("hello"));
REQUIRE(true == obj.containsKey("hello"));
}
SECTION("ContainsKeyReturnsFalseAfterRemove") {
_object.set("hello", 42);
_object.remove("hello");
obj.set("hello", 42);
obj.remove("hello");
REQUIRE(false == _object.containsKey("hello"));
REQUIRE(false == obj.containsKey("hello"));
}
}

View File

@ -8,7 +8,8 @@
using namespace Catch::Matchers;
TEST_CASE("JsonObject::get()") {
DynamicJsonObject obj;
DynamicJsonDocument doc;
JsonObject& obj = doc.to<JsonObject>();
SECTION("GetConstCharPointer_GivenStringLiteral") {
obj.set("hello", "world");

View File

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

View File

@ -7,7 +7,8 @@
#include <string>
TEST_CASE("JsonObject::remove()") {
DynamicJsonObject obj;
DynamicJsonDocument doc;
JsonObject& obj = doc.to<JsonObject>();
SECTION("SizeDecreased_WhenValuesAreRemoved") {
obj["hello"] = 1;
@ -26,7 +27,9 @@ TEST_CASE("JsonObject::remove()") {
}
SECTION("RemoveByIterator") {
deserializeJson(obj, "{\"a\":0,\"b\":1,\"c\":2}");
obj["a"] = 0;
obj["b"] = 1;
obj["c"] = 2;
for (JsonObject::iterator it = obj.begin(); it != obj.end(); ++it) {
if (it->value == 1) obj.remove(it);

View File

@ -7,129 +7,136 @@
#include <string>
TEST_CASE("JsonObject::set()") {
DynamicJsonObject _object;
DynamicJsonDocument doc;
JsonObject& obj = doc.to<JsonObject>();
SECTION("int") {
_object.set("hello", 123);
obj.set("hello", 123);
REQUIRE(123 == _object["hello"].as<int>());
REQUIRE(_object["hello"].is<int>());
REQUIRE_FALSE(_object["hello"].is<bool>());
REQUIRE(123 == obj["hello"].as<int>());
REQUIRE(obj["hello"].is<int>());
REQUIRE_FALSE(obj["hello"].is<bool>());
}
SECTION("double") {
_object.set("hello", 123.45);
obj.set("hello", 123.45);
REQUIRE(123.45 == _object["hello"].as<double>());
REQUIRE(_object["hello"].is<double>());
REQUIRE_FALSE(_object["hello"].is<bool>());
REQUIRE(123.45 == obj["hello"].as<double>());
REQUIRE(obj["hello"].is<double>());
REQUIRE_FALSE(obj["hello"].is<bool>());
}
SECTION("bool") {
_object.set("hello", true);
obj.set("hello", true);
REQUIRE(_object["hello"].as<bool>());
REQUIRE(_object["hello"].is<bool>());
REQUIRE_FALSE(_object["hello"].is<long>());
REQUIRE(obj["hello"].as<bool>());
REQUIRE(obj["hello"].is<bool>());
REQUIRE_FALSE(obj["hello"].is<long>());
}
SECTION("const char*") {
_object.set("hello", "h3110");
obj.set("hello", "h3110");
REQUIRE(std::string("h3110") == _object["hello"].as<const char*>());
REQUIRE(_object["hello"].is<const char*>());
REQUIRE_FALSE(_object["hello"].is<long>());
REQUIRE(std::string("h3110") == obj["hello"].as<const char*>());
REQUIRE(obj["hello"].is<const char*>());
REQUIRE_FALSE(obj["hello"].is<long>());
}
SECTION("nested array") {
DynamicJsonArray arr;
DynamicJsonDocument doc2;
JsonArray& arr = doc2.to<JsonArray>();
_object.set("hello", arr);
obj.set("hello", arr);
REQUIRE(&arr == &_object["hello"].as<JsonArray>());
REQUIRE(_object["hello"].is<JsonArray&>());
REQUIRE_FALSE(_object["hello"].is<JsonObject&>());
REQUIRE(&arr == &obj["hello"].as<JsonArray>());
REQUIRE(obj["hello"].is<JsonArray&>());
REQUIRE_FALSE(obj["hello"].is<JsonObject&>());
}
SECTION("nested object") {
DynamicJsonObject obj;
DynamicJsonDocument doc2;
JsonObject& obj2 = doc2.to<JsonObject>();
_object.set("hello", obj);
obj.set("hello", obj2);
REQUIRE(&obj == &_object["hello"].as<JsonObject>());
REQUIRE(_object["hello"].is<JsonObject&>());
REQUIRE_FALSE(_object["hello"].is<JsonArray&>());
REQUIRE(&obj2 == &obj["hello"].as<JsonObject>());
REQUIRE(obj["hello"].is<JsonObject&>());
REQUIRE_FALSE(obj["hello"].is<JsonArray&>());
}
SECTION("array subscript") {
DynamicJsonArray arr;
DynamicJsonDocument doc2;
JsonArray& arr = doc2.to<JsonArray>();
arr.add(42);
_object.set("a", arr[0]);
obj.set("a", arr[0]);
REQUIRE(42 == _object["a"]);
REQUIRE(42 == obj["a"]);
}
SECTION("object subscript") {
DynamicJsonObject obj;
obj.set("x", 42);
DynamicJsonDocument doc2;
JsonObject& obj2 = doc2.to<JsonObject>();
obj2.set("x", 42);
_object.set("a", obj["x"]);
obj.set("a", obj2["x"]);
REQUIRE(42 == _object["a"]);
REQUIRE(42 == obj["a"]);
}
SECTION("returns true when allocation succeeds") {
StaticJsonObject<JSON_OBJECT_SIZE(1) + 15> obj;
StaticJsonDocument<JSON_OBJECT_SIZE(1) + 15> doc2;
JsonObject& obj2 = doc2.to<JsonObject>();
REQUIRE(true == obj.set(std::string("hello"), std::string("world")));
REQUIRE(true == obj2.set(std::string("hello"), std::string("world")));
}
SECTION("returns false when allocation fails") {
StaticJsonObject<JSON_OBJECT_SIZE(1) + 10> obj;
StaticJsonDocument<JSON_OBJECT_SIZE(1) + 10> doc2;
JsonObject& obj2 = doc2.to<JsonObject>();
REQUIRE(false == obj.set(std::string("hello"), std::string("world")));
REQUIRE(false == obj2.set(std::string("hello"), std::string("world")));
}
SECTION("should not duplicate const char*") {
_object.set("hello", "world");
obj.set("hello", "world");
const size_t expectedSize = JSON_OBJECT_SIZE(1);
REQUIRE(expectedSize == _object.memoryUsage());
REQUIRE(expectedSize == doc.memoryUsage());
}
SECTION("should duplicate char* value") {
_object.set("hello", const_cast<char*>("world"));
obj.set("hello", const_cast<char*>("world"));
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
REQUIRE(expectedSize == _object.memoryUsage());
REQUIRE(expectedSize == doc.memoryUsage());
}
SECTION("should duplicate char* key") {
_object.set(const_cast<char*>("hello"), "world");
obj.set(const_cast<char*>("hello"), "world");
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
REQUIRE(expectedSize == _object.memoryUsage());
REQUIRE(expectedSize == doc.memoryUsage());
}
SECTION("should duplicate char* key&value") {
_object.set(const_cast<char*>("hello"), const_cast<char*>("world"));
obj.set(const_cast<char*>("hello"), const_cast<char*>("world"));
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 12;
REQUIRE(expectedSize <= _object.memoryUsage());
REQUIRE(expectedSize <= doc.memoryUsage());
}
SECTION("should duplicate std::string value") {
_object.set("hello", std::string("world"));
obj.set("hello", std::string("world"));
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
REQUIRE(expectedSize == _object.memoryUsage());
REQUIRE(expectedSize == doc.memoryUsage());
}
SECTION("should duplicate std::string key") {
_object.set(std::string("hello"), "world");
obj.set(std::string("hello"), "world");
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
REQUIRE(expectedSize == _object.memoryUsage());
REQUIRE(expectedSize == doc.memoryUsage());
}
SECTION("should duplicate std::string key&value") {
_object.set(std::string("hello"), std::string("world"));
obj.set(std::string("hello"), std::string("world"));
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 12;
REQUIRE(expectedSize <= _object.memoryUsage());
REQUIRE(expectedSize <= doc.memoryUsage());
}
}

View File

@ -7,16 +7,21 @@
#include <string>
TEST_CASE("JsonObject::size()") {
DynamicJsonObject _object;
DynamicJsonDocument doc;
JsonObject& obj = doc.to<JsonObject>();
SECTION("initial size is zero") {
REQUIRE(0 == obj.size());
}
SECTION("increases when values are added") {
_object.set("hello", 42);
REQUIRE(1 == _object.size());
obj.set("hello", 42);
REQUIRE(1 == obj.size());
}
SECTION("doesn't increase when the smae key is added twice") {
_object["hello"] = 1;
_object["hello"] = 2;
REQUIRE(1 == _object.size());
obj["hello"] = 1;
obj["hello"] = 2;
REQUIRE(1 == obj.size());
}
}

View File

@ -6,146 +6,150 @@
#include <catch.hpp>
TEST_CASE("JsonObject::operator[]") {
DynamicJsonObject _object;
DynamicJsonDocument doc;
JsonObject& obj = doc.to<JsonObject>();
SECTION("int") {
_object["hello"] = 123;
obj["hello"] = 123;
REQUIRE(123 == _object["hello"].as<int>());
REQUIRE(true == _object["hello"].is<int>());
REQUIRE(false == _object["hello"].is<bool>());
REQUIRE(123 == obj["hello"].as<int>());
REQUIRE(true == obj["hello"].is<int>());
REQUIRE(false == obj["hello"].is<bool>());
}
SECTION("volatile int") { // issue #415
volatile int i = 123;
_object["hello"] = i;
obj["hello"] = i;
REQUIRE(123 == _object["hello"].as<int>());
REQUIRE(true == _object["hello"].is<int>());
REQUIRE(false == _object["hello"].is<bool>());
REQUIRE(123 == obj["hello"].as<int>());
REQUIRE(true == obj["hello"].is<int>());
REQUIRE(false == obj["hello"].is<bool>());
}
SECTION("double") {
_object["hello"] = 123.45;
obj["hello"] = 123.45;
REQUIRE(true == _object["hello"].is<double>());
REQUIRE(false == _object["hello"].is<long>());
REQUIRE(123.45 == _object["hello"].as<double>());
REQUIRE(true == obj["hello"].is<double>());
REQUIRE(false == obj["hello"].is<long>());
REQUIRE(123.45 == obj["hello"].as<double>());
}
SECTION("bool") {
_object["hello"] = true;
obj["hello"] = true;
REQUIRE(true == _object["hello"].is<bool>());
REQUIRE(false == _object["hello"].is<long>());
REQUIRE(true == _object["hello"].as<bool>());
REQUIRE(true == obj["hello"].is<bool>());
REQUIRE(false == obj["hello"].is<long>());
REQUIRE(true == obj["hello"].as<bool>());
}
SECTION("const char*") {
_object["hello"] = "h3110";
obj["hello"] = "h3110";
REQUIRE(true == _object["hello"].is<const char*>());
REQUIRE(false == _object["hello"].is<long>());
REQUIRE(std::string("h3110") == _object["hello"].as<const char*>());
REQUIRE(std::string("h3110") ==
_object["hello"].as<char*>()); // <- short hand
REQUIRE(true == obj["hello"].is<const char*>());
REQUIRE(false == obj["hello"].is<long>());
REQUIRE(std::string("h3110") == obj["hello"].as<const char*>());
REQUIRE(std::string("h3110") == obj["hello"].as<char*>()); // <- short hand
}
SECTION("array") {
DynamicJsonArray arr;
DynamicJsonDocument doc2;
JsonArray& arr = doc2.to<JsonArray>();
_object["hello"] = arr;
obj["hello"] = arr;
REQUIRE(&arr == &_object["hello"].as<JsonArray&>());
REQUIRE(&arr == &_object["hello"].as<JsonArray>()); // <- short hand
REQUIRE(&arr == &_object["hello"].as<const JsonArray&>());
REQUIRE(&arr == &_object["hello"].as<const JsonArray>()); // <- short hand
REQUIRE(true == _object["hello"].is<JsonArray&>());
REQUIRE(true == _object["hello"].is<JsonArray>());
REQUIRE(true == _object["hello"].is<const JsonArray&>());
REQUIRE(true == _object["hello"].is<const JsonArray>());
REQUIRE(false == _object["hello"].is<JsonObject&>());
REQUIRE(&arr == &obj["hello"].as<JsonArray&>());
REQUIRE(&arr == &obj["hello"].as<JsonArray>()); // <- short hand
REQUIRE(&arr == &obj["hello"].as<const JsonArray&>());
REQUIRE(&arr == &obj["hello"].as<const JsonArray>()); // <- short hand
REQUIRE(true == obj["hello"].is<JsonArray&>());
REQUIRE(true == obj["hello"].is<JsonArray>());
REQUIRE(true == obj["hello"].is<const JsonArray&>());
REQUIRE(true == obj["hello"].is<const JsonArray>());
REQUIRE(false == obj["hello"].is<JsonObject&>());
}
SECTION("object") {
DynamicJsonObject obj;
DynamicJsonDocument doc2;
JsonObject& obj2 = doc2.to<JsonObject>();
_object["hello"] = obj;
obj["hello"] = obj2;
REQUIRE(&obj == &_object["hello"].as<JsonObject&>());
REQUIRE(&obj == &_object["hello"].as<JsonObject>()); // <- short hand
REQUIRE(&obj == &_object["hello"].as<const JsonObject&>());
REQUIRE(&obj == &_object["hello"].as<const JsonObject>()); // <- short hand
REQUIRE(true == _object["hello"].is<JsonObject&>());
REQUIRE(true == _object["hello"].is<JsonObject>());
REQUIRE(true == _object["hello"].is<const JsonObject&>());
REQUIRE(true == _object["hello"].is<const JsonObject>());
REQUIRE(false == _object["hello"].is<JsonArray&>());
REQUIRE(&obj2 == &obj["hello"].as<JsonObject&>());
REQUIRE(&obj2 == &obj["hello"].as<JsonObject>()); // <- short hand
REQUIRE(&obj2 == &obj["hello"].as<const JsonObject&>());
REQUIRE(&obj2 == &obj["hello"].as<const JsonObject>()); // <- short hand
REQUIRE(true == obj["hello"].is<JsonObject&>());
REQUIRE(true == obj["hello"].is<JsonObject>());
REQUIRE(true == obj["hello"].is<const JsonObject&>());
REQUIRE(true == obj["hello"].is<const JsonObject>());
REQUIRE(false == obj["hello"].is<JsonArray&>());
}
SECTION("array subscript") {
DynamicJsonArray arr;
DynamicJsonDocument doc2;
JsonArray& arr = doc2.to<JsonArray>();
arr.add(42);
_object["a"] = arr[0];
obj["a"] = arr[0];
REQUIRE(42 == _object["a"]);
REQUIRE(42 == obj["a"]);
}
SECTION("object subscript") {
DynamicJsonObject obj;
obj.set("x", 42);
DynamicJsonDocument doc2;
JsonObject& obj2 = doc2.to<JsonObject>();
obj2.set("x", 42);
_object["a"] = obj["x"];
obj["a"] = obj2["x"];
REQUIRE(42 == _object["a"]);
REQUIRE(42 == obj["a"]);
}
SECTION("char key[]") { // issue #423
char key[] = "hello";
_object[key] = 42;
REQUIRE(42 == _object[key]);
obj[key] = 42;
REQUIRE(42 == obj[key]);
}
SECTION("should not duplicate const char*") {
_object["hello"] = "world";
obj["hello"] = "world";
const size_t expectedSize = JSON_OBJECT_SIZE(1);
REQUIRE(expectedSize == _object.memoryUsage());
REQUIRE(expectedSize == doc.memoryUsage());
}
SECTION("should duplicate char* value") {
_object["hello"] = const_cast<char*>("world");
obj["hello"] = const_cast<char*>("world");
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
REQUIRE(expectedSize == _object.memoryUsage());
REQUIRE(expectedSize == doc.memoryUsage());
}
SECTION("should duplicate char* key") {
_object[const_cast<char*>("hello")] = "world";
obj[const_cast<char*>("hello")] = "world";
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
REQUIRE(expectedSize == _object.memoryUsage());
REQUIRE(expectedSize == doc.memoryUsage());
}
SECTION("should duplicate char* key&value") {
_object[const_cast<char*>("hello")] = const_cast<char*>("world");
obj[const_cast<char*>("hello")] = const_cast<char*>("world");
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 12;
REQUIRE(expectedSize <= _object.memoryUsage());
REQUIRE(expectedSize <= doc.memoryUsage());
}
SECTION("should duplicate std::string value") {
_object["hello"] = std::string("world");
obj["hello"] = std::string("world");
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
REQUIRE(expectedSize == _object.memoryUsage());
REQUIRE(expectedSize == doc.memoryUsage());
}
SECTION("should duplicate std::string key") {
_object[std::string("hello")] = "world";
obj[std::string("hello")] = "world";
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
REQUIRE(expectedSize == _object.memoryUsage());
REQUIRE(expectedSize == doc.memoryUsage());
}
SECTION("should duplicate std::string key&value") {
_object[std::string("hello")] = std::string("world");
obj[std::string("hello")] = std::string("world");
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 12;
REQUIRE(expectedSize <= _object.memoryUsage());
REQUIRE(expectedSize <= doc.memoryUsage());
}
}