2021-03-29 17:14:01 +02:00
|
|
|
// ArduinoJson - https://arduinojson.org
|
2025-02-24 15:18:26 +01:00
|
|
|
// Copyright © 2014-2025, Benoit BLANCHON
|
2015-09-28 22:14:50 +02:00
|
|
|
// MIT License
|
|
|
|
|
|
|
|
#include <ArduinoJson.h>
|
2017-04-18 18:22:24 +02:00
|
|
|
#include <catch.hpp>
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2023-07-24 17:21:25 +02:00
|
|
|
#include "Allocators.hpp"
|
2024-06-07 09:35:45 +02:00
|
|
|
#include "Literals.hpp"
|
2023-07-24 17:21:25 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
TEST_CASE("JsonObject::operator[]") {
|
2023-07-25 14:53:54 +02:00
|
|
|
SpyingAllocator spy;
|
|
|
|
JsonDocument doc(&spy);
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonObject obj = doc.to<JsonObject>();
|
2017-04-18 18:22:24 +02:00
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("int") {
|
2018-04-17 21:27:45 +02:00
|
|
|
obj["hello"] = 123;
|
2017-04-18 18:22:24 +02:00
|
|
|
|
2018-04-17 21:27:45 +02:00
|
|
|
REQUIRE(123 == obj["hello"].as<int>());
|
|
|
|
REQUIRE(true == obj["hello"].is<int>());
|
|
|
|
REQUIRE(false == obj["hello"].is<bool>());
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("volatile int") { // issue #415
|
2017-04-18 18:22:24 +02:00
|
|
|
volatile int i = 123;
|
2018-04-17 21:27:45 +02:00
|
|
|
obj["hello"] = i;
|
2017-04-18 18:22:24 +02:00
|
|
|
|
2018-04-17 21:27:45 +02:00
|
|
|
REQUIRE(123 == obj["hello"].as<int>());
|
|
|
|
REQUIRE(true == obj["hello"].is<int>());
|
|
|
|
REQUIRE(false == obj["hello"].is<bool>());
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("double") {
|
2018-04-17 21:27:45 +02:00
|
|
|
obj["hello"] = 123.45;
|
2017-04-18 18:22:24 +02:00
|
|
|
|
2018-04-17 21:27:45 +02:00
|
|
|
REQUIRE(true == obj["hello"].is<double>());
|
|
|
|
REQUIRE(false == obj["hello"].is<long>());
|
|
|
|
REQUIRE(123.45 == obj["hello"].as<double>());
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("bool") {
|
2018-04-17 21:27:45 +02:00
|
|
|
obj["hello"] = true;
|
2017-04-18 18:22:24 +02:00
|
|
|
|
2018-04-17 21:27:45 +02:00
|
|
|
REQUIRE(true == obj["hello"].is<bool>());
|
|
|
|
REQUIRE(false == obj["hello"].is<long>());
|
|
|
|
REQUIRE(true == obj["hello"].as<bool>());
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("const char*") {
|
2018-04-17 21:27:45 +02:00
|
|
|
obj["hello"] = "h3110";
|
2017-04-18 18:22:24 +02:00
|
|
|
|
2018-04-17 21:27:45 +02:00
|
|
|
REQUIRE(true == obj["hello"].is<const char*>());
|
|
|
|
REQUIRE(false == obj["hello"].is<long>());
|
2024-06-07 09:35:45 +02:00
|
|
|
REQUIRE("h3110"_s == obj["hello"].as<const char*>());
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("array") {
|
2023-07-17 18:15:13 +02:00
|
|
|
JsonDocument doc2;
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonArray arr = doc2.to<JsonArray>();
|
2017-04-18 18:22:24 +02:00
|
|
|
|
2018-04-17 21:27:45 +02:00
|
|
|
obj["hello"] = arr;
|
2017-04-18 18:22:24 +02:00
|
|
|
|
2018-07-02 09:35:21 +02:00
|
|
|
REQUIRE(arr == obj["hello"].as<JsonArray>());
|
2018-04-17 21:27:45 +02:00
|
|
|
REQUIRE(true == obj["hello"].is<JsonArray>());
|
2018-07-02 09:35:21 +02:00
|
|
|
REQUIRE(false == obj["hello"].is<JsonObject>());
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("object") {
|
2023-07-17 18:15:13 +02:00
|
|
|
JsonDocument doc2;
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonObject obj2 = doc2.to<JsonObject>();
|
2017-04-18 18:22:24 +02:00
|
|
|
|
2018-04-17 21:27:45 +02:00
|
|
|
obj["hello"] = obj2;
|
2017-04-18 18:22:24 +02:00
|
|
|
|
2018-07-02 09:35:21 +02:00
|
|
|
REQUIRE(obj2 == obj["hello"].as<JsonObject>());
|
2018-04-17 21:27:45 +02:00
|
|
|
REQUIRE(true == obj["hello"].is<JsonObject>());
|
2018-07-02 09:35:21 +02:00
|
|
|
REQUIRE(false == obj["hello"].is<JsonArray>());
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("array subscript") {
|
2023-07-17 18:15:13 +02:00
|
|
|
JsonDocument doc2;
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonArray arr = doc2.to<JsonArray>();
|
2017-04-18 18:22:24 +02:00
|
|
|
arr.add(42);
|
|
|
|
|
2018-04-17 21:27:45 +02:00
|
|
|
obj["a"] = arr[0];
|
2017-04-18 18:22:24 +02:00
|
|
|
|
2018-04-17 21:27:45 +02:00
|
|
|
REQUIRE(42 == obj["a"]);
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("object subscript") {
|
2023-07-17 18:15:13 +02:00
|
|
|
JsonDocument doc2;
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonObject obj2 = doc2.to<JsonObject>();
|
2018-10-18 14:51:02 +02:00
|
|
|
obj2["x"] = 42;
|
2017-04-18 18:22:24 +02:00
|
|
|
|
2018-04-17 21:27:45 +02:00
|
|
|
obj["a"] = obj2["x"];
|
2017-04-18 18:22:24 +02:00
|
|
|
|
2018-04-17 21:27:45 +02:00
|
|
|
REQUIRE(42 == obj["a"]);
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|
|
|
|
|
2018-01-14 13:46:28 +01:00
|
|
|
SECTION("char key[]") { // issue #423
|
2017-04-18 18:22:24 +02:00
|
|
|
char key[] = "hello";
|
2018-04-17 21:27:45 +02:00
|
|
|
obj[key] = 42;
|
|
|
|
REQUIRE(42 == obj[key]);
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|
2018-01-14 13:46:28 +01:00
|
|
|
|
2025-02-24 15:35:09 +01:00
|
|
|
SECTION("string literals") {
|
2018-04-17 21:27:45 +02:00
|
|
|
obj["hello"] = "world";
|
2025-02-24 15:35:09 +01:00
|
|
|
REQUIRE(spy.log() == AllocatorLog{
|
|
|
|
Allocate(sizeofPool()),
|
|
|
|
Allocate(sizeofStaticStringPool()),
|
|
|
|
});
|
2018-01-14 13:46:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("should duplicate char* value") {
|
2018-04-17 21:27:45 +02:00
|
|
|
obj["hello"] = const_cast<char*>("world");
|
2023-07-26 06:06:38 +02:00
|
|
|
REQUIRE(spy.log() == AllocatorLog{
|
|
|
|
Allocate(sizeofPool()),
|
2025-02-24 15:35:09 +01:00
|
|
|
Allocate(sizeofStaticStringPool()),
|
2023-07-26 06:06:38 +02:00
|
|
|
Allocate(sizeofString("world")),
|
|
|
|
});
|
2018-01-14 13:46:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("should duplicate char* key") {
|
2025-02-24 15:35:09 +01:00
|
|
|
obj[const_cast<char*>("hello")] = 42;
|
2023-07-26 06:06:38 +02:00
|
|
|
REQUIRE(spy.log() == AllocatorLog{
|
|
|
|
Allocate(sizeofPool()),
|
2024-08-23 15:27:46 +02:00
|
|
|
Allocate(sizeofString("hello")),
|
2023-07-26 06:06:38 +02:00
|
|
|
});
|
2018-01-14 13:46:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("should duplicate char* key&value") {
|
2018-04-17 21:27:45 +02:00
|
|
|
obj[const_cast<char*>("hello")] = const_cast<char*>("world");
|
2023-07-26 06:06:38 +02:00
|
|
|
REQUIRE(spy.log() == AllocatorLog{
|
|
|
|
Allocate(sizeofPool()),
|
2024-08-23 15:27:46 +02:00
|
|
|
Allocate(sizeofString("hello")),
|
2023-07-26 06:06:38 +02:00
|
|
|
Allocate(sizeofString("world")),
|
|
|
|
});
|
2018-01-14 13:46:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("should duplicate std::string value") {
|
2024-06-07 09:35:45 +02:00
|
|
|
obj["hello"] = "world"_s;
|
2023-07-26 06:06:38 +02:00
|
|
|
REQUIRE(spy.log() == AllocatorLog{
|
|
|
|
Allocate(sizeofPool()),
|
2025-02-24 15:35:09 +01:00
|
|
|
Allocate(sizeofStaticStringPool()),
|
2023-07-26 06:06:38 +02:00
|
|
|
Allocate(sizeofString("world")),
|
|
|
|
});
|
2018-01-14 13:46:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("should duplicate std::string key") {
|
2025-02-24 15:35:09 +01:00
|
|
|
obj["hello"_s] = 42;
|
2023-07-26 06:06:38 +02:00
|
|
|
REQUIRE(spy.log() == AllocatorLog{
|
|
|
|
Allocate(sizeofPool()),
|
2024-08-23 15:27:46 +02:00
|
|
|
Allocate(sizeofString("hello")),
|
2023-07-26 06:06:38 +02:00
|
|
|
});
|
2018-01-14 13:46:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("should duplicate std::string key&value") {
|
2024-06-07 09:35:45 +02:00
|
|
|
obj["hello"_s] = "world"_s;
|
2023-07-26 06:06:38 +02:00
|
|
|
REQUIRE(spy.log() == AllocatorLog{
|
|
|
|
Allocate(sizeofPool()),
|
2024-08-23 15:27:46 +02:00
|
|
|
Allocate(sizeofString("hello")),
|
2023-07-26 06:06:38 +02:00
|
|
|
Allocate(sizeofString("world")),
|
|
|
|
});
|
2018-01-14 13:46:28 +01:00
|
|
|
}
|
2018-06-01 09:08:38 +02:00
|
|
|
|
2019-01-29 17:00:11 +01:00
|
|
|
SECTION("should duplicate a non-static JsonString key") {
|
2025-02-24 15:35:09 +01:00
|
|
|
obj[JsonString("hello", false)] = 42;
|
2023-07-26 06:06:38 +02:00
|
|
|
REQUIRE(spy.log() == AllocatorLog{
|
|
|
|
Allocate(sizeofPool()),
|
2024-08-23 15:27:46 +02:00
|
|
|
Allocate(sizeofString("hello")),
|
2023-07-26 06:06:38 +02:00
|
|
|
});
|
2019-01-29 17:00:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("should not duplicate a static JsonString key") {
|
2025-02-24 15:35:09 +01:00
|
|
|
obj[JsonString("hello", true)] = 42;
|
2023-07-26 06:06:38 +02:00
|
|
|
REQUIRE(spy.log() == AllocatorLog{
|
|
|
|
Allocate(sizeofPool()),
|
2025-02-24 15:35:09 +01:00
|
|
|
Allocate(sizeofStaticStringPool()),
|
2023-07-26 06:06:38 +02:00
|
|
|
});
|
2019-01-29 17:00:11 +01:00
|
|
|
}
|
|
|
|
|
2018-06-01 09:08:38 +02:00
|
|
|
SECTION("should ignore null key") {
|
|
|
|
// object must have a value to make a call to strcmp()
|
2018-06-04 17:44:10 +02:00
|
|
|
obj["dummy"] = 42;
|
2018-06-01 09:08:38 +02:00
|
|
|
|
|
|
|
const char* null = 0;
|
2018-06-04 17:44:10 +02:00
|
|
|
obj[null] = 666;
|
2018-06-01 09:08:38 +02:00
|
|
|
|
2018-06-04 17:44:10 +02:00
|
|
|
REQUIRE(obj.size() == 1);
|
2019-05-23 21:54:42 +02:00
|
|
|
REQUIRE(obj[null] == null);
|
2018-06-01 09:08:38 +02:00
|
|
|
}
|
2018-08-22 14:37:17 +02:00
|
|
|
|
2018-09-11 16:05:56 +02:00
|
|
|
SECTION("obj[key].to<JsonArray>()") {
|
|
|
|
JsonArray arr = obj["hello"].to<JsonArray>();
|
|
|
|
|
|
|
|
REQUIRE(arr.isNull() == false);
|
|
|
|
}
|
|
|
|
|
2018-08-22 14:37:17 +02:00
|
|
|
#if defined(HAS_VARIABLE_LENGTH_ARRAY) && \
|
|
|
|
!defined(SUBSCRIPT_CONFLICTS_WITH_BUILTIN_OPERATOR)
|
|
|
|
SECTION("obj[VLA] = str") {
|
2022-02-25 09:23:51 +01:00
|
|
|
size_t i = 16;
|
2018-08-22 14:37:17 +02:00
|
|
|
char vla[i];
|
|
|
|
strcpy(vla, "hello");
|
|
|
|
|
|
|
|
obj[vla] = "world";
|
|
|
|
|
2024-06-07 09:35:45 +02:00
|
|
|
REQUIRE("world"_s == obj["hello"]);
|
2018-08-22 14:37:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("obj[str] = VLA") { // issue #416
|
2022-02-25 09:23:51 +01:00
|
|
|
size_t i = 32;
|
2018-08-22 14:37:17 +02:00
|
|
|
char vla[i];
|
|
|
|
strcpy(vla, "world");
|
|
|
|
|
|
|
|
obj["hello"] = vla;
|
|
|
|
|
2024-06-07 09:35:45 +02:00
|
|
|
REQUIRE("world"_s == obj["hello"].as<const char*>());
|
2018-08-22 14:37:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("obj.set(VLA, str)") {
|
2022-02-25 09:23:51 +01:00
|
|
|
size_t i = 16;
|
2018-08-22 14:37:17 +02:00
|
|
|
char vla[i];
|
|
|
|
strcpy(vla, "hello");
|
|
|
|
|
|
|
|
obj[vla] = "world";
|
|
|
|
|
2024-06-07 09:35:45 +02:00
|
|
|
REQUIRE("world"_s == obj["hello"]);
|
2018-08-22 14:37:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("obj.set(str, VLA)") {
|
2022-02-25 09:23:51 +01:00
|
|
|
size_t i = 32;
|
2018-08-22 14:37:17 +02:00
|
|
|
char vla[i];
|
|
|
|
strcpy(vla, "world");
|
|
|
|
|
|
|
|
obj["hello"].set(vla);
|
|
|
|
|
2024-06-07 09:35:45 +02:00
|
|
|
REQUIRE("world"_s == obj["hello"].as<const char*>());
|
2018-08-22 14:37:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("obj[VLA]") {
|
2022-02-25 09:23:51 +01:00
|
|
|
size_t i = 16;
|
2018-08-22 14:37:17 +02:00
|
|
|
char vla[i];
|
|
|
|
strcpy(vla, "hello");
|
|
|
|
|
|
|
|
deserializeJson(doc, "{\"hello\":\"world\"}");
|
|
|
|
|
|
|
|
obj = doc.as<JsonObject>();
|
2024-06-07 09:35:45 +02:00
|
|
|
REQUIRE("world"_s == obj[vla]);
|
2018-08-22 14:37:17 +02:00
|
|
|
}
|
|
|
|
#endif
|
2018-10-12 12:00:27 +02:00
|
|
|
|
|
|
|
SECTION("chain") {
|
2023-08-09 11:31:29 +02:00
|
|
|
obj["hello"]["world"] = 123;
|
2018-10-12 12:00:27 +02:00
|
|
|
|
|
|
|
REQUIRE(123 == obj["hello"]["world"].as<int>());
|
|
|
|
REQUIRE(true == obj["hello"]["world"].is<int>());
|
|
|
|
REQUIRE(false == obj["hello"]["world"].is<bool>());
|
|
|
|
}
|
2024-05-14 21:06:02 +02:00
|
|
|
|
|
|
|
SECTION("JsonVariant") {
|
|
|
|
obj["hello"] = "world";
|
2024-11-14 14:56:47 +01:00
|
|
|
obj["a\0b"_s] = "ABC";
|
2024-05-14 21:06:02 +02:00
|
|
|
|
2024-11-14 14:56:47 +01:00
|
|
|
doc["key1"] = "hello";
|
|
|
|
doc["key2"] = "a\0b"_s;
|
|
|
|
doc["key3"] = "foo";
|
|
|
|
|
|
|
|
REQUIRE(obj[obj["key1"]] == "world");
|
|
|
|
REQUIRE(obj[obj["key2"]] == "ABC");
|
|
|
|
REQUIRE(obj[obj["key3"]] == nullptr);
|
|
|
|
REQUIRE(obj[obj["key4"]] == nullptr);
|
2024-05-14 21:06:02 +02:00
|
|
|
}
|
2017-01-22 11:10:45 +01:00
|
|
|
}
|