forked from bblanchon/ArduinoJson
Compare commits
24 Commits
Author | SHA1 | Date | |
---|---|---|---|
5bc6a3077a | |||
31a729d35c | |||
48dec78781 | |||
8cc190b7bf | |||
b1a58f3c0c | |||
fe6de3a82b | |||
e27439f3cd | |||
7e093ffc25 | |||
8e3286aac8 | |||
c07744dc0d | |||
733bc4ee82 | |||
9aa77994b4 | |||
f051f8328d | |||
411424b74e | |||
5e5c287978 | |||
377cf63075 | |||
3252013509 | |||
deab127a2f | |||
96281de682 | |||
f0e84e4933 | |||
91397f9f06 | |||
7f75985e47 | |||
05b68fc7cc | |||
b06cee8f4d |
99
.github/workflows/ci.yml
vendored
99
.github/workflows/ci.yml
vendored
@ -93,44 +93,40 @@ jobs:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
include:
|
||||
- clang: "3.9"
|
||||
runner: ubuntu-20.04
|
||||
archive: bionic
|
||||
- clang: "4.0"
|
||||
runner: ubuntu-20.04
|
||||
archive: bionic
|
||||
- clang: "5.0"
|
||||
runner: ubuntu-20.04
|
||||
archive: bionic
|
||||
- clang: "6.0"
|
||||
runner: ubuntu-20.04
|
||||
archive: bionic
|
||||
- clang: "7"
|
||||
runner: ubuntu-20.04
|
||||
runner: ubuntu-22.04
|
||||
archive: focal
|
||||
- clang: "8"
|
||||
cxxflags: -fsanitize=leak -fno-sanitize-recover=all
|
||||
runner: ubuntu-20.04
|
||||
runner: ubuntu-22.04
|
||||
archive: focal
|
||||
- clang: "9"
|
||||
cxxflags: -fsanitize=undefined -fno-sanitize-recover=all
|
||||
runner: ubuntu-20.04
|
||||
runner: ubuntu-22.04
|
||||
archive: focal
|
||||
- clang: "10"
|
||||
cxxflags: -fsanitize=address -fno-sanitize-recover=all
|
||||
runner: ubuntu-20.04
|
||||
runner: ubuntu-22.04
|
||||
archive: focal
|
||||
- clang: "11"
|
||||
runner: ubuntu-22.04
|
||||
- clang: "12"
|
||||
runner: ubuntu-22.04
|
||||
- clang: "13"
|
||||
runner: ubuntu-22.04
|
||||
- clang: "14"
|
||||
runner: ubuntu-22.04
|
||||
- clang: "15"
|
||||
runner: ubuntu-22.04
|
||||
runs-on: ${{ matrix.runner }}
|
||||
- clang: 14
|
||||
- clang: 15
|
||||
- clang: 16
|
||||
- clang: 17
|
||||
- clang: 18
|
||||
- clang: 19
|
||||
runs-on: ${{ matrix.runner || 'ubuntu-latest' }}
|
||||
steps:
|
||||
- name: Add archive repositories
|
||||
if: matrix.archive
|
||||
run: |
|
||||
sudo gpg --keyserver keyserver.ubuntu.com --recv-keys 3B4FE6ACC0B21F32
|
||||
sudo gpg --export 3B4FE6ACC0B21F32 | sudo tee /etc/apt/trusted.gpg.d/ubuntu-keyring.gpg > /dev/null
|
||||
sudo add-apt-repository -yn 'deb http://archive.ubuntu.com/ubuntu/ ${{ matrix.archive }} main'
|
||||
sudo add-apt-repository -yn 'deb http://archive.ubuntu.com/ubuntu/ ${{ matrix.archive }} universe'
|
||||
- name: Install Clang ${{ matrix.clang }}
|
||||
@ -138,10 +134,9 @@ jobs:
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y clang-${{ matrix.clang }}
|
||||
- name: Install libc++ ${{ matrix.clang }}
|
||||
if: matrix.clang >= 11
|
||||
run: sudo apt-get install -y libc++-${{ matrix.clang }}-dev libc++abi-${{ matrix.clang }}-dev
|
||||
- name: Install libunwind ${{ matrix.clang }}
|
||||
if: matrix.clang == 12 # dependency is missing in Ubuntu 22.04
|
||||
if: matrix.clang == 12 # dependency is missing in Ubuntu 22.04
|
||||
run: sudo apt-get install -y libunwind-${{ matrix.clang }}-dev
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
@ -163,7 +158,7 @@ jobs:
|
||||
conf_test:
|
||||
name: Test configuration on Linux
|
||||
needs: [gcc, clang]
|
||||
runs-on: ubuntu-20.04
|
||||
runs-on: ubuntu-22.04
|
||||
steps:
|
||||
- name: Install
|
||||
run: |
|
||||
@ -184,19 +179,19 @@ jobs:
|
||||
|
||||
conf_test_windows:
|
||||
name: Test configuration on Windows
|
||||
runs-on: windows-2019
|
||||
runs-on: windows-2022
|
||||
needs: [gcc, clang]
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
- name: 32-bit
|
||||
run: |
|
||||
call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvars32.bat"
|
||||
call "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvars32.bat"
|
||||
cl /Isrc extras/conf_test/x86.cpp
|
||||
shell: cmd
|
||||
- name: 64-bit
|
||||
run: |
|
||||
call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvars64.bat"
|
||||
call "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvars64.bat"
|
||||
cl /Isrc extras/conf_test/x64.cpp
|
||||
shell: cmd
|
||||
|
||||
@ -257,7 +252,7 @@ jobs:
|
||||
board: arduino:avr:uno
|
||||
- core: arduino:samd
|
||||
board: arduino:samd:mkr1000
|
||||
runs-on: ubuntu-20.04
|
||||
runs-on: ubuntu-22.04
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
@ -412,7 +407,7 @@ jobs:
|
||||
arm:
|
||||
name: GCC for ARM processor
|
||||
needs: gcc
|
||||
runs-on: ubuntu-20.04
|
||||
runs-on: ubuntu-22.04
|
||||
steps:
|
||||
- name: Install
|
||||
run: |
|
||||
@ -431,7 +426,7 @@ jobs:
|
||||
coverage:
|
||||
needs: gcc
|
||||
name: Coverage
|
||||
runs-on: ubuntu-20.04
|
||||
runs-on: ubuntu-22.04
|
||||
steps:
|
||||
- name: Install
|
||||
run: sudo apt-get install -y lcov ninja-build
|
||||
@ -463,7 +458,7 @@ jobs:
|
||||
valgrind:
|
||||
needs: gcc
|
||||
name: Valgrind
|
||||
runs-on: ubuntu-20.04
|
||||
runs-on: ubuntu-22.04
|
||||
steps:
|
||||
- name: Install
|
||||
run: |
|
||||
@ -485,24 +480,24 @@ jobs:
|
||||
clang-tidy:
|
||||
needs: clang
|
||||
name: Clang-Tidy
|
||||
runs-on: ubuntu-20.04
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Install
|
||||
run: sudo apt-get install -y clang-tidy cmake ninja-build
|
||||
run: sudo apt-get install -y clang-tidy libc++-dev libc++abi-dev
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
- name: Configure
|
||||
run: cmake -G Ninja -DCMAKE_CXX_CLANG_TIDY="clang-tidy-10;--warnings-as-errors=*" -DCMAKE_BUILD_TYPE=Debug .
|
||||
run: cmake -G Ninja -DCMAKE_CXX_CLANG_TIDY="clang-tidy;--warnings-as-errors=*" -DCMAKE_BUILD_TYPE=Debug .
|
||||
env:
|
||||
CC: clang-10
|
||||
CXX: clang++-10
|
||||
CC: clang
|
||||
CXX: clang++
|
||||
- name: Check
|
||||
run: cmake --build . -- -k 0
|
||||
|
||||
amalgamate:
|
||||
needs: gcc
|
||||
name: Amalgamate ArduinoJson.h
|
||||
runs-on: ubuntu-20.04
|
||||
runs-on: ubuntu-22.04
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
@ -578,7 +573,7 @@ jobs:
|
||||
|
||||
codeql:
|
||||
name: CodeQL
|
||||
runs-on: ubuntu-20.04
|
||||
runs-on: ubuntu-22.04
|
||||
needs: gcc
|
||||
|
||||
permissions:
|
||||
@ -587,20 +582,20 @@ jobs:
|
||||
security-events: write
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Initialize CodeQL
|
||||
uses: github/codeql-action/init@v3
|
||||
with:
|
||||
languages: cpp
|
||||
- name: Initialize CodeQL
|
||||
uses: github/codeql-action/init@v3
|
||||
with:
|
||||
languages: cpp
|
||||
|
||||
- name: Build
|
||||
run: |
|
||||
cmake -DCMAKE_BUILD_TYPE=Debug .
|
||||
cmake --build .
|
||||
- name: Build
|
||||
run: |
|
||||
cmake -DCMAKE_BUILD_TYPE=Debug .
|
||||
cmake --build .
|
||||
|
||||
- name: Perform CodeQL Analysis
|
||||
uses: github/codeql-action/analyze@v3
|
||||
with:
|
||||
category: "/language:cpp"
|
||||
- name: Perform CodeQL Analysis
|
||||
uses: github/codeql-action/analyze@v3
|
||||
with:
|
||||
category: "/language:cpp"
|
||||
|
2
.github/workflows/release.yml
vendored
2
.github/workflows/release.yml
vendored
@ -8,7 +8,7 @@ on:
|
||||
jobs:
|
||||
release:
|
||||
name: Create release
|
||||
runs-on: ubuntu-20.04
|
||||
runs-on: ubuntu-22.04
|
||||
steps:
|
||||
- name: Set variables
|
||||
id: init
|
||||
|
53
CHANGELOG.md
53
CHANGELOG.md
@ -1,6 +1,59 @@
|
||||
ArduinoJson: change log
|
||||
=======================
|
||||
|
||||
HEAD
|
||||
----
|
||||
|
||||
* Optimize storage of static strings
|
||||
|
||||
> ### BREAKING CHANGES
|
||||
>
|
||||
> Static string cannot contain NUL characters anymore (they could since 7.3.0).
|
||||
> This is an extremely rare case, so you probably won't be affected.
|
||||
>
|
||||
> For example, the following code produces different output in 7.3 and 7.4:
|
||||
>
|
||||
> ```cpp
|
||||
> JsonDocument doc;
|
||||
> doc["a\0b"] = "c\0d";
|
||||
> serializeJson(doc, Serial);
|
||||
> // With Arduino 7.3 -> {"a\u0000b":"c\u0000d"}
|
||||
> // With Arduino 7.4 -> {"a":"c"}
|
||||
> ```
|
||||
>
|
||||
> `JsonString` contructor now only accepts two arguments, not three.
|
||||
> If your code uses `JsonString` to store a string as a pointer, you must remove the size argument.
|
||||
>
|
||||
> For example, if you have something like this:
|
||||
>
|
||||
> ```cpp
|
||||
> doc["key"] = JsonString(str.c_str(), str.size(), true);
|
||||
> ```
|
||||
>
|
||||
> You must replace with either:
|
||||
>
|
||||
> ```cpp
|
||||
> doc["key"] = JsonString(str.c_str(), true); // store as pointer, cannot contain NUL characters
|
||||
> doc["key"] = JsonString(str.c_str(), str.size()); // store by copy, NUL characters allowed
|
||||
> doc["key"] = str; // same as previous line for supported string classes (`String`, `std::string`, etc.)
|
||||
> ```
|
||||
|
||||
v7.4.2 (2025-06-20)
|
||||
------
|
||||
|
||||
* Fix truncated strings on Arduino Due (issue #2181)
|
||||
|
||||
v7.4.1 (2025-04-11)
|
||||
------
|
||||
|
||||
* Fix crash with tiny Flash strings (issue #2170)
|
||||
|
||||
v7.4.0 (2025-04-09)
|
||||
------
|
||||
|
||||
* Optimize storage of tiny strings (up to 3 characters)
|
||||
* Fix support for `const char[]` (issue #2166)
|
||||
|
||||
v7.3.1 (2025-02-27)
|
||||
------
|
||||
|
||||
|
@ -10,7 +10,7 @@ if(ESP_PLATFORM)
|
||||
return()
|
||||
endif()
|
||||
|
||||
project(ArduinoJson VERSION 7.3.1)
|
||||
project(ArduinoJson VERSION 7.4.2)
|
||||
|
||||
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
|
||||
include(CTest)
|
||||
|
@ -80,7 +80,7 @@ ArduinoJson is a C++ JSON library for Arduino and IoT (Internet Of Things).
|
||||
* Continuously tested on
|
||||
* [Visual Studio 2017, 2019, 2022](https://ci.appveyor.com/project/bblanchon/arduinojson/branch/7.x)
|
||||
* [GCC 4.8, 5, 6, 7, 8, 9, 10, 11, 12](https://github.com/bblanchon/ArduinoJson/actions?query=workflow%3A%22Continuous+Integration%22)
|
||||
* [Clang 3.9, 4.0, 5.0, 6.0, 7, 8, 9, 10, 11, 12, 13, 14, 15](https://github.com/bblanchon/ArduinoJson/actions?query=workflow%3A%22Continuous+Integration%22)
|
||||
* [Clang 7 to 19](https://github.com/bblanchon/ArduinoJson/actions?query=workflow%3A%22Continuous+Integration%22)
|
||||
* [Continuously fuzzed with Google OSS Fuzz](https://bugs.chromium.org/p/oss-fuzz/issues/list?sort=-opened&can=1&q=proj:arduinojson)
|
||||
* Passes all default checks of [clang-tidy](https://releases.llvm.org/10.0.0/tools/clang/tools/extra/docs/clang-tidy/)
|
||||
* Well documented
|
||||
@ -139,11 +139,6 @@ See the [tutorial on arduinojson.org](https://arduinojson.org/v7/doc/serializati
|
||||
|
||||
ArduinoJson is thankful to its sponsors. Please give them a visit; they deserve it!
|
||||
|
||||
<p>
|
||||
<a href="https://www.programmingelectronics.com/" rel="sponsored">
|
||||
<img src="https://arduinojson.org/images/2021/10/programmingeleactronicsacademy.png" alt="Programming Electronics Academy" width="200">
|
||||
</a>
|
||||
</p>
|
||||
<p>
|
||||
<a href="https://github.com/1technophile" rel="sponsored">
|
||||
<img alt="1technophile" src="https://avatars.githubusercontent.com/u/12672732?s=40&v=4">
|
||||
|
@ -1,4 +1,4 @@
|
||||
version: 7.3.1.{build}
|
||||
version: 7.4.2.{build}
|
||||
environment:
|
||||
matrix:
|
||||
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2022
|
||||
|
@ -12,7 +12,7 @@ static_assert(ARDUINOJSON_LITTLE_ENDIAN == 1, "ARDUINOJSON_LITTLE_ENDIAN");
|
||||
|
||||
static_assert(ARDUINOJSON_USE_DOUBLE == 0, "ARDUINOJSON_USE_DOUBLE");
|
||||
|
||||
static_assert(ArduinoJson::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() {}
|
||||
|
@ -46,7 +46,7 @@ TEST_CASE("BasicJsonDocument") {
|
||||
deserializeJson(doc, "{\"hello\":\"world\"}");
|
||||
REQUIRE(doc.as<std::string>() == "{\"hello\":\"world\"}");
|
||||
doc.clear();
|
||||
REQUIRE(allocatorLog == "ARAARDDD");
|
||||
REQUIRE(allocatorLog == "AARARDDD");
|
||||
}
|
||||
|
||||
SECTION("copy") {
|
||||
@ -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 == "AAAA");
|
||||
}
|
||||
|
||||
SECTION("capacity") {
|
||||
|
@ -269,10 +269,16 @@ 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) {
|
||||
return ArduinoJson::detail::MemoryPool<T>::slotsToBytes(n);
|
||||
}
|
||||
|
||||
inline size_t sizeofStaticStringPool(
|
||||
ArduinoJson::detail::SlotCount n = ARDUINOJSON_POOL_CAPACITY) {
|
||||
using namespace ArduinoJson::detail;
|
||||
return MemoryPool<VariantData>::slotsToBytes(n);
|
||||
return MemoryPool<const char*>::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(sizeofStaticStringPool()),
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -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(sizeofStaticStringPool()),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -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") {
|
||||
|
@ -825,7 +825,9 @@ TEST_CASE("shrink filter") {
|
||||
|
||||
deserializeJson(doc, "{}", DeserializationOption::Filter(filter));
|
||||
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Reallocate(sizeofPool(), sizeofObject(1)),
|
||||
});
|
||||
REQUIRE(spy.log() ==
|
||||
AllocatorLog{
|
||||
Reallocate(sizeofPool(), sizeofObject(1)),
|
||||
Reallocate(sizeofStaticStringPool(), sizeofStaticStringPool(1)),
|
||||
});
|
||||
}
|
||||
|
@ -26,8 +26,8 @@ TEST_CASE("deserializeJson(char*)") {
|
||||
REQUIRE(spy.log() ==
|
||||
AllocatorLog{
|
||||
Allocate(sizeofStringBuffer()),
|
||||
Reallocate(sizeofStringBuffer(), sizeofString("hello")),
|
||||
Allocate(sizeofPool()),
|
||||
Reallocate(sizeofStringBuffer(), sizeofString("hello")),
|
||||
Allocate(sizeofStringBuffer()),
|
||||
Reallocate(sizeofStringBuffer(), sizeofString("world")),
|
||||
Reallocate(sizeofPool(), sizeofObject(1)),
|
||||
|
@ -292,22 +292,23 @@ TEST_CASE("deserialize JSON object") {
|
||||
}
|
||||
|
||||
SECTION("Repeated key") {
|
||||
DeserializationError err = deserializeJson(doc, "{a:{b:{c:1}},a:2}");
|
||||
DeserializationError err =
|
||||
deserializeJson(doc, "{alfa:{bravo:{charlie:1}},alfa:2}");
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.as<std::string>() == "{\"a\":2}");
|
||||
REQUIRE(doc.as<std::string>() == "{\"alfa\":2}");
|
||||
REQUIRE(spy.log() ==
|
||||
AllocatorLog{
|
||||
Allocate(sizeofStringBuffer()),
|
||||
Reallocate(sizeofStringBuffer(), sizeofString("a")),
|
||||
Allocate(sizeofPool()),
|
||||
Reallocate(sizeofStringBuffer(), sizeofString("alfa")),
|
||||
Allocate(sizeofStringBuffer()),
|
||||
Reallocate(sizeofStringBuffer(), sizeofString("b")),
|
||||
Reallocate(sizeofStringBuffer(), sizeofString("bravo")),
|
||||
Allocate(sizeofStringBuffer()),
|
||||
Reallocate(sizeofStringBuffer(), sizeofString("c")),
|
||||
Reallocate(sizeofStringBuffer(), sizeofString("charlie")),
|
||||
Allocate(sizeofStringBuffer()),
|
||||
Deallocate(sizeofString("b")),
|
||||
Deallocate(sizeofString("c")),
|
||||
Deallocate(sizeofString("bravo")),
|
||||
Deallocate(sizeofString("charlie")),
|
||||
Deallocate(sizeofStringBuffer()),
|
||||
Reallocate(sizeofPool(), sizeofObject(2) + sizeofObject(1)),
|
||||
});
|
||||
@ -378,7 +379,7 @@ TEST_CASE("deserialize JSON object under memory constraints") {
|
||||
}
|
||||
|
||||
SECTION("pool allocation fails") {
|
||||
timebomb.setCountdown(2);
|
||||
timebomb.setCountdown(1);
|
||||
char input[] = "{\"a\":1}";
|
||||
|
||||
DeserializationError err = deserializeJson(doc, input);
|
||||
@ -389,11 +390,11 @@ TEST_CASE("deserialize JSON object under memory constraints") {
|
||||
|
||||
SECTION("string allocation fails") {
|
||||
timebomb.setCountdown(3);
|
||||
char input[] = "{\"a\":\"b\"}";
|
||||
char input[] = "{\"alfa\":\"bravo\"}";
|
||||
|
||||
DeserializationError err = deserializeJson(doc, input);
|
||||
|
||||
REQUIRE(err == DeserializationError::NoMemory);
|
||||
REQUIRE(doc.as<std::string>() == "{\"a\":null}");
|
||||
REQUIRE(doc.as<std::string>() == "{\"alfa\":null}");
|
||||
}
|
||||
}
|
||||
|
@ -133,8 +133,8 @@ TEST_CASE("Allocation of the key fails") {
|
||||
REQUIRE(spy.log() ==
|
||||
AllocatorLog{
|
||||
Allocate(sizeofStringBuffer()),
|
||||
Reallocate(sizeofStringBuffer(), sizeofString("hello")),
|
||||
Allocate(sizeofPool()),
|
||||
Reallocate(sizeofStringBuffer(), sizeofString("hello")),
|
||||
AllocateFail(sizeofStringBuffer()),
|
||||
ReallocateFail(sizeofPool(), sizeofObject(1)),
|
||||
});
|
||||
@ -155,8 +155,8 @@ TEST_CASE("Allocation of the key fails") {
|
||||
REQUIRE(spy.log() ==
|
||||
AllocatorLog{
|
||||
Allocate(sizeofStringBuffer()),
|
||||
Reallocate(sizeofStringBuffer(), sizeofString("hello")),
|
||||
Allocate(sizeofPool()),
|
||||
Reallocate(sizeofStringBuffer(), sizeofString("hello")),
|
||||
AllocateFail(sizeofStringBuffer()),
|
||||
ReallocateFail(sizeofPool(), sizeofObject(1)),
|
||||
});
|
||||
|
@ -31,6 +31,7 @@ TEST_CASE("ElementProxy::add()") {
|
||||
REQUIRE(doc.as<std::string>() == "[[\"world\"]]");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofStaticStringPool()),
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -25,6 +25,7 @@ TEST_CASE("MemberProxy::add()") {
|
||||
REQUIRE(doc.as<std::string>() == "{\"hello\":[42]}");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofStaticStringPool()),
|
||||
});
|
||||
}
|
||||
|
||||
@ -34,6 +35,7 @@ TEST_CASE("MemberProxy::add()") {
|
||||
REQUIRE(doc.as<std::string>() == "{\"hello\":[\"world\"]}");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofStaticStringPool()),
|
||||
});
|
||||
}
|
||||
|
||||
@ -44,6 +46,7 @@ TEST_CASE("MemberProxy::add()") {
|
||||
REQUIRE(doc.as<std::string>() == "{\"hello\":[\"world\"]}");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofStaticStringPool()),
|
||||
Allocate(sizeofString("world")),
|
||||
});
|
||||
}
|
||||
@ -55,8 +58,8 @@ TEST_CASE("MemberProxy::add()") {
|
||||
REQUIRE(doc.as<std::string>() == "{\"hello\":[\"world\"]}");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofStaticStringPool()),
|
||||
Allocate(sizeofString("world")),
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
@ -71,6 +74,7 @@ TEST_CASE("MemberProxy::add()") {
|
||||
REQUIRE(doc.as<std::string>() == "{\"hello\":[\"world\"]}");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofStaticStringPool()),
|
||||
Allocate(sizeofString("world")),
|
||||
});
|
||||
}
|
||||
@ -399,7 +403,7 @@ TEST_CASE("MemberProxy under memory constraints") {
|
||||
}
|
||||
|
||||
SECTION("value slot allocation fails") {
|
||||
timebomb.setCountdown(1);
|
||||
timebomb.setCountdown(2);
|
||||
|
||||
// fill the pool entirely, but leave one slot for the key
|
||||
doc["foo"][ARDUINOJSON_POOL_CAPACITY - 4] = 1;
|
||||
@ -412,6 +416,7 @@ TEST_CASE("MemberProxy under memory constraints") {
|
||||
REQUIRE(doc.overflowed() == true);
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofStaticStringPool()),
|
||||
AllocateFail(sizeofPool()),
|
||||
});
|
||||
}
|
||||
|
@ -32,6 +32,7 @@ TEST_CASE("JsonDocument::add(T)") {
|
||||
REQUIRE(doc.as<std::string>() == "[\"hello\"]");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofStaticStringPool()),
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -69,6 +69,8 @@ TEST_CASE("JsonDocument assignment") {
|
||||
doc2 = std::move(doc1);
|
||||
|
||||
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
|
||||
|
||||
// NOLINTNEXTLINE(clang-analyzer-cplusplus.Move)
|
||||
REQUIRE(doc1.as<std::string>() == "null");
|
||||
}
|
||||
REQUIRE(spyingAllocator.log() == AllocatorLog{
|
||||
|
@ -44,6 +44,8 @@ TEST_CASE("JsonDocument constructor") {
|
||||
JsonDocument doc2(std::move(doc1));
|
||||
|
||||
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
|
||||
|
||||
// NOLINTNEXTLINE(clang-analyzer-cplusplus.Move)
|
||||
REQUIRE(doc1.as<std::string>() == "null");
|
||||
}
|
||||
REQUIRE(spyingAllocator.log() == AllocatorLog{
|
||||
@ -62,6 +64,7 @@ TEST_CASE("JsonDocument constructor") {
|
||||
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
|
||||
REQUIRE(spyingAllocator.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofStaticStringPool()),
|
||||
});
|
||||
}
|
||||
|
||||
@ -85,6 +88,7 @@ TEST_CASE("JsonDocument constructor") {
|
||||
REQUIRE(doc2.as<std::string>() == "[\"hello\"]");
|
||||
REQUIRE(spyingAllocator.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofStaticStringPool()),
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -22,10 +22,10 @@ TEST_CASE("JsonDocument::remove()") {
|
||||
|
||||
SECTION("string literal") {
|
||||
doc["a"] = 1;
|
||||
doc["a\0b"_s] = 2;
|
||||
doc["x"] = 2;
|
||||
doc["b"] = 3;
|
||||
|
||||
doc.remove("a\0b");
|
||||
doc.remove("x");
|
||||
|
||||
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(sizeofStaticStringPool()),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("const char*") {
|
||||
@ -87,6 +89,13 @@ TEST_CASE("JsonDocument::set()") {
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("Flash tiny string") { // issue #2170
|
||||
doc.set(F("abc"));
|
||||
|
||||
REQUIRE(doc.as<const char*>() == "abc"_s);
|
||||
REQUIRE(spy.log() == AllocatorLog{});
|
||||
}
|
||||
|
||||
#ifdef HAS_VARIABLE_LENGTH_ARRAY
|
||||
SECTION("VLA") {
|
||||
size_t i = 16;
|
||||
|
@ -75,7 +75,11 @@ TEST_CASE("JsonDocument::shrinkToFit()") {
|
||||
doc.shrinkToFit();
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "hello");
|
||||
REQUIRE(spyingAllocator.log() == AllocatorLog{});
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog{
|
||||
Allocate(sizeofStaticStringPool()),
|
||||
Reallocate(sizeofStaticStringPool(), sizeofStaticStringPool(1)),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("owned string") {
|
||||
@ -110,7 +114,9 @@ TEST_CASE("JsonDocument::shrinkToFit()") {
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofStaticStringPool()),
|
||||
Reallocate(sizeofPool(), sizeofObject(1)),
|
||||
Reallocate(sizeofStaticStringPool(), sizeofStaticStringPool(1)),
|
||||
});
|
||||
}
|
||||
|
||||
@ -137,7 +143,9 @@ TEST_CASE("JsonDocument::shrinkToFit()") {
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofStaticStringPool()),
|
||||
Reallocate(sizeofPool(), sizeofArray(1)),
|
||||
Reallocate(sizeofStaticStringPool(), sizeofStaticStringPool(1)),
|
||||
});
|
||||
}
|
||||
|
||||
@ -164,20 +172,23 @@ TEST_CASE("JsonDocument::shrinkToFit()") {
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofStaticStringPool()),
|
||||
Reallocate(sizeofPool(), sizeofObject(1)),
|
||||
Reallocate(sizeofStaticStringPool(), sizeofStaticStringPool(2)),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("owned string in object") {
|
||||
doc["key"] = "abcdefg"_s;
|
||||
doc["key1"_s] = "value"_s;
|
||||
|
||||
doc.shrinkToFit();
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "{\"key\":\"abcdefg\"}");
|
||||
REQUIRE(doc.as<std::string>() == "{\"key1\":\"value\"}");
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("abcdefg")),
|
||||
Allocate(sizeofString("key1")),
|
||||
Allocate(sizeofString("value")),
|
||||
Reallocate(sizeofPool(), sizeofPool(2)),
|
||||
});
|
||||
}
|
||||
|
@ -25,8 +25,6 @@ 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");
|
||||
}
|
||||
|
||||
SECTION("std::string") {
|
||||
@ -114,6 +112,7 @@ TEST_CASE("JsonDocument::operator[] key storage") {
|
||||
REQUIRE(doc.as<std::string>() == "{\"hello\":0}");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofStaticStringPool()),
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -26,25 +26,12 @@ TEST_CASE("JsonObject::set()") {
|
||||
REQUIRE(obj2["hello"] == "world"_s);
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofStaticStringPool()),
|
||||
});
|
||||
}
|
||||
|
||||
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";
|
||||
SECTION("copy local string key and value") {
|
||||
obj1["hello"_s] = "world"_s;
|
||||
spy.clearLog();
|
||||
|
||||
bool success = obj2.set(obj1);
|
||||
@ -54,6 +41,7 @@ TEST_CASE("JsonObject::set()") {
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
Allocate(sizeofString("world")),
|
||||
});
|
||||
}
|
||||
|
||||
@ -100,17 +88,17 @@ TEST_CASE("JsonObject::set()") {
|
||||
JsonDocument doc3(&timebomb);
|
||||
JsonObject obj3 = doc3.to<JsonObject>();
|
||||
|
||||
obj1["a"_s] = 1;
|
||||
obj1["b"_s] = 2;
|
||||
obj1["alpha"_s] = 1;
|
||||
obj1["beta"_s] = 2;
|
||||
|
||||
bool success = obj3.set(obj1);
|
||||
|
||||
REQUIRE(success == false);
|
||||
REQUIRE(doc3.as<std::string>() == "{\"a\":1}");
|
||||
REQUIRE(doc3.as<std::string>() == "{\"alpha\":1}");
|
||||
}
|
||||
|
||||
SECTION("copy fails in the middle of an array") {
|
||||
TimebombAllocator timebomb(1);
|
||||
TimebombAllocator timebomb(2);
|
||||
JsonDocument doc3(&timebomb);
|
||||
JsonObject obj3 = doc3.to<JsonObject>();
|
||||
|
||||
|
@ -102,21 +102,25 @@ TEST_CASE("JsonObject::operator[]") {
|
||||
REQUIRE(42 == obj[key]);
|
||||
}
|
||||
|
||||
SECTION("should not duplicate const char*") {
|
||||
SECTION("string literals") {
|
||||
obj["hello"] = "world";
|
||||
REQUIRE(spy.log() == AllocatorLog{Allocate(sizeofPool())});
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofStaticStringPool()),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("should duplicate char* value") {
|
||||
obj["hello"] = const_cast<char*>("world");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofStaticStringPool()),
|
||||
Allocate(sizeofString("world")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("should duplicate char* key") {
|
||||
obj[const_cast<char*>("hello")] = "world";
|
||||
obj[const_cast<char*>("hello")] = 42;
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
@ -136,12 +140,13 @@ TEST_CASE("JsonObject::operator[]") {
|
||||
obj["hello"] = "world"_s;
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofStaticStringPool()),
|
||||
Allocate(sizeofString("world")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("should duplicate std::string key") {
|
||||
obj["hello"_s] = "world";
|
||||
obj["hello"_s] = 42;
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
@ -158,7 +163,7 @@ TEST_CASE("JsonObject::operator[]") {
|
||||
}
|
||||
|
||||
SECTION("should duplicate a non-static JsonString key") {
|
||||
obj[JsonString("hello", false)] = "world";
|
||||
obj[JsonString("hello", false)] = 42;
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("hello")),
|
||||
@ -166,9 +171,10 @@ TEST_CASE("JsonObject::operator[]") {
|
||||
}
|
||||
|
||||
SECTION("should not duplicate a static JsonString key") {
|
||||
obj[JsonString("hello", true)] = "world";
|
||||
obj[JsonString("hello", true)] = 42;
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofStaticStringPool()),
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -199,7 +199,7 @@ TEST_CASE("JsonVariant::as()") {
|
||||
REQUIRE(variant.as<JsonString>() == "hello");
|
||||
}
|
||||
|
||||
SECTION("set(std::string(\"4.2\"))") {
|
||||
SECTION("set(std::string(\"4.2\")) (tiny string optimization)") {
|
||||
variant.set("4.2"_s);
|
||||
|
||||
REQUIRE(variant.as<bool>() == true);
|
||||
@ -211,6 +211,18 @@ TEST_CASE("JsonVariant::as()") {
|
||||
REQUIRE(variant.as<JsonString>().isStatic() == false);
|
||||
}
|
||||
|
||||
SECTION("set(std::string(\"123.45\"))") {
|
||||
variant.set("123.45"_s);
|
||||
|
||||
REQUIRE(variant.as<bool>() == true);
|
||||
REQUIRE(variant.as<long>() == 123L);
|
||||
REQUIRE(variant.as<double>() == Approx(123.45));
|
||||
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\")") {
|
||||
variant.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 pointer") {
|
||||
var1.set("hello!!");
|
||||
spyingAllocator.clearLog();
|
||||
|
||||
var2.set(var1);
|
||||
|
||||
REQUIRE(spyingAllocator.log() == AllocatorLog{});
|
||||
REQUIRE(spyingAllocator.log() == AllocatorLog{
|
||||
Allocate(sizeofStaticStringPool()),
|
||||
});
|
||||
}
|
||||
|
||||
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 };
|
||||
|
||||
@ -23,7 +23,9 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
|
||||
REQUIRE(result == true);
|
||||
CHECK(variant ==
|
||||
"hello"_s); // linked string cannot contain '\0' at the moment
|
||||
CHECK(spy.log() == AllocatorLog{});
|
||||
CHECK(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofStaticStringPool()),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("const char*") {
|
||||
@ -63,6 +65,18 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("char* (tiny string optimization)") {
|
||||
char str[16];
|
||||
|
||||
strcpy(str, "abc");
|
||||
bool result = variant.set(str);
|
||||
strcpy(str, "def");
|
||||
|
||||
REQUIRE(result == true);
|
||||
REQUIRE(variant == "abc"); // stores by copy
|
||||
REQUIRE(spy.log() == AllocatorLog{});
|
||||
}
|
||||
|
||||
SECTION("(char*)0") {
|
||||
bool result = variant.set(static_cast<char*>(0));
|
||||
|
||||
@ -137,7 +151,9 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
|
||||
|
||||
REQUIRE(result == true);
|
||||
REQUIRE(variant == "world"); // stores by pointer
|
||||
REQUIRE(spy.log() == AllocatorLog{});
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofStaticStringPool()),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("non-static JsonString") {
|
||||
@ -181,11 +197,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") {
|
||||
@ -204,11 +220,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") {
|
||||
@ -227,11 +243,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") {
|
||||
@ -253,6 +269,20 @@ TEST_CASE("JsonVariant::set() with not enough memory") {
|
||||
|
||||
JsonVariant v = doc.to<JsonVariant>();
|
||||
|
||||
SECTION("string literal") {
|
||||
bool result = v.set("hello world");
|
||||
|
||||
REQUIRE(result == false);
|
||||
REQUIRE(v.isNull());
|
||||
}
|
||||
|
||||
SECTION("static JsonString") {
|
||||
bool result = v.set(JsonString("hello world", true));
|
||||
|
||||
REQUIRE(result == false);
|
||||
REQUIRE(v.isNull());
|
||||
}
|
||||
|
||||
SECTION("std::string") {
|
||||
bool result = v.set("hello world!!"_s);
|
||||
|
||||
@ -348,7 +378,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")),
|
||||
});
|
||||
@ -363,7 +393,7 @@ TEST_CASE("JsonVariant::set() releases the previous value") {
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("JsonVariant::set() reuses extension slot") {
|
||||
TEST_CASE("JsonVariant::set() reuses 8-bit slot") {
|
||||
SpyingAllocator spy;
|
||||
JsonDocument doc(&spy);
|
||||
JsonVariant variant = doc.to<JsonVariant>();
|
||||
|
@ -57,7 +57,7 @@ TEST_CASE("JsonVariantConst::operator[]") {
|
||||
SECTION("string literal") {
|
||||
REQUIRE(var["ab"] == "AB"_s);
|
||||
REQUIRE(var["abc"] == "ABC"_s);
|
||||
REQUIRE(var["abc\0d"] == "ABCD"_s);
|
||||
REQUIRE(var["abc\0d"] == "ABC"_s);
|
||||
REQUIRE(var["def"].isNull());
|
||||
REQUIRE(var[0].isNull());
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ add_executable(MiscTests
|
||||
conflicts.cpp
|
||||
issue1967.cpp
|
||||
issue2129.cpp
|
||||
issue2166.cpp
|
||||
JsonString.cpp
|
||||
NoArduinoHeader.cpp
|
||||
printable.cpp
|
||||
@ -28,3 +29,23 @@ set_tests_properties(Misc
|
||||
PROPERTIES
|
||||
LABELS "Catch"
|
||||
)
|
||||
|
||||
add_executable(Issue2181
|
||||
issue2181.cpp # Cannot be linked with other tests
|
||||
)
|
||||
|
||||
set_target_properties(Issue2181 PROPERTIES UNITY_BUILD OFF)
|
||||
|
||||
add_test(Issue2181 Issue2181)
|
||||
|
||||
set_tests_properties(Issue2181
|
||||
PROPERTIES
|
||||
LABELS "Catch"
|
||||
)
|
||||
|
||||
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
|
||||
target_compile_options(Issue2181
|
||||
PRIVATE
|
||||
-Wno-keyword-macro # keyword is hidden by macro definition
|
||||
)
|
||||
endif()
|
||||
|
@ -21,7 +21,7 @@ TEST_CASE("adaptString()") {
|
||||
auto s = adaptString("bravo\0alpha");
|
||||
|
||||
CHECK(s.isNull() == false);
|
||||
CHECK(s.size() == 11);
|
||||
CHECK(s.size() == 5);
|
||||
CHECK(s.isStatic() == true);
|
||||
}
|
||||
|
||||
@ -128,6 +128,7 @@ TEST_CASE("IsString<T>") {
|
||||
CHECK(IsString<const __FlashStringHelper*>::value == true);
|
||||
CHECK(IsString<const char*>::value == true);
|
||||
CHECK(IsString<const char[8]>::value == true);
|
||||
CHECK(IsString<const char[]>::value == true);
|
||||
CHECK(IsString<::String>::value == true);
|
||||
CHECK(IsString<::StringSumHelper>::value == true);
|
||||
CHECK(IsString<const EmptyStruct*>::value == false);
|
||||
|
22
extras/tests/Misc/issue2166.cpp
Normal file
22
extras/tests/Misc/issue2166.cpp
Normal file
@ -0,0 +1,22 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2025, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
struct CCLASS {
|
||||
static const char mszKey[];
|
||||
};
|
||||
|
||||
TEST_CASE("Issue #2166") {
|
||||
JsonDocument doc;
|
||||
doc[CCLASS::mszKey] = 12;
|
||||
REQUIRE(doc.as<std::string>() == "{\"test3\":12}");
|
||||
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
obj[CCLASS::mszKey] = 12;
|
||||
REQUIRE(doc.as<std::string>() == "{\"test3\":12}");
|
||||
}
|
||||
|
||||
const char CCLASS::mszKey[] = "test3";
|
15
extras/tests/Misc/issue2181.cpp
Normal file
15
extras/tests/Misc/issue2181.cpp
Normal file
@ -0,0 +1,15 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2025, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#define true 0x1
|
||||
#define false 0x0
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("Issue #2181") {
|
||||
JsonDocument doc;
|
||||
doc["hello"] = "world";
|
||||
REQUIRE(doc.as<std::string>() == "{\"hello\":\"world\"}");
|
||||
}
|
@ -5,8 +5,11 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
|
||||
TEST_CASE("deserialize MsgPack array") {
|
||||
JsonDocument doc;
|
||||
SpyingAllocator spy;
|
||||
JsonDocument doc(&spy);
|
||||
|
||||
SECTION("fixarray") {
|
||||
SECTION("empty") {
|
||||
@ -30,6 +33,24 @@ TEST_CASE("deserialize MsgPack array") {
|
||||
REQUIRE(array[0] == 1);
|
||||
REQUIRE(array[1] == 2);
|
||||
}
|
||||
|
||||
SECTION("tiny strings") {
|
||||
DeserializationError error =
|
||||
deserializeMsgPack(doc, "\x92\xA3xxx\xA3yyy");
|
||||
|
||||
REQUIRE(error == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<JsonArray>());
|
||||
REQUIRE(doc.size() == 2);
|
||||
REQUIRE(doc[0] == "xxx");
|
||||
REQUIRE(doc[1] == "yyy");
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
Allocate(sizeofPool()),
|
||||
Allocate(sizeofString("xxx")),
|
||||
// Buffer is reused for the next string
|
||||
Deallocate(sizeofString("xxx")),
|
||||
Reallocate(sizeofPool(), sizeofPool(2)),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("array 16") {
|
||||
|
@ -348,13 +348,14 @@ TEST_CASE("deserializeMsgPack() under memory constaints") {
|
||||
SECTION("{}") {
|
||||
checkError(0, "\x80", DeserializationError::Ok);
|
||||
}
|
||||
SECTION("{H:1}") {
|
||||
checkError(1, "\x81\xA1H\x01", DeserializationError::NoMemory);
|
||||
checkError(2, "\x81\xA1H\x01", DeserializationError::Ok);
|
||||
SECTION("{Hello:1}") {
|
||||
checkError(1, "\x81\xA5Hello\x01", DeserializationError::NoMemory);
|
||||
checkError(2, "\x81\xA5Hello\x01", DeserializationError::Ok);
|
||||
}
|
||||
SECTION("{H:1,W:2}") {
|
||||
checkError(2, "\x82\xA1H\x01\xA1W\x02", DeserializationError::NoMemory);
|
||||
checkError(3, "\x82\xA1H\x01\xA1W\x02", DeserializationError::Ok);
|
||||
SECTION("{Hello:1,World:2}") {
|
||||
checkError(2, "\x82\xA5Hello\x01\xA5World\x02",
|
||||
DeserializationError::NoMemory);
|
||||
checkError(3, "\x82\xA5Hello\x01\xA5World\x02", DeserializationError::Ok);
|
||||
}
|
||||
}
|
||||
|
||||
@ -362,14 +363,16 @@ TEST_CASE("deserializeMsgPack() under memory constaints") {
|
||||
SECTION("{}") {
|
||||
checkError(0, "\xDE\x00\x00", DeserializationError::Ok);
|
||||
}
|
||||
SECTION("{H:1}") {
|
||||
checkError(1, "\xDE\x00\x01\xA1H\x01", DeserializationError::NoMemory);
|
||||
checkError(2, "\xDE\x00\x01\xA1H\x01", DeserializationError::Ok);
|
||||
}
|
||||
SECTION("{H:1,W:2}") {
|
||||
checkError(2, "\xDE\x00\x02\xA1H\x01\xA1W\x02",
|
||||
SECTION("{Hello:1}") {
|
||||
checkError(1, "\xDE\x00\x01\xA5Hello\x01",
|
||||
DeserializationError::NoMemory);
|
||||
checkError(3, "\xDE\x00\x02\xA1H\x01\xA1W\x02", DeserializationError::Ok);
|
||||
checkError(2, "\xDE\x00\x01\xA5Hello\x01", DeserializationError::Ok);
|
||||
}
|
||||
SECTION("{Hello:1,World:2}") {
|
||||
checkError(2, "\xDE\x00\x02\xA5Hello\x01\xA5World\x02",
|
||||
DeserializationError::NoMemory);
|
||||
checkError(3, "\xDE\x00\x02\xA5Hello\x01\xA5World\x02",
|
||||
DeserializationError::Ok);
|
||||
}
|
||||
}
|
||||
|
||||
@ -382,8 +385,8 @@ TEST_CASE("deserializeMsgPack() under memory constaints") {
|
||||
DeserializationError::NoMemory);
|
||||
checkError(2, "\xDF\x00\x00\x00\x01\xA1H\x01", DeserializationError::Ok);
|
||||
}
|
||||
SECTION("{H:1,W:2}") {
|
||||
checkError(2, "\xDF\x00\x00\x00\x02\xA1H\x01\xA1W\x02",
|
||||
SECTION("{Hello:1,World:2}") {
|
||||
checkError(2, "\xDF\x00\x00\x00\x02\xA5Hello\x01\xA5World\x02",
|
||||
DeserializationError::NoMemory);
|
||||
checkError(3, "\xDF\x00\x00\x00\x02\xA1H\x01\xA1W\x02",
|
||||
DeserializationError::Ok);
|
||||
|
@ -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(sizeofStaticStringPool()),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -5,9 +5,11 @@
|
||||
add_executable(ResourceManagerTests
|
||||
allocVariant.cpp
|
||||
clear.cpp
|
||||
saveStaticString.cpp
|
||||
saveString.cpp
|
||||
shrinkToFit.cpp
|
||||
size.cpp
|
||||
StringBuffer.cpp
|
||||
StringBuilder.cpp
|
||||
swap.cpp
|
||||
)
|
||||
|
50
extras/tests/ResourceManager/StringBuffer.cpp
Normal file
50
extras/tests/ResourceManager/StringBuffer.cpp
Normal file
@ -0,0 +1,50 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2025, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
#include "Literals.hpp"
|
||||
|
||||
using namespace ArduinoJson::detail;
|
||||
|
||||
TEST_CASE("StringBuffer") {
|
||||
SpyingAllocator spy;
|
||||
ResourceManager resources(&spy);
|
||||
StringBuffer sb(&resources);
|
||||
VariantData variant;
|
||||
|
||||
SECTION("Tiny string") {
|
||||
auto ptr = sb.reserve(3);
|
||||
strcpy(ptr, "hi!");
|
||||
sb.save(&variant);
|
||||
|
||||
REQUIRE(variant.type == VariantType::TinyString);
|
||||
REQUIRE(VariantImpl(&variant, &resources).asString() == "hi!");
|
||||
}
|
||||
|
||||
SECTION("Tiny string can't contain NUL") {
|
||||
auto ptr = sb.reserve(3);
|
||||
memcpy(ptr, "a\0b", 3);
|
||||
sb.save(&variant);
|
||||
|
||||
REQUIRE(variant.type == VariantType::OwnedString);
|
||||
|
||||
auto str = VariantImpl(&variant, &resources).asString();
|
||||
REQUIRE(str.size() == 3);
|
||||
REQUIRE(str.c_str()[0] == 'a');
|
||||
REQUIRE(str.c_str()[1] == 0);
|
||||
REQUIRE(str.c_str()[2] == 'b');
|
||||
}
|
||||
|
||||
SECTION("Tiny string can't have 4 characters") {
|
||||
auto ptr = sb.reserve(4);
|
||||
strcpy(ptr, "alfa");
|
||||
sb.save(&variant);
|
||||
|
||||
REQUIRE(variant.type == VariantType::OwnedString);
|
||||
REQUIRE(VariantImpl(&variant, &resources).asString() == "alfa");
|
||||
}
|
||||
}
|
@ -2,11 +2,12 @@
|
||||
// Copyright © 2014-2025, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Memory/StringBuilder.hpp>
|
||||
#include <ArduinoJson.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
|
||||
using namespace ArduinoJson;
|
||||
using namespace ArduinoJson::detail;
|
||||
|
||||
TEST_CASE("StringBuilder") {
|
||||
@ -16,17 +17,36 @@ TEST_CASE("StringBuilder") {
|
||||
|
||||
SECTION("Empty string") {
|
||||
StringBuilder str(&resources);
|
||||
VariantData data;
|
||||
|
||||
str.startString();
|
||||
str.save();
|
||||
str.save(&data);
|
||||
|
||||
REQUIRE(resources.size() == sizeofString(""));
|
||||
REQUIRE(resources.overflowed() == false);
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog{
|
||||
Allocate(sizeofStringBuffer()),
|
||||
Reallocate(sizeofStringBuffer(), sizeofString("")),
|
||||
});
|
||||
REQUIRE(spyingAllocator.log() == AllocatorLog{
|
||||
Allocate(sizeofStringBuffer()),
|
||||
});
|
||||
REQUIRE(data.type == VariantType::TinyString);
|
||||
}
|
||||
|
||||
SECTION("Tiny string") {
|
||||
StringBuilder str(&resources);
|
||||
|
||||
str.startString();
|
||||
str.append("url");
|
||||
|
||||
REQUIRE(str.isValid() == true);
|
||||
REQUIRE(str.str() == "url");
|
||||
REQUIRE(spyingAllocator.log() == AllocatorLog{
|
||||
Allocate(sizeofStringBuffer()),
|
||||
});
|
||||
|
||||
VariantData data;
|
||||
str.save(&data);
|
||||
|
||||
REQUIRE(resources.overflowed() == false);
|
||||
REQUIRE(data.type == VariantType::TinyString);
|
||||
REQUIRE(VariantImpl(&data, &resources).asString() == "url");
|
||||
}
|
||||
|
||||
SECTION("Short string fits in first allocation") {
|
||||
@ -96,48 +116,74 @@ TEST_CASE("StringBuilder") {
|
||||
}
|
||||
}
|
||||
|
||||
static StringNode* addStringToPool(ResourceManager& resources, const char* s) {
|
||||
StringBuilder str(&resources);
|
||||
str.startString();
|
||||
str.append(s);
|
||||
return str.save();
|
||||
static VariantData saveString(StringBuilder& builder, const char* s) {
|
||||
VariantData data;
|
||||
builder.startString();
|
||||
builder.append(s);
|
||||
builder.save(&data);
|
||||
return data;
|
||||
}
|
||||
|
||||
TEST_CASE("StringBuilder::save() deduplicates strings") {
|
||||
ResourceManager resources;
|
||||
SpyingAllocator spy;
|
||||
ResourceManager resources(&spy);
|
||||
StringBuilder builder(&resources);
|
||||
|
||||
SECTION("Basic") {
|
||||
auto s1 = addStringToPool(resources, "hello");
|
||||
auto s2 = addStringToPool(resources, "world");
|
||||
auto s3 = addStringToPool(resources, "hello");
|
||||
auto s1 = saveString(builder, "hello");
|
||||
auto s2 = saveString(builder, "world");
|
||||
auto s3 = saveString(builder, "hello");
|
||||
|
||||
REQUIRE(s1 == s3);
|
||||
REQUIRE(s2 != s3);
|
||||
REQUIRE(s1->references == 2);
|
||||
REQUIRE(s2->references == 1);
|
||||
REQUIRE(s3->references == 2);
|
||||
REQUIRE(resources.size() == sizeofString("hello") + sizeofString("world"));
|
||||
REQUIRE(VariantImpl(&s1, &resources).asString() == "hello");
|
||||
REQUIRE(VariantImpl(&s2, &resources).asString() == "world");
|
||||
REQUIRE(+VariantImpl(&s1, &resources).asString().c_str() ==
|
||||
+VariantImpl(&s3, &resources).asString().c_str()); // same address
|
||||
|
||||
REQUIRE(spy.log() ==
|
||||
AllocatorLog{
|
||||
Allocate(sizeofStringBuffer()),
|
||||
Reallocate(sizeofStringBuffer(), sizeofString("hello")),
|
||||
Allocate(sizeofStringBuffer()),
|
||||
Reallocate(sizeofStringBuffer(), sizeofString("world")),
|
||||
Allocate(sizeofStringBuffer()),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("Requires terminator") {
|
||||
auto s1 = addStringToPool(resources, "hello world");
|
||||
auto s2 = addStringToPool(resources, "hello");
|
||||
auto s1 = saveString(builder, "hello world");
|
||||
auto s2 = saveString(builder, "hello");
|
||||
|
||||
REQUIRE(s2 != s1);
|
||||
REQUIRE(s1->references == 1);
|
||||
REQUIRE(s2->references == 1);
|
||||
REQUIRE(resources.size() ==
|
||||
sizeofString("hello world") + sizeofString("hello"));
|
||||
REQUIRE(VariantImpl(&s1, &resources).asString() == "hello world");
|
||||
REQUIRE(VariantImpl(&s2, &resources).asString() == "hello");
|
||||
REQUIRE(
|
||||
+VariantImpl(&s1, &resources).asString().c_str() !=
|
||||
+VariantImpl(&s2, &resources).asString().c_str()); // different address
|
||||
|
||||
REQUIRE(spy.log() ==
|
||||
AllocatorLog{
|
||||
Allocate(sizeofStringBuffer()),
|
||||
Reallocate(sizeofStringBuffer(), sizeofString("hello world")),
|
||||
Allocate(sizeofStringBuffer()),
|
||||
Reallocate(sizeofStringBuffer(), sizeofString("hello")),
|
||||
});
|
||||
}
|
||||
|
||||
SECTION("Don't overrun") {
|
||||
auto s1 = addStringToPool(resources, "hello world");
|
||||
auto s2 = addStringToPool(resources, "wor");
|
||||
auto s1 = saveString(builder, "hello world");
|
||||
auto s2 = saveString(builder, "worl");
|
||||
|
||||
REQUIRE(s2 != s1);
|
||||
REQUIRE(s1->references == 1);
|
||||
REQUIRE(s2->references == 1);
|
||||
REQUIRE(resources.size() ==
|
||||
sizeofString("hello world") + sizeofString("wor"));
|
||||
REQUIRE(VariantImpl(&s1, &resources).asString() == "hello world");
|
||||
REQUIRE(VariantImpl(&s2, &resources).asString() == "worl");
|
||||
REQUIRE(
|
||||
VariantImpl(&s1, &resources).asString().c_str() !=
|
||||
VariantImpl(&s2, &resources).asString().c_str()); // different address
|
||||
|
||||
REQUIRE(spy.log() ==
|
||||
AllocatorLog{
|
||||
Allocate(sizeofStringBuffer()),
|
||||
Reallocate(sizeofStringBuffer(), sizeofString("hello world")),
|
||||
Allocate(sizeofStringBuffer()),
|
||||
Reallocate(sizeofStringBuffer(), sizeofString("worl")),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
47
extras/tests/ResourceManager/saveStaticString.cpp
Normal file
47
extras/tests/ResourceManager/saveStaticString.cpp
Normal file
@ -0,0 +1,47 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2025, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Memory/ResourceManager.hpp>
|
||||
#include <ArduinoJson/Strings/StringAdapters.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
|
||||
using namespace ArduinoJson::detail;
|
||||
|
||||
TEST_CASE("ResourceManager::saveStaticString() deduplicates strings") {
|
||||
SpyingAllocator spy;
|
||||
ResourceManager resources(&spy);
|
||||
|
||||
auto str1 = "hello";
|
||||
auto str2 = "world";
|
||||
|
||||
auto id1 = resources.saveStaticString(str1);
|
||||
auto id2 = resources.saveStaticString(str2);
|
||||
REQUIRE(id1 != id2);
|
||||
|
||||
auto id3 = resources.saveStaticString(str1);
|
||||
REQUIRE(id1 == id3);
|
||||
|
||||
resources.shrinkToFit();
|
||||
REQUIRE(spy.log() ==
|
||||
AllocatorLog{
|
||||
Allocate(sizeofStaticStringPool()),
|
||||
Reallocate(sizeofStaticStringPool(), sizeofStaticStringPool(2)),
|
||||
});
|
||||
REQUIRE(resources.overflowed() == false);
|
||||
}
|
||||
|
||||
TEST_CASE("ResourceManager::saveStaticString() when allocation fails") {
|
||||
SpyingAllocator spy(FailingAllocator::instance());
|
||||
ResourceManager resources(&spy);
|
||||
|
||||
auto slotId = resources.saveStaticString("hello");
|
||||
|
||||
REQUIRE(slotId == NULL_SLOT);
|
||||
REQUIRE(resources.overflowed() == true);
|
||||
REQUIRE(spy.log() == AllocatorLog{
|
||||
AllocateFail(sizeofStaticStringPool()),
|
||||
});
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
version: "7.3.1"
|
||||
version: "7.4.2"
|
||||
description: >-
|
||||
A simple and efficient JSON library for embedded C++.
|
||||
★ 6849 stars on GitHub!
|
||||
★ 6953 stars on GitHub!
|
||||
Supports serialization, deserialization, MessagePack, streams, filtering, and more.
|
||||
Fully tested and documented.
|
||||
url: https://arduinojson.org/
|
||||
|
@ -1,13 +1,13 @@
|
||||
{
|
||||
"name": "ArduinoJson",
|
||||
"keywords": "json, rest, http, web",
|
||||
"description": "A simple and efficient JSON library for embedded C++. ⭐ 6849 stars on GitHub! Supports serialization, deserialization, MessagePack, streams, filtering, and more. Fully tested and documented.",
|
||||
"description": "A simple and efficient JSON library for embedded C++. ⭐ 6953 stars on GitHub! Supports serialization, deserialization, MessagePack, streams, filtering, and more. Fully tested and documented.",
|
||||
"homepage": "https://arduinojson.org/?utm_source=meta&utm_medium=library.json",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/bblanchon/ArduinoJson.git"
|
||||
},
|
||||
"version": "7.3.1",
|
||||
"version": "7.4.2",
|
||||
"authors": {
|
||||
"name": "Benoit Blanchon",
|
||||
"url": "https://blog.benoitblanchon.fr"
|
||||
|
@ -1,9 +1,9 @@
|
||||
name=ArduinoJson
|
||||
version=7.3.1
|
||||
version=7.4.2
|
||||
author=Benoit Blanchon <blog.benoitblanchon.fr>
|
||||
maintainer=Benoit Blanchon <blog.benoitblanchon.fr>
|
||||
sentence=A simple and efficient JSON library for embedded C++.
|
||||
paragraph=⭐ 6849 stars on GitHub! Supports serialization, deserialization, MessagePack, streams, filtering, and more. Fully tested and documented.
|
||||
paragraph=⭐ 6953 stars on GitHub! Supports serialization, deserialization, MessagePack, streams, filtering, and more. Fully tested and documented.
|
||||
category=Data Processing
|
||||
url=https://arduinojson.org/?utm_source=meta&utm_medium=library.properties
|
||||
architectures=*
|
||||
|
@ -26,6 +26,15 @@
|
||||
# endif
|
||||
#endif
|
||||
|
||||
// Remove true and false macros defined by some cores, such as Arduino Due's
|
||||
// See issues #2181 and arduino/ArduinoCore-sam#50
|
||||
#ifdef true
|
||||
# undef true
|
||||
#endif
|
||||
#ifdef false
|
||||
# undef false
|
||||
#endif
|
||||
|
||||
#include "ArduinoJson/Array/JsonArray.hpp"
|
||||
#include "ArduinoJson/Object/JsonObject.hpp"
|
||||
#include "ArduinoJson/Variant/JsonVariantConst.hpp"
|
||||
@ -42,7 +51,6 @@
|
||||
#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"
|
||||
|
@ -8,59 +8,30 @@
|
||||
|
||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
|
||||
class ArrayData : public CollectionData {
|
||||
class ArrayImpl : public CollectionImpl {
|
||||
public:
|
||||
VariantData* addElement(ResourceManager* resources);
|
||||
ArrayImpl() {}
|
||||
|
||||
static VariantData* addElement(ArrayData* array, ResourceManager* resources) {
|
||||
if (!array)
|
||||
return nullptr;
|
||||
return array->addElement(resources);
|
||||
}
|
||||
ArrayImpl(CollectionData* data, ResourceManager* resources)
|
||||
: CollectionImpl(data, resources) {}
|
||||
|
||||
VariantData* addElement();
|
||||
|
||||
template <typename T>
|
||||
bool addValue(const T& value, ResourceManager* resources);
|
||||
bool addValue(const T& value);
|
||||
|
||||
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);
|
||||
|
||||
VariantData* getOrAddElement(size_t index, ResourceManager* resources);
|
||||
VariantData* getElement(size_t index) const;
|
||||
|
||||
VariantData* getElement(size_t index, const ResourceManager* resources) const;
|
||||
void removeElement(size_t index);
|
||||
|
||||
static VariantData* getElement(const ArrayData* array, size_t index,
|
||||
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);
|
||||
void remove(iterator it) {
|
||||
CollectionImpl::removeOne(it);
|
||||
}
|
||||
|
||||
private:
|
||||
iterator at(size_t index, const ResourceManager* resources) const;
|
||||
iterator at(size_t index) const;
|
||||
};
|
||||
|
||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||
|
@ -10,36 +10,37 @@
|
||||
|
||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
|
||||
inline ArrayData::iterator ArrayData::at(
|
||||
size_t index, const ResourceManager* resources) const {
|
||||
auto it = createIterator(resources);
|
||||
inline ArrayImpl::iterator ArrayImpl::at(size_t index) const {
|
||||
auto it = createIterator();
|
||||
while (!it.done() && index) {
|
||||
it.next(resources);
|
||||
it.next(resources_);
|
||||
--index;
|
||||
}
|
||||
return it;
|
||||
}
|
||||
|
||||
inline VariantData* ArrayData::addElement(ResourceManager* resources) {
|
||||
auto slot = resources->allocVariant();
|
||||
inline VariantData* ArrayImpl::addElement() {
|
||||
if (!data_)
|
||||
return nullptr;
|
||||
ARDUINOJSON_ASSERT(resources_ != nullptr);
|
||||
auto slot = resources_->allocVariant();
|
||||
if (!slot)
|
||||
return nullptr;
|
||||
CollectionData::appendOne(slot, resources);
|
||||
CollectionImpl::appendOne(slot);
|
||||
return slot.ptr();
|
||||
}
|
||||
|
||||
inline VariantData* ArrayData::getOrAddElement(size_t index,
|
||||
ResourceManager* resources) {
|
||||
auto it = createIterator(resources);
|
||||
inline VariantData* ArrayImpl::getOrAddElement(size_t index) {
|
||||
auto it = createIterator();
|
||||
while (!it.done() && index > 0) {
|
||||
it.next(resources);
|
||||
it.next(resources_);
|
||||
index--;
|
||||
}
|
||||
if (it.done())
|
||||
index++;
|
||||
VariantData* element = it.data();
|
||||
while (index > 0) {
|
||||
element = addElement(resources);
|
||||
element = addElement();
|
||||
if (!element)
|
||||
return nullptr;
|
||||
index--;
|
||||
@ -47,33 +48,34 @@ 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* ArrayImpl::getElement(size_t index) const {
|
||||
return at(index).data();
|
||||
}
|
||||
|
||||
inline void ArrayData::removeElement(size_t index, ResourceManager* resources) {
|
||||
remove(at(index, resources), resources);
|
||||
inline void ArrayImpl::removeElement(size_t index) {
|
||||
remove(at(index));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline bool ArrayData::addValue(const T& value, ResourceManager* resources) {
|
||||
ARDUINOJSON_ASSERT(resources != nullptr);
|
||||
auto slot = resources->allocVariant();
|
||||
inline bool ArrayImpl::addValue(const T& value) {
|
||||
if (!data_)
|
||||
return false;
|
||||
ARDUINOJSON_ASSERT(resources_ != nullptr);
|
||||
auto slot = resources_->allocVariant();
|
||||
if (!slot)
|
||||
return false;
|
||||
JsonVariant variant(slot.ptr(), resources);
|
||||
JsonVariant variant(slot.ptr(), resources_);
|
||||
if (!variant.set(value)) {
|
||||
resources->freeVariant(slot);
|
||||
resources_->freeVariant(slot);
|
||||
return false;
|
||||
}
|
||||
CollectionData::appendOne(slot, resources);
|
||||
CollectionImpl::appendOne(slot);
|
||||
return true;
|
||||
}
|
||||
|
||||
// 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,12 @@ 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_));
|
||||
return VariantAttorney::getOrCreateVariantImpl(upstream_).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_(detail::VariantImpl(data, resources).asArray()) {}
|
||||
|
||||
// INTERNAL USE ONLY
|
||||
JsonArray(const detail::ArrayImpl& 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,14 @@ 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_.addElement(), impl_.getResourceManager());
|
||||
}
|
||||
|
||||
// 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_);
|
||||
return impl_.addValue(value);
|
||||
}
|
||||
|
||||
// Appends a value to the array.
|
||||
@ -71,15 +71,13 @@ 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_);
|
||||
return impl_.addValue(value);
|
||||
}
|
||||
|
||||
// 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_.getResourceManager());
|
||||
}
|
||||
|
||||
// Returns an iterator following the last element of the array.
|
||||
@ -91,7 +89,7 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
|
||||
// Copies an array.
|
||||
// https://arduinojson.org/v7/api/jsonarray/set/
|
||||
bool set(JsonArrayConst src) const {
|
||||
if (!data_)
|
||||
if (isNull())
|
||||
return false;
|
||||
|
||||
clear();
|
||||
@ -106,13 +104,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_.remove(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 +125,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_.clear();
|
||||
}
|
||||
|
||||
// Gets or sets the element at the specified index.
|
||||
@ -150,31 +148,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 +199,18 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
|
||||
|
||||
private:
|
||||
detail::ResourceManager* getResourceManager() const {
|
||||
return resources_;
|
||||
return impl_.getResourceManager();
|
||||
}
|
||||
|
||||
detail::VariantData* getData() const {
|
||||
return collectionToVariant(data_);
|
||||
return impl_.getData();
|
||||
}
|
||||
|
||||
detail::VariantData* getOrCreateData() const {
|
||||
return collectionToVariant(data_);
|
||||
return impl_.getData();
|
||||
}
|
||||
|
||||
detail::ArrayData* data_;
|
||||
detail::ResourceManager* resources_;
|
||||
mutable detail::ArrayImpl 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_.getResourceManager());
|
||||
}
|
||||
|
||||
// Returns an iterator to the element following the last element of the array.
|
||||
@ -36,21 +34,22 @@ class JsonArrayConst : public detail::VariantOperators<JsonArrayConst> {
|
||||
}
|
||||
|
||||
// Creates an unbound reference.
|
||||
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_(detail::VariantImpl(data, resources).asArray()) {}
|
||||
|
||||
// INTERNAL USE ONLY
|
||||
JsonArrayConst(const detail::ArrayImpl& 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_.getResourceManager());
|
||||
}
|
||||
|
||||
// Returns the element at the specified index.
|
||||
@ -65,31 +64,31 @@ class JsonArrayConst : public detail::VariantOperators<JsonArrayConst> {
|
||||
}
|
||||
|
||||
operator JsonVariantConst() const {
|
||||
return JsonVariantConst(getData(), resources_);
|
||||
return JsonVariantConst(impl_.getData(), impl_.getResourceManager());
|
||||
}
|
||||
|
||||
// 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 +99,10 @@ class JsonArrayConst : public detail::VariantOperators<JsonArrayConst> {
|
||||
|
||||
private:
|
||||
const detail::VariantData* getData() const {
|
||||
return collectionToVariant(data_);
|
||||
return impl_.getData();
|
||||
}
|
||||
|
||||
const detail::ArrayData* data_;
|
||||
const detail::ResourceManager* resources_;
|
||||
detail::ArrayImpl impl_;
|
||||
};
|
||||
|
||||
// Compares the content of two arrays.
|
||||
|
@ -30,7 +30,7 @@ class JsonArrayIterator {
|
||||
|
||||
public:
|
||||
JsonArrayIterator() {}
|
||||
explicit JsonArrayIterator(detail::ArrayData::iterator iterator,
|
||||
explicit JsonArrayIterator(detail::ArrayImpl::iterator iterator,
|
||||
detail::ResourceManager* resources)
|
||||
: iterator_(iterator), resources_(resources) {}
|
||||
|
||||
@ -55,7 +55,7 @@ class JsonArrayIterator {
|
||||
}
|
||||
|
||||
private:
|
||||
detail::ArrayData::iterator iterator_;
|
||||
detail::ArrayImpl::iterator 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::ArrayImpl::iterator iterator,
|
||||
detail::ResourceManager* resources)
|
||||
: iterator_(iterator), resources_(resources) {}
|
||||
|
||||
JsonVariantConst operator*() const {
|
||||
@ -89,8 +89,8 @@ class JsonArrayConstIterator {
|
||||
}
|
||||
|
||||
private:
|
||||
detail::ArrayData::iterator iterator_;
|
||||
const detail::ResourceManager* resources_;
|
||||
mutable detail::ArrayImpl::iterator iterator_;
|
||||
mutable detail::ResourceManager* resources_;
|
||||
};
|
||||
|
||||
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
||||
|
@ -12,11 +12,11 @@
|
||||
|
||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
|
||||
class VariantData;
|
||||
struct VariantData;
|
||||
class ResourceManager;
|
||||
|
||||
class CollectionIterator {
|
||||
friend class CollectionData;
|
||||
friend class CollectionImpl;
|
||||
|
||||
public:
|
||||
CollectionIterator() : slot_(nullptr), currentId_(NULL_SLOT) {}
|
||||
@ -51,11 +51,11 @@ class CollectionIterator {
|
||||
}
|
||||
|
||||
VariantData* data() {
|
||||
return reinterpret_cast<VariantData*>(slot_);
|
||||
return slot_;
|
||||
}
|
||||
|
||||
const VariantData* data() const {
|
||||
return reinterpret_cast<const VariantData*>(slot_);
|
||||
return slot_;
|
||||
}
|
||||
|
||||
private:
|
||||
@ -65,58 +65,68 @@ class CollectionIterator {
|
||||
SlotId currentId_, nextId_;
|
||||
};
|
||||
|
||||
class CollectionData {
|
||||
SlotId head_ = NULL_SLOT;
|
||||
SlotId tail_ = NULL_SLOT;
|
||||
struct 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 {}
|
||||
};
|
||||
|
||||
class CollectionImpl {
|
||||
protected:
|
||||
CollectionData* data_;
|
||||
ResourceManager* resources_;
|
||||
|
||||
public:
|
||||
using iterator = CollectionIterator;
|
||||
|
||||
iterator createIterator(const ResourceManager* resources) const;
|
||||
CollectionImpl() : data_(nullptr), resources_(nullptr) {}
|
||||
|
||||
size_t size(const ResourceManager*) const;
|
||||
size_t nesting(const ResourceManager*) const;
|
||||
CollectionImpl(CollectionData* data, ResourceManager* resources)
|
||||
: data_(data), resources_(resources) {}
|
||||
|
||||
void clear(ResourceManager* resources);
|
||||
|
||||
static void clear(CollectionData* collection, ResourceManager* resources) {
|
||||
if (!collection)
|
||||
return;
|
||||
collection->clear(resources);
|
||||
explicit operator bool() const {
|
||||
return data_ != nullptr;
|
||||
}
|
||||
|
||||
bool isNull() const {
|
||||
return data_ == nullptr;
|
||||
}
|
||||
|
||||
VariantData* getData() const {
|
||||
void* data = data_; // prevent warning cast-align
|
||||
return reinterpret_cast<VariantData*>(data);
|
||||
}
|
||||
|
||||
ResourceManager* getResourceManager() const {
|
||||
return resources_;
|
||||
}
|
||||
|
||||
iterator createIterator() const;
|
||||
|
||||
size_t size() const;
|
||||
size_t nesting() const;
|
||||
|
||||
void clear();
|
||||
|
||||
SlotId head() const {
|
||||
return head_;
|
||||
return data_->head;
|
||||
}
|
||||
|
||||
protected:
|
||||
void appendOne(Slot<VariantData> slot, const ResourceManager* resources);
|
||||
void appendPair(Slot<VariantData> key, Slot<VariantData> value,
|
||||
const ResourceManager* resources);
|
||||
void appendOne(Slot<VariantData> slot);
|
||||
void appendPair(Slot<VariantData> key, Slot<VariantData> value);
|
||||
|
||||
void removeOne(iterator it, ResourceManager* resources);
|
||||
void removePair(iterator it, ResourceManager* resources);
|
||||
void removeOne(iterator it);
|
||||
void removePair(iterator it);
|
||||
|
||||
private:
|
||||
Slot<VariantData> getPreviousSlot(VariantData*, const ResourceManager*) const;
|
||||
Slot<VariantData> getPreviousSlot(VariantData*) const;
|
||||
};
|
||||
|
||||
inline const VariantData* collectionToVariant(
|
||||
const CollectionData* collection) {
|
||||
const void* data = collection; // prevent warning cast-align
|
||||
return reinterpret_cast<const VariantData*>(data);
|
||||
}
|
||||
|
||||
inline VariantData* collectionToVariant(CollectionData* collection) {
|
||||
void* data = collection; // prevent warning cast-align
|
||||
return reinterpret_cast<VariantData*>(data);
|
||||
}
|
||||
|
||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||
|
@ -14,7 +14,7 @@ ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
|
||||
inline CollectionIterator::CollectionIterator(VariantData* slot, SlotId slotId)
|
||||
: slot_(slot), currentId_(slotId) {
|
||||
nextId_ = slot_ ? slot_->next() : NULL_SLOT;
|
||||
nextId_ = slot_ ? slot_->next : NULL_SLOT;
|
||||
}
|
||||
|
||||
inline void CollectionIterator::next(const ResourceManager* resources) {
|
||||
@ -22,114 +22,123 @@ inline void CollectionIterator::next(const ResourceManager* resources) {
|
||||
slot_ = resources->getVariant(nextId_);
|
||||
currentId_ = nextId_;
|
||||
if (slot_)
|
||||
nextId_ = slot_->next();
|
||||
nextId_ = slot_->next;
|
||||
}
|
||||
|
||||
inline CollectionData::iterator CollectionData::createIterator(
|
||||
const ResourceManager* resources) const {
|
||||
return iterator(resources->getVariant(head_), head_);
|
||||
inline CollectionImpl::iterator CollectionImpl::createIterator() const {
|
||||
if (!data_)
|
||||
return iterator();
|
||||
return iterator(resources_->getVariant(data_->head), data_->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 CollectionImpl::appendOne(Slot<VariantData> slot) {
|
||||
ARDUINOJSON_ASSERT(data_ != nullptr);
|
||||
ARDUINOJSON_ASSERT(resources_ != nullptr);
|
||||
|
||||
if (data_->tail != NULL_SLOT) {
|
||||
auto tail = resources_->getVariant(data_->tail);
|
||||
tail->next = slot.id();
|
||||
data_->tail = slot.id();
|
||||
} else {
|
||||
head_ = slot.id();
|
||||
tail_ = slot.id();
|
||||
data_->head = slot.id();
|
||||
data_->tail = slot.id();
|
||||
}
|
||||
}
|
||||
|
||||
inline void CollectionData::appendPair(Slot<VariantData> key,
|
||||
Slot<VariantData> value,
|
||||
const ResourceManager* resources) {
|
||||
key->setNext(value.id());
|
||||
inline void CollectionImpl::appendPair(Slot<VariantData> key,
|
||||
Slot<VariantData> value) {
|
||||
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();
|
||||
|
||||
if (data_->tail != NULL_SLOT) {
|
||||
auto tail = resources_->getVariant(data_->tail);
|
||||
tail->next = key.id();
|
||||
data_->tail = value.id();
|
||||
} else {
|
||||
head_ = key.id();
|
||||
tail_ = value.id();
|
||||
data_->head = key.id();
|
||||
data_->tail = value.id();
|
||||
}
|
||||
}
|
||||
|
||||
inline void CollectionData::clear(ResourceManager* resources) {
|
||||
auto next = head_;
|
||||
inline void CollectionImpl::clear() {
|
||||
if (!data_)
|
||||
return;
|
||||
auto next = data_->head;
|
||||
while (next != NULL_SLOT) {
|
||||
auto currId = next;
|
||||
auto slot = resources->getVariant(next);
|
||||
next = slot->next();
|
||||
resources->freeVariant({slot, currId});
|
||||
auto slot = resources_->getVariant(next);
|
||||
next = slot->next;
|
||||
resources_->freeVariant({slot, currId});
|
||||
}
|
||||
|
||||
head_ = NULL_SLOT;
|
||||
tail_ = NULL_SLOT;
|
||||
data_->head = NULL_SLOT;
|
||||
data_->tail = NULL_SLOT;
|
||||
}
|
||||
|
||||
inline Slot<VariantData> CollectionData::getPreviousSlot(
|
||||
VariantData* target, const ResourceManager* resources) const {
|
||||
inline Slot<VariantData> CollectionImpl::getPreviousSlot(
|
||||
VariantData* target) const {
|
||||
auto prev = Slot<VariantData>();
|
||||
auto currentId = head_;
|
||||
auto currentId = data_->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 CollectionImpl::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;
|
||||
if (prev)
|
||||
prev->setNext(next);
|
||||
prev->next = next;
|
||||
else
|
||||
head_ = next;
|
||||
data_->head = next;
|
||||
if (next == NULL_SLOT)
|
||||
tail_ = prev.id();
|
||||
resources->freeVariant({it.slot_, it.currentId_});
|
||||
data_->tail = prev.id();
|
||||
resources_->freeVariant({it.slot_, it.currentId_});
|
||||
}
|
||||
|
||||
inline void CollectionData::removePair(ObjectData::iterator it,
|
||||
ResourceManager* resources) {
|
||||
inline void CollectionImpl::removePair(ObjectImpl::iterator it) {
|
||||
if (it.done())
|
||||
return;
|
||||
|
||||
auto keySlot = it.slot_;
|
||||
|
||||
auto valueId = it.nextId_;
|
||||
auto valueSlot = resources->getVariant(valueId);
|
||||
auto valueSlot = resources_->getVariant(valueId);
|
||||
|
||||
// remove value slot
|
||||
keySlot->setNext(valueSlot->next());
|
||||
resources->freeVariant({valueSlot, valueId});
|
||||
keySlot->next = valueSlot->next;
|
||||
resources_->freeVariant({valueSlot, valueId});
|
||||
|
||||
// remove key slot
|
||||
removeOne(it, resources);
|
||||
removeOne(it);
|
||||
}
|
||||
|
||||
inline size_t CollectionData::nesting(const ResourceManager* resources) const {
|
||||
inline size_t CollectionImpl::nesting() const {
|
||||
if (!data_)
|
||||
return 0;
|
||||
size_t maxChildNesting = 0;
|
||||
for (auto it = createIterator(resources); !it.done(); it.next(resources)) {
|
||||
size_t childNesting = it->nesting(resources);
|
||||
for (auto it = createIterator(); !it.done(); it.next(resources_)) {
|
||||
VariantImpl variant(it.data(), resources_);
|
||||
size_t childNesting = variant.nesting();
|
||||
if (childNesting > maxChildNesting)
|
||||
maxChildNesting = childNesting;
|
||||
}
|
||||
return maxChildNesting + 1;
|
||||
}
|
||||
|
||||
inline size_t CollectionData::size(const ResourceManager* resources) const {
|
||||
inline size_t CollectionImpl::size() const {
|
||||
size_t count = 0;
|
||||
for (auto it = createIterator(resources); !it.done(); it.next(resources))
|
||||
for (auto it = createIterator(); !it.done(); it.next(resources_))
|
||||
count++;
|
||||
return count;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -88,7 +88,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 +120,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.
|
||||
@ -165,7 +165,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 +174,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
|
||||
@ -212,7 +212,7 @@ 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.
|
||||
@ -223,7 +223,7 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
||||
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 +237,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,14 +267,14 @@ 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 JsonVariant(getVariantImpl().addElement(), &resources_);
|
||||
}
|
||||
|
||||
// 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 getVariantImpl().addValue(value);
|
||||
}
|
||||
|
||||
// Appends a value to the root array.
|
||||
@ -282,7 +282,7 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
||||
template <typename TChar,
|
||||
detail::enable_if_t<!detail::is_const<TChar>::value, int> = 0>
|
||||
bool add(TChar* value) {
|
||||
return data_.addValue(value, &resources_);
|
||||
return getVariantImpl().addValue(value);
|
||||
}
|
||||
|
||||
// Removes an element of the root array.
|
||||
@ -290,8 +290,7 @@ 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.
|
||||
@ -301,8 +300,7 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
||||
!detail::is_const<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 +308,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 +388,10 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
||||
}
|
||||
|
||||
private:
|
||||
detail::VariantImpl getVariantImpl() const {
|
||||
return detail::VariantImpl(&data_, &resources_);
|
||||
}
|
||||
|
||||
JsonVariant getVariant() {
|
||||
return JsonVariant(&data_, &resources_);
|
||||
}
|
||||
@ -415,8 +416,8 @@ 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) {
|
||||
|
@ -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,15 @@ class JsonDeserializer {
|
||||
switch (current()) {
|
||||
case '[':
|
||||
if (filter.allowArray())
|
||||
return parseArray(variant.toArray(), filter, nestingLimit);
|
||||
return parseArray(VariantImpl(variant, resources_).toArray(), filter,
|
||||
nestingLimit);
|
||||
else
|
||||
return skipArray(nestingLimit);
|
||||
|
||||
case '{':
|
||||
if (filter.allowObject())
|
||||
return parseObject(variant.toObject(), filter, nestingLimit);
|
||||
return parseObject(VariantImpl(variant, resources_).toObject(),
|
||||
filter, nestingLimit);
|
||||
else
|
||||
return skipObject(nestingLimit);
|
||||
|
||||
@ -90,12 +92,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,7 +148,7 @@ class JsonDeserializer {
|
||||
|
||||
template <typename TFilter>
|
||||
DeserializationError::Code parseArray(
|
||||
ArrayData& array, TFilter filter,
|
||||
ArrayImpl array, TFilter filter,
|
||||
DeserializationOption::NestingLimit nestingLimit) {
|
||||
DeserializationError::Code err;
|
||||
|
||||
@ -172,12 +174,12 @@ class JsonDeserializer {
|
||||
for (;;) {
|
||||
if (elementFilter.allow()) {
|
||||
// Allocate slot in array
|
||||
VariantData* value = array.addElement(resources_);
|
||||
VariantData* value = array.addElement();
|
||||
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,7 +234,7 @@ class JsonDeserializer {
|
||||
|
||||
template <typename TFilter>
|
||||
DeserializationError::Code parseObject(
|
||||
ObjectData& object, TFilter filter,
|
||||
ObjectImpl object, TFilter filter,
|
||||
DeserializationOption::NestingLimit nestingLimit) {
|
||||
DeserializationError::Code err;
|
||||
|
||||
@ -273,21 +275,19 @@ class JsonDeserializer {
|
||||
TFilter memberFilter = filter[key];
|
||||
|
||||
if (memberFilter.allow()) {
|
||||
auto member = object.getMember(adaptString(key), resources_);
|
||||
auto member = object.getMember(adaptString(key));
|
||||
if (!member) {
|
||||
// Save key in memory pool.
|
||||
auto savedKey = stringBuilder_.save();
|
||||
|
||||
// Allocate slot in object
|
||||
member = object.addMember(savedKey, resources_);
|
||||
if (!member)
|
||||
auto keyVariant = object.addPair(&member);
|
||||
if (!keyVariant)
|
||||
return DeserializationError::NoMemory;
|
||||
|
||||
stringBuilder_.save(keyVariant);
|
||||
} else {
|
||||
member->clear(resources_);
|
||||
VariantImpl(member, resources_).clear();
|
||||
}
|
||||
|
||||
// Parse value
|
||||
err = parseVariant(*member, memberFilter, nestingLimit.decrement());
|
||||
err = parseVariant(member, memberFilter, nestingLimit.decrement());
|
||||
if (err)
|
||||
return err;
|
||||
} else {
|
||||
@ -381,7 +381,7 @@ class JsonDeserializer {
|
||||
}
|
||||
}
|
||||
|
||||
DeserializationError::Code parseStringValue(VariantData& variant) {
|
||||
DeserializationError::Code parseStringValue(VariantData* variant) {
|
||||
DeserializationError::Code err;
|
||||
|
||||
stringBuilder_.startString();
|
||||
@ -390,7 +390,7 @@ class JsonDeserializer {
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
variant.setOwnedString(stringBuilder_.save());
|
||||
stringBuilder_.save(variant);
|
||||
|
||||
return DeserializationError::Ok;
|
||||
}
|
||||
@ -506,7 +506,7 @@ class JsonDeserializer {
|
||||
return DeserializationError::Ok;
|
||||
}
|
||||
|
||||
DeserializationError::Code parseNumericValue(VariantData& result) {
|
||||
DeserializationError::Code parseNumericValue(VariantData* result) {
|
||||
uint8_t n = 0;
|
||||
|
||||
char c = current();
|
||||
@ -520,26 +520,28 @@ class JsonDeserializer {
|
||||
auto number = parseNumber(buffer_);
|
||||
switch (number.type()) {
|
||||
case NumberType::UnsignedInteger:
|
||||
if (result.setInteger(number.asUnsignedInteger(), resources_))
|
||||
if (VariantImpl(result, resources_)
|
||||
.setInteger(number.asUnsignedInteger()))
|
||||
return DeserializationError::Ok;
|
||||
else
|
||||
return DeserializationError::NoMemory;
|
||||
|
||||
case NumberType::SignedInteger:
|
||||
if (result.setInteger(number.asSignedInteger(), resources_))
|
||||
if (VariantImpl(result, resources_)
|
||||
.setInteger(number.asSignedInteger()))
|
||||
return DeserializationError::Ok;
|
||||
else
|
||||
return DeserializationError::NoMemory;
|
||||
|
||||
case NumberType::Float:
|
||||
if (result.setFloat(number.asFloat(), resources_))
|
||||
if (VariantImpl(result, resources_).setFloat(number.asFloat()))
|
||||
return DeserializationError::Ok;
|
||||
else
|
||||
return DeserializationError::NoMemory;
|
||||
|
||||
#if ARDUINOJSON_USE_DOUBLE
|
||||
case NumberType::Double:
|
||||
if (result.setFloat(number.asDouble(), resources_))
|
||||
if (VariantImpl(result, resources_).setFloat(number.asDouble()))
|
||||
return DeserializationError::Ok;
|
||||
else
|
||||
return DeserializationError::NoMemory;
|
||||
|
@ -16,10 +16,10 @@ 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 visit(const ArrayImpl& array) {
|
||||
write('[');
|
||||
|
||||
auto slotId = array.head();
|
||||
@ -27,9 +27,9 @@ class JsonSerializer : public VariantDataVisitor<size_t> {
|
||||
while (slotId != NULL_SLOT) {
|
||||
auto slot = resources_->getVariant(slotId);
|
||||
|
||||
slot->accept(*this, resources_);
|
||||
VariantImpl(slot, resources_).accept(*this);
|
||||
|
||||
slotId = slot->next();
|
||||
slotId = slot->next;
|
||||
|
||||
if (slotId != NULL_SLOT)
|
||||
write(',');
|
||||
@ -39,7 +39,7 @@ class JsonSerializer : public VariantDataVisitor<size_t> {
|
||||
return bytesWritten();
|
||||
}
|
||||
|
||||
size_t visit(const ObjectData& object) {
|
||||
size_t visit(const ObjectImpl& object) {
|
||||
write('{');
|
||||
|
||||
auto slotId = object.head();
|
||||
@ -48,9 +48,9 @@ class JsonSerializer : public VariantDataVisitor<size_t> {
|
||||
|
||||
while (slotId != NULL_SLOT) {
|
||||
auto slot = resources_->getVariant(slotId);
|
||||
slot->accept(*this, resources_);
|
||||
VariantImpl(slot, resources_).accept(*this);
|
||||
|
||||
slotId = slot->next();
|
||||
slotId = slot->next;
|
||||
|
||||
if (slotId != NULL_SLOT)
|
||||
write(isKey ? ':' : ',');
|
||||
@ -120,7 +120,7 @@ class JsonSerializer : public VariantDataVisitor<size_t> {
|
||||
TextFormatter<TWriter> formatter_;
|
||||
|
||||
protected:
|
||||
const ResourceManager* resources_;
|
||||
ResourceManager* resources_;
|
||||
};
|
||||
|
||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||
|
@ -16,17 +16,17 @@ 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_);
|
||||
size_t visit(const ArrayImpl& array) {
|
||||
auto it = array.createIterator();
|
||||
if (!it.done()) {
|
||||
base::write("[\r\n");
|
||||
nesting_++;
|
||||
while (!it.done()) {
|
||||
indent();
|
||||
it->accept(*this, base::resources_);
|
||||
VariantImpl(it.data(), base::resources_).accept(*this);
|
||||
|
||||
it.next(base::resources_);
|
||||
base::write(it.done() ? "\r\n" : ",\r\n");
|
||||
@ -40,8 +40,8 @@ class PrettyJsonSerializer : public JsonSerializer<TWriter> {
|
||||
return this->bytesWritten();
|
||||
}
|
||||
|
||||
size_t visit(const ObjectData& object) {
|
||||
auto it = object.createIterator(base::resources_);
|
||||
size_t visit(const ObjectImpl& object) {
|
||||
auto it = object.createIterator();
|
||||
if (!it.done()) {
|
||||
base::write("{\r\n");
|
||||
nesting_++;
|
||||
@ -49,7 +49,7 @@ class PrettyJsonSerializer : public JsonSerializer<TWriter> {
|
||||
while (!it.done()) {
|
||||
if (isKey)
|
||||
indent();
|
||||
it->accept(*this, base::resources_);
|
||||
VariantImpl(it.data(), base::resources_).accept(*this);
|
||||
it.next(base::resources_);
|
||||
if (isKey)
|
||||
base::write(": ");
|
||||
|
@ -34,6 +34,11 @@ class Slot {
|
||||
return ptr_;
|
||||
}
|
||||
|
||||
T& operator*() const {
|
||||
ARDUINOJSON_ASSERT(ptr_ != nullptr);
|
||||
return *ptr_;
|
||||
}
|
||||
|
||||
T* operator->() const {
|
||||
ARDUINOJSON_ASSERT(ptr_ != nullptr);
|
||||
return ptr_;
|
||||
@ -76,6 +81,14 @@ class MemoryPool {
|
||||
return slots_ + id;
|
||||
}
|
||||
|
||||
SlotId find(const T& value) const {
|
||||
for (SlotId i = 0; i < usage_; i++) {
|
||||
if (slots_[i] == value)
|
||||
return i;
|
||||
}
|
||||
return NULL_SLOT;
|
||||
}
|
||||
|
||||
void clear() {
|
||||
usage_ = 0;
|
||||
}
|
||||
|
@ -114,6 +114,15 @@ class MemoryPoolList {
|
||||
return pools_[poolIndex].getSlot(indexInPool);
|
||||
}
|
||||
|
||||
SlotId find(const T& value) const {
|
||||
for (PoolCount i = 0; i < count_; i++) {
|
||||
SlotId id = pools_[i].find(value);
|
||||
if (id != NULL_SLOT)
|
||||
return SlotId(i * ARDUINOJSON_POOL_CAPACITY + id);
|
||||
}
|
||||
return NULL_SLOT;
|
||||
}
|
||||
|
||||
void clear(Allocator* allocator) {
|
||||
for (PoolCount i = 0; i < count_; i++)
|
||||
pools_[i].destroy(allocator);
|
||||
|
@ -14,26 +14,21 @@
|
||||
|
||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
|
||||
class VariantData;
|
||||
struct 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_);
|
||||
staticStringsPools_.clear(allocator_);
|
||||
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||
eightBytePools_.clear(allocator_);
|
||||
#endif
|
||||
}
|
||||
|
||||
ResourceManager(const ResourceManager&) = delete;
|
||||
@ -42,6 +37,10 @@ class ResourceManager {
|
||||
friend void swap(ResourceManager& a, ResourceManager& b) {
|
||||
swap(a.stringPool_, b.stringPool_);
|
||||
swap(a.variantPools_, b.variantPools_);
|
||||
swap(a.staticStringsPools_, b.staticStringsPools_);
|
||||
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||
swap(a.eightBytePools_, b.eightBytePools_);
|
||||
#endif
|
||||
swap_(a.allocator_, b.allocator_);
|
||||
swap_(a.overflowed_, b.overflowed_);
|
||||
}
|
||||
@ -62,10 +61,10 @@ class ResourceManager {
|
||||
void freeVariant(Slot<VariantData> slot);
|
||||
VariantData* getVariant(SlotId id) const;
|
||||
|
||||
#if ARDUINOJSON_USE_EXTENSIONS
|
||||
Slot<VariantExtension> allocExtension();
|
||||
void freeExtension(SlotId slot);
|
||||
VariantExtension* getExtension(SlotId id) const;
|
||||
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||
Slot<EightByteValue> allocEightByte();
|
||||
void freeEightByte(SlotId slot);
|
||||
EightByteValue* getEightByte(SlotId id) const;
|
||||
#endif
|
||||
|
||||
template <typename TAdaptedString>
|
||||
@ -111,21 +110,51 @@ class ResourceManager {
|
||||
stringPool_.dereference(s, allocator_);
|
||||
}
|
||||
|
||||
SlotId saveStaticString(const char* s) {
|
||||
auto existingSlotId = staticStringsPools_.find(s);
|
||||
if (existingSlotId != NULL_SLOT)
|
||||
return existingSlotId;
|
||||
|
||||
auto slot = staticStringsPools_.allocSlot(allocator_);
|
||||
if (slot)
|
||||
*slot = s;
|
||||
else
|
||||
overflowed_ = true;
|
||||
|
||||
return slot.id();
|
||||
}
|
||||
|
||||
const char* getStaticString(SlotId id) const {
|
||||
return *staticStringsPools_.getSlot(id);
|
||||
}
|
||||
|
||||
void clear() {
|
||||
variantPools_.clear(allocator_);
|
||||
overflowed_ = false;
|
||||
variantPools_.clear(allocator_);
|
||||
stringPool_.clear(allocator_);
|
||||
staticStringsPools_.clear(allocator_);
|
||||
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||
eightBytePools_.clear(allocator_);
|
||||
#endif
|
||||
}
|
||||
|
||||
void shrinkToFit() {
|
||||
variantPools_.shrinkToFit(allocator_);
|
||||
staticStringsPools_.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_;
|
||||
MemoryPoolList<const char*> staticStringsPools_;
|
||||
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||
MemoryPoolList<EightByteValue> eightBytePools_;
|
||||
#endif
|
||||
};
|
||||
|
||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||
|
@ -4,48 +4,48 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson/Collection/CollectionData.hpp>
|
||||
#include <ArduinoJson/Memory/ResourceManager.hpp>
|
||||
#include <ArduinoJson/Polyfills/alias_cast.hpp>
|
||||
#include <ArduinoJson/Variant/VariantData.hpp>
|
||||
#include <ArduinoJson/Variant/VariantImpl.hpp>
|
||||
|
||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
|
||||
inline Slot<VariantData> ResourceManager::allocVariant() {
|
||||
auto p = variantPools_.allocSlot(allocator_);
|
||||
if (!p) {
|
||||
auto slot = variantPools_.allocSlot(allocator_);
|
||||
if (!slot) {
|
||||
overflowed_ = true;
|
||||
return {};
|
||||
}
|
||||
return {new (&p->variant) VariantData, p.id()};
|
||||
new (slot.ptr()) VariantData();
|
||||
return slot;
|
||||
}
|
||||
|
||||
inline void ResourceManager::freeVariant(Slot<VariantData> variant) {
|
||||
variant->clear(this);
|
||||
variantPools_.freeSlot({alias_cast<SlotData*>(variant.ptr()), variant.id()});
|
||||
inline void ResourceManager::freeVariant(Slot<VariantData> slot) {
|
||||
VariantImpl(slot.ptr(), this).clear();
|
||||
variantPools_.freeSlot(slot);
|
||||
}
|
||||
|
||||
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) {
|
||||
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||
inline Slot<EightByteValue> ResourceManager::allocEightByte() {
|
||||
auto slot = eightBytePools_.allocSlot(allocator_);
|
||||
if (!slot) {
|
||||
overflowed_ = true;
|
||||
return {};
|
||||
}
|
||||
return {&p->extension, p.id()};
|
||||
return slot;
|
||||
}
|
||||
|
||||
inline void ResourceManager::freeExtension(SlotId id) {
|
||||
auto p = getExtension(id);
|
||||
variantPools_.freeSlot({reinterpret_cast<SlotData*>(p), id});
|
||||
inline void ResourceManager::freeEightByte(SlotId id) {
|
||||
auto p = getEightByte(id);
|
||||
eightBytePools_.freeSlot({p, id});
|
||||
}
|
||||
|
||||
inline VariantExtension* ResourceManager::getExtension(SlotId id) const {
|
||||
return &variantPools_.getSlot(id)->extension;
|
||||
inline EightByteValue* ResourceManager::getEightByte(SlotId id) const {
|
||||
return eightBytePools_.getSlot(id);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -32,7 +32,26 @@ class StringBuffer {
|
||||
return node_->data;
|
||||
}
|
||||
|
||||
StringNode* save() {
|
||||
JsonString str() const {
|
||||
ARDUINOJSON_ASSERT(node_ != nullptr);
|
||||
return JsonString(node_->data, node_->length);
|
||||
}
|
||||
|
||||
void save(VariantData* data) {
|
||||
ARDUINOJSON_ASSERT(node_ != nullptr);
|
||||
const char* s = node_->data;
|
||||
if (isTinyString(s, size_))
|
||||
data->setTinyString(adaptString(s, size_));
|
||||
else
|
||||
data->setOwnedString(commitStringNode());
|
||||
}
|
||||
|
||||
void saveRaw(VariantData* data) {
|
||||
data->setRawString(commitStringNode());
|
||||
}
|
||||
|
||||
private:
|
||||
StringNode* commitStringNode() {
|
||||
ARDUINOJSON_ASSERT(node_ != nullptr);
|
||||
node_->data[size_] = 0;
|
||||
auto node = resources_->getString(adaptString(node_->data, size_));
|
||||
@ -52,13 +71,6 @@ class StringBuffer {
|
||||
return node;
|
||||
}
|
||||
|
||||
JsonString str() const {
|
||||
ARDUINOJSON_ASSERT(node_ != nullptr);
|
||||
|
||||
return JsonString(node_->data, node_->length);
|
||||
}
|
||||
|
||||
private:
|
||||
ResourceManager* resources_;
|
||||
StringNode* node_ = nullptr;
|
||||
size_t size_ = 0;
|
||||
|
@ -5,6 +5,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson/Memory/ResourceManager.hpp>
|
||||
#include <ArduinoJson/Strings/JsonString.hpp>
|
||||
|
||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
|
||||
@ -25,10 +26,18 @@ class StringBuilder {
|
||||
node_ = resources_->createString(initialCapacity);
|
||||
}
|
||||
|
||||
StringNode* save() {
|
||||
void save(VariantData* variant) {
|
||||
ARDUINOJSON_ASSERT(variant != nullptr);
|
||||
ARDUINOJSON_ASSERT(node_ != nullptr);
|
||||
node_->data[size_] = 0;
|
||||
StringNode* node = resources_->getString(adaptString(node_->data, size_));
|
||||
|
||||
char* p = node_->data;
|
||||
if (isTinyString(p, size_)) {
|
||||
variant->setTinyString(adaptString(p, size_));
|
||||
return;
|
||||
}
|
||||
|
||||
p[size_] = 0;
|
||||
StringNode* node = resources_->getString(adaptString(p, size_));
|
||||
if (!node) {
|
||||
node = resources_->resizeString(node_, size_);
|
||||
ARDUINOJSON_ASSERT(node != nullptr); // realloc to smaller can't fail
|
||||
@ -37,7 +46,7 @@ class StringBuilder {
|
||||
} else {
|
||||
node->references++;
|
||||
}
|
||||
return node;
|
||||
variant->setOwnedString(node);
|
||||
}
|
||||
|
||||
void append(const char* s) {
|
||||
|
@ -29,7 +29,7 @@ struct Converter<MsgPackBinary> : private detail::VariantAttorney {
|
||||
if (!data)
|
||||
return;
|
||||
auto resources = getResourceManager(dst);
|
||||
data->clear(resources);
|
||||
detail::VariantImpl(data, resources).clear();
|
||||
if (src.data()) {
|
||||
size_t headerSize = src.size() >= 0x10000 ? 5
|
||||
: src.size() >= 0x100 ? 3
|
||||
@ -66,10 +66,8 @@ struct Converter<MsgPackBinary> : private detail::VariantAttorney {
|
||||
}
|
||||
|
||||
static MsgPackBinary fromJson(JsonVariantConst src) {
|
||||
auto data = getData(src);
|
||||
if (!data)
|
||||
return {};
|
||||
auto rawstr = data->asRawString();
|
||||
auto variant = VariantAttorney::getVariantImpl(src);
|
||||
auto rawstr = variant.asRawString();
|
||||
auto p = reinterpret_cast<const uint8_t*>(rawstr.c_str());
|
||||
auto n = rawstr.size();
|
||||
if (n >= 2 && p[0] == 0xc4) { // bin 8
|
||||
|
@ -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(variant, resources_).setInteger(static_cast<int8_t>(code));
|
||||
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(variant, resources_).setInteger(truncatedValue))
|
||||
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(variant, resources_).setInteger(truncatedValue))
|
||||
return DeserializationError::NoMemory;
|
||||
// else set null on overflow
|
||||
}
|
||||
@ -257,7 +257,7 @@ class MsgPackDeserializer {
|
||||
return err;
|
||||
|
||||
fixEndianness(value);
|
||||
variant->setFloat(value, resources_);
|
||||
VariantImpl(variant, resources_).setFloat(value);
|
||||
|
||||
return DeserializationError::Ok;
|
||||
}
|
||||
@ -273,7 +273,7 @@ class MsgPackDeserializer {
|
||||
return err;
|
||||
|
||||
fixEndianness(value);
|
||||
if (variant->setFloat(value, resources_))
|
||||
if (VariantImpl(variant, resources_).setFloat(value))
|
||||
return DeserializationError::Ok;
|
||||
else
|
||||
return DeserializationError::NoMemory;
|
||||
@ -293,7 +293,7 @@ class MsgPackDeserializer {
|
||||
|
||||
doubleToFloat(i, o);
|
||||
fixEndianness(value);
|
||||
variant->setFloat(value, resources_);
|
||||
VariantImpl(variant, resources_).setFloat(value);
|
||||
|
||||
return DeserializationError::Ok;
|
||||
}
|
||||
@ -305,7 +305,7 @@ class MsgPackDeserializer {
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
variant->setOwnedString(stringBuffer_.save());
|
||||
stringBuffer_.save(variant);
|
||||
return DeserializationError::Ok;
|
||||
}
|
||||
|
||||
@ -334,7 +334,7 @@ class MsgPackDeserializer {
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
variant->setRawString(stringBuffer_.save());
|
||||
stringBuffer_.saveRaw(variant);
|
||||
return DeserializationError::Ok;
|
||||
}
|
||||
|
||||
@ -349,12 +349,10 @@ class MsgPackDeserializer {
|
||||
|
||||
bool allowArray = filter.allowArray();
|
||||
|
||||
ArrayData* array;
|
||||
ArrayImpl array;
|
||||
if (allowArray) {
|
||||
ARDUINOJSON_ASSERT(variant != 0);
|
||||
array = &variant->toArray();
|
||||
} else {
|
||||
array = 0;
|
||||
array = VariantImpl(variant, resources_).toArray();
|
||||
}
|
||||
|
||||
TFilter elementFilter = filter[0U];
|
||||
@ -363,8 +361,7 @@ class MsgPackDeserializer {
|
||||
VariantData* value;
|
||||
|
||||
if (elementFilter.allow()) {
|
||||
ARDUINOJSON_ASSERT(array != 0);
|
||||
value = array->addElement(resources_);
|
||||
value = array.addElement();
|
||||
if (!value)
|
||||
return DeserializationError::NoMemory;
|
||||
} else {
|
||||
@ -388,12 +385,10 @@ class MsgPackDeserializer {
|
||||
if (nestingLimit.reached())
|
||||
return DeserializationError::TooDeep;
|
||||
|
||||
ObjectData* object;
|
||||
ObjectImpl object;
|
||||
if (filter.allowObject()) {
|
||||
ARDUINOJSON_ASSERT(variant != 0);
|
||||
object = &variant->toObject();
|
||||
} else {
|
||||
object = 0;
|
||||
object = VariantImpl(variant, resources_).toObject();
|
||||
}
|
||||
|
||||
for (; n; --n) {
|
||||
@ -403,19 +398,14 @@ class MsgPackDeserializer {
|
||||
|
||||
JsonString key = stringBuffer_.str();
|
||||
TFilter memberFilter = filter[key.c_str()];
|
||||
VariantData* member;
|
||||
VariantData* member = 0;
|
||||
|
||||
if (memberFilter.allow()) {
|
||||
ARDUINOJSON_ASSERT(object != 0);
|
||||
|
||||
// Save key in memory pool.
|
||||
auto savedKey = stringBuffer_.save();
|
||||
|
||||
member = object->addMember(savedKey, resources_);
|
||||
if (!member)
|
||||
auto keyVariant = object.addPair(&member);
|
||||
if (!keyVariant)
|
||||
return DeserializationError::NoMemory;
|
||||
} else {
|
||||
member = 0;
|
||||
|
||||
stringBuffer_.save(keyVariant);
|
||||
}
|
||||
|
||||
err = parseVariant(member, memberFilter, nestingLimit.decrement());
|
||||
|
@ -31,11 +31,11 @@ class MsgPackExtension {
|
||||
template <>
|
||||
struct Converter<MsgPackExtension> : private detail::VariantAttorney {
|
||||
static void toJson(MsgPackExtension src, JsonVariant dst) {
|
||||
auto data = VariantAttorney::getData(dst);
|
||||
auto data = getData(dst);
|
||||
if (!data)
|
||||
return;
|
||||
auto resources = getResourceManager(dst);
|
||||
data->clear(resources);
|
||||
detail::VariantImpl(data, resources).clear();
|
||||
if (src.data()) {
|
||||
uint8_t format, sizeBytes;
|
||||
if (src.size() >= 0x10000) {
|
||||
@ -80,10 +80,8 @@ struct Converter<MsgPackExtension> : private detail::VariantAttorney {
|
||||
}
|
||||
|
||||
static MsgPackExtension fromJson(JsonVariantConst src) {
|
||||
auto data = getData(src);
|
||||
if (!data)
|
||||
return {};
|
||||
auto rawstr = data->asRawString();
|
||||
auto variant = VariantAttorney::getVariantImpl(src);
|
||||
auto rawstr = variant.asRawString();
|
||||
if (rawstr.size() == 0)
|
||||
return {};
|
||||
auto p = reinterpret_cast<const uint8_t*>(rawstr.c_str());
|
||||
|
@ -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,8 @@ class MsgPackSerializer : public VariantDataVisitor<size_t> {
|
||||
return bytesWritten();
|
||||
}
|
||||
|
||||
size_t visit(const ArrayData& array) {
|
||||
size_t n = array.size(resources_);
|
||||
size_t visit(const ArrayImpl& array) {
|
||||
size_t n = array.size();
|
||||
if (n < 0x10) {
|
||||
writeByte(uint8_t(0x90 + n));
|
||||
} else if (n < 0x10000) {
|
||||
@ -62,15 +62,15 @@ class MsgPackSerializer : public VariantDataVisitor<size_t> {
|
||||
auto slotId = array.head();
|
||||
while (slotId != NULL_SLOT) {
|
||||
auto slot = resources_->getVariant(slotId);
|
||||
slot->accept(*this, resources_);
|
||||
slotId = slot->next();
|
||||
VariantImpl(slot, resources_).accept(*this);
|
||||
slotId = slot->next;
|
||||
}
|
||||
|
||||
return bytesWritten();
|
||||
}
|
||||
|
||||
size_t visit(const ObjectData& object) {
|
||||
size_t n = object.size(resources_);
|
||||
size_t visit(const ObjectImpl& object) {
|
||||
size_t n = object.size();
|
||||
if (n < 0x10) {
|
||||
writeByte(uint8_t(0x80 + n));
|
||||
} else if (n < 0x10000) {
|
||||
@ -84,8 +84,8 @@ class MsgPackSerializer : public VariantDataVisitor<size_t> {
|
||||
auto slotId = object.head();
|
||||
while (slotId != NULL_SLOT) {
|
||||
auto slot = resources_->getVariant(slotId);
|
||||
slot->accept(*this, resources_);
|
||||
slotId = slot->next();
|
||||
VariantImpl(slot, resources_).accept(*this);
|
||||
slotId = slot->next;
|
||||
}
|
||||
|
||||
return bytesWritten();
|
||||
@ -209,7 +209,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::ObjectImpl& impl) : impl_(impl) {}
|
||||
|
||||
// INTERNAL USE ONLY
|
||||
JsonObject(detail::VariantData* data, detail::ResourceManager* resource)
|
||||
: impl_(detail::VariantImpl(data, resource).asObject()) {}
|
||||
|
||||
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_.isNull();
|
||||
}
|
||||
|
||||
// Returns true if the reference is bound.
|
||||
// https://arduinojson.org/v7/api/jsonobject/isnull/
|
||||
operator bool() const {
|
||||
return data_ != 0;
|
||||
return !isNull();
|
||||
}
|
||||
|
||||
// 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_.getResourceManager());
|
||||
}
|
||||
|
||||
// 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_.clear();
|
||||
}
|
||||
|
||||
// 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();
|
||||
@ -131,7 +130,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_.remove(it.iterator_);
|
||||
}
|
||||
|
||||
// Removes the member with the specified key.
|
||||
@ -139,8 +138,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 +154,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,8 +163,7 @@ 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
|
||||
@ -178,8 +174,7 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
|
||||
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 +222,18 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
|
||||
|
||||
private:
|
||||
detail::ResourceManager* getResourceManager() const {
|
||||
return resources_;
|
||||
return impl_.getResourceManager();
|
||||
}
|
||||
|
||||
detail::VariantData* getData() const {
|
||||
return detail::collectionToVariant(data_);
|
||||
return impl_.getData();
|
||||
}
|
||||
|
||||
detail::VariantData* getOrCreateData() const {
|
||||
return detail::collectionToVariant(data_);
|
||||
return impl_.getData();
|
||||
}
|
||||
|
||||
detail::ObjectData* data_;
|
||||
detail::ResourceManager* resources_;
|
||||
mutable detail::ObjectImpl 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_(detail::VariantImpl(data, resources).asObject()) {}
|
||||
|
||||
// INTERNAL USE ONLY
|
||||
JsonObjectConst(const detail::ObjectImpl& impl) : impl_(impl) {}
|
||||
|
||||
operator JsonVariantConst() const {
|
||||
return JsonVariantConst(getData(), resources_);
|
||||
return JsonVariantConst(impl_.getData(), impl_.getResourceManager());
|
||||
}
|
||||
|
||||
// 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_.getResourceManager());
|
||||
}
|
||||
|
||||
// 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,9 +99,8 @@ 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_.getResourceManager());
|
||||
}
|
||||
|
||||
// Gets the member with specified key.
|
||||
@ -113,9 +110,8 @@ class JsonObjectConst : public detail::VariantOperators<JsonObjectConst> {
|
||||
!detail::is_const<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_.getResourceManager());
|
||||
}
|
||||
|
||||
// Gets the member with specified key.
|
||||
@ -137,11 +133,10 @@ class JsonObjectConst : public detail::VariantOperators<JsonObjectConst> {
|
||||
|
||||
private:
|
||||
const detail::VariantData* getData() const {
|
||||
return collectionToVariant(data_);
|
||||
return impl_.getData();
|
||||
}
|
||||
|
||||
const detail::ObjectData* data_;
|
||||
const detail::ResourceManager* resources_;
|
||||
detail::ObjectImpl 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::ObjectImpl::iterator iterator,
|
||||
detail::ResourceManager* resources)
|
||||
: iterator_(iterator), resources_(resources) {}
|
||||
|
||||
@ -40,7 +40,7 @@ class JsonObjectIterator {
|
||||
}
|
||||
|
||||
private:
|
||||
detail::ObjectData::iterator iterator_;
|
||||
detail::ObjectImpl::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::ObjectImpl::iterator iterator,
|
||||
detail::ResourceManager* resources)
|
||||
: iterator_(iterator), resources_(resources) {}
|
||||
|
||||
JsonPairConst operator*() const {
|
||||
@ -76,8 +76,8 @@ class JsonObjectConstIterator {
|
||||
}
|
||||
|
||||
private:
|
||||
detail::ObjectData::iterator iterator_;
|
||||
const detail::ResourceManager* resources_;
|
||||
detail::ObjectImpl::iterator iterator_;
|
||||
detail::ResourceManager* resources_;
|
||||
};
|
||||
|
||||
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
||||
|
@ -15,10 +15,11 @@ ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE
|
||||
class JsonPair {
|
||||
public:
|
||||
// INTERNAL USE ONLY
|
||||
JsonPair(detail::ObjectData::iterator iterator,
|
||||
JsonPair(detail::ObjectImpl::iterator iterator,
|
||||
detail::ResourceManager* resources) {
|
||||
if (!iterator.done()) {
|
||||
key_ = iterator->asString();
|
||||
detail::VariantImpl variant(iterator.data(), resources);
|
||||
key_ = variant.asString();
|
||||
iterator.next(resources);
|
||||
value_ = JsonVariant(iterator.data(), resources);
|
||||
}
|
||||
@ -43,10 +44,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::ObjectImpl::iterator iterator,
|
||||
detail::ResourceManager* resources) {
|
||||
if (!iterator.done()) {
|
||||
key_ = iterator->asString();
|
||||
detail::VariantImpl variant(iterator.data(), resources);
|
||||
key_ = variant.asString();
|
||||
iterator.next(resources);
|
||||
value_ = JsonVariantConst(iterator.data(), resources);
|
||||
}
|
||||
|
@ -56,17 +56,12 @@ 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_));
|
||||
return VariantAttorney::getOrCreateVariantImpl(upstream_).getOrAddMember(
|
||||
key_);
|
||||
}
|
||||
|
||||
private:
|
||||
|
@ -8,61 +8,40 @@
|
||||
|
||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
|
||||
class ObjectData : public CollectionData {
|
||||
class VariantImpl;
|
||||
|
||||
class ObjectImpl : public CollectionImpl {
|
||||
public:
|
||||
template <typename TAdaptedString> // also works with StringNode*
|
||||
VariantData* addMember(TAdaptedString key, ResourceManager* resources);
|
||||
ObjectImpl() {}
|
||||
|
||||
ObjectImpl(CollectionData* data, ResourceManager* resources)
|
||||
: CollectionImpl(data, resources) {}
|
||||
|
||||
template <typename TAdaptedString>
|
||||
VariantData* getOrAddMember(TAdaptedString key, ResourceManager* resources);
|
||||
VariantData* addMember(TAdaptedString key);
|
||||
|
||||
VariantData* addPair(VariantData** value);
|
||||
|
||||
template <typename TAdaptedString>
|
||||
VariantData* getMember(TAdaptedString key,
|
||||
const ResourceManager* resources) const;
|
||||
VariantData* getOrAddMember(TAdaptedString key);
|
||||
|
||||
template <typename TAdaptedString>
|
||||
static VariantData* getMember(const ObjectData* object, TAdaptedString key,
|
||||
const ResourceManager* resources) {
|
||||
if (!object)
|
||||
return nullptr;
|
||||
return object->getMember(key, resources);
|
||||
}
|
||||
VariantData* getMember(TAdaptedString key) const;
|
||||
|
||||
template <typename TAdaptedString>
|
||||
void removeMember(TAdaptedString key, ResourceManager* resources);
|
||||
void removeMember(TAdaptedString key);
|
||||
|
||||
template <typename TAdaptedString>
|
||||
static void removeMember(ObjectData* obj, TAdaptedString key,
|
||||
ResourceManager* resources) {
|
||||
if (!obj)
|
||||
return;
|
||||
obj->removeMember(key, resources);
|
||||
void remove(iterator it) {
|
||||
CollectionImpl::removePair(it);
|
||||
}
|
||||
|
||||
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);
|
||||
size_t size() const {
|
||||
return CollectionImpl::size() / 2;
|
||||
}
|
||||
|
||||
private:
|
||||
template <typename TAdaptedString>
|
||||
iterator findKey(TAdaptedString key, const ResourceManager* resources) const;
|
||||
iterator findKey(TAdaptedString key) const;
|
||||
};
|
||||
|
||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||
|
@ -11,32 +11,30 @@
|
||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
|
||||
template <typename TAdaptedString>
|
||||
inline VariantData* ObjectData::getMember(
|
||||
TAdaptedString key, const ResourceManager* resources) const {
|
||||
auto it = findKey(key, resources);
|
||||
inline VariantData* ObjectImpl::getMember(TAdaptedString key) const {
|
||||
auto it = findKey(key);
|
||||
if (it.done())
|
||||
return nullptr;
|
||||
it.next(resources);
|
||||
it.next(resources_);
|
||||
return it.data();
|
||||
}
|
||||
|
||||
template <typename TAdaptedString>
|
||||
VariantData* ObjectData::getOrAddMember(TAdaptedString key,
|
||||
ResourceManager* resources) {
|
||||
auto data = getMember(key, resources);
|
||||
VariantData* ObjectImpl::getOrAddMember(TAdaptedString key) {
|
||||
auto data = getMember(key);
|
||||
if (data)
|
||||
return data;
|
||||
return addMember(key, resources);
|
||||
return addMember(key);
|
||||
}
|
||||
|
||||
template <typename TAdaptedString>
|
||||
inline ObjectData::iterator ObjectData::findKey(
|
||||
TAdaptedString key, const ResourceManager* resources) const {
|
||||
inline ObjectImpl::iterator ObjectImpl::findKey(TAdaptedString key) const {
|
||||
if (key.isNull())
|
||||
return iterator();
|
||||
bool isKey = true;
|
||||
for (auto it = createIterator(resources); !it.done(); it.next(resources)) {
|
||||
if (isKey && stringEquals(key, adaptString(it->asString())))
|
||||
for (auto it = createIterator(); !it.done(); it.next(resources_)) {
|
||||
VariantImpl variant(it.data(), resources_);
|
||||
if (isKey && stringEquals(key, adaptString(variant.asString())))
|
||||
return it;
|
||||
isKey = !isKey;
|
||||
}
|
||||
@ -44,33 +42,55 @@ inline ObjectData::iterator ObjectData::findKey(
|
||||
}
|
||||
|
||||
template <typename TAdaptedString>
|
||||
inline void ObjectData::removeMember(TAdaptedString key,
|
||||
ResourceManager* resources) {
|
||||
remove(findKey(key, resources), resources);
|
||||
inline void ObjectImpl::removeMember(TAdaptedString key) {
|
||||
remove(findKey(key));
|
||||
}
|
||||
|
||||
template <typename TAdaptedString>
|
||||
inline VariantData* ObjectData::addMember(TAdaptedString key,
|
||||
ResourceManager* resources) {
|
||||
auto keySlot = resources->allocVariant();
|
||||
inline VariantData* ObjectImpl::addMember(TAdaptedString key) {
|
||||
if (!data_)
|
||||
return nullptr;
|
||||
ARDUINOJSON_ASSERT(resources_ != nullptr);
|
||||
|
||||
auto keySlot = resources_->allocVariant();
|
||||
if (!keySlot)
|
||||
return nullptr;
|
||||
|
||||
auto valueSlot = resources->allocVariant();
|
||||
auto valueSlot = resources_->allocVariant();
|
||||
if (!valueSlot)
|
||||
return nullptr;
|
||||
|
||||
if (!keySlot->setString(key, resources))
|
||||
VariantImpl keyImpl(keySlot.ptr(), resources_);
|
||||
if (!keyImpl.setString(key))
|
||||
return nullptr;
|
||||
|
||||
CollectionData::appendPair(keySlot, valueSlot, resources);
|
||||
CollectionImpl::appendPair(keySlot, valueSlot);
|
||||
|
||||
return valueSlot.ptr();
|
||||
}
|
||||
|
||||
inline VariantData* ObjectImpl::addPair(VariantData** value) {
|
||||
if (!data_)
|
||||
return nullptr;
|
||||
ARDUINOJSON_ASSERT(resources_ != nullptr);
|
||||
|
||||
auto keySlot = resources_->allocVariant();
|
||||
if (!keySlot)
|
||||
return nullptr;
|
||||
|
||||
auto valueSlot = resources_->allocVariant();
|
||||
if (!valueSlot)
|
||||
return nullptr;
|
||||
*value = valueSlot.ptr();
|
||||
|
||||
CollectionImpl::appendPair(keySlot, valueSlot);
|
||||
|
||||
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(data, resources).accept(serializer);
|
||||
}
|
||||
|
||||
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(data, resources).accept(serializer);
|
||||
}
|
||||
|
||||
template <template <typename> class TSerializer, typename TDestination>
|
||||
|
@ -76,12 +76,23 @@ 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);
|
||||
static AdaptedString adapt(const char* p) {
|
||||
ARDUINOJSON_ASSERT(p);
|
||||
return RamString(p, ::strlen(p), true);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -28,8 +28,7 @@ class JsonString {
|
||||
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), false) {}
|
||||
|
||||
// Returns a pointer to the characters.
|
||||
const char* c_str() const {
|
||||
|
@ -60,25 +60,18 @@ 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);
|
||||
auto variant = getVariantImpl(dst);
|
||||
variant.clear();
|
||||
return variant.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,40 +83,29 @@ 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;
|
||||
auto variant = getVariantImpl(dst);
|
||||
variant.clear();
|
||||
return variant.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) {
|
||||
auto data = getData(src);
|
||||
return data && data->isBoolean();
|
||||
return data && data->type == detail::VariantType::Boolean;
|
||||
}
|
||||
};
|
||||
|
||||
@ -131,18 +113,13 @@ template <typename T>
|
||||
struct Converter<T, detail::enable_if_t<detail::is_floating_point<T>::value>>
|
||||
: 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);
|
||||
auto variant = getVariantImpl(dst);
|
||||
variant.clear();
|
||||
return variant.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) {
|
||||
@ -154,13 +131,13 @@ 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));
|
||||
auto variant = getVariantImpl(dst);
|
||||
variant.clear();
|
||||
variant.setString(detail::adaptString(src));
|
||||
}
|
||||
|
||||
static const char* fromJson(JsonVariantConst src) {
|
||||
auto data = getData(src);
|
||||
return data ? data->asString().c_str() : 0;
|
||||
return getVariantImpl(src).asString().c_str();
|
||||
}
|
||||
|
||||
static bool checkJson(JsonVariantConst src) {
|
||||
@ -172,13 +149,13 @@ 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));
|
||||
auto variant = getVariantImpl(dst);
|
||||
variant.clear();
|
||||
variant.setString(detail::adaptString(src));
|
||||
}
|
||||
|
||||
static JsonString fromJson(JsonVariantConst src) {
|
||||
auto data = getData(src);
|
||||
return data ? data->asString() : JsonString();
|
||||
return getVariantImpl(src).asString();
|
||||
}
|
||||
|
||||
static bool checkJson(JsonVariantConst src) {
|
||||
@ -190,10 +167,9 @@ struct Converter<JsonString> : private detail::VariantAttorney {
|
||||
template <typename T>
|
||||
inline detail::enable_if_t<detail::IsString<T>::value> 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);
|
||||
auto variant = detail::VariantAttorney::getVariantImpl(dst);
|
||||
variant.clear();
|
||||
variant.setString(detail::adaptString(src));
|
||||
}
|
||||
|
||||
// SerializedValue<std::string>
|
||||
@ -202,22 +178,23 @@ inline detail::enable_if_t<detail::IsString<T>::value> convertToJson(
|
||||
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));
|
||||
auto variant = getVariantImpl(dst);
|
||||
variant.clear();
|
||||
variant.setRawString(src);
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<detail::nullptr_t> : private detail::VariantAttorney {
|
||||
static void toJson(detail::nullptr_t, JsonVariant dst) {
|
||||
detail::VariantData::clear(getData(dst), getResourceManager(dst));
|
||||
getVariantImpl(dst).clear();
|
||||
}
|
||||
static detail::nullptr_t fromJson(JsonVariantConst) {
|
||||
return nullptr;
|
||||
}
|
||||
static bool checkJson(JsonVariantConst src) {
|
||||
auto data = getData(src);
|
||||
return data == 0 || data->isNull();
|
||||
return data == 0 || data->type == detail::VariantType::Null;
|
||||
}
|
||||
};
|
||||
|
||||
@ -230,9 +207,9 @@ class StringBuilderPrint : public Print {
|
||||
copier_.startString();
|
||||
}
|
||||
|
||||
StringNode* save() {
|
||||
void save(VariantData* data) {
|
||||
ARDUINOJSON_ASSERT(!overflowed());
|
||||
return copier_.save();
|
||||
copier_.save(data);
|
||||
}
|
||||
|
||||
size_t write(uint8_t c) {
|
||||
@ -263,12 +240,12 @@ inline void convertToJson(const ::Printable& src, JsonVariant dst) {
|
||||
auto data = detail::VariantAttorney::getData(dst);
|
||||
if (!resources || !data)
|
||||
return;
|
||||
data->clear(resources);
|
||||
detail::VariantImpl(data, resources).clear();
|
||||
detail::StringBuilderPrint print(resources);
|
||||
src.printTo(print);
|
||||
if (print.overflowed())
|
||||
return;
|
||||
data->setOwnedString(print.save());
|
||||
print.save(data);
|
||||
}
|
||||
|
||||
#endif
|
||||
@ -329,14 +306,12 @@ struct Converter<JsonArrayConst> : private detail::VariantAttorney {
|
||||
}
|
||||
|
||||
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) {
|
||||
auto data = getData(src);
|
||||
return data && data->isArray();
|
||||
return data && data->type == detail::VariantType::Array;
|
||||
}
|
||||
};
|
||||
|
||||
@ -350,14 +325,12 @@ struct Converter<JsonArray> : private detail::VariantAttorney {
|
||||
}
|
||||
|
||||
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) {
|
||||
auto data = getData(src);
|
||||
return data && data->isArray();
|
||||
return data && data->type == detail::VariantType::Array;
|
||||
}
|
||||
};
|
||||
|
||||
@ -371,14 +344,12 @@ struct Converter<JsonObjectConst> : private detail::VariantAttorney {
|
||||
}
|
||||
|
||||
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) {
|
||||
auto data = getData(src);
|
||||
return data && data->isObject();
|
||||
return data && data->type == detail::VariantType::Object;
|
||||
}
|
||||
};
|
||||
|
||||
@ -392,14 +363,12 @@ struct Converter<JsonObject> : private detail::VariantAttorney {
|
||||
}
|
||||
|
||||
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) {
|
||||
auto data = getData(src);
|
||||
return data && data->isObject();
|
||||
return data && data->type == detail::VariantType::Object;
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -16,27 +16,29 @@ class JsonVariant : public detail::VariantRefBase<JsonVariant>,
|
||||
|
||||
public:
|
||||
// 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_.getResourceManager();
|
||||
}
|
||||
|
||||
detail::VariantData* getData() const {
|
||||
return data_;
|
||||
return impl_.getData();
|
||||
}
|
||||
|
||||
detail::VariantData* getOrCreateData() const {
|
||||
return data_;
|
||||
return impl_.getData();
|
||||
}
|
||||
|
||||
detail::VariantData* data_;
|
||||
detail::ResourceManager* resources_;
|
||||
mutable detail::VariantImpl impl_;
|
||||
};
|
||||
|
||||
namespace detail {
|
||||
|
@ -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_.getData() == 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,8 @@ 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_.getResourceManager());
|
||||
}
|
||||
|
||||
// Gets object's member with specified key.
|
||||
@ -114,9 +113,8 @@ 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_.getResourceManager());
|
||||
}
|
||||
|
||||
// Gets object's member with specified key.
|
||||
@ -126,9 +124,8 @@ class JsonVariantConst : public detail::VariantTag,
|
||||
!detail::is_const<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_.getResourceManager());
|
||||
}
|
||||
|
||||
// Gets object's member with specified key or the array's element at the
|
||||
@ -149,8 +146,7 @@ class JsonVariantConst : public detail::VariantTag,
|
||||
detail::enable_if_t<detail::IsString<TString>::value, int> = 0>
|
||||
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
|
||||
@ -161,8 +157,7 @@ class JsonVariantConst : public detail::VariantTag,
|
||||
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 +176,16 @@ class JsonVariantConst : public detail::VariantTag,
|
||||
}
|
||||
|
||||
protected:
|
||||
const detail::VariantData* getData() const {
|
||||
return data_;
|
||||
detail::VariantData* getData() const {
|
||||
return impl_.getData();
|
||||
}
|
||||
|
||||
const detail::ResourceManager* getResourceManager() const {
|
||||
return resources_;
|
||||
detail::ResourceManager* getResourceManager() const {
|
||||
return impl_.getResourceManager();
|
||||
}
|
||||
|
||||
private:
|
||||
const detail::VariantData* data_;
|
||||
const detail::ResourceManager* resources_;
|
||||
mutable detail::VariantImpl impl_;
|
||||
};
|
||||
|
||||
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
||||
|
@ -26,15 +26,14 @@ class VisitorAdapter {
|
||||
public:
|
||||
using result_type = typename TVisitor::result_type;
|
||||
|
||||
VisitorAdapter(TVisitor& visitor, const ResourceManager* resources)
|
||||
: visitor_(&visitor), resources_(resources) {}
|
||||
VisitorAdapter(TVisitor& visitor) : visitor_(&visitor) {}
|
||||
|
||||
result_type visit(const ArrayData& value) {
|
||||
return visitor_->visit(JsonArrayConst(&value, resources_));
|
||||
result_type visit(const ArrayImpl& array) {
|
||||
return visitor_->visit(JsonArrayConst(array));
|
||||
}
|
||||
|
||||
result_type visit(const ObjectData& value) {
|
||||
return visitor_->visit(JsonObjectConst(&value, resources_));
|
||||
result_type visit(const ObjectImpl& object) {
|
||||
return visitor_->visit(JsonObjectConst(object));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
@ -44,18 +43,15 @@ class VisitorAdapter {
|
||||
|
||||
private:
|
||||
TVisitor* visitor_;
|
||||
const 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);
|
||||
return VariantImpl(data, resources).accept(adapter);
|
||||
}
|
||||
|
||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||
|
@ -6,7 +6,7 @@
|
||||
|
||||
#include <ArduinoJson/Polyfills/attributes.hpp>
|
||||
#include <ArduinoJson/Polyfills/type_traits.hpp>
|
||||
#include <ArduinoJson/Variant/VariantData.hpp>
|
||||
#include <ArduinoJson/Variant/VariantImpl.hpp>
|
||||
#include <ArduinoJson/Variant/VariantTo.hpp>
|
||||
#include "JsonVariantConst.hpp"
|
||||
|
||||
@ -26,6 +26,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();
|
||||
|
@ -16,14 +16,15 @@ 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
|
||||
@ -46,6 +47,8 @@ inline bool operator&(VariantType type, VariantTypeBits bit) {
|
||||
return (uint8_t(type) & uint8_t(bit)) != 0;
|
||||
}
|
||||
|
||||
const size_t tinyStringMaxLength = 3;
|
||||
|
||||
union VariantContent {
|
||||
VariantContent() {}
|
||||
|
||||
@ -53,18 +56,14 @@ union VariantContent {
|
||||
bool asBoolean;
|
||||
uint32_t asUint32;
|
||||
int32_t asInt32;
|
||||
#if ARDUINOJSON_USE_EXTENSIONS
|
||||
SlotId asSlotId;
|
||||
#endif
|
||||
ArrayData asArray;
|
||||
ObjectData asObject;
|
||||
CollectionData asCollection;
|
||||
const char* asLinkedString;
|
||||
struct StringNode* asOwnedString;
|
||||
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;
|
||||
@ -73,6 +72,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,7 @@
|
||||
|
||||
#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
|
||||
@ -17,12 +12,21 @@ ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||
template <typename T>
|
||||
T parseNumber(const char* s);
|
||||
|
||||
class VariantData {
|
||||
VariantContent content_; // must be first to allow cast from array to variant
|
||||
VariantType type_;
|
||||
SlotId next_;
|
||||
template <typename T>
|
||||
static bool isTinyString(const T& s, size_t n) {
|
||||
if (n > tinyStringMaxLength)
|
||||
return false;
|
||||
bool containsNul = false;
|
||||
for (uint8_t i = 0; i < uint8_t(n); i++)
|
||||
containsNul |= !s[i];
|
||||
return !containsNul;
|
||||
}
|
||||
|
||||
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;
|
||||
@ -30,545 +34,50 @@ 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::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::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::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_) {
|
||||
case VariantType::RawString:
|
||||
return JsonString(content_.asOwnedString->data,
|
||||
content_.asOwnedString->length);
|
||||
default:
|
||||
return JsonString();
|
||||
}
|
||||
}
|
||||
|
||||
JsonString asString() const {
|
||||
switch (type_) {
|
||||
case VariantType::LinkedString:
|
||||
return JsonString(content_.asLinkedString, true);
|
||||
case VariantType::OwnedString:
|
||||
return JsonString(content_.asOwnedString->data,
|
||||
content_.asOwnedString->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;
|
||||
}
|
||||
|
||||
bool isBoolean() const {
|
||||
return type_ == VariantType::Boolean;
|
||||
}
|
||||
|
||||
bool isCollection() const {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
bool isNull() const {
|
||||
return type_ == VariantType::Null;
|
||||
}
|
||||
|
||||
static bool isNull(const VariantData* var) {
|
||||
if (!var)
|
||||
return true;
|
||||
return var->isNull();
|
||||
}
|
||||
|
||||
bool isObject() const {
|
||||
return type_ == VariantType::Object;
|
||||
}
|
||||
|
||||
bool isString() const {
|
||||
return type_ == VariantType::LinkedString ||
|
||||
type_ == VariantType::OwnedString;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void setBoolean(bool value) {
|
||||
ARDUINOJSON_ASSERT(type_ == VariantType::Null); // must call clear() first
|
||||
type_ = VariantType::Boolean;
|
||||
content_.asBoolean = value;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
enable_if_t<sizeof(T) == 4, bool> setFloat(T value, ResourceManager*) {
|
||||
ARDUINOJSON_ASSERT(type_ == VariantType::Null); // must call clear() first
|
||||
type_ = VariantType::Float;
|
||||
content_.asFloat = value;
|
||||
return true;
|
||||
}
|
||||
|
||||
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(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);
|
||||
ARDUINOJSON_ASSERT(type == VariantType::Null);
|
||||
type = VariantType::Boolean;
|
||||
content.asBoolean = value;
|
||||
}
|
||||
|
||||
template <typename TAdaptedString>
|
||||
bool setString(TAdaptedString value, ResourceManager* resources);
|
||||
void setTinyString(const TAdaptedString& s) {
|
||||
ARDUINOJSON_ASSERT(type == VariantType::Null);
|
||||
ARDUINOJSON_ASSERT(s.size() <= tinyStringMaxLength);
|
||||
|
||||
bool setString(StringNode* s, ResourceManager*) {
|
||||
setOwnedString(s);
|
||||
return true;
|
||||
}
|
||||
type = VariantType::TinyString;
|
||||
|
||||
template <typename TAdaptedString>
|
||||
static void setString(VariantData* var, TAdaptedString value,
|
||||
ResourceManager* resources) {
|
||||
if (!var)
|
||||
return;
|
||||
var->clear(resources);
|
||||
var->setString(value, resources);
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
void setLinkedString(const char* s) {
|
||||
ARDUINOJSON_ASSERT(type_ == VariantType::Null); // must call clear() first
|
||||
ARDUINOJSON_ASSERT(s);
|
||||
type_ = VariantType::LinkedString;
|
||||
content_.asLinkedString = s;
|
||||
content.asTinyString[n] = 0;
|
||||
}
|
||||
|
||||
void setOwnedString(StringNode* s) {
|
||||
ARDUINOJSON_ASSERT(type_ == VariantType::Null); // must call clear() first
|
||||
ARDUINOJSON_ASSERT(type == VariantType::Null);
|
||||
ARDUINOJSON_ASSERT(s);
|
||||
type_ = VariantType::OwnedString;
|
||||
content_.asOwnedString = s;
|
||||
type = VariantType::OwnedString;
|
||||
content.asOwnedString = s;
|
||||
}
|
||||
|
||||
size_t size(const ResourceManager* resources) const {
|
||||
if (isObject())
|
||||
return content_.asObject.size(resources);
|
||||
|
||||
if (isArray())
|
||||
return content_.asArray.size(resources);
|
||||
|
||||
return 0;
|
||||
void setRawString(StringNode* s) {
|
||||
ARDUINOJSON_ASSERT(type == VariantType::Null);
|
||||
ARDUINOJSON_ASSERT(s);
|
||||
type = VariantType::RawString;
|
||||
content.asOwnedString = s;
|
||||
}
|
||||
|
||||
static size_t size(const VariantData* var, const ResourceManager* resources) {
|
||||
return var != 0 ? var->size(resources) : 0;
|
||||
bool isFloat() const {
|
||||
return type & VariantTypeBits::NumberBit;
|
||||
}
|
||||
|
||||
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);
|
||||
bool isString() const {
|
||||
return type == VariantType::LinkedString ||
|
||||
type == VariantType::OwnedString || type == VariantType::TinyString;
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -5,137 +5,570 @@
|
||||
#pragma once
|
||||
|
||||
#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
|
||||
|
||||
class VariantImpl {
|
||||
public:
|
||||
VariantImpl() : data_(nullptr), resources_(nullptr) {}
|
||||
|
||||
VariantImpl(VariantData* data, ResourceManager* resources)
|
||||
: data_(data), resources_(resources) {}
|
||||
|
||||
VariantData* getData() const {
|
||||
return data_;
|
||||
}
|
||||
|
||||
ResourceManager* getResourceManager() const {
|
||||
return resources_;
|
||||
}
|
||||
|
||||
template <typename TVisitor>
|
||||
typename TVisitor::result_type accept(TVisitor& visit) {
|
||||
if (!data_)
|
||||
return visit.visit(nullptr);
|
||||
|
||||
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||
auto eightByteValue = getEightByte();
|
||||
#endif
|
||||
switch (data_->type) {
|
||||
case VariantType::Float:
|
||||
return visit.visit(data_->content.asFloat);
|
||||
|
||||
#if ARDUINOJSON_USE_DOUBLE
|
||||
case VariantType::Double:
|
||||
return visit.visit(eightByteValue->asDouble);
|
||||
#endif
|
||||
|
||||
case VariantType::Array:
|
||||
return visit.visit(asArray());
|
||||
|
||||
case VariantType::Object:
|
||||
return visit.visit(asObject());
|
||||
|
||||
case VariantType::TinyString:
|
||||
return visit.visit(JsonString(data_->content.asTinyString));
|
||||
|
||||
case VariantType::LinkedString:
|
||||
return visit.visit(JsonString(asLinkedString(), true));
|
||||
|
||||
case VariantType::OwnedString:
|
||||
return visit.visit(JsonString(data_->content.asOwnedString->data,
|
||||
data_->content.asOwnedString->length));
|
||||
|
||||
case VariantType::RawString:
|
||||
return visit.visit(RawString(data_->content.asOwnedString->data,
|
||||
data_->content.asOwnedString->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* addElement() {
|
||||
auto array = isNull() ? toArray() : asArray();
|
||||
return array.addElement();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool addValue(const T& value) {
|
||||
auto array = isNull() ? toArray() : asArray();
|
||||
return array.addValue(value);
|
||||
}
|
||||
|
||||
bool asBoolean() const {
|
||||
if (!data_)
|
||||
return false;
|
||||
|
||||
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||
auto eightByteValue = getEightByte();
|
||||
#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;
|
||||
}
|
||||
}
|
||||
|
||||
ArrayImpl asArray() {
|
||||
return ArrayImpl(isArray() ? &data_->content.asCollection : nullptr,
|
||||
resources_);
|
||||
}
|
||||
|
||||
CollectionImpl asCollection() {
|
||||
return CollectionImpl(
|
||||
isCollection() ? &data_->content.asCollection : nullptr, resources_);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T asFloat() const {
|
||||
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();
|
||||
#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::LinkedString:
|
||||
str = asLinkedString();
|
||||
break;
|
||||
case VariantType::OwnedString:
|
||||
str = data_->content.asOwnedString->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 {
|
||||
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();
|
||||
#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::LinkedString:
|
||||
str = asLinkedString();
|
||||
break;
|
||||
case VariantType::OwnedString:
|
||||
str = data_->content.asOwnedString->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);
|
||||
}
|
||||
|
||||
ObjectImpl asObject() {
|
||||
return ObjectImpl(isObject() ? &data_->content.asCollection : nullptr,
|
||||
resources_);
|
||||
}
|
||||
|
||||
JsonString asRawString() const {
|
||||
switch (type()) {
|
||||
case VariantType::RawString:
|
||||
return JsonString(data_->content.asOwnedString->data,
|
||||
data_->content.asOwnedString->length);
|
||||
default:
|
||||
return JsonString();
|
||||
}
|
||||
}
|
||||
|
||||
const char* asLinkedString() const {
|
||||
ARDUINOJSON_ASSERT(type() == VariantType::LinkedString);
|
||||
return resources_->getStaticString(data_->content.asSlotId);
|
||||
}
|
||||
|
||||
JsonString asString() const {
|
||||
switch (type()) {
|
||||
case VariantType::TinyString:
|
||||
return JsonString(data_->content.asTinyString);
|
||||
case VariantType::LinkedString:
|
||||
return JsonString(asLinkedString(), true);
|
||||
case VariantType::OwnedString:
|
||||
return JsonString(data_->content.asOwnedString->data,
|
||||
data_->content.asOwnedString->length);
|
||||
default:
|
||||
return JsonString();
|
||||
}
|
||||
}
|
||||
|
||||
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||
const EightByteValue* getEightByte() const {
|
||||
return type() & VariantTypeBits::EightByteBit
|
||||
? resources_->getEightByte(data_->content.asSlotId)
|
||||
: 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
VariantData* getElement(size_t index) {
|
||||
return asArray().getElement(index);
|
||||
}
|
||||
|
||||
template <typename TAdaptedString>
|
||||
VariantData* getMember(TAdaptedString key) {
|
||||
return asObject().getMember(key);
|
||||
}
|
||||
|
||||
VariantData* getOrAddElement(size_t index) {
|
||||
auto array = isNull() ? toArray() : asArray();
|
||||
return array.getOrAddElement(index);
|
||||
}
|
||||
|
||||
template <typename TAdaptedString>
|
||||
VariantData* getOrAddMember(TAdaptedString key) {
|
||||
if (key.isNull())
|
||||
return nullptr;
|
||||
auto obj = isNull() ? toObject() : asObject();
|
||||
return obj.getOrAddMember(key);
|
||||
}
|
||||
|
||||
bool isArray() const {
|
||||
return type() == VariantType::Array;
|
||||
}
|
||||
|
||||
bool isBoolean() const {
|
||||
return type() == VariantType::Boolean;
|
||||
}
|
||||
|
||||
bool isCollection() const {
|
||||
return type() & VariantTypeBits::CollectionMask;
|
||||
}
|
||||
|
||||
bool isFloat() const {
|
||||
return data_ && data_->isFloat();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool isInteger() const {
|
||||
if (!data_)
|
||||
return false;
|
||||
|
||||
#if ARDUINOJSON_USE_LONG_LONG
|
||||
auto eightByteValue = getEightByte();
|
||||
#endif
|
||||
switch (data_->type) {
|
||||
case VariantType::Uint32:
|
||||
return canConvertNumber<T>(data_->content.asUint32);
|
||||
|
||||
case VariantType::Int32:
|
||||
return canConvertNumber<T>(data_->content.asInt32);
|
||||
|
||||
#if ARDUINOJSON_USE_LONG_LONG
|
||||
case VariantType::Uint64:
|
||||
return canConvertNumber<T>(eightByteValue->asUint64);
|
||||
|
||||
case VariantType::Int64:
|
||||
return canConvertNumber<T>(eightByteValue->asInt64);
|
||||
#endif
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool isNull() const {
|
||||
return type() == VariantType::Null;
|
||||
}
|
||||
|
||||
bool isObject() const {
|
||||
return type() == VariantType::Object;
|
||||
}
|
||||
|
||||
bool isString() const {
|
||||
return data_ && data_->isString();
|
||||
}
|
||||
|
||||
size_t nesting() {
|
||||
return asCollection().nesting();
|
||||
}
|
||||
|
||||
void removeElement(size_t index) {
|
||||
asArray().removeElement(index);
|
||||
}
|
||||
|
||||
template <typename TAdaptedString>
|
||||
void removeMember(TAdaptedString key) {
|
||||
asObject().removeMember(key);
|
||||
}
|
||||
|
||||
bool setBoolean(bool value) {
|
||||
if (!data_)
|
||||
return false;
|
||||
data_->setBoolean(value);
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
enable_if_t<sizeof(T) == 4, bool> setFloat(T value) {
|
||||
ARDUINOJSON_ASSERT(type() == VariantType::Null); // must call clear() first
|
||||
if (!data_)
|
||||
return false;
|
||||
data_->type = VariantType::Float;
|
||||
data_->content.asFloat = value;
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
enable_if_t<sizeof(T) == 8, bool> setFloat(T value) {
|
||||
ARDUINOJSON_ASSERT(isNull()); // must call clear() first
|
||||
|
||||
if (!data_)
|
||||
return false;
|
||||
|
||||
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>
|
||||
enable_if_t<is_signed<T>::value, bool> setInteger(T value) {
|
||||
ARDUINOJSON_ASSERT(isNull()); // must call clear() first
|
||||
|
||||
if (!data_)
|
||||
return false;
|
||||
|
||||
if (canConvertNumber<int32_t>(value)) {
|
||||
data_->type = VariantType::Int32;
|
||||
data_->content.asInt32 = static_cast<int32_t>(value);
|
||||
}
|
||||
#if ARDUINOJSON_USE_LONG_LONG
|
||||
else {
|
||||
auto slot = resources_->allocEightByte();
|
||||
if (!slot)
|
||||
return false;
|
||||
data_->type = VariantType::Int64;
|
||||
data_->content.asSlotId = slot.id();
|
||||
slot->asInt64 = value;
|
||||
}
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
enable_if_t<is_unsigned<T>::value, bool> setInteger(T value) {
|
||||
ARDUINOJSON_ASSERT(isNull()); // must call clear() first
|
||||
|
||||
if (!data_)
|
||||
return false;
|
||||
|
||||
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;
|
||||
}
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void setRawString(SerializedValue<T> value);
|
||||
|
||||
template <typename TAdaptedString>
|
||||
bool setString(TAdaptedString value);
|
||||
|
||||
bool setLinkedString(const char* s);
|
||||
|
||||
size_t size() {
|
||||
if (isObject())
|
||||
return asObject().size();
|
||||
|
||||
if (isArray())
|
||||
return asArray().size();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
ArrayImpl toArray() {
|
||||
ARDUINOJSON_ASSERT(type() == VariantType::Null); // must call clear() first
|
||||
if (!data_)
|
||||
return ArrayImpl();
|
||||
data_->type = VariantType::Array;
|
||||
return ArrayImpl(new (&data_->content.asCollection) CollectionData(),
|
||||
resources_);
|
||||
}
|
||||
|
||||
ObjectImpl toObject() {
|
||||
ARDUINOJSON_ASSERT(type() == VariantType::Null); // must call clear() first
|
||||
if (!data_)
|
||||
return ObjectImpl();
|
||||
data_->type = VariantType::Object;
|
||||
return ObjectImpl(new (&data_->content.asCollection) CollectionData(),
|
||||
resources_);
|
||||
}
|
||||
|
||||
VariantType type() const {
|
||||
return data_ ? data_->type : VariantType::Null;
|
||||
}
|
||||
|
||||
// Release the resources used by this variant and set it to null.
|
||||
void clear();
|
||||
|
||||
private:
|
||||
VariantData* data_;
|
||||
ResourceManager* resources_;
|
||||
};
|
||||
|
||||
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()));
|
||||
inline void VariantImpl::setRawString(SerializedValue<T> value) {
|
||||
if (!data_)
|
||||
return;
|
||||
auto dup = resources_->saveString(adaptString(value.data(), value.size()));
|
||||
if (dup)
|
||||
setRawString(dup);
|
||||
data_->setRawString(dup);
|
||||
}
|
||||
|
||||
inline bool VariantImpl::setLinkedString(const char* s) {
|
||||
ARDUINOJSON_ASSERT(isNull()); // must call clear() first
|
||||
ARDUINOJSON_ASSERT(s);
|
||||
|
||||
auto slotId = resources_->saveStaticString(s);
|
||||
if (slotId == NULL_SLOT)
|
||||
return false;
|
||||
|
||||
data_->type = VariantType::LinkedString;
|
||||
data_->content.asSlotId = slotId;
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename TAdaptedString>
|
||||
inline bool VariantData::setString(TAdaptedString value,
|
||||
ResourceManager* resources) {
|
||||
ARDUINOJSON_ASSERT(type_ == VariantType::Null); // must call clear() first
|
||||
inline bool VariantImpl::setString(TAdaptedString value) {
|
||||
ARDUINOJSON_ASSERT(isNull()); // must call clear() first
|
||||
|
||||
if (!data_)
|
||||
return false;
|
||||
|
||||
if (value.isNull())
|
||||
return false;
|
||||
|
||||
if (value.isStatic()) {
|
||||
setLinkedString(value.data());
|
||||
if (value.isStatic())
|
||||
return setLinkedString(value.data());
|
||||
|
||||
if (isTinyString(value, value.size())) {
|
||||
data_->setTinyString(value);
|
||||
return true;
|
||||
}
|
||||
|
||||
auto dup = resources->saveString(value);
|
||||
auto dup = resources_->saveString(value);
|
||||
if (dup) {
|
||||
setOwnedString(dup);
|
||||
data_->setOwnedString(dup);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
inline void VariantData::clear(ResourceManager* resources) {
|
||||
if (type_ & VariantTypeBits::OwnedStringBit)
|
||||
resources->dereferenceString(content_.asOwnedString->data);
|
||||
inline void VariantImpl::clear() {
|
||||
if (!data_)
|
||||
return;
|
||||
|
||||
#if ARDUINOJSON_USE_EXTENSIONS
|
||||
if (type_ & VariantTypeBits::ExtensionBit)
|
||||
resources->freeExtension(content_.asSlotId);
|
||||
if (data_->type & VariantTypeBits::OwnedStringBit)
|
||||
resources_->dereferenceString(data_->content.asOwnedString->data);
|
||||
|
||||
#if ARDUINOJSON_USE_8_BYTE_POOL
|
||||
if (data_->type & VariantTypeBits::EightByteBit)
|
||||
resources_->freeEightByte(data_->content.asSlotId);
|
||||
#endif
|
||||
|
||||
auto collection = asCollection();
|
||||
if (collection)
|
||||
collection->clear(resources);
|
||||
asCollection().clear();
|
||||
|
||||
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);
|
||||
|
||||
#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;
|
||||
}
|
||||
#else
|
||||
type_ = VariantType::Float;
|
||||
content_.asFloat = valueAsFloat;
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
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;
|
||||
}
|
||||
#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);
|
||||
}
|
||||
#if ARDUINOJSON_USE_LONG_LONG
|
||||
else {
|
||||
auto extension = resources->allocExtension();
|
||||
if (!extension)
|
||||
return false;
|
||||
type_ = VariantType::Uint64;
|
||||
content_.asSlotId = extension.id();
|
||||
extension->asUint64 = value;
|
||||
}
|
||||
#endif
|
||||
return true;
|
||||
data_->type = VariantType::Null;
|
||||
}
|
||||
|
||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||
|
@ -29,13 +29,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.
|
||||
@ -93,13 +93,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 +120,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 getOrCreateVariantImpl().addValue(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>
|
||||
bool add(T* value) const {
|
||||
return detail::VariantData::addValue(getOrCreateData(), value,
|
||||
getResourceManager());
|
||||
return getOrCreateVariantImpl().addValue(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.
|
||||
@ -275,6 +271,14 @@ class VariantRefBase : public VariantTag {
|
||||
return VariantAttorney::getOrCreateData(derived());
|
||||
}
|
||||
|
||||
VariantImpl getVariantImpl() const {
|
||||
return VariantImpl(getData(), getResourceManager());
|
||||
}
|
||||
|
||||
VariantImpl getOrCreateVariantImpl() const {
|
||||
return VariantImpl(getOrCreateData(), getResourceManager());
|
||||
}
|
||||
|
||||
FORCE_INLINE ArduinoJson::JsonVariant getVariant() const;
|
||||
|
||||
FORCE_INLINE ArduinoJson::JsonVariantConst getVariantConst() const {
|
||||
|
@ -69,23 +69,20 @@ inline void convertToJson(const VariantRefBase<TDerived>& src,
|
||||
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 JsonVariant(getOrCreateVariantImpl().addElement(),
|
||||
getResourceManager());
|
||||
}
|
||||
|
||||
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>
|
||||
@ -150,26 +147,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 variant = getOrCreateVariantImpl();
|
||||
variant.clear();
|
||||
return JsonArray(variant.toArray());
|
||||
}
|
||||
|
||||
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 variant = getOrCreateVariantImpl();
|
||||
variant.clear();
|
||||
return JsonObject(variant.toObject());
|
||||
}
|
||||
|
||||
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);
|
||||
detail::VariantImpl impl(getOrCreateData(), getResourceManager());
|
||||
impl.clear();
|
||||
return JsonVariant(impl);
|
||||
}
|
||||
|
||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||
|
@ -4,8 +4,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#define ARDUINOJSON_VERSION "7.3.1"
|
||||
#define ARDUINOJSON_VERSION "7.4.2"
|
||||
#define ARDUINOJSON_VERSION_MAJOR 7
|
||||
#define ARDUINOJSON_VERSION_MINOR 3
|
||||
#define ARDUINOJSON_VERSION_REVISION 1
|
||||
#define ARDUINOJSON_VERSION_MACRO V731
|
||||
#define ARDUINOJSON_VERSION_MINOR 4
|
||||
#define ARDUINOJSON_VERSION_REVISION 2
|
||||
#define ARDUINOJSON_VERSION_MACRO V742
|
||||
|
Reference in New Issue
Block a user