Update to ArduinoJson 7.0.4

This commit is contained in:
iranl
2024-04-19 14:44:01 +02:00
parent 1378732081
commit 81be0a689a
444 changed files with 10842 additions and 9422 deletions

View File

@@ -1,5 +1,5 @@
# ArduinoJson - https://arduinojson.org
# Copyright © 2014-2023, Benoit BLANCHON
# Copyright © 2014-2024, Benoit BLANCHON
# MIT License
add_executable(JsonObjectTests
@@ -7,18 +7,15 @@ add_executable(JsonObjectTests
compare.cpp
containsKey.cpp
copy.cpp
createNestedArray.cpp
createNestedObject.cpp
equals.cpp
invalid.cpp
isNull.cpp
iterator.cpp
memoryUsage.cpp
nesting.cpp
remove.cpp
size.cpp
std_string.cpp
subscript.cpp
unbound.cpp
)
add_test(JsonObject JsonObjectTests)

View File

@@ -1,5 +1,5 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
@@ -14,7 +14,7 @@ TEST_CASE("JsonObject::clear()") {
}
SECTION("Removes all elements") {
StaticJsonDocument<64> doc;
JsonDocument doc;
JsonObject obj = doc.to<JsonObject>();
obj["hello"] = 1;
obj["world"] = 2;

View File

@@ -1,12 +1,12 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("Compare JsonObject with JsonObject") {
StaticJsonDocument<512> doc;
JsonDocument doc;
SECTION("Compare with unbound") {
JsonObject object = doc.to<JsonObject>();
@@ -43,12 +43,12 @@ TEST_CASE("Compare JsonObject with JsonObject") {
}
SECTION("Compare with identical object") {
JsonObject object1 = doc.createNestedObject();
JsonObject object1 = doc.add<JsonObject>();
object1["a"] = 1;
object1["b"] = "hello";
object1["c"][0] = false;
JsonObject object2 = doc.createNestedObject();
JsonObject object2 = doc.add<JsonObject>();
object2["a"] = 1;
object2["b"] = "hello";
object2["c"][0] = false;
@@ -62,12 +62,12 @@ TEST_CASE("Compare JsonObject with JsonObject") {
}
SECTION("Compare with different object") {
JsonObject object1 = doc.createNestedObject();
JsonObject object1 = doc.add<JsonObject>();
object1["a"] = 1;
object1["b"] = "hello1";
object1["c"][0] = false;
JsonObject object2 = doc.createNestedObject();
JsonObject object2 = doc.add<JsonObject>();
object2["a"] = 1;
object2["b"] = "hello2";
object2["c"][0] = false;
@@ -82,7 +82,7 @@ TEST_CASE("Compare JsonObject with JsonObject") {
}
TEST_CASE("Compare JsonObject with JsonVariant") {
StaticJsonDocument<512> doc;
JsonDocument doc;
SECTION("Compare with self") {
JsonObject object = doc.to<JsonObject>();
@@ -107,12 +107,12 @@ TEST_CASE("Compare JsonObject with JsonVariant") {
}
SECTION("Compare with identical object") {
JsonObject object = doc.createNestedObject();
JsonObject object = doc.add<JsonObject>();
object["a"] = 1;
object["b"] = "hello";
object["c"][0] = false;
JsonVariant variant = doc.createNestedObject();
JsonVariant variant = doc.add<JsonObject>();
variant["a"] = 1;
variant["b"] = "hello";
variant["c"][0] = false;
@@ -133,12 +133,12 @@ TEST_CASE("Compare JsonObject with JsonVariant") {
}
SECTION("Compare with different object") {
JsonObject object = doc.createNestedObject();
JsonObject object = doc.add<JsonObject>();
object["a"] = 1;
object["b"] = "hello1";
object["c"][0] = false;
JsonVariant variant = doc.createNestedObject();
JsonVariant variant = doc.add<JsonObject>();
variant["a"] = 1;
variant["b"] = "hello2";
variant["c"][0] = false;
@@ -153,7 +153,7 @@ TEST_CASE("Compare JsonObject with JsonVariant") {
}
TEST_CASE("Compare JsonObject with JsonVariantConst") {
StaticJsonDocument<512> doc;
JsonDocument doc;
SECTION("Compare with unbound") {
JsonObject object = doc.to<JsonObject>();
@@ -199,12 +199,12 @@ TEST_CASE("Compare JsonObject with JsonVariantConst") {
}
SECTION("Compare with identical object") {
JsonObject object = doc.createNestedObject();
JsonObject object = doc.add<JsonObject>();
object["a"] = 1;
object["b"] = "hello";
object["c"][0] = false;
JsonObject object2 = doc.createNestedObject();
JsonObject object2 = doc.add<JsonObject>();
object2["a"] = 1;
object2["b"] = "hello";
object2["c"][0] = false;
@@ -226,12 +226,12 @@ TEST_CASE("Compare JsonObject with JsonVariantConst") {
}
SECTION("Compare with different object") {
JsonObject object = doc.createNestedObject();
JsonObject object = doc.add<JsonObject>();
object["a"] = 1;
object["b"] = "hello1";
object["c"][0] = false;
JsonObject object2 = doc.createNestedObject();
JsonObject object2 = doc.add<JsonObject>();
object2["a"] = 1;
object2["b"] = "hello2";
object2["c"][0] = false;
@@ -247,7 +247,7 @@ TEST_CASE("Compare JsonObject with JsonVariantConst") {
}
TEST_CASE("Compare JsonObject with JsonObjectConst") {
StaticJsonDocument<512> doc;
JsonDocument doc;
SECTION("Compare with unbound") {
JsonObject object = doc.to<JsonObject>();
@@ -292,12 +292,12 @@ TEST_CASE("Compare JsonObject with JsonObjectConst") {
}
SECTION("Compare with identical object") {
JsonObject object1 = doc.createNestedObject();
JsonObject object1 = doc.add<JsonObject>();
object1["a"] = 1;
object1["b"] = "hello";
object1["c"][0] = false;
JsonObject object2 = doc.createNestedObject();
JsonObject object2 = doc.add<JsonObject>();
object2["a"] = 1;
object2["b"] = "hello";
object2["c"][0] = false;
@@ -319,12 +319,12 @@ TEST_CASE("Compare JsonObject with JsonObjectConst") {
}
SECTION("Compare with different object") {
JsonObject object1 = doc.createNestedObject();
JsonObject object1 = doc.add<JsonObject>();
object1["a"] = 1;
object1["b"] = "hello1";
object1["c"][0] = false;
JsonObject object2 = doc.createNestedObject();
JsonObject object2 = doc.add<JsonObject>();
object2["a"] = 1;
object2["b"] = "hello2";
object2["c"][0] = false;
@@ -347,7 +347,7 @@ TEST_CASE("Compare JsonObject with JsonObjectConst") {
}
TEST_CASE("Compare JsonObjectConst with JsonObjectConst") {
StaticJsonDocument<512> doc;
JsonDocument doc;
SECTION("Compare with unbound") {
JsonObject object = doc.to<JsonObject>();
@@ -387,13 +387,13 @@ TEST_CASE("Compare JsonObjectConst with JsonObjectConst") {
}
SECTION("Compare with identical object") {
JsonObject object1 = doc.createNestedObject();
JsonObject object1 = doc.add<JsonObject>();
object1["a"] = 1;
object1["b"] = "hello";
object1["c"][0] = false;
JsonObjectConst carray1 = object1;
JsonObject object2 = doc.createNestedObject();
JsonObject object2 = doc.add<JsonObject>();
object2["a"] = 1;
object2["b"] = "hello";
object2["c"][0] = false;
@@ -408,13 +408,13 @@ TEST_CASE("Compare JsonObjectConst with JsonObjectConst") {
}
SECTION("Compare with different object") {
JsonObject object1 = doc.createNestedObject();
JsonObject object1 = doc.add<JsonObject>();
object1["a"] = 1;
object1["b"] = "hello1";
object1["c"][0] = false;
JsonObjectConst carray1 = object1;
JsonObject object2 = doc.createNestedObject();
JsonObject object2 = doc.add<JsonObject>();
object2["a"] = 1;
object2["b"] = "hello2";
object2["c"][0] = false;
@@ -430,7 +430,7 @@ TEST_CASE("Compare JsonObjectConst with JsonObjectConst") {
}
TEST_CASE("Compare JsonObjectConst with JsonVariant") {
StaticJsonDocument<512> doc;
JsonDocument doc;
SECTION("Compare with self") {
JsonObject object = doc.to<JsonObject>();
@@ -455,13 +455,13 @@ TEST_CASE("Compare JsonObjectConst with JsonVariant") {
}
SECTION("Compare with identical object") {
JsonObject object1 = doc.createNestedObject();
JsonObject object1 = doc.add<JsonObject>();
object1["a"] = 1;
object1["b"] = "hello";
object1["c"][0] = false;
JsonObjectConst carray1 = object1;
JsonObject object2 = doc.createNestedObject();
JsonObject object2 = doc.add<JsonObject>();
object2["a"] = 1;
object2["b"] = "hello";
object2["c"][0] = false;
@@ -483,13 +483,13 @@ TEST_CASE("Compare JsonObjectConst with JsonVariant") {
}
SECTION("Compare with different object") {
JsonObject object1 = doc.createNestedObject();
JsonObject object1 = doc.add<JsonObject>();
object1["a"] = 1;
object1["b"] = "hello1";
object1["c"][0] = false;
JsonObjectConst carray1 = object1;
JsonObject object2 = doc.createNestedObject();
JsonObject object2 = doc.add<JsonObject>();
object2["a"] = 1;
object2["b"] = "hello2";
object2["c"][0] = false;

View File

@@ -1,12 +1,12 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("JsonObject::containsKey()") {
DynamicJsonDocument doc(4096);
JsonDocument doc;
JsonObject obj = doc.to<JsonObject>();
obj["hello"] = 42;
@@ -15,12 +15,6 @@ TEST_CASE("JsonObject::containsKey()") {
REQUIRE(true == obj.containsKey("hello"));
}
SECTION("works with JsonObjectConst") {
JsonObjectConst cobj = obj;
REQUIRE(false == cobj.containsKey("world"));
REQUIRE(true == cobj.containsKey("hello"));
}
SECTION("returns false after remove()") {
obj.remove("hello");

View File

@@ -1,65 +1,89 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
#include "Allocators.hpp"
TEST_CASE("JsonObject::set()") {
DynamicJsonDocument doc1(4096);
DynamicJsonDocument doc2(4096);
SpyingAllocator spy;
JsonDocument doc1(&spy);
JsonDocument doc2(&spy);
JsonObject obj1 = doc1.to<JsonObject>();
JsonObject obj2 = doc2.to<JsonObject>();
SECTION("doesn't copy static string in key or value") {
obj1["hello"] = "world";
spy.clearLog();
bool success = obj2.set(obj1);
REQUIRE(success == true);
REQUIRE(doc1.memoryUsage() == doc2.memoryUsage());
REQUIRE(obj2["hello"] == std::string("world"));
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
});
}
SECTION("copy local string value") {
obj1["hello"] = std::string("world");
spy.clearLog();
bool success = obj2.set(obj1);
REQUIRE(success == true);
REQUIRE(doc1.memoryUsage() == doc2.memoryUsage());
REQUIRE(obj2["hello"] == std::string("world"));
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("world")),
});
}
SECTION("copy local key") {
obj1[std::string("hello")] = "world";
spy.clearLog();
bool success = obj2.set(obj1);
REQUIRE(success == true);
REQUIRE(doc1.memoryUsage() == doc2.memoryUsage());
REQUIRE(obj2["hello"] == std::string("world"));
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofString("hello")),
Allocate(sizeofPool()),
});
}
SECTION("copy string from deserializeJson()") {
deserializeJson(doc1, "{'hello':'world'}");
spy.clearLog();
bool success = obj2.set(obj1);
REQUIRE(success == true);
REQUIRE(doc1.memoryUsage() == doc2.memoryUsage());
REQUIRE(obj2["hello"] == std::string("world"));
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofString("hello")),
Allocate(sizeofPool()),
Allocate(sizeofString("world")),
});
}
SECTION("copy string from deserializeMsgPack()") {
deserializeMsgPack(doc1, "\x81\xA5hello\xA5world");
spy.clearLog();
bool success = obj2.set(obj1);
REQUIRE(success == true);
REQUIRE(doc1.memoryUsage() == doc2.memoryUsage());
REQUIRE(obj2["hello"] == std::string("world"));
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofString("hello")),
Allocate(sizeofPool()),
Allocate(sizeofString("world")),
});
}
SECTION("should work with JsonObjectConst") {
@@ -67,32 +91,34 @@ TEST_CASE("JsonObject::set()") {
obj2.set(static_cast<JsonObjectConst>(obj1));
REQUIRE(doc1.memoryUsage() == doc2.memoryUsage());
REQUIRE(obj2["hello"] == std::string("world"));
}
SECTION("destination too small to store the key") {
StaticJsonDocument<JSON_OBJECT_SIZE(1)> doc3;
SECTION("copy fails in the middle of an object") {
TimebombAllocator timebomb(2);
JsonDocument doc3(&timebomb);
JsonObject obj3 = doc3.to<JsonObject>();
obj1[std::string("hello")] = "world";
obj1[std::string("a")] = 1;
obj1[std::string("b")] = 2;
bool success = obj3.set(obj1);
REQUIRE(success == false);
REQUIRE(doc3.as<std::string>() == "{}");
REQUIRE(doc3.as<std::string>() == "{\"a\":1}");
}
SECTION("destination too small to store the value") {
StaticJsonDocument<JSON_OBJECT_SIZE(1)> doc3;
SECTION("copy fails in the middle of an array") {
TimebombAllocator timebomb(1);
JsonDocument doc3(&timebomb);
JsonObject obj3 = doc3.to<JsonObject>();
obj1["hello"] = std::string("world");
obj1["hello"][0] = std::string("world");
bool success = obj3.set(obj1);
REQUIRE(success == false);
REQUIRE(doc3.as<std::string>() == "{\"hello\":null}");
REQUIRE(doc3.as<std::string>() == "{\"hello\":[null]}");
}
SECTION("destination is null") {

View File

@@ -1,27 +0,0 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("JsonObject::createNestedArray()") {
DynamicJsonDocument doc(4096);
JsonObject obj = doc.to<JsonObject>();
SECTION("key is a const char*") {
JsonArray arr = obj.createNestedArray("hello");
REQUIRE(arr.isNull() == false);
}
#ifdef HAS_VARIABLE_LENGTH_ARRAY
SECTION("key is a VLA") {
size_t i = 16;
char vla[i];
strcpy(vla, "hello");
JsonArray arr = obj.createNestedArray(vla);
REQUIRE(arr.isNull() == false);
}
#endif
}

View File

@@ -1,25 +0,0 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("JsonObject::createNestedObject()") {
DynamicJsonDocument doc(4096);
JsonObject obj = doc.to<JsonObject>();
SECTION("key is a const char*") {
obj.createNestedObject("hello");
}
#ifdef HAS_VARIABLE_LENGTH_ARRAY
SECTION("key is a VLA") {
size_t i = 16;
char vla[i];
strcpy(vla, "hello");
obj.createNestedObject(vla);
}
#endif
}

View File

@@ -1,25 +1,22 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("JsonObject::operator==()") {
DynamicJsonDocument doc1(4096);
JsonDocument doc1;
JsonObject obj1 = doc1.to<JsonObject>();
JsonObjectConst obj1c = obj1;
DynamicJsonDocument doc2(4096);
JsonDocument doc2;
JsonObject obj2 = doc2.to<JsonObject>();
JsonObjectConst obj2c = obj2;
SECTION("should return false when objs differ") {
obj1["hello"] = "coucou";
obj2["world"] = 1;
REQUIRE_FALSE(obj1 == obj2);
REQUIRE_FALSE(obj1c == obj2c);
}
SECTION("should return false when LHS has more elements") {
@@ -28,7 +25,6 @@ TEST_CASE("JsonObject::operator==()") {
obj2["hello"] = "coucou";
REQUIRE_FALSE(obj1 == obj2);
REQUIRE_FALSE(obj1c == obj2c);
}
SECTION("should return false when RKS has more elements") {
@@ -37,7 +33,6 @@ TEST_CASE("JsonObject::operator==()") {
obj2["world"] = 666;
REQUIRE_FALSE(obj1 == obj2);
REQUIRE_FALSE(obj1c == obj2c);
}
SECTION("should return true when objs equal") {
@@ -48,20 +43,17 @@ TEST_CASE("JsonObject::operator==()") {
obj2["hello"] = "world";
REQUIRE(obj1 == obj2);
REQUIRE(obj1c == obj2c);
}
SECTION("should return false when RHS is null") {
JsonObject null;
REQUIRE_FALSE(obj1 == null);
REQUIRE_FALSE(obj1c == null);
}
SECTION("should return false when LHS is null") {
JsonObject null;
REQUIRE_FALSE(null == obj2);
REQUIRE_FALSE(null == obj2c);
}
}

View File

@@ -1,5 +1,5 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
@@ -12,25 +12,12 @@ TEST_CASE("JsonObject::isNull()") {
}
SECTION("returns false") {
DynamicJsonDocument doc(4096);
JsonDocument doc;
JsonObject obj = doc.to<JsonObject>();
REQUIRE(obj.isNull() == false);
}
}
TEST_CASE("JsonObjectConst::isNull()") {
SECTION("returns true") {
JsonObjectConst obj;
REQUIRE(obj.isNull() == true);
}
SECTION("returns false") {
DynamicJsonDocument doc(4096);
JsonObjectConst obj = doc.to<JsonObject>();
REQUIRE(obj.isNull() == false);
}
}
TEST_CASE("JsonObject::operator bool()") {
SECTION("returns false") {
JsonObject obj;
@@ -38,21 +25,8 @@ TEST_CASE("JsonObject::operator bool()") {
}
SECTION("returns true") {
DynamicJsonDocument doc(4096);
JsonDocument doc;
JsonObject obj = doc.to<JsonObject>();
REQUIRE(static_cast<bool>(obj) == true);
}
}
TEST_CASE("JsonObjectConst::operator bool()") {
SECTION("returns false") {
JsonObjectConst obj;
REQUIRE(static_cast<bool>(obj) == false);
}
SECTION("returns true") {
DynamicJsonDocument doc(4096);
JsonObjectConst obj = doc.to<JsonObject>();
REQUIRE(static_cast<bool>(obj) == true);
}
}

View File

@@ -1,14 +1,12 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
using namespace Catch::Matchers;
TEST_CASE("JsonObject::begin()/end()") {
StaticJsonDocument<JSON_OBJECT_SIZE(2)> doc;
JsonDocument doc;
JsonObject obj = doc.to<JsonObject>();
obj["ab"] = 12;
obj["cd"] = 34;
@@ -36,38 +34,3 @@ TEST_CASE("JsonObject::begin()/end()") {
REQUIRE(null.begin() == null.end());
}
}
TEST_CASE("JsonObjectConst::begin()/end()") {
StaticJsonDocument<JSON_OBJECT_SIZE(2)> doc;
JsonObject obj = doc.to<JsonObject>();
obj["ab"] = 12;
obj["cd"] = 34;
JsonObjectConst cobj = obj;
SECTION("Iteration") {
JsonObjectConst::iterator it = cobj.begin();
REQUIRE(cobj.end() != it);
REQUIRE(it->key() == "ab");
REQUIRE(12 == it->value());
++it;
REQUIRE(cobj.end() != it);
JsonPairConst pair = *it;
REQUIRE(pair.key() == "cd");
REQUIRE(34 == pair.value());
++it;
REQUIRE(cobj.end() == it);
}
SECTION("Dereferencing end() is safe") {
REQUIRE(cobj.end()->key().isNull());
REQUIRE(cobj.end()->value().isNull());
}
SECTION("null JsonObjectConst") {
JsonObjectConst null;
REQUIRE(null.begin() == null.end());
}
}

View File

@@ -1,43 +0,0 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
#include <string>
TEST_CASE("JsonObject::memoryUsage()") {
DynamicJsonDocument doc(4096);
JsonObject obj = doc.to<JsonObject>();
SECTION("return 0 if uninitialized") {
JsonObject unitialized;
REQUIRE(unitialized.memoryUsage() == 0);
}
SECTION("JSON_OBJECT_SIZE(0) for empty object") {
REQUIRE(obj.memoryUsage() == JSON_OBJECT_SIZE(0));
}
SECTION("JSON_OBJECT_SIZE(1) after add") {
obj["hello"] = 42;
REQUIRE(obj.memoryUsage() == JSON_OBJECT_SIZE(1));
}
SECTION("includes the size of the key") {
obj[std::string("hello")] = 42;
REQUIRE(obj.memoryUsage() == JSON_OBJECT_SIZE(1) + 6);
}
SECTION("includes the size of the nested array") {
JsonArray nested = obj.createNestedArray("nested");
nested.add(42);
REQUIRE(obj.memoryUsage() == JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(1));
}
SECTION("includes the size of the nested object") {
JsonObject nested = obj.createNestedObject("nested");
nested["hello"] = "world";
REQUIRE(obj.memoryUsage() == 2 * JSON_OBJECT_SIZE(1));
}
}

View File

@@ -1,12 +1,12 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("JsonObject::nesting()") {
DynamicJsonDocument doc(4096);
JsonDocument doc;
JsonObject obj = doc.to<JsonObject>();
SECTION("return 0 if uninitialized") {
@@ -24,12 +24,12 @@ TEST_CASE("JsonObject::nesting()") {
}
SECTION("returns 2 with nested array") {
obj.createNestedArray("nested");
obj["nested"].to<JsonArray>();
REQUIRE(obj.nesting() == 2);
}
SECTION("returns 2 with nested object") {
obj.createNestedObject("nested");
obj["nested"].to<JsonObject>();
REQUIRE(obj.nesting() == 2);
}
}

View File

@@ -1,5 +1,5 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
@@ -7,7 +7,7 @@
#include <string>
TEST_CASE("JsonObject::remove()") {
DynamicJsonDocument doc(4096);
JsonDocument doc;
JsonObject obj = doc.to<JsonObject>();
obj["a"] = 0;
obj["b"] = 1;
@@ -51,7 +51,8 @@ TEST_CASE("JsonObject::remove()") {
}
SECTION("Remove last") {
it += 2;
++it;
++it;
obj.remove(it);
serializeJson(obj, result);
REQUIRE("{\"a\":0,\"b\":1}" == result);

View File

@@ -1,5 +1,5 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
@@ -7,7 +7,7 @@
#include <string>
TEST_CASE("JsonObject::size()") {
DynamicJsonDocument doc(4096);
JsonDocument doc;
JsonObject obj = doc.to<JsonObject>();
SECTION("initial size is zero") {

View File

@@ -1,5 +1,5 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
@@ -12,7 +12,7 @@ static void eraseString(std::string& str) {
}
TEST_CASE("std::string") {
DynamicJsonDocument doc(4096);
JsonDocument doc;
SECTION("operator[]") {
char json[] = "{\"key\":\"value\"}";
@@ -32,26 +32,6 @@ TEST_CASE("std::string") {
REQUIRE(std::string("value") == obj[std::string("key")]);
}
SECTION("createNestedObject()") {
JsonObject obj = doc.to<JsonObject>();
std::string key = "key";
char json[64];
obj.createNestedObject(key);
eraseString(key);
serializeJson(doc, json, sizeof(json));
REQUIRE(std::string("{\"key\":{}}") == json);
}
SECTION("createNestedArray()") {
JsonObject obj = doc.to<JsonObject>();
std::string key = "key";
char json[64];
obj.createNestedArray(key);
eraseString(key);
serializeJson(doc, json, sizeof(json));
REQUIRE(std::string("{\"key\":[]}") == json);
}
SECTION("containsKey()") {
char json[] = "{\"key\":\"value\"}";
deserializeJson(doc, json);
@@ -83,28 +63,4 @@ TEST_CASE("std::string") {
eraseString(value);
REQUIRE(std::string("world") == obj["hello"]);
}
SECTION("memoryUsage() increases when adding a new key") {
std::string key1("hello"), key2("world");
JsonObject obj = doc.to<JsonObject>();
obj[key1] = 1;
size_t sizeBefore = doc.memoryUsage();
obj[key2] = 2;
size_t sizeAfter = doc.memoryUsage();
REQUIRE(sizeAfter - sizeBefore >= key2.size());
}
SECTION("memoryUsage() remains when adding the same key") {
std::string key("hello");
JsonObject obj = doc.to<JsonObject>();
obj[key] = 1;
size_t sizeBefore = doc.memoryUsage();
obj[key] = 2;
size_t sizeAfter = doc.memoryUsage();
REQUIRE(sizeBefore == sizeAfter);
}
}

View File

@@ -1,12 +1,15 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
#include "Allocators.hpp"
TEST_CASE("JsonObject::operator[]") {
DynamicJsonDocument doc(4096);
SpyingAllocator spy;
JsonDocument doc(&spy);
JsonObject obj = doc.to<JsonObject>();
SECTION("int") {
@@ -51,7 +54,7 @@ TEST_CASE("JsonObject::operator[]") {
}
SECTION("array") {
DynamicJsonDocument doc2(4096);
JsonDocument doc2;
JsonArray arr = doc2.to<JsonArray>();
obj["hello"] = arr;
@@ -62,7 +65,7 @@ TEST_CASE("JsonObject::operator[]") {
}
SECTION("object") {
DynamicJsonDocument doc2(4096);
JsonDocument doc2;
JsonObject obj2 = doc2.to<JsonObject>();
obj["hello"] = obj2;
@@ -73,7 +76,7 @@ TEST_CASE("JsonObject::operator[]") {
}
SECTION("array subscript") {
DynamicJsonDocument doc2(4096);
JsonDocument doc2;
JsonArray arr = doc2.to<JsonArray>();
arr.add(42);
@@ -83,7 +86,7 @@ TEST_CASE("JsonObject::operator[]") {
}
SECTION("object subscript") {
DynamicJsonDocument doc2(4096);
JsonDocument doc2;
JsonObject obj2 = doc2.to<JsonObject>();
obj2["x"] = 42;
@@ -100,56 +103,72 @@ TEST_CASE("JsonObject::operator[]") {
SECTION("should not duplicate const char*") {
obj["hello"] = "world";
const size_t expectedSize = JSON_OBJECT_SIZE(1);
REQUIRE(expectedSize == doc.memoryUsage());
REQUIRE(spy.log() == AllocatorLog{Allocate(sizeofPool())});
}
SECTION("should duplicate char* value") {
obj["hello"] = const_cast<char*>("world");
const size_t expectedSize = JSON_OBJECT_SIZE(1) + JSON_STRING_SIZE(5);
REQUIRE(expectedSize == doc.memoryUsage());
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("world")),
});
}
SECTION("should duplicate char* key") {
obj[const_cast<char*>("hello")] = "world";
const size_t expectedSize = JSON_OBJECT_SIZE(1) + JSON_STRING_SIZE(5);
REQUIRE(expectedSize == doc.memoryUsage());
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofString("hello")),
Allocate(sizeofPool()),
});
}
SECTION("should duplicate char* key&value") {
obj[const_cast<char*>("hello")] = const_cast<char*>("world");
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 2 * JSON_STRING_SIZE(5);
REQUIRE(expectedSize <= doc.memoryUsage());
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofString("hello")),
Allocate(sizeofPool()),
Allocate(sizeofString("world")),
});
}
SECTION("should duplicate std::string value") {
obj["hello"] = std::string("world");
const size_t expectedSize = JSON_OBJECT_SIZE(1) + JSON_STRING_SIZE(5);
REQUIRE(expectedSize == doc.memoryUsage());
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("world")),
});
}
SECTION("should duplicate std::string key") {
obj[std::string("hello")] = "world";
const size_t expectedSize = JSON_OBJECT_SIZE(1) + JSON_STRING_SIZE(5);
REQUIRE(expectedSize == doc.memoryUsage());
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofString("hello")),
Allocate(sizeofPool()),
});
}
SECTION("should duplicate std::string key&value") {
obj[std::string("hello")] = std::string("world");
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 2 * JSON_STRING_SIZE(5);
REQUIRE(expectedSize <= doc.memoryUsage());
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofString("hello")),
Allocate(sizeofPool()),
Allocate(sizeofString("world")),
});
}
SECTION("should duplicate a non-static JsonString key") {
obj[JsonString("hello", JsonString::Copied)] = "world";
const size_t expectedSize = JSON_OBJECT_SIZE(1) + JSON_STRING_SIZE(5);
REQUIRE(expectedSize == doc.memoryUsage());
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofString("hello")),
Allocate(sizeofPool()),
});
}
SECTION("should not duplicate a static JsonString key") {
obj[JsonString("hello", JsonString::Linked)] = "world";
const size_t expectedSize = JSON_OBJECT_SIZE(1);
REQUIRE(expectedSize == doc.memoryUsage());
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
});
}
SECTION("should ignore null key") {
@@ -224,7 +243,7 @@ TEST_CASE("JsonObject::operator[]") {
#endif
SECTION("chain") {
obj.createNestedObject("hello")["world"] = 123;
obj["hello"]["world"] = 123;
REQUIRE(123 == obj["hello"]["world"].as<int>());
REQUIRE(true == obj["hello"]["world"].is<int>());

View File

@@ -1,5 +1,5 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
@@ -7,27 +7,19 @@
using namespace Catch::Matchers;
TEST_CASE("JsonObject::invalid()") {
TEST_CASE("Unbound JsonObject") {
JsonObject obj;
SECTION("SubscriptFails") {
SECTION("retrieve member") {
REQUIRE(obj["key"].isNull());
}
SECTION("AddFails") {
SECTION("add member") {
obj["hello"] = "world";
REQUIRE(0 == obj.size());
}
SECTION("CreateNestedArrayFails") {
REQUIRE(obj.createNestedArray("hello").isNull());
}
SECTION("CreateNestedObjectFails") {
REQUIRE(obj.createNestedObject("world").isNull());
}
SECTION("serialize to 'null'") {
SECTION("serialize") {
char buffer[32];
serializeJson(obj, buffer, sizeof(buffer));
REQUIRE_THAT(buffer, Equals("null"));