forked from bblanchon/ArduinoJson
Compare commits
14 Commits
string-lit
...
variant-im
Author | SHA1 | Date | |
---|---|---|---|
908a69213c | |||
242d3f2f35 | |||
be1e33f5ff | |||
cd2a4296af | |||
462fab6f01 | |||
2feb07dc15 | |||
7f0cca379a | |||
434e90025d | |||
19d2a8c555 | |||
29a1c5732f | |||
943f22d8f9 | |||
733bc4ee82 | |||
9aa77994b4 | |||
f051f8328d |
@ -38,6 +38,11 @@ HEAD
|
|||||||
> doc["key"] = str; // same as previous line for supported string classes (`String`, `std::string`, etc.)
|
> doc["key"] = str; // same as previous line for supported string classes (`String`, `std::string`, etc.)
|
||||||
> ```
|
> ```
|
||||||
|
|
||||||
|
v7.4.2 (2025-06-20)
|
||||||
|
------
|
||||||
|
|
||||||
|
* Fix truncated strings on Arduino Due (issue #2181)
|
||||||
|
|
||||||
v7.4.1 (2025-04-11)
|
v7.4.1 (2025-04-11)
|
||||||
------
|
------
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ if(ESP_PLATFORM)
|
|||||||
return()
|
return()
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
project(ArduinoJson VERSION 7.4.1)
|
project(ArduinoJson VERSION 7.4.2)
|
||||||
|
|
||||||
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
|
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
|
||||||
include(CTest)
|
include(CTest)
|
||||||
|
@ -139,11 +139,6 @@ See the [tutorial on arduinojson.org](https://arduinojson.org/v7/doc/serializati
|
|||||||
|
|
||||||
ArduinoJson is thankful to its sponsors. Please give them a visit; they deserve it!
|
ArduinoJson is thankful to its sponsors. Please give them a visit; they deserve it!
|
||||||
|
|
||||||
<p>
|
|
||||||
<a href="https://www.programmingelectronics.com/" rel="sponsored">
|
|
||||||
<img src="https://arduinojson.org/images/2021/10/programmingeleactronicsacademy.png" alt="Programming Electronics Academy" width="200">
|
|
||||||
</a>
|
|
||||||
</p>
|
|
||||||
<p>
|
<p>
|
||||||
<a href="https://github.com/1technophile" rel="sponsored">
|
<a href="https://github.com/1technophile" rel="sponsored">
|
||||||
<img alt="1technophile" src="https://avatars.githubusercontent.com/u/12672732?s=40&v=4">
|
<img alt="1technophile" src="https://avatars.githubusercontent.com/u/12672732?s=40&v=4">
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
version: 7.4.1.{build}
|
version: 7.4.2.{build}
|
||||||
environment:
|
environment:
|
||||||
matrix:
|
matrix:
|
||||||
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2022
|
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2022
|
||||||
|
@ -12,7 +12,7 @@ static_assert(ARDUINOJSON_LITTLE_ENDIAN == 1, "ARDUINOJSON_LITTLE_ENDIAN");
|
|||||||
|
|
||||||
static_assert(ARDUINOJSON_USE_DOUBLE == 0, "ARDUINOJSON_USE_DOUBLE");
|
static_assert(ARDUINOJSON_USE_DOUBLE == 0, "ARDUINOJSON_USE_DOUBLE");
|
||||||
|
|
||||||
static_assert(ArduinoJson::detail::ResourceManager::slotSize == 6, "slot size");
|
static_assert(sizeof(ArduinoJson::detail::VariantData) == 6, "slot size");
|
||||||
|
|
||||||
void setup() {}
|
void setup() {}
|
||||||
void loop() {}
|
void loop() {}
|
||||||
|
@ -10,7 +10,7 @@ static_assert(ARDUINOJSON_LITTLE_ENDIAN == 1, "ARDUINOJSON_LITTLE_ENDIAN");
|
|||||||
|
|
||||||
static_assert(ARDUINOJSON_USE_DOUBLE == 1, "ARDUINOJSON_USE_DOUBLE");
|
static_assert(ARDUINOJSON_USE_DOUBLE == 1, "ARDUINOJSON_USE_DOUBLE");
|
||||||
|
|
||||||
static_assert(ArduinoJson::detail::ResourceManager::slotSize == 8, "slot size");
|
static_assert(sizeof(ArduinoJson::detail::VariantData) == 8, "slot size");
|
||||||
|
|
||||||
void setup() {}
|
void setup() {}
|
||||||
void loop() {}
|
void loop() {}
|
||||||
|
@ -10,7 +10,6 @@ static_assert(ARDUINOJSON_LITTLE_ENDIAN == 1, "ARDUINOJSON_LITTLE_ENDIAN");
|
|||||||
|
|
||||||
static_assert(ARDUINOJSON_USE_DOUBLE == 1, "ARDUINOJSON_USE_DOUBLE");
|
static_assert(ARDUINOJSON_USE_DOUBLE == 1, "ARDUINOJSON_USE_DOUBLE");
|
||||||
|
|
||||||
static_assert(ArduinoJson::detail::ResourceManager::slotSize == 16,
|
static_assert(sizeof(ArduinoJson::detail::VariantData) == 16, "slot size");
|
||||||
"slot size");
|
|
||||||
|
|
||||||
int main() {}
|
int main() {}
|
||||||
|
@ -10,6 +10,6 @@ static_assert(ARDUINOJSON_LITTLE_ENDIAN == 1, "ARDUINOJSON_LITTLE_ENDIAN");
|
|||||||
|
|
||||||
static_assert(ARDUINOJSON_USE_DOUBLE == 1, "ARDUINOJSON_USE_DOUBLE");
|
static_assert(ARDUINOJSON_USE_DOUBLE == 1, "ARDUINOJSON_USE_DOUBLE");
|
||||||
|
|
||||||
static_assert(ArduinoJson::detail::ResourceManager::slotSize == 8, "slot size");
|
static_assert(sizeof(ArduinoJson::detail::VariantData) == 8, "slot size");
|
||||||
|
|
||||||
int main() {}
|
int main() {}
|
||||||
|
@ -269,10 +269,10 @@ inline size_t sizeofPoolList(size_t n = ARDUINOJSON_INITIAL_POOL_COUNT) {
|
|||||||
return sizeof(MemoryPool<VariantData>) * n;
|
return sizeof(MemoryPool<VariantData>) * n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename T = ArduinoJson::detail::VariantData>
|
||||||
inline size_t sizeofPool(
|
inline size_t sizeofPool(
|
||||||
ArduinoJson::detail::SlotCount n = ARDUINOJSON_POOL_CAPACITY) {
|
ArduinoJson::detail::SlotCount n = ARDUINOJSON_POOL_CAPACITY) {
|
||||||
using namespace ArduinoJson::detail;
|
return ArduinoJson::detail::MemoryPool<T>::slotsToBytes(n);
|
||||||
return MemoryPool<VariantData>::slotsToBytes(n);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline size_t sizeofStaticStringPool(
|
inline size_t sizeofStaticStringPool(
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
#include "Allocators.hpp"
|
#include "Allocators.hpp"
|
||||||
#include "Literals.hpp"
|
#include "Literals.hpp"
|
||||||
|
|
||||||
using ArduinoJson::detail::sizeofArray;
|
using namespace ArduinoJson::detail;
|
||||||
|
|
||||||
TEST_CASE("JsonArray::add(T)") {
|
TEST_CASE("JsonArray::add(T)") {
|
||||||
SpyingAllocator spy;
|
SpyingAllocator spy;
|
||||||
@ -33,7 +33,8 @@ TEST_CASE("JsonArray::add(T)") {
|
|||||||
REQUIRE(array[0].is<double>());
|
REQUIRE(array[0].is<double>());
|
||||||
REQUIRE_FALSE(array[0].is<bool>());
|
REQUIRE_FALSE(array[0].is<bool>());
|
||||||
REQUIRE(spy.log() == AllocatorLog{
|
REQUIRE(spy.log() == AllocatorLog{
|
||||||
Allocate(sizeofPool()),
|
Allocate(sizeofPool<VariantData>()),
|
||||||
|
Allocate(sizeofPool<EightByteValue>()),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
#include "Allocators.hpp"
|
#include "Allocators.hpp"
|
||||||
|
|
||||||
using ArduinoJson::detail::sizeofArray;
|
using namespace ArduinoJson::detail;
|
||||||
|
|
||||||
TEST_CASE("deserialize JSON array") {
|
TEST_CASE("deserialize JSON array") {
|
||||||
SpyingAllocator spy;
|
SpyingAllocator spy;
|
||||||
@ -92,8 +92,12 @@ TEST_CASE("deserialize JSON array") {
|
|||||||
REQUIRE(arr[0].as<double>() == Approx(4.2123456));
|
REQUIRE(arr[0].as<double>() == Approx(4.2123456));
|
||||||
REQUIRE(arr[1] == -7E89);
|
REQUIRE(arr[1] == -7E89);
|
||||||
REQUIRE(spy.log() == AllocatorLog{
|
REQUIRE(spy.log() == AllocatorLog{
|
||||||
Allocate(sizeofPool()),
|
Allocate(sizeofPool<VariantData>()),
|
||||||
Reallocate(sizeofPool(), sizeofPool(4)),
|
Allocate(sizeofPool<EightByteValue>()),
|
||||||
|
Reallocate(sizeofPool<VariantData>(),
|
||||||
|
sizeofPool<VariantData>(2)),
|
||||||
|
Reallocate(sizeofPool<EightByteValue>(),
|
||||||
|
sizeofPool<EightByteValue>(2)),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -121,7 +121,7 @@ TEST_CASE("deserializeJson() returns NoMemory if string length overflows") {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("deserializeJson() returns NoMemory if extension allocation fails") {
|
TEST_CASE("deserializeJson() returns NoMemory if 8-bit slot allocation fails") {
|
||||||
JsonDocument doc(FailingAllocator::instance());
|
JsonDocument doc(FailingAllocator::instance());
|
||||||
|
|
||||||
SECTION("uint32_t should pass") {
|
SECTION("uint32_t should pass") {
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
#include "Allocators.hpp"
|
#include "Allocators.hpp"
|
||||||
#include "Literals.hpp"
|
#include "Literals.hpp"
|
||||||
|
|
||||||
using ArduinoJson::detail::sizeofObject;
|
using namespace ArduinoJson::detail;
|
||||||
|
|
||||||
enum ErrorCode { ERROR_01 = 1, ERROR_10 = 10 };
|
enum ErrorCode { ERROR_01 = 1, ERROR_10 = 10 };
|
||||||
|
|
||||||
@ -197,10 +197,10 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
|
|||||||
REQUIRE(result == true);
|
REQUIRE(result == true);
|
||||||
REQUIRE(variant.is<double>() == true);
|
REQUIRE(variant.is<double>() == true);
|
||||||
REQUIRE(variant.as<double>() == 1.2);
|
REQUIRE(variant.as<double>() == 1.2);
|
||||||
REQUIRE(spy.log() ==
|
REQUIRE(spy.log() == AllocatorLog{
|
||||||
AllocatorLog{
|
Allocate(sizeofPool<EightByteValue>()),
|
||||||
Allocate(sizeofPool()),
|
Reallocate(sizeofPool<EightByteValue>(),
|
||||||
Reallocate(sizeofPool(), sizeofPool(1)), // one extension slot
|
sizeofPool<EightByteValue>(1)),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -220,10 +220,10 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
|
|||||||
REQUIRE(result == true);
|
REQUIRE(result == true);
|
||||||
REQUIRE(variant.is<int64_t>() == true);
|
REQUIRE(variant.is<int64_t>() == true);
|
||||||
REQUIRE(variant.as<int64_t>() == -2147483649LL);
|
REQUIRE(variant.as<int64_t>() == -2147483649LL);
|
||||||
REQUIRE(spy.log() ==
|
REQUIRE(spy.log() == AllocatorLog{
|
||||||
AllocatorLog{
|
Allocate(sizeofPool<EightByteValue>()),
|
||||||
Allocate(sizeofPool()),
|
Reallocate(sizeofPool<EightByteValue>(),
|
||||||
Reallocate(sizeofPool(), sizeofPool(1)), // one extension slot
|
sizeofPool<EightByteValue>(1)),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -243,10 +243,10 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
|
|||||||
REQUIRE(result == true);
|
REQUIRE(result == true);
|
||||||
REQUIRE(variant.is<uint64_t>() == true);
|
REQUIRE(variant.is<uint64_t>() == true);
|
||||||
REQUIRE(variant.as<uint64_t>() == 4294967296);
|
REQUIRE(variant.as<uint64_t>() == 4294967296);
|
||||||
REQUIRE(spy.log() ==
|
REQUIRE(spy.log() == AllocatorLog{
|
||||||
AllocatorLog{
|
Allocate(sizeofPool<EightByteValue>()),
|
||||||
Allocate(sizeofPool()),
|
Reallocate(sizeofPool<EightByteValue>(),
|
||||||
Reallocate(sizeofPool(), sizeofPool(1)), // one extension slot
|
sizeofPool<EightByteValue>(1)),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -378,7 +378,7 @@ TEST_CASE("JsonVariant::set() releases the previous value") {
|
|||||||
}
|
}
|
||||||
|
|
||||||
SECTION("float") {
|
SECTION("float") {
|
||||||
v.set(1.2);
|
v.set(1.2f);
|
||||||
REQUIRE(spy.log() == AllocatorLog{
|
REQUIRE(spy.log() == AllocatorLog{
|
||||||
Deallocate(sizeofString("world")),
|
Deallocate(sizeofString("world")),
|
||||||
});
|
});
|
||||||
@ -393,7 +393,7 @@ TEST_CASE("JsonVariant::set() releases the previous value") {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("JsonVariant::set() reuses extension slot") {
|
TEST_CASE("JsonVariant::set() reuses 8-bit slot") {
|
||||||
SpyingAllocator spy;
|
SpyingAllocator spy;
|
||||||
JsonDocument doc(&spy);
|
JsonDocument doc(&spy);
|
||||||
JsonVariant variant = doc.to<JsonVariant>();
|
JsonVariant variant = doc.to<JsonVariant>();
|
||||||
|
@ -29,3 +29,23 @@ set_tests_properties(Misc
|
|||||||
PROPERTIES
|
PROPERTIES
|
||||||
LABELS "Catch"
|
LABELS "Catch"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
add_executable(Issue2181
|
||||||
|
issue2181.cpp # Cannot be linked with other tests
|
||||||
|
)
|
||||||
|
|
||||||
|
set_target_properties(Issue2181 PROPERTIES UNITY_BUILD OFF)
|
||||||
|
|
||||||
|
add_test(Issue2181 Issue2181)
|
||||||
|
|
||||||
|
set_tests_properties(Issue2181
|
||||||
|
PROPERTIES
|
||||||
|
LABELS "Catch"
|
||||||
|
)
|
||||||
|
|
||||||
|
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
|
||||||
|
target_compile_options(Issue2181
|
||||||
|
PRIVATE
|
||||||
|
-Wno-keyword-macro # keyword is hidden by macro definition
|
||||||
|
)
|
||||||
|
endif()
|
||||||
|
15
extras/tests/Misc/issue2181.cpp
Normal file
15
extras/tests/Misc/issue2181.cpp
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
// ArduinoJson - https://arduinojson.org
|
||||||
|
// Copyright © 2014-2025, Benoit BLANCHON
|
||||||
|
// MIT License
|
||||||
|
|
||||||
|
#define true 0x1
|
||||||
|
#define false 0x0
|
||||||
|
|
||||||
|
#include <ArduinoJson.h>
|
||||||
|
#include <catch.hpp>
|
||||||
|
|
||||||
|
TEST_CASE("Issue #2181") {
|
||||||
|
JsonDocument doc;
|
||||||
|
doc["hello"] = "world";
|
||||||
|
REQUIRE(doc.as<std::string>() == "{\"hello\":\"world\"}");
|
||||||
|
}
|
@ -21,8 +21,8 @@ TEST_CASE("StringBuffer") {
|
|||||||
strcpy(ptr, "hi!");
|
strcpy(ptr, "hi!");
|
||||||
sb.save(&variant);
|
sb.save(&variant);
|
||||||
|
|
||||||
REQUIRE(variant.type() == VariantType::TinyString);
|
REQUIRE(variant.type == VariantType::TinyString);
|
||||||
REQUIRE(variant.asString(&resources) == "hi!");
|
REQUIRE(VariantImpl(&variant, &resources).asString() == "hi!");
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Tiny string can't contain NUL") {
|
SECTION("Tiny string can't contain NUL") {
|
||||||
@ -30,9 +30,9 @@ TEST_CASE("StringBuffer") {
|
|||||||
memcpy(ptr, "a\0b", 3);
|
memcpy(ptr, "a\0b", 3);
|
||||||
sb.save(&variant);
|
sb.save(&variant);
|
||||||
|
|
||||||
REQUIRE(variant.type() == VariantType::OwnedString);
|
REQUIRE(variant.type == VariantType::OwnedString);
|
||||||
|
|
||||||
auto str = variant.asString(&resources);
|
auto str = VariantImpl(&variant, &resources).asString();
|
||||||
REQUIRE(str.size() == 3);
|
REQUIRE(str.size() == 3);
|
||||||
REQUIRE(str.c_str()[0] == 'a');
|
REQUIRE(str.c_str()[0] == 'a');
|
||||||
REQUIRE(str.c_str()[1] == 0);
|
REQUIRE(str.c_str()[1] == 0);
|
||||||
@ -44,7 +44,7 @@ TEST_CASE("StringBuffer") {
|
|||||||
strcpy(ptr, "alfa");
|
strcpy(ptr, "alfa");
|
||||||
sb.save(&variant);
|
sb.save(&variant);
|
||||||
|
|
||||||
REQUIRE(variant.type() == VariantType::OwnedString);
|
REQUIRE(variant.type == VariantType::OwnedString);
|
||||||
REQUIRE(variant.asString(&resources) == "alfa");
|
REQUIRE(VariantImpl(&variant, &resources).asString() == "alfa");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ TEST_CASE("StringBuilder") {
|
|||||||
REQUIRE(spyingAllocator.log() == AllocatorLog{
|
REQUIRE(spyingAllocator.log() == AllocatorLog{
|
||||||
Allocate(sizeofStringBuffer()),
|
Allocate(sizeofStringBuffer()),
|
||||||
});
|
});
|
||||||
REQUIRE(data.type() == VariantType::TinyString);
|
REQUIRE(data.type == VariantType::TinyString);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Tiny string") {
|
SECTION("Tiny string") {
|
||||||
@ -45,8 +45,8 @@ TEST_CASE("StringBuilder") {
|
|||||||
str.save(&data);
|
str.save(&data);
|
||||||
|
|
||||||
REQUIRE(resources.overflowed() == false);
|
REQUIRE(resources.overflowed() == false);
|
||||||
REQUIRE(data.type() == VariantType::TinyString);
|
REQUIRE(data.type == VariantType::TinyString);
|
||||||
REQUIRE(data.asString(&resources) == "url");
|
REQUIRE(VariantImpl(&data, &resources).asString() == "url");
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Short string fits in first allocation") {
|
SECTION("Short string fits in first allocation") {
|
||||||
@ -134,10 +134,10 @@ TEST_CASE("StringBuilder::save() deduplicates strings") {
|
|||||||
auto s2 = saveString(builder, "world");
|
auto s2 = saveString(builder, "world");
|
||||||
auto s3 = saveString(builder, "hello");
|
auto s3 = saveString(builder, "hello");
|
||||||
|
|
||||||
REQUIRE(s1.asString(&resources) == "hello");
|
REQUIRE(VariantImpl(&s1, &resources).asString() == "hello");
|
||||||
REQUIRE(s2.asString(&resources) == "world");
|
REQUIRE(VariantImpl(&s2, &resources).asString() == "world");
|
||||||
REQUIRE(+s1.asString(&resources).c_str() ==
|
REQUIRE(+VariantImpl(&s1, &resources).asString().c_str() ==
|
||||||
+s3.asString(&resources).c_str()); // same address
|
+VariantImpl(&s3, &resources).asString().c_str()); // same address
|
||||||
|
|
||||||
REQUIRE(spy.log() ==
|
REQUIRE(spy.log() ==
|
||||||
AllocatorLog{
|
AllocatorLog{
|
||||||
@ -153,10 +153,11 @@ TEST_CASE("StringBuilder::save() deduplicates strings") {
|
|||||||
auto s1 = saveString(builder, "hello world");
|
auto s1 = saveString(builder, "hello world");
|
||||||
auto s2 = saveString(builder, "hello");
|
auto s2 = saveString(builder, "hello");
|
||||||
|
|
||||||
REQUIRE(s1.asString(&resources) == "hello world");
|
REQUIRE(VariantImpl(&s1, &resources).asString() == "hello world");
|
||||||
REQUIRE(s2.asString(&resources) == "hello");
|
REQUIRE(VariantImpl(&s2, &resources).asString() == "hello");
|
||||||
REQUIRE(+s2.asString(&resources).c_str() !=
|
REQUIRE(
|
||||||
+s1.asString(&resources).c_str()); // different address
|
+VariantImpl(&s1, &resources).asString().c_str() !=
|
||||||
|
+VariantImpl(&s2, &resources).asString().c_str()); // different address
|
||||||
|
|
||||||
REQUIRE(spy.log() ==
|
REQUIRE(spy.log() ==
|
||||||
AllocatorLog{
|
AllocatorLog{
|
||||||
@ -171,10 +172,11 @@ TEST_CASE("StringBuilder::save() deduplicates strings") {
|
|||||||
auto s1 = saveString(builder, "hello world");
|
auto s1 = saveString(builder, "hello world");
|
||||||
auto s2 = saveString(builder, "worl");
|
auto s2 = saveString(builder, "worl");
|
||||||
|
|
||||||
REQUIRE(s1.asString(&resources) == "hello world");
|
REQUIRE(VariantImpl(&s1, &resources).asString() == "hello world");
|
||||||
REQUIRE(s2.asString(&resources) == "worl");
|
REQUIRE(VariantImpl(&s2, &resources).asString() == "worl");
|
||||||
REQUIRE(s2.asString(&resources).c_str() !=
|
REQUIRE(
|
||||||
s1.asString(&resources).c_str()); // different address
|
VariantImpl(&s1, &resources).asString().c_str() !=
|
||||||
|
VariantImpl(&s2, &resources).asString().c_str()); // different address
|
||||||
|
|
||||||
REQUIRE(spy.log() ==
|
REQUIRE(spy.log() ==
|
||||||
AllocatorLog{
|
AllocatorLog{
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
version: "7.4.1"
|
version: "7.4.2"
|
||||||
description: >-
|
description: >-
|
||||||
A simple and efficient JSON library for embedded C++.
|
A simple and efficient JSON library for embedded C++.
|
||||||
★ 6898 stars on GitHub!
|
★ 6953 stars on GitHub!
|
||||||
Supports serialization, deserialization, MessagePack, streams, filtering, and more.
|
Supports serialization, deserialization, MessagePack, streams, filtering, and more.
|
||||||
Fully tested and documented.
|
Fully tested and documented.
|
||||||
url: https://arduinojson.org/
|
url: https://arduinojson.org/
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
{
|
{
|
||||||
"name": "ArduinoJson",
|
"name": "ArduinoJson",
|
||||||
"keywords": "json, rest, http, web",
|
"keywords": "json, rest, http, web",
|
||||||
"description": "A simple and efficient JSON library for embedded C++. ⭐ 6898 stars on GitHub! Supports serialization, deserialization, MessagePack, streams, filtering, and more. Fully tested and documented.",
|
"description": "A simple and efficient JSON library for embedded C++. ⭐ 6953 stars on GitHub! Supports serialization, deserialization, MessagePack, streams, filtering, and more. Fully tested and documented.",
|
||||||
"homepage": "https://arduinojson.org/?utm_source=meta&utm_medium=library.json",
|
"homepage": "https://arduinojson.org/?utm_source=meta&utm_medium=library.json",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/bblanchon/ArduinoJson.git"
|
"url": "https://github.com/bblanchon/ArduinoJson.git"
|
||||||
},
|
},
|
||||||
"version": "7.4.1",
|
"version": "7.4.2",
|
||||||
"authors": {
|
"authors": {
|
||||||
"name": "Benoit Blanchon",
|
"name": "Benoit Blanchon",
|
||||||
"url": "https://blog.benoitblanchon.fr"
|
"url": "https://blog.benoitblanchon.fr"
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
name=ArduinoJson
|
name=ArduinoJson
|
||||||
version=7.4.1
|
version=7.4.2
|
||||||
author=Benoit Blanchon <blog.benoitblanchon.fr>
|
author=Benoit Blanchon <blog.benoitblanchon.fr>
|
||||||
maintainer=Benoit Blanchon <blog.benoitblanchon.fr>
|
maintainer=Benoit Blanchon <blog.benoitblanchon.fr>
|
||||||
sentence=A simple and efficient JSON library for embedded C++.
|
sentence=A simple and efficient JSON library for embedded C++.
|
||||||
paragraph=⭐ 6898 stars on GitHub! Supports serialization, deserialization, MessagePack, streams, filtering, and more. Fully tested and documented.
|
paragraph=⭐ 6953 stars on GitHub! Supports serialization, deserialization, MessagePack, streams, filtering, and more. Fully tested and documented.
|
||||||
category=Data Processing
|
category=Data Processing
|
||||||
url=https://arduinojson.org/?utm_source=meta&utm_medium=library.properties
|
url=https://arduinojson.org/?utm_source=meta&utm_medium=library.properties
|
||||||
architectures=*
|
architectures=*
|
||||||
|
@ -26,6 +26,15 @@
|
|||||||
# endif
|
# endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// Remove true and false macros defined by some cores, such as Arduino Due's
|
||||||
|
// See issues #2181 and arduino/ArduinoCore-sam#50
|
||||||
|
#ifdef true
|
||||||
|
# undef true
|
||||||
|
#endif
|
||||||
|
#ifdef false
|
||||||
|
# undef false
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "ArduinoJson/Array/JsonArray.hpp"
|
#include "ArduinoJson/Array/JsonArray.hpp"
|
||||||
#include "ArduinoJson/Object/JsonObject.hpp"
|
#include "ArduinoJson/Object/JsonObject.hpp"
|
||||||
#include "ArduinoJson/Variant/JsonVariantConst.hpp"
|
#include "ArduinoJson/Variant/JsonVariantConst.hpp"
|
||||||
|
@ -8,59 +8,30 @@
|
|||||||
|
|
||||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||||
|
|
||||||
class ArrayData : public CollectionData {
|
class ArrayImpl : public CollectionImpl {
|
||||||
public:
|
public:
|
||||||
VariantData* addElement(ResourceManager* resources);
|
ArrayImpl() {}
|
||||||
|
|
||||||
static VariantData* addElement(ArrayData* array, ResourceManager* resources) {
|
ArrayImpl(CollectionData* data, ResourceManager* resources)
|
||||||
if (!array)
|
: CollectionImpl(data, resources) {}
|
||||||
return nullptr;
|
|
||||||
return array->addElement(resources);
|
VariantData* addElement();
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
bool addValue(const T& value, ResourceManager* resources);
|
bool addValue(const T& value);
|
||||||
|
|
||||||
template <typename T>
|
VariantData* getOrAddElement(size_t index);
|
||||||
static bool addValue(ArrayData* array, const T& value,
|
|
||||||
ResourceManager* resources) {
|
|
||||||
if (!array)
|
|
||||||
return false;
|
|
||||||
return array->addValue(value, resources);
|
|
||||||
}
|
|
||||||
|
|
||||||
VariantData* getOrAddElement(size_t index, ResourceManager* resources);
|
VariantData* getElement(size_t index) const;
|
||||||
|
|
||||||
VariantData* getElement(size_t index, const ResourceManager* resources) const;
|
void removeElement(size_t index);
|
||||||
|
|
||||||
static VariantData* getElement(const ArrayData* array, size_t index,
|
void remove(iterator it) {
|
||||||
const ResourceManager* resources) {
|
CollectionImpl::removeOne(it);
|
||||||
if (!array)
|
|
||||||
return nullptr;
|
|
||||||
return array->getElement(index, resources);
|
|
||||||
}
|
|
||||||
|
|
||||||
void removeElement(size_t index, ResourceManager* resources);
|
|
||||||
|
|
||||||
static void removeElement(ArrayData* array, size_t index,
|
|
||||||
ResourceManager* resources) {
|
|
||||||
if (!array)
|
|
||||||
return;
|
|
||||||
array->removeElement(index, resources);
|
|
||||||
}
|
|
||||||
|
|
||||||
void remove(iterator it, ResourceManager* resources) {
|
|
||||||
CollectionData::removeOne(it, resources);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void remove(ArrayData* array, iterator it,
|
|
||||||
ResourceManager* resources) {
|
|
||||||
if (array)
|
|
||||||
return array->remove(it, resources);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
iterator at(size_t index, const ResourceManager* resources) const;
|
iterator at(size_t index) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -10,36 +10,37 @@
|
|||||||
|
|
||||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||||
|
|
||||||
inline ArrayData::iterator ArrayData::at(
|
inline ArrayImpl::iterator ArrayImpl::at(size_t index) const {
|
||||||
size_t index, const ResourceManager* resources) const {
|
auto it = createIterator();
|
||||||
auto it = createIterator(resources);
|
|
||||||
while (!it.done() && index) {
|
while (!it.done() && index) {
|
||||||
it.next(resources);
|
it.next(resources_);
|
||||||
--index;
|
--index;
|
||||||
}
|
}
|
||||||
return it;
|
return it;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline VariantData* ArrayData::addElement(ResourceManager* resources) {
|
inline VariantData* ArrayImpl::addElement() {
|
||||||
auto slot = resources->allocVariant();
|
if (!data_)
|
||||||
|
return nullptr;
|
||||||
|
ARDUINOJSON_ASSERT(resources_ != nullptr);
|
||||||
|
auto slot = resources_->allocVariant();
|
||||||
if (!slot)
|
if (!slot)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
CollectionData::appendOne(slot, resources);
|
CollectionImpl::appendOne(slot);
|
||||||
return slot.ptr();
|
return slot.ptr();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline VariantData* ArrayData::getOrAddElement(size_t index,
|
inline VariantData* ArrayImpl::getOrAddElement(size_t index) {
|
||||||
ResourceManager* resources) {
|
auto it = createIterator();
|
||||||
auto it = createIterator(resources);
|
|
||||||
while (!it.done() && index > 0) {
|
while (!it.done() && index > 0) {
|
||||||
it.next(resources);
|
it.next(resources_);
|
||||||
index--;
|
index--;
|
||||||
}
|
}
|
||||||
if (it.done())
|
if (it.done())
|
||||||
index++;
|
index++;
|
||||||
VariantData* element = it.data();
|
VariantData* element = it.data();
|
||||||
while (index > 0) {
|
while (index > 0) {
|
||||||
element = addElement(resources);
|
element = addElement();
|
||||||
if (!element)
|
if (!element)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
index--;
|
index--;
|
||||||
@ -47,33 +48,34 @@ inline VariantData* ArrayData::getOrAddElement(size_t index,
|
|||||||
return element;
|
return element;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline VariantData* ArrayData::getElement(
|
inline VariantData* ArrayImpl::getElement(size_t index) const {
|
||||||
size_t index, const ResourceManager* resources) const {
|
return at(index).data();
|
||||||
return at(index, resources).data();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void ArrayData::removeElement(size_t index, ResourceManager* resources) {
|
inline void ArrayImpl::removeElement(size_t index) {
|
||||||
remove(at(index, resources), resources);
|
remove(at(index));
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline bool ArrayData::addValue(const T& value, ResourceManager* resources) {
|
inline bool ArrayImpl::addValue(const T& value) {
|
||||||
ARDUINOJSON_ASSERT(resources != nullptr);
|
if (!data_)
|
||||||
auto slot = resources->allocVariant();
|
return false;
|
||||||
|
ARDUINOJSON_ASSERT(resources_ != nullptr);
|
||||||
|
auto slot = resources_->allocVariant();
|
||||||
if (!slot)
|
if (!slot)
|
||||||
return false;
|
return false;
|
||||||
JsonVariant variant(slot.ptr(), resources);
|
JsonVariant variant(slot.ptr(), resources_);
|
||||||
if (!variant.set(value)) {
|
if (!variant.set(value)) {
|
||||||
resources->freeVariant(slot);
|
resources_->freeVariant(slot);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
CollectionData::appendOne(slot, resources);
|
CollectionImpl::appendOne(slot);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the size (in bytes) of an array with n elements.
|
// Returns the size (in bytes) of an array with n elements.
|
||||||
constexpr size_t sizeofArray(size_t n) {
|
constexpr size_t sizeofArray(size_t n) {
|
||||||
return n * ResourceManager::slotSize;
|
return n * sizeof(VariantData);
|
||||||
}
|
}
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -55,17 +55,12 @@ class ElementProxy : public VariantRefBase<ElementProxy<TUpstream>>,
|
|||||||
}
|
}
|
||||||
|
|
||||||
FORCE_INLINE VariantData* getData() const {
|
FORCE_INLINE VariantData* getData() const {
|
||||||
return VariantData::getElement(
|
return VariantAttorney::getVariantImpl(upstream_).getElement(index_);
|
||||||
VariantAttorney::getData(upstream_), index_,
|
|
||||||
VariantAttorney::getResourceManager(upstream_));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
VariantData* getOrCreateData() const {
|
VariantData* getOrCreateData() const {
|
||||||
auto data = VariantAttorney::getOrCreateData(upstream_);
|
return VariantAttorney::getOrCreateVariantImpl(upstream_).getOrAddElement(
|
||||||
if (!data)
|
index_);
|
||||||
return nullptr;
|
|
||||||
return data->getOrAddElement(
|
|
||||||
index_, VariantAttorney::getResourceManager(upstream_));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TUpstream upstream_;
|
TUpstream upstream_;
|
||||||
|
@ -20,24 +20,25 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
|
|||||||
using iterator = JsonArrayIterator;
|
using iterator = JsonArrayIterator;
|
||||||
|
|
||||||
// Constructs an unbound reference.
|
// Constructs an unbound reference.
|
||||||
JsonArray() : data_(0), resources_(0) {}
|
JsonArray() {}
|
||||||
|
|
||||||
// INTERNAL USE ONLY
|
// INTERNAL USE ONLY
|
||||||
JsonArray(detail::ArrayData* data, detail::ResourceManager* resources)
|
JsonArray(detail::VariantData* data, detail::ResourceManager* resources)
|
||||||
: data_(data), resources_(resources) {}
|
: impl_(detail::VariantImpl(data, resources).asArray()) {}
|
||||||
|
|
||||||
|
// INTERNAL USE ONLY
|
||||||
|
JsonArray(const detail::ArrayImpl& impl) : impl_(impl) {}
|
||||||
|
|
||||||
// Returns a JsonVariant pointing to the array.
|
// Returns a JsonVariant pointing to the array.
|
||||||
// https://arduinojson.org/v7/api/jsonvariant/
|
// https://arduinojson.org/v7/api/jsonvariant/
|
||||||
operator JsonVariant() {
|
operator JsonVariant() {
|
||||||
void* data = data_; // prevent warning cast-align
|
return JsonVariant(getData(), getResourceManager());
|
||||||
return JsonVariant(reinterpret_cast<detail::VariantData*>(data),
|
|
||||||
resources_);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns a read-only reference to the array.
|
// Returns a read-only reference to the array.
|
||||||
// https://arduinojson.org/v7/api/jsonarrayconst/
|
// https://arduinojson.org/v7/api/jsonarrayconst/
|
||||||
operator JsonArrayConst() const {
|
operator JsonArrayConst() const {
|
||||||
return JsonArrayConst(data_, resources_);
|
return JsonArrayConst(getData(), getResourceManager());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Appends a new (empty) element to the array.
|
// Appends a new (empty) element to the array.
|
||||||
@ -55,15 +56,14 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
|
|||||||
template <typename T, detail::enable_if_t<
|
template <typename T, detail::enable_if_t<
|
||||||
detail::is_same<T, JsonVariant>::value, int> = 0>
|
detail::is_same<T, JsonVariant>::value, int> = 0>
|
||||||
JsonVariant add() const {
|
JsonVariant add() const {
|
||||||
return JsonVariant(detail::ArrayData::addElement(data_, resources_),
|
return JsonVariant(impl_.addElement(), impl_.getResourceManager());
|
||||||
resources_);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Appends a value to the array.
|
// Appends a value to the array.
|
||||||
// https://arduinojson.org/v7/api/jsonarray/add/
|
// https://arduinojson.org/v7/api/jsonarray/add/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
bool add(const T& value) const {
|
bool add(const T& value) const {
|
||||||
return detail::ArrayData::addValue(data_, value, resources_);
|
return impl_.addValue(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Appends a value to the array.
|
// Appends a value to the array.
|
||||||
@ -71,15 +71,13 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
|
|||||||
template <typename T,
|
template <typename T,
|
||||||
detail::enable_if_t<!detail::is_const<T>::value, int> = 0>
|
detail::enable_if_t<!detail::is_const<T>::value, int> = 0>
|
||||||
bool add(T* value) const {
|
bool add(T* value) const {
|
||||||
return detail::ArrayData::addValue(data_, value, resources_);
|
return impl_.addValue(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns an iterator to the first element of the array.
|
// Returns an iterator to the first element of the array.
|
||||||
// https://arduinojson.org/v7/api/jsonarray/begin/
|
// https://arduinojson.org/v7/api/jsonarray/begin/
|
||||||
iterator begin() const {
|
iterator begin() const {
|
||||||
if (!data_)
|
return iterator(impl_.createIterator(), impl_.getResourceManager());
|
||||||
return iterator();
|
|
||||||
return iterator(data_->createIterator(resources_), resources_);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns an iterator following the last element of the array.
|
// Returns an iterator following the last element of the array.
|
||||||
@ -91,7 +89,7 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
|
|||||||
// Copies an array.
|
// Copies an array.
|
||||||
// https://arduinojson.org/v7/api/jsonarray/set/
|
// https://arduinojson.org/v7/api/jsonarray/set/
|
||||||
bool set(JsonArrayConst src) const {
|
bool set(JsonArrayConst src) const {
|
||||||
if (!data_)
|
if (isNull())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
clear();
|
clear();
|
||||||
@ -106,13 +104,13 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
|
|||||||
// Removes the element at the specified iterator.
|
// Removes the element at the specified iterator.
|
||||||
// https://arduinojson.org/v7/api/jsonarray/remove/
|
// https://arduinojson.org/v7/api/jsonarray/remove/
|
||||||
void remove(iterator it) const {
|
void remove(iterator it) const {
|
||||||
detail::ArrayData::remove(data_, it.iterator_, resources_);
|
impl_.remove(it.iterator_);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Removes the element at the specified index.
|
// Removes the element at the specified index.
|
||||||
// https://arduinojson.org/v7/api/jsonarray/remove/
|
// https://arduinojson.org/v7/api/jsonarray/remove/
|
||||||
void remove(size_t index) const {
|
void remove(size_t index) const {
|
||||||
detail::ArrayData::removeElement(data_, index, resources_);
|
impl_.removeElement(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Removes the element at the specified index.
|
// Removes the element at the specified index.
|
||||||
@ -127,7 +125,7 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
|
|||||||
// Removes all the elements of the array.
|
// Removes all the elements of the array.
|
||||||
// https://arduinojson.org/v7/api/jsonarray/clear/
|
// https://arduinojson.org/v7/api/jsonarray/clear/
|
||||||
void clear() const {
|
void clear() const {
|
||||||
detail::ArrayData::clear(data_, resources_);
|
impl_.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gets or sets the element at the specified index.
|
// Gets or sets the element at the specified index.
|
||||||
@ -150,31 +148,31 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
operator JsonVariantConst() const {
|
operator JsonVariantConst() const {
|
||||||
return JsonVariantConst(collectionToVariant(data_), resources_);
|
return JsonVariantConst(getData(), getResourceManager());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if the reference is unbound.
|
// Returns true if the reference is unbound.
|
||||||
// https://arduinojson.org/v7/api/jsonarray/isnull/
|
// https://arduinojson.org/v7/api/jsonarray/isnull/
|
||||||
bool isNull() const {
|
bool isNull() const {
|
||||||
return data_ == 0;
|
return impl_.isNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if the reference is bound.
|
// Returns true if the reference is bound.
|
||||||
// https://arduinojson.org/v7/api/jsonarray/isnull/
|
// https://arduinojson.org/v7/api/jsonarray/isnull/
|
||||||
operator bool() const {
|
operator bool() const {
|
||||||
return data_ != 0;
|
return !isNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the depth (nesting level) of the array.
|
// Returns the depth (nesting level) of the array.
|
||||||
// https://arduinojson.org/v7/api/jsonarray/nesting/
|
// https://arduinojson.org/v7/api/jsonarray/nesting/
|
||||||
size_t nesting() const {
|
size_t nesting() const {
|
||||||
return detail::VariantData::nesting(collectionToVariant(data_), resources_);
|
return impl_.nesting();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the number of elements in the array.
|
// Returns the number of elements in the array.
|
||||||
// https://arduinojson.org/v7/api/jsonarray/size/
|
// https://arduinojson.org/v7/api/jsonarray/size/
|
||||||
size_t size() const {
|
size_t size() const {
|
||||||
return data_ ? data_->size(resources_) : 0;
|
return impl_.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
// DEPRECATED: use add<JsonVariant>() instead
|
// DEPRECATED: use add<JsonVariant>() instead
|
||||||
@ -201,19 +199,18 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
detail::ResourceManager* getResourceManager() const {
|
detail::ResourceManager* getResourceManager() const {
|
||||||
return resources_;
|
return impl_.getResourceManager();
|
||||||
}
|
}
|
||||||
|
|
||||||
detail::VariantData* getData() const {
|
detail::VariantData* getData() const {
|
||||||
return collectionToVariant(data_);
|
return impl_.getData();
|
||||||
}
|
}
|
||||||
|
|
||||||
detail::VariantData* getOrCreateData() const {
|
detail::VariantData* getOrCreateData() const {
|
||||||
return collectionToVariant(data_);
|
return impl_.getData();
|
||||||
}
|
}
|
||||||
|
|
||||||
detail::ArrayData* data_;
|
mutable detail::ArrayImpl impl_;
|
||||||
detail::ResourceManager* resources_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
||||||
|
@ -24,9 +24,7 @@ class JsonArrayConst : public detail::VariantOperators<JsonArrayConst> {
|
|||||||
// Returns an iterator to the first element of the array.
|
// Returns an iterator to the first element of the array.
|
||||||
// https://arduinojson.org/v7/api/jsonarrayconst/begin/
|
// https://arduinojson.org/v7/api/jsonarrayconst/begin/
|
||||||
iterator begin() const {
|
iterator begin() const {
|
||||||
if (!data_)
|
return iterator(impl_.createIterator(), impl_.getResourceManager());
|
||||||
return iterator();
|
|
||||||
return iterator(data_->createIterator(resources_), resources_);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns an iterator to the element following the last element of the array.
|
// Returns an iterator to the element following the last element of the array.
|
||||||
@ -36,21 +34,22 @@ class JsonArrayConst : public detail::VariantOperators<JsonArrayConst> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Creates an unbound reference.
|
// Creates an unbound reference.
|
||||||
JsonArrayConst() : data_(0), resources_(0) {}
|
JsonArrayConst() {}
|
||||||
|
|
||||||
// INTERNAL USE ONLY
|
// INTERNAL USE ONLY
|
||||||
JsonArrayConst(const detail::ArrayData* data,
|
JsonArrayConst(detail::VariantData* data, detail::ResourceManager* resources)
|
||||||
const detail::ResourceManager* resources)
|
: impl_(detail::VariantImpl(data, resources).asArray()) {}
|
||||||
: data_(data), resources_(resources) {}
|
|
||||||
|
// INTERNAL USE ONLY
|
||||||
|
JsonArrayConst(const detail::ArrayImpl& impl) : impl_(impl) {}
|
||||||
|
|
||||||
// Returns the element at the specified index.
|
// Returns the element at the specified index.
|
||||||
// https://arduinojson.org/v7/api/jsonarrayconst/subscript/
|
// https://arduinojson.org/v7/api/jsonarrayconst/subscript/
|
||||||
template <typename T,
|
template <typename T,
|
||||||
detail::enable_if_t<detail::is_integral<T>::value, int> = 0>
|
detail::enable_if_t<detail::is_integral<T>::value, int> = 0>
|
||||||
JsonVariantConst operator[](T index) const {
|
JsonVariantConst operator[](T index) const {
|
||||||
return JsonVariantConst(
|
return JsonVariantConst(impl_.getElement(size_t(index)),
|
||||||
detail::ArrayData::getElement(data_, size_t(index), resources_),
|
impl_.getResourceManager());
|
||||||
resources_);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the element at the specified index.
|
// Returns the element at the specified index.
|
||||||
@ -65,31 +64,31 @@ class JsonArrayConst : public detail::VariantOperators<JsonArrayConst> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
operator JsonVariantConst() const {
|
operator JsonVariantConst() const {
|
||||||
return JsonVariantConst(getData(), resources_);
|
return JsonVariantConst(impl_.getData(), impl_.getResourceManager());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if the reference is unbound.
|
// Returns true if the reference is unbound.
|
||||||
// https://arduinojson.org/v7/api/jsonarrayconst/isnull/
|
// https://arduinojson.org/v7/api/jsonarrayconst/isnull/
|
||||||
bool isNull() const {
|
bool isNull() const {
|
||||||
return data_ == 0;
|
return impl_.isNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if the reference is bound.
|
// Returns true if the reference is bound.
|
||||||
// https://arduinojson.org/v7/api/jsonarrayconst/isnull/
|
// https://arduinojson.org/v7/api/jsonarrayconst/isnull/
|
||||||
operator bool() const {
|
operator bool() const {
|
||||||
return data_ != 0;
|
return !isNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the depth (nesting level) of the array.
|
// Returns the depth (nesting level) of the array.
|
||||||
// https://arduinojson.org/v7/api/jsonarrayconst/nesting/
|
// https://arduinojson.org/v7/api/jsonarrayconst/nesting/
|
||||||
size_t nesting() const {
|
size_t nesting() const {
|
||||||
return detail::VariantData::nesting(getData(), resources_);
|
return impl_.nesting();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the number of elements in the array.
|
// Returns the number of elements in the array.
|
||||||
// https://arduinojson.org/v7/api/jsonarrayconst/size/
|
// https://arduinojson.org/v7/api/jsonarrayconst/size/
|
||||||
size_t size() const {
|
size_t size() const {
|
||||||
return data_ ? data_->size(resources_) : 0;
|
return impl_.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
// DEPRECATED: always returns zero
|
// DEPRECATED: always returns zero
|
||||||
@ -100,11 +99,10 @@ class JsonArrayConst : public detail::VariantOperators<JsonArrayConst> {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
const detail::VariantData* getData() const {
|
const detail::VariantData* getData() const {
|
||||||
return collectionToVariant(data_);
|
return impl_.getData();
|
||||||
}
|
}
|
||||||
|
|
||||||
const detail::ArrayData* data_;
|
detail::ArrayImpl impl_;
|
||||||
const detail::ResourceManager* resources_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Compares the content of two arrays.
|
// Compares the content of two arrays.
|
||||||
|
@ -30,7 +30,7 @@ class JsonArrayIterator {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
JsonArrayIterator() {}
|
JsonArrayIterator() {}
|
||||||
explicit JsonArrayIterator(detail::ArrayData::iterator iterator,
|
explicit JsonArrayIterator(detail::ArrayImpl::iterator iterator,
|
||||||
detail::ResourceManager* resources)
|
detail::ResourceManager* resources)
|
||||||
: iterator_(iterator), resources_(resources) {}
|
: iterator_(iterator), resources_(resources) {}
|
||||||
|
|
||||||
@ -55,7 +55,7 @@ class JsonArrayIterator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
detail::ArrayData::iterator iterator_;
|
detail::ArrayImpl::iterator iterator_;
|
||||||
detail::ResourceManager* resources_;
|
detail::ResourceManager* resources_;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -64,8 +64,8 @@ class JsonArrayConstIterator {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
JsonArrayConstIterator() {}
|
JsonArrayConstIterator() {}
|
||||||
explicit JsonArrayConstIterator(detail::ArrayData::iterator iterator,
|
explicit JsonArrayConstIterator(detail::ArrayImpl::iterator iterator,
|
||||||
const detail::ResourceManager* resources)
|
detail::ResourceManager* resources)
|
||||||
: iterator_(iterator), resources_(resources) {}
|
: iterator_(iterator), resources_(resources) {}
|
||||||
|
|
||||||
JsonVariantConst operator*() const {
|
JsonVariantConst operator*() const {
|
||||||
@ -89,8 +89,8 @@ class JsonArrayConstIterator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
detail::ArrayData::iterator iterator_;
|
mutable detail::ArrayImpl::iterator iterator_;
|
||||||
const detail::ResourceManager* resources_;
|
mutable detail::ResourceManager* resources_;
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
||||||
|
@ -12,11 +12,11 @@
|
|||||||
|
|
||||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||||
|
|
||||||
class VariantData;
|
struct VariantData;
|
||||||
class ResourceManager;
|
class ResourceManager;
|
||||||
|
|
||||||
class CollectionIterator {
|
class CollectionIterator {
|
||||||
friend class CollectionData;
|
friend class CollectionImpl;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
CollectionIterator() : slot_(nullptr), currentId_(NULL_SLOT) {}
|
CollectionIterator() : slot_(nullptr), currentId_(NULL_SLOT) {}
|
||||||
@ -51,11 +51,11 @@ class CollectionIterator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
VariantData* data() {
|
VariantData* data() {
|
||||||
return reinterpret_cast<VariantData*>(slot_);
|
return slot_;
|
||||||
}
|
}
|
||||||
|
|
||||||
const VariantData* data() const {
|
const VariantData* data() const {
|
||||||
return reinterpret_cast<const VariantData*>(slot_);
|
return slot_;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -65,58 +65,68 @@ class CollectionIterator {
|
|||||||
SlotId currentId_, nextId_;
|
SlotId currentId_, nextId_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class CollectionData {
|
struct CollectionData {
|
||||||
SlotId head_ = NULL_SLOT;
|
SlotId head = NULL_SLOT;
|
||||||
SlotId tail_ = NULL_SLOT;
|
SlotId tail = NULL_SLOT;
|
||||||
|
|
||||||
public:
|
|
||||||
// Placement new
|
// Placement new
|
||||||
static void* operator new(size_t, void* p) noexcept {
|
static void* operator new(size_t, void* p) noexcept {
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void operator delete(void*, void*) noexcept {}
|
static void operator delete(void*, void*) noexcept {}
|
||||||
|
};
|
||||||
|
|
||||||
|
class CollectionImpl {
|
||||||
|
protected:
|
||||||
|
CollectionData* data_;
|
||||||
|
ResourceManager* resources_;
|
||||||
|
|
||||||
|
public:
|
||||||
using iterator = CollectionIterator;
|
using iterator = CollectionIterator;
|
||||||
|
|
||||||
iterator createIterator(const ResourceManager* resources) const;
|
CollectionImpl() : data_(nullptr), resources_(nullptr) {}
|
||||||
|
|
||||||
size_t size(const ResourceManager*) const;
|
CollectionImpl(CollectionData* data, ResourceManager* resources)
|
||||||
size_t nesting(const ResourceManager*) const;
|
: data_(data), resources_(resources) {}
|
||||||
|
|
||||||
void clear(ResourceManager* resources);
|
explicit operator bool() const {
|
||||||
|
return data_ != nullptr;
|
||||||
static void clear(CollectionData* collection, ResourceManager* resources) {
|
|
||||||
if (!collection)
|
|
||||||
return;
|
|
||||||
collection->clear(resources);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool isNull() const {
|
||||||
|
return data_ == nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
VariantData* getData() const {
|
||||||
|
void* data = data_; // prevent warning cast-align
|
||||||
|
return reinterpret_cast<VariantData*>(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
ResourceManager* getResourceManager() const {
|
||||||
|
return resources_;
|
||||||
|
}
|
||||||
|
|
||||||
|
iterator createIterator() const;
|
||||||
|
|
||||||
|
size_t size() const;
|
||||||
|
size_t nesting() const;
|
||||||
|
|
||||||
|
void clear();
|
||||||
|
|
||||||
SlotId head() const {
|
SlotId head() const {
|
||||||
return head_;
|
return data_->head;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void appendOne(Slot<VariantData> slot, const ResourceManager* resources);
|
void appendOne(Slot<VariantData> slot);
|
||||||
void appendPair(Slot<VariantData> key, Slot<VariantData> value,
|
void appendPair(Slot<VariantData> key, Slot<VariantData> value);
|
||||||
const ResourceManager* resources);
|
|
||||||
|
|
||||||
void removeOne(iterator it, ResourceManager* resources);
|
void removeOne(iterator it);
|
||||||
void removePair(iterator it, ResourceManager* resources);
|
void removePair(iterator it);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Slot<VariantData> getPreviousSlot(VariantData*, const ResourceManager*) const;
|
Slot<VariantData> getPreviousSlot(VariantData*) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
inline const VariantData* collectionToVariant(
|
|
||||||
const CollectionData* collection) {
|
|
||||||
const void* data = collection; // prevent warning cast-align
|
|
||||||
return reinterpret_cast<const VariantData*>(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline VariantData* collectionToVariant(CollectionData* collection) {
|
|
||||||
void* data = collection; // prevent warning cast-align
|
|
||||||
return reinterpret_cast<VariantData*>(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -14,7 +14,7 @@ ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
|||||||
|
|
||||||
inline CollectionIterator::CollectionIterator(VariantData* slot, SlotId slotId)
|
inline CollectionIterator::CollectionIterator(VariantData* slot, SlotId slotId)
|
||||||
: slot_(slot), currentId_(slotId) {
|
: slot_(slot), currentId_(slotId) {
|
||||||
nextId_ = slot_ ? slot_->next() : NULL_SLOT;
|
nextId_ = slot_ ? slot_->next : NULL_SLOT;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void CollectionIterator::next(const ResourceManager* resources) {
|
inline void CollectionIterator::next(const ResourceManager* resources) {
|
||||||
@ -22,114 +22,123 @@ inline void CollectionIterator::next(const ResourceManager* resources) {
|
|||||||
slot_ = resources->getVariant(nextId_);
|
slot_ = resources->getVariant(nextId_);
|
||||||
currentId_ = nextId_;
|
currentId_ = nextId_;
|
||||||
if (slot_)
|
if (slot_)
|
||||||
nextId_ = slot_->next();
|
nextId_ = slot_->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline CollectionData::iterator CollectionData::createIterator(
|
inline CollectionImpl::iterator CollectionImpl::createIterator() const {
|
||||||
const ResourceManager* resources) const {
|
if (!data_)
|
||||||
return iterator(resources->getVariant(head_), head_);
|
return iterator();
|
||||||
|
return iterator(resources_->getVariant(data_->head), data_->head);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void CollectionData::appendOne(Slot<VariantData> slot,
|
inline void CollectionImpl::appendOne(Slot<VariantData> slot) {
|
||||||
const ResourceManager* resources) {
|
ARDUINOJSON_ASSERT(data_ != nullptr);
|
||||||
if (tail_ != NULL_SLOT) {
|
ARDUINOJSON_ASSERT(resources_ != nullptr);
|
||||||
auto tail = resources->getVariant(tail_);
|
|
||||||
tail->setNext(slot.id());
|
if (data_->tail != NULL_SLOT) {
|
||||||
tail_ = slot.id();
|
auto tail = resources_->getVariant(data_->tail);
|
||||||
|
tail->next = slot.id();
|
||||||
|
data_->tail = slot.id();
|
||||||
} else {
|
} else {
|
||||||
head_ = slot.id();
|
data_->head = slot.id();
|
||||||
tail_ = slot.id();
|
data_->tail = slot.id();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void CollectionData::appendPair(Slot<VariantData> key,
|
inline void CollectionImpl::appendPair(Slot<VariantData> key,
|
||||||
Slot<VariantData> value,
|
Slot<VariantData> value) {
|
||||||
const ResourceManager* resources) {
|
ARDUINOJSON_ASSERT(data_ != nullptr);
|
||||||
key->setNext(value.id());
|
ARDUINOJSON_ASSERT(resources_ != nullptr);
|
||||||
|
|
||||||
if (tail_ != NULL_SLOT) {
|
key->next = value.id();
|
||||||
auto tail = resources->getVariant(tail_);
|
|
||||||
tail->setNext(key.id());
|
if (data_->tail != NULL_SLOT) {
|
||||||
tail_ = value.id();
|
auto tail = resources_->getVariant(data_->tail);
|
||||||
|
tail->next = key.id();
|
||||||
|
data_->tail = value.id();
|
||||||
} else {
|
} else {
|
||||||
head_ = key.id();
|
data_->head = key.id();
|
||||||
tail_ = value.id();
|
data_->tail = value.id();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void CollectionData::clear(ResourceManager* resources) {
|
inline void CollectionImpl::clear() {
|
||||||
auto next = head_;
|
if (!data_)
|
||||||
|
return;
|
||||||
|
auto next = data_->head;
|
||||||
while (next != NULL_SLOT) {
|
while (next != NULL_SLOT) {
|
||||||
auto currId = next;
|
auto currId = next;
|
||||||
auto slot = resources->getVariant(next);
|
auto slot = resources_->getVariant(next);
|
||||||
next = slot->next();
|
next = slot->next;
|
||||||
resources->freeVariant({slot, currId});
|
resources_->freeVariant({slot, currId});
|
||||||
}
|
}
|
||||||
|
|
||||||
head_ = NULL_SLOT;
|
data_->head = NULL_SLOT;
|
||||||
tail_ = NULL_SLOT;
|
data_->tail = NULL_SLOT;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Slot<VariantData> CollectionData::getPreviousSlot(
|
inline Slot<VariantData> CollectionImpl::getPreviousSlot(
|
||||||
VariantData* target, const ResourceManager* resources) const {
|
VariantData* target) const {
|
||||||
auto prev = Slot<VariantData>();
|
auto prev = Slot<VariantData>();
|
||||||
auto currentId = head_;
|
auto currentId = data_->head;
|
||||||
while (currentId != NULL_SLOT) {
|
while (currentId != NULL_SLOT) {
|
||||||
auto currentSlot = resources->getVariant(currentId);
|
auto currentSlot = resources_->getVariant(currentId);
|
||||||
if (currentSlot == target)
|
if (currentSlot == target)
|
||||||
break;
|
break;
|
||||||
prev = Slot<VariantData>(currentSlot, currentId);
|
prev = Slot<VariantData>(currentSlot, currentId);
|
||||||
currentId = currentSlot->next();
|
currentId = currentSlot->next;
|
||||||
}
|
}
|
||||||
return prev;
|
return prev;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void CollectionData::removeOne(iterator it, ResourceManager* resources) {
|
inline void CollectionImpl::removeOne(iterator it) {
|
||||||
if (it.done())
|
if (it.done())
|
||||||
return;
|
return;
|
||||||
auto curr = it.slot_;
|
auto curr = it.slot_;
|
||||||
auto prev = getPreviousSlot(curr, resources);
|
auto prev = getPreviousSlot(curr);
|
||||||
auto next = curr->next();
|
auto next = curr->next;
|
||||||
if (prev)
|
if (prev)
|
||||||
prev->setNext(next);
|
prev->next = next;
|
||||||
else
|
else
|
||||||
head_ = next;
|
data_->head = next;
|
||||||
if (next == NULL_SLOT)
|
if (next == NULL_SLOT)
|
||||||
tail_ = prev.id();
|
data_->tail = prev.id();
|
||||||
resources->freeVariant({it.slot_, it.currentId_});
|
resources_->freeVariant({it.slot_, it.currentId_});
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void CollectionData::removePair(ObjectData::iterator it,
|
inline void CollectionImpl::removePair(ObjectImpl::iterator it) {
|
||||||
ResourceManager* resources) {
|
|
||||||
if (it.done())
|
if (it.done())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
auto keySlot = it.slot_;
|
auto keySlot = it.slot_;
|
||||||
|
|
||||||
auto valueId = it.nextId_;
|
auto valueId = it.nextId_;
|
||||||
auto valueSlot = resources->getVariant(valueId);
|
auto valueSlot = resources_->getVariant(valueId);
|
||||||
|
|
||||||
// remove value slot
|
// remove value slot
|
||||||
keySlot->setNext(valueSlot->next());
|
keySlot->next = valueSlot->next;
|
||||||
resources->freeVariant({valueSlot, valueId});
|
resources_->freeVariant({valueSlot, valueId});
|
||||||
|
|
||||||
// remove key slot
|
// remove key slot
|
||||||
removeOne(it, resources);
|
removeOne(it);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline size_t CollectionData::nesting(const ResourceManager* resources) const {
|
inline size_t CollectionImpl::nesting() const {
|
||||||
|
if (!data_)
|
||||||
|
return 0;
|
||||||
size_t maxChildNesting = 0;
|
size_t maxChildNesting = 0;
|
||||||
for (auto it = createIterator(resources); !it.done(); it.next(resources)) {
|
for (auto it = createIterator(); !it.done(); it.next(resources_)) {
|
||||||
size_t childNesting = it->nesting(resources);
|
VariantImpl variant(it.data(), resources_);
|
||||||
|
size_t childNesting = variant.nesting();
|
||||||
if (childNesting > maxChildNesting)
|
if (childNesting > maxChildNesting)
|
||||||
maxChildNesting = childNesting;
|
maxChildNesting = childNesting;
|
||||||
}
|
}
|
||||||
return maxChildNesting + 1;
|
return maxChildNesting + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline size_t CollectionData::size(const ResourceManager* resources) const {
|
inline size_t CollectionImpl::size() const {
|
||||||
size_t count = 0;
|
size_t count = 0;
|
||||||
for (auto it = createIterator(resources); !it.done(); it.next(resources))
|
for (auto it = createIterator(); !it.done(); it.next(resources_))
|
||||||
count++;
|
count++;
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
@ -274,9 +274,9 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_DOUBLE
|
#if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_DOUBLE
|
||||||
# define ARDUINOJSON_USE_EXTENSIONS 1
|
# define ARDUINOJSON_USE_8_BYTE_POOL 1
|
||||||
#else
|
#else
|
||||||
# define ARDUINOJSON_USE_EXTENSIONS 0
|
# define ARDUINOJSON_USE_8_BYTE_POOL 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(nullptr)
|
#if defined(nullptr)
|
||||||
|
@ -50,7 +50,7 @@ DeserializationError doDeserialize(TDestination&& dst, TReader reader,
|
|||||||
auto resources = VariantAttorney::getResourceManager(dst);
|
auto resources = VariantAttorney::getResourceManager(dst);
|
||||||
dst.clear();
|
dst.clear();
|
||||||
auto err = TDeserializer<TReader>(resources, reader)
|
auto err = TDeserializer<TReader>(resources, reader)
|
||||||
.parse(*data, options.filter, options.nestingLimit);
|
.parse(data, options.filter, options.nestingLimit);
|
||||||
shrinkJsonDocument(dst);
|
shrinkJsonDocument(dst);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
@ -88,7 +88,7 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
|||||||
// https://arduinojson.org/v7/api/jsondocument/clear/
|
// https://arduinojson.org/v7/api/jsondocument/clear/
|
||||||
void clear() {
|
void clear() {
|
||||||
resources_.clear();
|
resources_.clear();
|
||||||
data_.reset();
|
data_.type = detail::VariantType::Null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if the root is of the specified type.
|
// Returns true if the root is of the specified type.
|
||||||
@ -120,13 +120,13 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
|||||||
// Returns the depth (nesting level) of the array.
|
// Returns the depth (nesting level) of the array.
|
||||||
// https://arduinojson.org/v7/api/jsondocument/nesting/
|
// https://arduinojson.org/v7/api/jsondocument/nesting/
|
||||||
size_t nesting() const {
|
size_t nesting() const {
|
||||||
return data_.nesting(&resources_);
|
return getVariantImpl().nesting();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the number of elements in the root array or object.
|
// Returns the number of elements in the root array or object.
|
||||||
// https://arduinojson.org/v7/api/jsondocument/size/
|
// https://arduinojson.org/v7/api/jsondocument/size/
|
||||||
size_t size() const {
|
size_t size() const {
|
||||||
return data_.size(&resources_);
|
return getVariantImpl().size();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copies the specified document.
|
// Copies the specified document.
|
||||||
@ -165,7 +165,7 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
|||||||
template <typename TChar>
|
template <typename TChar>
|
||||||
ARDUINOJSON_DEPRECATED("use doc[\"key\"].is<T>() instead")
|
ARDUINOJSON_DEPRECATED("use doc[\"key\"].is<T>() instead")
|
||||||
bool containsKey(TChar* key) const {
|
bool containsKey(TChar* key) const {
|
||||||
return data_.getMember(detail::adaptString(key), &resources_) != 0;
|
return getVariantImpl().getMember(detail::adaptString(key)) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// DEPRECATED: use obj[key].is<T>() instead
|
// DEPRECATED: use obj[key].is<T>() instead
|
||||||
@ -174,7 +174,7 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
|||||||
detail::enable_if_t<detail::IsString<TString>::value, int> = 0>
|
detail::enable_if_t<detail::IsString<TString>::value, int> = 0>
|
||||||
ARDUINOJSON_DEPRECATED("use doc[key].is<T>() instead")
|
ARDUINOJSON_DEPRECATED("use doc[key].is<T>() instead")
|
||||||
bool containsKey(const TString& key) const {
|
bool containsKey(const TString& key) const {
|
||||||
return data_.getMember(detail::adaptString(key), &resources_) != 0;
|
return getVariantImpl().getMember(detail::adaptString(key)) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// DEPRECATED: use obj[key].is<T>() instead
|
// DEPRECATED: use obj[key].is<T>() instead
|
||||||
@ -212,7 +212,7 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
|||||||
detail::enable_if_t<detail::IsString<TString>::value, int> = 0>
|
detail::enable_if_t<detail::IsString<TString>::value, int> = 0>
|
||||||
JsonVariantConst operator[](const TString& key) const {
|
JsonVariantConst operator[](const TString& key) const {
|
||||||
return JsonVariantConst(
|
return JsonVariantConst(
|
||||||
data_.getMember(detail::adaptString(key), &resources_), &resources_);
|
getVariantImpl().getMember(detail::adaptString(key)), &resources_);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gets a root object's member.
|
// Gets a root object's member.
|
||||||
@ -223,7 +223,7 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
|||||||
int> = 0>
|
int> = 0>
|
||||||
JsonVariantConst operator[](TChar* key) const {
|
JsonVariantConst operator[](TChar* key) const {
|
||||||
return JsonVariantConst(
|
return JsonVariantConst(
|
||||||
data_.getMember(detail::adaptString(key), &resources_), &resources_);
|
getVariantImpl().getMember(detail::adaptString(key)), &resources_);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gets or sets a root array's element.
|
// Gets or sets a root array's element.
|
||||||
@ -237,7 +237,7 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
|||||||
// Gets a root array's member.
|
// Gets a root array's member.
|
||||||
// https://arduinojson.org/v7/api/jsondocument/subscript/
|
// https://arduinojson.org/v7/api/jsondocument/subscript/
|
||||||
JsonVariantConst operator[](size_t index) const {
|
JsonVariantConst operator[](size_t index) const {
|
||||||
return JsonVariantConst(data_.getElement(index, &resources_), &resources_);
|
return JsonVariantConst(getVariantImpl().getElement(index), &resources_);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gets or sets a root object's member.
|
// Gets or sets a root object's member.
|
||||||
@ -267,14 +267,14 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
|||||||
template <typename T, detail::enable_if_t<
|
template <typename T, detail::enable_if_t<
|
||||||
detail::is_same<T, JsonVariant>::value, int> = 0>
|
detail::is_same<T, JsonVariant>::value, int> = 0>
|
||||||
JsonVariant add() {
|
JsonVariant add() {
|
||||||
return JsonVariant(data_.addElement(&resources_), &resources_);
|
return JsonVariant(getVariantImpl().addElement(), &resources_);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Appends a value to the root array.
|
// Appends a value to the root array.
|
||||||
// https://arduinojson.org/v7/api/jsondocument/add/
|
// https://arduinojson.org/v7/api/jsondocument/add/
|
||||||
template <typename TValue>
|
template <typename TValue>
|
||||||
bool add(const TValue& value) {
|
bool add(const TValue& value) {
|
||||||
return data_.addValue(value, &resources_);
|
return getVariantImpl().addValue(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Appends a value to the root array.
|
// Appends a value to the root array.
|
||||||
@ -282,7 +282,7 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
|||||||
template <typename TChar,
|
template <typename TChar,
|
||||||
detail::enable_if_t<!detail::is_const<TChar>::value, int> = 0>
|
detail::enable_if_t<!detail::is_const<TChar>::value, int> = 0>
|
||||||
bool add(TChar* value) {
|
bool add(TChar* value) {
|
||||||
return data_.addValue(value, &resources_);
|
return getVariantImpl().addValue(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Removes an element of the root array.
|
// Removes an element of the root array.
|
||||||
@ -290,8 +290,7 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
|||||||
template <typename T,
|
template <typename T,
|
||||||
detail::enable_if_t<detail::is_integral<T>::value, int> = 0>
|
detail::enable_if_t<detail::is_integral<T>::value, int> = 0>
|
||||||
void remove(T index) {
|
void remove(T index) {
|
||||||
detail::VariantData::removeElement(getData(), size_t(index),
|
getVariantImpl().removeElement(size_t(index));
|
||||||
getResourceManager());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Removes a member of the root object.
|
// Removes a member of the root object.
|
||||||
@ -301,8 +300,7 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
|||||||
!detail::is_const<TChar>::value,
|
!detail::is_const<TChar>::value,
|
||||||
int> = 0>
|
int> = 0>
|
||||||
void remove(TChar* key) {
|
void remove(TChar* key) {
|
||||||
detail::VariantData::removeMember(getData(), detail::adaptString(key),
|
getVariantImpl().removeMember(detail::adaptString(key));
|
||||||
getResourceManager());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Removes a member of the root object.
|
// Removes a member of the root object.
|
||||||
@ -310,8 +308,8 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
|||||||
template <typename TString,
|
template <typename TString,
|
||||||
detail::enable_if_t<detail::IsString<TString>::value, int> = 0>
|
detail::enable_if_t<detail::IsString<TString>::value, int> = 0>
|
||||||
void remove(const TString& key) {
|
void remove(const TString& key) {
|
||||||
detail::VariantData::removeMember(getData(), detail::adaptString(key),
|
detail::VariantImpl(getData(), getResourceManager())
|
||||||
getResourceManager());
|
.removeMember(detail::adaptString(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Removes a member of the root object or an element of the root array.
|
// Removes a member of the root object or an element of the root array.
|
||||||
@ -391,6 +389,10 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
detail::VariantImpl getVariantImpl() const {
|
||||||
|
return detail::VariantImpl(&data_, &resources_);
|
||||||
|
}
|
||||||
|
|
||||||
JsonVariant getVariant() {
|
JsonVariant getVariant() {
|
||||||
return JsonVariant(&data_, &resources_);
|
return JsonVariant(&data_, &resources_);
|
||||||
}
|
}
|
||||||
@ -415,8 +417,8 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
|||||||
return &data_;
|
return &data_;
|
||||||
}
|
}
|
||||||
|
|
||||||
detail::ResourceManager resources_;
|
mutable detail::ResourceManager resources_;
|
||||||
detail::VariantData data_;
|
mutable detail::VariantData data_;
|
||||||
};
|
};
|
||||||
|
|
||||||
inline void convertToJson(const JsonDocument& src, JsonVariant dst) {
|
inline void convertToJson(const JsonDocument& src, JsonVariant dst) {
|
||||||
|
@ -28,13 +28,13 @@ class JsonDeserializer {
|
|||||||
resources_(resources) {}
|
resources_(resources) {}
|
||||||
|
|
||||||
template <typename TFilter>
|
template <typename TFilter>
|
||||||
DeserializationError parse(VariantData& variant, TFilter filter,
|
DeserializationError parse(VariantData* variant, TFilter filter,
|
||||||
DeserializationOption::NestingLimit nestingLimit) {
|
DeserializationOption::NestingLimit nestingLimit) {
|
||||||
DeserializationError::Code err;
|
DeserializationError::Code err;
|
||||||
|
|
||||||
err = parseVariant(variant, filter, nestingLimit);
|
err = parseVariant(variant, filter, nestingLimit);
|
||||||
|
|
||||||
if (!err && latch_.last() != 0 && variant.isFloat()) {
|
if (!err && latch_.last() != 0 && variant->isFloat()) {
|
||||||
// We don't detect trailing characters earlier, so we need to check now
|
// We don't detect trailing characters earlier, so we need to check now
|
||||||
return DeserializationError::InvalidInput;
|
return DeserializationError::InvalidInput;
|
||||||
}
|
}
|
||||||
@ -60,7 +60,7 @@ class JsonDeserializer {
|
|||||||
|
|
||||||
template <typename TFilter>
|
template <typename TFilter>
|
||||||
DeserializationError::Code parseVariant(
|
DeserializationError::Code parseVariant(
|
||||||
VariantData& variant, TFilter filter,
|
VariantData* variant, TFilter filter,
|
||||||
DeserializationOption::NestingLimit nestingLimit) {
|
DeserializationOption::NestingLimit nestingLimit) {
|
||||||
DeserializationError::Code err;
|
DeserializationError::Code err;
|
||||||
|
|
||||||
@ -71,13 +71,15 @@ class JsonDeserializer {
|
|||||||
switch (current()) {
|
switch (current()) {
|
||||||
case '[':
|
case '[':
|
||||||
if (filter.allowArray())
|
if (filter.allowArray())
|
||||||
return parseArray(variant.toArray(), filter, nestingLimit);
|
return parseArray(VariantImpl(variant, resources_).toArray(), filter,
|
||||||
|
nestingLimit);
|
||||||
else
|
else
|
||||||
return skipArray(nestingLimit);
|
return skipArray(nestingLimit);
|
||||||
|
|
||||||
case '{':
|
case '{':
|
||||||
if (filter.allowObject())
|
if (filter.allowObject())
|
||||||
return parseObject(variant.toObject(), filter, nestingLimit);
|
return parseObject(VariantImpl(variant, resources_).toObject(),
|
||||||
|
filter, nestingLimit);
|
||||||
else
|
else
|
||||||
return skipObject(nestingLimit);
|
return skipObject(nestingLimit);
|
||||||
|
|
||||||
@ -90,12 +92,12 @@ class JsonDeserializer {
|
|||||||
|
|
||||||
case 't':
|
case 't':
|
||||||
if (filter.allowValue())
|
if (filter.allowValue())
|
||||||
variant.setBoolean(true);
|
variant->setBoolean(true);
|
||||||
return skipKeyword("true");
|
return skipKeyword("true");
|
||||||
|
|
||||||
case 'f':
|
case 'f':
|
||||||
if (filter.allowValue())
|
if (filter.allowValue())
|
||||||
variant.setBoolean(false);
|
variant->setBoolean(false);
|
||||||
return skipKeyword("false");
|
return skipKeyword("false");
|
||||||
|
|
||||||
case 'n':
|
case 'n':
|
||||||
@ -146,7 +148,7 @@ class JsonDeserializer {
|
|||||||
|
|
||||||
template <typename TFilter>
|
template <typename TFilter>
|
||||||
DeserializationError::Code parseArray(
|
DeserializationError::Code parseArray(
|
||||||
ArrayData& array, TFilter filter,
|
ArrayImpl array, TFilter filter,
|
||||||
DeserializationOption::NestingLimit nestingLimit) {
|
DeserializationOption::NestingLimit nestingLimit) {
|
||||||
DeserializationError::Code err;
|
DeserializationError::Code err;
|
||||||
|
|
||||||
@ -172,12 +174,12 @@ class JsonDeserializer {
|
|||||||
for (;;) {
|
for (;;) {
|
||||||
if (elementFilter.allow()) {
|
if (elementFilter.allow()) {
|
||||||
// Allocate slot in array
|
// Allocate slot in array
|
||||||
VariantData* value = array.addElement(resources_);
|
VariantData* value = array.addElement();
|
||||||
if (!value)
|
if (!value)
|
||||||
return DeserializationError::NoMemory;
|
return DeserializationError::NoMemory;
|
||||||
|
|
||||||
// 1 - Parse value
|
// 1 - Parse value
|
||||||
err = parseVariant(*value, elementFilter, nestingLimit.decrement());
|
err = parseVariant(value, elementFilter, nestingLimit.decrement());
|
||||||
if (err)
|
if (err)
|
||||||
return err;
|
return err;
|
||||||
} else {
|
} else {
|
||||||
@ -232,7 +234,7 @@ class JsonDeserializer {
|
|||||||
|
|
||||||
template <typename TFilter>
|
template <typename TFilter>
|
||||||
DeserializationError::Code parseObject(
|
DeserializationError::Code parseObject(
|
||||||
ObjectData& object, TFilter filter,
|
ObjectImpl object, TFilter filter,
|
||||||
DeserializationOption::NestingLimit nestingLimit) {
|
DeserializationOption::NestingLimit nestingLimit) {
|
||||||
DeserializationError::Code err;
|
DeserializationError::Code err;
|
||||||
|
|
||||||
@ -273,19 +275,19 @@ class JsonDeserializer {
|
|||||||
TFilter memberFilter = filter[key];
|
TFilter memberFilter = filter[key];
|
||||||
|
|
||||||
if (memberFilter.allow()) {
|
if (memberFilter.allow()) {
|
||||||
auto member = object.getMember(adaptString(key), resources_);
|
auto member = object.getMember(adaptString(key));
|
||||||
if (!member) {
|
if (!member) {
|
||||||
auto keyVariant = object.addPair(&member, resources_);
|
auto keyVariant = object.addPair(&member);
|
||||||
if (!keyVariant)
|
if (!keyVariant)
|
||||||
return DeserializationError::NoMemory;
|
return DeserializationError::NoMemory;
|
||||||
|
|
||||||
stringBuilder_.save(keyVariant);
|
stringBuilder_.save(keyVariant);
|
||||||
} else {
|
} else {
|
||||||
member->clear(resources_);
|
VariantImpl(member, resources_).clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse value
|
// Parse value
|
||||||
err = parseVariant(*member, memberFilter, nestingLimit.decrement());
|
err = parseVariant(member, memberFilter, nestingLimit.decrement());
|
||||||
if (err)
|
if (err)
|
||||||
return err;
|
return err;
|
||||||
} else {
|
} else {
|
||||||
@ -379,7 +381,7 @@ class JsonDeserializer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
DeserializationError::Code parseStringValue(VariantData& variant) {
|
DeserializationError::Code parseStringValue(VariantData* variant) {
|
||||||
DeserializationError::Code err;
|
DeserializationError::Code err;
|
||||||
|
|
||||||
stringBuilder_.startString();
|
stringBuilder_.startString();
|
||||||
@ -388,7 +390,7 @@ class JsonDeserializer {
|
|||||||
if (err)
|
if (err)
|
||||||
return err;
|
return err;
|
||||||
|
|
||||||
stringBuilder_.save(&variant);
|
stringBuilder_.save(variant);
|
||||||
|
|
||||||
return DeserializationError::Ok;
|
return DeserializationError::Ok;
|
||||||
}
|
}
|
||||||
@ -504,7 +506,7 @@ class JsonDeserializer {
|
|||||||
return DeserializationError::Ok;
|
return DeserializationError::Ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
DeserializationError::Code parseNumericValue(VariantData& result) {
|
DeserializationError::Code parseNumericValue(VariantData* result) {
|
||||||
uint8_t n = 0;
|
uint8_t n = 0;
|
||||||
|
|
||||||
char c = current();
|
char c = current();
|
||||||
@ -518,26 +520,28 @@ class JsonDeserializer {
|
|||||||
auto number = parseNumber(buffer_);
|
auto number = parseNumber(buffer_);
|
||||||
switch (number.type()) {
|
switch (number.type()) {
|
||||||
case NumberType::UnsignedInteger:
|
case NumberType::UnsignedInteger:
|
||||||
if (result.setInteger(number.asUnsignedInteger(), resources_))
|
if (VariantImpl(result, resources_)
|
||||||
|
.setInteger(number.asUnsignedInteger()))
|
||||||
return DeserializationError::Ok;
|
return DeserializationError::Ok;
|
||||||
else
|
else
|
||||||
return DeserializationError::NoMemory;
|
return DeserializationError::NoMemory;
|
||||||
|
|
||||||
case NumberType::SignedInteger:
|
case NumberType::SignedInteger:
|
||||||
if (result.setInteger(number.asSignedInteger(), resources_))
|
if (VariantImpl(result, resources_)
|
||||||
|
.setInteger(number.asSignedInteger()))
|
||||||
return DeserializationError::Ok;
|
return DeserializationError::Ok;
|
||||||
else
|
else
|
||||||
return DeserializationError::NoMemory;
|
return DeserializationError::NoMemory;
|
||||||
|
|
||||||
case NumberType::Float:
|
case NumberType::Float:
|
||||||
if (result.setFloat(number.asFloat(), resources_))
|
if (VariantImpl(result, resources_).setFloat(number.asFloat()))
|
||||||
return DeserializationError::Ok;
|
return DeserializationError::Ok;
|
||||||
else
|
else
|
||||||
return DeserializationError::NoMemory;
|
return DeserializationError::NoMemory;
|
||||||
|
|
||||||
#if ARDUINOJSON_USE_DOUBLE
|
#if ARDUINOJSON_USE_DOUBLE
|
||||||
case NumberType::Double:
|
case NumberType::Double:
|
||||||
if (result.setFloat(number.asDouble(), resources_))
|
if (VariantImpl(result, resources_).setFloat(number.asDouble()))
|
||||||
return DeserializationError::Ok;
|
return DeserializationError::Ok;
|
||||||
else
|
else
|
||||||
return DeserializationError::NoMemory;
|
return DeserializationError::NoMemory;
|
||||||
|
@ -16,10 +16,10 @@ class JsonSerializer : public VariantDataVisitor<size_t> {
|
|||||||
public:
|
public:
|
||||||
static const bool producesText = true;
|
static const bool producesText = true;
|
||||||
|
|
||||||
JsonSerializer(TWriter writer, const ResourceManager* resources)
|
JsonSerializer(TWriter writer, ResourceManager* resources)
|
||||||
: formatter_(writer), resources_(resources) {}
|
: formatter_(writer), resources_(resources) {}
|
||||||
|
|
||||||
size_t visit(const ArrayData& array) {
|
size_t visit(const ArrayImpl& array) {
|
||||||
write('[');
|
write('[');
|
||||||
|
|
||||||
auto slotId = array.head();
|
auto slotId = array.head();
|
||||||
@ -27,9 +27,9 @@ class JsonSerializer : public VariantDataVisitor<size_t> {
|
|||||||
while (slotId != NULL_SLOT) {
|
while (slotId != NULL_SLOT) {
|
||||||
auto slot = resources_->getVariant(slotId);
|
auto slot = resources_->getVariant(slotId);
|
||||||
|
|
||||||
slot->accept(*this, resources_);
|
VariantImpl(slot, resources_).accept(*this);
|
||||||
|
|
||||||
slotId = slot->next();
|
slotId = slot->next;
|
||||||
|
|
||||||
if (slotId != NULL_SLOT)
|
if (slotId != NULL_SLOT)
|
||||||
write(',');
|
write(',');
|
||||||
@ -39,7 +39,7 @@ class JsonSerializer : public VariantDataVisitor<size_t> {
|
|||||||
return bytesWritten();
|
return bytesWritten();
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t visit(const ObjectData& object) {
|
size_t visit(const ObjectImpl& object) {
|
||||||
write('{');
|
write('{');
|
||||||
|
|
||||||
auto slotId = object.head();
|
auto slotId = object.head();
|
||||||
@ -48,9 +48,9 @@ class JsonSerializer : public VariantDataVisitor<size_t> {
|
|||||||
|
|
||||||
while (slotId != NULL_SLOT) {
|
while (slotId != NULL_SLOT) {
|
||||||
auto slot = resources_->getVariant(slotId);
|
auto slot = resources_->getVariant(slotId);
|
||||||
slot->accept(*this, resources_);
|
VariantImpl(slot, resources_).accept(*this);
|
||||||
|
|
||||||
slotId = slot->next();
|
slotId = slot->next;
|
||||||
|
|
||||||
if (slotId != NULL_SLOT)
|
if (slotId != NULL_SLOT)
|
||||||
write(isKey ? ':' : ',');
|
write(isKey ? ':' : ',');
|
||||||
@ -120,7 +120,7 @@ class JsonSerializer : public VariantDataVisitor<size_t> {
|
|||||||
TextFormatter<TWriter> formatter_;
|
TextFormatter<TWriter> formatter_;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
const ResourceManager* resources_;
|
ResourceManager* resources_;
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -16,17 +16,17 @@ class PrettyJsonSerializer : public JsonSerializer<TWriter> {
|
|||||||
using base = JsonSerializer<TWriter>;
|
using base = JsonSerializer<TWriter>;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
PrettyJsonSerializer(TWriter writer, const ResourceManager* resources)
|
PrettyJsonSerializer(TWriter writer, ResourceManager* resources)
|
||||||
: base(writer, resources), nesting_(0) {}
|
: base(writer, resources), nesting_(0) {}
|
||||||
|
|
||||||
size_t visit(const ArrayData& array) {
|
size_t visit(const ArrayImpl& array) {
|
||||||
auto it = array.createIterator(base::resources_);
|
auto it = array.createIterator();
|
||||||
if (!it.done()) {
|
if (!it.done()) {
|
||||||
base::write("[\r\n");
|
base::write("[\r\n");
|
||||||
nesting_++;
|
nesting_++;
|
||||||
while (!it.done()) {
|
while (!it.done()) {
|
||||||
indent();
|
indent();
|
||||||
it->accept(*this, base::resources_);
|
VariantImpl(it.data(), base::resources_).accept(*this);
|
||||||
|
|
||||||
it.next(base::resources_);
|
it.next(base::resources_);
|
||||||
base::write(it.done() ? "\r\n" : ",\r\n");
|
base::write(it.done() ? "\r\n" : ",\r\n");
|
||||||
@ -40,8 +40,8 @@ class PrettyJsonSerializer : public JsonSerializer<TWriter> {
|
|||||||
return this->bytesWritten();
|
return this->bytesWritten();
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t visit(const ObjectData& object) {
|
size_t visit(const ObjectImpl& object) {
|
||||||
auto it = object.createIterator(base::resources_);
|
auto it = object.createIterator();
|
||||||
if (!it.done()) {
|
if (!it.done()) {
|
||||||
base::write("{\r\n");
|
base::write("{\r\n");
|
||||||
nesting_++;
|
nesting_++;
|
||||||
@ -49,7 +49,7 @@ class PrettyJsonSerializer : public JsonSerializer<TWriter> {
|
|||||||
while (!it.done()) {
|
while (!it.done()) {
|
||||||
if (isKey)
|
if (isKey)
|
||||||
indent();
|
indent();
|
||||||
it->accept(*this, base::resources_);
|
VariantImpl(it.data(), base::resources_).accept(*this);
|
||||||
it.next(base::resources_);
|
it.next(base::resources_);
|
||||||
if (isKey)
|
if (isKey)
|
||||||
base::write(": ");
|
base::write(": ");
|
||||||
|
@ -14,20 +14,11 @@
|
|||||||
|
|
||||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||||
|
|
||||||
class VariantData;
|
struct VariantData;
|
||||||
class VariantWithId;
|
class VariantWithId;
|
||||||
|
|
||||||
class ResourceManager {
|
class ResourceManager {
|
||||||
union SlotData {
|
|
||||||
VariantData variant;
|
|
||||||
#if ARDUINOJSON_USE_EXTENSIONS
|
|
||||||
VariantExtension extension;
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
constexpr static size_t slotSize = sizeof(SlotData);
|
|
||||||
|
|
||||||
ResourceManager(Allocator* allocator = DefaultAllocator::instance())
|
ResourceManager(Allocator* allocator = DefaultAllocator::instance())
|
||||||
: allocator_(allocator), overflowed_(false) {}
|
: allocator_(allocator), overflowed_(false) {}
|
||||||
|
|
||||||
@ -35,6 +26,9 @@ class ResourceManager {
|
|||||||
stringPool_.clear(allocator_);
|
stringPool_.clear(allocator_);
|
||||||
variantPools_.clear(allocator_);
|
variantPools_.clear(allocator_);
|
||||||
staticStringsPools_.clear(allocator_);
|
staticStringsPools_.clear(allocator_);
|
||||||
|
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||||
|
eightBytePools_.clear(allocator_);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
ResourceManager(const ResourceManager&) = delete;
|
ResourceManager(const ResourceManager&) = delete;
|
||||||
@ -44,6 +38,9 @@ class ResourceManager {
|
|||||||
swap(a.stringPool_, b.stringPool_);
|
swap(a.stringPool_, b.stringPool_);
|
||||||
swap(a.variantPools_, b.variantPools_);
|
swap(a.variantPools_, b.variantPools_);
|
||||||
swap(a.staticStringsPools_, b.staticStringsPools_);
|
swap(a.staticStringsPools_, b.staticStringsPools_);
|
||||||
|
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||||
|
swap(a.eightBytePools_, b.eightBytePools_);
|
||||||
|
#endif
|
||||||
swap_(a.allocator_, b.allocator_);
|
swap_(a.allocator_, b.allocator_);
|
||||||
swap_(a.overflowed_, b.overflowed_);
|
swap_(a.overflowed_, b.overflowed_);
|
||||||
}
|
}
|
||||||
@ -64,10 +61,10 @@ class ResourceManager {
|
|||||||
void freeVariant(Slot<VariantData> slot);
|
void freeVariant(Slot<VariantData> slot);
|
||||||
VariantData* getVariant(SlotId id) const;
|
VariantData* getVariant(SlotId id) const;
|
||||||
|
|
||||||
#if ARDUINOJSON_USE_EXTENSIONS
|
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||||
Slot<VariantExtension> allocExtension();
|
Slot<EightByteValue> allocEightByte();
|
||||||
void freeExtension(SlotId slot);
|
void freeEightByte(SlotId slot);
|
||||||
VariantExtension* getExtension(SlotId id) const;
|
EightByteValue* getEightByte(SlotId id) const;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
template <typename TAdaptedString>
|
template <typename TAdaptedString>
|
||||||
@ -136,19 +133,28 @@ class ResourceManager {
|
|||||||
variantPools_.clear(allocator_);
|
variantPools_.clear(allocator_);
|
||||||
stringPool_.clear(allocator_);
|
stringPool_.clear(allocator_);
|
||||||
staticStringsPools_.clear(allocator_);
|
staticStringsPools_.clear(allocator_);
|
||||||
|
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||||
|
eightBytePools_.clear(allocator_);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void shrinkToFit() {
|
void shrinkToFit() {
|
||||||
variantPools_.shrinkToFit(allocator_);
|
variantPools_.shrinkToFit(allocator_);
|
||||||
staticStringsPools_.shrinkToFit(allocator_);
|
staticStringsPools_.shrinkToFit(allocator_);
|
||||||
|
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||||
|
eightBytePools_.shrinkToFit(allocator_);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Allocator* allocator_;
|
Allocator* allocator_;
|
||||||
bool overflowed_;
|
bool overflowed_;
|
||||||
StringPool stringPool_;
|
StringPool stringPool_;
|
||||||
MemoryPoolList<SlotData> variantPools_;
|
MemoryPoolList<VariantData> variantPools_;
|
||||||
MemoryPoolList<const char*> staticStringsPools_;
|
MemoryPoolList<const char*> staticStringsPools_;
|
||||||
|
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||||
|
MemoryPoolList<EightByteValue> eightBytePools_;
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -12,40 +12,41 @@
|
|||||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||||
|
|
||||||
inline Slot<VariantData> ResourceManager::allocVariant() {
|
inline Slot<VariantData> ResourceManager::allocVariant() {
|
||||||
auto p = variantPools_.allocSlot(allocator_);
|
auto slot = variantPools_.allocSlot(allocator_);
|
||||||
if (!p) {
|
if (!slot) {
|
||||||
overflowed_ = true;
|
overflowed_ = true;
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
return {new (&p->variant) VariantData, p.id()};
|
new (slot.ptr()) VariantData();
|
||||||
|
return slot;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void ResourceManager::freeVariant(Slot<VariantData> variant) {
|
inline void ResourceManager::freeVariant(Slot<VariantData> slot) {
|
||||||
variant->clear(this);
|
VariantImpl(slot.ptr(), this).clear();
|
||||||
variantPools_.freeSlot({alias_cast<SlotData*>(variant.ptr()), variant.id()});
|
variantPools_.freeSlot(slot);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline VariantData* ResourceManager::getVariant(SlotId id) const {
|
inline VariantData* ResourceManager::getVariant(SlotId id) const {
|
||||||
return reinterpret_cast<VariantData*>(variantPools_.getSlot(id));
|
return reinterpret_cast<VariantData*>(variantPools_.getSlot(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
#if ARDUINOJSON_USE_EXTENSIONS
|
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||||
inline Slot<VariantExtension> ResourceManager::allocExtension() {
|
inline Slot<EightByteValue> ResourceManager::allocEightByte() {
|
||||||
auto p = variantPools_.allocSlot(allocator_);
|
auto slot = eightBytePools_.allocSlot(allocator_);
|
||||||
if (!p) {
|
if (!slot) {
|
||||||
overflowed_ = true;
|
overflowed_ = true;
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
return {&p->extension, p.id()};
|
return slot;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void ResourceManager::freeExtension(SlotId id) {
|
inline void ResourceManager::freeEightByte(SlotId id) {
|
||||||
auto p = getExtension(id);
|
auto p = getEightByte(id);
|
||||||
variantPools_.freeSlot({reinterpret_cast<SlotData*>(p), id});
|
eightBytePools_.freeSlot({p, id});
|
||||||
}
|
}
|
||||||
|
|
||||||
inline VariantExtension* ResourceManager::getExtension(SlotId id) const {
|
inline EightByteValue* ResourceManager::getEightByte(SlotId id) const {
|
||||||
return &variantPools_.getSlot(id)->extension;
|
return eightBytePools_.getSlot(id);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ struct Converter<MsgPackBinary> : private detail::VariantAttorney {
|
|||||||
if (!data)
|
if (!data)
|
||||||
return;
|
return;
|
||||||
auto resources = getResourceManager(dst);
|
auto resources = getResourceManager(dst);
|
||||||
data->clear(resources);
|
detail::VariantImpl(data, resources).clear();
|
||||||
if (src.data()) {
|
if (src.data()) {
|
||||||
size_t headerSize = src.size() >= 0x10000 ? 5
|
size_t headerSize = src.size() >= 0x10000 ? 5
|
||||||
: src.size() >= 0x100 ? 3
|
: src.size() >= 0x100 ? 3
|
||||||
@ -66,10 +66,8 @@ struct Converter<MsgPackBinary> : private detail::VariantAttorney {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static MsgPackBinary fromJson(JsonVariantConst src) {
|
static MsgPackBinary fromJson(JsonVariantConst src) {
|
||||||
auto data = getData(src);
|
auto variant = VariantAttorney::getVariantImpl(src);
|
||||||
if (!data)
|
auto rawstr = variant.asRawString();
|
||||||
return {};
|
|
||||||
auto rawstr = data->asRawString();
|
|
||||||
auto p = reinterpret_cast<const uint8_t*>(rawstr.c_str());
|
auto p = reinterpret_cast<const uint8_t*>(rawstr.c_str());
|
||||||
auto n = rawstr.size();
|
auto n = rawstr.size();
|
||||||
if (n >= 2 && p[0] == 0xc4) { // bin 8
|
if (n >= 2 && p[0] == 0xc4) { // bin 8
|
||||||
|
@ -24,10 +24,10 @@ class MsgPackDeserializer {
|
|||||||
foundSomething_(false) {}
|
foundSomething_(false) {}
|
||||||
|
|
||||||
template <typename TFilter>
|
template <typename TFilter>
|
||||||
DeserializationError parse(VariantData& variant, TFilter filter,
|
DeserializationError parse(VariantData* variant, TFilter filter,
|
||||||
DeserializationOption::NestingLimit nestingLimit) {
|
DeserializationOption::NestingLimit nestingLimit) {
|
||||||
DeserializationError::Code err;
|
DeserializationError::Code err;
|
||||||
err = parseVariant(&variant, filter, nestingLimit);
|
err = parseVariant(variant, filter, nestingLimit);
|
||||||
return foundSomething_ ? err : DeserializationError::EmptyInput;
|
return foundSomething_ ? err : DeserializationError::EmptyInput;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,7 +91,7 @@ class MsgPackDeserializer {
|
|||||||
|
|
||||||
if (code <= 0x7f || code >= 0xe0) { // fixint
|
if (code <= 0x7f || code >= 0xe0) { // fixint
|
||||||
if (allowValue)
|
if (allowValue)
|
||||||
variant->setInteger(static_cast<int8_t>(code), resources_);
|
VariantImpl(variant, resources_).setInteger(static_cast<int8_t>(code));
|
||||||
return DeserializationError::Ok;
|
return DeserializationError::Ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -231,14 +231,14 @@ class MsgPackDeserializer {
|
|||||||
if (isSigned) {
|
if (isSigned) {
|
||||||
auto truncatedValue = static_cast<JsonInteger>(signedValue);
|
auto truncatedValue = static_cast<JsonInteger>(signedValue);
|
||||||
if (truncatedValue == signedValue) {
|
if (truncatedValue == signedValue) {
|
||||||
if (!variant->setInteger(truncatedValue, resources_))
|
if (!VariantImpl(variant, resources_).setInteger(truncatedValue))
|
||||||
return DeserializationError::NoMemory;
|
return DeserializationError::NoMemory;
|
||||||
}
|
}
|
||||||
// else set null on overflow
|
// else set null on overflow
|
||||||
} else {
|
} else {
|
||||||
auto truncatedValue = static_cast<JsonUInt>(unsignedValue);
|
auto truncatedValue = static_cast<JsonUInt>(unsignedValue);
|
||||||
if (truncatedValue == unsignedValue)
|
if (truncatedValue == unsignedValue)
|
||||||
if (!variant->setInteger(truncatedValue, resources_))
|
if (!VariantImpl(variant, resources_).setInteger(truncatedValue))
|
||||||
return DeserializationError::NoMemory;
|
return DeserializationError::NoMemory;
|
||||||
// else set null on overflow
|
// else set null on overflow
|
||||||
}
|
}
|
||||||
@ -257,7 +257,7 @@ class MsgPackDeserializer {
|
|||||||
return err;
|
return err;
|
||||||
|
|
||||||
fixEndianness(value);
|
fixEndianness(value);
|
||||||
variant->setFloat(value, resources_);
|
VariantImpl(variant, resources_).setFloat(value);
|
||||||
|
|
||||||
return DeserializationError::Ok;
|
return DeserializationError::Ok;
|
||||||
}
|
}
|
||||||
@ -273,7 +273,7 @@ class MsgPackDeserializer {
|
|||||||
return err;
|
return err;
|
||||||
|
|
||||||
fixEndianness(value);
|
fixEndianness(value);
|
||||||
if (variant->setFloat(value, resources_))
|
if (VariantImpl(variant, resources_).setFloat(value))
|
||||||
return DeserializationError::Ok;
|
return DeserializationError::Ok;
|
||||||
else
|
else
|
||||||
return DeserializationError::NoMemory;
|
return DeserializationError::NoMemory;
|
||||||
@ -293,7 +293,7 @@ class MsgPackDeserializer {
|
|||||||
|
|
||||||
doubleToFloat(i, o);
|
doubleToFloat(i, o);
|
||||||
fixEndianness(value);
|
fixEndianness(value);
|
||||||
variant->setFloat(value, resources_);
|
VariantImpl(variant, resources_).setFloat(value);
|
||||||
|
|
||||||
return DeserializationError::Ok;
|
return DeserializationError::Ok;
|
||||||
}
|
}
|
||||||
@ -349,12 +349,10 @@ class MsgPackDeserializer {
|
|||||||
|
|
||||||
bool allowArray = filter.allowArray();
|
bool allowArray = filter.allowArray();
|
||||||
|
|
||||||
ArrayData* array;
|
ArrayImpl array;
|
||||||
if (allowArray) {
|
if (allowArray) {
|
||||||
ARDUINOJSON_ASSERT(variant != 0);
|
ARDUINOJSON_ASSERT(variant != 0);
|
||||||
array = &variant->toArray();
|
array = VariantImpl(variant, resources_).toArray();
|
||||||
} else {
|
|
||||||
array = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TFilter elementFilter = filter[0U];
|
TFilter elementFilter = filter[0U];
|
||||||
@ -363,8 +361,7 @@ class MsgPackDeserializer {
|
|||||||
VariantData* value;
|
VariantData* value;
|
||||||
|
|
||||||
if (elementFilter.allow()) {
|
if (elementFilter.allow()) {
|
||||||
ARDUINOJSON_ASSERT(array != 0);
|
value = array.addElement();
|
||||||
value = array->addElement(resources_);
|
|
||||||
if (!value)
|
if (!value)
|
||||||
return DeserializationError::NoMemory;
|
return DeserializationError::NoMemory;
|
||||||
} else {
|
} else {
|
||||||
@ -388,12 +385,10 @@ class MsgPackDeserializer {
|
|||||||
if (nestingLimit.reached())
|
if (nestingLimit.reached())
|
||||||
return DeserializationError::TooDeep;
|
return DeserializationError::TooDeep;
|
||||||
|
|
||||||
ObjectData* object;
|
ObjectImpl object;
|
||||||
if (filter.allowObject()) {
|
if (filter.allowObject()) {
|
||||||
ARDUINOJSON_ASSERT(variant != 0);
|
ARDUINOJSON_ASSERT(variant != 0);
|
||||||
object = &variant->toObject();
|
object = VariantImpl(variant, resources_).toObject();
|
||||||
} else {
|
|
||||||
object = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (; n; --n) {
|
for (; n; --n) {
|
||||||
@ -406,9 +401,7 @@ class MsgPackDeserializer {
|
|||||||
VariantData* member = 0;
|
VariantData* member = 0;
|
||||||
|
|
||||||
if (memberFilter.allow()) {
|
if (memberFilter.allow()) {
|
||||||
ARDUINOJSON_ASSERT(object != 0);
|
auto keyVariant = object.addPair(&member);
|
||||||
|
|
||||||
auto keyVariant = object->addPair(&member, resources_);
|
|
||||||
if (!keyVariant)
|
if (!keyVariant)
|
||||||
return DeserializationError::NoMemory;
|
return DeserializationError::NoMemory;
|
||||||
|
|
||||||
|
@ -31,11 +31,11 @@ class MsgPackExtension {
|
|||||||
template <>
|
template <>
|
||||||
struct Converter<MsgPackExtension> : private detail::VariantAttorney {
|
struct Converter<MsgPackExtension> : private detail::VariantAttorney {
|
||||||
static void toJson(MsgPackExtension src, JsonVariant dst) {
|
static void toJson(MsgPackExtension src, JsonVariant dst) {
|
||||||
auto data = VariantAttorney::getData(dst);
|
auto data = getData(dst);
|
||||||
if (!data)
|
if (!data)
|
||||||
return;
|
return;
|
||||||
auto resources = getResourceManager(dst);
|
auto resources = getResourceManager(dst);
|
||||||
data->clear(resources);
|
detail::VariantImpl(data, resources).clear();
|
||||||
if (src.data()) {
|
if (src.data()) {
|
||||||
uint8_t format, sizeBytes;
|
uint8_t format, sizeBytes;
|
||||||
if (src.size() >= 0x10000) {
|
if (src.size() >= 0x10000) {
|
||||||
@ -80,10 +80,8 @@ struct Converter<MsgPackExtension> : private detail::VariantAttorney {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static MsgPackExtension fromJson(JsonVariantConst src) {
|
static MsgPackExtension fromJson(JsonVariantConst src) {
|
||||||
auto data = getData(src);
|
auto variant = VariantAttorney::getVariantImpl(src);
|
||||||
if (!data)
|
auto rawstr = variant.asRawString();
|
||||||
return {};
|
|
||||||
auto rawstr = data->asRawString();
|
|
||||||
if (rawstr.size() == 0)
|
if (rawstr.size() == 0)
|
||||||
return {};
|
return {};
|
||||||
auto p = reinterpret_cast<const uint8_t*>(rawstr.c_str());
|
auto p = reinterpret_cast<const uint8_t*>(rawstr.c_str());
|
||||||
|
@ -19,7 +19,7 @@ class MsgPackSerializer : public VariantDataVisitor<size_t> {
|
|||||||
public:
|
public:
|
||||||
static const bool producesText = false;
|
static const bool producesText = false;
|
||||||
|
|
||||||
MsgPackSerializer(TWriter writer, const ResourceManager* resources)
|
MsgPackSerializer(TWriter writer, ResourceManager* resources)
|
||||||
: writer_(writer), resources_(resources) {}
|
: writer_(writer), resources_(resources) {}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
@ -47,8 +47,8 @@ class MsgPackSerializer : public VariantDataVisitor<size_t> {
|
|||||||
return bytesWritten();
|
return bytesWritten();
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t visit(const ArrayData& array) {
|
size_t visit(const ArrayImpl& array) {
|
||||||
size_t n = array.size(resources_);
|
size_t n = array.size();
|
||||||
if (n < 0x10) {
|
if (n < 0x10) {
|
||||||
writeByte(uint8_t(0x90 + n));
|
writeByte(uint8_t(0x90 + n));
|
||||||
} else if (n < 0x10000) {
|
} else if (n < 0x10000) {
|
||||||
@ -62,15 +62,15 @@ class MsgPackSerializer : public VariantDataVisitor<size_t> {
|
|||||||
auto slotId = array.head();
|
auto slotId = array.head();
|
||||||
while (slotId != NULL_SLOT) {
|
while (slotId != NULL_SLOT) {
|
||||||
auto slot = resources_->getVariant(slotId);
|
auto slot = resources_->getVariant(slotId);
|
||||||
slot->accept(*this, resources_);
|
VariantImpl(slot, resources_).accept(*this);
|
||||||
slotId = slot->next();
|
slotId = slot->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
return bytesWritten();
|
return bytesWritten();
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t visit(const ObjectData& object) {
|
size_t visit(const ObjectImpl& object) {
|
||||||
size_t n = object.size(resources_);
|
size_t n = object.size();
|
||||||
if (n < 0x10) {
|
if (n < 0x10) {
|
||||||
writeByte(uint8_t(0x80 + n));
|
writeByte(uint8_t(0x80 + n));
|
||||||
} else if (n < 0x10000) {
|
} else if (n < 0x10000) {
|
||||||
@ -84,8 +84,8 @@ class MsgPackSerializer : public VariantDataVisitor<size_t> {
|
|||||||
auto slotId = object.head();
|
auto slotId = object.head();
|
||||||
while (slotId != NULL_SLOT) {
|
while (slotId != NULL_SLOT) {
|
||||||
auto slot = resources_->getVariant(slotId);
|
auto slot = resources_->getVariant(slotId);
|
||||||
slot->accept(*this, resources_);
|
VariantImpl(slot, resources_).accept(*this);
|
||||||
slotId = slot->next();
|
slotId = slot->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
return bytesWritten();
|
return bytesWritten();
|
||||||
@ -209,7 +209,7 @@ class MsgPackSerializer : public VariantDataVisitor<size_t> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
CountingDecorator<TWriter> writer_;
|
CountingDecorator<TWriter> writer_;
|
||||||
const ResourceManager* resources_;
|
ResourceManager* resources_;
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -20,56 +20,55 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
|
|||||||
using iterator = JsonObjectIterator;
|
using iterator = JsonObjectIterator;
|
||||||
|
|
||||||
// Creates an unbound reference.
|
// Creates an unbound reference.
|
||||||
JsonObject() : data_(0), resources_(0) {}
|
JsonObject() {}
|
||||||
|
|
||||||
// INTERNAL USE ONLY
|
// INTERNAL USE ONLY
|
||||||
JsonObject(detail::ObjectData* data, detail::ResourceManager* resource)
|
JsonObject(const detail::ObjectImpl& impl) : impl_(impl) {}
|
||||||
: data_(data), resources_(resource) {}
|
|
||||||
|
// INTERNAL USE ONLY
|
||||||
|
JsonObject(detail::VariantData* data, detail::ResourceManager* resource)
|
||||||
|
: impl_(detail::VariantImpl(data, resource).asObject()) {}
|
||||||
|
|
||||||
operator JsonVariant() const {
|
operator JsonVariant() const {
|
||||||
void* data = data_; // prevent warning cast-align
|
return JsonVariant(getData(), getResourceManager());
|
||||||
return JsonVariant(reinterpret_cast<detail::VariantData*>(data),
|
|
||||||
resources_);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
operator JsonObjectConst() const {
|
operator JsonObjectConst() const {
|
||||||
return JsonObjectConst(data_, resources_);
|
return JsonObjectConst(getData(), getResourceManager());
|
||||||
}
|
}
|
||||||
|
|
||||||
operator JsonVariantConst() const {
|
operator JsonVariantConst() const {
|
||||||
return JsonVariantConst(collectionToVariant(data_), resources_);
|
return JsonVariantConst(getData(), getResourceManager());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if the reference is unbound.
|
// Returns true if the reference is unbound.
|
||||||
// https://arduinojson.org/v7/api/jsonobject/isnull/
|
// https://arduinojson.org/v7/api/jsonobject/isnull/
|
||||||
bool isNull() const {
|
bool isNull() const {
|
||||||
return data_ == 0;
|
return impl_.isNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if the reference is bound.
|
// Returns true if the reference is bound.
|
||||||
// https://arduinojson.org/v7/api/jsonobject/isnull/
|
// https://arduinojson.org/v7/api/jsonobject/isnull/
|
||||||
operator bool() const {
|
operator bool() const {
|
||||||
return data_ != 0;
|
return !isNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the depth (nesting level) of the object.
|
// Returns the depth (nesting level) of the object.
|
||||||
// https://arduinojson.org/v7/api/jsonobject/nesting/
|
// https://arduinojson.org/v7/api/jsonobject/nesting/
|
||||||
size_t nesting() const {
|
size_t nesting() const {
|
||||||
return detail::VariantData::nesting(collectionToVariant(data_), resources_);
|
return impl_.nesting();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the number of members in the object.
|
// Returns the number of members in the object.
|
||||||
// https://arduinojson.org/v7/api/jsonobject/size/
|
// https://arduinojson.org/v7/api/jsonobject/size/
|
||||||
size_t size() const {
|
size_t size() const {
|
||||||
return data_ ? data_->size(resources_) : 0;
|
return impl_.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns an iterator to the first key-value pair of the object.
|
// Returns an iterator to the first key-value pair of the object.
|
||||||
// https://arduinojson.org/v7/api/jsonobject/begin/
|
// https://arduinojson.org/v7/api/jsonobject/begin/
|
||||||
iterator begin() const {
|
iterator begin() const {
|
||||||
if (!data_)
|
return iterator(impl_.createIterator(), impl_.getResourceManager());
|
||||||
return iterator();
|
|
||||||
return iterator(data_->createIterator(resources_), resources_);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns an iterator following the last key-value pair of the object.
|
// Returns an iterator following the last key-value pair of the object.
|
||||||
@ -81,13 +80,13 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
|
|||||||
// Removes all the members of the object.
|
// Removes all the members of the object.
|
||||||
// https://arduinojson.org/v7/api/jsonobject/clear/
|
// https://arduinojson.org/v7/api/jsonobject/clear/
|
||||||
void clear() const {
|
void clear() const {
|
||||||
detail::ObjectData::clear(data_, resources_);
|
impl_.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copies an object.
|
// Copies an object.
|
||||||
// https://arduinojson.org/v7/api/jsonobject/set/
|
// https://arduinojson.org/v7/api/jsonobject/set/
|
||||||
bool set(JsonObjectConst src) {
|
bool set(JsonObjectConst src) {
|
||||||
if (!data_ || !src.data_)
|
if (isNull() || src.isNull())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
clear();
|
clear();
|
||||||
@ -131,7 +130,7 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
|
|||||||
// Removes the member at the specified iterator.
|
// Removes the member at the specified iterator.
|
||||||
// https://arduinojson.org/v7/api/jsonobject/remove/
|
// https://arduinojson.org/v7/api/jsonobject/remove/
|
||||||
FORCE_INLINE void remove(iterator it) const {
|
FORCE_INLINE void remove(iterator it) const {
|
||||||
detail::ObjectData::remove(data_, it.iterator_, resources_);
|
impl_.remove(it.iterator_);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Removes the member with the specified key.
|
// Removes the member with the specified key.
|
||||||
@ -139,8 +138,7 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
|
|||||||
template <typename TString,
|
template <typename TString,
|
||||||
detail::enable_if_t<detail::IsString<TString>::value, int> = 0>
|
detail::enable_if_t<detail::IsString<TString>::value, int> = 0>
|
||||||
void remove(const TString& key) const {
|
void remove(const TString& key) const {
|
||||||
detail::ObjectData::removeMember(data_, detail::adaptString(key),
|
impl_.removeMember(detail::adaptString(key));
|
||||||
resources_);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Removes the member with the specified key.
|
// Removes the member with the specified key.
|
||||||
@ -156,8 +154,7 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
|
|||||||
// https://arduinojson.org/v7/api/jsonobject/remove/
|
// https://arduinojson.org/v7/api/jsonobject/remove/
|
||||||
template <typename TChar>
|
template <typename TChar>
|
||||||
FORCE_INLINE void remove(TChar* key) const {
|
FORCE_INLINE void remove(TChar* key) const {
|
||||||
detail::ObjectData::removeMember(data_, detail::adaptString(key),
|
impl_.removeMember(detail::adaptString(key));
|
||||||
resources_);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DEPRECATED: use obj[key].is<T>() instead
|
// DEPRECATED: use obj[key].is<T>() instead
|
||||||
@ -166,8 +163,7 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
|
|||||||
detail::enable_if_t<detail::IsString<TString>::value, int> = 0>
|
detail::enable_if_t<detail::IsString<TString>::value, int> = 0>
|
||||||
ARDUINOJSON_DEPRECATED("use obj[key].is<T>() instead")
|
ARDUINOJSON_DEPRECATED("use obj[key].is<T>() instead")
|
||||||
bool containsKey(const TString& key) const {
|
bool containsKey(const TString& key) const {
|
||||||
return detail::ObjectData::getMember(data_, detail::adaptString(key),
|
return impl_.getMember(detail::adaptString(key)) != 0;
|
||||||
resources_) != 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DEPRECATED: use obj["key"].is<T>() instead
|
// DEPRECATED: use obj["key"].is<T>() instead
|
||||||
@ -178,8 +174,7 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
|
|||||||
int> = 0>
|
int> = 0>
|
||||||
ARDUINOJSON_DEPRECATED("use obj[\"key\"].is<T>() instead")
|
ARDUINOJSON_DEPRECATED("use obj[\"key\"].is<T>() instead")
|
||||||
bool containsKey(TChar* key) const {
|
bool containsKey(TChar* key) const {
|
||||||
return detail::ObjectData::getMember(data_, detail::adaptString(key),
|
return impl_.getMember(detail::adaptString(key)) != 0;
|
||||||
resources_) != 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DEPRECATED: use obj[key].is<T>() instead
|
// DEPRECATED: use obj[key].is<T>() instead
|
||||||
@ -227,19 +222,18 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
detail::ResourceManager* getResourceManager() const {
|
detail::ResourceManager* getResourceManager() const {
|
||||||
return resources_;
|
return impl_.getResourceManager();
|
||||||
}
|
}
|
||||||
|
|
||||||
detail::VariantData* getData() const {
|
detail::VariantData* getData() const {
|
||||||
return detail::collectionToVariant(data_);
|
return impl_.getData();
|
||||||
}
|
}
|
||||||
|
|
||||||
detail::VariantData* getOrCreateData() const {
|
detail::VariantData* getOrCreateData() const {
|
||||||
return detail::collectionToVariant(data_);
|
return impl_.getData();
|
||||||
}
|
}
|
||||||
|
|
||||||
detail::ObjectData* data_;
|
mutable detail::ObjectImpl impl_;
|
||||||
detail::ResourceManager* resources_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
||||||
|
@ -19,47 +19,47 @@ class JsonObjectConst : public detail::VariantOperators<JsonObjectConst> {
|
|||||||
using iterator = JsonObjectConstIterator;
|
using iterator = JsonObjectConstIterator;
|
||||||
|
|
||||||
// Creates an unbound reference.
|
// Creates an unbound reference.
|
||||||
JsonObjectConst() : data_(0), resources_(0) {}
|
JsonObjectConst() {}
|
||||||
|
|
||||||
// INTERNAL USE ONLY
|
// INTERNAL USE ONLY
|
||||||
JsonObjectConst(const detail::ObjectData* data,
|
JsonObjectConst(detail::VariantData* data, detail::ResourceManager* resources)
|
||||||
const detail::ResourceManager* resources)
|
: impl_(detail::VariantImpl(data, resources).asObject()) {}
|
||||||
: data_(data), resources_(resources) {}
|
|
||||||
|
// INTERNAL USE ONLY
|
||||||
|
JsonObjectConst(const detail::ObjectImpl& impl) : impl_(impl) {}
|
||||||
|
|
||||||
operator JsonVariantConst() const {
|
operator JsonVariantConst() const {
|
||||||
return JsonVariantConst(getData(), resources_);
|
return JsonVariantConst(impl_.getData(), impl_.getResourceManager());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if the reference is unbound.
|
// Returns true if the reference is unbound.
|
||||||
// https://arduinojson.org/v7/api/jsonobjectconst/isnull/
|
// https://arduinojson.org/v7/api/jsonobjectconst/isnull/
|
||||||
bool isNull() const {
|
bool isNull() const {
|
||||||
return data_ == 0;
|
return impl_.isNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if the reference is bound.
|
// Returns true if the reference is bound.
|
||||||
// https://arduinojson.org/v7/api/jsonobjectconst/isnull/
|
// https://arduinojson.org/v7/api/jsonobjectconst/isnull/
|
||||||
operator bool() const {
|
operator bool() const {
|
||||||
return data_ != 0;
|
return !isNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the depth (nesting level) of the object.
|
// Returns the depth (nesting level) of the object.
|
||||||
// https://arduinojson.org/v7/api/jsonobjectconst/nesting/
|
// https://arduinojson.org/v7/api/jsonobjectconst/nesting/
|
||||||
size_t nesting() const {
|
size_t nesting() const {
|
||||||
return detail::VariantData::nesting(getData(), resources_);
|
return impl_.nesting();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the number of members in the object.
|
// Returns the number of members in the object.
|
||||||
// https://arduinojson.org/v7/api/jsonobjectconst/size/
|
// https://arduinojson.org/v7/api/jsonobjectconst/size/
|
||||||
size_t size() const {
|
size_t size() const {
|
||||||
return data_ ? data_->size(resources_) : 0;
|
return impl_.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns an iterator to the first key-value pair of the object.
|
// Returns an iterator to the first key-value pair of the object.
|
||||||
// https://arduinojson.org/v7/api/jsonobjectconst/begin/
|
// https://arduinojson.org/v7/api/jsonobjectconst/begin/
|
||||||
iterator begin() const {
|
iterator begin() const {
|
||||||
if (!data_)
|
return iterator(impl_.createIterator(), impl_.getResourceManager());
|
||||||
return iterator();
|
|
||||||
return iterator(data_->createIterator(resources_), resources_);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns an iterator following the last key-value pair of the object.
|
// Returns an iterator following the last key-value pair of the object.
|
||||||
@ -74,8 +74,7 @@ class JsonObjectConst : public detail::VariantOperators<JsonObjectConst> {
|
|||||||
detail::enable_if_t<detail::IsString<TString>::value, int> = 0>
|
detail::enable_if_t<detail::IsString<TString>::value, int> = 0>
|
||||||
ARDUINOJSON_DEPRECATED("use obj[key].is<T>() instead")
|
ARDUINOJSON_DEPRECATED("use obj[key].is<T>() instead")
|
||||||
bool containsKey(const TString& key) const {
|
bool containsKey(const TString& key) const {
|
||||||
return detail::ObjectData::getMember(data_, detail::adaptString(key),
|
return impl_.getMember(detail::adaptString(key)) != 0;
|
||||||
resources_) != 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DEPRECATED: use obj["key"].is<T>() instead
|
// DEPRECATED: use obj["key"].is<T>() instead
|
||||||
@ -83,8 +82,7 @@ class JsonObjectConst : public detail::VariantOperators<JsonObjectConst> {
|
|||||||
template <typename TChar>
|
template <typename TChar>
|
||||||
ARDUINOJSON_DEPRECATED("use obj[\"key\"].is<T>() instead")
|
ARDUINOJSON_DEPRECATED("use obj[\"key\"].is<T>() instead")
|
||||||
bool containsKey(TChar* key) const {
|
bool containsKey(TChar* key) const {
|
||||||
return detail::ObjectData::getMember(data_, detail::adaptString(key),
|
return impl_.getMember(detail::adaptString(key)) != 0;
|
||||||
resources_) != 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DEPRECATED: use obj[key].is<T>() instead
|
// DEPRECATED: use obj[key].is<T>() instead
|
||||||
@ -101,9 +99,8 @@ class JsonObjectConst : public detail::VariantOperators<JsonObjectConst> {
|
|||||||
template <typename TString,
|
template <typename TString,
|
||||||
detail::enable_if_t<detail::IsString<TString>::value, int> = 0>
|
detail::enable_if_t<detail::IsString<TString>::value, int> = 0>
|
||||||
JsonVariantConst operator[](const TString& key) const {
|
JsonVariantConst operator[](const TString& key) const {
|
||||||
return JsonVariantConst(detail::ObjectData::getMember(
|
return JsonVariantConst(impl_.getMember(detail::adaptString(key)),
|
||||||
data_, detail::adaptString(key), resources_),
|
impl_.getResourceManager());
|
||||||
resources_);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gets the member with specified key.
|
// Gets the member with specified key.
|
||||||
@ -113,9 +110,8 @@ class JsonObjectConst : public detail::VariantOperators<JsonObjectConst> {
|
|||||||
!detail::is_const<TChar>::value,
|
!detail::is_const<TChar>::value,
|
||||||
int> = 0>
|
int> = 0>
|
||||||
JsonVariantConst operator[](TChar* key) const {
|
JsonVariantConst operator[](TChar* key) const {
|
||||||
return JsonVariantConst(detail::ObjectData::getMember(
|
return JsonVariantConst(impl_.getMember(detail::adaptString(key)),
|
||||||
data_, detail::adaptString(key), resources_),
|
impl_.getResourceManager());
|
||||||
resources_);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gets the member with specified key.
|
// Gets the member with specified key.
|
||||||
@ -137,11 +133,10 @@ class JsonObjectConst : public detail::VariantOperators<JsonObjectConst> {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
const detail::VariantData* getData() const {
|
const detail::VariantData* getData() const {
|
||||||
return collectionToVariant(data_);
|
return impl_.getData();
|
||||||
}
|
}
|
||||||
|
|
||||||
const detail::ObjectData* data_;
|
detail::ObjectImpl impl_;
|
||||||
const detail::ResourceManager* resources_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
inline bool operator==(JsonObjectConst lhs, JsonObjectConst rhs) {
|
inline bool operator==(JsonObjectConst lhs, JsonObjectConst rhs) {
|
||||||
|
@ -14,7 +14,7 @@ class JsonObjectIterator {
|
|||||||
public:
|
public:
|
||||||
JsonObjectIterator() {}
|
JsonObjectIterator() {}
|
||||||
|
|
||||||
explicit JsonObjectIterator(detail::ObjectData::iterator iterator,
|
explicit JsonObjectIterator(detail::ObjectImpl::iterator iterator,
|
||||||
detail::ResourceManager* resources)
|
detail::ResourceManager* resources)
|
||||||
: iterator_(iterator), resources_(resources) {}
|
: iterator_(iterator), resources_(resources) {}
|
||||||
|
|
||||||
@ -40,7 +40,7 @@ class JsonObjectIterator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
detail::ObjectData::iterator iterator_;
|
detail::ObjectImpl::iterator iterator_;
|
||||||
detail::ResourceManager* resources_;
|
detail::ResourceManager* resources_;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -50,8 +50,8 @@ class JsonObjectConstIterator {
|
|||||||
public:
|
public:
|
||||||
JsonObjectConstIterator() {}
|
JsonObjectConstIterator() {}
|
||||||
|
|
||||||
explicit JsonObjectConstIterator(detail::ObjectData::iterator iterator,
|
explicit JsonObjectConstIterator(detail::ObjectImpl::iterator iterator,
|
||||||
const detail::ResourceManager* resources)
|
detail::ResourceManager* resources)
|
||||||
: iterator_(iterator), resources_(resources) {}
|
: iterator_(iterator), resources_(resources) {}
|
||||||
|
|
||||||
JsonPairConst operator*() const {
|
JsonPairConst operator*() const {
|
||||||
@ -76,8 +76,8 @@ class JsonObjectConstIterator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
detail::ObjectData::iterator iterator_;
|
detail::ObjectImpl::iterator iterator_;
|
||||||
const detail::ResourceManager* resources_;
|
detail::ResourceManager* resources_;
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
||||||
|
@ -15,10 +15,11 @@ ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE
|
|||||||
class JsonPair {
|
class JsonPair {
|
||||||
public:
|
public:
|
||||||
// INTERNAL USE ONLY
|
// INTERNAL USE ONLY
|
||||||
JsonPair(detail::ObjectData::iterator iterator,
|
JsonPair(detail::ObjectImpl::iterator iterator,
|
||||||
detail::ResourceManager* resources) {
|
detail::ResourceManager* resources) {
|
||||||
if (!iterator.done()) {
|
if (!iterator.done()) {
|
||||||
key_ = iterator->asString(resources);
|
detail::VariantImpl variant(iterator.data(), resources);
|
||||||
|
key_ = variant.asString();
|
||||||
iterator.next(resources);
|
iterator.next(resources);
|
||||||
value_ = JsonVariant(iterator.data(), resources);
|
value_ = JsonVariant(iterator.data(), resources);
|
||||||
}
|
}
|
||||||
@ -43,10 +44,11 @@ class JsonPair {
|
|||||||
// https://arduinojson.org/v7/api/jsonobjectconst/begin_end/
|
// https://arduinojson.org/v7/api/jsonobjectconst/begin_end/
|
||||||
class JsonPairConst {
|
class JsonPairConst {
|
||||||
public:
|
public:
|
||||||
JsonPairConst(detail::ObjectData::iterator iterator,
|
JsonPairConst(detail::ObjectImpl::iterator iterator,
|
||||||
const detail::ResourceManager* resources) {
|
detail::ResourceManager* resources) {
|
||||||
if (!iterator.done()) {
|
if (!iterator.done()) {
|
||||||
key_ = iterator->asString(resources);
|
detail::VariantImpl variant(iterator.data(), resources);
|
||||||
|
key_ = variant.asString();
|
||||||
iterator.next(resources);
|
iterator.next(resources);
|
||||||
value_ = JsonVariantConst(iterator.data(), resources);
|
value_ = JsonVariantConst(iterator.data(), resources);
|
||||||
}
|
}
|
||||||
|
@ -56,17 +56,14 @@ class MemberProxy
|
|||||||
}
|
}
|
||||||
|
|
||||||
VariantData* getData() const {
|
VariantData* getData() const {
|
||||||
return VariantData::getMember(
|
VariantImpl variant(VariantAttorney::getData(upstream_),
|
||||||
VariantAttorney::getData(upstream_), key_,
|
|
||||||
VariantAttorney::getResourceManager(upstream_));
|
VariantAttorney::getResourceManager(upstream_));
|
||||||
|
return variant.getMember(key_);
|
||||||
}
|
}
|
||||||
|
|
||||||
VariantData* getOrCreateData() const {
|
VariantData* getOrCreateData() const {
|
||||||
auto data = VariantAttorney::getOrCreateData(upstream_);
|
return VariantAttorney::getOrCreateVariantImpl(upstream_).getOrAddMember(
|
||||||
if (!data)
|
key_);
|
||||||
return nullptr;
|
|
||||||
return data->getOrAddMember(key_,
|
|
||||||
VariantAttorney::getResourceManager(upstream_));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -8,63 +8,40 @@
|
|||||||
|
|
||||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||||
|
|
||||||
class ObjectData : public CollectionData {
|
class VariantImpl;
|
||||||
|
|
||||||
|
class ObjectImpl : public CollectionImpl {
|
||||||
public:
|
public:
|
||||||
template <typename TAdaptedString>
|
ObjectImpl() {}
|
||||||
VariantData* addMember(TAdaptedString key, ResourceManager* resources);
|
|
||||||
|
|
||||||
VariantData* addPair(VariantData** value, ResourceManager* resources);
|
ObjectImpl(CollectionData* data, ResourceManager* resources)
|
||||||
|
: CollectionImpl(data, resources) {}
|
||||||
|
|
||||||
template <typename TAdaptedString>
|
template <typename TAdaptedString>
|
||||||
VariantData* getOrAddMember(TAdaptedString key, ResourceManager* resources);
|
VariantData* addMember(TAdaptedString key);
|
||||||
|
|
||||||
|
VariantData* addPair(VariantData** value);
|
||||||
|
|
||||||
template <typename TAdaptedString>
|
template <typename TAdaptedString>
|
||||||
VariantData* getMember(TAdaptedString key,
|
VariantData* getOrAddMember(TAdaptedString key);
|
||||||
const ResourceManager* resources) const;
|
|
||||||
|
|
||||||
template <typename TAdaptedString>
|
template <typename TAdaptedString>
|
||||||
static VariantData* getMember(const ObjectData* object, TAdaptedString key,
|
VariantData* getMember(TAdaptedString key) const;
|
||||||
const ResourceManager* resources) {
|
|
||||||
if (!object)
|
template <typename TAdaptedString>
|
||||||
return nullptr;
|
void removeMember(TAdaptedString key);
|
||||||
return object->getMember(key, resources);
|
|
||||||
|
void remove(iterator it) {
|
||||||
|
CollectionImpl::removePair(it);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TAdaptedString>
|
size_t size() const {
|
||||||
void removeMember(TAdaptedString key, ResourceManager* resources);
|
return CollectionImpl::size() / 2;
|
||||||
|
|
||||||
template <typename TAdaptedString>
|
|
||||||
static void removeMember(ObjectData* obj, TAdaptedString key,
|
|
||||||
ResourceManager* resources) {
|
|
||||||
if (!obj)
|
|
||||||
return;
|
|
||||||
obj->removeMember(key, resources);
|
|
||||||
}
|
|
||||||
|
|
||||||
void remove(iterator it, ResourceManager* resources) {
|
|
||||||
CollectionData::removePair(it, resources);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void remove(ObjectData* obj, ObjectData::iterator it,
|
|
||||||
ResourceManager* resources) {
|
|
||||||
if (!obj)
|
|
||||||
return;
|
|
||||||
obj->remove(it, resources);
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t size(const ResourceManager* resources) const {
|
|
||||||
return CollectionData::size(resources) / 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
static size_t size(const ObjectData* obj, const ResourceManager* resources) {
|
|
||||||
if (!obj)
|
|
||||||
return 0;
|
|
||||||
return obj->size(resources);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
template <typename TAdaptedString>
|
template <typename TAdaptedString>
|
||||||
iterator findKey(TAdaptedString key, const ResourceManager* resources) const;
|
iterator findKey(TAdaptedString key) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -11,32 +11,30 @@
|
|||||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||||
|
|
||||||
template <typename TAdaptedString>
|
template <typename TAdaptedString>
|
||||||
inline VariantData* ObjectData::getMember(
|
inline VariantData* ObjectImpl::getMember(TAdaptedString key) const {
|
||||||
TAdaptedString key, const ResourceManager* resources) const {
|
auto it = findKey(key);
|
||||||
auto it = findKey(key, resources);
|
|
||||||
if (it.done())
|
if (it.done())
|
||||||
return nullptr;
|
return nullptr;
|
||||||
it.next(resources);
|
it.next(resources_);
|
||||||
return it.data();
|
return it.data();
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TAdaptedString>
|
template <typename TAdaptedString>
|
||||||
VariantData* ObjectData::getOrAddMember(TAdaptedString key,
|
VariantData* ObjectImpl::getOrAddMember(TAdaptedString key) {
|
||||||
ResourceManager* resources) {
|
auto data = getMember(key);
|
||||||
auto data = getMember(key, resources);
|
|
||||||
if (data)
|
if (data)
|
||||||
return data;
|
return data;
|
||||||
return addMember(key, resources);
|
return addMember(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TAdaptedString>
|
template <typename TAdaptedString>
|
||||||
inline ObjectData::iterator ObjectData::findKey(
|
inline ObjectImpl::iterator ObjectImpl::findKey(TAdaptedString key) const {
|
||||||
TAdaptedString key, const ResourceManager* resources) const {
|
|
||||||
if (key.isNull())
|
if (key.isNull())
|
||||||
return iterator();
|
return iterator();
|
||||||
bool isKey = true;
|
bool isKey = true;
|
||||||
for (auto it = createIterator(resources); !it.done(); it.next(resources)) {
|
for (auto it = createIterator(); !it.done(); it.next(resources_)) {
|
||||||
if (isKey && stringEquals(key, adaptString(it->asString(resources))))
|
VariantImpl variant(it.data(), resources_);
|
||||||
|
if (isKey && stringEquals(key, adaptString(variant.asString())))
|
||||||
return it;
|
return it;
|
||||||
isKey = !isKey;
|
isKey = !isKey;
|
||||||
}
|
}
|
||||||
@ -44,49 +42,55 @@ inline ObjectData::iterator ObjectData::findKey(
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename TAdaptedString>
|
template <typename TAdaptedString>
|
||||||
inline void ObjectData::removeMember(TAdaptedString key,
|
inline void ObjectImpl::removeMember(TAdaptedString key) {
|
||||||
ResourceManager* resources) {
|
remove(findKey(key));
|
||||||
remove(findKey(key, resources), resources);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TAdaptedString>
|
template <typename TAdaptedString>
|
||||||
inline VariantData* ObjectData::addMember(TAdaptedString key,
|
inline VariantData* ObjectImpl::addMember(TAdaptedString key) {
|
||||||
ResourceManager* resources) {
|
if (!data_)
|
||||||
auto keySlot = resources->allocVariant();
|
return nullptr;
|
||||||
|
ARDUINOJSON_ASSERT(resources_ != nullptr);
|
||||||
|
|
||||||
|
auto keySlot = resources_->allocVariant();
|
||||||
if (!keySlot)
|
if (!keySlot)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
auto valueSlot = resources->allocVariant();
|
auto valueSlot = resources_->allocVariant();
|
||||||
if (!valueSlot)
|
if (!valueSlot)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
if (!keySlot->setString(key, resources))
|
VariantImpl keyImpl(keySlot.ptr(), resources_);
|
||||||
|
if (!keyImpl.setString(key))
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
CollectionData::appendPair(keySlot, valueSlot, resources);
|
CollectionImpl::appendPair(keySlot, valueSlot);
|
||||||
|
|
||||||
return valueSlot.ptr();
|
return valueSlot.ptr();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline VariantData* ObjectData::addPair(VariantData** value,
|
inline VariantData* ObjectImpl::addPair(VariantData** value) {
|
||||||
ResourceManager* resources) {
|
if (!data_)
|
||||||
auto keySlot = resources->allocVariant();
|
return nullptr;
|
||||||
|
ARDUINOJSON_ASSERT(resources_ != nullptr);
|
||||||
|
|
||||||
|
auto keySlot = resources_->allocVariant();
|
||||||
if (!keySlot)
|
if (!keySlot)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
auto valueSlot = resources->allocVariant();
|
auto valueSlot = resources_->allocVariant();
|
||||||
if (!valueSlot)
|
if (!valueSlot)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
*value = valueSlot.ptr();
|
*value = valueSlot.ptr();
|
||||||
|
|
||||||
CollectionData::appendPair(keySlot, valueSlot, resources);
|
CollectionImpl::appendPair(keySlot, valueSlot);
|
||||||
|
|
||||||
return keySlot.ptr();
|
return keySlot.ptr();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the size (in bytes) of an object with n members.
|
// Returns the size (in bytes) of an object with n members.
|
||||||
constexpr size_t sizeofObject(size_t n) {
|
constexpr size_t sizeofObject(size_t n) {
|
||||||
return 2 * n * ResourceManager::slotSize;
|
return 2 * n * sizeof(VariantData);
|
||||||
}
|
}
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -14,7 +14,8 @@ size_t measure(ArduinoJson::JsonVariantConst source) {
|
|||||||
auto data = VariantAttorney::getData(source);
|
auto data = VariantAttorney::getData(source);
|
||||||
auto resources = VariantAttorney::getResourceManager(source);
|
auto resources = VariantAttorney::getResourceManager(source);
|
||||||
TSerializer<DummyWriter> serializer(dp, resources);
|
TSerializer<DummyWriter> serializer(dp, resources);
|
||||||
return VariantData::accept(data, resources, serializer);
|
VariantImpl variant(data, resources);
|
||||||
|
return variant.accept(serializer);
|
||||||
}
|
}
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -13,7 +13,8 @@ size_t doSerialize(ArduinoJson::JsonVariantConst source, TWriter writer) {
|
|||||||
auto data = VariantAttorney::getData(source);
|
auto data = VariantAttorney::getData(source);
|
||||||
auto resources = VariantAttorney::getResourceManager(source);
|
auto resources = VariantAttorney::getResourceManager(source);
|
||||||
TSerializer<TWriter> serializer(writer, resources);
|
TSerializer<TWriter> serializer(writer, resources);
|
||||||
return VariantData::accept(data, resources, serializer);
|
VariantImpl variant(data, resources);
|
||||||
|
return variant.accept(serializer);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <template <typename> class TSerializer, typename TDestination>
|
template <template <typename> class TSerializer, typename TDestination>
|
||||||
|
@ -60,25 +60,18 @@ struct Converter<T, detail::enable_if_t<detail::is_integral<T>::value &&
|
|||||||
: private detail::VariantAttorney {
|
: private detail::VariantAttorney {
|
||||||
static bool toJson(T src, JsonVariant dst) {
|
static bool toJson(T src, JsonVariant dst) {
|
||||||
ARDUINOJSON_ASSERT_INTEGER_TYPE_IS_SUPPORTED(T);
|
ARDUINOJSON_ASSERT_INTEGER_TYPE_IS_SUPPORTED(T);
|
||||||
auto data = getData(dst);
|
auto variant = getVariantImpl(dst);
|
||||||
if (!data)
|
variant.clear();
|
||||||
return false;
|
return variant.setInteger(src);
|
||||||
auto resources = getResourceManager(dst);
|
|
||||||
data->clear(resources);
|
|
||||||
return data->setInteger(src, resources);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static T fromJson(JsonVariantConst src) {
|
static T fromJson(JsonVariantConst src) {
|
||||||
ARDUINOJSON_ASSERT_INTEGER_TYPE_IS_SUPPORTED(T);
|
ARDUINOJSON_ASSERT_INTEGER_TYPE_IS_SUPPORTED(T);
|
||||||
auto data = getData(src);
|
return getVariantImpl(src).template asIntegral<T>();
|
||||||
auto resources = getResourceManager(src);
|
|
||||||
return data ? data->template asIntegral<T>(resources) : T();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool checkJson(JsonVariantConst src) {
|
static bool checkJson(JsonVariantConst src) {
|
||||||
auto data = getData(src);
|
return getVariantImpl(src).template isInteger<T>();
|
||||||
auto resources = getResourceManager(src);
|
|
||||||
return data && data->template isInteger<T>(resources);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -90,40 +83,29 @@ struct Converter<T, detail::enable_if_t<detail::is_enum<T>::value>>
|
|||||||
}
|
}
|
||||||
|
|
||||||
static T fromJson(JsonVariantConst src) {
|
static T fromJson(JsonVariantConst src) {
|
||||||
auto data = getData(src);
|
return static_cast<T>(getVariantImpl(src).template asIntegral<int>());
|
||||||
auto resources = getResourceManager(src);
|
|
||||||
return data ? static_cast<T>(data->template asIntegral<int>(resources))
|
|
||||||
: T();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool checkJson(JsonVariantConst src) {
|
static bool checkJson(JsonVariantConst src) {
|
||||||
auto data = getData(src);
|
return getVariantImpl(src).template isInteger<int>();
|
||||||
auto resources = getResourceManager(src);
|
|
||||||
return data && data->template isInteger<int>(resources);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
struct Converter<bool> : private detail::VariantAttorney {
|
struct Converter<bool> : private detail::VariantAttorney {
|
||||||
static bool toJson(bool src, JsonVariant dst) {
|
static bool toJson(bool src, JsonVariant dst) {
|
||||||
auto data = getData(dst);
|
auto variant = getVariantImpl(dst);
|
||||||
if (!data)
|
variant.clear();
|
||||||
return false;
|
return variant.setBoolean(src);
|
||||||
auto resources = getResourceManager(dst);
|
|
||||||
data->clear(resources);
|
|
||||||
data->setBoolean(src);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool fromJson(JsonVariantConst src) {
|
static bool fromJson(JsonVariantConst src) {
|
||||||
auto data = getData(src);
|
return getVariantImpl(src).asBoolean();
|
||||||
auto resources = getResourceManager(src);
|
|
||||||
return data ? data->asBoolean(resources) : false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool checkJson(JsonVariantConst src) {
|
static bool checkJson(JsonVariantConst src) {
|
||||||
auto data = getData(src);
|
auto data = getData(src);
|
||||||
return data && data->isBoolean();
|
return data && data->type == detail::VariantType::Boolean;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -131,18 +113,13 @@ template <typename T>
|
|||||||
struct Converter<T, detail::enable_if_t<detail::is_floating_point<T>::value>>
|
struct Converter<T, detail::enable_if_t<detail::is_floating_point<T>::value>>
|
||||||
: private detail::VariantAttorney {
|
: private detail::VariantAttorney {
|
||||||
static bool toJson(T src, JsonVariant dst) {
|
static bool toJson(T src, JsonVariant dst) {
|
||||||
auto data = getData(dst);
|
auto variant = getVariantImpl(dst);
|
||||||
if (!data)
|
variant.clear();
|
||||||
return false;
|
return variant.setFloat(src);
|
||||||
auto resources = getResourceManager(dst);
|
|
||||||
data->clear(resources);
|
|
||||||
return data->setFloat(src, resources);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static T fromJson(JsonVariantConst src) {
|
static T fromJson(JsonVariantConst src) {
|
||||||
auto data = getData(src);
|
return getVariantImpl(src).template asFloat<T>();
|
||||||
auto resources = getResourceManager(src);
|
|
||||||
return data ? data->template asFloat<T>(resources) : 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool checkJson(JsonVariantConst src) {
|
static bool checkJson(JsonVariantConst src) {
|
||||||
@ -154,13 +131,13 @@ struct Converter<T, detail::enable_if_t<detail::is_floating_point<T>::value>>
|
|||||||
template <>
|
template <>
|
||||||
struct Converter<const char*> : private detail::VariantAttorney {
|
struct Converter<const char*> : private detail::VariantAttorney {
|
||||||
static void toJson(const char* src, JsonVariant dst) {
|
static void toJson(const char* src, JsonVariant dst) {
|
||||||
detail::VariantData::setString(getData(dst), detail::adaptString(src),
|
auto variant = getVariantImpl(dst);
|
||||||
getResourceManager(dst));
|
variant.clear();
|
||||||
|
variant.setString(detail::adaptString(src));
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char* fromJson(JsonVariantConst src) {
|
static const char* fromJson(JsonVariantConst src) {
|
||||||
auto data = getData(src);
|
return getVariantImpl(src).asString().c_str();
|
||||||
return data ? data->asString(getResourceManager(src)).c_str() : 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool checkJson(JsonVariantConst src) {
|
static bool checkJson(JsonVariantConst src) {
|
||||||
@ -172,13 +149,13 @@ struct Converter<const char*> : private detail::VariantAttorney {
|
|||||||
template <>
|
template <>
|
||||||
struct Converter<JsonString> : private detail::VariantAttorney {
|
struct Converter<JsonString> : private detail::VariantAttorney {
|
||||||
static void toJson(JsonString src, JsonVariant dst) {
|
static void toJson(JsonString src, JsonVariant dst) {
|
||||||
detail::VariantData::setString(getData(dst), detail::adaptString(src),
|
auto variant = getVariantImpl(dst);
|
||||||
getResourceManager(dst));
|
variant.clear();
|
||||||
|
variant.setString(detail::adaptString(src));
|
||||||
}
|
}
|
||||||
|
|
||||||
static JsonString fromJson(JsonVariantConst src) {
|
static JsonString fromJson(JsonVariantConst src) {
|
||||||
auto data = getData(src);
|
return getVariantImpl(src).asString();
|
||||||
return data ? data->asString(getResourceManager(src)) : JsonString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool checkJson(JsonVariantConst src) {
|
static bool checkJson(JsonVariantConst src) {
|
||||||
@ -190,10 +167,9 @@ struct Converter<JsonString> : private detail::VariantAttorney {
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
inline detail::enable_if_t<detail::IsString<T>::value> convertToJson(
|
inline detail::enable_if_t<detail::IsString<T>::value> convertToJson(
|
||||||
const T& src, JsonVariant dst) {
|
const T& src, JsonVariant dst) {
|
||||||
using namespace detail;
|
auto variant = detail::VariantAttorney::getVariantImpl(dst);
|
||||||
auto data = VariantAttorney::getData(dst);
|
variant.clear();
|
||||||
auto resources = VariantAttorney::getResourceManager(dst);
|
variant.setString(detail::adaptString(src));
|
||||||
detail::VariantData::setString(data, adaptString(src), resources);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SerializedValue<std::string>
|
// SerializedValue<std::string>
|
||||||
@ -202,22 +178,23 @@ inline detail::enable_if_t<detail::IsString<T>::value> convertToJson(
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
struct Converter<SerializedValue<T>> : private detail::VariantAttorney {
|
struct Converter<SerializedValue<T>> : private detail::VariantAttorney {
|
||||||
static void toJson(SerializedValue<T> src, JsonVariant dst) {
|
static void toJson(SerializedValue<T> src, JsonVariant dst) {
|
||||||
detail::VariantData::setRawString(getData(dst), src,
|
auto variant = getVariantImpl(dst);
|
||||||
getResourceManager(dst));
|
variant.clear();
|
||||||
|
variant.setRawString(src);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
struct Converter<detail::nullptr_t> : private detail::VariantAttorney {
|
struct Converter<detail::nullptr_t> : private detail::VariantAttorney {
|
||||||
static void toJson(detail::nullptr_t, JsonVariant dst) {
|
static void toJson(detail::nullptr_t, JsonVariant dst) {
|
||||||
detail::VariantData::clear(getData(dst), getResourceManager(dst));
|
getVariantImpl(dst).clear();
|
||||||
}
|
}
|
||||||
static detail::nullptr_t fromJson(JsonVariantConst) {
|
static detail::nullptr_t fromJson(JsonVariantConst) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
static bool checkJson(JsonVariantConst src) {
|
static bool checkJson(JsonVariantConst src) {
|
||||||
auto data = getData(src);
|
auto data = getData(src);
|
||||||
return data == 0 || data->isNull();
|
return data == 0 || data->type == detail::VariantType::Null;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -263,7 +240,8 @@ inline void convertToJson(const ::Printable& src, JsonVariant dst) {
|
|||||||
auto data = detail::VariantAttorney::getData(dst);
|
auto data = detail::VariantAttorney::getData(dst);
|
||||||
if (!resources || !data)
|
if (!resources || !data)
|
||||||
return;
|
return;
|
||||||
data->clear(resources);
|
detail::VariantImpl impl(data, resources);
|
||||||
|
impl.clear();
|
||||||
detail::StringBuilderPrint print(resources);
|
detail::StringBuilderPrint print(resources);
|
||||||
src.printTo(print);
|
src.printTo(print);
|
||||||
if (print.overflowed())
|
if (print.overflowed())
|
||||||
@ -329,14 +307,12 @@ struct Converter<JsonArrayConst> : private detail::VariantAttorney {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static JsonArrayConst fromJson(JsonVariantConst src) {
|
static JsonArrayConst fromJson(JsonVariantConst src) {
|
||||||
auto data = getData(src);
|
return JsonArrayConst(getData(src), getResourceManager(src));
|
||||||
auto array = data ? data->asArray() : nullptr;
|
|
||||||
return JsonArrayConst(array, getResourceManager(src));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool checkJson(JsonVariantConst src) {
|
static bool checkJson(JsonVariantConst src) {
|
||||||
auto data = getData(src);
|
auto data = getData(src);
|
||||||
return data && data->isArray();
|
return data && data->type == detail::VariantType::Array;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -350,14 +326,12 @@ struct Converter<JsonArray> : private detail::VariantAttorney {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static JsonArray fromJson(JsonVariant src) {
|
static JsonArray fromJson(JsonVariant src) {
|
||||||
auto data = getData(src);
|
return JsonArray(getData(src), getResourceManager(src));
|
||||||
auto resources = getResourceManager(src);
|
|
||||||
return JsonArray(data != 0 ? data->asArray() : 0, resources);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool checkJson(JsonVariant src) {
|
static bool checkJson(JsonVariant src) {
|
||||||
auto data = getData(src);
|
auto data = getData(src);
|
||||||
return data && data->isArray();
|
return data && data->type == detail::VariantType::Array;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -371,14 +345,12 @@ struct Converter<JsonObjectConst> : private detail::VariantAttorney {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static JsonObjectConst fromJson(JsonVariantConst src) {
|
static JsonObjectConst fromJson(JsonVariantConst src) {
|
||||||
auto data = getData(src);
|
return JsonObjectConst(getData(src), getResourceManager(src));
|
||||||
auto object = data != 0 ? data->asObject() : nullptr;
|
|
||||||
return JsonObjectConst(object, getResourceManager(src));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool checkJson(JsonVariantConst src) {
|
static bool checkJson(JsonVariantConst src) {
|
||||||
auto data = getData(src);
|
auto data = getData(src);
|
||||||
return data && data->isObject();
|
return data && data->type == detail::VariantType::Object;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -392,14 +364,12 @@ struct Converter<JsonObject> : private detail::VariantAttorney {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static JsonObject fromJson(JsonVariant src) {
|
static JsonObject fromJson(JsonVariant src) {
|
||||||
auto data = getData(src);
|
return JsonObject(getData(src), getResourceManager(src));
|
||||||
auto resources = getResourceManager(src);
|
|
||||||
return JsonObject(data != 0 ? data->asObject() : 0, resources);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool checkJson(JsonVariant src) {
|
static bool checkJson(JsonVariant src) {
|
||||||
auto data = getData(src);
|
auto data = getData(src);
|
||||||
return data && data->isObject();
|
return data && data->type == detail::VariantType::Object;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -16,27 +16,29 @@ class JsonVariant : public detail::VariantRefBase<JsonVariant>,
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
// Creates an unbound reference.
|
// Creates an unbound reference.
|
||||||
JsonVariant() : data_(0), resources_(0) {}
|
JsonVariant() {}
|
||||||
|
|
||||||
// INTERNAL USE ONLY
|
// INTERNAL USE ONLY
|
||||||
JsonVariant(detail::VariantData* data, detail::ResourceManager* resources)
|
JsonVariant(detail::VariantData* data, detail::ResourceManager* resources)
|
||||||
: data_(data), resources_(resources) {}
|
: impl_(data, resources) {}
|
||||||
|
|
||||||
|
// INTERNAL USE ONLY
|
||||||
|
JsonVariant(detail::VariantImpl impl) : impl_(impl) {}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
detail::ResourceManager* getResourceManager() const {
|
detail::ResourceManager* getResourceManager() const {
|
||||||
return resources_;
|
return impl_.getResourceManager();
|
||||||
}
|
}
|
||||||
|
|
||||||
detail::VariantData* getData() const {
|
detail::VariantData* getData() const {
|
||||||
return data_;
|
return impl_.getData();
|
||||||
}
|
}
|
||||||
|
|
||||||
detail::VariantData* getOrCreateData() const {
|
detail::VariantData* getOrCreateData() const {
|
||||||
return data_;
|
return impl_.getData();
|
||||||
}
|
}
|
||||||
|
|
||||||
detail::VariantData* data_;
|
mutable detail::VariantImpl impl_;
|
||||||
detail::ResourceManager* resources_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
@ -35,34 +35,34 @@ class JsonVariantConst : public detail::VariantTag,
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
// Creates an unbound reference.
|
// Creates an unbound reference.
|
||||||
JsonVariantConst() : data_(nullptr), resources_(nullptr) {}
|
JsonVariantConst() {}
|
||||||
|
|
||||||
// INTERNAL USE ONLY
|
// INTERNAL USE ONLY
|
||||||
explicit JsonVariantConst(const detail::VariantData* data,
|
explicit JsonVariantConst(detail::VariantData* data,
|
||||||
const detail::ResourceManager* resources)
|
detail::ResourceManager* resources)
|
||||||
: data_(data), resources_(resources) {}
|
: impl_(data, resources) {}
|
||||||
|
|
||||||
// Returns true if the value is null or the reference is unbound.
|
// Returns true if the value is null or the reference is unbound.
|
||||||
// https://arduinojson.org/v7/api/jsonvariantconst/isnull/
|
// https://arduinojson.org/v7/api/jsonvariantconst/isnull/
|
||||||
bool isNull() const {
|
bool isNull() const {
|
||||||
return detail::VariantData::isNull(data_);
|
return impl_.isNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if the reference is unbound.
|
// Returns true if the reference is unbound.
|
||||||
bool isUnbound() const {
|
bool isUnbound() const {
|
||||||
return !data_;
|
return impl_.getData() == nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the depth (nesting level) of the value.
|
// Returns the depth (nesting level) of the value.
|
||||||
// https://arduinojson.org/v7/api/jsonvariantconst/nesting/
|
// https://arduinojson.org/v7/api/jsonvariantconst/nesting/
|
||||||
size_t nesting() const {
|
size_t nesting() const {
|
||||||
return detail::VariantData::nesting(data_, resources_);
|
return impl_.nesting();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the size of the array or object.
|
// Returns the size of the array or object.
|
||||||
// https://arduinojson.org/v7/api/jsonvariantconst/size/
|
// https://arduinojson.org/v7/api/jsonvariantconst/size/
|
||||||
size_t size() const {
|
size_t size() const {
|
||||||
return detail::VariantData::size(data_, resources_);
|
return impl_.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Casts the value to the specified type.
|
// Casts the value to the specified type.
|
||||||
@ -104,9 +104,8 @@ class JsonVariantConst : public detail::VariantTag,
|
|||||||
template <typename T,
|
template <typename T,
|
||||||
detail::enable_if_t<detail::is_integral<T>::value, int> = 0>
|
detail::enable_if_t<detail::is_integral<T>::value, int> = 0>
|
||||||
JsonVariantConst operator[](T index) const {
|
JsonVariantConst operator[](T index) const {
|
||||||
return JsonVariantConst(
|
return JsonVariantConst(impl_.getElement(size_t(index)),
|
||||||
detail::VariantData::getElement(data_, size_t(index), resources_),
|
impl_.getResourceManager());
|
||||||
resources_);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gets object's member with specified key.
|
// Gets object's member with specified key.
|
||||||
@ -114,9 +113,8 @@ class JsonVariantConst : public detail::VariantTag,
|
|||||||
template <typename TString,
|
template <typename TString,
|
||||||
detail::enable_if_t<detail::IsString<TString>::value, int> = 0>
|
detail::enable_if_t<detail::IsString<TString>::value, int> = 0>
|
||||||
JsonVariantConst operator[](const TString& key) const {
|
JsonVariantConst operator[](const TString& key) const {
|
||||||
return JsonVariantConst(detail::VariantData::getMember(
|
return JsonVariantConst(impl_.getMember(detail::adaptString(key)),
|
||||||
data_, detail::adaptString(key), resources_),
|
impl_.getResourceManager());
|
||||||
resources_);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gets object's member with specified key.
|
// Gets object's member with specified key.
|
||||||
@ -126,9 +124,8 @@ class JsonVariantConst : public detail::VariantTag,
|
|||||||
!detail::is_const<TChar>::value,
|
!detail::is_const<TChar>::value,
|
||||||
int> = 0>
|
int> = 0>
|
||||||
JsonVariantConst operator[](TChar* key) const {
|
JsonVariantConst operator[](TChar* key) const {
|
||||||
return JsonVariantConst(detail::VariantData::getMember(
|
return JsonVariantConst(impl_.getMember(detail::adaptString(key)),
|
||||||
data_, detail::adaptString(key), resources_),
|
impl_.getResourceManager());
|
||||||
resources_);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gets object's member with specified key or the array's element at the
|
// Gets object's member with specified key or the array's element at the
|
||||||
@ -149,8 +146,7 @@ class JsonVariantConst : public detail::VariantTag,
|
|||||||
detail::enable_if_t<detail::IsString<TString>::value, int> = 0>
|
detail::enable_if_t<detail::IsString<TString>::value, int> = 0>
|
||||||
ARDUINOJSON_DEPRECATED("use var[key].is<T>() instead")
|
ARDUINOJSON_DEPRECATED("use var[key].is<T>() instead")
|
||||||
bool containsKey(const TString& key) const {
|
bool containsKey(const TString& key) const {
|
||||||
return detail::VariantData::getMember(getData(), detail::adaptString(key),
|
return impl_.getMember(detail::adaptString(key)) != 0;
|
||||||
resources_) != 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DEPRECATED: use obj["key"].is<T>() instead
|
// DEPRECATED: use obj["key"].is<T>() instead
|
||||||
@ -161,8 +157,7 @@ class JsonVariantConst : public detail::VariantTag,
|
|||||||
int> = 0>
|
int> = 0>
|
||||||
ARDUINOJSON_DEPRECATED("use obj[\"key\"].is<T>() instead")
|
ARDUINOJSON_DEPRECATED("use obj[\"key\"].is<T>() instead")
|
||||||
bool containsKey(TChar* key) const {
|
bool containsKey(TChar* key) const {
|
||||||
return detail::VariantData::getMember(getData(), detail::adaptString(key),
|
return impl_.getMember(detail::adaptString(key)) != 0;
|
||||||
resources_) != 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DEPRECATED: use obj[key].is<T>() instead
|
// DEPRECATED: use obj[key].is<T>() instead
|
||||||
@ -181,17 +176,16 @@ class JsonVariantConst : public detail::VariantTag,
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
const detail::VariantData* getData() const {
|
detail::VariantData* getData() const {
|
||||||
return data_;
|
return impl_.getData();
|
||||||
}
|
}
|
||||||
|
|
||||||
const detail::ResourceManager* getResourceManager() const {
|
detail::ResourceManager* getResourceManager() const {
|
||||||
return resources_;
|
return impl_.getResourceManager();
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const detail::VariantData* data_;
|
mutable detail::VariantImpl impl_;
|
||||||
const detail::ResourceManager* resources_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
||||||
|
@ -26,15 +26,14 @@ class VisitorAdapter {
|
|||||||
public:
|
public:
|
||||||
using result_type = typename TVisitor::result_type;
|
using result_type = typename TVisitor::result_type;
|
||||||
|
|
||||||
VisitorAdapter(TVisitor& visitor, const ResourceManager* resources)
|
VisitorAdapter(TVisitor& visitor) : visitor_(&visitor) {}
|
||||||
: visitor_(&visitor), resources_(resources) {}
|
|
||||||
|
|
||||||
result_type visit(const ArrayData& value) {
|
result_type visit(const ArrayImpl& array) {
|
||||||
return visitor_->visit(JsonArrayConst(&value, resources_));
|
return visitor_->visit(JsonArrayConst(array));
|
||||||
}
|
}
|
||||||
|
|
||||||
result_type visit(const ObjectData& value) {
|
result_type visit(const ObjectImpl& object) {
|
||||||
return visitor_->visit(JsonObjectConst(&value, resources_));
|
return visitor_->visit(JsonObjectConst(object));
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
@ -44,18 +43,16 @@ class VisitorAdapter {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
TVisitor* visitor_;
|
TVisitor* visitor_;
|
||||||
const ResourceManager* resources_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename TVisitor>
|
template <typename TVisitor>
|
||||||
typename TVisitor::result_type accept(JsonVariantConst variant,
|
typename TVisitor::result_type accept(JsonVariantConst variant,
|
||||||
TVisitor& visit) {
|
TVisitor& visit) {
|
||||||
auto data = VariantAttorney::getData(variant);
|
auto data = VariantAttorney::getData(variant);
|
||||||
if (!data)
|
|
||||||
return visit.visit(nullptr);
|
|
||||||
auto resources = VariantAttorney::getResourceManager(variant);
|
auto resources = VariantAttorney::getResourceManager(variant);
|
||||||
VisitorAdapter<TVisitor> adapter(visit, resources);
|
VariantImpl impl(data, resources);
|
||||||
return data->accept(adapter, resources);
|
VisitorAdapter<TVisitor> adapter(visit);
|
||||||
|
return impl.accept(adapter);
|
||||||
}
|
}
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -26,6 +26,16 @@ class VariantAttorney {
|
|||||||
return client.getData();
|
return client.getData();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename TClient>
|
||||||
|
static VariantImpl getVariantImpl(TClient& client) {
|
||||||
|
return VariantImpl(client.getData(), client.getResourceManager());
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename TClient>
|
||||||
|
static VariantImpl getOrCreateVariantImpl(TClient& client) {
|
||||||
|
return VariantImpl(client.getOrCreateData(), client.getResourceManager());
|
||||||
|
}
|
||||||
|
|
||||||
template <typename TClient>
|
template <typename TClient>
|
||||||
static VariantData* getOrCreateData(TClient& client) {
|
static VariantData* getOrCreateData(TClient& client) {
|
||||||
return client.getOrCreateData();
|
return client.getOrCreateData();
|
||||||
|
@ -16,8 +16,8 @@ ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
|||||||
enum class VariantTypeBits : uint8_t {
|
enum class VariantTypeBits : uint8_t {
|
||||||
OwnedStringBit = 0x01, // 0000 0001
|
OwnedStringBit = 0x01, // 0000 0001
|
||||||
NumberBit = 0x08, // 0000 1000
|
NumberBit = 0x08, // 0000 1000
|
||||||
#if ARDUINOJSON_USE_EXTENSIONS
|
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||||
ExtensionBit = 0x10, // 0001 0000
|
EightByteBit = 0x10, // 0001 0000
|
||||||
#endif
|
#endif
|
||||||
CollectionMask = 0x60,
|
CollectionMask = 0x60,
|
||||||
};
|
};
|
||||||
@ -57,15 +57,13 @@ union VariantContent {
|
|||||||
uint32_t asUint32;
|
uint32_t asUint32;
|
||||||
int32_t asInt32;
|
int32_t asInt32;
|
||||||
SlotId asSlotId;
|
SlotId asSlotId;
|
||||||
ArrayData asArray;
|
|
||||||
ObjectData asObject;
|
|
||||||
CollectionData asCollection;
|
CollectionData asCollection;
|
||||||
struct StringNode* asOwnedString;
|
struct StringNode* asOwnedString;
|
||||||
char asTinyString[tinyStringMaxLength + 1];
|
char asTinyString[tinyStringMaxLength + 1];
|
||||||
};
|
};
|
||||||
|
|
||||||
#if ARDUINOJSON_USE_EXTENSIONS
|
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||||
union VariantExtension {
|
union EightByteValue {
|
||||||
# if ARDUINOJSON_USE_LONG_LONG
|
# if ARDUINOJSON_USE_LONG_LONG
|
||||||
uint64_t asUint64;
|
uint64_t asUint64;
|
||||||
int64_t asInt64;
|
int64_t asInt64;
|
||||||
@ -74,6 +72,9 @@ union VariantExtension {
|
|||||||
double asDouble;
|
double asDouble;
|
||||||
# endif
|
# endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static_assert(sizeof(EightByteValue) == 8,
|
||||||
|
"sizeof(EightByteValue) must be 8 bytes");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -27,12 +27,11 @@ static bool isTinyString(const T& s, size_t n) {
|
|||||||
return !containsNul;
|
return !containsNul;
|
||||||
}
|
}
|
||||||
|
|
||||||
class VariantData {
|
struct VariantData {
|
||||||
VariantContent content_; // must be first to allow cast from array to variant
|
VariantContent content; // must be first to allow cast from array to variant
|
||||||
VariantType type_;
|
VariantType type = VariantType::Null;
|
||||||
SlotId next_;
|
SlotId next = NULL_SLOT;
|
||||||
|
|
||||||
public:
|
|
||||||
// Placement new
|
// Placement new
|
||||||
static void* operator new(size_t, void* p) noexcept {
|
static void* operator new(size_t, void* p) noexcept {
|
||||||
return p;
|
return p;
|
||||||
@ -40,192 +39,216 @@ class VariantData {
|
|||||||
|
|
||||||
static void operator delete(void*, void*) noexcept {}
|
static void operator delete(void*, void*) noexcept {}
|
||||||
|
|
||||||
VariantData() : type_(VariantType::Null), next_(NULL_SLOT) {}
|
void setBoolean(bool value) {
|
||||||
|
ARDUINOJSON_ASSERT(type == VariantType::Null);
|
||||||
SlotId next() const {
|
type = VariantType::Boolean;
|
||||||
return next_;
|
content.asBoolean = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setNext(SlotId slot) {
|
template <typename TAdaptedString>
|
||||||
next_ = slot;
|
void setTinyString(const TAdaptedString& s) {
|
||||||
|
ARDUINOJSON_ASSERT(type == VariantType::Null);
|
||||||
|
ARDUINOJSON_ASSERT(s.size() <= tinyStringMaxLength);
|
||||||
|
|
||||||
|
type = VariantType::TinyString;
|
||||||
|
|
||||||
|
auto n = uint8_t(s.size());
|
||||||
|
for (uint8_t i = 0; i < n; i++) {
|
||||||
|
char c = s[i];
|
||||||
|
ARDUINOJSON_ASSERT(c != 0); // no NUL in tiny string
|
||||||
|
content.asTinyString[i] = c;
|
||||||
|
}
|
||||||
|
|
||||||
|
content.asTinyString[n] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setOwnedString(StringNode* s) {
|
||||||
|
ARDUINOJSON_ASSERT(type == VariantType::Null);
|
||||||
|
ARDUINOJSON_ASSERT(s);
|
||||||
|
type = VariantType::OwnedString;
|
||||||
|
content.asOwnedString = s;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setRawString(StringNode* s) {
|
||||||
|
ARDUINOJSON_ASSERT(type == VariantType::Null);
|
||||||
|
ARDUINOJSON_ASSERT(s);
|
||||||
|
type = VariantType::RawString;
|
||||||
|
content.asOwnedString = s;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isFloat() const {
|
||||||
|
return type & VariantTypeBits::NumberBit;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isString() const {
|
||||||
|
return type == VariantType::LinkedString ||
|
||||||
|
type == VariantType::OwnedString || type == VariantType::TinyString;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class VariantImpl {
|
||||||
|
public:
|
||||||
|
VariantImpl() : data_(nullptr), resources_(nullptr) {}
|
||||||
|
|
||||||
|
VariantImpl(VariantData* data, ResourceManager* resources)
|
||||||
|
: data_(data), resources_(resources) {}
|
||||||
|
|
||||||
|
VariantData* getData() const {
|
||||||
|
return data_;
|
||||||
|
}
|
||||||
|
|
||||||
|
ResourceManager* getResourceManager() const {
|
||||||
|
return resources_;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TVisitor>
|
template <typename TVisitor>
|
||||||
typename TVisitor::result_type accept(
|
typename TVisitor::result_type accept(TVisitor& visit) {
|
||||||
TVisitor& visit, const ResourceManager* resources) const {
|
if (!data_)
|
||||||
#if ARDUINOJSON_USE_EXTENSIONS
|
return visit.visit(nullptr);
|
||||||
auto extension = getExtension(resources);
|
|
||||||
#else
|
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||||
(void)resources; // silence warning
|
auto eightByteValue = getEightByte();
|
||||||
#endif
|
#endif
|
||||||
switch (type_) {
|
switch (data_->type) {
|
||||||
case VariantType::Float:
|
case VariantType::Float:
|
||||||
return visit.visit(content_.asFloat);
|
return visit.visit(data_->content.asFloat);
|
||||||
|
|
||||||
#if ARDUINOJSON_USE_DOUBLE
|
#if ARDUINOJSON_USE_DOUBLE
|
||||||
case VariantType::Double:
|
case VariantType::Double:
|
||||||
return visit.visit(extension->asDouble);
|
return visit.visit(eightByteValue->asDouble);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
case VariantType::Array:
|
case VariantType::Array:
|
||||||
return visit.visit(content_.asArray);
|
return visit.visit(asArray());
|
||||||
|
|
||||||
case VariantType::Object:
|
case VariantType::Object:
|
||||||
return visit.visit(content_.asObject);
|
return visit.visit(asObject());
|
||||||
|
|
||||||
case VariantType::TinyString:
|
case VariantType::TinyString:
|
||||||
return visit.visit(JsonString(content_.asTinyString));
|
return visit.visit(JsonString(data_->content.asTinyString));
|
||||||
|
|
||||||
case VariantType::LinkedString:
|
case VariantType::LinkedString:
|
||||||
return visit.visit(JsonString(asLinkedString(resources), true));
|
return visit.visit(JsonString(asLinkedString(), true));
|
||||||
|
|
||||||
case VariantType::OwnedString:
|
case VariantType::OwnedString:
|
||||||
return visit.visit(JsonString(content_.asOwnedString->data,
|
return visit.visit(JsonString(data_->content.asOwnedString->data,
|
||||||
content_.asOwnedString->length));
|
data_->content.asOwnedString->length));
|
||||||
|
|
||||||
case VariantType::RawString:
|
case VariantType::RawString:
|
||||||
return visit.visit(RawString(content_.asOwnedString->data,
|
return visit.visit(RawString(data_->content.asOwnedString->data,
|
||||||
content_.asOwnedString->length));
|
data_->content.asOwnedString->length));
|
||||||
|
|
||||||
case VariantType::Int32:
|
case VariantType::Int32:
|
||||||
return visit.visit(static_cast<JsonInteger>(content_.asInt32));
|
return visit.visit(static_cast<JsonInteger>(data_->content.asInt32));
|
||||||
|
|
||||||
case VariantType::Uint32:
|
case VariantType::Uint32:
|
||||||
return visit.visit(static_cast<JsonUInt>(content_.asUint32));
|
return visit.visit(static_cast<JsonUInt>(data_->content.asUint32));
|
||||||
|
|
||||||
#if ARDUINOJSON_USE_LONG_LONG
|
#if ARDUINOJSON_USE_LONG_LONG
|
||||||
case VariantType::Int64:
|
case VariantType::Int64:
|
||||||
return visit.visit(extension->asInt64);
|
return visit.visit(eightByteValue->asInt64);
|
||||||
|
|
||||||
case VariantType::Uint64:
|
case VariantType::Uint64:
|
||||||
return visit.visit(extension->asUint64);
|
return visit.visit(eightByteValue->asUint64);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
case VariantType::Boolean:
|
case VariantType::Boolean:
|
||||||
return visit.visit(content_.asBoolean != 0);
|
return visit.visit(data_->content.asBoolean != 0);
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return visit.visit(nullptr);
|
return visit.visit(nullptr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TVisitor>
|
VariantData* addElement() {
|
||||||
static typename TVisitor::result_type accept(const VariantData* var,
|
auto array = isNull() ? toArray() : asArray();
|
||||||
const ResourceManager* resources,
|
return array.addElement();
|
||||||
TVisitor& visit) {
|
|
||||||
if (var != 0)
|
|
||||||
return var->accept(visit, resources);
|
|
||||||
else
|
|
||||||
return visit.visit(nullptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
VariantData* addElement(ResourceManager* resources) {
|
|
||||||
auto array = isNull() ? &toArray() : asArray();
|
|
||||||
return detail::ArrayData::addElement(array, resources);
|
|
||||||
}
|
|
||||||
|
|
||||||
static VariantData* addElement(VariantData* var, ResourceManager* resources) {
|
|
||||||
if (!var)
|
|
||||||
return nullptr;
|
|
||||||
return var->addElement(resources);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
bool addValue(const T& value, ResourceManager* resources) {
|
bool addValue(const T& value) {
|
||||||
auto array = isNull() ? &toArray() : asArray();
|
auto array = isNull() ? toArray() : asArray();
|
||||||
return detail::ArrayData::addValue(array, value, resources);
|
return array.addValue(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
bool asBoolean() const {
|
||||||
static bool addValue(VariantData* var, const T& value,
|
if (!data_)
|
||||||
ResourceManager* resources) {
|
|
||||||
if (!var)
|
|
||||||
return false;
|
return false;
|
||||||
return var->addValue(value, resources);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool asBoolean(const ResourceManager* resources) const {
|
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||||
#if ARDUINOJSON_USE_EXTENSIONS
|
auto eightByteValue = getEightByte();
|
||||||
auto extension = getExtension(resources);
|
|
||||||
#else
|
|
||||||
(void)resources; // silence warning
|
|
||||||
#endif
|
#endif
|
||||||
switch (type_) {
|
switch (data_->type) {
|
||||||
case VariantType::Boolean:
|
case VariantType::Boolean:
|
||||||
return content_.asBoolean;
|
return data_->content.asBoolean;
|
||||||
case VariantType::Uint32:
|
case VariantType::Uint32:
|
||||||
case VariantType::Int32:
|
case VariantType::Int32:
|
||||||
return content_.asUint32 != 0;
|
return data_->content.asUint32 != 0;
|
||||||
case VariantType::Float:
|
case VariantType::Float:
|
||||||
return content_.asFloat != 0;
|
return data_->content.asFloat != 0;
|
||||||
#if ARDUINOJSON_USE_DOUBLE
|
#if ARDUINOJSON_USE_DOUBLE
|
||||||
case VariantType::Double:
|
case VariantType::Double:
|
||||||
return extension->asDouble != 0;
|
return eightByteValue->asDouble != 0;
|
||||||
#endif
|
#endif
|
||||||
case VariantType::Null:
|
case VariantType::Null:
|
||||||
return false;
|
return false;
|
||||||
#if ARDUINOJSON_USE_LONG_LONG
|
#if ARDUINOJSON_USE_LONG_LONG
|
||||||
case VariantType::Uint64:
|
case VariantType::Uint64:
|
||||||
case VariantType::Int64:
|
case VariantType::Int64:
|
||||||
return extension->asUint64 != 0;
|
return eightByteValue->asUint64 != 0;
|
||||||
#endif
|
#endif
|
||||||
default:
|
default:
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ArrayData* asArray() {
|
ArrayImpl asArray() {
|
||||||
return isArray() ? &content_.asArray : 0;
|
return ArrayImpl(isArray() ? &data_->content.asCollection : nullptr,
|
||||||
|
resources_);
|
||||||
}
|
}
|
||||||
|
|
||||||
const ArrayData* asArray() const {
|
CollectionImpl asCollection() {
|
||||||
return const_cast<VariantData*>(this)->asArray();
|
return CollectionImpl(
|
||||||
}
|
isCollection() ? &data_->content.asCollection : nullptr, resources_);
|
||||||
|
|
||||||
CollectionData* asCollection() {
|
|
||||||
return isCollection() ? &content_.asCollection : 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
const CollectionData* asCollection() const {
|
|
||||||
return const_cast<VariantData*>(this)->asCollection();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T asFloat(const ResourceManager* resources) const {
|
T asFloat() const {
|
||||||
|
if (!data_)
|
||||||
|
return 0.0;
|
||||||
|
|
||||||
static_assert(is_floating_point<T>::value, "T must be a floating point");
|
static_assert(is_floating_point<T>::value, "T must be a floating point");
|
||||||
#if ARDUINOJSON_USE_EXTENSIONS
|
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||||
auto extension = getExtension(resources);
|
auto eightByteValue = getEightByte();
|
||||||
#else
|
|
||||||
(void)resources; // silence warning
|
|
||||||
#endif
|
#endif
|
||||||
const char* str = nullptr;
|
const char* str = nullptr;
|
||||||
switch (type_) {
|
switch (data_->type) {
|
||||||
case VariantType::Boolean:
|
case VariantType::Boolean:
|
||||||
return static_cast<T>(content_.asBoolean);
|
return static_cast<T>(data_->content.asBoolean);
|
||||||
case VariantType::Uint32:
|
case VariantType::Uint32:
|
||||||
return static_cast<T>(content_.asUint32);
|
return static_cast<T>(data_->content.asUint32);
|
||||||
case VariantType::Int32:
|
case VariantType::Int32:
|
||||||
return static_cast<T>(content_.asInt32);
|
return static_cast<T>(data_->content.asInt32);
|
||||||
#if ARDUINOJSON_USE_LONG_LONG
|
#if ARDUINOJSON_USE_LONG_LONG
|
||||||
case VariantType::Uint64:
|
case VariantType::Uint64:
|
||||||
return static_cast<T>(extension->asUint64);
|
return static_cast<T>(eightByteValue->asUint64);
|
||||||
case VariantType::Int64:
|
case VariantType::Int64:
|
||||||
return static_cast<T>(extension->asInt64);
|
return static_cast<T>(eightByteValue->asInt64);
|
||||||
#endif
|
#endif
|
||||||
case VariantType::TinyString:
|
case VariantType::TinyString:
|
||||||
str = content_.asTinyString;
|
str = data_->content.asTinyString;
|
||||||
break;
|
break;
|
||||||
case VariantType::LinkedString:
|
case VariantType::LinkedString:
|
||||||
str = asLinkedString(resources);
|
str = asLinkedString();
|
||||||
break;
|
break;
|
||||||
case VariantType::OwnedString:
|
case VariantType::OwnedString:
|
||||||
str = content_.asOwnedString->data;
|
str = data_->content.asOwnedString->data;
|
||||||
break;
|
break;
|
||||||
case VariantType::Float:
|
case VariantType::Float:
|
||||||
return static_cast<T>(content_.asFloat);
|
return static_cast<T>(data_->content.asFloat);
|
||||||
#if ARDUINOJSON_USE_DOUBLE
|
#if ARDUINOJSON_USE_DOUBLE
|
||||||
case VariantType::Double:
|
case VariantType::Double:
|
||||||
return static_cast<T>(extension->asDouble);
|
return static_cast<T>(eightByteValue->asDouble);
|
||||||
#endif
|
#endif
|
||||||
default:
|
default:
|
||||||
return 0.0;
|
return 0.0;
|
||||||
@ -236,41 +259,42 @@ class VariantData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T asIntegral(const ResourceManager* resources) const {
|
T asIntegral() const {
|
||||||
|
if (!data_)
|
||||||
|
return 0;
|
||||||
|
|
||||||
static_assert(is_integral<T>::value, "T must be an integral type");
|
static_assert(is_integral<T>::value, "T must be an integral type");
|
||||||
#if ARDUINOJSON_USE_EXTENSIONS
|
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||||
auto extension = getExtension(resources);
|
auto eightByteValue = getEightByte();
|
||||||
#else
|
|
||||||
(void)resources; // silence warning
|
|
||||||
#endif
|
#endif
|
||||||
const char* str = nullptr;
|
const char* str = nullptr;
|
||||||
switch (type_) {
|
switch (data_->type) {
|
||||||
case VariantType::Boolean:
|
case VariantType::Boolean:
|
||||||
return content_.asBoolean;
|
return data_->content.asBoolean;
|
||||||
case VariantType::Uint32:
|
case VariantType::Uint32:
|
||||||
return convertNumber<T>(content_.asUint32);
|
return convertNumber<T>(data_->content.asUint32);
|
||||||
case VariantType::Int32:
|
case VariantType::Int32:
|
||||||
return convertNumber<T>(content_.asInt32);
|
return convertNumber<T>(data_->content.asInt32);
|
||||||
#if ARDUINOJSON_USE_LONG_LONG
|
#if ARDUINOJSON_USE_LONG_LONG
|
||||||
case VariantType::Uint64:
|
case VariantType::Uint64:
|
||||||
return convertNumber<T>(extension->asUint64);
|
return convertNumber<T>(eightByteValue->asUint64);
|
||||||
case VariantType::Int64:
|
case VariantType::Int64:
|
||||||
return convertNumber<T>(extension->asInt64);
|
return convertNumber<T>(eightByteValue->asInt64);
|
||||||
#endif
|
#endif
|
||||||
case VariantType::TinyString:
|
case VariantType::TinyString:
|
||||||
str = content_.asTinyString;
|
str = data_->content.asTinyString;
|
||||||
break;
|
break;
|
||||||
case VariantType::LinkedString:
|
case VariantType::LinkedString:
|
||||||
str = asLinkedString(resources);
|
str = asLinkedString();
|
||||||
break;
|
break;
|
||||||
case VariantType::OwnedString:
|
case VariantType::OwnedString:
|
||||||
str = content_.asOwnedString->data;
|
str = data_->content.asOwnedString->data;
|
||||||
break;
|
break;
|
||||||
case VariantType::Float:
|
case VariantType::Float:
|
||||||
return convertNumber<T>(content_.asFloat);
|
return convertNumber<T>(data_->content.asFloat);
|
||||||
#if ARDUINOJSON_USE_DOUBLE
|
#if ARDUINOJSON_USE_DOUBLE
|
||||||
case VariantType::Double:
|
case VariantType::Double:
|
||||||
return convertNumber<T>(extension->asDouble);
|
return convertNumber<T>(eightByteValue->asDouble);
|
||||||
#endif
|
#endif
|
||||||
default:
|
default:
|
||||||
return 0;
|
return 0;
|
||||||
@ -280,121 +304,100 @@ class VariantData {
|
|||||||
return parseNumber<T>(str);
|
return parseNumber<T>(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
ObjectData* asObject() {
|
ObjectImpl asObject() {
|
||||||
return isObject() ? &content_.asObject : 0;
|
return ObjectImpl(isObject() ? &data_->content.asCollection : nullptr,
|
||||||
}
|
resources_);
|
||||||
|
|
||||||
const ObjectData* asObject() const {
|
|
||||||
return const_cast<VariantData*>(this)->asObject();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonString asRawString() const {
|
JsonString asRawString() const {
|
||||||
switch (type_) {
|
switch (type()) {
|
||||||
case VariantType::RawString:
|
case VariantType::RawString:
|
||||||
return JsonString(content_.asOwnedString->data,
|
return JsonString(data_->content.asOwnedString->data,
|
||||||
content_.asOwnedString->length);
|
data_->content.asOwnedString->length);
|
||||||
default:
|
default:
|
||||||
return JsonString();
|
return JsonString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* asLinkedString(const ResourceManager* resources) const;
|
const char* asLinkedString() const;
|
||||||
|
|
||||||
JsonString asString(const ResourceManager* resources) const {
|
JsonString asString() const {
|
||||||
switch (type_) {
|
switch (type()) {
|
||||||
case VariantType::TinyString:
|
case VariantType::TinyString:
|
||||||
return JsonString(content_.asTinyString);
|
return JsonString(data_->content.asTinyString);
|
||||||
case VariantType::LinkedString:
|
case VariantType::LinkedString:
|
||||||
return JsonString(asLinkedString(resources), true);
|
return JsonString(asLinkedString(), true);
|
||||||
case VariantType::OwnedString:
|
case VariantType::OwnedString:
|
||||||
return JsonString(content_.asOwnedString->data,
|
return JsonString(data_->content.asOwnedString->data,
|
||||||
content_.asOwnedString->length);
|
data_->content.asOwnedString->length);
|
||||||
default:
|
default:
|
||||||
return JsonString();
|
return JsonString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#if ARDUINOJSON_USE_EXTENSIONS
|
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||||
const VariantExtension* getExtension(const ResourceManager* resources) const;
|
const EightByteValue* getEightByte() const;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
VariantData* getElement(size_t index,
|
VariantData* getElement(size_t index) {
|
||||||
const ResourceManager* resources) const {
|
return asArray().getElement(index);
|
||||||
return ArrayData::getElement(asArray(), index, resources);
|
|
||||||
}
|
|
||||||
|
|
||||||
static VariantData* getElement(const VariantData* var, size_t index,
|
|
||||||
const ResourceManager* resources) {
|
|
||||||
return var != 0 ? var->getElement(index, resources) : 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TAdaptedString>
|
template <typename TAdaptedString>
|
||||||
VariantData* getMember(TAdaptedString key,
|
VariantData* getMember(TAdaptedString key) {
|
||||||
const ResourceManager* resources) const {
|
return asObject().getMember(key);
|
||||||
return ObjectData::getMember(asObject(), key, resources);
|
}
|
||||||
|
|
||||||
|
VariantData* getOrAddElement(size_t index) {
|
||||||
|
auto array = isNull() ? toArray() : asArray();
|
||||||
|
return array.getOrAddElement(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TAdaptedString>
|
template <typename TAdaptedString>
|
||||||
static VariantData* getMember(const VariantData* var, TAdaptedString key,
|
VariantData* getOrAddMember(TAdaptedString key) {
|
||||||
const ResourceManager* resources) {
|
|
||||||
if (!var)
|
|
||||||
return 0;
|
|
||||||
return var->getMember(key, resources);
|
|
||||||
}
|
|
||||||
|
|
||||||
VariantData* getOrAddElement(size_t index, ResourceManager* resources) {
|
|
||||||
auto array = isNull() ? &toArray() : asArray();
|
|
||||||
if (!array)
|
|
||||||
return nullptr;
|
|
||||||
return array->getOrAddElement(index, resources);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename TAdaptedString>
|
|
||||||
VariantData* getOrAddMember(TAdaptedString key, ResourceManager* resources) {
|
|
||||||
if (key.isNull())
|
if (key.isNull())
|
||||||
return nullptr;
|
return nullptr;
|
||||||
auto obj = isNull() ? &toObject() : asObject();
|
auto obj = isNull() ? toObject() : asObject();
|
||||||
if (!obj)
|
return obj.getOrAddMember(key);
|
||||||
return nullptr;
|
|
||||||
return obj->getOrAddMember(key, resources);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isArray() const {
|
bool isArray() const {
|
||||||
return type_ == VariantType::Array;
|
return type() == VariantType::Array;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isBoolean() const {
|
bool isBoolean() const {
|
||||||
return type_ == VariantType::Boolean;
|
return type() == VariantType::Boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isCollection() const {
|
bool isCollection() const {
|
||||||
return type_ & VariantTypeBits::CollectionMask;
|
return type() & VariantTypeBits::CollectionMask;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isFloat() const {
|
bool isFloat() const {
|
||||||
return type_ & VariantTypeBits::NumberBit;
|
return data_ && data_->isFloat();
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
bool isInteger(const ResourceManager* resources) const {
|
bool isInteger() const {
|
||||||
|
if (!data_)
|
||||||
|
return false;
|
||||||
|
|
||||||
#if ARDUINOJSON_USE_LONG_LONG
|
#if ARDUINOJSON_USE_LONG_LONG
|
||||||
auto extension = getExtension(resources);
|
auto eightByteValue = getEightByte();
|
||||||
#else
|
|
||||||
(void)resources; // silence warning
|
|
||||||
#endif
|
#endif
|
||||||
switch (type_) {
|
switch (data_->type) {
|
||||||
case VariantType::Uint32:
|
case VariantType::Uint32:
|
||||||
return canConvertNumber<T>(content_.asUint32);
|
return canConvertNumber<T>(data_->content.asUint32);
|
||||||
|
|
||||||
case VariantType::Int32:
|
case VariantType::Int32:
|
||||||
return canConvertNumber<T>(content_.asInt32);
|
return canConvertNumber<T>(data_->content.asInt32);
|
||||||
|
|
||||||
#if ARDUINOJSON_USE_LONG_LONG
|
#if ARDUINOJSON_USE_LONG_LONG
|
||||||
case VariantType::Uint64:
|
case VariantType::Uint64:
|
||||||
return canConvertNumber<T>(extension->asUint64);
|
return canConvertNumber<T>(eightByteValue->asUint64);
|
||||||
|
|
||||||
case VariantType::Int64:
|
case VariantType::Int64:
|
||||||
return canConvertNumber<T>(extension->asInt64);
|
return canConvertNumber<T>(eightByteValue->asInt64);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@ -403,204 +406,102 @@ class VariantData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool isNull() const {
|
bool isNull() const {
|
||||||
return type_ == VariantType::Null;
|
return type() == VariantType::Null;
|
||||||
}
|
|
||||||
|
|
||||||
static bool isNull(const VariantData* var) {
|
|
||||||
if (!var)
|
|
||||||
return true;
|
|
||||||
return var->isNull();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isObject() const {
|
bool isObject() const {
|
||||||
return type_ == VariantType::Object;
|
return type() == VariantType::Object;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isString() const {
|
bool isString() const {
|
||||||
return type_ == VariantType::LinkedString ||
|
return data_ && data_->isString();
|
||||||
type_ == VariantType::OwnedString ||
|
|
||||||
type_ == VariantType::TinyString;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t nesting(const ResourceManager* resources) const {
|
size_t nesting() {
|
||||||
auto collection = asCollection();
|
return asCollection().nesting();
|
||||||
if (collection)
|
|
||||||
return collection->nesting(resources);
|
|
||||||
else
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static size_t nesting(const VariantData* var,
|
void removeElement(size_t index) {
|
||||||
const ResourceManager* resources) {
|
asArray().removeElement(index);
|
||||||
if (!var)
|
|
||||||
return 0;
|
|
||||||
return var->nesting(resources);
|
|
||||||
}
|
|
||||||
|
|
||||||
void removeElement(size_t index, ResourceManager* resources) {
|
|
||||||
ArrayData::removeElement(asArray(), index, resources);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void removeElement(VariantData* var, size_t index,
|
|
||||||
ResourceManager* resources) {
|
|
||||||
if (!var)
|
|
||||||
return;
|
|
||||||
var->removeElement(index, resources);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TAdaptedString>
|
template <typename TAdaptedString>
|
||||||
void removeMember(TAdaptedString key, ResourceManager* resources) {
|
void removeMember(TAdaptedString key) {
|
||||||
ObjectData::removeMember(asObject(), key, resources);
|
asObject().removeMember(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TAdaptedString>
|
bool setBoolean(bool value) {
|
||||||
static void removeMember(VariantData* var, TAdaptedString key,
|
if (!data_)
|
||||||
ResourceManager* resources) {
|
return false;
|
||||||
if (!var)
|
data_->setBoolean(value);
|
||||||
return;
|
|
||||||
var->removeMember(key, resources);
|
|
||||||
}
|
|
||||||
|
|
||||||
void reset() { // TODO: remove
|
|
||||||
type_ = VariantType::Null;
|
|
||||||
}
|
|
||||||
|
|
||||||
void setBoolean(bool value) {
|
|
||||||
ARDUINOJSON_ASSERT(type_ == VariantType::Null); // must call clear() first
|
|
||||||
type_ = VariantType::Boolean;
|
|
||||||
content_.asBoolean = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
enable_if_t<sizeof(T) == 4, bool> setFloat(T value, ResourceManager*) {
|
|
||||||
ARDUINOJSON_ASSERT(type_ == VariantType::Null); // must call clear() first
|
|
||||||
type_ = VariantType::Float;
|
|
||||||
content_.asFloat = value;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
enable_if_t<sizeof(T) == 8, bool> setFloat(T value, ResourceManager*);
|
enable_if_t<sizeof(T) == 4, bool> setFloat(T value) {
|
||||||
|
ARDUINOJSON_ASSERT(type() == VariantType::Null); // must call clear() first
|
||||||
template <typename T>
|
if (!data_)
|
||||||
enable_if_t<is_signed<T>::value, bool> setInteger(T value,
|
return false;
|
||||||
ResourceManager* resources);
|
data_->type = VariantType::Float;
|
||||||
|
data_->content.asFloat = value;
|
||||||
template <typename T>
|
return true;
|
||||||
enable_if_t<is_unsigned<T>::value, bool> setInteger(
|
|
||||||
T value, ResourceManager* resources);
|
|
||||||
|
|
||||||
void setRawString(StringNode* s) {
|
|
||||||
ARDUINOJSON_ASSERT(type_ == VariantType::Null); // must call clear() first
|
|
||||||
ARDUINOJSON_ASSERT(s);
|
|
||||||
type_ = VariantType::RawString;
|
|
||||||
content_.asOwnedString = s;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void setRawString(SerializedValue<T> value, ResourceManager* resources);
|
enable_if_t<sizeof(T) == 8, bool> setFloat(T value);
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
static void setRawString(VariantData* var, SerializedValue<T> value,
|
enable_if_t<is_signed<T>::value, bool> setInteger(T value);
|
||||||
ResourceManager* resources) {
|
|
||||||
if (!var)
|
template <typename T>
|
||||||
return;
|
enable_if_t<is_unsigned<T>::value, bool> setInteger(T value);
|
||||||
var->clear(resources);
|
|
||||||
var->setRawString(value, resources);
|
template <typename T>
|
||||||
}
|
void setRawString(SerializedValue<T> value);
|
||||||
|
|
||||||
template <typename TAdaptedString>
|
template <typename TAdaptedString>
|
||||||
bool setString(TAdaptedString value, ResourceManager* resources);
|
bool setString(TAdaptedString value);
|
||||||
|
|
||||||
template <typename TAdaptedString>
|
bool setLinkedString(const char* s);
|
||||||
static void setString(VariantData* var, TAdaptedString value,
|
|
||||||
ResourceManager* resources) {
|
|
||||||
if (!var)
|
|
||||||
return;
|
|
||||||
var->clear(resources);
|
|
||||||
var->setString(value, resources);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool setLinkedString(const char* s, ResourceManager* resources);
|
size_t size() {
|
||||||
|
|
||||||
template <typename TAdaptedString>
|
|
||||||
void setTinyString(const TAdaptedString& s) {
|
|
||||||
ARDUINOJSON_ASSERT(type_ == VariantType::Null); // must call clear() first
|
|
||||||
ARDUINOJSON_ASSERT(s.size() <= tinyStringMaxLength);
|
|
||||||
|
|
||||||
type_ = VariantType::TinyString;
|
|
||||||
|
|
||||||
auto n = uint8_t(s.size());
|
|
||||||
for (uint8_t i = 0; i < n; i++) {
|
|
||||||
char c = s[i];
|
|
||||||
ARDUINOJSON_ASSERT(c != 0); // no NUL in tiny string
|
|
||||||
content_.asTinyString[i] = c;
|
|
||||||
}
|
|
||||||
|
|
||||||
content_.asTinyString[n] = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void setOwnedString(StringNode* s) {
|
|
||||||
ARDUINOJSON_ASSERT(type_ == VariantType::Null); // must call clear() first
|
|
||||||
ARDUINOJSON_ASSERT(s);
|
|
||||||
type_ = VariantType::OwnedString;
|
|
||||||
content_.asOwnedString = s;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t size(const ResourceManager* resources) const {
|
|
||||||
if (isObject())
|
if (isObject())
|
||||||
return content_.asObject.size(resources);
|
return asObject().size();
|
||||||
|
|
||||||
if (isArray())
|
if (isArray())
|
||||||
return content_.asArray.size(resources);
|
return asArray().size();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static size_t size(const VariantData* var, const ResourceManager* resources) {
|
ArrayImpl toArray() {
|
||||||
return var != 0 ? var->size(resources) : 0;
|
ARDUINOJSON_ASSERT(type() == VariantType::Null); // must call clear() first
|
||||||
|
if (!data_)
|
||||||
|
return ArrayImpl();
|
||||||
|
data_->type = VariantType::Array;
|
||||||
|
return ArrayImpl(new (&data_->content.asCollection) CollectionData(),
|
||||||
|
resources_);
|
||||||
}
|
}
|
||||||
|
|
||||||
ArrayData& toArray() {
|
ObjectImpl toObject() {
|
||||||
ARDUINOJSON_ASSERT(type_ == VariantType::Null); // must call clear() first
|
ARDUINOJSON_ASSERT(type() == VariantType::Null); // must call clear() first
|
||||||
type_ = VariantType::Array;
|
if (!data_)
|
||||||
new (&content_.asArray) ArrayData();
|
return ObjectImpl();
|
||||||
return content_.asArray;
|
data_->type = VariantType::Object;
|
||||||
}
|
return ObjectImpl(new (&data_->content.asCollection) CollectionData(),
|
||||||
|
resources_);
|
||||||
static ArrayData* toArray(VariantData* var, ResourceManager* resources) {
|
|
||||||
if (!var)
|
|
||||||
return 0;
|
|
||||||
var->clear(resources);
|
|
||||||
return &var->toArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
ObjectData& toObject() {
|
|
||||||
ARDUINOJSON_ASSERT(type_ == VariantType::Null); // must call clear() first
|
|
||||||
type_ = VariantType::Object;
|
|
||||||
new (&content_.asObject) ObjectData();
|
|
||||||
return content_.asObject;
|
|
||||||
}
|
|
||||||
|
|
||||||
static ObjectData* toObject(VariantData* var, ResourceManager* resources) {
|
|
||||||
if (!var)
|
|
||||||
return 0;
|
|
||||||
var->clear(resources);
|
|
||||||
return &var->toObject();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
VariantType type() const {
|
VariantType type() const {
|
||||||
return type_;
|
return data_ ? data_->type : VariantType::Null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Release the resources used by this variant and set it to null.
|
// Release the resources used by this variant and set it to null.
|
||||||
void clear(ResourceManager* resources);
|
void clear();
|
||||||
|
|
||||||
static void clear(VariantData* var, ResourceManager* resources) {
|
private:
|
||||||
if (!var)
|
VariantData* data_;
|
||||||
return;
|
ResourceManager* resources_;
|
||||||
var->clear(resources);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -10,152 +10,155 @@
|
|||||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline void VariantData::setRawString(SerializedValue<T> value,
|
inline void VariantImpl::setRawString(SerializedValue<T> value) {
|
||||||
ResourceManager* resources) {
|
if (!data_)
|
||||||
ARDUINOJSON_ASSERT(type_ == VariantType::Null); // must call clear() first
|
return;
|
||||||
auto dup = resources->saveString(adaptString(value.data(), value.size()));
|
auto dup = resources_->saveString(adaptString(value.data(), value.size()));
|
||||||
if (dup)
|
if (dup)
|
||||||
setRawString(dup);
|
data_->setRawString(dup);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool VariantData::setLinkedString(const char* s,
|
inline bool VariantImpl::setLinkedString(const char* s) {
|
||||||
ResourceManager* resources) {
|
ARDUINOJSON_ASSERT(isNull()); // must call clear() first
|
||||||
ARDUINOJSON_ASSERT(type_ == VariantType::Null); // must call clear() first
|
|
||||||
ARDUINOJSON_ASSERT(s);
|
ARDUINOJSON_ASSERT(s);
|
||||||
|
|
||||||
auto slotId = resources->saveStaticString(s);
|
auto slotId = resources_->saveStaticString(s);
|
||||||
if (slotId == NULL_SLOT)
|
if (slotId == NULL_SLOT)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
type_ = VariantType::LinkedString;
|
data_->type = VariantType::LinkedString;
|
||||||
content_.asSlotId = slotId;
|
data_->content.asSlotId = slotId;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TAdaptedString>
|
template <typename TAdaptedString>
|
||||||
inline bool VariantData::setString(TAdaptedString value,
|
inline bool VariantImpl::setString(TAdaptedString value) {
|
||||||
ResourceManager* resources) {
|
ARDUINOJSON_ASSERT(isNull()); // must call clear() first
|
||||||
ARDUINOJSON_ASSERT(type_ == VariantType::Null); // must call clear() first
|
|
||||||
|
if (!data_)
|
||||||
|
return false;
|
||||||
|
|
||||||
if (value.isNull())
|
if (value.isNull())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (value.isStatic())
|
if (value.isStatic())
|
||||||
return setLinkedString(value.data(), resources);
|
return setLinkedString(value.data());
|
||||||
|
|
||||||
if (isTinyString(value, value.size())) {
|
if (isTinyString(value, value.size())) {
|
||||||
setTinyString(value);
|
data_->setTinyString(value);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto dup = resources->saveString(value);
|
auto dup = resources_->saveString(value);
|
||||||
if (dup) {
|
if (dup) {
|
||||||
setOwnedString(dup);
|
data_->setOwnedString(dup);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void VariantData::clear(ResourceManager* resources) {
|
inline void VariantImpl::clear() {
|
||||||
if (type_ & VariantTypeBits::OwnedStringBit)
|
if (!data_)
|
||||||
resources->dereferenceString(content_.asOwnedString->data);
|
return;
|
||||||
|
|
||||||
#if ARDUINOJSON_USE_EXTENSIONS
|
if (data_->type & VariantTypeBits::OwnedStringBit)
|
||||||
if (type_ & VariantTypeBits::ExtensionBit)
|
resources_->dereferenceString(data_->content.asOwnedString->data);
|
||||||
resources->freeExtension(content_.asSlotId);
|
|
||||||
|
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||||
|
if (data_->type & VariantTypeBits::EightByteBit)
|
||||||
|
resources_->freeEightByte(data_->content.asSlotId);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
auto collection = asCollection();
|
asCollection().clear();
|
||||||
if (collection)
|
|
||||||
collection->clear(resources);
|
|
||||||
|
|
||||||
type_ = VariantType::Null;
|
data_->type = VariantType::Null;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if ARDUINOJSON_USE_EXTENSIONS
|
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||||
inline const VariantExtension* VariantData::getExtension(
|
inline const EightByteValue* VariantImpl::getEightByte() const {
|
||||||
const ResourceManager* resources) const {
|
return type() & VariantTypeBits::EightByteBit
|
||||||
return type_ & VariantTypeBits::ExtensionBit
|
? resources_->getEightByte(data_->content.asSlotId)
|
||||||
? resources->getExtension(content_.asSlotId)
|
: 0;
|
||||||
: nullptr;
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
inline const char* VariantData::asLinkedString(
|
inline const char* VariantImpl::asLinkedString() const {
|
||||||
const ResourceManager* resources) const {
|
ARDUINOJSON_ASSERT(type() == VariantType::LinkedString);
|
||||||
ARDUINOJSON_ASSERT(type_ == VariantType::LinkedString);
|
return resources_->getStaticString(data_->content.asSlotId);
|
||||||
return resources->getStaticString(content_.asSlotId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
enable_if_t<sizeof(T) == 8, bool> VariantData::setFloat(
|
enable_if_t<sizeof(T) == 8, bool> VariantImpl::setFloat(T value) {
|
||||||
T value, ResourceManager* resources) {
|
ARDUINOJSON_ASSERT(isNull()); // must call clear() first
|
||||||
ARDUINOJSON_ASSERT(type_ == VariantType::Null); // must call clear() first
|
|
||||||
(void)resources; // silence warning
|
if (!data_)
|
||||||
|
return false;
|
||||||
|
|
||||||
float valueAsFloat = static_cast<float>(value);
|
float valueAsFloat = static_cast<float>(value);
|
||||||
|
|
||||||
#if ARDUINOJSON_USE_DOUBLE
|
#if ARDUINOJSON_USE_DOUBLE
|
||||||
if (value == valueAsFloat) {
|
if (value == valueAsFloat) {
|
||||||
type_ = VariantType::Float;
|
data_->type = VariantType::Float;
|
||||||
content_.asFloat = valueAsFloat;
|
data_->content.asFloat = valueAsFloat;
|
||||||
} else {
|
} else {
|
||||||
auto extension = resources->allocExtension();
|
auto slot = resources_->allocEightByte();
|
||||||
if (!extension)
|
if (!slot)
|
||||||
return false;
|
return false;
|
||||||
type_ = VariantType::Double;
|
data_->type = VariantType::Double;
|
||||||
content_.asSlotId = extension.id();
|
data_->content.asSlotId = slot.id();
|
||||||
extension->asDouble = value;
|
slot->asDouble = value;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
type_ = VariantType::Float;
|
data_->type = VariantType::Float;
|
||||||
content_.asFloat = valueAsFloat;
|
data_->content.asFloat = valueAsFloat;
|
||||||
#endif
|
#endif
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
enable_if_t<is_signed<T>::value, bool> VariantData::setInteger(
|
enable_if_t<is_signed<T>::value, bool> VariantImpl::setInteger(T value) {
|
||||||
T value, ResourceManager* resources) {
|
ARDUINOJSON_ASSERT(isNull()); // must call clear() first
|
||||||
ARDUINOJSON_ASSERT(type_ == VariantType::Null); // must call clear() first
|
|
||||||
(void)resources; // silence warning
|
if (!data_)
|
||||||
|
return false;
|
||||||
|
|
||||||
if (canConvertNumber<int32_t>(value)) {
|
if (canConvertNumber<int32_t>(value)) {
|
||||||
type_ = VariantType::Int32;
|
data_->type = VariantType::Int32;
|
||||||
content_.asInt32 = static_cast<int32_t>(value);
|
data_->content.asInt32 = static_cast<int32_t>(value);
|
||||||
}
|
}
|
||||||
#if ARDUINOJSON_USE_LONG_LONG
|
#if ARDUINOJSON_USE_LONG_LONG
|
||||||
else {
|
else {
|
||||||
auto extension = resources->allocExtension();
|
auto slot = resources_->allocEightByte();
|
||||||
if (!extension)
|
if (!slot)
|
||||||
return false;
|
return false;
|
||||||
type_ = VariantType::Int64;
|
data_->type = VariantType::Int64;
|
||||||
content_.asSlotId = extension.id();
|
data_->content.asSlotId = slot.id();
|
||||||
extension->asInt64 = value;
|
slot->asInt64 = value;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
enable_if_t<is_unsigned<T>::value, bool> VariantData::setInteger(
|
enable_if_t<is_unsigned<T>::value, bool> VariantImpl::setInteger(T value) {
|
||||||
T value, ResourceManager* resources) {
|
ARDUINOJSON_ASSERT(isNull()); // must call clear() first
|
||||||
ARDUINOJSON_ASSERT(type_ == VariantType::Null); // must call clear() first
|
|
||||||
(void)resources; // silence warning
|
if (!data_)
|
||||||
|
return false;
|
||||||
|
|
||||||
if (canConvertNumber<uint32_t>(value)) {
|
if (canConvertNumber<uint32_t>(value)) {
|
||||||
type_ = VariantType::Uint32;
|
data_->type = VariantType::Uint32;
|
||||||
content_.asUint32 = static_cast<uint32_t>(value);
|
data_->content.asUint32 = static_cast<uint32_t>(value);
|
||||||
}
|
}
|
||||||
#if ARDUINOJSON_USE_LONG_LONG
|
#if ARDUINOJSON_USE_LONG_LONG
|
||||||
else {
|
else {
|
||||||
auto extension = resources->allocExtension();
|
auto slot = resources_->allocEightByte();
|
||||||
if (!extension)
|
if (!slot)
|
||||||
return false;
|
return false;
|
||||||
type_ = VariantType::Uint64;
|
data_->type = VariantType::Uint64;
|
||||||
content_.asSlotId = extension.id();
|
data_->content.asSlotId = slot.id();
|
||||||
extension->asUint64 = value;
|
slot->asUint64 = value;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
return true;
|
return true;
|
||||||
|
@ -29,13 +29,13 @@ class VariantRefBase : public VariantTag {
|
|||||||
// Sets the value to null.
|
// Sets the value to null.
|
||||||
// https://arduinojson.org/v7/api/jsonvariant/clear/
|
// https://arduinojson.org/v7/api/jsonvariant/clear/
|
||||||
void clear() const {
|
void clear() const {
|
||||||
VariantData::clear(getOrCreateData(), getResourceManager());
|
getOrCreateVariantImpl().clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if the value is null or the reference is unbound.
|
// Returns true if the value is null or the reference is unbound.
|
||||||
// https://arduinojson.org/v7/api/jsonvariant/isnull/
|
// https://arduinojson.org/v7/api/jsonvariant/isnull/
|
||||||
bool isNull() const {
|
bool isNull() const {
|
||||||
return VariantData::isNull(getData());
|
return getVariantImpl().isNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if the reference is unbound.
|
// Returns true if the reference is unbound.
|
||||||
@ -93,13 +93,13 @@ class VariantRefBase : public VariantTag {
|
|||||||
// Returns the size of the array or object.
|
// Returns the size of the array or object.
|
||||||
// https://arduinojson.org/v7/api/jsonvariant/size/
|
// https://arduinojson.org/v7/api/jsonvariant/size/
|
||||||
size_t size() const {
|
size_t size() const {
|
||||||
return VariantData::size(getData(), getResourceManager());
|
return getVariantImpl().size();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the depth (nesting level) of the value.
|
// Returns the depth (nesting level) of the value.
|
||||||
// https://arduinojson.org/v7/api/jsonvariant/nesting/
|
// https://arduinojson.org/v7/api/jsonvariant/nesting/
|
||||||
size_t nesting() const {
|
size_t nesting() const {
|
||||||
return VariantData::nesting(getData(), getResourceManager());
|
return getVariantImpl().nesting();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Appends a new (empty) element to the array.
|
// Appends a new (empty) element to the array.
|
||||||
@ -120,38 +120,34 @@ class VariantRefBase : public VariantTag {
|
|||||||
// https://arduinojson.org/v7/api/jsonvariant/add/
|
// https://arduinojson.org/v7/api/jsonvariant/add/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
bool add(const T& value) const {
|
bool add(const T& value) const {
|
||||||
return detail::VariantData::addValue(getOrCreateData(), value,
|
return getOrCreateVariantImpl().addValue(value);
|
||||||
getResourceManager());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Appends a value to the array.
|
// Appends a value to the array.
|
||||||
// https://arduinojson.org/v7/api/jsonvariant/add/
|
// https://arduinojson.org/v7/api/jsonvariant/add/
|
||||||
template <typename T, enable_if_t<!is_const<T>::value, int> = 0>
|
template <typename T, enable_if_t<!is_const<T>::value, int> = 0>
|
||||||
bool add(T* value) const {
|
bool add(T* value) const {
|
||||||
return detail::VariantData::addValue(getOrCreateData(), value,
|
return getOrCreateVariantImpl().addValue(value);
|
||||||
getResourceManager());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Removes an element of the array.
|
// Removes an element of the array.
|
||||||
// https://arduinojson.org/v7/api/jsonvariant/remove/
|
// https://arduinojson.org/v7/api/jsonvariant/remove/
|
||||||
void remove(size_t index) const {
|
void remove(size_t index) const {
|
||||||
VariantData::removeElement(getData(), index, getResourceManager());
|
getVariantImpl().removeElement(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Removes a member of the object.
|
// Removes a member of the object.
|
||||||
// https://arduinojson.org/v7/api/jsonvariant/remove/
|
// https://arduinojson.org/v7/api/jsonvariant/remove/
|
||||||
template <typename TChar, enable_if_t<IsString<TChar*>::value, int> = 0>
|
template <typename TChar, enable_if_t<IsString<TChar*>::value, int> = 0>
|
||||||
void remove(TChar* key) const {
|
void remove(TChar* key) const {
|
||||||
VariantData::removeMember(getData(), adaptString(key),
|
getVariantImpl().removeMember(adaptString(key));
|
||||||
getResourceManager());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Removes a member of the object.
|
// Removes a member of the object.
|
||||||
// https://arduinojson.org/v7/api/jsonvariant/remove/
|
// https://arduinojson.org/v7/api/jsonvariant/remove/
|
||||||
template <typename TString, enable_if_t<IsString<TString>::value, int> = 0>
|
template <typename TString, enable_if_t<IsString<TString>::value, int> = 0>
|
||||||
void remove(const TString& key) const {
|
void remove(const TString& key) const {
|
||||||
VariantData::removeMember(getData(), adaptString(key),
|
getVariantImpl().removeMember(adaptString(key));
|
||||||
getResourceManager());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Removes a member of the object or an element of the array.
|
// Removes a member of the object or an element of the array.
|
||||||
@ -275,6 +271,14 @@ class VariantRefBase : public VariantTag {
|
|||||||
return VariantAttorney::getOrCreateData(derived());
|
return VariantAttorney::getOrCreateData(derived());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
VariantImpl getVariantImpl() const {
|
||||||
|
return VariantImpl(getData(), getResourceManager());
|
||||||
|
}
|
||||||
|
|
||||||
|
VariantImpl getOrCreateVariantImpl() const {
|
||||||
|
return VariantImpl(getOrCreateData(), getResourceManager());
|
||||||
|
}
|
||||||
|
|
||||||
FORCE_INLINE ArduinoJson::JsonVariant getVariant() const;
|
FORCE_INLINE ArduinoJson::JsonVariant getVariant() const;
|
||||||
|
|
||||||
FORCE_INLINE ArduinoJson::JsonVariantConst getVariantConst() const {
|
FORCE_INLINE ArduinoJson::JsonVariantConst getVariantConst() const {
|
||||||
|
@ -69,23 +69,22 @@ inline void convertToJson(const VariantRefBase<TDerived>& src,
|
|||||||
template <typename TDerived>
|
template <typename TDerived>
|
||||||
template <typename T, enable_if_t<is_same<T, JsonVariant>::value, int>>
|
template <typename T, enable_if_t<is_same<T, JsonVariant>::value, int>>
|
||||||
inline T VariantRefBase<TDerived>::add() const {
|
inline T VariantRefBase<TDerived>::add() const {
|
||||||
return JsonVariant(
|
detail::VariantImpl variant(getOrCreateData(), getResourceManager());
|
||||||
detail::VariantData::addElement(getOrCreateData(), getResourceManager()),
|
return JsonVariant(variant.addElement(), getResourceManager());
|
||||||
getResourceManager());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TDerived>
|
template <typename TDerived>
|
||||||
template <typename TString, enable_if_t<IsString<TString>::value, int>>
|
template <typename TString, enable_if_t<IsString<TString>::value, int>>
|
||||||
inline bool VariantRefBase<TDerived>::containsKey(const TString& key) const {
|
inline bool VariantRefBase<TDerived>::containsKey(const TString& key) const {
|
||||||
return VariantData::getMember(getData(), adaptString(key),
|
detail::VariantImpl variant(getData(), getResourceManager());
|
||||||
getResourceManager()) != 0;
|
return variant.getMember(adaptString(key)) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TDerived>
|
template <typename TDerived>
|
||||||
template <typename TChar, enable_if_t<IsString<TChar*>::value, int>>
|
template <typename TChar, enable_if_t<IsString<TChar*>::value, int>>
|
||||||
inline bool VariantRefBase<TDerived>::containsKey(TChar* key) const {
|
inline bool VariantRefBase<TDerived>::containsKey(TChar* key) const {
|
||||||
return VariantData::getMember(getData(), adaptString(key),
|
detail::VariantImpl variant(getData(), getResourceManager());
|
||||||
getResourceManager()) != 0;
|
return variant.getMember(adaptString(key)) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TDerived>
|
template <typename TDerived>
|
||||||
@ -150,26 +149,25 @@ inline bool VariantRefBase<TDerived>::doSet(const T& value, true_type) const {
|
|||||||
template <typename TDerived>
|
template <typename TDerived>
|
||||||
template <typename T, enable_if_t<is_same<T, JsonArray>::value, int>>
|
template <typename T, enable_if_t<is_same<T, JsonArray>::value, int>>
|
||||||
inline JsonArray VariantRefBase<TDerived>::to() const {
|
inline JsonArray VariantRefBase<TDerived>::to() const {
|
||||||
return JsonArray(
|
auto variant = getOrCreateVariantImpl();
|
||||||
VariantData::toArray(getOrCreateData(), getResourceManager()),
|
variant.clear();
|
||||||
getResourceManager());
|
return JsonArray(variant.toArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TDerived>
|
template <typename TDerived>
|
||||||
template <typename T, enable_if_t<is_same<T, JsonObject>::value, int>>
|
template <typename T, enable_if_t<is_same<T, JsonObject>::value, int>>
|
||||||
JsonObject VariantRefBase<TDerived>::to() const {
|
JsonObject VariantRefBase<TDerived>::to() const {
|
||||||
return JsonObject(
|
auto variant = getOrCreateVariantImpl();
|
||||||
VariantData::toObject(getOrCreateData(), getResourceManager()),
|
variant.clear();
|
||||||
getResourceManager());
|
return JsonObject(variant.toObject());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TDerived>
|
template <typename TDerived>
|
||||||
template <typename T, enable_if_t<is_same<T, JsonVariant>::value, int>>
|
template <typename T, enable_if_t<is_same<T, JsonVariant>::value, int>>
|
||||||
JsonVariant VariantRefBase<TDerived>::to() const {
|
JsonVariant VariantRefBase<TDerived>::to() const {
|
||||||
auto data = getOrCreateData();
|
detail::VariantImpl impl(getOrCreateData(), getResourceManager());
|
||||||
auto resources = getResourceManager();
|
impl.clear();
|
||||||
detail::VariantData::clear(data, resources);
|
return JsonVariant(impl);
|
||||||
return JsonVariant(data, resources);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -4,8 +4,8 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#define ARDUINOJSON_VERSION "7.4.1"
|
#define ARDUINOJSON_VERSION "7.4.2"
|
||||||
#define ARDUINOJSON_VERSION_MAJOR 7
|
#define ARDUINOJSON_VERSION_MAJOR 7
|
||||||
#define ARDUINOJSON_VERSION_MINOR 4
|
#define ARDUINOJSON_VERSION_MINOR 4
|
||||||
#define ARDUINOJSON_VERSION_REVISION 1
|
#define ARDUINOJSON_VERSION_REVISION 2
|
||||||
#define ARDUINOJSON_VERSION_MACRO V741
|
#define ARDUINOJSON_VERSION_MACRO V742
|
||||||
|
Reference in New Issue
Block a user