mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-12-23 15:08:08 +01:00
Compare commits
31 Commits
7.6-with-s
...
7.x
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aa7fbd6c8b | ||
|
|
21144d5691 | ||
|
|
5f96515360 | ||
|
|
bec657eda7 | ||
|
|
1232f61e55 | ||
|
|
65518a8b00 | ||
|
|
a51865ca42 | ||
|
|
af36add5d8 | ||
|
|
3eced7d7ec | ||
|
|
c4c5c69baf | ||
|
|
b017dfb13a | ||
|
|
88769c44b4 | ||
|
|
01de2f3fbb | ||
|
|
4a2568e3c8 | ||
|
|
f124af3909 | ||
|
|
76eec98efd | ||
|
|
04823fd304 | ||
|
|
3f62a2ca96 | ||
|
|
6dbdeca58a | ||
|
|
a159820ade | ||
|
|
c222fc2f0a | ||
|
|
b4024e7032 | ||
|
|
7dece839d6 | ||
|
|
ed6b470896 | ||
|
|
539b66fc7a | ||
|
|
6e7eddb959 | ||
|
|
3a49896200 | ||
|
|
c743c1e327 | ||
|
|
c541c57900 | ||
|
|
db2eec46c7 | ||
|
|
dddc4912c4 |
4
.github/workflows/ci.yml
vendored
4
.github/workflows/ci.yml
vendored
@@ -398,7 +398,9 @@ jobs:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
- name: Install Particle CLI
|
||||
run: sudo npm install -g particle-cli
|
||||
run: |
|
||||
bash <( curl -sL https://particle.io/install-cli )
|
||||
echo "$HOME/bin" >> $GITHUB_PATH
|
||||
- name: Login to Particle
|
||||
run: particle login -t "${{ secrets.PARTICLE_TOKEN }}"
|
||||
- name: Compile
|
||||
|
||||
34
CHANGELOG.md
34
CHANGELOG.md
@@ -1,6 +1,40 @@
|
||||
ArduinoJson: change log
|
||||
=======================
|
||||
|
||||
HEAD
|
||||
----
|
||||
|
||||
* Don't store string literals by pointer anymore (issue #2189)
|
||||
Version 7.3 introduced a new way to detect string literals, but it fails in some edge cases.
|
||||
I could not find a way to fix it, so I chose to remove the optimization rather than keep it broken.
|
||||
* Replace the "extension slots" mechanism with a memory pool dedicated to 8-byte values.
|
||||
|
||||
> ### BREAKING CHANGES
|
||||
>
|
||||
> #### `JsonString` constructor's boolean parameter
|
||||
>
|
||||
> Since version 7.3, you could pass a boolean to `JsonString`'s constructor to force the string to be stored by pointer.
|
||||
> This optimization has been removed, and you'll get a deprecation warning if you use it.
|
||||
> To fix the issue, you must remove the boolean argument from the constructor, or better yet, remove `JsonString` altogether.
|
||||
>
|
||||
> ```diff
|
||||
> char name[] = "ArduinoJson";
|
||||
> - doc["name"] = JsonString(name, true);
|
||||
> + doc["name"] = name;
|
||||
> ```
|
||||
>
|
||||
> #### NUL characters in string literals
|
||||
>
|
||||
> Since version 7.3, ArduinoJson has supported NUL characters (`\0`) in string literals.
|
||||
> This feature has been removed as part of the storage policy change.
|
||||
>
|
||||
> If you do need to include NULs in your string, you must use a `JsonString` instead:
|
||||
>
|
||||
> ```diff
|
||||
> - doc["strings"] = "hello\0world"
|
||||
> + doc["strings"] = JsonString("hello\0world", 11)
|
||||
> ```
|
||||
|
||||
v7.4.2 (2025-06-20)
|
||||
------
|
||||
|
||||
|
||||
@@ -91,10 +91,6 @@ ArduinoJson is a C++ JSON library for Arduino and IoT (Internet Of Things).
|
||||
* [Troubleshooter](https://arduinojson.org/v7/troubleshooter/)
|
||||
* [Book](https://arduinojson.org/book/)
|
||||
* [Changelog](CHANGELOG.md)
|
||||
* Vibrant user community
|
||||
* Most popular of all Arduino libraries on [GitHub](https://github.com/search?o=desc&q=arduino+library&s=stars&type=Repositories)
|
||||
* [Used in hundreds of projects](https://www.hackster.io/search?i=projects&q=arduinojson)
|
||||
* [Responsive support](https://github.com/bblanchon/ArduinoJson/issues?q=is%3Aissue+is%3Aclosed)
|
||||
|
||||
## Quickstart
|
||||
|
||||
|
||||
@@ -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::detail::ResourceManager::slotSize == 6, "slot size");
|
||||
static_assert(sizeof(ArduinoJson::detail::VariantData) == 6, "slot size");
|
||||
|
||||
void setup() {}
|
||||
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::detail::ResourceManager::slotSize == 8, "slot size");
|
||||
static_assert(sizeof(ArduinoJson::detail::VariantData) == 8, "slot size");
|
||||
|
||||
void setup() {}
|
||||
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::detail::ResourceManager::slotSize == 16,
|
||||
"slot size");
|
||||
static_assert(sizeof(ArduinoJson::detail::VariantData) == 16, "slot size");
|
||||
|
||||
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::detail::ResourceManager::slotSize == 8, "slot size");
|
||||
static_assert(sizeof(ArduinoJson::detail::VariantData) == 8, "slot size");
|
||||
|
||||
int main() {}
|
||||
|
||||
@@ -54,7 +54,7 @@ TEST_CASE("BasicJsonDocument") {
|
||||
doc["hello"] = "world";
|
||||
auto copy = doc;
|
||||
REQUIRE(copy.as<std::string>() == "{\"hello\":\"world\"}");
|
||||
REQUIRE(allocatorLog == "AA");
|
||||
REQUIRE(allocatorLog == "AAAAAA");
|
||||
}
|
||||
|
||||
SECTION("capacity") {
|
||||
|
||||
@@ -269,10 +269,10 @@ inline size_t sizeofPoolList(size_t n = ARDUINOJSON_INITIAL_POOL_COUNT) {
|
||||
return sizeof(MemoryPool<VariantData>) * n;
|
||||
}
|
||||
|
||||
template <typename T = ArduinoJson::detail::VariantData>
|
||||
inline size_t sizeofPool(
|
||||
ArduinoJson::detail::SlotCount n = ARDUINOJSON_POOL_CAPACITY) {
|
||||
using namespace ArduinoJson::detail;
|
||||
return MemoryPool<VariantData>::slotsToBytes(n);
|
||||
return ArduinoJson::detail::MemoryPool<T>::slotsToBytes(n);
|
||||
}
|
||||
|
||||
inline size_t sizeofStringBuffer(size_t iteration = 1) {
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#include "Allocators.hpp"
|
||||
#include "Literals.hpp"
|
||||
|
||||
using ArduinoJson::detail::sizeofArray;
|
||||
using namespace ArduinoJson::detail;
|
||||
|
||||
TEST_CASE("JsonArray::add(T)") {
|
||||
SpyingAllocator spy;
|
||||
@@ -33,7 +33,8 @@ TEST_CASE("JsonArray::add(T)") {
|
||||
REQUIRE(array[0].is<double>());
|
||||
REQUIRE_FALSE(array[0].is<bool>());
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofPool<VariantData>()),
|
||||
Allocate(sizeofPool<EightByteValue>()),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -56,6 +57,7 @@ TEST_CASE("JsonArray::add(T)") {
|
||||
REQUIRE(array[0].is<int>() == false);
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
#include "Allocators.hpp"
|
||||
|
||||
using ArduinoJson::detail::sizeofArray;
|
||||
using namespace ArduinoJson::detail;
|
||||
|
||||
TEST_CASE("deserialize JSON array") {
|
||||
SpyingAllocator spy;
|
||||
@@ -92,8 +92,12 @@ TEST_CASE("deserialize JSON array") {
|
||||
REQUIRE(arr[0].as<double>() == Approx(4.2123456));
|
||||
REQUIRE(arr[1] == -7E89);
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Reallocate(sizeofPool(), sizeofPool(4)),
|
||||
Allocate(sizeofPool<VariantData>()),
|
||||
Allocate(sizeofPool<EightByteValue>()),
|
||||
Reallocate(sizeofPool<VariantData>(),
|
||||
sizeofPool<VariantData>(2)),
|
||||
Reallocate(sizeofPool<EightByteValue>(),
|
||||
sizeofPool<EightByteValue>(2)),
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -104,6 +104,8 @@ TEST_CASE("deserializeJson(MemberProxy)") {
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.as<std::string>() == "{\"hello\":\"world\",\"value\":[42]}");
|
||||
REQUIRE(spy.log() == AllocatorLog{});
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofString("value")),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
|
||||
SECTION("uint32_t should pass") {
|
||||
|
||||
@@ -31,6 +31,7 @@ TEST_CASE("ElementProxy::add()") {
|
||||
REQUIRE(doc.as<std::string>() == "[[\"world\"]]");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("world")),
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@ TEST_CASE("MemberProxy::add()") {
|
||||
REQUIRE(doc.as<std::string>() == "{\"hello\":[42]}");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -34,6 +35,8 @@ TEST_CASE("MemberProxy::add()") {
|
||||
REQUIRE(doc.as<std::string>() == "{\"hello\":[\"world\"]}");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
Allocate(sizeofString("world")),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -44,6 +47,7 @@ TEST_CASE("MemberProxy::add()") {
|
||||
REQUIRE(doc.as<std::string>() == "{\"hello\":[\"world\"]}");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
Allocate(sizeofString("world")),
|
||||
});
|
||||
}
|
||||
@@ -55,6 +59,7 @@ TEST_CASE("MemberProxy::add()") {
|
||||
REQUIRE(doc.as<std::string>() == "{\"hello\":[\"world\"]}");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
Allocate(sizeofString("world")),
|
||||
|
||||
});
|
||||
@@ -71,6 +76,7 @@ TEST_CASE("MemberProxy::add()") {
|
||||
REQUIRE(doc.as<std::string>() == "{\"hello\":[\"world\"]}");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
Allocate(sizeofString("world")),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@ TEST_CASE("JsonDocument::add(T)") {
|
||||
REQUIRE(doc.as<std::string>() == "[\"hello\"]");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -8,8 +8,6 @@
|
||||
#include "Allocators.hpp"
|
||||
#include "Literals.hpp"
|
||||
|
||||
using ArduinoJson::detail::addPadding;
|
||||
|
||||
TEST_CASE("JsonDocument constructor") {
|
||||
SpyingAllocator spyingAllocator;
|
||||
|
||||
@@ -64,6 +62,8 @@ TEST_CASE("JsonDocument constructor") {
|
||||
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
|
||||
REQUIRE(spyingAllocator.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
Allocate(sizeofString("world")),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -87,6 +87,7 @@ TEST_CASE("JsonDocument constructor") {
|
||||
REQUIRE(doc2.as<std::string>() == "[\"hello\"]");
|
||||
REQUIRE(spyingAllocator.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -22,10 +22,10 @@ TEST_CASE("JsonDocument::remove()") {
|
||||
|
||||
SECTION("string literal") {
|
||||
doc["a"] = 1;
|
||||
doc["a\0b"_s] = 2;
|
||||
doc["ab"_s] = 2;
|
||||
doc["b"] = 3;
|
||||
|
||||
doc.remove("a\0b");
|
||||
doc.remove("ab");
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "{\"a\":1,\"b\":3}");
|
||||
}
|
||||
|
||||
@@ -37,7 +37,9 @@ TEST_CASE("JsonDocument::set()") {
|
||||
doc.set("example");
|
||||
|
||||
REQUIRE(doc.as<const char*>() == "example"_s);
|
||||
REQUIRE(spy.log() == AllocatorLog{});
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofString("example")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("const char*") {
|
||||
|
||||
@@ -69,17 +69,8 @@ TEST_CASE("JsonDocument::shrinkToFit()") {
|
||||
REQUIRE(spyingAllocator.log() == AllocatorLog{});
|
||||
}
|
||||
|
||||
SECTION("linked string") {
|
||||
doc.set("hello");
|
||||
|
||||
doc.shrinkToFit();
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "hello");
|
||||
REQUIRE(spyingAllocator.log() == AllocatorLog{});
|
||||
}
|
||||
|
||||
SECTION("owned string") {
|
||||
doc.set("abcdefg"_s);
|
||||
SECTION("string") {
|
||||
doc.set("abcdefg");
|
||||
REQUIRE(doc.as<std::string>() == "abcdefg");
|
||||
|
||||
doc.shrinkToFit();
|
||||
@@ -101,20 +92,7 @@ TEST_CASE("JsonDocument::shrinkToFit()") {
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("linked key") {
|
||||
doc["key"] = 42;
|
||||
|
||||
doc.shrinkToFit();
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "{\"key\":42}");
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Reallocate(sizeofPool(), sizeofObject(1)),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("owned key") {
|
||||
SECTION("object key") {
|
||||
doc["abcdefg"_s] = 42;
|
||||
|
||||
doc.shrinkToFit();
|
||||
@@ -128,20 +106,7 @@ TEST_CASE("JsonDocument::shrinkToFit()") {
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("linked string in array") {
|
||||
doc.add("hello");
|
||||
|
||||
doc.shrinkToFit();
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "[\"hello\"]");
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Reallocate(sizeofPool(), sizeofArray(1)),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("owned string in array") {
|
||||
SECTION("string in array") {
|
||||
doc.add("abcdefg"_s);
|
||||
|
||||
doc.shrinkToFit();
|
||||
@@ -155,20 +120,7 @@ TEST_CASE("JsonDocument::shrinkToFit()") {
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("linked string in object") {
|
||||
doc["key"] = "hello";
|
||||
|
||||
doc.shrinkToFit();
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "{\"key\":\"hello\"}");
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Reallocate(sizeofPool(), sizeofObject(1)),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("owned string in object") {
|
||||
SECTION("string in object") {
|
||||
doc["key"] = "abcdefg"_s;
|
||||
|
||||
doc.shrinkToFit();
|
||||
|
||||
@@ -14,7 +14,7 @@ TEST_CASE("JsonDocument::operator[]") {
|
||||
|
||||
SECTION("object") {
|
||||
doc["abc"_s] = "ABC";
|
||||
doc["abc\0d"_s] = "ABCD";
|
||||
doc["abcd"_s] = "ABCD";
|
||||
|
||||
SECTION("const char*") {
|
||||
const char* key = "abc";
|
||||
@@ -25,20 +25,20 @@ TEST_CASE("JsonDocument::operator[]") {
|
||||
SECTION("string literal") {
|
||||
REQUIRE(doc["abc"] == "ABC");
|
||||
REQUIRE(cdoc["abc"] == "ABC");
|
||||
REQUIRE(doc["abc\0d"] == "ABCD");
|
||||
REQUIRE(cdoc["abc\0d"] == "ABCD");
|
||||
REQUIRE(doc["abcd"] == "ABCD");
|
||||
REQUIRE(cdoc["abcd"] == "ABCD");
|
||||
}
|
||||
|
||||
SECTION("std::string") {
|
||||
REQUIRE(doc["abc"_s] == "ABC");
|
||||
REQUIRE(cdoc["abc"_s] == "ABC");
|
||||
REQUIRE(doc["abc\0d"_s] == "ABCD");
|
||||
REQUIRE(cdoc["abc\0d"_s] == "ABCD");
|
||||
REQUIRE(doc["abcd"_s] == "ABCD");
|
||||
REQUIRE(cdoc["abcd"_s] == "ABCD");
|
||||
}
|
||||
|
||||
SECTION("JsonVariant") {
|
||||
doc["key1"] = "abc";
|
||||
doc["key2"] = "abc\0d"_s;
|
||||
doc["key2"] = "abcd"_s;
|
||||
doc["key3"] = "foo";
|
||||
|
||||
CHECK(doc[doc["key1"]] == "ABC");
|
||||
@@ -114,6 +114,7 @@ TEST_CASE("JsonDocument::operator[] key storage") {
|
||||
REQUIRE(doc.as<std::string>() == "{\"hello\":0}");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -16,44 +16,18 @@ TEST_CASE("JsonObject::set()") {
|
||||
JsonObject obj1 = doc1.to<JsonObject>();
|
||||
JsonObject obj2 = doc2.to<JsonObject>();
|
||||
|
||||
SECTION("doesn't copy static string in key or value") {
|
||||
SECTION("copy key and string value") {
|
||||
obj1["hello"] = "world";
|
||||
spy.clearLog();
|
||||
|
||||
bool success = obj2.set(obj1);
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj2["hello"] == "world"_s);
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("copy local string value") {
|
||||
obj1["hello"] = "world"_s;
|
||||
spy.clearLog();
|
||||
|
||||
bool success = obj2.set(obj1);
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj2["hello"] == "world"_s);
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("world")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("copy local key") {
|
||||
obj1["hello"_s] = "world";
|
||||
spy.clearLog();
|
||||
|
||||
bool success = obj2.set(obj1);
|
||||
|
||||
REQUIRE(success == true);
|
||||
REQUIRE(obj2["hello"] == "world"_s);
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
Allocate(sizeofString("world")),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -110,7 +84,7 @@ TEST_CASE("JsonObject::set()") {
|
||||
}
|
||||
|
||||
SECTION("copy fails in the middle of an array") {
|
||||
TimebombAllocator timebomb(1);
|
||||
TimebombAllocator timebomb(2);
|
||||
JsonDocument doc3(&timebomb);
|
||||
JsonObject obj3 = doc3.to<JsonObject>();
|
||||
|
||||
|
||||
@@ -101,30 +101,8 @@ TEST_CASE("JsonObject::operator[]") {
|
||||
obj[key] = 42;
|
||||
REQUIRE(42 == obj[key]);
|
||||
}
|
||||
|
||||
SECTION("should not duplicate const char*") {
|
||||
SECTION("should duplicate key and value strings") {
|
||||
obj["hello"] = "world";
|
||||
REQUIRE(spy.log() == AllocatorLog{Allocate(sizeofPool())});
|
||||
}
|
||||
|
||||
SECTION("should duplicate char* value") {
|
||||
obj["hello"] = const_cast<char*>("world");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("world")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("should duplicate char* key") {
|
||||
obj[const_cast<char*>("hello")] = "world";
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("should duplicate char* key&value") {
|
||||
obj[const_cast<char*>("hello")] = const_cast<char*>("world");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
@@ -132,46 +110,6 @@ TEST_CASE("JsonObject::operator[]") {
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("should duplicate std::string value") {
|
||||
obj["hello"] = "world"_s;
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("world")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("should duplicate std::string key") {
|
||||
obj["hello"_s] = "world";
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("should duplicate std::string key&value") {
|
||||
obj["hello"_s] = "world"_s;
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
Allocate(sizeofString("world")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("should duplicate a non-static JsonString key") {
|
||||
obj[JsonString("hello", false)] = "world";
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("should not duplicate a static JsonString key") {
|
||||
obj[JsonString("hello", true)] = "world";
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("should ignore null key") {
|
||||
// object must have a value to make a call to strcmp()
|
||||
obj["dummy"] = 42;
|
||||
|
||||
@@ -185,7 +185,6 @@ TEST_CASE("JsonVariant::as()") {
|
||||
REQUIRE(variant.as<long>() == 42L);
|
||||
REQUIRE(variant.as<double>() == 42);
|
||||
REQUIRE(variant.as<JsonString>() == "42");
|
||||
REQUIRE(variant.as<JsonString>().isStatic() == true);
|
||||
}
|
||||
|
||||
SECTION("set(\"hello\")") {
|
||||
@@ -208,7 +207,6 @@ TEST_CASE("JsonVariant::as()") {
|
||||
REQUIRE(variant.as<const char*>() == "4.2"_s);
|
||||
REQUIRE(variant.as<std::string>() == "4.2"_s);
|
||||
REQUIRE(variant.as<JsonString>() == "4.2");
|
||||
REQUIRE(variant.as<JsonString>().isStatic() == false);
|
||||
}
|
||||
|
||||
SECTION("set(std::string(\"123.45\"))") {
|
||||
@@ -220,7 +218,6 @@ TEST_CASE("JsonVariant::as()") {
|
||||
REQUIRE(variant.as<const char*>() == "123.45"_s);
|
||||
REQUIRE(variant.as<std::string>() == "123.45"_s);
|
||||
REQUIRE(variant.as<JsonString>() == "123.45");
|
||||
REQUIRE(variant.as<JsonString>().isStatic() == false);
|
||||
}
|
||||
|
||||
SECTION("set(\"true\")") {
|
||||
|
||||
@@ -38,13 +38,15 @@ TEST_CASE("JsonVariant::set(JsonVariant)") {
|
||||
REQUIRE(var1.as<std::string>() == "{\"value\":[42]}");
|
||||
}
|
||||
|
||||
SECTION("stores const char* by reference") {
|
||||
SECTION("stores string literals by copy") {
|
||||
var1.set("hello!!");
|
||||
spyingAllocator.clearLog();
|
||||
|
||||
var2.set(var1);
|
||||
|
||||
REQUIRE(spyingAllocator.log() == AllocatorLog{});
|
||||
REQUIRE(spyingAllocator.log() == AllocatorLog{
|
||||
Allocate(sizeofString("hello!!")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("stores char* by copy") {
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#include "Allocators.hpp"
|
||||
#include "Literals.hpp"
|
||||
|
||||
using ArduinoJson::detail::sizeofObject;
|
||||
using namespace ArduinoJson::detail;
|
||||
|
||||
enum ErrorCode { ERROR_01 = 1, ERROR_10 = 10 };
|
||||
|
||||
@@ -18,12 +18,13 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
|
||||
JsonVariant variant = doc.to<JsonVariant>();
|
||||
|
||||
SECTION("string literal") {
|
||||
bool result = variant.set("hello\0world");
|
||||
bool result = variant.set("hello world");
|
||||
|
||||
REQUIRE(result == true);
|
||||
CHECK(variant ==
|
||||
"hello"_s); // linked string cannot contain '\0' at the moment
|
||||
CHECK(spy.log() == AllocatorLog{});
|
||||
REQUIRE(variant == "hello world"_s); // stores by copy
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofString(11)),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("const char*") {
|
||||
@@ -140,19 +141,7 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("static JsonString") {
|
||||
char str[16];
|
||||
|
||||
strcpy(str, "hello");
|
||||
bool result = variant.set(JsonString(str, true));
|
||||
strcpy(str, "world");
|
||||
|
||||
REQUIRE(result == true);
|
||||
REQUIRE(variant == "world"); // stores by pointer
|
||||
REQUIRE(spy.log() == AllocatorLog{});
|
||||
}
|
||||
|
||||
SECTION("non-static JsonString") {
|
||||
SECTION("JsonString") {
|
||||
char str[16];
|
||||
|
||||
strcpy(str, "hello");
|
||||
@@ -193,11 +182,11 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
|
||||
REQUIRE(result == true);
|
||||
REQUIRE(variant.is<double>() == true);
|
||||
REQUIRE(variant.as<double>() == 1.2);
|
||||
REQUIRE(spy.log() ==
|
||||
AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Reallocate(sizeofPool(), sizeofPool(1)), // one extension slot
|
||||
});
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool<EightByteValue>()),
|
||||
Reallocate(sizeofPool<EightByteValue>(),
|
||||
sizeofPool<EightByteValue>(1)),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("int32_t") {
|
||||
@@ -216,11 +205,11 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
|
||||
REQUIRE(result == true);
|
||||
REQUIRE(variant.is<int64_t>() == true);
|
||||
REQUIRE(variant.as<int64_t>() == -2147483649LL);
|
||||
REQUIRE(spy.log() ==
|
||||
AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Reallocate(sizeofPool(), sizeofPool(1)), // one extension slot
|
||||
});
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool<EightByteValue>()),
|
||||
Reallocate(sizeofPool<EightByteValue>(),
|
||||
sizeofPool<EightByteValue>(1)),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("uint32_t") {
|
||||
@@ -239,11 +228,11 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
|
||||
REQUIRE(result == true);
|
||||
REQUIRE(variant.is<uint64_t>() == true);
|
||||
REQUIRE(variant.as<uint64_t>() == 4294967296);
|
||||
REQUIRE(spy.log() ==
|
||||
AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Reallocate(sizeofPool(), sizeofPool(1)), // one extension slot
|
||||
});
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool<EightByteValue>()),
|
||||
Reallocate(sizeofPool<EightByteValue>(),
|
||||
sizeofPool<EightByteValue>(1)),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("JsonDocument") {
|
||||
@@ -360,7 +349,7 @@ TEST_CASE("JsonVariant::set() releases the previous value") {
|
||||
}
|
||||
|
||||
SECTION("float") {
|
||||
v.set(1.2);
|
||||
v.set(1.2f);
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Deallocate(sizeofString("world")),
|
||||
});
|
||||
@@ -375,7 +364,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;
|
||||
JsonDocument doc(&spy);
|
||||
JsonVariant variant = doc.to<JsonVariant>();
|
||||
|
||||
@@ -52,12 +52,12 @@ TEST_CASE("JsonVariantConst::operator[]") {
|
||||
JsonObject object = doc.to<JsonObject>();
|
||||
object["ab"_s] = "AB";
|
||||
object["abc"_s] = "ABC";
|
||||
object["abc\0d"_s] = "ABCD";
|
||||
object["abcd"_s] = "ABCD";
|
||||
|
||||
SECTION("string literal") {
|
||||
REQUIRE(var["ab"] == "AB"_s);
|
||||
REQUIRE(var["abc"] == "ABC"_s);
|
||||
REQUIRE(var["abc\0d"] == "ABCD"_s);
|
||||
REQUIRE(var["abcd"] == "ABCD"_s);
|
||||
REQUIRE(var["def"].isNull());
|
||||
REQUIRE(var[0].isNull());
|
||||
}
|
||||
@@ -73,7 +73,7 @@ TEST_CASE("JsonVariantConst::operator[]") {
|
||||
SECTION("supports std::string") {
|
||||
REQUIRE(var["ab"_s] == "AB"_s);
|
||||
REQUIRE(var["abc"_s] == "ABC"_s);
|
||||
REQUIRE(var["abc\0d"_s] == "ABCD"_s);
|
||||
REQUIRE(var["abcd"_s] == "ABCD"_s);
|
||||
REQUIRE(var["def"_s].isNull());
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ TEST_CASE("JsonVariantConst::operator[]") {
|
||||
SECTION("supports JsonVariant") {
|
||||
object["key1"] = "ab";
|
||||
object["key2"] = "abc";
|
||||
object["key3"] = "abc\0d"_s;
|
||||
object["key3"] = "abcd"_s;
|
||||
object["key4"] = "foo";
|
||||
|
||||
REQUIRE(var[var["key1"]] == "AB"_s);
|
||||
|
||||
@@ -13,7 +13,6 @@ TEST_CASE("JsonString") {
|
||||
|
||||
CHECK(s.isNull() == true);
|
||||
CHECK(s.c_str() == 0);
|
||||
CHECK(s.isStatic() == true);
|
||||
CHECK(s == JsonString());
|
||||
CHECK(s != "");
|
||||
}
|
||||
@@ -96,7 +95,6 @@ TEST_CASE("JsonString") {
|
||||
JsonString s("hello world", 5);
|
||||
|
||||
CHECK(s.size() == 5);
|
||||
CHECK(s.isStatic() == false);
|
||||
CHECK(s == "hello");
|
||||
CHECK(s != "hello world");
|
||||
}
|
||||
|
||||
@@ -21,8 +21,7 @@ TEST_CASE("adaptString()") {
|
||||
auto s = adaptString("bravo\0alpha");
|
||||
|
||||
CHECK(s.isNull() == false);
|
||||
CHECK(s.size() == 11);
|
||||
CHECK(s.isStatic() == true);
|
||||
CHECK(s.size() == 5);
|
||||
}
|
||||
|
||||
SECTION("null const char*") {
|
||||
@@ -38,7 +37,6 @@ TEST_CASE("adaptString()") {
|
||||
|
||||
CHECK(s.isNull() == false);
|
||||
CHECK(s.size() == 5);
|
||||
CHECK(s.isStatic() == false);
|
||||
CHECK(s.data() == p);
|
||||
}
|
||||
|
||||
@@ -46,7 +44,6 @@ TEST_CASE("adaptString()") {
|
||||
auto s = adaptString(static_cast<const char*>(0), 10);
|
||||
|
||||
CHECK(s.isNull() == true);
|
||||
CHECK(s.isStatic() == false);
|
||||
}
|
||||
|
||||
SECTION("non-null const char* + size") {
|
||||
@@ -54,7 +51,6 @@ TEST_CASE("adaptString()") {
|
||||
|
||||
CHECK(s.isNull() == false);
|
||||
CHECK(s.size() == 5);
|
||||
CHECK(s.isStatic() == false);
|
||||
}
|
||||
|
||||
SECTION("null Flash string") {
|
||||
@@ -62,7 +58,6 @@ TEST_CASE("adaptString()") {
|
||||
|
||||
CHECK(s.isNull() == true);
|
||||
CHECK(s.size() == 0);
|
||||
CHECK(s.isStatic() == false);
|
||||
}
|
||||
|
||||
SECTION("non-null Flash string") {
|
||||
@@ -70,7 +65,6 @@ TEST_CASE("adaptString()") {
|
||||
|
||||
CHECK(s.isNull() == false);
|
||||
CHECK(s.size() == 5);
|
||||
CHECK(s.isStatic() == false);
|
||||
}
|
||||
|
||||
SECTION("std::string") {
|
||||
@@ -79,7 +73,6 @@ TEST_CASE("adaptString()") {
|
||||
|
||||
CHECK(s.isNull() == false);
|
||||
CHECK(s.size() == 5);
|
||||
CHECK(s.isStatic() == false);
|
||||
}
|
||||
|
||||
SECTION("Arduino String") {
|
||||
@@ -88,7 +81,6 @@ TEST_CASE("adaptString()") {
|
||||
|
||||
CHECK(s.isNull() == false);
|
||||
CHECK(s.size() == 5);
|
||||
CHECK(s.isStatic() == false);
|
||||
}
|
||||
|
||||
SECTION("custom_string") {
|
||||
@@ -97,25 +89,14 @@ TEST_CASE("adaptString()") {
|
||||
|
||||
CHECK(s.isNull() == false);
|
||||
CHECK(s.size() == 5);
|
||||
CHECK(s.isStatic() == false);
|
||||
}
|
||||
|
||||
SECTION("JsonString linked") {
|
||||
JsonString orig("hello", true);
|
||||
SECTION("JsonString") {
|
||||
JsonString orig("hello");
|
||||
auto s = adaptString(orig);
|
||||
|
||||
CHECK(s.isNull() == false);
|
||||
CHECK(s.size() == 5);
|
||||
CHECK(s.isStatic() == true);
|
||||
}
|
||||
|
||||
SECTION("JsonString copied") {
|
||||
JsonString orig("hello", false);
|
||||
auto s = adaptString(orig);
|
||||
|
||||
CHECK(s.isNull() == false);
|
||||
CHECK(s.size() == 5);
|
||||
CHECK(s.isStatic() == false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
#define ARDUINOJSON_ENABLE_ALIGNMENT 0
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
#include <ArduinoJson/Memory/Alignment.hpp>
|
||||
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("ARDUINOJSON_ENABLE_ALIGNMENT == 0") {
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#define ARDUINOJSON_ENABLE_ALIGNMENT 1
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
#include <ArduinoJson/Memory/Alignment.hpp>
|
||||
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("ARDUINOJSON_ENABLE_ALIGNMENT == 1") {
|
||||
|
||||
@@ -89,58 +89,71 @@ TEST_CASE("ARDUINOJSON_STRING_LENGTH_SIZE == 4") {
|
||||
|
||||
REQUIRE(err != DeserializationError::Ok);
|
||||
}
|
||||
|
||||
SECTION("bin 32") {
|
||||
auto str = std::string(65536, '?');
|
||||
auto input = "\xc6\x00\x01\x00\x00"_s + str;
|
||||
|
||||
auto err = deserializeMsgPack(doc, input);
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<MsgPackBinary>());
|
||||
auto binary = doc.as<MsgPackBinary>();
|
||||
REQUIRE(binary.size() == 65536);
|
||||
REQUIRE(binary.data() != nullptr);
|
||||
REQUIRE(std::string(reinterpret_cast<const char*>(binary.data()),
|
||||
binary.size()) == str);
|
||||
}
|
||||
|
||||
SECTION("ext 32 deserialization") {
|
||||
auto str = std::string(65536, '?');
|
||||
auto input = "\xc9\x00\x01\x00\x00\x2a"_s + str;
|
||||
|
||||
auto err = deserializeMsgPack(doc, input);
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<MsgPackExtension>());
|
||||
auto value = doc.as<MsgPackExtension>();
|
||||
REQUIRE(value.type() == 42);
|
||||
REQUIRE(value.size() == 65536);
|
||||
REQUIRE(value.data() != nullptr);
|
||||
REQUIRE(std::string(reinterpret_cast<const char*>(value.data()),
|
||||
value.size()) == str);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("bin 32 deserialization") {
|
||||
auto str = std::string(65536, '?');
|
||||
auto input = "\xc6\x00\x01\x00\x00"_s + str;
|
||||
SECTION("serializeMsgPack()") {
|
||||
SECTION("bin 32 serialization") {
|
||||
auto str = std::string(65536, '?');
|
||||
doc.set(MsgPackBinary(str.data(), str.size()));
|
||||
|
||||
auto err = deserializeMsgPack(doc, input);
|
||||
std::string output;
|
||||
auto result = serializeMsgPack(doc, output);
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<MsgPackBinary>());
|
||||
auto binary = doc.as<MsgPackBinary>();
|
||||
REQUIRE(binary.size() == 65536);
|
||||
REQUIRE(binary.data() != nullptr);
|
||||
REQUIRE(std::string(reinterpret_cast<const char*>(binary.data()),
|
||||
binary.size()) == str);
|
||||
}
|
||||
REQUIRE(result == 5 + str.size());
|
||||
REQUIRE(output == "\xc6\x00\x01\x00\x00"_s + str);
|
||||
}
|
||||
|
||||
SECTION("bin 32 serialization") {
|
||||
auto str = std::string(65536, '?');
|
||||
doc.set(MsgPackBinary(str.data(), str.size()));
|
||||
SECTION("ext 32 serialization") {
|
||||
auto str = std::string(65536, '?');
|
||||
doc.set(MsgPackExtension(42, str.data(), str.size()));
|
||||
|
||||
std::string output;
|
||||
auto result = serializeMsgPack(doc, output);
|
||||
std::string output;
|
||||
auto result = serializeMsgPack(doc, output);
|
||||
|
||||
REQUIRE(result == 5 + str.size());
|
||||
REQUIRE(output == "\xc6\x00\x01\x00\x00"_s + str);
|
||||
}
|
||||
REQUIRE(result == 6 + str.size());
|
||||
REQUIRE(output == "\xc9\x00\x01\x00\x00\x2a"_s + str);
|
||||
}
|
||||
|
||||
SECTION("ext 32 deserialization") {
|
||||
auto str = std::string(65536, '?');
|
||||
auto input = "\xc9\x00\x01\x00\x00\x2a"_s + str;
|
||||
SECTION("str 32 serialization") {
|
||||
auto str = std::string(65536, '?');
|
||||
doc.set(str);
|
||||
|
||||
auto err = deserializeMsgPack(doc, input);
|
||||
std::string output;
|
||||
auto result = serializeMsgPack(doc, output);
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<MsgPackExtension>());
|
||||
auto value = doc.as<MsgPackExtension>();
|
||||
REQUIRE(value.type() == 42);
|
||||
REQUIRE(value.size() == 65536);
|
||||
REQUIRE(value.data() != nullptr);
|
||||
REQUIRE(std::string(reinterpret_cast<const char*>(value.data()),
|
||||
value.size()) == str);
|
||||
}
|
||||
|
||||
SECTION("ext 32 serialization") {
|
||||
auto str = std::string(65536, '?');
|
||||
doc.set(MsgPackExtension(42, str.data(), str.size()));
|
||||
|
||||
std::string output;
|
||||
auto result = serializeMsgPack(doc, output);
|
||||
|
||||
REQUIRE(result == 6 + str.size());
|
||||
REQUIRE(output == "\xc9\x00\x01\x00\x00\x2a"_s + str);
|
||||
REQUIRE(result == 5 + str.size());
|
||||
REQUIRE(output == "\xDB\x00\x01\x00\x00"_s + str);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,6 +104,8 @@ TEST_CASE("deserializeMsgPack(MemberProxy)") {
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.as<std::string>() == "{\"hello\":\"world\",\"value\":[42]}");
|
||||
REQUIRE(spy.log() == AllocatorLog{});
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofString("value")),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,11 +137,12 @@ TEST_CASE("serialize MsgPack value") {
|
||||
checkVariant(longest.c_str(), "\xDA\xFF\xFF"_s + longest);
|
||||
}
|
||||
|
||||
#if ARDUINOJSON_STRING_LENGTH_SIZE > 2
|
||||
SECTION("str 32") {
|
||||
std::string shortest(65536, '?');
|
||||
checkVariant(JsonString(shortest.c_str(), true), // force store by pointer
|
||||
"\xDB\x00\x01\x00\x00"_s + shortest);
|
||||
checkVariant(shortest.c_str(), "\xDB\x00\x01\x00\x00"_s + shortest);
|
||||
}
|
||||
#endif
|
||||
|
||||
SECTION("serialized(const char*)") {
|
||||
checkVariant(serialized("\xDA\xFF\xFF"), "\xDA\xFF\xFF");
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Memory/StringBuffer.hpp>
|
||||
#include <ArduinoJson/Variant/VariantImpl.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
@@ -21,7 +22,7 @@ TEST_CASE("StringBuffer") {
|
||||
strcpy(ptr, "hi!");
|
||||
sb.save(&variant);
|
||||
|
||||
REQUIRE(variant.type() == VariantType::TinyString);
|
||||
REQUIRE(variant.type == VariantType::TinyString);
|
||||
REQUIRE(variant.asString() == "hi!");
|
||||
}
|
||||
|
||||
@@ -30,7 +31,7 @@ TEST_CASE("StringBuffer") {
|
||||
memcpy(ptr, "a\0b", 3);
|
||||
sb.save(&variant);
|
||||
|
||||
REQUIRE(variant.type() == VariantType::OwnedString);
|
||||
REQUIRE(variant.type == VariantType::LongString);
|
||||
|
||||
auto str = variant.asString();
|
||||
REQUIRE(str.size() == 3);
|
||||
@@ -44,7 +45,7 @@ TEST_CASE("StringBuffer") {
|
||||
strcpy(ptr, "alfa");
|
||||
sb.save(&variant);
|
||||
|
||||
REQUIRE(variant.type() == VariantType::OwnedString);
|
||||
REQUIRE(variant.type == VariantType::LongString);
|
||||
REQUIRE(variant.asString() == "alfa");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Memory/StringBuilder.hpp>
|
||||
#include <ArduinoJson/Variant/VariantImpl.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
@@ -26,7 +27,7 @@ TEST_CASE("StringBuilder") {
|
||||
REQUIRE(spyingAllocator.log() == AllocatorLog{
|
||||
Allocate(sizeofStringBuffer()),
|
||||
});
|
||||
REQUIRE(data.type() == VariantType::TinyString);
|
||||
REQUIRE(data.type == VariantType::TinyString);
|
||||
}
|
||||
|
||||
SECTION("Tiny string") {
|
||||
@@ -45,7 +46,7 @@ TEST_CASE("StringBuilder") {
|
||||
str.save(&data);
|
||||
|
||||
REQUIRE(resources.overflowed() == false);
|
||||
REQUIRE(data.type() == VariantType::TinyString);
|
||||
REQUIRE(data.type == VariantType::TinyString);
|
||||
REQUIRE(data.asString() == "url");
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
#include <ArduinoJson.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
#include <ArduinoJson/Memory/Alignment.hpp>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
|
||||
using namespace ArduinoJson::detail;
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Memory/ResourceManager.hpp>
|
||||
#include <ArduinoJson/Memory/ResourceManagerImpl.hpp>
|
||||
#include <ArduinoJson/Strings/StringAdapters.hpp>
|
||||
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ArduinoJson::detail;
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Memory/ResourceManager.hpp>
|
||||
#include <ArduinoJson/Memory/ResourceManagerImpl.hpp>
|
||||
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Memory/ResourceManager.hpp>
|
||||
#include <ArduinoJson/Memory/ResourceManagerImpl.hpp>
|
||||
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
#include <ArduinoJson/Memory/Alignment.hpp>
|
||||
#include <ArduinoJson/Memory/ResourceManager.hpp>
|
||||
#include <ArduinoJson/Memory/ResourceManagerImpl.hpp>
|
||||
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
|
||||
@@ -45,13 +45,11 @@
|
||||
#include "ArduinoJson/Array/ElementProxy.hpp"
|
||||
#include "ArduinoJson/Array/Utilities.hpp"
|
||||
#include "ArduinoJson/Collection/CollectionImpl.hpp"
|
||||
#include "ArduinoJson/Memory/ResourceManagerImpl.hpp"
|
||||
#include "ArduinoJson/Object/MemberProxy.hpp"
|
||||
#include "ArduinoJson/Object/ObjectImpl.hpp"
|
||||
#include "ArduinoJson/Variant/ConverterImpl.hpp"
|
||||
#include "ArduinoJson/Variant/JsonVariantCopier.hpp"
|
||||
#include "ArduinoJson/Variant/VariantCompare.hpp"
|
||||
#include "ArduinoJson/Variant/VariantImpl.hpp"
|
||||
#include "ArduinoJson/Variant/VariantRefBaseImpl.hpp"
|
||||
|
||||
#include "ArduinoJson/Json/JsonDeserializer.hpp"
|
||||
|
||||
@@ -1,66 +0,0 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2025, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson/Collection/CollectionData.hpp>
|
||||
|
||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
|
||||
class ArrayData : public CollectionData {
|
||||
public:
|
||||
VariantData* addElement(ResourceManager* resources);
|
||||
|
||||
static VariantData* addElement(ArrayData* array, ResourceManager* resources) {
|
||||
if (!array)
|
||||
return nullptr;
|
||||
return array->addElement(resources);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool addValue(const T& value, ResourceManager* resources);
|
||||
|
||||
template <typename T>
|
||||
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 ResourceManager* resources) const;
|
||||
|
||||
static VariantData* getElement(const ArrayData* array, size_t index,
|
||||
const ResourceManager* resources) {
|
||||
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:
|
||||
iterator at(size_t index, const ResourceManager* resources) const;
|
||||
};
|
||||
|
||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||
@@ -4,42 +4,47 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson/Array/ArrayData.hpp>
|
||||
#include <ArduinoJson/Variant/VariantCompare.hpp>
|
||||
#include <ArduinoJson/Variant/VariantData.hpp>
|
||||
#include <ArduinoJson/Variant/VariantImpl.hpp>
|
||||
|
||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
|
||||
inline ArrayData::iterator ArrayData::at(
|
||||
size_t index, const ResourceManager* resources) const {
|
||||
auto it = createIterator(resources);
|
||||
inline VariantImpl::iterator VariantImpl::at(size_t index) const {
|
||||
if (!isArray())
|
||||
return iterator();
|
||||
|
||||
auto it = createIterator();
|
||||
while (!it.done() && index) {
|
||||
it.next(resources);
|
||||
it.move(resources_);
|
||||
--index;
|
||||
}
|
||||
return it;
|
||||
}
|
||||
|
||||
inline VariantData* ArrayData::addElement(ResourceManager* resources) {
|
||||
inline VariantData* VariantImpl::addNewElement(VariantData* data,
|
||||
ResourceManager* resources) {
|
||||
ARDUINOJSON_ASSERT(data != nullptr);
|
||||
ARDUINOJSON_ASSERT(data->isArray());
|
||||
ARDUINOJSON_ASSERT(resources != nullptr);
|
||||
|
||||
auto slot = resources->allocVariant();
|
||||
if (!slot)
|
||||
return nullptr;
|
||||
CollectionData::appendOne(slot, resources);
|
||||
addElement(slot, data, resources);
|
||||
return slot.ptr();
|
||||
}
|
||||
|
||||
inline VariantData* ArrayData::getOrAddElement(size_t index,
|
||||
ResourceManager* resources) {
|
||||
auto it = createIterator(resources);
|
||||
inline VariantData* VariantImpl::getOrAddElement(size_t index) {
|
||||
auto it = createIterator();
|
||||
while (!it.done() && index > 0) {
|
||||
it.next(resources);
|
||||
it.move(resources_);
|
||||
index--;
|
||||
}
|
||||
if (it.done())
|
||||
index++;
|
||||
VariantData* element = it.data();
|
||||
while (index > 0) {
|
||||
element = addElement(resources);
|
||||
element = addNewElement();
|
||||
if (!element)
|
||||
return nullptr;
|
||||
index--;
|
||||
@@ -47,33 +52,17 @@ inline VariantData* ArrayData::getOrAddElement(size_t index,
|
||||
return element;
|
||||
}
|
||||
|
||||
inline VariantData* ArrayData::getElement(
|
||||
size_t index, const ResourceManager* resources) const {
|
||||
return at(index, resources).data();
|
||||
inline VariantData* VariantImpl::getElement(size_t index) const {
|
||||
return at(index).data();
|
||||
}
|
||||
|
||||
inline void ArrayData::removeElement(size_t index, ResourceManager* resources) {
|
||||
remove(at(index, resources), resources);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline bool ArrayData::addValue(const T& value, ResourceManager* resources) {
|
||||
ARDUINOJSON_ASSERT(resources != nullptr);
|
||||
auto slot = resources->allocVariant();
|
||||
if (!slot)
|
||||
return false;
|
||||
JsonVariant variant(slot.ptr(), resources);
|
||||
if (!variant.set(value)) {
|
||||
resources->freeVariant(slot);
|
||||
return false;
|
||||
}
|
||||
CollectionData::appendOne(slot, resources);
|
||||
return true;
|
||||
inline void VariantImpl::removeElement(size_t index) {
|
||||
removeElement(at(index));
|
||||
}
|
||||
|
||||
// Returns the size (in bytes) of an array with n elements.
|
||||
constexpr size_t sizeofArray(size_t n) {
|
||||
return n * ResourceManager::slotSize;
|
||||
return n * sizeof(VariantData);
|
||||
}
|
||||
|
||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||
|
||||
@@ -55,17 +55,15 @@ class ElementProxy : public VariantRefBase<ElementProxy<TUpstream>>,
|
||||
}
|
||||
|
||||
FORCE_INLINE VariantData* getData() const {
|
||||
return VariantData::getElement(
|
||||
VariantAttorney::getData(upstream_), index_,
|
||||
VariantAttorney::getResourceManager(upstream_));
|
||||
return VariantAttorney::getVariantImpl(upstream_).getElement(index_);
|
||||
}
|
||||
|
||||
VariantData* getOrCreateData() const {
|
||||
auto data = VariantAttorney::getOrCreateData(upstream_);
|
||||
if (!data)
|
||||
return nullptr;
|
||||
return data->getOrAddElement(
|
||||
index_, VariantAttorney::getResourceManager(upstream_));
|
||||
auto resources = VariantAttorney::getResourceManager(upstream_);
|
||||
if (data && data->type == VariantType::Null)
|
||||
data->toArray();
|
||||
return VariantImpl(data, resources).getOrAddElement(index_);
|
||||
}
|
||||
|
||||
TUpstream upstream_;
|
||||
|
||||
@@ -20,24 +20,25 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
|
||||
using iterator = JsonArrayIterator;
|
||||
|
||||
// Constructs an unbound reference.
|
||||
JsonArray() : data_(0), resources_(0) {}
|
||||
JsonArray() {}
|
||||
|
||||
// INTERNAL USE ONLY
|
||||
JsonArray(detail::ArrayData* data, detail::ResourceManager* resources)
|
||||
: data_(data), resources_(resources) {}
|
||||
JsonArray(detail::VariantData* data, detail::ResourceManager* resources)
|
||||
: impl_(data, resources) {}
|
||||
|
||||
// INTERNAL USE ONLY
|
||||
JsonArray(const detail::VariantImpl& impl) : impl_(impl) {}
|
||||
|
||||
// Returns a JsonVariant pointing to the array.
|
||||
// https://arduinojson.org/v7/api/jsonvariant/
|
||||
operator JsonVariant() {
|
||||
void* data = data_; // prevent warning cast-align
|
||||
return JsonVariant(reinterpret_cast<detail::VariantData*>(data),
|
||||
resources_);
|
||||
return JsonVariant(getData(), getResourceManager());
|
||||
}
|
||||
|
||||
// Returns a read-only reference to the array.
|
||||
// https://arduinojson.org/v7/api/jsonarrayconst/
|
||||
operator JsonArrayConst() const {
|
||||
return JsonArrayConst(data_, resources_);
|
||||
return JsonArrayConst(getData(), getResourceManager());
|
||||
}
|
||||
|
||||
// Appends a new (empty) element to the array.
|
||||
@@ -55,15 +56,16 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
|
||||
template <typename T, detail::enable_if_t<
|
||||
detail::is_same<T, JsonVariant>::value, int> = 0>
|
||||
JsonVariant add() const {
|
||||
return JsonVariant(detail::ArrayData::addElement(data_, resources_),
|
||||
resources_);
|
||||
return JsonVariant(impl_.addNewElement(), impl_.resources());
|
||||
}
|
||||
|
||||
// Appends a value to the array.
|
||||
// https://arduinojson.org/v7/api/jsonarray/add/
|
||||
template <typename T>
|
||||
bool add(const T& value) const {
|
||||
return detail::ArrayData::addValue(data_, value, resources_);
|
||||
if (!impl_.isArray())
|
||||
return false;
|
||||
return addValue(value, impl_.data(), impl_.resources());
|
||||
}
|
||||
|
||||
// Appends a value to the array.
|
||||
@@ -71,15 +73,15 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
|
||||
template <typename T,
|
||||
detail::enable_if_t<!detail::is_const<T>::value, int> = 0>
|
||||
bool add(T* value) const {
|
||||
return detail::ArrayData::addValue(data_, value, resources_);
|
||||
if (!impl_.isArray())
|
||||
return false;
|
||||
return addValue(value, impl_.data(), impl_.resources());
|
||||
}
|
||||
|
||||
// Returns an iterator to the first element of the array.
|
||||
// https://arduinojson.org/v7/api/jsonarray/begin/
|
||||
iterator begin() const {
|
||||
if (!data_)
|
||||
return iterator();
|
||||
return iterator(data_->createIterator(resources_), resources_);
|
||||
return iterator(impl_.createIterator(), impl_.resources());
|
||||
}
|
||||
|
||||
// Returns an iterator following the last element of the array.
|
||||
@@ -91,9 +93,6 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
|
||||
// Copies an array.
|
||||
// https://arduinojson.org/v7/api/jsonarray/set/
|
||||
bool set(JsonArrayConst src) const {
|
||||
if (!data_)
|
||||
return false;
|
||||
|
||||
clear();
|
||||
for (auto element : src) {
|
||||
if (!add(element))
|
||||
@@ -106,13 +105,13 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
|
||||
// Removes the element at the specified iterator.
|
||||
// https://arduinojson.org/v7/api/jsonarray/remove/
|
||||
void remove(iterator it) const {
|
||||
detail::ArrayData::remove(data_, it.iterator_, resources_);
|
||||
impl_.removeElement(it.iterator_);
|
||||
}
|
||||
|
||||
// Removes the element at the specified index.
|
||||
// https://arduinojson.org/v7/api/jsonarray/remove/
|
||||
void remove(size_t index) const {
|
||||
detail::ArrayData::removeElement(data_, index, resources_);
|
||||
impl_.removeElement(index);
|
||||
}
|
||||
|
||||
// Removes the element at the specified index.
|
||||
@@ -127,7 +126,7 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
|
||||
// Removes all the elements of the array.
|
||||
// https://arduinojson.org/v7/api/jsonarray/clear/
|
||||
void clear() const {
|
||||
detail::ArrayData::clear(data_, resources_);
|
||||
impl_.empty();
|
||||
}
|
||||
|
||||
// Gets or sets the element at the specified index.
|
||||
@@ -150,31 +149,31 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
|
||||
}
|
||||
|
||||
operator JsonVariantConst() const {
|
||||
return JsonVariantConst(collectionToVariant(data_), resources_);
|
||||
return JsonVariantConst(getData(), getResourceManager());
|
||||
}
|
||||
|
||||
// Returns true if the reference is unbound.
|
||||
// https://arduinojson.org/v7/api/jsonarray/isnull/
|
||||
bool isNull() const {
|
||||
return data_ == 0;
|
||||
return impl_.isNull();
|
||||
}
|
||||
|
||||
// Returns true if the reference is bound.
|
||||
// https://arduinojson.org/v7/api/jsonarray/isnull/
|
||||
operator bool() const {
|
||||
return data_ != 0;
|
||||
return !isNull();
|
||||
}
|
||||
|
||||
// Returns the depth (nesting level) of the array.
|
||||
// https://arduinojson.org/v7/api/jsonarray/nesting/
|
||||
size_t nesting() const {
|
||||
return detail::VariantData::nesting(collectionToVariant(data_), resources_);
|
||||
return impl_.nesting();
|
||||
}
|
||||
|
||||
// Returns the number of elements in the array.
|
||||
// https://arduinojson.org/v7/api/jsonarray/size/
|
||||
size_t size() const {
|
||||
return data_ ? data_->size(resources_) : 0;
|
||||
return impl_.size();
|
||||
}
|
||||
|
||||
// DEPRECATED: use add<JsonVariant>() instead
|
||||
@@ -201,19 +200,40 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
|
||||
|
||||
private:
|
||||
detail::ResourceManager* getResourceManager() const {
|
||||
return resources_;
|
||||
return impl_.resources();
|
||||
}
|
||||
|
||||
detail::VariantData* getData() const {
|
||||
return collectionToVariant(data_);
|
||||
return impl_.data();
|
||||
}
|
||||
|
||||
detail::VariantData* getOrCreateData() const {
|
||||
return collectionToVariant(data_);
|
||||
return impl_.data();
|
||||
}
|
||||
|
||||
detail::ArrayData* data_;
|
||||
detail::ResourceManager* resources_;
|
||||
// HACK: this function has been pulled out of VariantImpl to avoid the
|
||||
// circular dependency between VariantImpl and JsonVariant
|
||||
template <typename T>
|
||||
static bool addValue(const T& value, detail::VariantData* data,
|
||||
detail::ResourceManager* resources) {
|
||||
ARDUINOJSON_ASSERT(data != nullptr);
|
||||
ARDUINOJSON_ASSERT(data->isArray());
|
||||
ARDUINOJSON_ASSERT(resources != nullptr);
|
||||
|
||||
auto slot = resources->allocVariant();
|
||||
if (!slot)
|
||||
return false;
|
||||
|
||||
if (!JsonVariant(slot.ptr(), resources).set(value)) {
|
||||
detail::VariantImpl::freeVariant(slot, resources);
|
||||
return false;
|
||||
}
|
||||
|
||||
detail::VariantImpl::addElement(slot, data, resources);
|
||||
return true;
|
||||
}
|
||||
|
||||
mutable detail::VariantImpl impl_;
|
||||
};
|
||||
|
||||
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
||||
|
||||
@@ -24,9 +24,7 @@ class JsonArrayConst : public detail::VariantOperators<JsonArrayConst> {
|
||||
// Returns an iterator to the first element of the array.
|
||||
// https://arduinojson.org/v7/api/jsonarrayconst/begin/
|
||||
iterator begin() const {
|
||||
if (!data_)
|
||||
return iterator();
|
||||
return iterator(data_->createIterator(resources_), resources_);
|
||||
return iterator(impl_.createIterator(), impl_.resources());
|
||||
}
|
||||
|
||||
// Returns an iterator to the element following the last element of the array.
|
||||
@@ -36,21 +34,21 @@ class JsonArrayConst : public detail::VariantOperators<JsonArrayConst> {
|
||||
}
|
||||
|
||||
// Creates an unbound reference.
|
||||
JsonArrayConst() : data_(0), resources_(0) {}
|
||||
JsonArrayConst() {}
|
||||
|
||||
// INTERNAL USE ONLY
|
||||
JsonArrayConst(const detail::ArrayData* data,
|
||||
const detail::ResourceManager* resources)
|
||||
: data_(data), resources_(resources) {}
|
||||
JsonArrayConst(detail::VariantData* data, detail::ResourceManager* resources)
|
||||
: impl_(data, resources) {}
|
||||
|
||||
// INTERNAL USE ONLY
|
||||
JsonArrayConst(const detail::VariantImpl& impl) : impl_(impl) {}
|
||||
|
||||
// Returns the element at the specified index.
|
||||
// https://arduinojson.org/v7/api/jsonarrayconst/subscript/
|
||||
template <typename T,
|
||||
detail::enable_if_t<detail::is_integral<T>::value, int> = 0>
|
||||
JsonVariantConst operator[](T index) const {
|
||||
return JsonVariantConst(
|
||||
detail::ArrayData::getElement(data_, size_t(index), resources_),
|
||||
resources_);
|
||||
return JsonVariantConst(impl_.getElement(size_t(index)), impl_.resources());
|
||||
}
|
||||
|
||||
// Returns the element at the specified index.
|
||||
@@ -65,31 +63,31 @@ class JsonArrayConst : public detail::VariantOperators<JsonArrayConst> {
|
||||
}
|
||||
|
||||
operator JsonVariantConst() const {
|
||||
return JsonVariantConst(getData(), resources_);
|
||||
return JsonVariantConst(impl_.data(), impl_.resources());
|
||||
}
|
||||
|
||||
// Returns true if the reference is unbound.
|
||||
// https://arduinojson.org/v7/api/jsonarrayconst/isnull/
|
||||
bool isNull() const {
|
||||
return data_ == 0;
|
||||
return impl_.isNull();
|
||||
}
|
||||
|
||||
// Returns true if the reference is bound.
|
||||
// https://arduinojson.org/v7/api/jsonarrayconst/isnull/
|
||||
operator bool() const {
|
||||
return data_ != 0;
|
||||
return !isNull();
|
||||
}
|
||||
|
||||
// Returns the depth (nesting level) of the array.
|
||||
// https://arduinojson.org/v7/api/jsonarrayconst/nesting/
|
||||
size_t nesting() const {
|
||||
return detail::VariantData::nesting(getData(), resources_);
|
||||
return impl_.nesting();
|
||||
}
|
||||
|
||||
// Returns the number of elements in the array.
|
||||
// https://arduinojson.org/v7/api/jsonarrayconst/size/
|
||||
size_t size() const {
|
||||
return data_ ? data_->size(resources_) : 0;
|
||||
return impl_.size();
|
||||
}
|
||||
|
||||
// DEPRECATED: always returns zero
|
||||
@@ -100,11 +98,10 @@ class JsonArrayConst : public detail::VariantOperators<JsonArrayConst> {
|
||||
|
||||
private:
|
||||
const detail::VariantData* getData() const {
|
||||
return collectionToVariant(data_);
|
||||
return impl_.data();
|
||||
}
|
||||
|
||||
const detail::ArrayData* data_;
|
||||
const detail::ResourceManager* resources_;
|
||||
detail::VariantImpl impl_;
|
||||
};
|
||||
|
||||
// Compares the content of two arrays.
|
||||
|
||||
@@ -30,7 +30,7 @@ class JsonArrayIterator {
|
||||
|
||||
public:
|
||||
JsonArrayIterator() {}
|
||||
explicit JsonArrayIterator(detail::ArrayData::iterator iterator,
|
||||
explicit JsonArrayIterator(detail::CollectionIterator iterator,
|
||||
detail::ResourceManager* resources)
|
||||
: iterator_(iterator), resources_(resources) {}
|
||||
|
||||
@@ -50,12 +50,12 @@ class JsonArrayIterator {
|
||||
}
|
||||
|
||||
JsonArrayIterator& operator++() {
|
||||
iterator_.next(resources_);
|
||||
iterator_.move(resources_);
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
detail::ArrayData::iterator iterator_;
|
||||
detail::CollectionIterator iterator_;
|
||||
detail::ResourceManager* resources_;
|
||||
};
|
||||
|
||||
@@ -64,8 +64,8 @@ class JsonArrayConstIterator {
|
||||
|
||||
public:
|
||||
JsonArrayConstIterator() {}
|
||||
explicit JsonArrayConstIterator(detail::ArrayData::iterator iterator,
|
||||
const detail::ResourceManager* resources)
|
||||
explicit JsonArrayConstIterator(detail::CollectionIterator iterator,
|
||||
detail::ResourceManager* resources)
|
||||
: iterator_(iterator), resources_(resources) {}
|
||||
|
||||
JsonVariantConst operator*() const {
|
||||
@@ -84,13 +84,13 @@ class JsonArrayConstIterator {
|
||||
}
|
||||
|
||||
JsonArrayConstIterator& operator++() {
|
||||
iterator_.next(resources_);
|
||||
iterator_.move(resources_);
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
detail::ArrayData::iterator iterator_;
|
||||
const detail::ResourceManager* resources_;
|
||||
mutable detail::CollectionIterator iterator_;
|
||||
mutable detail::ResourceManager* resources_;
|
||||
};
|
||||
|
||||
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
||||
|
||||
@@ -1,123 +0,0 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2025, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson/Memory/MemoryPool.hpp>
|
||||
#include <ArduinoJson/Namespace.hpp>
|
||||
#include <ArduinoJson/Polyfills/assert.hpp>
|
||||
|
||||
#include <stddef.h> // size_t
|
||||
|
||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
|
||||
class VariantData;
|
||||
class ResourceManager;
|
||||
|
||||
class CollectionIterator {
|
||||
friend class CollectionData;
|
||||
|
||||
public:
|
||||
CollectionIterator() : slot_(nullptr), currentId_(NULL_SLOT) {}
|
||||
|
||||
void next(const ResourceManager* resources);
|
||||
|
||||
bool done() const {
|
||||
return slot_ == nullptr;
|
||||
}
|
||||
|
||||
bool operator==(const CollectionIterator& other) const {
|
||||
return slot_ == other.slot_;
|
||||
}
|
||||
|
||||
bool operator!=(const CollectionIterator& other) const {
|
||||
return slot_ != other.slot_;
|
||||
}
|
||||
|
||||
VariantData* operator->() {
|
||||
ARDUINOJSON_ASSERT(slot_ != nullptr);
|
||||
return data();
|
||||
}
|
||||
|
||||
VariantData& operator*() {
|
||||
ARDUINOJSON_ASSERT(slot_ != nullptr);
|
||||
return *data();
|
||||
}
|
||||
|
||||
const VariantData& operator*() const {
|
||||
ARDUINOJSON_ASSERT(slot_ != nullptr);
|
||||
return *data();
|
||||
}
|
||||
|
||||
VariantData* data() {
|
||||
return reinterpret_cast<VariantData*>(slot_);
|
||||
}
|
||||
|
||||
const VariantData* data() const {
|
||||
return reinterpret_cast<const VariantData*>(slot_);
|
||||
}
|
||||
|
||||
private:
|
||||
CollectionIterator(VariantData* slot, SlotId slotId)
|
||||
: slot_(slot), currentId_(slotId) {}
|
||||
|
||||
VariantData* slot_;
|
||||
SlotId currentId_;
|
||||
};
|
||||
|
||||
class CollectionData {
|
||||
SlotId head_ = NULL_SLOT;
|
||||
SlotId tail_ = NULL_SLOT;
|
||||
|
||||
public:
|
||||
// Placement new
|
||||
static void* operator new(size_t, void* p) noexcept {
|
||||
return p;
|
||||
}
|
||||
|
||||
static void operator delete(void*, void*) noexcept {}
|
||||
|
||||
using iterator = CollectionIterator;
|
||||
|
||||
iterator createIterator(const ResourceManager* resources) const;
|
||||
|
||||
size_t size(const ResourceManager*) const;
|
||||
size_t nesting(const ResourceManager*) const;
|
||||
|
||||
void clear(ResourceManager* resources);
|
||||
|
||||
static void clear(CollectionData* collection, ResourceManager* resources) {
|
||||
if (!collection)
|
||||
return;
|
||||
collection->clear(resources);
|
||||
}
|
||||
|
||||
SlotId head() const {
|
||||
return head_;
|
||||
}
|
||||
|
||||
protected:
|
||||
void appendOne(Slot<VariantData> slot, const ResourceManager* resources);
|
||||
void appendPair(Slot<VariantData> key, Slot<VariantData> value,
|
||||
const ResourceManager* resources);
|
||||
|
||||
void removeOne(iterator it, ResourceManager* resources);
|
||||
void removePair(iterator it, ResourceManager* resources);
|
||||
|
||||
private:
|
||||
Slot<VariantData> getPreviousSlot(VariantData*, const ResourceManager*) 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
|
||||
@@ -4,128 +4,144 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson/Collection/CollectionData.hpp>
|
||||
#include <ArduinoJson/Memory/Alignment.hpp>
|
||||
#include <ArduinoJson/Strings/StringAdapters.hpp>
|
||||
#include <ArduinoJson/Variant/VariantCompare.hpp>
|
||||
#include <ArduinoJson/Variant/VariantData.hpp>
|
||||
#include <ArduinoJson/Variant/VariantImpl.hpp>
|
||||
|
||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
|
||||
inline void CollectionIterator::next(const ResourceManager* resources) {
|
||||
inline void CollectionIterator::move(const ResourceManager* resources) {
|
||||
ARDUINOJSON_ASSERT(slot_);
|
||||
auto nextId = slot_->next();
|
||||
auto nextId = slot_->next;
|
||||
slot_ = resources->getVariant(nextId);
|
||||
currentId_ = nextId;
|
||||
}
|
||||
|
||||
inline CollectionData::iterator CollectionData::createIterator(
|
||||
const ResourceManager* resources) const {
|
||||
return iterator(resources->getVariant(head_), head_);
|
||||
inline VariantImpl::iterator VariantImpl::createIterator(
|
||||
VariantData* data, ResourceManager* resources) {
|
||||
ARDUINOJSON_ASSERT(data != nullptr);
|
||||
ARDUINOJSON_ASSERT(data->isCollection());
|
||||
ARDUINOJSON_ASSERT(resources != nullptr);
|
||||
auto head = data->content.asCollection.head;
|
||||
return iterator(resources->getVariant(head), head);
|
||||
}
|
||||
|
||||
inline void CollectionData::appendOne(Slot<VariantData> slot,
|
||||
const ResourceManager* resources) {
|
||||
if (tail_ != NULL_SLOT) {
|
||||
auto tail = resources->getVariant(tail_);
|
||||
tail->setNext(slot.id());
|
||||
tail_ = slot.id();
|
||||
inline void VariantImpl::addElement(Slot<VariantData> slot, VariantData* data,
|
||||
ResourceManager* resources) {
|
||||
ARDUINOJSON_ASSERT(data != nullptr);
|
||||
ARDUINOJSON_ASSERT(data->isCollection());
|
||||
ARDUINOJSON_ASSERT(resources != nullptr);
|
||||
|
||||
auto coll = &data->content.asCollection;
|
||||
|
||||
if (coll->tail != NULL_SLOT) {
|
||||
auto tail = resources->getVariant(coll->tail);
|
||||
tail->next = slot.id();
|
||||
coll->tail = slot.id();
|
||||
} else {
|
||||
head_ = slot.id();
|
||||
tail_ = slot.id();
|
||||
coll->head = slot.id();
|
||||
coll->tail = slot.id();
|
||||
}
|
||||
}
|
||||
|
||||
inline void CollectionData::appendPair(Slot<VariantData> key,
|
||||
Slot<VariantData> value,
|
||||
const ResourceManager* resources) {
|
||||
key->setNext(value.id());
|
||||
inline void VariantImpl::appendPair(Slot<VariantData> key,
|
||||
Slot<VariantData> value, VariantData* data,
|
||||
ResourceManager* resources) {
|
||||
ARDUINOJSON_ASSERT(data != nullptr);
|
||||
ARDUINOJSON_ASSERT(resources != nullptr);
|
||||
|
||||
if (tail_ != NULL_SLOT) {
|
||||
auto tail = resources->getVariant(tail_);
|
||||
tail->setNext(key.id());
|
||||
tail_ = value.id();
|
||||
key->next = value.id();
|
||||
|
||||
auto coll = &data->content.asCollection;
|
||||
|
||||
if (coll->tail != NULL_SLOT) {
|
||||
auto tail = resources->getVariant(coll->tail);
|
||||
tail->next = key.id();
|
||||
coll->tail = value.id();
|
||||
} else {
|
||||
head_ = key.id();
|
||||
tail_ = value.id();
|
||||
coll->head = key.id();
|
||||
coll->tail = value.id();
|
||||
}
|
||||
}
|
||||
|
||||
inline void CollectionData::clear(ResourceManager* resources) {
|
||||
auto next = head_;
|
||||
inline void VariantImpl::empty(VariantData* data, ResourceManager* resources) {
|
||||
ARDUINOJSON_ASSERT(data != nullptr);
|
||||
ARDUINOJSON_ASSERT(data->isCollection());
|
||||
ARDUINOJSON_ASSERT(resources != nullptr);
|
||||
|
||||
auto coll = &data->content.asCollection;
|
||||
|
||||
auto next = coll->head;
|
||||
while (next != NULL_SLOT) {
|
||||
auto currId = next;
|
||||
auto slot = resources->getVariant(next);
|
||||
next = slot->next();
|
||||
resources->freeVariant({slot, currId});
|
||||
next = slot->next;
|
||||
freeVariant({slot, currId}, resources);
|
||||
}
|
||||
|
||||
head_ = NULL_SLOT;
|
||||
tail_ = NULL_SLOT;
|
||||
coll->head = NULL_SLOT;
|
||||
coll->tail = NULL_SLOT;
|
||||
}
|
||||
|
||||
inline Slot<VariantData> CollectionData::getPreviousSlot(
|
||||
VariantData* target, const ResourceManager* resources) const {
|
||||
inline Slot<VariantData> VariantImpl::getPreviousSlot(
|
||||
VariantData* target) const {
|
||||
ARDUINOJSON_ASSERT(data_ != nullptr);
|
||||
ARDUINOJSON_ASSERT(data_->isCollection());
|
||||
ARDUINOJSON_ASSERT(resources_ != nullptr);
|
||||
|
||||
auto prev = Slot<VariantData>();
|
||||
auto currentId = head_;
|
||||
auto currentId = data_->content.asCollection.head;
|
||||
while (currentId != NULL_SLOT) {
|
||||
auto currentSlot = resources->getVariant(currentId);
|
||||
auto currentSlot = resources_->getVariant(currentId);
|
||||
if (currentSlot == target)
|
||||
break;
|
||||
prev = Slot<VariantData>(currentSlot, currentId);
|
||||
currentId = currentSlot->next();
|
||||
currentId = currentSlot->next;
|
||||
}
|
||||
return prev;
|
||||
}
|
||||
|
||||
inline void CollectionData::removeOne(iterator it, ResourceManager* resources) {
|
||||
inline void VariantImpl::removeOne(iterator it) {
|
||||
if (it.done())
|
||||
return;
|
||||
auto curr = it.slot_;
|
||||
auto prev = getPreviousSlot(curr, resources);
|
||||
auto next = curr->next();
|
||||
auto prev = getPreviousSlot(curr);
|
||||
auto next = curr->next;
|
||||
auto coll = &data_->content.asCollection;
|
||||
if (prev)
|
||||
prev->setNext(next);
|
||||
prev->next = next;
|
||||
else
|
||||
head_ = next;
|
||||
coll->head = next;
|
||||
if (next == NULL_SLOT)
|
||||
tail_ = prev.id();
|
||||
resources->freeVariant({it.slot_, it.currentId_});
|
||||
coll->tail = prev.id();
|
||||
freeVariant({it.slot_, it.currentId_}, resources_);
|
||||
}
|
||||
|
||||
inline void CollectionData::removePair(ObjectData::iterator it,
|
||||
ResourceManager* resources) {
|
||||
inline void VariantImpl::removePair(iterator it) {
|
||||
if (it.done())
|
||||
return;
|
||||
|
||||
auto keySlot = it.data();
|
||||
auto keySlot = it.slot_;
|
||||
|
||||
auto valueId = keySlot->next();
|
||||
auto valueSlot = resources->getVariant(valueId);
|
||||
auto valueId = keySlot->next;
|
||||
auto valueSlot = resources_->getVariant(valueId);
|
||||
|
||||
// remove value slot
|
||||
keySlot->setNext(valueSlot->next());
|
||||
resources->freeVariant({valueSlot, valueId});
|
||||
keySlot->next = valueSlot->next;
|
||||
freeVariant({valueSlot, valueId}, resources_);
|
||||
|
||||
// remove key slot
|
||||
removeOne(it, resources);
|
||||
removeOne(it);
|
||||
}
|
||||
|
||||
inline size_t CollectionData::nesting(const ResourceManager* resources) const {
|
||||
inline size_t VariantImpl::nesting() const {
|
||||
if (!data_ || !data_->isCollection())
|
||||
return 0;
|
||||
size_t maxChildNesting = 0;
|
||||
for (auto it = createIterator(resources); !it.done(); it.next(resources)) {
|
||||
size_t childNesting = it->nesting(resources);
|
||||
for (auto it = createIterator(); !it.done(); it.move(resources_)) {
|
||||
size_t childNesting = VariantImpl(it.data(), resources_).nesting();
|
||||
if (childNesting > maxChildNesting)
|
||||
maxChildNesting = childNesting;
|
||||
}
|
||||
return maxChildNesting + 1;
|
||||
}
|
||||
|
||||
inline size_t CollectionData::size(const ResourceManager* resources) const {
|
||||
size_t count = 0;
|
||||
for (auto it = createIterator(resources); !it.done(); it.next(resources))
|
||||
count++;
|
||||
return count;
|
||||
}
|
||||
|
||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||
|
||||
68
src/ArduinoJson/Collection/CollectionIterator.hpp
Normal file
68
src/ArduinoJson/Collection/CollectionIterator.hpp
Normal file
@@ -0,0 +1,68 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2025, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson/Namespace.hpp>
|
||||
#include <ArduinoJson/Polyfills/assert.hpp>
|
||||
|
||||
#include <stddef.h> // size_t
|
||||
|
||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
|
||||
struct VariantData;
|
||||
class ResourceManager;
|
||||
|
||||
class CollectionIterator {
|
||||
friend class VariantImpl;
|
||||
|
||||
public:
|
||||
CollectionIterator() : slot_(nullptr), currentId_(NULL_SLOT) {}
|
||||
|
||||
void move(const ResourceManager* resources);
|
||||
|
||||
bool done() const {
|
||||
return slot_ == nullptr;
|
||||
}
|
||||
|
||||
bool operator==(const CollectionIterator& other) const {
|
||||
return slot_ == other.slot_;
|
||||
}
|
||||
|
||||
bool operator!=(const CollectionIterator& other) const {
|
||||
return slot_ != other.slot_;
|
||||
}
|
||||
|
||||
VariantData* operator->() {
|
||||
ARDUINOJSON_ASSERT(slot_ != nullptr);
|
||||
return data();
|
||||
}
|
||||
|
||||
VariantData& operator*() {
|
||||
ARDUINOJSON_ASSERT(slot_ != nullptr);
|
||||
return *data();
|
||||
}
|
||||
|
||||
const VariantData& operator*() const {
|
||||
ARDUINOJSON_ASSERT(slot_ != nullptr);
|
||||
return *data();
|
||||
}
|
||||
|
||||
VariantData* data() {
|
||||
return slot_;
|
||||
}
|
||||
|
||||
const VariantData* data() const {
|
||||
return slot_;
|
||||
}
|
||||
|
||||
private:
|
||||
CollectionIterator(VariantData* slot, SlotId slotId)
|
||||
: slot_(slot), currentId_(slotId) {}
|
||||
|
||||
VariantData* slot_;
|
||||
SlotId currentId_;
|
||||
};
|
||||
|
||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||
@@ -274,9 +274,9 @@
|
||||
#endif
|
||||
|
||||
#if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_DOUBLE
|
||||
# define ARDUINOJSON_USE_EXTENSIONS 1
|
||||
# define ARDUINOJSON_USE_8_BYTE_POOL 1
|
||||
#else
|
||||
# define ARDUINOJSON_USE_EXTENSIONS 0
|
||||
# define ARDUINOJSON_USE_8_BYTE_POOL 0
|
||||
#endif
|
||||
|
||||
#if defined(nullptr)
|
||||
|
||||
@@ -50,7 +50,7 @@ DeserializationError doDeserialize(TDestination&& dst, TReader reader,
|
||||
auto resources = VariantAttorney::getResourceManager(dst);
|
||||
dst.clear();
|
||||
auto err = TDeserializer<TReader>(resources, reader)
|
||||
.parse(*data, options.filter, options.nestingLimit);
|
||||
.parse(data, options.filter, options.nestingLimit);
|
||||
shrinkJsonDocument(dst);
|
||||
return err;
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@
|
||||
#include <ArduinoJson/Object/MemberProxy.hpp>
|
||||
#include <ArduinoJson/Polyfills/utility.hpp>
|
||||
#include <ArduinoJson/Variant/JsonVariantConst.hpp>
|
||||
#include <ArduinoJson/Variant/VariantTo.hpp>
|
||||
|
||||
ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE
|
||||
|
||||
@@ -88,7 +87,7 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
||||
// https://arduinojson.org/v7/api/jsondocument/clear/
|
||||
void clear() {
|
||||
resources_.clear();
|
||||
data_.reset();
|
||||
data_.type = detail::VariantType::Null;
|
||||
}
|
||||
|
||||
// Returns true if the root is of the specified type.
|
||||
@@ -120,13 +119,13 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
||||
// Returns the depth (nesting level) of the array.
|
||||
// https://arduinojson.org/v7/api/jsondocument/nesting/
|
||||
size_t nesting() const {
|
||||
return data_.nesting(&resources_);
|
||||
return getVariantImpl().nesting();
|
||||
}
|
||||
|
||||
// Returns the number of elements in the root array or object.
|
||||
// https://arduinojson.org/v7/api/jsondocument/size/
|
||||
size_t size() const {
|
||||
return data_.size(&resources_);
|
||||
return getVariantImpl().size();
|
||||
}
|
||||
|
||||
// Copies the specified document.
|
||||
@@ -146,18 +145,38 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
||||
|
||||
// Replaces the root with the specified value.
|
||||
// https://arduinojson.org/v7/api/jsondocument/set/
|
||||
template <typename TChar,
|
||||
detail::enable_if_t<!detail::is_const<TChar>::value, int> = 0>
|
||||
template <typename TChar>
|
||||
bool set(TChar* src) {
|
||||
return to<JsonVariant>().set(src);
|
||||
}
|
||||
|
||||
// Clears the document and converts it to the specified type.
|
||||
// Sets the document to an empty array.
|
||||
// https://arduinojson.org/v7/api/jsondocument/to/
|
||||
template <typename T>
|
||||
typename detail::VariantTo<T>::type to() {
|
||||
template <typename T,
|
||||
detail::enable_if_t<detail::is_same<T, JsonArray>::value, int> = 0>
|
||||
JsonArray to() {
|
||||
clear();
|
||||
return getVariant().template to<T>();
|
||||
data_.toArray();
|
||||
return JsonArray(&data_, &resources_);
|
||||
}
|
||||
|
||||
// Sets the document to an empty object.
|
||||
// https://arduinojson.org/v7/api/jsondocument/to/
|
||||
template <typename T,
|
||||
detail::enable_if_t<detail::is_same<T, JsonObject>::value, int> = 0>
|
||||
JsonObject to() {
|
||||
clear();
|
||||
data_.toObject();
|
||||
return JsonObject(&data_, &resources_);
|
||||
}
|
||||
|
||||
// Sets the document to null.
|
||||
// https://arduinojson.org/v7/api/jsondocument/to/
|
||||
template <typename T, detail::enable_if_t<
|
||||
detail::is_same<T, JsonVariant>::value, int> = 0>
|
||||
JsonVariant to() {
|
||||
clear();
|
||||
return JsonVariant(&data_, &resources_);
|
||||
}
|
||||
|
||||
// DEPRECATED: use obj["key"].is<T>() instead
|
||||
@@ -165,7 +184,7 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
||||
template <typename TChar>
|
||||
ARDUINOJSON_DEPRECATED("use doc[\"key\"].is<T>() instead")
|
||||
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
|
||||
@@ -174,7 +193,7 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
||||
detail::enable_if_t<detail::IsString<TString>::value, int> = 0>
|
||||
ARDUINOJSON_DEPRECATED("use doc[key].is<T>() instead")
|
||||
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
|
||||
@@ -198,9 +217,7 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
||||
// Gets or sets a root object's member.
|
||||
// https://arduinojson.org/v7/api/jsondocument/subscript/
|
||||
template <typename TChar,
|
||||
detail::enable_if_t<detail::IsString<TChar*>::value &&
|
||||
!detail::is_const<TChar>::value,
|
||||
int> = 0>
|
||||
detail::enable_if_t<detail::IsString<TChar*>::value, int> = 0>
|
||||
detail::MemberProxy<JsonDocument&, detail::AdaptedString<TChar*>> operator[](
|
||||
TChar* key) {
|
||||
return {*this, detail::adaptString(key)};
|
||||
@@ -212,18 +229,16 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
||||
detail::enable_if_t<detail::IsString<TString>::value, int> = 0>
|
||||
JsonVariantConst operator[](const TString& key) const {
|
||||
return JsonVariantConst(
|
||||
data_.getMember(detail::adaptString(key), &resources_), &resources_);
|
||||
getVariantImpl().getMember(detail::adaptString(key)), &resources_);
|
||||
}
|
||||
|
||||
// Gets a root object's member.
|
||||
// https://arduinojson.org/v7/api/jsondocument/subscript/
|
||||
template <typename TChar,
|
||||
detail::enable_if_t<detail::IsString<TChar*>::value &&
|
||||
!detail::is_const<TChar>::value,
|
||||
int> = 0>
|
||||
detail::enable_if_t<detail::IsString<TChar*>::value, int> = 0>
|
||||
JsonVariantConst operator[](TChar* key) const {
|
||||
return JsonVariantConst(
|
||||
data_.getMember(detail::adaptString(key), &resources_), &resources_);
|
||||
getVariantImpl().getMember(detail::adaptString(key)), &resources_);
|
||||
}
|
||||
|
||||
// Gets or sets a root array's element.
|
||||
@@ -237,7 +252,7 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
||||
// Gets a root array's member.
|
||||
// https://arduinojson.org/v7/api/jsondocument/subscript/
|
||||
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.
|
||||
@@ -267,22 +282,21 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
||||
template <typename T, detail::enable_if_t<
|
||||
detail::is_same<T, JsonVariant>::value, int> = 0>
|
||||
JsonVariant add() {
|
||||
return JsonVariant(data_.addElement(&resources_), &resources_);
|
||||
return getOrCreateArray().add<T>();
|
||||
}
|
||||
|
||||
// Appends a value to the root array.
|
||||
// https://arduinojson.org/v7/api/jsondocument/add/
|
||||
template <typename TValue>
|
||||
bool add(const TValue& value) {
|
||||
return data_.addValue(value, &resources_);
|
||||
return getOrCreateArray().add(value);
|
||||
}
|
||||
|
||||
// Appends a value to the root array.
|
||||
// https://arduinojson.org/v7/api/jsondocument/add/
|
||||
template <typename TChar,
|
||||
detail::enable_if_t<!detail::is_const<TChar>::value, int> = 0>
|
||||
template <typename TChar>
|
||||
bool add(TChar* value) {
|
||||
return data_.addValue(value, &resources_);
|
||||
return getOrCreateArray().add(value);
|
||||
}
|
||||
|
||||
// Removes an element of the root array.
|
||||
@@ -290,19 +304,15 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
||||
template <typename T,
|
||||
detail::enable_if_t<detail::is_integral<T>::value, int> = 0>
|
||||
void remove(T index) {
|
||||
detail::VariantData::removeElement(getData(), size_t(index),
|
||||
getResourceManager());
|
||||
getVariantImpl().removeElement(size_t(index));
|
||||
}
|
||||
|
||||
// Removes a member of the root object.
|
||||
// https://arduinojson.org/v7/api/jsondocument/remove/
|
||||
template <typename TChar,
|
||||
detail::enable_if_t<detail::IsString<TChar*>::value &&
|
||||
!detail::is_const<TChar>::value,
|
||||
int> = 0>
|
||||
detail::enable_if_t<detail::IsString<TChar*>::value, int> = 0>
|
||||
void remove(TChar* key) {
|
||||
detail::VariantData::removeMember(getData(), detail::adaptString(key),
|
||||
getResourceManager());
|
||||
getVariantImpl().removeMember(detail::adaptString(key));
|
||||
}
|
||||
|
||||
// Removes a member of the root object.
|
||||
@@ -310,8 +320,7 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
||||
template <typename TString,
|
||||
detail::enable_if_t<detail::IsString<TString>::value, int> = 0>
|
||||
void remove(const TString& key) {
|
||||
detail::VariantData::removeMember(getData(), detail::adaptString(key),
|
||||
getResourceManager());
|
||||
getVariantImpl().removeMember(detail::adaptString(key));
|
||||
}
|
||||
|
||||
// Removes a member of the root object or an element of the root array.
|
||||
@@ -391,6 +400,16 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
||||
}
|
||||
|
||||
private:
|
||||
detail::VariantImpl getVariantImpl() const {
|
||||
return detail::VariantImpl(&data_, &resources_);
|
||||
}
|
||||
|
||||
JsonArray getOrCreateArray() const {
|
||||
if (data_.type == detail::VariantType::Null)
|
||||
data_.toArray();
|
||||
return JsonArray(&data_, &resources_);
|
||||
}
|
||||
|
||||
JsonVariant getVariant() {
|
||||
return JsonVariant(&data_, &resources_);
|
||||
}
|
||||
@@ -415,12 +434,12 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
||||
return &data_;
|
||||
}
|
||||
|
||||
detail::ResourceManager resources_;
|
||||
detail::VariantData data_;
|
||||
mutable detail::ResourceManager resources_;
|
||||
mutable detail::VariantData data_;
|
||||
};
|
||||
|
||||
inline void convertToJson(const JsonDocument& src, JsonVariant dst) {
|
||||
dst.set(src.as<JsonVariantConst>());
|
||||
inline bool convertToJson(const JsonDocument& src, JsonVariant dst) {
|
||||
return dst.set(src.as<JsonVariantConst>());
|
||||
}
|
||||
|
||||
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
||||
|
||||
@@ -28,13 +28,13 @@ class JsonDeserializer {
|
||||
resources_(resources) {}
|
||||
|
||||
template <typename TFilter>
|
||||
DeserializationError parse(VariantData& variant, TFilter filter,
|
||||
DeserializationError parse(VariantData* variant, TFilter filter,
|
||||
DeserializationOption::NestingLimit nestingLimit) {
|
||||
DeserializationError::Code err;
|
||||
|
||||
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
|
||||
return DeserializationError::InvalidInput;
|
||||
}
|
||||
@@ -60,7 +60,7 @@ class JsonDeserializer {
|
||||
|
||||
template <typename TFilter>
|
||||
DeserializationError::Code parseVariant(
|
||||
VariantData& variant, TFilter filter,
|
||||
VariantData* variant, TFilter filter,
|
||||
DeserializationOption::NestingLimit nestingLimit) {
|
||||
DeserializationError::Code err;
|
||||
|
||||
@@ -71,13 +71,13 @@ class JsonDeserializer {
|
||||
switch (current()) {
|
||||
case '[':
|
||||
if (filter.allowArray())
|
||||
return parseArray(variant.toArray(), filter, nestingLimit);
|
||||
return parseArray(variant, filter, nestingLimit);
|
||||
else
|
||||
return skipArray(nestingLimit);
|
||||
|
||||
case '{':
|
||||
if (filter.allowObject())
|
||||
return parseObject(variant.toObject(), filter, nestingLimit);
|
||||
return parseObject(variant, filter, nestingLimit);
|
||||
else
|
||||
return skipObject(nestingLimit);
|
||||
|
||||
@@ -90,12 +90,12 @@ class JsonDeserializer {
|
||||
|
||||
case 't':
|
||||
if (filter.allowValue())
|
||||
variant.setBoolean(true);
|
||||
variant->setBoolean(true);
|
||||
return skipKeyword("true");
|
||||
|
||||
case 'f':
|
||||
if (filter.allowValue())
|
||||
variant.setBoolean(false);
|
||||
variant->setBoolean(false);
|
||||
return skipKeyword("false");
|
||||
|
||||
case 'n':
|
||||
@@ -146,10 +146,12 @@ class JsonDeserializer {
|
||||
|
||||
template <typename TFilter>
|
||||
DeserializationError::Code parseArray(
|
||||
ArrayData& array, TFilter filter,
|
||||
VariantData* array, TFilter filter,
|
||||
DeserializationOption::NestingLimit nestingLimit) {
|
||||
DeserializationError::Code err;
|
||||
|
||||
array->toArray();
|
||||
|
||||
if (nestingLimit.reached())
|
||||
return DeserializationError::TooDeep;
|
||||
|
||||
@@ -172,12 +174,12 @@ class JsonDeserializer {
|
||||
for (;;) {
|
||||
if (elementFilter.allow()) {
|
||||
// Allocate slot in array
|
||||
VariantData* value = array.addElement(resources_);
|
||||
VariantData* value = VariantImpl::addNewElement(array, resources_);
|
||||
if (!value)
|
||||
return DeserializationError::NoMemory;
|
||||
|
||||
// 1 - Parse value
|
||||
err = parseVariant(*value, elementFilter, nestingLimit.decrement());
|
||||
err = parseVariant(value, elementFilter, nestingLimit.decrement());
|
||||
if (err)
|
||||
return err;
|
||||
} else {
|
||||
@@ -232,10 +234,12 @@ class JsonDeserializer {
|
||||
|
||||
template <typename TFilter>
|
||||
DeserializationError::Code parseObject(
|
||||
ObjectData& object, TFilter filter,
|
||||
VariantData* object, TFilter filter,
|
||||
DeserializationOption::NestingLimit nestingLimit) {
|
||||
DeserializationError::Code err;
|
||||
|
||||
object->toObject();
|
||||
|
||||
if (nestingLimit.reached())
|
||||
return DeserializationError::TooDeep;
|
||||
|
||||
@@ -273,19 +277,20 @@ class JsonDeserializer {
|
||||
TFilter memberFilter = filter[key];
|
||||
|
||||
if (memberFilter.allow()) {
|
||||
auto member = object.getMember(adaptString(key), resources_);
|
||||
auto member =
|
||||
VariantImpl::getMember(adaptString(key), object, resources_);
|
||||
if (!member) {
|
||||
auto keyVariant = object.addPair(&member, resources_);
|
||||
auto keyVariant = VariantImpl::addPair(&member, object, resources_);
|
||||
if (!keyVariant)
|
||||
return DeserializationError::NoMemory;
|
||||
|
||||
stringBuilder_.save(keyVariant);
|
||||
} else {
|
||||
member->clear(resources_);
|
||||
VariantImpl::clear(member, resources_);
|
||||
}
|
||||
|
||||
// Parse value
|
||||
err = parseVariant(*member, memberFilter, nestingLimit.decrement());
|
||||
err = parseVariant(member, memberFilter, nestingLimit.decrement());
|
||||
if (err)
|
||||
return err;
|
||||
} else {
|
||||
@@ -379,7 +384,7 @@ class JsonDeserializer {
|
||||
}
|
||||
}
|
||||
|
||||
DeserializationError::Code parseStringValue(VariantData& variant) {
|
||||
DeserializationError::Code parseStringValue(VariantData* variant) {
|
||||
DeserializationError::Code err;
|
||||
|
||||
stringBuilder_.startString();
|
||||
@@ -388,7 +393,7 @@ class JsonDeserializer {
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
stringBuilder_.save(&variant);
|
||||
stringBuilder_.save(variant);
|
||||
|
||||
return DeserializationError::Ok;
|
||||
}
|
||||
@@ -504,7 +509,7 @@ class JsonDeserializer {
|
||||
return DeserializationError::Ok;
|
||||
}
|
||||
|
||||
DeserializationError::Code parseNumericValue(VariantData& result) {
|
||||
DeserializationError::Code parseNumericValue(VariantData* result) {
|
||||
uint8_t n = 0;
|
||||
|
||||
char c = current();
|
||||
@@ -518,26 +523,28 @@ class JsonDeserializer {
|
||||
auto number = parseNumber(buffer_);
|
||||
switch (number.type()) {
|
||||
case NumberType::UnsignedInteger:
|
||||
if (result.setInteger(number.asUnsignedInteger(), resources_))
|
||||
if (VariantImpl::setInteger(number.asUnsignedInteger(), result,
|
||||
resources_))
|
||||
return DeserializationError::Ok;
|
||||
else
|
||||
return DeserializationError::NoMemory;
|
||||
|
||||
case NumberType::SignedInteger:
|
||||
if (result.setInteger(number.asSignedInteger(), resources_))
|
||||
if (VariantImpl::setInteger(number.asSignedInteger(), result,
|
||||
resources_))
|
||||
return DeserializationError::Ok;
|
||||
else
|
||||
return DeserializationError::NoMemory;
|
||||
|
||||
case NumberType::Float:
|
||||
if (result.setFloat(number.asFloat(), resources_))
|
||||
if (VariantImpl::setFloat(number.asFloat(), result, resources_))
|
||||
return DeserializationError::Ok;
|
||||
else
|
||||
return DeserializationError::NoMemory;
|
||||
|
||||
#if ARDUINOJSON_USE_DOUBLE
|
||||
case NumberType::Double:
|
||||
if (result.setFloat(number.asDouble(), resources_))
|
||||
if (VariantImpl::setFloat(number.asDouble(), result, resources_))
|
||||
return DeserializationError::Ok;
|
||||
else
|
||||
return DeserializationError::NoMemory;
|
||||
|
||||
@@ -16,20 +16,23 @@ class JsonSerializer : public VariantDataVisitor<size_t> {
|
||||
public:
|
||||
static const bool producesText = true;
|
||||
|
||||
JsonSerializer(TWriter writer, const ResourceManager* resources)
|
||||
JsonSerializer(TWriter writer, ResourceManager* resources)
|
||||
: formatter_(writer), resources_(resources) {}
|
||||
|
||||
size_t visit(const ArrayData& array) {
|
||||
size_t visitArray(VariantData* array) {
|
||||
ARDUINOJSON_ASSERT(array != nullptr);
|
||||
ARDUINOJSON_ASSERT(array->isArray());
|
||||
|
||||
write('[');
|
||||
|
||||
auto slotId = array.head();
|
||||
auto slotId = array->content.asCollection.head;
|
||||
|
||||
while (slotId != NULL_SLOT) {
|
||||
auto slot = resources_->getVariant(slotId);
|
||||
|
||||
slot->accept(*this, resources_);
|
||||
VariantImpl::accept(*this, slot, resources_);
|
||||
|
||||
slotId = slot->next();
|
||||
slotId = slot->next;
|
||||
|
||||
if (slotId != NULL_SLOT)
|
||||
write(',');
|
||||
@@ -39,18 +42,21 @@ class JsonSerializer : public VariantDataVisitor<size_t> {
|
||||
return bytesWritten();
|
||||
}
|
||||
|
||||
size_t visit(const ObjectData& object) {
|
||||
size_t visitObject(VariantData* object) {
|
||||
ARDUINOJSON_ASSERT(object != nullptr);
|
||||
ARDUINOJSON_ASSERT(object->isObject());
|
||||
|
||||
write('{');
|
||||
|
||||
auto slotId = object.head();
|
||||
auto slotId = object->content.asCollection.head;
|
||||
|
||||
bool isKey = true;
|
||||
|
||||
while (slotId != NULL_SLOT) {
|
||||
auto slot = resources_->getVariant(slotId);
|
||||
slot->accept(*this, resources_);
|
||||
VariantImpl::accept(*this, slot, resources_);
|
||||
|
||||
slotId = slot->next();
|
||||
slotId = slot->next;
|
||||
|
||||
if (slotId != NULL_SLOT)
|
||||
write(isKey ? ':' : ',');
|
||||
@@ -120,7 +126,7 @@ class JsonSerializer : public VariantDataVisitor<size_t> {
|
||||
TextFormatter<TWriter> formatter_;
|
||||
|
||||
protected:
|
||||
const ResourceManager* resources_;
|
||||
ResourceManager* resources_;
|
||||
};
|
||||
|
||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||
|
||||
@@ -16,20 +16,24 @@ class PrettyJsonSerializer : public JsonSerializer<TWriter> {
|
||||
using base = JsonSerializer<TWriter>;
|
||||
|
||||
public:
|
||||
PrettyJsonSerializer(TWriter writer, const ResourceManager* resources)
|
||||
PrettyJsonSerializer(TWriter writer, ResourceManager* resources)
|
||||
: base(writer, resources), nesting_(0) {}
|
||||
|
||||
size_t visit(const ArrayData& array) {
|
||||
auto it = array.createIterator(base::resources_);
|
||||
if (!it.done()) {
|
||||
size_t visitArray(VariantData* array) {
|
||||
ARDUINOJSON_ASSERT(array != nullptr);
|
||||
ARDUINOJSON_ASSERT(array->isArray());
|
||||
|
||||
auto slotId = array->content.asCollection.head;
|
||||
if (slotId != NULL_SLOT) {
|
||||
base::write("[\r\n");
|
||||
nesting_++;
|
||||
while (!it.done()) {
|
||||
while (slotId != NULL_SLOT) {
|
||||
indent();
|
||||
it->accept(*this, base::resources_);
|
||||
auto slot = base::resources_->getVariant(slotId);
|
||||
VariantImpl::accept(*this, slot, base::resources_);
|
||||
|
||||
it.next(base::resources_);
|
||||
base::write(it.done() ? "\r\n" : ",\r\n");
|
||||
slotId = slot->next;
|
||||
base::write(slotId == NULL_SLOT ? "\r\n" : ",\r\n");
|
||||
}
|
||||
nesting_--;
|
||||
indent();
|
||||
@@ -40,21 +44,25 @@ class PrettyJsonSerializer : public JsonSerializer<TWriter> {
|
||||
return this->bytesWritten();
|
||||
}
|
||||
|
||||
size_t visit(const ObjectData& object) {
|
||||
auto it = object.createIterator(base::resources_);
|
||||
if (!it.done()) {
|
||||
size_t visitObject(VariantData* object) {
|
||||
ARDUINOJSON_ASSERT(object != nullptr);
|
||||
ARDUINOJSON_ASSERT(object->isObject());
|
||||
|
||||
auto slotId = object->content.asCollection.head;
|
||||
if (slotId != NULL_SLOT) {
|
||||
base::write("{\r\n");
|
||||
nesting_++;
|
||||
bool isKey = true;
|
||||
while (!it.done()) {
|
||||
while (slotId != NULL_SLOT) {
|
||||
if (isKey)
|
||||
indent();
|
||||
it->accept(*this, base::resources_);
|
||||
it.next(base::resources_);
|
||||
auto slot = base::resources_->getVariant(slotId);
|
||||
VariantImpl::accept(*this, slot, base::resources_);
|
||||
slotId = slot->next;
|
||||
if (isKey)
|
||||
base::write(": ");
|
||||
else
|
||||
base::write(it.done() ? "\r\n" : ",\r\n");
|
||||
base::write(slotId == NULL_SLOT ? "\r\n" : ",\r\n");
|
||||
isKey = !isKey;
|
||||
}
|
||||
nesting_--;
|
||||
|
||||
@@ -14,26 +14,19 @@
|
||||
|
||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
|
||||
class VariantData;
|
||||
class VariantWithId;
|
||||
|
||||
class ResourceManager {
|
||||
union SlotData {
|
||||
VariantData variant;
|
||||
#if ARDUINOJSON_USE_EXTENSIONS
|
||||
VariantExtension extension;
|
||||
#endif
|
||||
};
|
||||
|
||||
public:
|
||||
constexpr static size_t slotSize = sizeof(SlotData);
|
||||
|
||||
ResourceManager(Allocator* allocator = DefaultAllocator::instance())
|
||||
: allocator_(allocator), overflowed_(false) {}
|
||||
|
||||
~ResourceManager() {
|
||||
stringPool_.clear(allocator_);
|
||||
variantPools_.clear(allocator_);
|
||||
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||
eightBytePools_.clear(allocator_);
|
||||
#endif
|
||||
}
|
||||
|
||||
ResourceManager(const ResourceManager&) = delete;
|
||||
@@ -42,6 +35,9 @@ class ResourceManager {
|
||||
friend void swap(ResourceManager& a, ResourceManager& b) {
|
||||
swap(a.stringPool_, b.stringPool_);
|
||||
swap(a.variantPools_, b.variantPools_);
|
||||
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||
swap(a.eightBytePools_, b.eightBytePools_);
|
||||
#endif
|
||||
swap_(a.allocator_, b.allocator_);
|
||||
swap_(a.overflowed_, b.overflowed_);
|
||||
}
|
||||
@@ -58,14 +54,43 @@ class ResourceManager {
|
||||
return overflowed_;
|
||||
}
|
||||
|
||||
Slot<VariantData> allocVariant();
|
||||
void freeVariant(Slot<VariantData> slot);
|
||||
VariantData* getVariant(SlotId id) const;
|
||||
Slot<VariantData> allocVariant() {
|
||||
auto slot = variantPools_.allocSlot(allocator_);
|
||||
if (!slot) {
|
||||
overflowed_ = true;
|
||||
return {};
|
||||
}
|
||||
new (slot.ptr()) VariantData();
|
||||
return slot;
|
||||
}
|
||||
|
||||
#if ARDUINOJSON_USE_EXTENSIONS
|
||||
Slot<VariantExtension> allocExtension();
|
||||
void freeExtension(SlotId slot);
|
||||
VariantExtension* getExtension(SlotId id) const;
|
||||
void freeVariant(Slot<VariantData> slot) {
|
||||
ARDUINOJSON_ASSERT(slot->type == VariantType::Null);
|
||||
variantPools_.freeSlot(slot);
|
||||
}
|
||||
|
||||
VariantData* getVariant(SlotId id) const {
|
||||
return reinterpret_cast<VariantData*>(variantPools_.getSlot(id));
|
||||
}
|
||||
|
||||
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||
Slot<EightByteValue> allocEightByte() {
|
||||
auto slot = eightBytePools_.allocSlot(allocator_);
|
||||
if (!slot) {
|
||||
overflowed_ = true;
|
||||
return {};
|
||||
}
|
||||
return slot;
|
||||
}
|
||||
|
||||
void freeEightByte(SlotId id) {
|
||||
auto p = getEightByte(id);
|
||||
eightBytePools_.freeSlot({p, id});
|
||||
}
|
||||
|
||||
EightByteValue* getEightByte(SlotId id) const {
|
||||
return eightBytePools_.getSlot(id);
|
||||
}
|
||||
#endif
|
||||
|
||||
template <typename TAdaptedString>
|
||||
@@ -115,17 +140,26 @@ class ResourceManager {
|
||||
variantPools_.clear(allocator_);
|
||||
overflowed_ = false;
|
||||
stringPool_.clear(allocator_);
|
||||
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||
eightBytePools_.clear(allocator_);
|
||||
#endif
|
||||
}
|
||||
|
||||
void shrinkToFit() {
|
||||
variantPools_.shrinkToFit(allocator_);
|
||||
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||
eightBytePools_.shrinkToFit(allocator_);
|
||||
#endif
|
||||
}
|
||||
|
||||
private:
|
||||
Allocator* allocator_;
|
||||
bool overflowed_;
|
||||
StringPool stringPool_;
|
||||
MemoryPoolList<SlotData> variantPools_;
|
||||
MemoryPoolList<VariantData> variantPools_;
|
||||
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||
MemoryPoolList<EightByteValue> eightBytePools_;
|
||||
#endif
|
||||
};
|
||||
|
||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2025, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson/Collection/CollectionData.hpp>
|
||||
#include <ArduinoJson/Memory/ResourceManager.hpp>
|
||||
#include <ArduinoJson/Polyfills/alias_cast.hpp>
|
||||
#include <ArduinoJson/Variant/VariantData.hpp>
|
||||
|
||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
|
||||
inline Slot<VariantData> ResourceManager::allocVariant() {
|
||||
auto p = variantPools_.allocSlot(allocator_);
|
||||
if (!p) {
|
||||
overflowed_ = true;
|
||||
return {};
|
||||
}
|
||||
return {new (&p->variant) VariantData, p.id()};
|
||||
}
|
||||
|
||||
inline void ResourceManager::freeVariant(Slot<VariantData> variant) {
|
||||
variant->clear(this);
|
||||
variantPools_.freeSlot({alias_cast<SlotData*>(variant.ptr()), variant.id()});
|
||||
}
|
||||
|
||||
inline VariantData* ResourceManager::getVariant(SlotId id) const {
|
||||
return reinterpret_cast<VariantData*>(variantPools_.getSlot(id));
|
||||
}
|
||||
|
||||
#if ARDUINOJSON_USE_EXTENSIONS
|
||||
inline Slot<VariantExtension> ResourceManager::allocExtension() {
|
||||
auto p = variantPools_.allocSlot(allocator_);
|
||||
if (!p) {
|
||||
overflowed_ = true;
|
||||
return {};
|
||||
}
|
||||
return {&p->extension, p.id()};
|
||||
}
|
||||
|
||||
inline void ResourceManager::freeExtension(SlotId id) {
|
||||
auto p = getExtension(id);
|
||||
variantPools_.freeSlot({reinterpret_cast<SlotData*>(p), id});
|
||||
}
|
||||
|
||||
inline VariantExtension* ResourceManager::getExtension(SlotId id) const {
|
||||
return &variantPools_.getSlot(id)->extension;
|
||||
}
|
||||
#endif
|
||||
|
||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||
@@ -5,6 +5,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson/Memory/ResourceManager.hpp>
|
||||
#include <ArduinoJson/Strings/JsonString.hpp>
|
||||
|
||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
|
||||
@@ -43,7 +44,7 @@ class StringBuffer {
|
||||
if (isTinyString(s, size_))
|
||||
data->setTinyString(adaptString(s, size_));
|
||||
else
|
||||
data->setOwnedString(commitStringNode());
|
||||
data->setLongString(commitStringNode());
|
||||
}
|
||||
|
||||
void saveRaw(VariantData* data) {
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson/Memory/ResourceManager.hpp>
|
||||
#include <ArduinoJson/Strings/JsonString.hpp>
|
||||
|
||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
|
||||
@@ -45,7 +46,7 @@ class StringBuilder {
|
||||
} else {
|
||||
node->references++;
|
||||
}
|
||||
variant->setOwnedString(node);
|
||||
variant->setLongString(node);
|
||||
}
|
||||
|
||||
void append(const char* s) {
|
||||
|
||||
@@ -24,45 +24,48 @@ class MsgPackBinary {
|
||||
|
||||
template <>
|
||||
struct Converter<MsgPackBinary> : private detail::VariantAttorney {
|
||||
static void toJson(MsgPackBinary src, JsonVariant dst) {
|
||||
static bool toJson(MsgPackBinary src, JsonVariant dst) {
|
||||
auto data = VariantAttorney::getData(dst);
|
||||
if (!data)
|
||||
return;
|
||||
return false;
|
||||
|
||||
auto resources = getResourceManager(dst);
|
||||
data->clear(resources);
|
||||
if (src.data()) {
|
||||
size_t headerSize = src.size() >= 0x10000 ? 5
|
||||
: src.size() >= 0x100 ? 3
|
||||
: 2;
|
||||
auto str = resources->createString(src.size() + headerSize);
|
||||
if (str) {
|
||||
resources->saveString(str);
|
||||
auto ptr = reinterpret_cast<uint8_t*>(str->data);
|
||||
switch (headerSize) {
|
||||
case 2:
|
||||
ptr[0] = uint8_t(0xc4);
|
||||
ptr[1] = uint8_t(src.size() & 0xff);
|
||||
break;
|
||||
case 3:
|
||||
ptr[0] = uint8_t(0xc5);
|
||||
ptr[1] = uint8_t(src.size() >> 8 & 0xff);
|
||||
ptr[2] = uint8_t(src.size() & 0xff);
|
||||
break;
|
||||
case 5:
|
||||
ptr[0] = uint8_t(0xc6);
|
||||
ptr[1] = uint8_t(src.size() >> 24 & 0xff);
|
||||
ptr[2] = uint8_t(src.size() >> 16 & 0xff);
|
||||
ptr[3] = uint8_t(src.size() >> 8 & 0xff);
|
||||
ptr[4] = uint8_t(src.size() & 0xff);
|
||||
break;
|
||||
default:
|
||||
ARDUINOJSON_ASSERT(false);
|
||||
}
|
||||
memcpy(ptr + headerSize, src.data(), src.size());
|
||||
data->setRawString(str);
|
||||
return;
|
||||
}
|
||||
detail::VariantImpl::clear(data, resources);
|
||||
|
||||
if (!src.data())
|
||||
return true;
|
||||
|
||||
size_t headerSize = src.size() >= 0x10000 ? 5 : src.size() >= 0x100 ? 3 : 2;
|
||||
|
||||
auto str = resources->createString(src.size() + headerSize);
|
||||
if (!str)
|
||||
return false;
|
||||
|
||||
resources->saveString(str);
|
||||
auto ptr = reinterpret_cast<uint8_t*>(str->data);
|
||||
switch (headerSize) {
|
||||
case 2:
|
||||
ptr[0] = uint8_t(0xc4);
|
||||
ptr[1] = uint8_t(src.size() & 0xff);
|
||||
break;
|
||||
case 3:
|
||||
ptr[0] = uint8_t(0xc5);
|
||||
ptr[1] = uint8_t(src.size() >> 8 & 0xff);
|
||||
ptr[2] = uint8_t(src.size() & 0xff);
|
||||
break;
|
||||
case 5:
|
||||
ptr[0] = uint8_t(0xc6);
|
||||
ptr[1] = uint8_t(src.size() >> 24 & 0xff);
|
||||
ptr[2] = uint8_t(src.size() >> 16 & 0xff);
|
||||
ptr[3] = uint8_t(src.size() >> 8 & 0xff);
|
||||
ptr[4] = uint8_t(src.size() & 0xff);
|
||||
break;
|
||||
default:
|
||||
ARDUINOJSON_ASSERT(false);
|
||||
}
|
||||
memcpy(ptr + headerSize, src.data(), src.size());
|
||||
data->setRawString(str);
|
||||
return true;
|
||||
}
|
||||
|
||||
static MsgPackBinary fromJson(JsonVariantConst src) {
|
||||
|
||||
@@ -24,10 +24,10 @@ class MsgPackDeserializer {
|
||||
foundSomething_(false) {}
|
||||
|
||||
template <typename TFilter>
|
||||
DeserializationError parse(VariantData& variant, TFilter filter,
|
||||
DeserializationError parse(VariantData* variant, TFilter filter,
|
||||
DeserializationOption::NestingLimit nestingLimit) {
|
||||
DeserializationError::Code err;
|
||||
err = parseVariant(&variant, filter, nestingLimit);
|
||||
err = parseVariant(variant, filter, nestingLimit);
|
||||
return foundSomething_ ? err : DeserializationError::EmptyInput;
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ class MsgPackDeserializer {
|
||||
|
||||
if (code <= 0x7f || code >= 0xe0) { // fixint
|
||||
if (allowValue)
|
||||
variant->setInteger(static_cast<int8_t>(code), resources_);
|
||||
VariantImpl::setInteger(static_cast<int8_t>(code), variant, resources_);
|
||||
return DeserializationError::Ok;
|
||||
}
|
||||
|
||||
@@ -231,14 +231,14 @@ class MsgPackDeserializer {
|
||||
if (isSigned) {
|
||||
auto truncatedValue = static_cast<JsonInteger>(signedValue);
|
||||
if (truncatedValue == signedValue) {
|
||||
if (!variant->setInteger(truncatedValue, resources_))
|
||||
if (!VariantImpl::setInteger(truncatedValue, variant, resources_))
|
||||
return DeserializationError::NoMemory;
|
||||
}
|
||||
// else set null on overflow
|
||||
} else {
|
||||
auto truncatedValue = static_cast<JsonUInt>(unsignedValue);
|
||||
if (truncatedValue == unsignedValue)
|
||||
if (!variant->setInteger(truncatedValue, resources_))
|
||||
if (!VariantImpl::setInteger(truncatedValue, variant, resources_))
|
||||
return DeserializationError::NoMemory;
|
||||
// else set null on overflow
|
||||
}
|
||||
@@ -257,7 +257,7 @@ class MsgPackDeserializer {
|
||||
return err;
|
||||
|
||||
fixEndianness(value);
|
||||
variant->setFloat(value, resources_);
|
||||
VariantImpl::setFloat(value, variant, resources_);
|
||||
|
||||
return DeserializationError::Ok;
|
||||
}
|
||||
@@ -273,7 +273,7 @@ class MsgPackDeserializer {
|
||||
return err;
|
||||
|
||||
fixEndianness(value);
|
||||
if (variant->setFloat(value, resources_))
|
||||
if (VariantImpl::setFloat(value, variant, resources_))
|
||||
return DeserializationError::Ok;
|
||||
else
|
||||
return DeserializationError::NoMemory;
|
||||
@@ -293,7 +293,7 @@ class MsgPackDeserializer {
|
||||
|
||||
doubleToFloat(i, o);
|
||||
fixEndianness(value);
|
||||
variant->setFloat(value, resources_);
|
||||
VariantImpl::setFloat(value, variant, resources_);
|
||||
|
||||
return DeserializationError::Ok;
|
||||
}
|
||||
@@ -349,12 +349,9 @@ class MsgPackDeserializer {
|
||||
|
||||
bool allowArray = filter.allowArray();
|
||||
|
||||
ArrayData* array;
|
||||
if (allowArray) {
|
||||
ARDUINOJSON_ASSERT(variant != 0);
|
||||
array = &variant->toArray();
|
||||
} else {
|
||||
array = 0;
|
||||
variant->toArray();
|
||||
}
|
||||
|
||||
TFilter elementFilter = filter[0U];
|
||||
@@ -363,8 +360,7 @@ class MsgPackDeserializer {
|
||||
VariantData* value;
|
||||
|
||||
if (elementFilter.allow()) {
|
||||
ARDUINOJSON_ASSERT(array != 0);
|
||||
value = array->addElement(resources_);
|
||||
value = VariantImpl::addNewElement(variant, resources_);
|
||||
if (!value)
|
||||
return DeserializationError::NoMemory;
|
||||
} else {
|
||||
@@ -388,12 +384,9 @@ class MsgPackDeserializer {
|
||||
if (nestingLimit.reached())
|
||||
return DeserializationError::TooDeep;
|
||||
|
||||
ObjectData* object;
|
||||
if (filter.allowObject()) {
|
||||
ARDUINOJSON_ASSERT(variant != 0);
|
||||
object = &variant->toObject();
|
||||
} else {
|
||||
object = 0;
|
||||
variant->toObject();
|
||||
}
|
||||
|
||||
for (; n; --n) {
|
||||
@@ -406,9 +399,7 @@ class MsgPackDeserializer {
|
||||
VariantData* member = 0;
|
||||
|
||||
if (memberFilter.allow()) {
|
||||
ARDUINOJSON_ASSERT(object != 0);
|
||||
|
||||
auto keyVariant = object->addPair(&member, resources_);
|
||||
auto keyVariant = VariantImpl::addPair(&member, variant, resources_);
|
||||
if (!keyVariant)
|
||||
return DeserializationError::NoMemory;
|
||||
|
||||
|
||||
@@ -30,53 +30,57 @@ class MsgPackExtension {
|
||||
|
||||
template <>
|
||||
struct Converter<MsgPackExtension> : private detail::VariantAttorney {
|
||||
static void toJson(MsgPackExtension src, JsonVariant dst) {
|
||||
auto data = VariantAttorney::getData(dst);
|
||||
static bool toJson(MsgPackExtension src, JsonVariant dst) {
|
||||
auto data = getData(dst);
|
||||
if (!data)
|
||||
return;
|
||||
auto resources = getResourceManager(dst);
|
||||
data->clear(resources);
|
||||
if (src.data()) {
|
||||
uint8_t format, sizeBytes;
|
||||
if (src.size() >= 0x10000) {
|
||||
format = 0xc9; // ext 32
|
||||
sizeBytes = 4;
|
||||
} else if (src.size() >= 0x100) {
|
||||
format = 0xc8; // ext 16
|
||||
sizeBytes = 2;
|
||||
} else if (src.size() == 16) {
|
||||
format = 0xd8; // fixext 16
|
||||
sizeBytes = 0;
|
||||
} else if (src.size() == 8) {
|
||||
format = 0xd7; // fixext 8
|
||||
sizeBytes = 0;
|
||||
} else if (src.size() == 4) {
|
||||
format = 0xd6; // fixext 4
|
||||
sizeBytes = 0;
|
||||
} else if (src.size() == 2) {
|
||||
format = 0xd5; // fixext 2
|
||||
sizeBytes = 0;
|
||||
} else if (src.size() == 1) {
|
||||
format = 0xd4; // fixext 1
|
||||
sizeBytes = 0;
|
||||
} else {
|
||||
format = 0xc7; // ext 8
|
||||
sizeBytes = 1;
|
||||
}
|
||||
return false;
|
||||
|
||||
auto str = resources->createString(src.size() + 2 + sizeBytes);
|
||||
if (str) {
|
||||
resources->saveString(str);
|
||||
auto ptr = reinterpret_cast<uint8_t*>(str->data);
|
||||
*ptr++ = uint8_t(format);
|
||||
for (uint8_t i = 0; i < sizeBytes; i++)
|
||||
*ptr++ = uint8_t(src.size() >> (sizeBytes - i - 1) * 8 & 0xff);
|
||||
*ptr++ = uint8_t(src.type());
|
||||
memcpy(ptr, src.data(), src.size());
|
||||
data->setRawString(str);
|
||||
return;
|
||||
}
|
||||
auto resources = getResourceManager(dst);
|
||||
detail::VariantImpl::clear(data, resources);
|
||||
|
||||
if (!src.data())
|
||||
return true;
|
||||
|
||||
uint8_t format, sizeBytes;
|
||||
if (src.size() >= 0x10000) {
|
||||
format = 0xc9; // ext 32
|
||||
sizeBytes = 4;
|
||||
} else if (src.size() >= 0x100) {
|
||||
format = 0xc8; // ext 16
|
||||
sizeBytes = 2;
|
||||
} else if (src.size() == 16) {
|
||||
format = 0xd8; // fixext 16
|
||||
sizeBytes = 0;
|
||||
} else if (src.size() == 8) {
|
||||
format = 0xd7; // fixext 8
|
||||
sizeBytes = 0;
|
||||
} else if (src.size() == 4) {
|
||||
format = 0xd6; // fixext 4
|
||||
sizeBytes = 0;
|
||||
} else if (src.size() == 2) {
|
||||
format = 0xd5; // fixext 2
|
||||
sizeBytes = 0;
|
||||
} else if (src.size() == 1) {
|
||||
format = 0xd4; // fixext 1
|
||||
sizeBytes = 0;
|
||||
} else {
|
||||
format = 0xc7; // ext 8
|
||||
sizeBytes = 1;
|
||||
}
|
||||
|
||||
auto str = resources->createString(src.size() + 2 + sizeBytes);
|
||||
if (!str)
|
||||
return false;
|
||||
|
||||
resources->saveString(str);
|
||||
auto ptr = reinterpret_cast<uint8_t*>(str->data);
|
||||
*ptr++ = uint8_t(format);
|
||||
for (uint8_t i = 0; i < sizeBytes; i++)
|
||||
*ptr++ = uint8_t(src.size() >> (sizeBytes - i - 1) * 8 & 0xff);
|
||||
*ptr++ = uint8_t(src.type());
|
||||
memcpy(ptr, src.data(), src.size());
|
||||
data->setRawString(str);
|
||||
return true;
|
||||
}
|
||||
|
||||
static MsgPackExtension fromJson(JsonVariantConst src) {
|
||||
|
||||
@@ -19,7 +19,7 @@ class MsgPackSerializer : public VariantDataVisitor<size_t> {
|
||||
public:
|
||||
static const bool producesText = false;
|
||||
|
||||
MsgPackSerializer(TWriter writer, const ResourceManager* resources)
|
||||
MsgPackSerializer(TWriter writer, ResourceManager* resources)
|
||||
: writer_(writer), resources_(resources) {}
|
||||
|
||||
template <typename T>
|
||||
@@ -47,8 +47,11 @@ class MsgPackSerializer : public VariantDataVisitor<size_t> {
|
||||
return bytesWritten();
|
||||
}
|
||||
|
||||
size_t visit(const ArrayData& array) {
|
||||
size_t n = array.size(resources_);
|
||||
size_t visitArray(VariantData* array) {
|
||||
ARDUINOJSON_ASSERT(array != nullptr);
|
||||
ARDUINOJSON_ASSERT(array->isArray());
|
||||
|
||||
auto n = VariantImpl::size(array, resources_);
|
||||
if (n < 0x10) {
|
||||
writeByte(uint8_t(0x90 + n));
|
||||
} else if (n < 0x10000) {
|
||||
@@ -59,18 +62,21 @@ class MsgPackSerializer : public VariantDataVisitor<size_t> {
|
||||
writeInteger(uint32_t(n));
|
||||
}
|
||||
|
||||
auto slotId = array.head();
|
||||
auto slotId = array->content.asCollection.head;
|
||||
while (slotId != NULL_SLOT) {
|
||||
auto slot = resources_->getVariant(slotId);
|
||||
slot->accept(*this, resources_);
|
||||
slotId = slot->next();
|
||||
VariantImpl::accept(*this, slot, resources_);
|
||||
slotId = slot->next;
|
||||
}
|
||||
|
||||
return bytesWritten();
|
||||
}
|
||||
|
||||
size_t visit(const ObjectData& object) {
|
||||
size_t n = object.size(resources_);
|
||||
size_t visitObject(VariantData* object) {
|
||||
ARDUINOJSON_ASSERT(object != nullptr);
|
||||
ARDUINOJSON_ASSERT(object->isObject());
|
||||
|
||||
auto n = VariantImpl::size(object, resources_);
|
||||
if (n < 0x10) {
|
||||
writeByte(uint8_t(0x80 + n));
|
||||
} else if (n < 0x10000) {
|
||||
@@ -81,11 +87,11 @@ class MsgPackSerializer : public VariantDataVisitor<size_t> {
|
||||
writeInteger(uint32_t(n));
|
||||
}
|
||||
|
||||
auto slotId = object.head();
|
||||
auto slotId = object->content.asCollection.head;
|
||||
while (slotId != NULL_SLOT) {
|
||||
auto slot = resources_->getVariant(slotId);
|
||||
slot->accept(*this, resources_);
|
||||
slotId = slot->next();
|
||||
VariantImpl::accept(*this, slot, resources_);
|
||||
slotId = slot->next;
|
||||
}
|
||||
|
||||
return bytesWritten();
|
||||
@@ -209,7 +215,7 @@ class MsgPackSerializer : public VariantDataVisitor<size_t> {
|
||||
}
|
||||
|
||||
CountingDecorator<TWriter> writer_;
|
||||
const ResourceManager* resources_;
|
||||
ResourceManager* resources_;
|
||||
};
|
||||
|
||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||
|
||||
@@ -20,56 +20,55 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
|
||||
using iterator = JsonObjectIterator;
|
||||
|
||||
// Creates an unbound reference.
|
||||
JsonObject() : data_(0), resources_(0) {}
|
||||
JsonObject() {}
|
||||
|
||||
// INTERNAL USE ONLY
|
||||
JsonObject(detail::ObjectData* data, detail::ResourceManager* resource)
|
||||
: data_(data), resources_(resource) {}
|
||||
JsonObject(const detail::VariantImpl& impl) : impl_(impl) {}
|
||||
|
||||
// INTERNAL USE ONLY
|
||||
JsonObject(detail::VariantData* data, detail::ResourceManager* resource)
|
||||
: impl_(data, resource) {}
|
||||
|
||||
operator JsonVariant() const {
|
||||
void* data = data_; // prevent warning cast-align
|
||||
return JsonVariant(reinterpret_cast<detail::VariantData*>(data),
|
||||
resources_);
|
||||
return JsonVariant(getData(), getResourceManager());
|
||||
}
|
||||
|
||||
operator JsonObjectConst() const {
|
||||
return JsonObjectConst(data_, resources_);
|
||||
return JsonObjectConst(getData(), getResourceManager());
|
||||
}
|
||||
|
||||
operator JsonVariantConst() const {
|
||||
return JsonVariantConst(collectionToVariant(data_), resources_);
|
||||
return JsonVariantConst(getData(), getResourceManager());
|
||||
}
|
||||
|
||||
// Returns true if the reference is unbound.
|
||||
// https://arduinojson.org/v7/api/jsonobject/isnull/
|
||||
bool isNull() const {
|
||||
return data_ == 0;
|
||||
return !impl_.isObject();
|
||||
}
|
||||
|
||||
// Returns true if the reference is bound.
|
||||
// https://arduinojson.org/v7/api/jsonobject/isnull/
|
||||
operator bool() const {
|
||||
return data_ != 0;
|
||||
return impl_.isObject();
|
||||
}
|
||||
|
||||
// Returns the depth (nesting level) of the object.
|
||||
// https://arduinojson.org/v7/api/jsonobject/nesting/
|
||||
size_t nesting() const {
|
||||
return detail::VariantData::nesting(collectionToVariant(data_), resources_);
|
||||
return impl_.nesting();
|
||||
}
|
||||
|
||||
// Returns the number of members in the object.
|
||||
// https://arduinojson.org/v7/api/jsonobject/size/
|
||||
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.
|
||||
// https://arduinojson.org/v7/api/jsonobject/begin/
|
||||
iterator begin() const {
|
||||
if (!data_)
|
||||
return iterator();
|
||||
return iterator(data_->createIterator(resources_), resources_);
|
||||
return iterator(impl_.createIterator(), impl_.resources());
|
||||
}
|
||||
|
||||
// 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.
|
||||
// https://arduinojson.org/v7/api/jsonobject/clear/
|
||||
void clear() const {
|
||||
detail::ObjectData::clear(data_, resources_);
|
||||
impl_.empty();
|
||||
}
|
||||
|
||||
// Copies an object.
|
||||
// https://arduinojson.org/v7/api/jsonobject/set/
|
||||
bool set(JsonObjectConst src) {
|
||||
if (!data_ || !src.data_)
|
||||
if (isNull() || src.isNull())
|
||||
return false;
|
||||
|
||||
clear();
|
||||
@@ -111,9 +110,7 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
|
||||
// Gets or sets the member with specified key.
|
||||
// https://arduinojson.org/v7/api/jsonobject/subscript/
|
||||
template <typename TChar,
|
||||
detail::enable_if_t<detail::IsString<TChar*>::value &&
|
||||
!detail::is_const<TChar>::value,
|
||||
int> = 0>
|
||||
detail::enable_if_t<detail::IsString<TChar*>::value, int> = 0>
|
||||
detail::MemberProxy<JsonObject, detail::AdaptedString<TChar*>> operator[](
|
||||
TChar* key) const {
|
||||
return {*this, detail::adaptString(key)};
|
||||
@@ -131,7 +128,7 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
|
||||
// Removes the member at the specified iterator.
|
||||
// https://arduinojson.org/v7/api/jsonobject/remove/
|
||||
FORCE_INLINE void remove(iterator it) const {
|
||||
detail::ObjectData::remove(data_, it.iterator_, resources_);
|
||||
impl_.removeMember(it.iterator_);
|
||||
}
|
||||
|
||||
// Removes the member with the specified key.
|
||||
@@ -139,8 +136,7 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
|
||||
template <typename TString,
|
||||
detail::enable_if_t<detail::IsString<TString>::value, int> = 0>
|
||||
void remove(const TString& key) const {
|
||||
detail::ObjectData::removeMember(data_, detail::adaptString(key),
|
||||
resources_);
|
||||
impl_.removeMember(detail::adaptString(key));
|
||||
}
|
||||
|
||||
// Removes the member with the specified key.
|
||||
@@ -156,8 +152,7 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
|
||||
// https://arduinojson.org/v7/api/jsonobject/remove/
|
||||
template <typename TChar>
|
||||
FORCE_INLINE void remove(TChar* key) const {
|
||||
detail::ObjectData::removeMember(data_, detail::adaptString(key),
|
||||
resources_);
|
||||
impl_.removeMember(detail::adaptString(key));
|
||||
}
|
||||
|
||||
// DEPRECATED: use obj[key].is<T>() instead
|
||||
@@ -166,20 +161,16 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
|
||||
detail::enable_if_t<detail::IsString<TString>::value, int> = 0>
|
||||
ARDUINOJSON_DEPRECATED("use obj[key].is<T>() instead")
|
||||
bool containsKey(const TString& key) const {
|
||||
return detail::ObjectData::getMember(data_, detail::adaptString(key),
|
||||
resources_) != 0;
|
||||
return impl_.getMember(detail::adaptString(key)) != 0;
|
||||
}
|
||||
|
||||
// DEPRECATED: use obj["key"].is<T>() instead
|
||||
// https://arduinojson.org/v7/api/jsonobject/containskey/
|
||||
template <typename TChar,
|
||||
detail::enable_if_t<detail::IsString<TChar*>::value &&
|
||||
!detail::is_const<TChar>::value,
|
||||
int> = 0>
|
||||
detail::enable_if_t<detail::IsString<TChar*>::value, int> = 0>
|
||||
ARDUINOJSON_DEPRECATED("use obj[\"key\"].is<T>() instead")
|
||||
bool containsKey(TChar* key) const {
|
||||
return detail::ObjectData::getMember(data_, detail::adaptString(key),
|
||||
resources_) != 0;
|
||||
return impl_.getMember(detail::adaptString(key)) != 0;
|
||||
}
|
||||
|
||||
// DEPRECATED: use obj[key].is<T>() instead
|
||||
@@ -227,19 +218,18 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
|
||||
|
||||
private:
|
||||
detail::ResourceManager* getResourceManager() const {
|
||||
return resources_;
|
||||
return impl_.resources();
|
||||
}
|
||||
|
||||
detail::VariantData* getData() const {
|
||||
return detail::collectionToVariant(data_);
|
||||
return impl_.data();
|
||||
}
|
||||
|
||||
detail::VariantData* getOrCreateData() const {
|
||||
return detail::collectionToVariant(data_);
|
||||
return impl_.data();
|
||||
}
|
||||
|
||||
detail::ObjectData* data_;
|
||||
detail::ResourceManager* resources_;
|
||||
mutable detail::VariantImpl impl_;
|
||||
};
|
||||
|
||||
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
||||
|
||||
@@ -19,47 +19,47 @@ class JsonObjectConst : public detail::VariantOperators<JsonObjectConst> {
|
||||
using iterator = JsonObjectConstIterator;
|
||||
|
||||
// Creates an unbound reference.
|
||||
JsonObjectConst() : data_(0), resources_(0) {}
|
||||
JsonObjectConst() {}
|
||||
|
||||
// INTERNAL USE ONLY
|
||||
JsonObjectConst(const detail::ObjectData* data,
|
||||
const detail::ResourceManager* resources)
|
||||
: data_(data), resources_(resources) {}
|
||||
JsonObjectConst(detail::VariantData* data, detail::ResourceManager* resources)
|
||||
: impl_(data, resources) {}
|
||||
|
||||
// INTERNAL USE ONLY
|
||||
JsonObjectConst(const detail::VariantImpl& impl) : impl_(impl) {}
|
||||
|
||||
operator JsonVariantConst() const {
|
||||
return JsonVariantConst(getData(), resources_);
|
||||
return JsonVariantConst(impl_.data(), impl_.resources());
|
||||
}
|
||||
|
||||
// Returns true if the reference is unbound.
|
||||
// https://arduinojson.org/v7/api/jsonobjectconst/isnull/
|
||||
bool isNull() const {
|
||||
return data_ == 0;
|
||||
return impl_.isNull();
|
||||
}
|
||||
|
||||
// Returns true if the reference is bound.
|
||||
// https://arduinojson.org/v7/api/jsonobjectconst/isnull/
|
||||
operator bool() const {
|
||||
return data_ != 0;
|
||||
return !isNull();
|
||||
}
|
||||
|
||||
// Returns the depth (nesting level) of the object.
|
||||
// https://arduinojson.org/v7/api/jsonobjectconst/nesting/
|
||||
size_t nesting() const {
|
||||
return detail::VariantData::nesting(getData(), resources_);
|
||||
return impl_.nesting();
|
||||
}
|
||||
|
||||
// Returns the number of members in the object.
|
||||
// https://arduinojson.org/v7/api/jsonobjectconst/size/
|
||||
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.
|
||||
// https://arduinojson.org/v7/api/jsonobjectconst/begin/
|
||||
iterator begin() const {
|
||||
if (!data_)
|
||||
return iterator();
|
||||
return iterator(data_->createIterator(resources_), resources_);
|
||||
return iterator(impl_.createIterator(), impl_.resources());
|
||||
}
|
||||
|
||||
// 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>
|
||||
ARDUINOJSON_DEPRECATED("use obj[key].is<T>() instead")
|
||||
bool containsKey(const TString& key) const {
|
||||
return detail::ObjectData::getMember(data_, detail::adaptString(key),
|
||||
resources_) != 0;
|
||||
return impl_.getMember(detail::adaptString(key)) != 0;
|
||||
}
|
||||
|
||||
// DEPRECATED: use obj["key"].is<T>() instead
|
||||
@@ -83,8 +82,7 @@ class JsonObjectConst : public detail::VariantOperators<JsonObjectConst> {
|
||||
template <typename TChar>
|
||||
ARDUINOJSON_DEPRECATED("use obj[\"key\"].is<T>() instead")
|
||||
bool containsKey(TChar* key) const {
|
||||
return detail::ObjectData::getMember(data_, detail::adaptString(key),
|
||||
resources_) != 0;
|
||||
return impl_.getMember(detail::adaptString(key)) != 0;
|
||||
}
|
||||
|
||||
// DEPRECATED: use obj[key].is<T>() instead
|
||||
@@ -101,21 +99,17 @@ class JsonObjectConst : public detail::VariantOperators<JsonObjectConst> {
|
||||
template <typename TString,
|
||||
detail::enable_if_t<detail::IsString<TString>::value, int> = 0>
|
||||
JsonVariantConst operator[](const TString& key) const {
|
||||
return JsonVariantConst(detail::ObjectData::getMember(
|
||||
data_, detail::adaptString(key), resources_),
|
||||
resources_);
|
||||
return JsonVariantConst(impl_.getMember(detail::adaptString(key)),
|
||||
impl_.resources());
|
||||
}
|
||||
|
||||
// Gets the member with specified key.
|
||||
// https://arduinojson.org/v7/api/jsonobjectconst/subscript/
|
||||
template <typename TChar,
|
||||
detail::enable_if_t<detail::IsString<TChar*>::value &&
|
||||
!detail::is_const<TChar>::value,
|
||||
int> = 0>
|
||||
detail::enable_if_t<detail::IsString<TChar*>::value, int> = 0>
|
||||
JsonVariantConst operator[](TChar* key) const {
|
||||
return JsonVariantConst(detail::ObjectData::getMember(
|
||||
data_, detail::adaptString(key), resources_),
|
||||
resources_);
|
||||
return JsonVariantConst(impl_.getMember(detail::adaptString(key)),
|
||||
impl_.resources());
|
||||
}
|
||||
|
||||
// Gets the member with specified key.
|
||||
@@ -137,11 +131,10 @@ class JsonObjectConst : public detail::VariantOperators<JsonObjectConst> {
|
||||
|
||||
private:
|
||||
const detail::VariantData* getData() const {
|
||||
return collectionToVariant(data_);
|
||||
return impl_.data();
|
||||
}
|
||||
|
||||
const detail::ObjectData* data_;
|
||||
const detail::ResourceManager* resources_;
|
||||
detail::VariantImpl impl_;
|
||||
};
|
||||
|
||||
inline bool operator==(JsonObjectConst lhs, JsonObjectConst rhs) {
|
||||
|
||||
@@ -14,7 +14,7 @@ class JsonObjectIterator {
|
||||
public:
|
||||
JsonObjectIterator() {}
|
||||
|
||||
explicit JsonObjectIterator(detail::ObjectData::iterator iterator,
|
||||
explicit JsonObjectIterator(detail::VariantImpl::iterator iterator,
|
||||
detail::ResourceManager* resources)
|
||||
: iterator_(iterator), resources_(resources) {}
|
||||
|
||||
@@ -34,13 +34,13 @@ class JsonObjectIterator {
|
||||
}
|
||||
|
||||
JsonObjectIterator& operator++() {
|
||||
iterator_.next(resources_); // key
|
||||
iterator_.next(resources_); // value
|
||||
iterator_.move(resources_); // key
|
||||
iterator_.move(resources_); // value
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
detail::ObjectData::iterator iterator_;
|
||||
detail::VariantImpl::iterator iterator_;
|
||||
detail::ResourceManager* resources_;
|
||||
};
|
||||
|
||||
@@ -50,8 +50,8 @@ class JsonObjectConstIterator {
|
||||
public:
|
||||
JsonObjectConstIterator() {}
|
||||
|
||||
explicit JsonObjectConstIterator(detail::ObjectData::iterator iterator,
|
||||
const detail::ResourceManager* resources)
|
||||
explicit JsonObjectConstIterator(detail::VariantImpl::iterator iterator,
|
||||
detail::ResourceManager* resources)
|
||||
: iterator_(iterator), resources_(resources) {}
|
||||
|
||||
JsonPairConst operator*() const {
|
||||
@@ -70,14 +70,14 @@ class JsonObjectConstIterator {
|
||||
}
|
||||
|
||||
JsonObjectConstIterator& operator++() {
|
||||
iterator_.next(resources_); // key
|
||||
iterator_.next(resources_); // value
|
||||
iterator_.move(resources_); // key
|
||||
iterator_.move(resources_); // value
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
detail::ObjectData::iterator iterator_;
|
||||
const detail::ResourceManager* resources_;
|
||||
detail::VariantImpl::iterator iterator_;
|
||||
detail::ResourceManager* resources_;
|
||||
};
|
||||
|
||||
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
||||
|
||||
@@ -15,11 +15,11 @@ ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE
|
||||
class JsonPair {
|
||||
public:
|
||||
// INTERNAL USE ONLY
|
||||
JsonPair(detail::ObjectData::iterator iterator,
|
||||
JsonPair(detail::VariantImpl::iterator iterator,
|
||||
detail::ResourceManager* resources) {
|
||||
if (!iterator.done()) {
|
||||
key_ = iterator->asString();
|
||||
iterator.next(resources);
|
||||
iterator.move(resources);
|
||||
value_ = JsonVariant(iterator.data(), resources);
|
||||
}
|
||||
}
|
||||
@@ -43,11 +43,11 @@ class JsonPair {
|
||||
// https://arduinojson.org/v7/api/jsonobjectconst/begin_end/
|
||||
class JsonPairConst {
|
||||
public:
|
||||
JsonPairConst(detail::ObjectData::iterator iterator,
|
||||
const detail::ResourceManager* resources) {
|
||||
JsonPairConst(detail::VariantImpl::iterator iterator,
|
||||
detail::ResourceManager* resources) {
|
||||
if (!iterator.done()) {
|
||||
key_ = iterator->asString();
|
||||
iterator.next(resources);
|
||||
iterator.move(resources);
|
||||
value_ = JsonVariantConst(iterator.data(), resources);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ class MemberProxy
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T, enable_if_t<!is_const<T>::value, int> = 0>
|
||||
template <typename T>
|
||||
MemberProxy& operator=(T* src) {
|
||||
this->set(src);
|
||||
return *this;
|
||||
@@ -56,17 +56,15 @@ class MemberProxy
|
||||
}
|
||||
|
||||
VariantData* getData() const {
|
||||
return VariantData::getMember(
|
||||
VariantAttorney::getData(upstream_), key_,
|
||||
VariantAttorney::getResourceManager(upstream_));
|
||||
return VariantAttorney::getVariantImpl(upstream_).getMember(key_);
|
||||
}
|
||||
|
||||
VariantData* getOrCreateData() const {
|
||||
auto data = VariantAttorney::getOrCreateData(upstream_);
|
||||
if (!data)
|
||||
return nullptr;
|
||||
return data->getOrAddMember(key_,
|
||||
VariantAttorney::getResourceManager(upstream_));
|
||||
auto resources = VariantAttorney::getResourceManager(upstream_);
|
||||
if (data && data->type == VariantType::Null)
|
||||
data->toObject();
|
||||
return VariantImpl(data, resources).getOrAddMember(key_);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
@@ -1,70 +0,0 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2025, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson/Collection/CollectionData.hpp>
|
||||
|
||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
|
||||
class ObjectData : public CollectionData {
|
||||
public:
|
||||
template <typename TAdaptedString>
|
||||
VariantData* addMember(TAdaptedString key, ResourceManager* resources);
|
||||
|
||||
VariantData* addPair(VariantData** value, ResourceManager* resources);
|
||||
|
||||
template <typename TAdaptedString>
|
||||
VariantData* getOrAddMember(TAdaptedString key, ResourceManager* resources);
|
||||
|
||||
template <typename TAdaptedString>
|
||||
VariantData* getMember(TAdaptedString key,
|
||||
const ResourceManager* resources) const;
|
||||
|
||||
template <typename TAdaptedString>
|
||||
static VariantData* getMember(const ObjectData* object, TAdaptedString key,
|
||||
const ResourceManager* resources) {
|
||||
if (!object)
|
||||
return nullptr;
|
||||
return object->getMember(key, resources);
|
||||
}
|
||||
|
||||
template <typename TAdaptedString>
|
||||
void removeMember(TAdaptedString key, ResourceManager* resources);
|
||||
|
||||
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:
|
||||
template <typename TAdaptedString>
|
||||
iterator findKey(TAdaptedString key, const ResourceManager* resources) const;
|
||||
};
|
||||
|
||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||
@@ -4,38 +4,48 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson/Object/ObjectData.hpp>
|
||||
#include <ArduinoJson/Variant/VariantCompare.hpp>
|
||||
#include <ArduinoJson/Variant/VariantData.hpp>
|
||||
#include <ArduinoJson/Variant/VariantImpl.hpp>
|
||||
|
||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
|
||||
template <typename TAdaptedString>
|
||||
inline VariantData* ObjectData::getMember(
|
||||
TAdaptedString key, const ResourceManager* resources) const {
|
||||
auto it = findKey(key, resources);
|
||||
inline VariantData* VariantImpl::getMember(TAdaptedString key,
|
||||
VariantData* data,
|
||||
ResourceManager* resources) {
|
||||
auto it = findKey(key, data, resources);
|
||||
if (it.done())
|
||||
return nullptr;
|
||||
it.next(resources);
|
||||
it.move(resources);
|
||||
return it.data();
|
||||
}
|
||||
|
||||
template <typename TAdaptedString>
|
||||
VariantData* ObjectData::getOrAddMember(TAdaptedString key,
|
||||
ResourceManager* resources) {
|
||||
auto data = getMember(key, resources);
|
||||
if (data)
|
||||
return data;
|
||||
return addMember(key, resources);
|
||||
VariantData* VariantImpl::getOrAddMember(TAdaptedString key, VariantData* data,
|
||||
ResourceManager* resources) {
|
||||
ARDUINOJSON_ASSERT(data != nullptr);
|
||||
ARDUINOJSON_ASSERT(data->isObject());
|
||||
ARDUINOJSON_ASSERT(resources != nullptr);
|
||||
|
||||
auto member = getMember(key, data, resources);
|
||||
if (member)
|
||||
return member;
|
||||
return addMember(key, data, resources);
|
||||
}
|
||||
|
||||
template <typename TAdaptedString>
|
||||
inline ObjectData::iterator ObjectData::findKey(
|
||||
TAdaptedString key, const ResourceManager* resources) const {
|
||||
inline VariantImpl::iterator VariantImpl::findKey(TAdaptedString key,
|
||||
VariantData* data,
|
||||
ResourceManager* resources) {
|
||||
ARDUINOJSON_ASSERT(data != nullptr);
|
||||
ARDUINOJSON_ASSERT(data->isObject());
|
||||
ARDUINOJSON_ASSERT(resources != nullptr);
|
||||
|
||||
if (key.isNull())
|
||||
return iterator();
|
||||
bool isKey = true;
|
||||
for (auto it = createIterator(resources); !it.done(); it.next(resources)) {
|
||||
for (auto it = createIterator(data, resources); !it.done();
|
||||
it.move(resources)) {
|
||||
if (isKey && stringEquals(key, adaptString(it->asString())))
|
||||
return it;
|
||||
isKey = !isKey;
|
||||
@@ -44,14 +54,16 @@ inline ObjectData::iterator ObjectData::findKey(
|
||||
}
|
||||
|
||||
template <typename TAdaptedString>
|
||||
inline void ObjectData::removeMember(TAdaptedString key,
|
||||
ResourceManager* resources) {
|
||||
remove(findKey(key, resources), resources);
|
||||
}
|
||||
inline VariantData* VariantImpl::addMember(TAdaptedString key,
|
||||
VariantData* data,
|
||||
ResourceManager* resources) {
|
||||
ARDUINOJSON_ASSERT(data != nullptr);
|
||||
ARDUINOJSON_ASSERT(data->isObject());
|
||||
ARDUINOJSON_ASSERT(resources != nullptr);
|
||||
|
||||
if (key.isNull())
|
||||
return nullptr; // Ignore null key
|
||||
|
||||
template <typename TAdaptedString>
|
||||
inline VariantData* ObjectData::addMember(TAdaptedString key,
|
||||
ResourceManager* resources) {
|
||||
auto keySlot = resources->allocVariant();
|
||||
if (!keySlot)
|
||||
return nullptr;
|
||||
@@ -60,16 +72,21 @@ inline VariantData* ObjectData::addMember(TAdaptedString key,
|
||||
if (!valueSlot)
|
||||
return nullptr;
|
||||
|
||||
if (!keySlot->setString(key, resources))
|
||||
if (!VariantImpl::setString(key, keySlot.ptr(), resources))
|
||||
return nullptr;
|
||||
|
||||
CollectionData::appendPair(keySlot, valueSlot, resources);
|
||||
appendPair(keySlot, valueSlot, data, resources);
|
||||
|
||||
return valueSlot.ptr();
|
||||
}
|
||||
|
||||
inline VariantData* ObjectData::addPair(VariantData** value,
|
||||
ResourceManager* resources) {
|
||||
inline VariantData* VariantImpl::addPair(VariantData** value, VariantData* data,
|
||||
ResourceManager* resources) {
|
||||
ARDUINOJSON_ASSERT(value != nullptr);
|
||||
ARDUINOJSON_ASSERT(data != nullptr);
|
||||
ARDUINOJSON_ASSERT(data->isObject());
|
||||
ARDUINOJSON_ASSERT(resources != nullptr);
|
||||
|
||||
auto keySlot = resources->allocVariant();
|
||||
if (!keySlot)
|
||||
return nullptr;
|
||||
@@ -79,14 +96,14 @@ inline VariantData* ObjectData::addPair(VariantData** value,
|
||||
return nullptr;
|
||||
*value = valueSlot.ptr();
|
||||
|
||||
CollectionData::appendPair(keySlot, valueSlot, resources);
|
||||
appendPair(keySlot, valueSlot, data, resources);
|
||||
|
||||
return keySlot.ptr();
|
||||
}
|
||||
|
||||
// Returns the size (in bytes) of an object with n members.
|
||||
constexpr size_t sizeofObject(size_t n) {
|
||||
return 2 * n * ResourceManager::slotSize;
|
||||
return 2 * n * sizeof(VariantData);
|
||||
}
|
||||
|
||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||
|
||||
@@ -14,7 +14,7 @@ size_t measure(ArduinoJson::JsonVariantConst source) {
|
||||
auto data = VariantAttorney::getData(source);
|
||||
auto resources = VariantAttorney::getResourceManager(source);
|
||||
TSerializer<DummyWriter> serializer(dp, resources);
|
||||
return VariantData::accept(data, resources, serializer);
|
||||
return VariantImpl::accept(serializer, data, resources);
|
||||
}
|
||||
|
||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||
|
||||
@@ -13,7 +13,7 @@ size_t doSerialize(ArduinoJson::JsonVariantConst source, TWriter writer) {
|
||||
auto data = VariantAttorney::getData(source);
|
||||
auto resources = VariantAttorney::getResourceManager(source);
|
||||
TSerializer<TWriter> serializer(writer, resources);
|
||||
return VariantData::accept(data, resources, serializer);
|
||||
return VariantImpl::accept(serializer, data, resources);
|
||||
}
|
||||
|
||||
template <template <typename> class TSerializer, typename TDestination>
|
||||
|
||||
@@ -63,10 +63,6 @@ class FlashString {
|
||||
::memcpy_P(p, s.str_, n);
|
||||
}
|
||||
|
||||
bool isStatic() const {
|
||||
return false;
|
||||
}
|
||||
|
||||
private:
|
||||
const char* str_;
|
||||
size_t size_;
|
||||
|
||||
@@ -20,14 +20,8 @@ struct IsChar
|
||||
class RamString {
|
||||
public:
|
||||
static const size_t typeSortKey = 2;
|
||||
#if ARDUINOJSON_SIZEOF_POINTER <= 2
|
||||
static constexpr size_t sizeMask = size_t(-1) >> 1;
|
||||
#else
|
||||
static constexpr size_t sizeMask = size_t(-1);
|
||||
#endif
|
||||
|
||||
RamString(const char* str, size_t sz, bool isStatic = false)
|
||||
: str_(str), size_(sz & sizeMask), static_(isStatic) {
|
||||
RamString(const char* str, size_t sz) : str_(str), size_(sz) {
|
||||
ARDUINOJSON_ASSERT(size_ == sz);
|
||||
}
|
||||
|
||||
@@ -49,21 +43,9 @@ class RamString {
|
||||
return str_;
|
||||
}
|
||||
|
||||
bool isStatic() const {
|
||||
return static_;
|
||||
}
|
||||
|
||||
protected:
|
||||
const char* str_;
|
||||
|
||||
#if ARDUINOJSON_SIZEOF_POINTER <= 2
|
||||
// Use a bitfield only on 8-bit microcontrollers
|
||||
size_t size_ : sizeof(size_t) * 8 - 1;
|
||||
bool static_ : 1;
|
||||
#else
|
||||
size_t size_;
|
||||
bool static_;
|
||||
#endif
|
||||
};
|
||||
|
||||
template <typename TChar>
|
||||
@@ -76,36 +58,6 @@ struct StringAdapter<TChar*, enable_if_t<IsChar<TChar>::value>> {
|
||||
}
|
||||
};
|
||||
|
||||
template <typename TChar>
|
||||
struct StringAdapter<TChar[], enable_if_t<IsChar<TChar>::value>> {
|
||||
using AdaptedString = RamString;
|
||||
|
||||
static AdaptedString adapt(const TChar* p) {
|
||||
auto str = reinterpret_cast<const char*>(p);
|
||||
return AdaptedString(str, str ? ::strlen(str) : 0);
|
||||
}
|
||||
};
|
||||
|
||||
template <size_t N>
|
||||
struct StringAdapter<const char (&)[N]> {
|
||||
using AdaptedString = RamString;
|
||||
|
||||
static AdaptedString adapt(const char (&p)[N]) {
|
||||
return RamString(p, N - 1, true);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename TChar, size_t N>
|
||||
struct StringAdapter<TChar[N], enable_if_t<IsChar<TChar>::value>> {
|
||||
using AdaptedString = RamString;
|
||||
|
||||
static AdaptedString adapt(const TChar* p) {
|
||||
ARDUINOJSON_ASSERT(p);
|
||||
auto str = reinterpret_cast<const char*>(p);
|
||||
return AdaptedString(str, ::strlen(str));
|
||||
}
|
||||
};
|
||||
|
||||
template <typename TChar>
|
||||
struct SizedStringAdapter<TChar*, enable_if_t<IsChar<TChar>::value>> {
|
||||
using AdaptedString = RamString;
|
||||
|
||||
@@ -19,17 +19,25 @@ class JsonString {
|
||||
friend struct detail::StringAdapter<JsonString>;
|
||||
|
||||
public:
|
||||
JsonString() : str_(nullptr, 0, true) {}
|
||||
JsonString() : str_(nullptr, 0) {}
|
||||
|
||||
JsonString(const char* data, bool isStatic = false)
|
||||
: str_(data, data ? ::strlen(data) : 0, isStatic) {}
|
||||
JsonString(const char* data) : str_(data, data ? ::strlen(data) : 0) {}
|
||||
|
||||
ARDUINOJSON_DEPRECATED(
|
||||
"ArduinoJson doesn't differentiate between static and dynamic strings "
|
||||
"anymore. Remove the second argument to fix this warning.")
|
||||
JsonString(const char* data, bool) : JsonString(data) {}
|
||||
|
||||
template <typename TSize,
|
||||
detail::enable_if_t<detail::is_integral<TSize>::value &&
|
||||
!detail::is_same<TSize, bool>::value,
|
||||
int> = 0>
|
||||
JsonString(const char* data, TSize sz, bool isStatic = false)
|
||||
: str_(data, size_t(sz), isStatic) {}
|
||||
JsonString(const char* data, TSize sz) : str_(data, size_t(sz)) {}
|
||||
|
||||
ARDUINOJSON_DEPRECATED(
|
||||
"ArduinoJson doesn't differentiate between static and dynamic strings "
|
||||
"anymore. Remove the third argument to fix this warning.")
|
||||
JsonString(const char* data, size_t sz, bool) : JsonString(data, sz) {}
|
||||
|
||||
// Returns a pointer to the characters.
|
||||
const char* c_str() const {
|
||||
@@ -41,10 +49,10 @@ class JsonString {
|
||||
return str_.isNull();
|
||||
}
|
||||
|
||||
// Returns true if the string is stored by address.
|
||||
// Returns false if the string is stored by copy.
|
||||
// Deprecated: always returns false.
|
||||
ARDUINOJSON_DEPRECATED("The isStatic() was removed in v7.5")
|
||||
bool isStatic() const {
|
||||
return str_.isStatic();
|
||||
return false;
|
||||
}
|
||||
|
||||
// Returns length of the string.
|
||||
|
||||
@@ -8,13 +8,6 @@
|
||||
|
||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
|
||||
// a meta function that tells if the type is a string literal (const char[N])
|
||||
template <typename T>
|
||||
struct IsStringLiteral : false_type {};
|
||||
|
||||
template <size_t N>
|
||||
struct IsStringLiteral<const char (&)[N]> : true_type {};
|
||||
|
||||
template <typename TString, typename Enable = void>
|
||||
struct StringAdapter;
|
||||
|
||||
@@ -22,9 +15,7 @@ template <typename TString, typename Enable = void>
|
||||
struct SizedStringAdapter;
|
||||
|
||||
template <typename TString>
|
||||
using StringAdapterFor =
|
||||
StringAdapter<conditional_t<IsStringLiteral<TString>::value, TString,
|
||||
remove_cv_t<remove_reference_t<TString>>>>;
|
||||
using StringAdapterFor = StringAdapter<decay_t<TString>>;
|
||||
|
||||
template <typename T>
|
||||
using AdaptedString = typename StringAdapterFor<T>::AdaptedString;
|
||||
@@ -34,7 +25,7 @@ AdaptedString<TString> adaptString(TString&& s) {
|
||||
return StringAdapterFor<TString>::adapt(detail::forward<TString>(s));
|
||||
}
|
||||
|
||||
template <typename TChar, enable_if_t<!is_const<TChar>::value, int> = 0>
|
||||
template <typename TChar>
|
||||
AdaptedString<TChar*> adaptString(TChar* p) {
|
||||
return StringAdapter<TChar*>::adapt(p);
|
||||
}
|
||||
|
||||
@@ -25,9 +25,10 @@ struct Converter {
|
||||
"type 'char' is not supported, use 'signed char', 'unsigned "
|
||||
"char' or another integer type instead");
|
||||
|
||||
static void toJson(const T& src, JsonVariant dst) {
|
||||
static auto toJson(const T& src, JsonVariant dst)
|
||||
-> decltype(convertToJson(src, dst)) {
|
||||
// clang-format off
|
||||
convertToJson(src, dst); // Error here? See https://arduinojson.org/v7/unsupported-set/
|
||||
return convertToJson(src, dst); // Error here? See https://arduinojson.org/v7/unsupported-set/
|
||||
// clang-format on
|
||||
}
|
||||
|
||||
@@ -60,25 +61,16 @@ struct Converter<T, detail::enable_if_t<detail::is_integral<T>::value &&
|
||||
: private detail::VariantAttorney {
|
||||
static bool toJson(T src, JsonVariant dst) {
|
||||
ARDUINOJSON_ASSERT_INTEGER_TYPE_IS_SUPPORTED(T);
|
||||
auto data = getData(dst);
|
||||
if (!data)
|
||||
return false;
|
||||
auto resources = getResourceManager(dst);
|
||||
data->clear(resources);
|
||||
return data->setInteger(src, resources);
|
||||
return getVariantImpl(dst).setInteger(src);
|
||||
}
|
||||
|
||||
static T fromJson(JsonVariantConst src) {
|
||||
ARDUINOJSON_ASSERT_INTEGER_TYPE_IS_SUPPORTED(T);
|
||||
auto data = getData(src);
|
||||
auto resources = getResourceManager(src);
|
||||
return data ? data->template asIntegral<T>(resources) : T();
|
||||
return getVariantImpl(src).template asIntegral<T>();
|
||||
}
|
||||
|
||||
static bool checkJson(JsonVariantConst src) {
|
||||
auto data = getData(src);
|
||||
auto resources = getResourceManager(src);
|
||||
return data && data->template isInteger<T>(resources);
|
||||
return getVariantImpl(src).template isInteger<T>();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -90,35 +82,22 @@ struct Converter<T, detail::enable_if_t<detail::is_enum<T>::value>>
|
||||
}
|
||||
|
||||
static T fromJson(JsonVariantConst src) {
|
||||
auto data = getData(src);
|
||||
auto resources = getResourceManager(src);
|
||||
return data ? static_cast<T>(data->template asIntegral<int>(resources))
|
||||
: T();
|
||||
return static_cast<T>(getVariantImpl(src).template asIntegral<int>());
|
||||
}
|
||||
|
||||
static bool checkJson(JsonVariantConst src) {
|
||||
auto data = getData(src);
|
||||
auto resources = getResourceManager(src);
|
||||
return data && data->template isInteger<int>(resources);
|
||||
return getVariantImpl(src).template isInteger<int>();
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<bool> : private detail::VariantAttorney {
|
||||
static bool toJson(bool src, JsonVariant dst) {
|
||||
auto data = getData(dst);
|
||||
if (!data)
|
||||
return false;
|
||||
auto resources = getResourceManager(dst);
|
||||
data->clear(resources);
|
||||
data->setBoolean(src);
|
||||
return true;
|
||||
return getVariantImpl(dst).setBoolean(src);
|
||||
}
|
||||
|
||||
static bool fromJson(JsonVariantConst src) {
|
||||
auto data = getData(src);
|
||||
auto resources = getResourceManager(src);
|
||||
return data ? data->asBoolean(resources) : false;
|
||||
return getVariantImpl(src).asBoolean();
|
||||
}
|
||||
|
||||
static bool checkJson(JsonVariantConst src) {
|
||||
@@ -131,18 +110,11 @@ template <typename T>
|
||||
struct Converter<T, detail::enable_if_t<detail::is_floating_point<T>::value>>
|
||||
: private detail::VariantAttorney {
|
||||
static bool toJson(T src, JsonVariant dst) {
|
||||
auto data = getData(dst);
|
||||
if (!data)
|
||||
return false;
|
||||
auto resources = getResourceManager(dst);
|
||||
data->clear(resources);
|
||||
return data->setFloat(src, resources);
|
||||
return getVariantImpl(dst).setFloat(src);
|
||||
}
|
||||
|
||||
static T fromJson(JsonVariantConst src) {
|
||||
auto data = getData(src);
|
||||
auto resources = getResourceManager(src);
|
||||
return data ? data->template asFloat<T>(resources) : 0;
|
||||
return getVariantImpl(src).template asFloat<T>();
|
||||
}
|
||||
|
||||
static bool checkJson(JsonVariantConst src) {
|
||||
@@ -153,9 +125,8 @@ struct Converter<T, detail::enable_if_t<detail::is_floating_point<T>::value>>
|
||||
|
||||
template <>
|
||||
struct Converter<const char*> : private detail::VariantAttorney {
|
||||
static void toJson(const char* src, JsonVariant dst) {
|
||||
detail::VariantData::setString(getData(dst), detail::adaptString(src),
|
||||
getResourceManager(dst));
|
||||
static bool toJson(const char* src, JsonVariant dst) {
|
||||
return getVariantImpl(dst).setString(detail::adaptString(src));
|
||||
}
|
||||
|
||||
static const char* fromJson(JsonVariantConst src) {
|
||||
@@ -171,9 +142,8 @@ struct Converter<const char*> : private detail::VariantAttorney {
|
||||
|
||||
template <>
|
||||
struct Converter<JsonString> : private detail::VariantAttorney {
|
||||
static void toJson(JsonString src, JsonVariant dst) {
|
||||
detail::VariantData::setString(getData(dst), detail::adaptString(src),
|
||||
getResourceManager(dst));
|
||||
static bool toJson(JsonString src, JsonVariant dst) {
|
||||
return getVariantImpl(dst).setString(detail::adaptString(src));
|
||||
}
|
||||
|
||||
static JsonString fromJson(JsonVariantConst src) {
|
||||
@@ -188,12 +158,10 @@ struct Converter<JsonString> : private detail::VariantAttorney {
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
inline detail::enable_if_t<detail::IsString<T>::value> convertToJson(
|
||||
inline detail::enable_if_t<detail::IsString<T>::value, bool> convertToJson(
|
||||
const T& src, JsonVariant dst) {
|
||||
using namespace detail;
|
||||
auto data = VariantAttorney::getData(dst);
|
||||
auto resources = VariantAttorney::getResourceManager(dst);
|
||||
detail::VariantData::setString(data, adaptString(src), resources);
|
||||
return detail::VariantAttorney::getVariantImpl(dst).setString(
|
||||
detail::adaptString(src));
|
||||
}
|
||||
|
||||
// SerializedValue<std::string>
|
||||
@@ -201,16 +169,16 @@ inline detail::enable_if_t<detail::IsString<T>::value> convertToJson(
|
||||
// SerializedValue<const __FlashStringHelper*>
|
||||
template <typename T>
|
||||
struct Converter<SerializedValue<T>> : private detail::VariantAttorney {
|
||||
static void toJson(SerializedValue<T> src, JsonVariant dst) {
|
||||
detail::VariantData::setRawString(getData(dst), src,
|
||||
getResourceManager(dst));
|
||||
static bool toJson(SerializedValue<T> src, JsonVariant dst) {
|
||||
return getVariantImpl(dst).setRawString(
|
||||
detail::adaptString(src.data(), src.size()));
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<detail::nullptr_t> : private detail::VariantAttorney {
|
||||
static void toJson(detail::nullptr_t, JsonVariant dst) {
|
||||
detail::VariantData::clear(getData(dst), getResourceManager(dst));
|
||||
static bool toJson(detail::nullptr_t, JsonVariant dst) {
|
||||
return getVariantImpl(dst).clear();
|
||||
}
|
||||
static detail::nullptr_t fromJson(JsonVariantConst) {
|
||||
return nullptr;
|
||||
@@ -258,17 +226,18 @@ class StringBuilderPrint : public Print {
|
||||
};
|
||||
} // namespace detail
|
||||
|
||||
inline void convertToJson(const ::Printable& src, JsonVariant dst) {
|
||||
inline bool convertToJson(const ::Printable& src, JsonVariant dst) {
|
||||
auto resources = detail::VariantAttorney::getResourceManager(dst);
|
||||
auto data = detail::VariantAttorney::getData(dst);
|
||||
if (!resources || !data)
|
||||
return;
|
||||
data->clear(resources);
|
||||
return false;
|
||||
detail::VariantImpl::clear(data, resources);
|
||||
detail::StringBuilderPrint print(resources);
|
||||
src.printTo(print);
|
||||
if (print.overflowed())
|
||||
return;
|
||||
return false;
|
||||
print.save(data);
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -321,17 +290,15 @@ inline bool canConvertFromJson(JsonVariantConst src, const std::string_view&) {
|
||||
|
||||
template <>
|
||||
struct Converter<JsonArrayConst> : private detail::VariantAttorney {
|
||||
static void toJson(JsonArrayConst src, JsonVariant dst) {
|
||||
static bool toJson(JsonArrayConst src, JsonVariant dst) {
|
||||
if (src.isNull())
|
||||
dst.set(nullptr);
|
||||
return dst.set(nullptr);
|
||||
else
|
||||
dst.to<JsonArray>().set(src);
|
||||
return dst.to<JsonArray>().set(src);
|
||||
}
|
||||
|
||||
static JsonArrayConst fromJson(JsonVariantConst src) {
|
||||
auto data = getData(src);
|
||||
auto array = data ? data->asArray() : nullptr;
|
||||
return JsonArrayConst(array, getResourceManager(src));
|
||||
return JsonArrayConst(getData(src), getResourceManager(src));
|
||||
}
|
||||
|
||||
static bool checkJson(JsonVariantConst src) {
|
||||
@@ -342,17 +309,15 @@ struct Converter<JsonArrayConst> : private detail::VariantAttorney {
|
||||
|
||||
template <>
|
||||
struct Converter<JsonArray> : private detail::VariantAttorney {
|
||||
static void toJson(JsonVariantConst src, JsonVariant dst) {
|
||||
static bool toJson(JsonVariantConst src, JsonVariant dst) {
|
||||
if (src.isNull())
|
||||
dst.set(nullptr);
|
||||
return dst.set(nullptr);
|
||||
else
|
||||
dst.to<JsonArray>().set(src);
|
||||
return dst.to<JsonArray>().set(src);
|
||||
}
|
||||
|
||||
static JsonArray fromJson(JsonVariant src) {
|
||||
auto data = getData(src);
|
||||
auto resources = getResourceManager(src);
|
||||
return JsonArray(data != 0 ? data->asArray() : 0, resources);
|
||||
return JsonArray(getData(src), getResourceManager(src));
|
||||
}
|
||||
|
||||
static bool checkJson(JsonVariant src) {
|
||||
@@ -363,17 +328,15 @@ struct Converter<JsonArray> : private detail::VariantAttorney {
|
||||
|
||||
template <>
|
||||
struct Converter<JsonObjectConst> : private detail::VariantAttorney {
|
||||
static void toJson(JsonVariantConst src, JsonVariant dst) {
|
||||
static bool toJson(JsonVariantConst src, JsonVariant dst) {
|
||||
if (src.isNull())
|
||||
dst.set(nullptr);
|
||||
return dst.set(nullptr);
|
||||
else
|
||||
dst.to<JsonObject>().set(src);
|
||||
return dst.to<JsonObject>().set(src);
|
||||
}
|
||||
|
||||
static JsonObjectConst fromJson(JsonVariantConst src) {
|
||||
auto data = getData(src);
|
||||
auto object = data != 0 ? data->asObject() : nullptr;
|
||||
return JsonObjectConst(object, getResourceManager(src));
|
||||
return JsonObjectConst(getData(src), getResourceManager(src));
|
||||
}
|
||||
|
||||
static bool checkJson(JsonVariantConst src) {
|
||||
@@ -384,17 +347,15 @@ struct Converter<JsonObjectConst> : private detail::VariantAttorney {
|
||||
|
||||
template <>
|
||||
struct Converter<JsonObject> : private detail::VariantAttorney {
|
||||
static void toJson(JsonVariantConst src, JsonVariant dst) {
|
||||
static bool toJson(JsonVariantConst src, JsonVariant dst) {
|
||||
if (src.isNull())
|
||||
dst.set(nullptr);
|
||||
return dst.set(nullptr);
|
||||
else
|
||||
dst.to<JsonObject>().set(src);
|
||||
return dst.to<JsonObject>().set(src);
|
||||
}
|
||||
|
||||
static JsonObject fromJson(JsonVariant src) {
|
||||
auto data = getData(src);
|
||||
auto resources = getResourceManager(src);
|
||||
return JsonObject(data != 0 ? data->asObject() : 0, resources);
|
||||
return JsonObject(getData(src), getResourceManager(src));
|
||||
}
|
||||
|
||||
static bool checkJson(JsonVariant src) {
|
||||
|
||||
@@ -16,27 +16,29 @@ class JsonVariant : public detail::VariantRefBase<JsonVariant>,
|
||||
|
||||
public:
|
||||
// Creates an unbound reference.
|
||||
JsonVariant() : data_(0), resources_(0) {}
|
||||
JsonVariant() {}
|
||||
|
||||
// INTERNAL USE ONLY
|
||||
JsonVariant(detail::VariantData* data, detail::ResourceManager* resources)
|
||||
: data_(data), resources_(resources) {}
|
||||
: impl_(data, resources) {}
|
||||
|
||||
// INTERNAL USE ONLY
|
||||
JsonVariant(detail::VariantImpl impl) : impl_(impl) {}
|
||||
|
||||
private:
|
||||
detail::ResourceManager* getResourceManager() const {
|
||||
return resources_;
|
||||
return impl_.resources();
|
||||
}
|
||||
|
||||
detail::VariantData* getData() const {
|
||||
return data_;
|
||||
return impl_.data();
|
||||
}
|
||||
|
||||
detail::VariantData* getOrCreateData() const {
|
||||
return data_;
|
||||
return impl_.data();
|
||||
}
|
||||
|
||||
detail::VariantData* data_;
|
||||
detail::ResourceManager* resources_;
|
||||
mutable detail::VariantImpl impl_;
|
||||
};
|
||||
|
||||
namespace detail {
|
||||
@@ -45,8 +47,8 @@ bool copyVariant(JsonVariant dst, JsonVariantConst src);
|
||||
|
||||
template <>
|
||||
struct Converter<JsonVariant> : private detail::VariantAttorney {
|
||||
static void toJson(JsonVariantConst src, JsonVariant dst) {
|
||||
copyVariant(dst, src);
|
||||
static bool toJson(JsonVariantConst src, JsonVariant dst) {
|
||||
return copyVariant(dst, src);
|
||||
}
|
||||
|
||||
static JsonVariant fromJson(JsonVariant src) {
|
||||
@@ -61,8 +63,8 @@ struct Converter<JsonVariant> : private detail::VariantAttorney {
|
||||
|
||||
template <>
|
||||
struct Converter<JsonVariantConst> : private detail::VariantAttorney {
|
||||
static void toJson(JsonVariantConst src, JsonVariant dst) {
|
||||
copyVariant(dst, src);
|
||||
static bool toJson(JsonVariantConst src, JsonVariant dst) {
|
||||
return copyVariant(dst, src);
|
||||
}
|
||||
|
||||
static JsonVariantConst fromJson(JsonVariantConst src) {
|
||||
|
||||
@@ -35,34 +35,34 @@ class JsonVariantConst : public detail::VariantTag,
|
||||
|
||||
public:
|
||||
// Creates an unbound reference.
|
||||
JsonVariantConst() : data_(nullptr), resources_(nullptr) {}
|
||||
JsonVariantConst() {}
|
||||
|
||||
// INTERNAL USE ONLY
|
||||
explicit JsonVariantConst(const detail::VariantData* data,
|
||||
const detail::ResourceManager* resources)
|
||||
: data_(data), resources_(resources) {}
|
||||
explicit JsonVariantConst(detail::VariantData* data,
|
||||
detail::ResourceManager* resources)
|
||||
: impl_(data, resources) {}
|
||||
|
||||
// Returns true if the value is null or the reference is unbound.
|
||||
// https://arduinojson.org/v7/api/jsonvariantconst/isnull/
|
||||
bool isNull() const {
|
||||
return detail::VariantData::isNull(data_);
|
||||
return impl_.isNull();
|
||||
}
|
||||
|
||||
// Returns true if the reference is unbound.
|
||||
bool isUnbound() const {
|
||||
return !data_;
|
||||
return impl_.data() == nullptr;
|
||||
}
|
||||
|
||||
// Returns the depth (nesting level) of the value.
|
||||
// https://arduinojson.org/v7/api/jsonvariantconst/nesting/
|
||||
size_t nesting() const {
|
||||
return detail::VariantData::nesting(data_, resources_);
|
||||
return impl_.nesting();
|
||||
}
|
||||
|
||||
// Returns the size of the array or object.
|
||||
// https://arduinojson.org/v7/api/jsonvariantconst/size/
|
||||
size_t size() const {
|
||||
return detail::VariantData::size(data_, resources_);
|
||||
return impl_.size();
|
||||
}
|
||||
|
||||
// Casts the value to the specified type.
|
||||
@@ -104,9 +104,7 @@ class JsonVariantConst : public detail::VariantTag,
|
||||
template <typename T,
|
||||
detail::enable_if_t<detail::is_integral<T>::value, int> = 0>
|
||||
JsonVariantConst operator[](T index) const {
|
||||
return JsonVariantConst(
|
||||
detail::VariantData::getElement(data_, size_t(index), resources_),
|
||||
resources_);
|
||||
return JsonVariantConst(impl_.getElement(size_t(index)), impl_.resources());
|
||||
}
|
||||
|
||||
// Gets object's member with specified key.
|
||||
@@ -114,21 +112,17 @@ class JsonVariantConst : public detail::VariantTag,
|
||||
template <typename TString,
|
||||
detail::enable_if_t<detail::IsString<TString>::value, int> = 0>
|
||||
JsonVariantConst operator[](const TString& key) const {
|
||||
return JsonVariantConst(detail::VariantData::getMember(
|
||||
data_, detail::adaptString(key), resources_),
|
||||
resources_);
|
||||
return JsonVariantConst(impl_.getMember(detail::adaptString(key)),
|
||||
impl_.resources());
|
||||
}
|
||||
|
||||
// Gets object's member with specified key.
|
||||
// https://arduinojson.org/v7/api/jsonvariantconst/subscript/
|
||||
template <typename TChar,
|
||||
detail::enable_if_t<detail::IsString<TChar*>::value &&
|
||||
!detail::is_const<TChar>::value,
|
||||
int> = 0>
|
||||
detail::enable_if_t<detail::IsString<TChar*>::value, int> = 0>
|
||||
JsonVariantConst operator[](TChar* key) const {
|
||||
return JsonVariantConst(detail::VariantData::getMember(
|
||||
data_, detail::adaptString(key), resources_),
|
||||
resources_);
|
||||
return JsonVariantConst(impl_.getMember(detail::adaptString(key)),
|
||||
impl_.resources());
|
||||
}
|
||||
|
||||
// Gets object's member with specified key or the array's element at the
|
||||
@@ -149,20 +143,16 @@ class JsonVariantConst : public detail::VariantTag,
|
||||
detail::enable_if_t<detail::IsString<TString>::value, int> = 0>
|
||||
ARDUINOJSON_DEPRECATED("use var[key].is<T>() instead")
|
||||
bool containsKey(const TString& key) const {
|
||||
return detail::VariantData::getMember(getData(), detail::adaptString(key),
|
||||
resources_) != 0;
|
||||
return impl_.getMember(detail::adaptString(key)) != 0;
|
||||
}
|
||||
|
||||
// DEPRECATED: use obj["key"].is<T>() instead
|
||||
// https://arduinojson.org/v7/api/jsonvariantconst/containskey/
|
||||
template <typename TChar,
|
||||
detail::enable_if_t<detail::IsString<TChar*>::value &&
|
||||
!detail::is_const<TChar>::value,
|
||||
int> = 0>
|
||||
detail::enable_if_t<detail::IsString<TChar*>::value, int> = 0>
|
||||
ARDUINOJSON_DEPRECATED("use obj[\"key\"].is<T>() instead")
|
||||
bool containsKey(TChar* key) const {
|
||||
return detail::VariantData::getMember(getData(), detail::adaptString(key),
|
||||
resources_) != 0;
|
||||
return impl_.getMember(detail::adaptString(key)) != 0;
|
||||
}
|
||||
|
||||
// DEPRECATED: use obj[key].is<T>() instead
|
||||
@@ -181,17 +171,16 @@ class JsonVariantConst : public detail::VariantTag,
|
||||
}
|
||||
|
||||
protected:
|
||||
const detail::VariantData* getData() const {
|
||||
return data_;
|
||||
detail::VariantData* getData() const {
|
||||
return impl_.data();
|
||||
}
|
||||
|
||||
const detail::ResourceManager* getResourceManager() const {
|
||||
return resources_;
|
||||
detail::ResourceManager* getResourceManager() const {
|
||||
return impl_.resources();
|
||||
}
|
||||
|
||||
private:
|
||||
const detail::VariantData* data_;
|
||||
const detail::ResourceManager* resources_;
|
||||
mutable detail::VariantImpl impl_;
|
||||
};
|
||||
|
||||
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
||||
|
||||
@@ -26,15 +26,15 @@ class VisitorAdapter {
|
||||
public:
|
||||
using result_type = typename TVisitor::result_type;
|
||||
|
||||
VisitorAdapter(TVisitor& visitor, const ResourceManager* resources)
|
||||
VisitorAdapter(TVisitor& visitor, ResourceManager* resources)
|
||||
: visitor_(&visitor), resources_(resources) {}
|
||||
|
||||
result_type visit(const ArrayData& value) {
|
||||
return visitor_->visit(JsonArrayConst(&value, resources_));
|
||||
result_type visitArray(VariantData* data) {
|
||||
return visitor_->visit(JsonArrayConst(data, resources_));
|
||||
}
|
||||
|
||||
result_type visit(const ObjectData& value) {
|
||||
return visitor_->visit(JsonObjectConst(&value, resources_));
|
||||
result_type visitObject(VariantData* data) {
|
||||
return visitor_->visit(JsonObjectConst(data, resources_));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
@@ -44,18 +44,15 @@ class VisitorAdapter {
|
||||
|
||||
private:
|
||||
TVisitor* visitor_;
|
||||
const ResourceManager* resources_;
|
||||
ResourceManager* resources_;
|
||||
};
|
||||
|
||||
template <typename TVisitor>
|
||||
typename TVisitor::result_type accept(JsonVariantConst variant,
|
||||
TVisitor& visit) {
|
||||
auto data = VariantAttorney::getData(variant);
|
||||
if (!data)
|
||||
return visit.visit(nullptr);
|
||||
auto resources = VariantAttorney::getResourceManager(variant);
|
||||
VisitorAdapter<TVisitor> adapter(visit, resources);
|
||||
return data->accept(adapter, resources);
|
||||
VisitorAdapter<TVisitor> adapter(
|
||||
visit, VariantAttorney::getResourceManager(variant));
|
||||
return VariantAttorney::getVariantImpl(variant).accept(adapter);
|
||||
}
|
||||
|
||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||
|
||||
@@ -6,8 +6,7 @@
|
||||
|
||||
#include <ArduinoJson/Polyfills/attributes.hpp>
|
||||
#include <ArduinoJson/Polyfills/type_traits.hpp>
|
||||
#include <ArduinoJson/Variant/VariantData.hpp>
|
||||
#include <ArduinoJson/Variant/VariantTo.hpp>
|
||||
#include <ArduinoJson/Variant/VariantImpl.hpp>
|
||||
#include "JsonVariantConst.hpp"
|
||||
|
||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
@@ -26,6 +25,16 @@ class VariantAttorney {
|
||||
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>
|
||||
static VariantData* getOrCreateData(TClient& client) {
|
||||
return client.getOrCreateData();
|
||||
|
||||
@@ -6,32 +6,29 @@
|
||||
|
||||
#include <stddef.h> // size_t
|
||||
|
||||
#include <ArduinoJson/Array/ArrayData.hpp>
|
||||
#include <ArduinoJson/Numbers/JsonFloat.hpp>
|
||||
#include <ArduinoJson/Numbers/JsonInteger.hpp>
|
||||
#include <ArduinoJson/Object/ObjectData.hpp>
|
||||
|
||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
|
||||
enum class VariantTypeBits : uint8_t {
|
||||
OwnedStringBit = 0x01, // 0000 0001
|
||||
NumberBit = 0x08, // 0000 1000
|
||||
#if ARDUINOJSON_USE_EXTENSIONS
|
||||
ExtensionBit = 0x10, // 0001 0000
|
||||
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||
EightByteBit = 0x10, // 0001 0000
|
||||
#endif
|
||||
CollectionMask = 0x60,
|
||||
};
|
||||
|
||||
enum class VariantType : uint8_t {
|
||||
Null = 0, // 0000 0000
|
||||
TinyString = 0x02, // 0000 0010
|
||||
RawString = 0x03, // 0000 0011
|
||||
LinkedString = 0x04, // 0000 0100
|
||||
OwnedString = 0x05, // 0000 0101
|
||||
Boolean = 0x06, // 0000 0110
|
||||
Uint32 = 0x0A, // 0000 1010
|
||||
Int32 = 0x0C, // 0000 1100
|
||||
Float = 0x0E, // 0000 1110
|
||||
Null = 0, // 0000 0000
|
||||
TinyString = 0x02, // 0000 0010
|
||||
RawString = 0x03, // 0000 0011
|
||||
LongString = 0x05, // 0000 0101
|
||||
Boolean = 0x06, // 0000 0110
|
||||
Uint32 = 0x0A, // 0000 1010
|
||||
Int32 = 0x0C, // 0000 1100
|
||||
Float = 0x0E, // 0000 1110
|
||||
#if ARDUINOJSON_USE_LONG_LONG
|
||||
Uint64 = 0x1A, // 0001 1010
|
||||
Int64 = 0x1C, // 0001 1100
|
||||
@@ -47,6 +44,18 @@ inline bool operator&(VariantType type, VariantTypeBits bit) {
|
||||
return (uint8_t(type) & uint8_t(bit)) != 0;
|
||||
}
|
||||
|
||||
struct CollectionData {
|
||||
SlotId head = NULL_SLOT;
|
||||
SlotId tail = NULL_SLOT;
|
||||
|
||||
// Placement new
|
||||
static void* operator new(size_t, void* p) noexcept {
|
||||
return p;
|
||||
}
|
||||
|
||||
static void operator delete(void*, void*) noexcept {}
|
||||
};
|
||||
|
||||
const size_t tinyStringMaxLength = 3;
|
||||
|
||||
union VariantContent {
|
||||
@@ -56,19 +65,16 @@ union VariantContent {
|
||||
bool asBoolean;
|
||||
uint32_t asUint32;
|
||||
int32_t asInt32;
|
||||
#if ARDUINOJSON_USE_EXTENSIONS
|
||||
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||
SlotId asSlotId;
|
||||
#endif
|
||||
ArrayData asArray;
|
||||
ObjectData asObject;
|
||||
CollectionData asCollection;
|
||||
const char* asLinkedString;
|
||||
struct StringNode* asOwnedString;
|
||||
struct StringNode* asStringNode;
|
||||
char asTinyString[tinyStringMaxLength + 1];
|
||||
};
|
||||
|
||||
#if ARDUINOJSON_USE_EXTENSIONS
|
||||
union VariantExtension {
|
||||
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||
union EightByteValue {
|
||||
# if ARDUINOJSON_USE_LONG_LONG
|
||||
uint64_t asUint64;
|
||||
int64_t asInt64;
|
||||
@@ -77,6 +83,9 @@ union VariantExtension {
|
||||
double asDouble;
|
||||
# endif
|
||||
};
|
||||
|
||||
static_assert(sizeof(EightByteValue) == 8,
|
||||
"sizeof(EightByteValue) must be 8 bytes");
|
||||
#endif
|
||||
|
||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||
|
||||
@@ -4,12 +4,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson/Memory/MemoryPool.hpp>
|
||||
#include <ArduinoJson/Memory/StringNode.hpp>
|
||||
#include <ArduinoJson/Misc/SerializedValue.hpp>
|
||||
#include <ArduinoJson/Numbers/convertNumber.hpp>
|
||||
#include <ArduinoJson/Strings/JsonString.hpp>
|
||||
#include <ArduinoJson/Strings/StringAdapters.hpp>
|
||||
#include <ArduinoJson/Variant/VariantContent.hpp>
|
||||
|
||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
@@ -27,12 +23,11 @@ static bool isTinyString(const T& s, size_t n) {
|
||||
return !containsNul;
|
||||
}
|
||||
|
||||
class VariantData {
|
||||
VariantContent content_; // must be first to allow cast from array to variant
|
||||
VariantType type_;
|
||||
SlotId next_;
|
||||
struct VariantData {
|
||||
VariantContent content; // must be first to allow cast from array to variant
|
||||
VariantType type = VariantType::Null;
|
||||
SlotId next = NULL_SLOT;
|
||||
|
||||
public:
|
||||
// Placement new
|
||||
static void* operator new(size_t, void* p) noexcept {
|
||||
return p;
|
||||
@@ -40,569 +35,103 @@ class VariantData {
|
||||
|
||||
static void operator delete(void*, void*) noexcept {}
|
||||
|
||||
VariantData() : type_(VariantType::Null), next_(NULL_SLOT) {}
|
||||
|
||||
SlotId next() const {
|
||||
return next_;
|
||||
}
|
||||
|
||||
void setNext(SlotId slot) {
|
||||
next_ = slot;
|
||||
}
|
||||
|
||||
template <typename TVisitor>
|
||||
typename TVisitor::result_type accept(
|
||||
TVisitor& visit, const ResourceManager* resources) const {
|
||||
#if ARDUINOJSON_USE_EXTENSIONS
|
||||
auto extension = getExtension(resources);
|
||||
#else
|
||||
(void)resources; // silence warning
|
||||
#endif
|
||||
switch (type_) {
|
||||
case VariantType::Float:
|
||||
return visit.visit(content_.asFloat);
|
||||
|
||||
#if ARDUINOJSON_USE_DOUBLE
|
||||
case VariantType::Double:
|
||||
return visit.visit(extension->asDouble);
|
||||
#endif
|
||||
|
||||
case VariantType::Array:
|
||||
return visit.visit(content_.asArray);
|
||||
|
||||
case VariantType::Object:
|
||||
return visit.visit(content_.asObject);
|
||||
|
||||
case VariantType::TinyString:
|
||||
return visit.visit(JsonString(content_.asTinyString));
|
||||
|
||||
case VariantType::LinkedString:
|
||||
return visit.visit(JsonString(content_.asLinkedString, true));
|
||||
|
||||
case VariantType::OwnedString:
|
||||
return visit.visit(JsonString(content_.asOwnedString->data,
|
||||
content_.asOwnedString->length));
|
||||
|
||||
case VariantType::RawString:
|
||||
return visit.visit(RawString(content_.asOwnedString->data,
|
||||
content_.asOwnedString->length));
|
||||
|
||||
case VariantType::Int32:
|
||||
return visit.visit(static_cast<JsonInteger>(content_.asInt32));
|
||||
|
||||
case VariantType::Uint32:
|
||||
return visit.visit(static_cast<JsonUInt>(content_.asUint32));
|
||||
|
||||
#if ARDUINOJSON_USE_LONG_LONG
|
||||
case VariantType::Int64:
|
||||
return visit.visit(extension->asInt64);
|
||||
|
||||
case VariantType::Uint64:
|
||||
return visit.visit(extension->asUint64);
|
||||
#endif
|
||||
|
||||
case VariantType::Boolean:
|
||||
return visit.visit(content_.asBoolean != 0);
|
||||
|
||||
default:
|
||||
return visit.visit(nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename TVisitor>
|
||||
static typename TVisitor::result_type accept(const VariantData* var,
|
||||
const ResourceManager* resources,
|
||||
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>
|
||||
bool addValue(const T& value, ResourceManager* resources) {
|
||||
auto array = isNull() ? &toArray() : asArray();
|
||||
return detail::ArrayData::addValue(array, value, resources);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static bool addValue(VariantData* var, const T& value,
|
||||
ResourceManager* resources) {
|
||||
if (!var)
|
||||
return false;
|
||||
return var->addValue(value, resources);
|
||||
}
|
||||
|
||||
bool asBoolean(const ResourceManager* resources) const {
|
||||
#if ARDUINOJSON_USE_EXTENSIONS
|
||||
auto extension = getExtension(resources);
|
||||
#else
|
||||
(void)resources; // silence warning
|
||||
#endif
|
||||
switch (type_) {
|
||||
case VariantType::Boolean:
|
||||
return content_.asBoolean;
|
||||
case VariantType::Uint32:
|
||||
case VariantType::Int32:
|
||||
return content_.asUint32 != 0;
|
||||
case VariantType::Float:
|
||||
return content_.asFloat != 0;
|
||||
#if ARDUINOJSON_USE_DOUBLE
|
||||
case VariantType::Double:
|
||||
return extension->asDouble != 0;
|
||||
#endif
|
||||
case VariantType::Null:
|
||||
return false;
|
||||
#if ARDUINOJSON_USE_LONG_LONG
|
||||
case VariantType::Uint64:
|
||||
case VariantType::Int64:
|
||||
return extension->asUint64 != 0;
|
||||
#endif
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
ArrayData* asArray() {
|
||||
return isArray() ? &content_.asArray : 0;
|
||||
}
|
||||
|
||||
const ArrayData* asArray() const {
|
||||
return const_cast<VariantData*>(this)->asArray();
|
||||
}
|
||||
|
||||
CollectionData* asCollection() {
|
||||
return isCollection() ? &content_.asCollection : 0;
|
||||
}
|
||||
|
||||
const CollectionData* asCollection() const {
|
||||
return const_cast<VariantData*>(this)->asCollection();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T asFloat(const ResourceManager* resources) const {
|
||||
static_assert(is_floating_point<T>::value, "T must be a floating point");
|
||||
#if ARDUINOJSON_USE_EXTENSIONS
|
||||
auto extension = getExtension(resources);
|
||||
#else
|
||||
(void)resources; // silence warning
|
||||
#endif
|
||||
const char* str = nullptr;
|
||||
switch (type_) {
|
||||
case VariantType::Boolean:
|
||||
return static_cast<T>(content_.asBoolean);
|
||||
case VariantType::Uint32:
|
||||
return static_cast<T>(content_.asUint32);
|
||||
case VariantType::Int32:
|
||||
return static_cast<T>(content_.asInt32);
|
||||
#if ARDUINOJSON_USE_LONG_LONG
|
||||
case VariantType::Uint64:
|
||||
return static_cast<T>(extension->asUint64);
|
||||
case VariantType::Int64:
|
||||
return static_cast<T>(extension->asInt64);
|
||||
#endif
|
||||
case VariantType::TinyString:
|
||||
str = content_.asTinyString;
|
||||
break;
|
||||
case VariantType::LinkedString:
|
||||
str = content_.asLinkedString;
|
||||
break;
|
||||
case VariantType::OwnedString:
|
||||
str = content_.asOwnedString->data;
|
||||
break;
|
||||
case VariantType::Float:
|
||||
return static_cast<T>(content_.asFloat);
|
||||
#if ARDUINOJSON_USE_DOUBLE
|
||||
case VariantType::Double:
|
||||
return static_cast<T>(extension->asDouble);
|
||||
#endif
|
||||
default:
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
ARDUINOJSON_ASSERT(str != nullptr);
|
||||
return parseNumber<T>(str);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T asIntegral(const ResourceManager* resources) const {
|
||||
static_assert(is_integral<T>::value, "T must be an integral type");
|
||||
#if ARDUINOJSON_USE_EXTENSIONS
|
||||
auto extension = getExtension(resources);
|
||||
#else
|
||||
(void)resources; // silence warning
|
||||
#endif
|
||||
const char* str = nullptr;
|
||||
switch (type_) {
|
||||
case VariantType::Boolean:
|
||||
return content_.asBoolean;
|
||||
case VariantType::Uint32:
|
||||
return convertNumber<T>(content_.asUint32);
|
||||
case VariantType::Int32:
|
||||
return convertNumber<T>(content_.asInt32);
|
||||
#if ARDUINOJSON_USE_LONG_LONG
|
||||
case VariantType::Uint64:
|
||||
return convertNumber<T>(extension->asUint64);
|
||||
case VariantType::Int64:
|
||||
return convertNumber<T>(extension->asInt64);
|
||||
#endif
|
||||
case VariantType::TinyString:
|
||||
str = content_.asTinyString;
|
||||
break;
|
||||
case VariantType::LinkedString:
|
||||
str = content_.asLinkedString;
|
||||
break;
|
||||
case VariantType::OwnedString:
|
||||
str = content_.asOwnedString->data;
|
||||
break;
|
||||
case VariantType::Float:
|
||||
return convertNumber<T>(content_.asFloat);
|
||||
#if ARDUINOJSON_USE_DOUBLE
|
||||
case VariantType::Double:
|
||||
return convertNumber<T>(extension->asDouble);
|
||||
#endif
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
|
||||
ARDUINOJSON_ASSERT(str != nullptr);
|
||||
return parseNumber<T>(str);
|
||||
}
|
||||
|
||||
ObjectData* asObject() {
|
||||
return isObject() ? &content_.asObject : 0;
|
||||
}
|
||||
|
||||
const ObjectData* asObject() const {
|
||||
return const_cast<VariantData*>(this)->asObject();
|
||||
}
|
||||
|
||||
JsonString asRawString() const {
|
||||
switch (type_) {
|
||||
switch (type) {
|
||||
case VariantType::RawString:
|
||||
return JsonString(content_.asOwnedString->data,
|
||||
content_.asOwnedString->length);
|
||||
return JsonString(content.asStringNode->data,
|
||||
content.asStringNode->length);
|
||||
default:
|
||||
return JsonString();
|
||||
}
|
||||
}
|
||||
|
||||
JsonString asString() const {
|
||||
switch (type_) {
|
||||
switch (type) {
|
||||
case VariantType::TinyString:
|
||||
return JsonString(content_.asTinyString);
|
||||
case VariantType::LinkedString:
|
||||
return JsonString(content_.asLinkedString, true);
|
||||
case VariantType::OwnedString:
|
||||
return JsonString(content_.asOwnedString->data,
|
||||
content_.asOwnedString->length);
|
||||
return JsonString(content.asTinyString);
|
||||
case VariantType::LongString:
|
||||
return JsonString(content.asStringNode->data,
|
||||
content.asStringNode->length);
|
||||
default:
|
||||
return JsonString();
|
||||
}
|
||||
}
|
||||
|
||||
#if ARDUINOJSON_USE_EXTENSIONS
|
||||
const VariantExtension* getExtension(const ResourceManager* resources) const;
|
||||
#endif
|
||||
|
||||
VariantData* getElement(size_t index,
|
||||
const ResourceManager* resources) const {
|
||||
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>
|
||||
VariantData* getMember(TAdaptedString key,
|
||||
const ResourceManager* resources) const {
|
||||
return ObjectData::getMember(asObject(), key, resources);
|
||||
}
|
||||
|
||||
template <typename TAdaptedString>
|
||||
static VariantData* getMember(const VariantData* var, 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())
|
||||
return nullptr;
|
||||
auto obj = isNull() ? &toObject() : asObject();
|
||||
if (!obj)
|
||||
return nullptr;
|
||||
return obj->getOrAddMember(key, resources);
|
||||
}
|
||||
|
||||
bool isArray() const {
|
||||
return type_ == VariantType::Array;
|
||||
return type == VariantType::Array;
|
||||
}
|
||||
|
||||
bool isBoolean() const {
|
||||
return type_ == VariantType::Boolean;
|
||||
return type == VariantType::Boolean;
|
||||
}
|
||||
|
||||
bool isCollection() const {
|
||||
return type_ & VariantTypeBits::CollectionMask;
|
||||
return type & VariantTypeBits::CollectionMask;
|
||||
}
|
||||
|
||||
bool isFloat() const {
|
||||
return type_ & VariantTypeBits::NumberBit;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool isInteger(const ResourceManager* resources) const {
|
||||
#if ARDUINOJSON_USE_LONG_LONG
|
||||
auto extension = getExtension(resources);
|
||||
#else
|
||||
(void)resources; // silence warning
|
||||
#endif
|
||||
switch (type_) {
|
||||
case VariantType::Uint32:
|
||||
return canConvertNumber<T>(content_.asUint32);
|
||||
|
||||
case VariantType::Int32:
|
||||
return canConvertNumber<T>(content_.asInt32);
|
||||
|
||||
#if ARDUINOJSON_USE_LONG_LONG
|
||||
case VariantType::Uint64:
|
||||
return canConvertNumber<T>(extension->asUint64);
|
||||
|
||||
case VariantType::Int64:
|
||||
return canConvertNumber<T>(extension->asInt64);
|
||||
#endif
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return type & VariantTypeBits::NumberBit;
|
||||
}
|
||||
|
||||
bool isNull() const {
|
||||
return type_ == VariantType::Null;
|
||||
}
|
||||
|
||||
static bool isNull(const VariantData* var) {
|
||||
if (!var)
|
||||
return true;
|
||||
return var->isNull();
|
||||
return type == VariantType::Null;
|
||||
}
|
||||
|
||||
bool isObject() const {
|
||||
return type_ == VariantType::Object;
|
||||
return type == VariantType::Object;
|
||||
}
|
||||
|
||||
bool isString() const {
|
||||
return type_ == VariantType::LinkedString ||
|
||||
type_ == VariantType::OwnedString ||
|
||||
type_ == VariantType::TinyString;
|
||||
}
|
||||
|
||||
size_t nesting(const ResourceManager* resources) const {
|
||||
auto collection = asCollection();
|
||||
if (collection)
|
||||
return collection->nesting(resources);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
static size_t nesting(const VariantData* var,
|
||||
const ResourceManager* resources) {
|
||||
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>
|
||||
void removeMember(TAdaptedString key, ResourceManager* resources) {
|
||||
ObjectData::removeMember(asObject(), key, resources);
|
||||
}
|
||||
|
||||
template <typename TAdaptedString>
|
||||
static void removeMember(VariantData* var, TAdaptedString key,
|
||||
ResourceManager* resources) {
|
||||
if (!var)
|
||||
return;
|
||||
var->removeMember(key, resources);
|
||||
}
|
||||
|
||||
void reset() { // TODO: remove
|
||||
type_ = VariantType::Null;
|
||||
return type == VariantType::LongString || type == VariantType::TinyString;
|
||||
}
|
||||
|
||||
void setBoolean(bool value) {
|
||||
ARDUINOJSON_ASSERT(type_ == VariantType::Null); // must call clear() first
|
||||
type_ = VariantType::Boolean;
|
||||
content_.asBoolean = value;
|
||||
ARDUINOJSON_ASSERT(type == VariantType::Null);
|
||||
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;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
enable_if_t<sizeof(T) == 8, bool> setFloat(T value, ResourceManager*);
|
||||
|
||||
template <typename T>
|
||||
enable_if_t<is_signed<T>::value, bool> setInteger(T value,
|
||||
ResourceManager* resources);
|
||||
|
||||
template <typename T>
|
||||
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(type == VariantType::Null);
|
||||
ARDUINOJSON_ASSERT(s);
|
||||
type_ = VariantType::RawString;
|
||||
content_.asOwnedString = s;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void setRawString(SerializedValue<T> value, ResourceManager* resources);
|
||||
|
||||
template <typename T>
|
||||
static void setRawString(VariantData* var, SerializedValue<T> value,
|
||||
ResourceManager* resources) {
|
||||
if (!var)
|
||||
return;
|
||||
var->clear(resources);
|
||||
var->setRawString(value, resources);
|
||||
}
|
||||
|
||||
template <typename TAdaptedString>
|
||||
bool setString(TAdaptedString value, ResourceManager* resources);
|
||||
|
||||
template <typename TAdaptedString>
|
||||
static void setString(VariantData* var, TAdaptedString value,
|
||||
ResourceManager* resources) {
|
||||
if (!var)
|
||||
return;
|
||||
var->clear(resources);
|
||||
var->setString(value, resources);
|
||||
}
|
||||
|
||||
void setLinkedString(const char* s) {
|
||||
ARDUINOJSON_ASSERT(type_ == VariantType::Null); // must call clear() first
|
||||
ARDUINOJSON_ASSERT(s);
|
||||
type_ = VariantType::LinkedString;
|
||||
content_.asLinkedString = s;
|
||||
type = VariantType::RawString;
|
||||
content.asStringNode = s;
|
||||
}
|
||||
|
||||
template <typename TAdaptedString>
|
||||
void setTinyString(const TAdaptedString& s) {
|
||||
ARDUINOJSON_ASSERT(type_ == VariantType::Null); // must call clear() first
|
||||
ARDUINOJSON_ASSERT(type == VariantType::Null);
|
||||
ARDUINOJSON_ASSERT(s.size() <= tinyStringMaxLength);
|
||||
|
||||
type_ = VariantType::TinyString;
|
||||
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[i] = c;
|
||||
}
|
||||
|
||||
content_.asTinyString[n] = 0;
|
||||
content.asTinyString[n] = 0;
|
||||
}
|
||||
|
||||
void setOwnedString(StringNode* s) {
|
||||
ARDUINOJSON_ASSERT(type_ == VariantType::Null); // must call clear() first
|
||||
void setLongString(StringNode* s) {
|
||||
ARDUINOJSON_ASSERT(type == VariantType::Null);
|
||||
ARDUINOJSON_ASSERT(s);
|
||||
type_ = VariantType::OwnedString;
|
||||
content_.asOwnedString = s;
|
||||
type = VariantType::LongString;
|
||||
content.asStringNode = s;
|
||||
}
|
||||
|
||||
size_t size(const ResourceManager* resources) const {
|
||||
if (isObject())
|
||||
return content_.asObject.size(resources);
|
||||
|
||||
if (isArray())
|
||||
return content_.asArray.size(resources);
|
||||
|
||||
return 0;
|
||||
CollectionData* toArray() {
|
||||
ARDUINOJSON_ASSERT(type == VariantType::Null);
|
||||
type = VariantType::Array;
|
||||
return new (&content.asCollection) CollectionData();
|
||||
}
|
||||
|
||||
static size_t size(const VariantData* var, const ResourceManager* resources) {
|
||||
return var != 0 ? var->size(resources) : 0;
|
||||
}
|
||||
|
||||
ArrayData& toArray() {
|
||||
ARDUINOJSON_ASSERT(type_ == VariantType::Null); // must call clear() first
|
||||
type_ = VariantType::Array;
|
||||
new (&content_.asArray) ArrayData();
|
||||
return content_.asArray;
|
||||
}
|
||||
|
||||
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 {
|
||||
return type_;
|
||||
}
|
||||
|
||||
// Release the resources used by this variant and set it to null.
|
||||
void clear(ResourceManager* resources);
|
||||
|
||||
static void clear(VariantData* var, ResourceManager* resources) {
|
||||
if (!var)
|
||||
return;
|
||||
var->clear(resources);
|
||||
CollectionData* toObject() {
|
||||
ARDUINOJSON_ASSERT(type == VariantType::Null);
|
||||
type = VariantType::Object;
|
||||
return new (&content.asCollection) CollectionData();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -4,10 +4,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson/Array/ArrayData.hpp>
|
||||
#include <ArduinoJson/Numbers/JsonFloat.hpp>
|
||||
#include <ArduinoJson/Numbers/JsonInteger.hpp>
|
||||
#include <ArduinoJson/Object/ObjectData.hpp>
|
||||
|
||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
|
||||
@@ -19,6 +17,14 @@ struct VariantDataVisitor {
|
||||
TResult visit(const T&) {
|
||||
return TResult();
|
||||
}
|
||||
|
||||
TResult visitArray(VariantData*) {
|
||||
return TResult();
|
||||
}
|
||||
|
||||
TResult visitObject(VariantData*) {
|
||||
return TResult();
|
||||
}
|
||||
};
|
||||
|
||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||
|
||||
@@ -4,143 +4,638 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson/Collection/CollectionIterator.hpp>
|
||||
#include <ArduinoJson/Memory/ResourceManager.hpp>
|
||||
#include <ArduinoJson/Misc/SerializedValue.hpp>
|
||||
#include <ArduinoJson/Numbers/convertNumber.hpp>
|
||||
#include <ArduinoJson/Strings/JsonString.hpp>
|
||||
#include <ArduinoJson/Strings/StringAdapters.hpp>
|
||||
#include <ArduinoJson/Variant/VariantData.hpp>
|
||||
|
||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
|
||||
template <typename T>
|
||||
inline void VariantData::setRawString(SerializedValue<T> value,
|
||||
ResourceManager* resources) {
|
||||
ARDUINOJSON_ASSERT(type_ == VariantType::Null); // must call clear() first
|
||||
auto dup = resources->saveString(adaptString(value.data(), value.size()));
|
||||
if (dup)
|
||||
setRawString(dup);
|
||||
}
|
||||
// HACK: large functions are implemented in static function to give opportunity
|
||||
// to the compiler to optimize the `this` pointer away.
|
||||
class VariantImpl {
|
||||
public:
|
||||
using iterator = CollectionIterator;
|
||||
|
||||
template <typename TAdaptedString>
|
||||
inline bool VariantData::setString(TAdaptedString value,
|
||||
ResourceManager* resources) {
|
||||
ARDUINOJSON_ASSERT(type_ == VariantType::Null); // must call clear() first
|
||||
VariantImpl() : data_(nullptr), resources_(nullptr) {}
|
||||
|
||||
if (value.isNull())
|
||||
return false;
|
||||
VariantImpl(VariantData* data, ResourceManager* resources)
|
||||
: data_(data), resources_(resources) {}
|
||||
|
||||
if (value.isStatic()) {
|
||||
setLinkedString(value.data());
|
||||
return true;
|
||||
VariantData* data() const {
|
||||
return data_;
|
||||
}
|
||||
|
||||
if (isTinyString(value, value.size())) {
|
||||
setTinyString(value);
|
||||
return true;
|
||||
ResourceManager* resources() const {
|
||||
return resources_;
|
||||
}
|
||||
|
||||
auto dup = resources->saveString(value);
|
||||
if (dup) {
|
||||
setOwnedString(dup);
|
||||
return true;
|
||||
template <typename TVisitor>
|
||||
typename TVisitor::result_type accept(TVisitor& visit) {
|
||||
return accept(visit, data_, resources_);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
template <typename TVisitor>
|
||||
static typename TVisitor::result_type accept(TVisitor& visit,
|
||||
VariantData* data,
|
||||
ResourceManager* resources) {
|
||||
if (!data)
|
||||
return visit.visit(nullptr);
|
||||
|
||||
inline void VariantData::clear(ResourceManager* resources) {
|
||||
if (type_ & VariantTypeBits::OwnedStringBit)
|
||||
resources->dereferenceString(content_.asOwnedString->data);
|
||||
|
||||
#if ARDUINOJSON_USE_EXTENSIONS
|
||||
if (type_ & VariantTypeBits::ExtensionBit)
|
||||
resources->freeExtension(content_.asSlotId);
|
||||
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||
auto eightByteValue = getEightByte(data, resources);
|
||||
#endif
|
||||
|
||||
auto collection = asCollection();
|
||||
if (collection)
|
||||
collection->clear(resources);
|
||||
|
||||
type_ = VariantType::Null;
|
||||
}
|
||||
|
||||
#if ARDUINOJSON_USE_EXTENSIONS
|
||||
inline const VariantExtension* VariantData::getExtension(
|
||||
const ResourceManager* resources) const {
|
||||
return type_ & VariantTypeBits::ExtensionBit
|
||||
? resources->getExtension(content_.asSlotId)
|
||||
: nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
template <typename T>
|
||||
enable_if_t<sizeof(T) == 8, bool> VariantData::setFloat(
|
||||
T value, ResourceManager* resources) {
|
||||
ARDUINOJSON_ASSERT(type_ == VariantType::Null); // must call clear() first
|
||||
(void)resources; // silence warning
|
||||
|
||||
float valueAsFloat = static_cast<float>(value);
|
||||
switch (data->type) {
|
||||
case VariantType::Float:
|
||||
return visit.visit(data->content.asFloat);
|
||||
|
||||
#if ARDUINOJSON_USE_DOUBLE
|
||||
if (value == valueAsFloat) {
|
||||
type_ = VariantType::Float;
|
||||
content_.asFloat = valueAsFloat;
|
||||
} else {
|
||||
auto extension = resources->allocExtension();
|
||||
if (!extension)
|
||||
return false;
|
||||
type_ = VariantType::Double;
|
||||
content_.asSlotId = extension.id();
|
||||
extension->asDouble = value;
|
||||
case VariantType::Double:
|
||||
return visit.visit(eightByteValue->asDouble);
|
||||
#endif
|
||||
|
||||
case VariantType::Array:
|
||||
return visit.visitArray(data);
|
||||
|
||||
case VariantType::Object:
|
||||
return visit.visitObject(data);
|
||||
|
||||
case VariantType::TinyString:
|
||||
return visit.visit(JsonString(data->content.asTinyString));
|
||||
|
||||
case VariantType::LongString:
|
||||
return visit.visit(JsonString(data->content.asStringNode->data,
|
||||
data->content.asStringNode->length));
|
||||
|
||||
case VariantType::RawString:
|
||||
return visit.visit(RawString(data->content.asStringNode->data,
|
||||
data->content.asStringNode->length));
|
||||
|
||||
case VariantType::Int32:
|
||||
return visit.visit(static_cast<JsonInteger>(data->content.asInt32));
|
||||
|
||||
case VariantType::Uint32:
|
||||
return visit.visit(static_cast<JsonUInt>(data->content.asUint32));
|
||||
|
||||
#if ARDUINOJSON_USE_LONG_LONG
|
||||
case VariantType::Int64:
|
||||
return visit.visit(eightByteValue->asInt64);
|
||||
|
||||
case VariantType::Uint64:
|
||||
return visit.visit(eightByteValue->asUint64);
|
||||
#endif
|
||||
|
||||
case VariantType::Boolean:
|
||||
return visit.visit(data->content.asBoolean != 0);
|
||||
|
||||
default:
|
||||
return visit.visit(nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
VariantData* addNewElement() {
|
||||
if (!isArray())
|
||||
return nullptr;
|
||||
return addNewElement(data_, resources_);
|
||||
}
|
||||
|
||||
static VariantData* addNewElement(VariantData*, ResourceManager*);
|
||||
|
||||
static void addElement(Slot<VariantData> slot, VariantData*,
|
||||
ResourceManager*);
|
||||
|
||||
template <typename TAdaptedString>
|
||||
VariantData* addMember(TAdaptedString key) {
|
||||
if (!isObject())
|
||||
return nullptr;
|
||||
return addMember(key, data_, resources_);
|
||||
}
|
||||
|
||||
template <typename TAdaptedString>
|
||||
static VariantData* addMember(TAdaptedString key, VariantData*,
|
||||
ResourceManager*);
|
||||
|
||||
VariantData* addPair(VariantData** value) {
|
||||
if (isNull())
|
||||
return nullptr;
|
||||
return addPair(value, data_, resources_);
|
||||
}
|
||||
|
||||
static VariantData* addPair(VariantData** value, VariantData*,
|
||||
ResourceManager*);
|
||||
|
||||
bool asBoolean() const {
|
||||
return asBoolean(data_, resources_);
|
||||
}
|
||||
|
||||
static bool asBoolean(VariantData* data, ResourceManager* resources) {
|
||||
if (!data)
|
||||
return false;
|
||||
|
||||
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||
auto eightByteValue = getEightByte(data, resources);
|
||||
#endif
|
||||
switch (data->type) {
|
||||
case VariantType::Boolean:
|
||||
return data->content.asBoolean;
|
||||
case VariantType::Uint32:
|
||||
case VariantType::Int32:
|
||||
return data->content.asUint32 != 0;
|
||||
case VariantType::Float:
|
||||
return data->content.asFloat != 0;
|
||||
#if ARDUINOJSON_USE_DOUBLE
|
||||
case VariantType::Double:
|
||||
return eightByteValue->asDouble != 0;
|
||||
#endif
|
||||
case VariantType::Null:
|
||||
return false;
|
||||
#if ARDUINOJSON_USE_LONG_LONG
|
||||
case VariantType::Uint64:
|
||||
case VariantType::Int64:
|
||||
return eightByteValue->asUint64 != 0;
|
||||
#endif
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T asFloat() const {
|
||||
return asFloat<T>(data_, resources_);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static T asFloat(VariantData* data, ResourceManager* resources) {
|
||||
if (!data)
|
||||
return 0.0;
|
||||
|
||||
static_assert(is_floating_point<T>::value, "T must be a floating point");
|
||||
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||
auto eightByteValue = getEightByte(data, resources);
|
||||
#endif
|
||||
const char* str = nullptr;
|
||||
switch (data->type) {
|
||||
case VariantType::Boolean:
|
||||
return static_cast<T>(data->content.asBoolean);
|
||||
case VariantType::Uint32:
|
||||
return static_cast<T>(data->content.asUint32);
|
||||
case VariantType::Int32:
|
||||
return static_cast<T>(data->content.asInt32);
|
||||
#if ARDUINOJSON_USE_LONG_LONG
|
||||
case VariantType::Uint64:
|
||||
return static_cast<T>(eightByteValue->asUint64);
|
||||
case VariantType::Int64:
|
||||
return static_cast<T>(eightByteValue->asInt64);
|
||||
#endif
|
||||
case VariantType::TinyString:
|
||||
str = data->content.asTinyString;
|
||||
break;
|
||||
case VariantType::LongString:
|
||||
str = data->content.asStringNode->data;
|
||||
break;
|
||||
case VariantType::Float:
|
||||
return static_cast<T>(data->content.asFloat);
|
||||
#if ARDUINOJSON_USE_DOUBLE
|
||||
case VariantType::Double:
|
||||
return static_cast<T>(eightByteValue->asDouble);
|
||||
#endif
|
||||
default:
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
ARDUINOJSON_ASSERT(str != nullptr);
|
||||
return parseNumber<T>(str);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T asIntegral() const {
|
||||
return asIntegral<T>(data_, resources_);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static T asIntegral(VariantData* data, ResourceManager* resources) {
|
||||
if (!data)
|
||||
return 0;
|
||||
|
||||
static_assert(is_integral<T>::value, "T must be an integral type");
|
||||
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||
auto eightByteValue = getEightByte(data, resources);
|
||||
#endif
|
||||
const char* str = nullptr;
|
||||
switch (data->type) {
|
||||
case VariantType::Boolean:
|
||||
return data->content.asBoolean;
|
||||
case VariantType::Uint32:
|
||||
return convertNumber<T>(data->content.asUint32);
|
||||
case VariantType::Int32:
|
||||
return convertNumber<T>(data->content.asInt32);
|
||||
#if ARDUINOJSON_USE_LONG_LONG
|
||||
case VariantType::Uint64:
|
||||
return convertNumber<T>(eightByteValue->asUint64);
|
||||
case VariantType::Int64:
|
||||
return convertNumber<T>(eightByteValue->asInt64);
|
||||
#endif
|
||||
case VariantType::TinyString:
|
||||
str = data->content.asTinyString;
|
||||
break;
|
||||
case VariantType::LongString:
|
||||
str = data->content.asStringNode->data;
|
||||
break;
|
||||
case VariantType::Float:
|
||||
return convertNumber<T>(data->content.asFloat);
|
||||
#if ARDUINOJSON_USE_DOUBLE
|
||||
case VariantType::Double:
|
||||
return convertNumber<T>(eightByteValue->asDouble);
|
||||
#endif
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
|
||||
ARDUINOJSON_ASSERT(str != nullptr);
|
||||
return parseNumber<T>(str);
|
||||
}
|
||||
|
||||
iterator at(size_t index) const;
|
||||
|
||||
iterator createIterator() const {
|
||||
if (!isCollection())
|
||||
return iterator();
|
||||
return createIterator(data_, resources_);
|
||||
}
|
||||
|
||||
static iterator createIterator(VariantData*, ResourceManager*);
|
||||
|
||||
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||
static const EightByteValue* getEightByte(VariantData* data,
|
||||
ResourceManager* resources) {
|
||||
ARDUINOJSON_ASSERT(data != nullptr);
|
||||
ARDUINOJSON_ASSERT(resources != nullptr);
|
||||
return data->type & VariantTypeBits::EightByteBit
|
||||
? resources->getEightByte(data->content.asSlotId)
|
||||
: 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
VariantData* getOrAddElement(size_t index);
|
||||
|
||||
VariantData* getElement(size_t index) const;
|
||||
|
||||
template <typename TAdaptedString>
|
||||
VariantData* getMember(TAdaptedString key) const {
|
||||
if (!isObject())
|
||||
return nullptr;
|
||||
return getMember(key, data_, resources_);
|
||||
}
|
||||
|
||||
template <typename TAdaptedString>
|
||||
static VariantData* getMember(TAdaptedString key, VariantData*,
|
||||
ResourceManager*);
|
||||
|
||||
template <typename TAdaptedString>
|
||||
VariantData* getOrAddMember(TAdaptedString key) {
|
||||
if (!isObject())
|
||||
return nullptr;
|
||||
return getOrAddMember(key, data_, resources_);
|
||||
}
|
||||
|
||||
template <typename TAdaptedString>
|
||||
static VariantData* getOrAddMember(TAdaptedString key, VariantData*,
|
||||
ResourceManager*);
|
||||
|
||||
bool isArray() const {
|
||||
return type() == VariantType::Array;
|
||||
}
|
||||
|
||||
bool isCollection() const {
|
||||
return type() & VariantTypeBits::CollectionMask;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool isInteger() const {
|
||||
return isInteger<T>(data_, resources_);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static bool isInteger(VariantData* data, ResourceManager* resources) {
|
||||
if (!data)
|
||||
return false;
|
||||
|
||||
#if ARDUINOJSON_USE_LONG_LONG
|
||||
auto eightByteValue = getEightByte(data, resources);
|
||||
#else
|
||||
type_ = VariantType::Float;
|
||||
content_.asFloat = valueAsFloat;
|
||||
(void)resources;
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
switch (data->type) {
|
||||
case VariantType::Uint32:
|
||||
return canConvertNumber<T>(data->content.asUint32);
|
||||
|
||||
template <typename T>
|
||||
enable_if_t<is_signed<T>::value, bool> VariantData::setInteger(
|
||||
T value, ResourceManager* resources) {
|
||||
ARDUINOJSON_ASSERT(type_ == VariantType::Null); // must call clear() first
|
||||
(void)resources; // silence warning
|
||||
case VariantType::Int32:
|
||||
return canConvertNumber<T>(data->content.asInt32);
|
||||
|
||||
if (canConvertNumber<int32_t>(value)) {
|
||||
type_ = VariantType::Int32;
|
||||
content_.asInt32 = static_cast<int32_t>(value);
|
||||
}
|
||||
#if ARDUINOJSON_USE_LONG_LONG
|
||||
else {
|
||||
auto extension = resources->allocExtension();
|
||||
if (!extension)
|
||||
return false;
|
||||
type_ = VariantType::Int64;
|
||||
content_.asSlotId = extension.id();
|
||||
extension->asInt64 = value;
|
||||
}
|
||||
case VariantType::Uint64:
|
||||
return canConvertNumber<T>(eightByteValue->asUint64);
|
||||
|
||||
case VariantType::Int64:
|
||||
return canConvertNumber<T>(eightByteValue->asInt64);
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
enable_if_t<is_unsigned<T>::value, bool> VariantData::setInteger(
|
||||
T value, ResourceManager* resources) {
|
||||
ARDUINOJSON_ASSERT(type_ == VariantType::Null); // must call clear() first
|
||||
(void)resources; // silence warning
|
||||
|
||||
if (canConvertNumber<uint32_t>(value)) {
|
||||
type_ = VariantType::Uint32;
|
||||
content_.asUint32 = static_cast<uint32_t>(value);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool isNull() const {
|
||||
return type() == VariantType::Null;
|
||||
}
|
||||
|
||||
bool isObject() const {
|
||||
return type() == VariantType::Object;
|
||||
}
|
||||
|
||||
size_t nesting() const;
|
||||
|
||||
void removeElement(size_t index);
|
||||
|
||||
void removeElement(CollectionIterator it) {
|
||||
removeOne(it);
|
||||
}
|
||||
|
||||
template <typename TAdaptedString>
|
||||
void removeMember(TAdaptedString key) {
|
||||
removePair(findKey(key));
|
||||
}
|
||||
|
||||
void removeMember(CollectionIterator it) {
|
||||
removePair(it);
|
||||
}
|
||||
|
||||
bool setBoolean(bool value) {
|
||||
if (!data_)
|
||||
return false;
|
||||
clear(data_, resources_);
|
||||
data_->setBoolean(value);
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool setFloat(T value) {
|
||||
if (!data_)
|
||||
return false;
|
||||
clear(data_, resources_);
|
||||
return setFloat(value, data_, resources_);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static enable_if_t<sizeof(T) == 4, bool> setFloat(T value, VariantData* data,
|
||||
ResourceManager*) {
|
||||
ARDUINOJSON_ASSERT(data != nullptr);
|
||||
ARDUINOJSON_ASSERT(data->type == VariantType::Null);
|
||||
data->type = VariantType::Float;
|
||||
data->content.asFloat = value;
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static enable_if_t<sizeof(T) == 8, bool> setFloat(
|
||||
T value, VariantData* data, ResourceManager* resources) {
|
||||
ARDUINOJSON_ASSERT(data != nullptr);
|
||||
ARDUINOJSON_ASSERT(data->type == VariantType::Null);
|
||||
ARDUINOJSON_ASSERT(resources != nullptr);
|
||||
|
||||
float valueAsFloat = static_cast<float>(value);
|
||||
|
||||
#if ARDUINOJSON_USE_DOUBLE
|
||||
if (value == valueAsFloat) {
|
||||
data->type = VariantType::Float;
|
||||
data->content.asFloat = valueAsFloat;
|
||||
} else {
|
||||
auto slot = resources->allocEightByte();
|
||||
if (!slot)
|
||||
return false;
|
||||
data->type = VariantType::Double;
|
||||
data->content.asSlotId = slot.id();
|
||||
slot->asDouble = value;
|
||||
}
|
||||
#else
|
||||
data->type = VariantType::Float;
|
||||
data->content.asFloat = valueAsFloat;
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool setInteger(T value) {
|
||||
if (!data_)
|
||||
return false;
|
||||
clear(data_, resources_);
|
||||
return setInteger(value, data_, resources_);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static enable_if_t<is_signed<T>::value, bool> setInteger(
|
||||
T value, VariantData* data, ResourceManager* resources) {
|
||||
ARDUINOJSON_ASSERT(data != nullptr);
|
||||
ARDUINOJSON_ASSERT(data->type == VariantType::Null);
|
||||
ARDUINOJSON_ASSERT(resources != nullptr);
|
||||
|
||||
if (canConvertNumber<int32_t>(value)) {
|
||||
data->type = VariantType::Int32;
|
||||
data->content.asInt32 = static_cast<int32_t>(value);
|
||||
}
|
||||
#if ARDUINOJSON_USE_LONG_LONG
|
||||
else {
|
||||
auto extension = resources->allocExtension();
|
||||
if (!extension)
|
||||
return false;
|
||||
type_ = VariantType::Uint64;
|
||||
content_.asSlotId = extension.id();
|
||||
extension->asUint64 = value;
|
||||
}
|
||||
else {
|
||||
auto slot = resources->allocEightByte();
|
||||
if (!slot)
|
||||
return false;
|
||||
data->type = VariantType::Int64;
|
||||
data->content.asSlotId = slot.id();
|
||||
slot->asInt64 = value;
|
||||
}
|
||||
#else
|
||||
(void)resources;
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static enable_if_t<is_unsigned<T>::value, bool> setInteger(
|
||||
T value, VariantData* data, ResourceManager* resources) {
|
||||
ARDUINOJSON_ASSERT(data != nullptr);
|
||||
ARDUINOJSON_ASSERT(data->type == VariantType::Null);
|
||||
ARDUINOJSON_ASSERT(resources != nullptr);
|
||||
|
||||
if (canConvertNumber<uint32_t>(value)) {
|
||||
data->type = VariantType::Uint32;
|
||||
data->content.asUint32 = static_cast<uint32_t>(value);
|
||||
}
|
||||
#if ARDUINOJSON_USE_LONG_LONG
|
||||
else {
|
||||
auto slot = resources->allocEightByte();
|
||||
if (!slot)
|
||||
return false;
|
||||
data->type = VariantType::Uint64;
|
||||
data->content.asSlotId = slot.id();
|
||||
slot->asUint64 = value;
|
||||
}
|
||||
#else
|
||||
(void)resources;
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename TAdaptedString>
|
||||
bool setRawString(TAdaptedString value) {
|
||||
if (!data_)
|
||||
return false;
|
||||
clear(data_, resources_);
|
||||
auto dup = resources_->saveString(adaptString(value.data(), value.size()));
|
||||
if (!dup)
|
||||
return false;
|
||||
data_->setRawString(dup);
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename TAdaptedString>
|
||||
bool setString(TAdaptedString value) {
|
||||
if (!data_)
|
||||
return false;
|
||||
clear(data_, resources_);
|
||||
return setString(value, data_, resources_);
|
||||
}
|
||||
|
||||
template <typename TAdaptedString>
|
||||
static bool setString(TAdaptedString value, VariantData* data,
|
||||
ResourceManager* resources) {
|
||||
ARDUINOJSON_ASSERT(data != nullptr);
|
||||
ARDUINOJSON_ASSERT(data->type == VariantType::Null);
|
||||
ARDUINOJSON_ASSERT(resources != nullptr);
|
||||
|
||||
if (value.isNull())
|
||||
return true; // TODO: should this be moved up to the member function?
|
||||
|
||||
if (isTinyString(value, value.size())) {
|
||||
data->setTinyString(value);
|
||||
return true;
|
||||
}
|
||||
|
||||
auto dup = resources->saveString(value);
|
||||
if (dup) {
|
||||
data->setLongString(dup);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t size() const {
|
||||
if (!isCollection())
|
||||
return 0;
|
||||
|
||||
return size(data_, resources_);
|
||||
}
|
||||
|
||||
static size_t size(VariantData* data, ResourceManager* resources) {
|
||||
ARDUINOJSON_ASSERT(data != nullptr);
|
||||
ARDUINOJSON_ASSERT(data->isCollection());
|
||||
ARDUINOJSON_ASSERT(resources != nullptr);
|
||||
|
||||
size_t n = 0;
|
||||
for (auto it = createIterator(data, resources); !it.done();
|
||||
it.move(resources))
|
||||
n++;
|
||||
|
||||
if (data->type == VariantType::Object) {
|
||||
ARDUINOJSON_ASSERT((n % 2) == 0);
|
||||
n /= 2;
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
bool toArray() {
|
||||
if (!data_)
|
||||
return false;
|
||||
clear(data_, resources_);
|
||||
data_->toArray();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool toObject() {
|
||||
if (!data_)
|
||||
return false;
|
||||
clear(data_, resources_);
|
||||
data_->toObject();
|
||||
return true;
|
||||
}
|
||||
|
||||
VariantType type() const {
|
||||
return data_ ? data_->type : VariantType::Null;
|
||||
}
|
||||
|
||||
// Release the resources used by this variant and set it to null.
|
||||
bool clear() {
|
||||
if (!data_)
|
||||
return false;
|
||||
clear(data_, resources_);
|
||||
return true;
|
||||
}
|
||||
|
||||
static void clear(VariantData* data, ResourceManager* resources) {
|
||||
ARDUINOJSON_ASSERT(data != nullptr);
|
||||
ARDUINOJSON_ASSERT(resources != nullptr);
|
||||
|
||||
if (data->type & VariantTypeBits::OwnedStringBit)
|
||||
resources->dereferenceString(data->content.asStringNode->data);
|
||||
|
||||
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||
if (data->type & VariantTypeBits::EightByteBit)
|
||||
resources->freeEightByte(data->content.asSlotId);
|
||||
#endif
|
||||
|
||||
if (data->type & VariantTypeBits::CollectionMask)
|
||||
empty(data, resources);
|
||||
|
||||
data->type = VariantType::Null;
|
||||
}
|
||||
|
||||
void empty() {
|
||||
if (!isCollection())
|
||||
return;
|
||||
empty(data_, resources_);
|
||||
}
|
||||
|
||||
static void empty(VariantData*, ResourceManager*);
|
||||
|
||||
static void freeVariant(Slot<VariantData> slot, ResourceManager* resources) {
|
||||
clear(slot.ptr(), resources);
|
||||
resources->freeVariant(slot);
|
||||
}
|
||||
|
||||
private:
|
||||
VariantData* data_;
|
||||
ResourceManager* resources_;
|
||||
|
||||
template <typename TAdaptedString>
|
||||
iterator findKey(TAdaptedString key) const {
|
||||
if (!isObject())
|
||||
return iterator();
|
||||
return findKey(key, data_, resources_);
|
||||
}
|
||||
|
||||
template <typename TAdaptedString>
|
||||
static iterator findKey(TAdaptedString key, VariantData*, ResourceManager*);
|
||||
|
||||
static void appendPair(Slot<VariantData> key, Slot<VariantData> value,
|
||||
VariantData*, ResourceManager*);
|
||||
|
||||
void removeOne(iterator it);
|
||||
void removePair(iterator it);
|
||||
|
||||
Slot<VariantData> getPreviousSlot(VariantData*) const;
|
||||
};
|
||||
|
||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
#include <ArduinoJson/Variant/Converter.hpp>
|
||||
#include <ArduinoJson/Variant/JsonVariantConst.hpp>
|
||||
#include <ArduinoJson/Variant/VariantOperators.hpp>
|
||||
#include <ArduinoJson/Variant/VariantTo.hpp>
|
||||
|
||||
ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE
|
||||
class JsonVariant;
|
||||
@@ -29,13 +28,13 @@ class VariantRefBase : public VariantTag {
|
||||
// Sets the value to null.
|
||||
// https://arduinojson.org/v7/api/jsonvariant/clear/
|
||||
void clear() const {
|
||||
VariantData::clear(getOrCreateData(), getResourceManager());
|
||||
getOrCreateVariantImpl().clear();
|
||||
}
|
||||
|
||||
// Returns true if the value is null or the reference is unbound.
|
||||
// https://arduinojson.org/v7/api/jsonvariant/isnull/
|
||||
bool isNull() const {
|
||||
return VariantData::isNull(getData());
|
||||
return getVariantImpl().isNull();
|
||||
}
|
||||
|
||||
// Returns true if the reference is unbound.
|
||||
@@ -77,15 +76,13 @@ class VariantRefBase : public VariantTag {
|
||||
// https://arduinojson.org/v7/api/jsonvariant/set/
|
||||
template <typename T>
|
||||
bool set(const T& value) const {
|
||||
using TypeForConverter = conditional_t<IsStringLiteral<T>::value, T,
|
||||
remove_cv_t<remove_reference_t<T>>>;
|
||||
using TypeForConverter = remove_cv_t<remove_reference_t<T>>;
|
||||
return doSet<Converter<TypeForConverter>>(value);
|
||||
}
|
||||
|
||||
// Copies the specified value.
|
||||
// https://arduinojson.org/v7/api/jsonvariant/set/
|
||||
template <typename T,
|
||||
detail::enable_if_t<!detail::is_const<T>::value, int> = 0>
|
||||
template <typename T>
|
||||
bool set(T* value) const {
|
||||
return doSet<Converter<T*>>(value);
|
||||
}
|
||||
@@ -93,13 +90,13 @@ class VariantRefBase : public VariantTag {
|
||||
// Returns the size of the array or object.
|
||||
// https://arduinojson.org/v7/api/jsonvariant/size/
|
||||
size_t size() const {
|
||||
return VariantData::size(getData(), getResourceManager());
|
||||
return getVariantImpl().size();
|
||||
}
|
||||
|
||||
// Returns the depth (nesting level) of the value.
|
||||
// https://arduinojson.org/v7/api/jsonvariant/nesting/
|
||||
size_t nesting() const {
|
||||
return VariantData::nesting(getData(), getResourceManager());
|
||||
return getVariantImpl().nesting();
|
||||
}
|
||||
|
||||
// Appends a new (empty) element to the array.
|
||||
@@ -120,38 +117,34 @@ class VariantRefBase : public VariantTag {
|
||||
// https://arduinojson.org/v7/api/jsonvariant/add/
|
||||
template <typename T>
|
||||
bool add(const T& value) const {
|
||||
return detail::VariantData::addValue(getOrCreateData(), value,
|
||||
getResourceManager());
|
||||
return getOrCreateArray().add(value);
|
||||
}
|
||||
|
||||
// Appends a value to the array.
|
||||
// https://arduinojson.org/v7/api/jsonvariant/add/
|
||||
template <typename T, enable_if_t<!is_const<T>::value, int> = 0>
|
||||
template <typename T>
|
||||
bool add(T* value) const {
|
||||
return detail::VariantData::addValue(getOrCreateData(), value,
|
||||
getResourceManager());
|
||||
return getOrCreateArray().add(value);
|
||||
}
|
||||
|
||||
// Removes an element of the array.
|
||||
// https://arduinojson.org/v7/api/jsonvariant/remove/
|
||||
void remove(size_t index) const {
|
||||
VariantData::removeElement(getData(), index, getResourceManager());
|
||||
getVariantImpl().removeElement(index);
|
||||
}
|
||||
|
||||
// Removes a member of the object.
|
||||
// https://arduinojson.org/v7/api/jsonvariant/remove/
|
||||
template <typename TChar, enable_if_t<IsString<TChar*>::value, int> = 0>
|
||||
void remove(TChar* key) const {
|
||||
VariantData::removeMember(getData(), adaptString(key),
|
||||
getResourceManager());
|
||||
getVariantImpl().removeMember(adaptString(key));
|
||||
}
|
||||
|
||||
// Removes a member of the object.
|
||||
// https://arduinojson.org/v7/api/jsonvariant/remove/
|
||||
template <typename TString, enable_if_t<IsString<TString>::value, int> = 0>
|
||||
void remove(const TString& key) const {
|
||||
VariantData::removeMember(getData(), adaptString(key),
|
||||
getResourceManager());
|
||||
getVariantImpl().removeMember(adaptString(key));
|
||||
}
|
||||
|
||||
// Removes a member of the object or an element of the array.
|
||||
@@ -194,9 +187,7 @@ class VariantRefBase : public VariantTag {
|
||||
|
||||
// Gets or sets an object member.
|
||||
// https://arduinojson.org/v7/api/jsonvariant/subscript/
|
||||
template <
|
||||
typename TChar,
|
||||
enable_if_t<IsString<TChar*>::value && !is_const<TChar>::value, int> = 0>
|
||||
template <typename TChar, enable_if_t<IsString<TChar*>::value, int> = 0>
|
||||
FORCE_INLINE MemberProxy<TDerived, AdaptedString<TChar*>> operator[](
|
||||
TChar* key) const;
|
||||
|
||||
@@ -275,6 +266,16 @@ class VariantRefBase : public VariantTag {
|
||||
return VariantAttorney::getOrCreateData(derived());
|
||||
}
|
||||
|
||||
VariantImpl getVariantImpl() const {
|
||||
return VariantImpl(getData(), getResourceManager());
|
||||
}
|
||||
|
||||
VariantImpl getOrCreateVariantImpl() const {
|
||||
return VariantImpl(getOrCreateData(), getResourceManager());
|
||||
}
|
||||
|
||||
JsonArray getOrCreateArray() const;
|
||||
|
||||
FORCE_INLINE ArduinoJson::JsonVariant getVariant() const;
|
||||
|
||||
FORCE_INLINE ArduinoJson::JsonVariantConst getVariantConst() const {
|
||||
|
||||
@@ -61,31 +61,27 @@ inline JsonObject VariantRefBase<TDerived>::createNestedObject(
|
||||
}
|
||||
|
||||
template <typename TDerived>
|
||||
inline void convertToJson(const VariantRefBase<TDerived>& src,
|
||||
inline bool convertToJson(const VariantRefBase<TDerived>& src,
|
||||
JsonVariant dst) {
|
||||
dst.set(src.template as<JsonVariantConst>());
|
||||
return dst.set(src.template as<JsonVariantConst>());
|
||||
}
|
||||
|
||||
template <typename TDerived>
|
||||
template <typename T, enable_if_t<is_same<T, JsonVariant>::value, int>>
|
||||
inline T VariantRefBase<TDerived>::add() const {
|
||||
return JsonVariant(
|
||||
detail::VariantData::addElement(getOrCreateData(), getResourceManager()),
|
||||
getResourceManager());
|
||||
return getOrCreateArray().template add<T>();
|
||||
}
|
||||
|
||||
template <typename TDerived>
|
||||
template <typename TString, enable_if_t<IsString<TString>::value, int>>
|
||||
inline bool VariantRefBase<TDerived>::containsKey(const TString& key) const {
|
||||
return VariantData::getMember(getData(), adaptString(key),
|
||||
getResourceManager()) != 0;
|
||||
return getVariantImpl().getMember(adaptString(key)) != 0;
|
||||
}
|
||||
|
||||
template <typename TDerived>
|
||||
template <typename TChar, enable_if_t<IsString<TChar*>::value, int>>
|
||||
inline bool VariantRefBase<TDerived>::containsKey(TChar* key) const {
|
||||
return VariantData::getMember(getData(), adaptString(key),
|
||||
getResourceManager()) != 0;
|
||||
return getVariantImpl().getMember(adaptString(key)) != 0;
|
||||
}
|
||||
|
||||
template <typename TDerived>
|
||||
@@ -99,6 +95,15 @@ inline JsonVariant VariantRefBase<TDerived>::getVariant() const {
|
||||
return JsonVariant(getData(), getResourceManager());
|
||||
}
|
||||
|
||||
template <typename TDerived>
|
||||
inline JsonArray VariantRefBase<TDerived>::getOrCreateArray() const {
|
||||
auto data = getOrCreateData();
|
||||
auto resources = getResourceManager();
|
||||
if (data && data->type == VariantType::Null)
|
||||
data->toArray();
|
||||
return JsonArray(data, resources);
|
||||
}
|
||||
|
||||
template <typename TDerived>
|
||||
inline JsonVariant VariantRefBase<TDerived>::getOrCreateVariant() const {
|
||||
return JsonVariant(getOrCreateData(), getResourceManager());
|
||||
@@ -119,8 +124,7 @@ inline ElementProxy<TDerived> VariantRefBase<TDerived>::operator[](
|
||||
}
|
||||
|
||||
template <typename TDerived>
|
||||
template <typename TChar,
|
||||
enable_if_t<IsString<TChar*>::value && !is_const<TChar>::value, int>>
|
||||
template <typename TChar, enable_if_t<IsString<TChar*>::value, int>>
|
||||
inline MemberProxy<TDerived, AdaptedString<TChar*>>
|
||||
VariantRefBase<TDerived>::operator[](TChar* key) const {
|
||||
return {derived(), adaptString(key)};
|
||||
@@ -150,26 +154,25 @@ inline bool VariantRefBase<TDerived>::doSet(const T& value, true_type) const {
|
||||
template <typename TDerived>
|
||||
template <typename T, enable_if_t<is_same<T, JsonArray>::value, int>>
|
||||
inline JsonArray VariantRefBase<TDerived>::to() const {
|
||||
return JsonArray(
|
||||
VariantData::toArray(getOrCreateData(), getResourceManager()),
|
||||
getResourceManager());
|
||||
auto impl = getOrCreateVariantImpl();
|
||||
impl.toArray();
|
||||
return JsonArray(impl);
|
||||
}
|
||||
|
||||
template <typename TDerived>
|
||||
template <typename T, enable_if_t<is_same<T, JsonObject>::value, int>>
|
||||
JsonObject VariantRefBase<TDerived>::to() const {
|
||||
return JsonObject(
|
||||
VariantData::toObject(getOrCreateData(), getResourceManager()),
|
||||
getResourceManager());
|
||||
auto impl = getOrCreateVariantImpl();
|
||||
impl.toObject();
|
||||
return JsonObject(impl);
|
||||
}
|
||||
|
||||
template <typename TDerived>
|
||||
template <typename T, enable_if_t<is_same<T, JsonVariant>::value, int>>
|
||||
JsonVariant VariantRefBase<TDerived>::to() const {
|
||||
auto data = getOrCreateData();
|
||||
auto resources = getResourceManager();
|
||||
detail::VariantData::clear(data, resources);
|
||||
return JsonVariant(data, resources);
|
||||
auto impl = getOrCreateVariantImpl();
|
||||
impl.clear();
|
||||
return JsonVariant(impl);
|
||||
}
|
||||
|
||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2025, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson/Namespace.hpp>
|
||||
|
||||
ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE
|
||||
class JsonArray;
|
||||
class JsonObject;
|
||||
class JsonVariant;
|
||||
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
||||
|
||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
// A metafunction that returns the type of the value returned by
|
||||
// JsonVariant::to<T>()
|
||||
template <typename T>
|
||||
struct VariantTo {};
|
||||
|
||||
template <>
|
||||
struct VariantTo<JsonArray> {
|
||||
using type = JsonArray;
|
||||
};
|
||||
template <>
|
||||
struct VariantTo<JsonObject> {
|
||||
using type = JsonObject;
|
||||
};
|
||||
template <>
|
||||
struct VariantTo<JsonVariant> {
|
||||
using type = JsonVariant;
|
||||
};
|
||||
|
||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||
Reference in New Issue
Block a user