Compare commits

..

103 Commits

Author SHA1 Message Date
cd4b2b2463 Set version to 7.2.0 2024-09-18 10:48:50 +02:00
f806a42cc2 Add support for escape sequence \'
Fixes #2124
2024-09-17 10:33:47 +02:00
c1a507c158 Set ARDUINOJSON_POOL_CAPACITY from ARDUINOJSON_SLOT_ID_SIZE
This avoids the integer overflow when `ARDUINOJSON_SLOT_ID_SIZE=1` on 64-bit architectures.
2024-09-11 21:08:51 +02:00
a1809d0f31 Replace sizeof(VariantData) with sizeof(SlotData) 2024-09-10 18:26:58 +02:00
d92eee8736 Deprecate containsKey() in favor of doc["key"].is<T>()
See #2121
2024-09-06 17:32:09 +02:00
dd1d96e28f Group calls to getExtension()
This slightly reduces the code size.
2024-09-05 12:15:10 +02:00
3b64197869 MsgPackDeserializer: check extension allocation result 2024-09-04 14:34:02 +02:00
1f7a3f3174 JsonDeserializer: use float when the value has few digits 2024-09-04 14:33:14 +02:00
fd6314e132 Move some numbers tests to use_double_0.cpp 2024-09-04 14:29:19 +02:00
e4e2557b76 Move test of decomposeFloat() 2024-09-03 13:27:39 +02:00
3b6bf45b8a Serialize float with less decimal places than double 2024-09-03 11:44:35 +02:00
65ba36622c Add VariantType 2024-09-02 17:56:19 +02:00
33452c1f37 Test JsonVariant::as<T>() with extension slots 2024-08-27 16:08:47 +02:00
ee02c0d573 Test extension slot allocation failure 2024-08-27 16:08:47 +02:00
0278e94fce Set ARDUINOJSON_USE_DOUBLE to 0 by default on 8-bit architectures 2024-08-27 15:46:12 +02:00
7643dadaec WIP 2024-08-27 14:40:24 +02:00
c0bebe35f1 Improve message when user forgets third arg of serializeJson() et al.
See #1449, #1491, #1543, #1665, and #2122
2024-08-27 12:22:53 +02:00
b5bcb37657 Use enable_if_t instead of enable_if 2024-08-27 10:00:09 +02:00
b4a5b053ca Merge conf_test for linux and windows
This reverts commit 83516e1740.
2024-08-27 08:03:55 +02:00
e297932a98 Store 64-bit numbers (double and long long) in an additional slot
This change allows slots to be twices maller on 32-bit architectures.
See #1650 and #2103
2024-08-27 08:02:09 +02:00
e682337655 Release VariantData resources explicitly before setting value 2024-08-26 15:09:56 +02:00
4ada3f849c Decouple parseNumber() from VariantData 2024-08-25 16:46:55 +02:00
5dd203bca4 Rename SlotWithId to Slot 2024-08-25 15:04:39 +02:00
362201241f Make MemoryPool generic 2024-08-25 14:58:23 +02:00
2be24eded8 Rename SlotWithId::slot() and VariantWithId::data() to ptr() 2024-08-25 14:54:45 +02:00
4327f72140 Remove VariantSlot 2024-08-25 14:45:59 +02:00
f7f1b9745d Hide FreeSlot in MemoryPoolList 2024-08-25 14:39:18 +02:00
cec18177b0 Move sizeofArray() and sizeofObject() 2024-08-25 14:36:38 +02:00
f2894552f2 Rename VariantPool to MemoryPool 2024-08-25 14:36:38 +02:00
d3721cb122 Make VariantSlot a union. Include next slot id in VariantData 2024-08-24 19:20:39 +02:00
ab72bb8601 Rename flags_ to type_ 2024-08-24 11:18:21 +02:00
09c89dcacf Store object members with two slots: one for the key and one for the value 2024-08-24 10:45:51 +02:00
a2b09bfbd2 Remove unused code 2024-08-23 15:28:44 +02:00
386105be90 Allocate slot before key 2024-08-23 15:27:46 +02:00
83516e1740 Split conf_test between linux and windows 2024-08-23 15:24:45 +02:00
bf99aeedb1 Add @LArkema to the list of sponsors 💖 2024-07-02 10:27:36 +02:00
22dd4da3af Set version to 7.1.0 2024-06-27 18:20:22 +02:00
55c3b9b3a7 CI: update macOS runner to macos-13 2024-06-18 13:51:40 +02:00
d83515dcda Add DevContainer files for Clang 13 to 17 2024-06-18 13:51:40 +02:00
e34c27723a Fix build on modern compilers 2024-06-18 13:51:40 +02:00
b23ff65b99 Reduce MsgPack deserializer size 2024-06-18 13:51:33 +02:00
556785dc1e Read MsgPack's 64-bit ints even if ARDUINOJSON_USE_LONG_LONG is 0 2024-06-18 13:50:50 +02:00
e9c87858d1 Remove files.associations from vscode settings 2024-06-18 09:31:39 +02:00
208e7a3304 Fix integer overflow in MsgPackDeserializer 2024-06-11 09:47:11 +02:00
45611924f3 Tests: add user-defined literal ""_s for std::string 2024-06-07 09:35:45 +02:00
5b88b2c1f6 Update GitHub actions 2024-06-06 18:43:10 +02:00
e4f3fd8c91 Add support for MsgPack extension 2024-06-06 18:33:48 +02:00
aec642be20 Rename tests in MixedConfiguration 2024-06-06 18:26:16 +02:00
5a60c55be7 Don't add partial objects when allocation fails
Fixes #2081
2024-05-23 18:36:24 +02:00
f99b2d63f9 Make error message more readable in case of an invalid conversion 2024-05-23 14:01:30 +02:00
1db803bcd3 Add helpers for type traits, such as enable_if_t 2024-05-22 10:22:59 +02:00
04326d2655 Allow Converter<T>::toJson() to return a boolean as an optimization 2024-05-22 09:27:15 +02:00
9e0c56acc3 Replace ConverterNeedsWriteableRef with function_traits 2024-05-22 09:27:14 +02:00
60f9f7eff6 Move CollectionData::releaseSlot() to ResourceManager::freeSlot() 2024-05-17 17:10:45 +02:00
0fe202af03 CI: don't build fuzzers with Clang 11
Clang 11 fails on GitHub Actions with the following error:
ERROR: UndefinedBehaviorSanitizer failed to allocate 0x0 (0) bytes of SetAlternateSignalStack (error code: 22)
Sanitizer CHECK failed: /build/llvm-toolchain-11-mnvtwk/llvm-toolchain-11-11.1.0/compiler-rt/lib/sanitizer_common/sanitizer_common.cpp:54 ((0 && "unable to mmap")) != (0) (0, 0)
2024-05-16 17:47:07 +02:00
c41e8cc634 Add dev container for Clang 11 2024-05-16 17:47:07 +02:00
d486157a9e CMake: fix detection of GCC 4.8 2024-05-16 17:47:07 +02:00
91aad55412 CI: set cancel-in-progress to true 2024-05-16 17:47:07 +02:00
98fca74f66 Fix undefined reference to VariantPool::usage() 2024-05-16 17:47:07 +02:00
60fb268d9a Fix UBSAN error "member call does not point to an object of type"
runtime error: member call on address XXX which does not point to an object of type 'Allocator'
XXX: note: object is of type 'SpyingAllocator'

This is due to the fact that some of the compilation units have different library settings, so a different namespace, and therefore a different `Allocator` class.
2024-05-16 17:47:07 +02:00
f1899d3049 Fix error resources_ may be used uninitialized
JsonArrayConst.hpp:29:56: error: 'array.ArduinoJson::V704HB22::JsonArrayConst::resources_' may be used uninitialized in this function [-Werror=maybe-uninitialized]
JsonObjectConst.hpp:62:56: error: 'null.ArduinoJson::V704HB22::JsonObjectConst::resources_' may be used uninitialized in this function [-Werror=maybe-uninitialized]
2024-05-16 17:47:07 +02:00
4b779a7c1d Fix wrong build flags when COVERAGE is not defined 2024-05-16 17:47:07 +02:00
5d1aa04e21 CI: add Valgrind log to step summary 2024-05-16 17:47:07 +02:00
2f6db1edb0 CI: remove step summary
CTest errors were ignored in favor or `tee`'s exit code
2024-05-16 17:47:06 +02:00
aeb30ef307 Support ElementProxy and MemberProxy in JsonDocument's constructor 2024-05-15 13:37:33 +02:00
1c5e5db071 Test JsonDocument constructor with a JsonVariantConst argument
See #2091
2024-05-15 09:30:22 +02:00
68a13117dc Allow using a JsonVariant as a key or index
Closes #2080
2024-05-14 21:06:02 +02:00
071f718473 MsgPack: test failing string allocation 2024-05-06 11:23:20 +02:00
00949f8276 Remove redundant test 2024-05-06 11:16:55 +02:00
2c670e0148 Implement MsgPackBinary using raw strings and converters 2024-05-06 11:10:02 +02:00
002b07f0c5 Reduce the size of deserializeMsgPack() 2024-05-02 20:29:00 +02:00
5f5f927693 Add tests of as<MsgPackBinary>() 2024-05-01 19:26:19 +02:00
4d074840da Optimize deserializeMsgPack() 2024-05-01 18:00:59 +02:00
82de20ee14 Make string support even more generic
Ported from 57354de831
Closes #2084
2024-05-01 14:06:30 +02:00
ece4d030a8 CI: update runner for the lint job 2024-04-29 19:38:18 +02:00
4d5c17b5f6 Remove unused #include statements 2024-04-29 18:30:29 +02:00
18a9a5b590 Add MsgPack bin8/bin16/bin32 support
Closes #2078
Closes #922
2024-04-29 16:01:10 +02:00
cd4bf33132 Fix error "pasting X and Y does not give a valid preprocessing token" 2024-04-18 14:39:05 +02:00
2cfefe22ac Add ARDUINOJSON_STRING_LENGTH_SIZE to the namespace name 2024-04-18 14:19:43 +02:00
0d43e51d48 Fix typo in "endianness"
Closes #2071
2024-03-26 13:43:07 +01:00
ef28064317 CI: merge the two "amalgamate" jobs 2024-03-18 10:54:36 +01:00
7c62cdb264 CI: update GitHub Actions 2024-03-17 21:30:42 +01:00
0ab4bdd691 CI: add a workaround for actions/runner-images#9491 2024-03-17 21:30:42 +01:00
af8c615266 Rename tests/JsonObject/copy.cpp to set.cpp 2024-03-16 14:02:56 +01:00
bd13375729 CI: add timeouts in the gcc job 2024-03-14 10:27:09 +01:00
5f8502ce9d Upgrade to lock-threads version 5+ 2024-03-14 09:51:44 +01:00
1404b1ef70 Add a workaround for particle-iot/particle-cli#716 2024-03-12 10:02:30 +01:00
36e1eecc7d Set version to 7.0.4 2024-03-12 09:40:46 +01:00
ca2f80aeaf Add links to the documentation 2024-02-21 10:48:50 +01:00
f17fc055d3 Make JSON_STRING_SIZE(N) return N+1 to fix third-party code
ThingsBoard uses this macro to compute size of char arrays ಠ_ಠ
https://github.com/thingsboard/thingsboard-client-sdk/blob/v0.12.2/src/Helper.h#L38

Closes #2054
2024-02-18 20:00:25 +01:00
04ac53d114 Remove final from JsonArray, JsonObject, and JsonVariant
`final` breaks ThingsBoard ಠ_ಠ
https://github.com/thingsboard/thingsboard-client-sdk/blob/v0.12.2/src/RPC_Response.h#L10

See #2056.
This reverts commit 483a2c9101.
2024-02-18 20:00:24 +01:00
483a2c9101 Mark JsonArray, JsonObject, and JsonVariant as final
See #2056
2024-02-18 10:52:07 +01:00
848c0cdc7e Set version to 7.0.3 2024-02-05 13:34:38 +01:00
46a807bd30 Remove unused NO_INLINE 2024-02-05 11:56:08 +01:00
cb0dc94db4 Remove most FORCE_INLINEs
I kept only the ones that had a positive impact on code size AND no negative impact on stack memory.

Fixes #2046
2024-02-05 11:48:27 +01:00
72642e3090 Fix compatibility with GCC 4.8
Closes #2045
2024-02-01 21:37:45 +01:00
c98b05e207 Dont't inline JsonSerializer::visit(const ArrayData&)
It didn't improve the code size and was inconsistent with this other functions.
2024-02-01 10:24:01 +01:00
2a87cc5839 Stop using CollectionIterator in MsgPackSerializer
This doesn't reduce code size or stack usage, but as least it's consistent with `JsonSerializer`.
2024-02-01 10:24:01 +01:00
296fe79bfd Stop using CollectionIterator in JsonSerializer
This reduces stack consumption and code size.
See  #2046
2024-02-01 10:24:00 +01:00
650d537b5d Improve error messages when using char or char*
See #2043
2024-01-26 10:34:49 +01:00
0435945a62 Set version to 7.0.2 2024-01-19 14:36:16 +01:00
844a50296f Fix link to shrinkToFit()'s documentation 2024-01-16 14:29:08 +01:00
1b143d383b Fix assertion poolIndex < count_ after JsonDocument::clear()
Fixes #2034
2024-01-16 10:10:43 +01:00
218 changed files with 5633 additions and 3188 deletions

View File

@ -0,0 +1,18 @@
{
"name": "Clang 11",
"image": "conanio/clang11",
"runArgs": [
"--name=ArduinoJson-clang11"
],
"customizations": {
"vscode": {
"extensions": [
"ms-vscode.cmake-tools"
],
"settings": {
"cmake.generator": "Unix Makefiles",
"cmake.buildDirectory": "/tmp/build"
}
}
}
}

View File

@ -0,0 +1,5 @@
FROM ubuntu:22.04
RUN apt-get update
RUN apt-get install -y cmake git clang-13 libc++-13-dev libc++abi-13-dev
ENV CC=clang-13 CXX=clang++-13

View File

@ -0,0 +1,20 @@
{
"name": "Clang 13",
"build": {
"dockerfile": "Dockerfile"
},
"runArgs": [
"--name=ArduinoJson-clang13"
],
"customizations": {
"vscode": {
"extensions": [
"ms-vscode.cmake-tools"
],
"settings": {
"cmake.generator": "Unix Makefiles",
"cmake.buildDirectory": "/tmp/build"
}
}
}
}

View File

@ -0,0 +1,5 @@
FROM ubuntu:22.04
RUN apt-get update
RUN apt-get install -y cmake git clang-14 libc++-14-dev libc++abi-14-dev
ENV CC=clang-14 CXX=clang++-14

View File

@ -0,0 +1,20 @@
{
"name": "Clang 14",
"build": {
"dockerfile": "Dockerfile"
},
"runArgs": [
"--name=ArduinoJson-clang14"
],
"customizations": {
"vscode": {
"extensions": [
"ms-vscode.cmake-tools"
],
"settings": {
"cmake.generator": "Unix Makefiles",
"cmake.buildDirectory": "/tmp/build"
}
}
}
}

View File

@ -0,0 +1,5 @@
FROM ubuntu:22.04
RUN apt-get update
RUN apt-get install -y cmake git clang-15 libc++-15-dev libc++abi-15-dev
ENV CC=clang-15 CXX=clang++-15

View File

@ -0,0 +1,20 @@
{
"name": "Clang 15",
"build": {
"dockerfile": "Dockerfile"
},
"runArgs": [
"--name=ArduinoJson-clang15"
],
"customizations": {
"vscode": {
"extensions": [
"ms-vscode.cmake-tools"
],
"settings": {
"cmake.generator": "Unix Makefiles",
"cmake.buildDirectory": "/tmp/build"
}
}
}
}

View File

@ -0,0 +1,5 @@
FROM ubuntu:22.04
RUN apt-get update
RUN apt-get install -y cmake git clang-16 libc++-16-dev libc++abi-16-dev
ENV CC=clang-16 CXX=clang++-16

View File

@ -0,0 +1,20 @@
{
"name": "Clang 16",
"build": {
"dockerfile": "Dockerfile"
},
"runArgs": [
"--name=ArduinoJson-clang16"
],
"customizations": {
"vscode": {
"extensions": [
"ms-vscode.cmake-tools"
],
"settings": {
"cmake.generator": "Unix Makefiles",
"cmake.buildDirectory": "/tmp/build"
}
}
}
}

View File

@ -0,0 +1,5 @@
FROM ubuntu:24.04
RUN apt-get update
RUN apt-get install -y cmake git clang-17 libc++-17-dev libc++abi-17-dev
ENV CC=clang-17 CXX=clang++-17

View File

@ -0,0 +1,20 @@
{
"name": "Clang 17",
"build": {
"dockerfile": "Dockerfile"
},
"runArgs": [
"--name=ArduinoJson-clang17"
],
"customizations": {
"vscode": {
"extensions": [
"ms-vscode.cmake-tools"
],
"settings": {
"cmake.generator": "Unix Makefiles",
"cmake.buildDirectory": "/tmp/build"
}
}
}
}

View File

@ -0,0 +1,20 @@
{
"name": "GCC 4.8",
"image": "conanio/gcc48",
"runArgs": [
"--name=ArduinoJson-gcc48"
],
"customizations": {
"vscode": {
"extensions": [
"ms-vscode.cmake-tools",
"josetr.cmake-language-support-vscode",
"ms-vscode.cpptools"
],
"settings": {
"cmake.generator": "Unix Makefiles",
"cmake.buildDirectory": "/tmp/build"
}
}
}
}

View File

@ -2,15 +2,19 @@ name: Continuous Integration
on: [push, pull_request]
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
lint:
name: Lint
runs-on: ubuntu-20.04
runs-on: ubuntu-22.04
steps:
- name: Install
run: sudo apt-get install -y clang-format
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Symlinks
run: find * -type l -printf "::error::%p is a symlink. This is forbidden by the Arduino Library Specification." -exec false {} +
- name: Clang-format
@ -33,6 +37,7 @@ jobs:
fail-fast: false
matrix:
include:
- gcc: "4.8"
- gcc: "5"
- gcc: "6"
- gcc: "7"
@ -46,31 +51,40 @@ jobs:
- gcc: "11"
- gcc: "12"
steps:
- name: Workaround for actions/runner-images#9491
run: sudo sysctl vm.mmap_rnd_bits=28
- name: Install
run: |
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 40976EAF437D05B5 3B4FE6ACC0B21F32
sudo add-apt-repository -yn 'deb http://archive.ubuntu.com/ubuntu/ xenial main universe'
sudo add-apt-repository -yn 'deb http://archive.ubuntu.com/ubuntu/ bionic main universe'
sudo add-apt-repository -yn 'deb http://archive.ubuntu.com/ubuntu/ focal main universe'
sudo apt-get update
sudo apt-get install -y gcc-${{ matrix.gcc }} g++-${{ matrix.gcc }}
timeout-minutes: 5
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
timeout-minutes: 1
- name: Configure
run: cmake -DCMAKE_BUILD_TYPE=Debug .
env:
CC: gcc-${{ matrix.gcc }}
CXX: g++-${{ matrix.gcc }}
CXXFLAGS: ${{ matrix.cxxflags }}
timeout-minutes: 1
- name: Build
run: cmake --build .
timeout-minutes: 10
- name: Test
run: |
echo "## CTest output" >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
ctest --output-on-failure -C Debug . | tee -a $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
run: ctest --output-on-failure -C Debug .
env:
UBSAN_OPTIONS: print_stacktrace=1
timeout-minutes: 2
clang:
name: Clang
@ -130,7 +144,7 @@ jobs:
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@v3
uses: actions/checkout@v4
- name: Configure
run: cmake -DCMAKE_BUILD_TYPE=Debug .
env:
@ -142,11 +156,7 @@ jobs:
- name: Build
run: cmake --build .
- name: Test
run: |
echo "## CTest output" >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
ctest --output-on-failure -C Debug . | tee -a $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
run: ctest --output-on-failure -C Debug .
env:
UBSAN_OPTIONS: print_stacktrace=1
@ -160,7 +170,7 @@ jobs:
sudo apt-get update
sudo apt-get install -y g++-multilib gcc-avr avr-libc
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: AVR
run: avr-g++ -std=c++11 -Isrc extras/conf_test/avr.cpp
- name: GCC 32-bit
@ -178,7 +188,7 @@ jobs:
needs: [gcc, clang]
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: 32-bit
run: |
call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvars32.bat"
@ -193,17 +203,20 @@ jobs:
xcode:
name: XCode
needs: clang
runs-on: macos-11
runs-on: macos-13
strategy:
fail-fast: false
matrix:
include:
- xcode: "11.7"
- xcode: "12.4"
- xcode: "13.2.1"
- xcode: "14.1"
- xcode: "14.2"
- xcode: "14.3.1"
- xcode: "15.0.1"
- xcode: "15.1"
- xcode: "15.2"
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Select XCode version
run: sudo xcode-select --switch /Applications/Xcode_${{ matrix.xcode }}.app
- name: Configure
@ -225,7 +238,7 @@ jobs:
# runs-on: ${{ matrix.os }}
# steps:
# - name: Checkout
# uses: actions/checkout@v3
# uses: actions/checkout@v4
# - name: Configure
# run: cmake -DCMAKE_BUILD_TYPE=Debug .
# - name: Build
@ -247,7 +260,7 @@ jobs:
runs-on: ubuntu-20.04
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Install arduino-cli
run: curl -fsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | BINDIR=/usr/local/bin sh
- name: Install core
@ -320,14 +333,14 @@ jobs:
conf_test: esp8266
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Set up cache for pip
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip
- name: Set up Python 3.x
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: "3.x"
- name: Install PlatformIO
@ -339,7 +352,7 @@ jobs:
if: ${{ matrix.platform == 'nordicnrf52' }}
run: find examples/ -name '*.ino' -exec sed -i 's/\(#include <ArduinoJson.h>\)/\1\n#include <Adafruit_TinyUSB.h>/' {} +
- name: Set up cache for platformio
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ~/.platformio
key: ${{ runner.os }}-platformio-${{ matrix.platform }}
@ -388,9 +401,9 @@ jobs:
- board: argon
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Install Particle CLI
run: sudo npm install -g particle-cli
run: sudo npm install -g particle-cli particle-usb
- name: Login to Particle
run: particle login -t "${{ secrets.PARTICLE_TOKEN }}"
- name: Compile
@ -406,7 +419,7 @@ jobs:
sudo apt-get update
sudo apt-get install -y g++-arm-linux-gnueabihf
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Configure
run: cmake .
env:
@ -423,7 +436,7 @@ jobs:
- name: Install
run: sudo apt-get install -y lcov ninja-build
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Configure
run: cmake -G Ninja -DCOVERAGE=true .
- name: Build
@ -437,12 +450,12 @@ jobs:
- name: genhtml
run: mkdir coverage && genhtml coverage_filtered.info -o coverage -t ArduinoJson
- name: Upload HTML report
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: Coverage report
path: coverage
- name: Upload to Coveralls
uses: coverallsapp/github-action@master
uses: coverallsapp/github-action@v2
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
path-to-lcov: coverage_filtered.info
@ -457,7 +470,7 @@ jobs:
sudo apt-get update
sudo apt-get install -y valgrind ninja-build
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Configure
run: cmake -G Ninja -D MEMORYCHECK_COMMAND_OPTIONS="--error-exitcode=1 --leak-check=full" .
- name: Build
@ -466,7 +479,7 @@ jobs:
run: ctest --output-on-failure -LE WillFail -T memcheck
id: memcheck
- name: MemoryChecker.*.log
run: cat Testing/Temporary/MemoryChecker.*.log
run: cat Testing/Temporary/MemoryChecker.*.log > $GITHUB_STEP_SUMMARY
if: failure()
clang-tidy:
@ -477,7 +490,7 @@ jobs:
- name: Install
run: sudo apt-get install -y clang-tidy cmake ninja-build
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Configure
run: cmake -G Ninja -DCMAKE_CXX_CLANG_TIDY="clang-tidy-10;--warnings-as-errors=*" -DCMAKE_BUILD_TYPE=Debug .
env:
@ -486,73 +499,51 @@ jobs:
- name: Check
run: cmake --build . -- -k 0
amalgamate-h:
amalgamate:
needs: gcc
name: Amalgamate ArduinoJson.h
runs-on: ubuntu-20.04
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Amalgamate
id: amalgamate
uses: actions/checkout@v4
- name: Setup
run: |
if [[ $GITHUB_REF == refs/tags/* ]]; then
VERSION=${GITHUB_REF#refs/tags/}
else
VERSION=${GITHUB_SHA::7}
fi
INPUT=src/ArduinoJson.h
OUTPUT=ArduinoJson-$VERSION.h
extras/scripts/build-single-header.sh "$INPUT" "$OUTPUT"
echo "filename=${OUTPUT}" >> $GITHUB_OUTPUT
- name: Smoke test
echo "ARDUINOJSON_H=ArduinoJson-$VERSION.h" >> $GITHUB_ENV
echo "ARDUINOJSON_HPP=ArduinoJson-$VERSION.hpp" >> $GITHUB_ENV
- name: Amalgamate ArduinoJson.h
run: extras/scripts/build-single-header.sh "src/ArduinoJson.h" "$ARDUINOJSON_H"
- name: Amalgamate ArduinoJson.hpp
run: extras/scripts/build-single-header.sh "src/ArduinoJson.hpp" "$ARDUINOJSON_HPP"
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: Single headers
path: |
${{ env.ARDUINOJSON_H }}
${{ env.ARDUINOJSON_HPP }}
- name: Smoke test ArduinoJson.h
run: |
g++ -x c++ - <<END
#include "${{ steps.amalgamate.outputs.filename }}"
#include "$ARDUINOJSON_H"
int main() {
JsonDocument doc;
deserializeJson(doc, "{}");
}
END
- name: Upload artifact
uses: actions/upload-artifact@v3
with:
name: Single headers
path: ${{ steps.amalgamate.outputs.filename }}
amalgamate-hpp:
needs: gcc
name: Amalgamate ArduinoJson.hpp
runs-on: ubuntu-20.04
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Amalgamate
id: amalgamate
run: |
if [[ $GITHUB_REF == refs/tags/* ]]; then
VERSION=${GITHUB_REF#refs/tags/}
else
VERSION=${GITHUB_SHA::7}
fi
INPUT=src/ArduinoJson.hpp
OUTPUT=ArduinoJson-$VERSION.hpp
extras/scripts/build-single-header.sh "$INPUT" "$OUTPUT"
echo "filename=${OUTPUT}" >> $GITHUB_OUTPUT
- name: Smoke test
- name: Smoke test ArduinoJson.hpp
run: |
g++ -x c++ - <<END
#include "${{ steps.amalgamate.outputs.filename }}"
#include "$ARDUINOJSON_HPP"
int main() {
ArduinoJson::JsonDocument doc;
deserializeJson(doc, "{}");
}
END
- name: Upload artifact
uses: actions/upload-artifact@v3
with:
name: Single headers
path: ${{ steps.amalgamate.outputs.filename }}
esp-idf:
needs: gcc
@ -560,14 +551,14 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Setup cache
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ~/.espressif
key: ${{ runner.os }}-esp-idf
- name: Checkout ArduinoJson
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Checkout ESP-IDF
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
repository: espressif/esp-idf
path: esp-idf
@ -597,10 +588,10 @@ jobs:
steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
uses: github/codeql-action/init@v3
with:
languages: cpp
@ -610,6 +601,6 @@ jobs:
cmake --build .
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
uses: github/codeql-action/analyze@v3
with:
category: "/language:cpp"

View File

@ -8,7 +8,7 @@ jobs:
lock:
runs-on: ubuntu-latest
steps:
- uses: dessant/lock-threads@v4
- uses: dessant/lock-threads@v5
with:
github-token: ${{ github.token }}
issue-inactive-days: 30

View File

@ -16,7 +16,7 @@ jobs:
echo "tag=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
echo "version=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Write release body
id: body
run: |
@ -48,7 +48,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Upload component to the component registry
uses: espressif/upload-components-ci-action@v1
with:
@ -61,9 +61,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Install
run: npm install -g particle-cli
run: npm install -g particle-cli particle-usb
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Login
run: particle login --token ${{ secrets.PARTICLE_TOKEN }}
- name: Publish
@ -74,13 +74,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Set up Python 3.x
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: "3.x"
- name: Install PlatformIO
run: pip install platformio
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Publish
run: pio pkg publish --no-interactive --no-notify
env:

View File

@ -1,6 +1,74 @@
ArduinoJson: change log
=======================
v7.2.0 (2024-09-18)
------
* Store object members with two slots: one for the key and one for the value
* Store 64-bit numbers (`double` and `long long`) in an additional slot
* Reduce the slot size (see table below)
* Improve message when user forgets third arg of `serializeJson()` et al.
* Set `ARDUINOJSON_USE_DOUBLE` to `0` by default on 8-bit architectures
* Deprecate `containsKey()` in favor of `doc["key"].is<T>()`
* Add support for escape sequence `\'` (issue #2124)
| Architecture | before | after |
|--------------|----------|----------|
| 8-bit | 8 bytes | 6 bytes |
| 32-bit | 16 bytes | 8 bytes |
| 64-bit | 24 bytes | 16 bytes |
> ### BREAKING CHANGES
>
> After being on the death row for years, the `containsKey()` method has finally been deprecated.
> You should replace `doc.containsKey("key")` with `doc["key"].is<T>()`, which not only checks that the key exists but also that the value is of the expected type.
>
> ```cpp
> // Before
> if (doc.containsKey("value")) {
> int value = doc["value"];
> // ...
> }
>
> // After
> if (doc["value"].is<int>()) {
> int value = doc["value"];
> // ...
> }
> ```
v7.1.0 (2024-06-27)
------
* Add `ARDUINOJSON_STRING_LENGTH_SIZE` to the namespace name
* Add support for MsgPack binary (PR #2078 by @Sanae6)
* Add support for MsgPack extension
* Make string support even more generic (PR #2084 by @d-a-v)
* Optimize `deserializeMsgPack()`
* Allow using a `JsonVariant` as a key or index (issue #2080)
Note: works only for reading, not for writing
* Support `ElementProxy` and `MemberProxy` in `JsonDocument`'s constructor
* Don't add partial objects when allocation fails (issue #2081)
* Read MsgPack's 64-bit integers even if `ARDUINOJSON_USE_LONG_LONG` is `0`
(they are set to `null` if they don't fit in a `long`)
v7.0.4 (2024-03-12)
------
* Make `JSON_STRING_SIZE(N)` return `N+1` to fix third-party code (issue #2054)
v7.0.3 (2024-02-05)
------
* Improve error messages when using `char` or `char*` (issue #2043)
* Reduce stack consumption (issue #2046)
* Fix compatibility with GCC 4.8 (issue #2045)
v7.0.2 (2024-01-19)
------
* Fix assertion `poolIndex < count_` after `JsonDocument::clear()` (issue #2034)
v7.0.1 (2024-01-10)
------

View File

@ -10,7 +10,7 @@ if(ESP_PLATFORM)
return()
endif()
project(ArduinoJson VERSION 7.0.1)
project(ArduinoJson VERSION 7.2.0)
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
include(CTest)

View File

@ -79,7 +79,7 @@ ArduinoJson is a C++ JSON library for Arduino and IoT (Internet Of Things).
* [Unit test coverage close to 100%](https://coveralls.io/github/bblanchon/ArduinoJson?branch=7.x)
* Continuously tested on
* [Visual Studio 2017, 2019, 2022](https://ci.appveyor.com/project/bblanchon/arduinojson/branch/7.x)
* [GCC 5, 6, 7, 8, 9, 10, 11, 12](https://github.com/bblanchon/ArduinoJson/actions?query=workflow%3A%22Continuous+Integration%22)
* [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)
* [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/)
@ -148,6 +148,9 @@ ArduinoJson is thankful to its sponsors. Please give them a visit; they deserve
<a href="https://github.com/1technophile" rel="sponsored">
<img alt="1technophile" src="https://avatars.githubusercontent.com/u/12672732?s=40&v=4">
</a>
<a href="https://github.com/LArkema" rel="sponsored">
<img alt="LArkema" src="https://avatars.githubusercontent.com/u/38381313?s=40&v=4">
</a>
</p>
If you run a commercial project that embeds ArduinoJson, think about [sponsoring the library's development](https://github.com/sponsors/bblanchon): it ensures the code that your products rely on stays actively maintained. It can also give your project some exposure to the makers' community.

View File

@ -1,4 +1,4 @@
version: 7.0.1.{build}
version: 7.2.0.{build}
environment:
matrix:
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2022

View File

@ -1,3 +1,7 @@
if(NOT DEFINED COVERAGE)
set(COVERAGE OFF)
endif()
if(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Clang)")
add_compile_options(
-pedantic
@ -30,10 +34,20 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Clang)")
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
if((CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.8) AND(NOT ${COVERAGE}))
if((CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.9) AND(NOT ${COVERAGE}))
add_compile_options(-g -Og)
else()
add_compile_options(-g -O0)
else() # GCC 4.8
add_compile_options(
-g
-O0 # GCC 4.8 doesn't support -Og
-Wno-shadow # allow the same name for a function parameter and a member functions
-Wp,-w # Disable preprocessing warnings (see below)
)
# GCC 4.8 doesn't support __has_include, so we need to help him
add_definitions(
-DARDUINOJSON_ENABLE_STD_STRING=1
-DARDUINOJSON_ENABLE_STD_STREAM=1
)
endif()
add_compile_options(

View File

@ -6,12 +6,13 @@ static_assert(ARDUINOJSON_USE_LONG_LONG == 0, "ARDUINOJSON_USE_LONG_LONG");
static_assert(ARDUINOJSON_SLOT_ID_SIZE == 1, "ARDUINOJSON_SLOT_ID_SIZE");
static_assert(ARDUINOJSON_POOL_CAPACITY == 16, "ARDUINOJSON_POOL_CAPACITY");
static_assert(ARDUINOJSON_LITTLE_ENDIAN == 1, "ARDUINOJSON_LITTLE_ENDIAN");
static_assert(ARDUINOJSON_USE_DOUBLE == 1, "ARDUINOJSON_USE_DOUBLE");
static_assert(ARDUINOJSON_USE_DOUBLE == 0, "ARDUINOJSON_USE_DOUBLE");
static_assert(sizeof(ArduinoJson::detail::VariantSlot) == 8,
"sizeof(VariantSlot)");
static_assert(ArduinoJson::detail::ResourceManager::slotSize == 6, "slot size");
void setup() {}
void loop() {}

View File

@ -4,12 +4,13 @@ static_assert(ARDUINOJSON_USE_LONG_LONG == 1, "ARDUINOJSON_USE_LONG_LONG");
static_assert(ARDUINOJSON_SLOT_ID_SIZE == 2, "ARDUINOJSON_SLOT_ID_SIZE");
static_assert(ARDUINOJSON_POOL_CAPACITY == 128, "ARDUINOJSON_POOL_CAPACITY");
static_assert(ARDUINOJSON_LITTLE_ENDIAN == 1, "ARDUINOJSON_LITTLE_ENDIAN");
static_assert(ARDUINOJSON_USE_DOUBLE == 1, "ARDUINOJSON_USE_DOUBLE");
static_assert(sizeof(ArduinoJson::detail::VariantSlot) == 16,
"sizeof(VariantSlot)");
static_assert(ArduinoJson::detail::ResourceManager::slotSize == 8, "slot size");
void setup() {}
void loop() {}

View File

@ -4,11 +4,13 @@ static_assert(ARDUINOJSON_USE_LONG_LONG == 1, "ARDUINOJSON_USE_LONG_LONG");
static_assert(ARDUINOJSON_SLOT_ID_SIZE == 4, "ARDUINOJSON_SLOT_ID_SIZE");
static_assert(ARDUINOJSON_POOL_CAPACITY == 256, "ARDUINOJSON_POOL_CAPACITY");
static_assert(ARDUINOJSON_LITTLE_ENDIAN == 1, "ARDUINOJSON_LITTLE_ENDIAN");
static_assert(ARDUINOJSON_USE_DOUBLE == 1, "ARDUINOJSON_USE_DOUBLE");
static_assert(sizeof(ArduinoJson::detail::VariantSlot) == 24,
"sizeof(VariantSlot)");
static_assert(ArduinoJson::detail::ResourceManager::slotSize == 16,
"slot size");
int main() {}

View File

@ -4,11 +4,12 @@ static_assert(ARDUINOJSON_USE_LONG_LONG == 1, "ARDUINOJSON_USE_LONG_LONG");
static_assert(ARDUINOJSON_SLOT_ID_SIZE == 2, "ARDUINOJSON_SLOT_ID_SIZE");
static_assert(ARDUINOJSON_POOL_CAPACITY == 128, "ARDUINOJSON_POOL_CAPACITY");
static_assert(ARDUINOJSON_LITTLE_ENDIAN == 1, "ARDUINOJSON_LITTLE_ENDIAN");
static_assert(ARDUINOJSON_USE_DOUBLE == 1, "ARDUINOJSON_USE_DOUBLE");
static_assert(sizeof(ArduinoJson::detail::VariantSlot) == 16,
"sizeof(VariantSlot)");
static_assert(ArduinoJson::detail::ResourceManager::slotSize == 8, "slot size");
int main() {}

View File

@ -52,7 +52,16 @@ macro(add_fuzzer name)
)
endmacro()
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 6)
# Needs Clang 6+ to compile
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 6)
if(DEFINED ENV{GITHUB_ACTIONS} AND CMAKE_CXX_COMPILER_VERSION MATCHES "^11\\.")
# Clang 11 fails on GitHub Actions with the following error:
# > ERROR: UndefinedBehaviorSanitizer failed to allocate 0x0 (0) bytes of SetAlternateSignalStack (error code: 22)
# > Sanitizer CHECK failed: /build/llvm-toolchain-11-mnvtwk/llvm-toolchain-11-11.1.0/compiler-rt/lib/sanitizer_common/sanitizer_common.cpp:54 ((0 && "unable to mmap")) != (0) (0, 0)
message(WARNING "Fuzzing is disabled on GitHub Actions to workaround a bug in Clang 11")
return()
endif()
add_fuzzer(json)
add_fuzzer(msgpack)
endif()

View File

@ -12,6 +12,7 @@
#include <catch.hpp>
#include "Allocators.hpp"
#include "Literals.hpp"
#if !ARDUINOJSON_ENABLE_STRING_VIEW
# error ARDUINOJSON_ENABLE_STRING_VIEW must be set to 1
@ -92,7 +93,7 @@ TEST_CASE("string_view") {
}
SECTION("String containing NUL") {
doc.set(std::string("hello\0world", 11));
doc.set("hello\0world"_s);
REQUIRE(doc.as<std::string_view>().size() == 11);
REQUIRE(doc.as<std::string_view>() == std::string_view("hello\0world", 11));
}

View File

@ -16,9 +16,10 @@ endif()
add_executable(DeprecatedTests
add.cpp
BasicJsonDocument.cpp
containsKey.cpp
createNestedArray.cpp
createNestedObject.cpp
BasicJsonDocument.cpp
DynamicJsonDocument.cpp
macros.cpp
memoryUsage.cpp

View File

@ -0,0 +1,210 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
#include "Literals.hpp"
TEST_CASE("JsonDocument::containsKey()") {
JsonDocument doc;
SECTION("returns true on object") {
doc["hello"] = "world";
REQUIRE(doc.containsKey("hello") == true);
}
SECTION("returns true when value is null") {
doc["hello"] = static_cast<const char*>(0);
REQUIRE(doc.containsKey("hello") == true);
}
SECTION("returns true when key is a std::string") {
doc["hello"] = "world";
REQUIRE(doc.containsKey("hello"_s) == true);
}
SECTION("returns false on object") {
doc["world"] = "hello";
REQUIRE(doc.containsKey("hello") == false);
}
SECTION("returns false on array") {
doc.add("hello");
REQUIRE(doc.containsKey("hello") == false);
}
SECTION("returns false on null") {
REQUIRE(doc.containsKey("hello") == false);
}
SECTION("support JsonVariant") {
doc["hello"] = "world";
doc["key"] = "hello";
REQUIRE(doc.containsKey(doc["key"]) == true);
REQUIRE(doc.containsKey(doc["foo"]) == false);
}
}
TEST_CASE("MemberProxy::containsKey()") {
JsonDocument doc;
auto mp = doc["hello"];
SECTION("containsKey(const char*)") {
mp["key"] = "value";
REQUIRE(mp.containsKey("key") == true);
REQUIRE(mp.containsKey("key") == true);
}
SECTION("containsKey(std::string)") {
mp["key"] = "value";
REQUIRE(mp.containsKey("key"_s) == true);
REQUIRE(mp.containsKey("key"_s) == true);
}
}
TEST_CASE("JsonObject::containsKey()") {
JsonDocument doc;
JsonObject obj = doc.to<JsonObject>();
obj["hello"] = 42;
SECTION("returns true only if key is present") {
REQUIRE(false == obj.containsKey("world"));
REQUIRE(true == obj.containsKey("hello"));
}
SECTION("returns false after remove()") {
obj.remove("hello");
REQUIRE(false == obj.containsKey("hello"));
}
#ifdef HAS_VARIABLE_LENGTH_ARRAY
SECTION("key is a VLA") {
size_t i = 16;
char vla[i];
strcpy(vla, "hello");
REQUIRE(true == obj.containsKey(vla));
}
#endif
SECTION("key is a JsonVariant") {
doc["key"] = "hello";
REQUIRE(true == obj.containsKey(obj["key"]));
REQUIRE(false == obj.containsKey(obj["hello"]));
}
SECTION("std::string") {
REQUIRE(true == obj.containsKey("hello"_s));
}
SECTION("unsigned char[]") {
unsigned char key[] = "hello";
REQUIRE(true == obj.containsKey(key));
}
}
TEST_CASE("JsonObjectConst::containsKey()") {
JsonDocument doc;
doc["hello"] = 42;
auto obj = doc.as<JsonObjectConst>();
SECTION("supports const char*") {
REQUIRE(false == obj.containsKey("world"));
REQUIRE(true == obj.containsKey("hello"));
}
SECTION("supports std::string") {
REQUIRE(false == obj.containsKey("world"_s));
REQUIRE(true == obj.containsKey("hello"_s));
}
#ifdef HAS_VARIABLE_LENGTH_ARRAY
SECTION("supports VLA") {
size_t i = 16;
char vla[i];
strcpy(vla, "hello");
REQUIRE(true == obj.containsKey(vla));
}
#endif
SECTION("supports JsonVariant") {
doc["key"] = "hello";
REQUIRE(true == obj.containsKey(obj["key"]));
REQUIRE(false == obj.containsKey(obj["hello"]));
}
}
TEST_CASE("JsonVariant::containsKey()") {
JsonDocument doc;
JsonVariant var = doc.to<JsonVariant>();
SECTION("returns false is unbound") {
CHECK_FALSE(JsonVariant().containsKey("hello"));
}
SECTION("containsKey(const char*)") {
var["hello"] = "world";
REQUIRE(var.containsKey("hello") == true);
REQUIRE(var.containsKey("world") == false);
}
SECTION("containsKey(std::string)") {
var["hello"] = "world";
REQUIRE(var.containsKey("hello"_s) == true);
REQUIRE(var.containsKey("world"_s) == false);
}
SECTION("containsKey(JsonVariant)") {
var["hello"] = "world";
var["key"] = "hello";
REQUIRE(var.containsKey(doc["key"]) == true);
REQUIRE(var.containsKey(doc["foo"]) == false);
}
}
TEST_CASE("JsonVariantConst::containsKey()") {
JsonDocument doc;
doc["hello"] = "world";
JsonVariantConst var = doc.as<JsonVariant>();
SECTION("support const char*") {
REQUIRE(var.containsKey("hello") == true);
REQUIRE(var.containsKey("world") == false);
}
SECTION("support std::string") {
REQUIRE(var.containsKey("hello"_s) == true);
REQUIRE(var.containsKey("world"_s) == false);
}
#ifdef HAS_VARIABLE_LENGTH_ARRAY
SECTION("supports VLA") {
size_t i = 16;
char vla[i];
strcpy(vla, "hello");
REQUIRE(true == var.containsKey(vla));
}
#endif
SECTION("support JsonVariant") {
doc["key"] = "hello";
REQUIRE(var.containsKey(var["key"]) == true);
REQUIRE(var.containsKey(var["foo"]) == false);
}
}

View File

@ -7,6 +7,8 @@
#include <string>
#include "Literals.hpp"
TEST_CASE("JsonDocument::createNestedArray()") {
JsonDocument doc;
@ -23,7 +25,7 @@ TEST_CASE("JsonDocument::createNestedArray()") {
}
SECTION("createNestedArray(std::string)") {
JsonArray array = doc.createNestedArray(std::string("key"));
JsonArray array = doc.createNestedArray("key"_s);
array.add(42);
REQUIRE(doc.as<std::string>() == "{\"key\":[42]}");
}
@ -59,7 +61,7 @@ TEST_CASE("JsonObject::createNestedArray()") {
}
SECTION("createNestedArray(std::string)") {
JsonArray array = object.createNestedArray(std::string("key"));
JsonArray array = object.createNestedArray("key"_s);
array.add(42);
REQUIRE(doc.as<std::string>() == "{\"key\":[42]}");
}
@ -93,7 +95,7 @@ TEST_CASE("JsonVariant::createNestedArray()") {
}
SECTION("createNestedArray(std::string)") {
JsonArray array = variant.createNestedArray(std::string("key"));
JsonArray array = variant.createNestedArray("key"_s);
array.add(42);
REQUIRE(doc.as<std::string>() == "{\"key\":[42]}");
}

View File

@ -7,6 +7,8 @@
#include <string>
#include "Literals.hpp"
TEST_CASE("JsonDocument::createNestedObject()") {
JsonDocument doc;
@ -23,7 +25,7 @@ TEST_CASE("JsonDocument::createNestedObject()") {
}
SECTION("createNestedObject(std::string)") {
JsonObject object = doc.createNestedObject(std::string("key"));
JsonObject object = doc.createNestedObject("key"_s);
object["hello"] = "world";
REQUIRE(doc.as<std::string>() == "{\"key\":{\"hello\":\"world\"}}");
}
@ -59,7 +61,7 @@ TEST_CASE("JsonObject::createNestedObject()") {
}
SECTION("createNestedObject(std::string)") {
JsonObject nestedObject = object.createNestedObject(std::string("key"));
JsonObject nestedObject = object.createNestedObject("key"_s);
nestedObject["hello"] = "world";
REQUIRE(doc.as<std::string>() == "{\"key\":{\"hello\":\"world\"}}");
}
@ -93,7 +95,7 @@ TEST_CASE("JsonVariant::createNestedObject()") {
}
SECTION("createNestedObject(std::string)") {
JsonObject object = variant.createNestedObject(std::string("key"));
JsonObject object = variant.createNestedObject("key"_s);
object["hello"] = "world";
REQUIRE(doc.as<std::string>() == "{\"key\":{\"hello\":\"world\"}}");
}

View File

@ -14,5 +14,5 @@ TEST_CASE("JSON_OBJECT_SIZE") {
}
TEST_CASE("JSON_STRING_SIZE") {
REQUIRE(JSON_STRING_SIZE(10) == ArduinoJson::detail::sizeofString(10));
REQUIRE(JSON_STRING_SIZE(10) == 11); // issue #2054
}

View File

@ -5,11 +5,13 @@
#pragma once
#include <ArduinoJson/Memory/Allocator.hpp>
#include <ArduinoJson/Memory/MemoryPool.hpp>
#include <ArduinoJson/Memory/StringBuilder.hpp>
#include <ArduinoJson/Memory/VariantPool.hpp>
#include <sstream>
namespace {
struct FailingAllocator : ArduinoJson::Allocator {
static FailingAllocator* instance() {
static FailingAllocator allocator;
@ -54,31 +56,31 @@ class AllocatorLogEntry {
inline AllocatorLogEntry Allocate(size_t s) {
char buffer[32];
sprintf(buffer, "allocate(%zu)", s);
snprintf(buffer, sizeof(buffer), "allocate(%zu)", s);
return AllocatorLogEntry(buffer);
}
inline AllocatorLogEntry AllocateFail(size_t s) {
char buffer[32];
sprintf(buffer, "allocate(%zu) -> nullptr", s);
snprintf(buffer, sizeof(buffer), "allocate(%zu) -> nullptr", s);
return AllocatorLogEntry(buffer);
}
inline AllocatorLogEntry Reallocate(size_t s1, size_t s2) {
char buffer[32];
sprintf(buffer, "reallocate(%zu, %zu)", s1, s2);
snprintf(buffer, sizeof(buffer), "reallocate(%zu, %zu)", s1, s2);
return AllocatorLogEntry(buffer);
}
inline AllocatorLogEntry ReallocateFail(size_t s1, size_t s2) {
char buffer[32];
sprintf(buffer, "reallocate(%zu, %zu) -> nullptr", s1, s2);
snprintf(buffer, sizeof(buffer), "reallocate(%zu, %zu) -> nullptr", s1, s2);
return AllocatorLogEntry(buffer);
}
inline AllocatorLogEntry Deallocate(size_t s) {
char buffer[32];
sprintf(buffer, "deallocate(%zu)", s);
snprintf(buffer, sizeof(buffer), "deallocate(%zu)", s);
return AllocatorLogEntry(buffer);
}
@ -90,6 +92,10 @@ class AllocatorLog {
append(entry);
}
void clear() {
log_.str("");
}
void append(const AllocatorLogEntry& entry) {
for (size_t i = 0; i < entry.count(); i++)
log_ << entry.str() << "\n";
@ -165,7 +171,7 @@ class SpyingAllocator : public ArduinoJson::Allocator {
}
void clearLog() {
log_ = AllocatorLog();
log_.clear();
}
const AllocatorLog& log() const {
@ -256,14 +262,17 @@ class TimebombAllocator : public ArduinoJson::Allocator {
size_t countdown_ = 0;
Allocator* upstream_;
};
} // namespace
inline size_t sizeofPoolList(size_t n = ARDUINOJSON_INITIAL_POOL_COUNT) {
return sizeof(ArduinoJson::detail::VariantPool) * n;
using namespace ArduinoJson::detail;
return sizeof(MemoryPool<VariantData>) * n;
}
inline size_t sizeofPool(
ArduinoJson::detail::SlotCount n = ARDUINOJSON_POOL_CAPACITY) {
return ArduinoJson::detail::VariantPool::slotsToBytes(n);
using namespace ArduinoJson::detail;
return MemoryPool<VariantData>::slotsToBytes(n);
}
inline size_t sizeofStringBuffer(size_t iteration = 1) {

View File

@ -0,0 +1,12 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#pragma once
#include <string>
// the space before _s is required by GCC 4.8
inline std::string operator"" _s(const char* str, size_t len) {
return std::string(str, len);
}

View File

@ -6,6 +6,7 @@
#include <catch.hpp>
#include "Allocators.hpp"
#include "Literals.hpp"
using ArduinoJson::detail::sizeofArray;
@ -51,7 +52,7 @@ TEST_CASE("JsonArray::add(T)") {
array.add(vla);
REQUIRE(std::string("world") == array[0]);
REQUIRE("world"_s == array[0]);
}
#endif
@ -115,7 +116,7 @@ TEST_CASE("JsonArray::add(T)") {
}
SECTION("should duplicate std::string") {
array.add(std::string("world"));
array.add("world"_s);
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("world")),
@ -139,7 +140,7 @@ TEST_CASE("JsonArray::add(T)") {
}
SECTION("should duplicate serialized(std::string)") {
array.add(serialized(std::string("{}")));
array.add(serialized("{}"_s));
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("{}")),
@ -147,7 +148,7 @@ TEST_CASE("JsonArray::add(T)") {
}
SECTION("should duplicate serialized(std::string)") {
array.add(serialized(std::string("\0XX", 3)));
array.add(serialized("\0XX"_s));
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString(" XX")),
@ -179,3 +180,52 @@ TEST_CASE("JsonArray::add<T>()") {
REQUIRE(doc.as<std::string>() == "[42]");
}
}
TEST_CASE("JsonObject::add(JsonObject) ") {
JsonDocument doc1;
doc1["key1"_s] = "value1"_s;
TimebombAllocator allocator(10);
SpyingAllocator spy(&allocator);
JsonDocument doc2(&spy);
JsonArray array = doc2.to<JsonArray>();
SECTION("success") {
bool result = array.add(doc1.as<JsonObject>());
REQUIRE(result == true);
REQUIRE(doc2.as<std::string>() == "[{\"key1\":\"value1\"}]");
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("key1")),
Allocate(sizeofString("value1")),
});
}
SECTION("partial failure") { // issue #2081
allocator.setCountdown(2);
bool result = array.add(doc1.as<JsonObject>());
REQUIRE(result == false);
REQUIRE(doc2.as<std::string>() == "[]");
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("key1")),
AllocateFail(sizeofString("value1")),
Deallocate(sizeofString("key1")),
});
}
SECTION("complete failure") {
allocator.setCountdown(0);
bool result = array.add(doc1.as<JsonObject>());
REQUIRE(result == false);
REQUIRE(doc2.as<std::string>() == "[]");
REQUIRE(spy.log() == AllocatorLog{
AllocateFail(sizeofPool()),
});
}
}

View File

@ -6,6 +6,7 @@
#include <catch.hpp>
#include "Allocators.hpp"
#include "Literals.hpp"
TEST_CASE("copyArray()") {
SECTION("int[] -> JsonArray") {
@ -18,7 +19,7 @@ TEST_CASE("copyArray()") {
CHECK(ok);
serializeJson(array, json);
CHECK(std::string("[1,2,3]") == json);
CHECK("[1,2,3]"_s == json);
}
SECTION("std::string[] -> JsonArray") {
@ -31,7 +32,7 @@ TEST_CASE("copyArray()") {
CHECK(ok);
serializeJson(array, json);
CHECK(std::string("[\"a\",\"b\",\"c\"]") == json);
CHECK("[\"a\",\"b\",\"c\"]"_s == json);
}
SECTION("const char*[] -> JsonArray") {
@ -44,7 +45,7 @@ TEST_CASE("copyArray()") {
CHECK(ok);
serializeJson(array, json);
CHECK(std::string("[\"a\",\"b\",\"c\"]") == json);
CHECK("[\"a\",\"b\",\"c\"]"_s == json);
}
SECTION("const char[][] -> JsonArray") {
@ -57,7 +58,7 @@ TEST_CASE("copyArray()") {
CHECK(ok);
serializeJson(array, json);
CHECK(std::string("[\"a\",\"b\",\"c\"]") == json);
CHECK("[\"a\",\"b\",\"c\"]"_s == json);
}
SECTION("const char[][] -> JsonDocument") {
@ -69,7 +70,7 @@ TEST_CASE("copyArray()") {
CHECK(ok);
serializeJson(doc, json);
CHECK(std::string("[\"a\",\"b\",\"c\"]") == json);
CHECK("[\"a\",\"b\",\"c\"]"_s == json);
}
SECTION("const char[][] -> MemberProxy") {
@ -81,7 +82,7 @@ TEST_CASE("copyArray()") {
CHECK(ok);
serializeJson(doc, json);
CHECK(std::string("{\"data\":[\"a\",\"b\",\"c\"]}") == json);
CHECK("{\"data\":[\"a\",\"b\",\"c\"]}"_s == json);
}
SECTION("int[] -> JsonDocument") {
@ -93,7 +94,7 @@ TEST_CASE("copyArray()") {
CHECK(ok);
serializeJson(doc, json);
CHECK(std::string("[1,2,3]") == json);
CHECK("[1,2,3]"_s == json);
}
SECTION("int[] -> MemberProxy") {
@ -105,7 +106,7 @@ TEST_CASE("copyArray()") {
CHECK(ok);
serializeJson(doc, json);
CHECK(std::string("{\"data\":[1,2,3]}") == json);
CHECK("{\"data\":[1,2,3]}"_s == json);
}
SECTION("int[] -> JsonArray, but not enough memory") {
@ -127,7 +128,7 @@ TEST_CASE("copyArray()") {
CHECK(ok);
serializeJson(array, json);
CHECK(std::string("[[1,2,3],[4,5,6]]") == json);
CHECK("[[1,2,3],[4,5,6]]"_s == json);
}
SECTION("int[][] -> MemberProxy") {
@ -139,7 +140,7 @@ TEST_CASE("copyArray()") {
CHECK(ok);
serializeJson(doc, json);
CHECK(std::string("{\"data\":[[1,2,3],[4,5,6]]}") == json);
CHECK("{\"data\":[[1,2,3],[4,5,6]]}"_s == json);
}
SECTION("int[][] -> JsonDocument") {
@ -151,7 +152,7 @@ TEST_CASE("copyArray()") {
CHECK(ok);
serializeJson(doc, json);
CHECK(std::string("[[1,2,3],[4,5,6]]") == json);
CHECK("[[1,2,3],[4,5,6]]"_s == json);
}
SECTION("int[][] -> JsonArray, but not enough memory") {
@ -223,9 +224,9 @@ TEST_CASE("copyArray()") {
size_t result = copyArray(array, destination);
CHECK(3 == result);
CHECK(std::string("a12345") == destination[0]);
CHECK(std::string("b123456") == destination[1]);
CHECK(std::string("c123456") == destination[2]); // truncated
CHECK("a12345"_s == destination[0]);
CHECK("b123456"_s == destination[1]);
CHECK("c123456"_s == destination[2]); // truncated
CHECK(std::string("") == destination[3]);
}

View File

@ -68,6 +68,12 @@ TEST_CASE("JsonArray::remove()") {
REQUIRE(array[1] == 2);
}
SECTION("remove end()") {
array.remove(array.end());
REQUIRE(3 == array.size());
}
SECTION("In a loop") {
for (JsonArray::iterator it = array.begin(); it != array.end(); ++it) {
if (*it == 2)
@ -88,6 +94,15 @@ TEST_CASE("JsonArray::remove()") {
JsonArray unboundArray;
unboundArray.remove(unboundArray.begin());
}
SECTION("use JsonVariant as index") {
array.remove(array[3]); // no effect with null variant
array.remove(array[0]); // remove element at index 1
REQUIRE(2 == array.size());
REQUIRE(array[0] == 1);
REQUIRE(array[1] == 3);
}
}
TEST_CASE("Removed elements are recycled") {

View File

@ -5,6 +5,8 @@
#include <ArduinoJson.h>
#include <catch.hpp>
#include "Literals.hpp"
static void eraseString(std::string& str) {
char* p = const_cast<char*>(str.c_str());
while (*p)
@ -19,7 +21,7 @@ TEST_CASE("std::string") {
std::string value("hello");
array.add(value);
eraseString(value);
REQUIRE(std::string("hello") == array[0]);
REQUIRE("hello"_s == array[0]);
}
SECTION("operator[]") {
@ -27,6 +29,6 @@ TEST_CASE("std::string") {
array.add("hello");
array[0] = value;
eraseString(value);
REQUIRE(std::string("world") == array[0]);
REQUIRE("world"_s == array[0]);
}
}

View File

@ -7,6 +7,7 @@
#include <catch.hpp>
#include "Allocators.hpp"
#include "Literals.hpp"
TEST_CASE("JsonArray::operator[]") {
SpyingAllocator spy;
@ -129,7 +130,7 @@ TEST_CASE("JsonArray::operator[]") {
}
SECTION("should duplicate std::string") {
array[0] = std::string("world");
array[0] = "world"_s;
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("world")),
@ -150,7 +151,7 @@ TEST_CASE("JsonArray::operator[]") {
array.add("hello");
array[0].set(vla);
REQUIRE(std::string("world") == array[0]);
REQUIRE("world"_s == array[0]);
}
SECTION("operator=(VLA)") {
@ -161,7 +162,16 @@ TEST_CASE("JsonArray::operator[]") {
array.add("hello");
array[0] = vla;
REQUIRE(std::string("world") == array[0]);
REQUIRE("world"_s == array[0]);
}
#endif
SECTION("Use a JsonVariant as index") {
array[0] = 1;
array[1] = 2;
array[2] = 3;
REQUIRE(array[array[1]] == 3);
REQUIRE(array[array[3]] == nullptr);
}
}

View File

@ -13,8 +13,15 @@ TEST_CASE("JsonArrayConst::operator[]") {
doc.add(2);
doc.add(3);
REQUIRE(1 == arr[0].as<int>());
REQUIRE(2 == arr[1].as<int>());
REQUIRE(3 == arr[2].as<int>());
REQUIRE(0 == arr[3].as<int>());
SECTION("int") {
REQUIRE(1 == arr[0].as<int>());
REQUIRE(2 == arr[1].as<int>());
REQUIRE(3 == arr[2].as<int>());
REQUIRE(0 == arr[3].as<int>());
}
SECTION("JsonVariant") {
REQUIRE(2 == arr[arr[0]].as<int>());
REQUIRE(0 == arr[arr[3]].as<int>());
}
}

View File

@ -69,14 +69,32 @@ TEST_CASE("deserialize JSON array") {
REQUIRE(arr[1] == 84);
}
SECTION("Double") {
SECTION("Float") {
DeserializationError err = deserializeJson(doc, "[4.2,1e2]");
JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok);
REQUIRE(2 == arr.size());
REQUIRE(arr[0] == 4.2);
REQUIRE(arr[1] == 1e2);
REQUIRE(arr[0].as<float>() == Approx(4.2f));
REQUIRE(arr[1] == 1e2f);
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Reallocate(sizeofPool(), sizeofPool(2)),
});
}
SECTION("Double") {
DeserializationError err = deserializeJson(doc, "[4.2123456,-7E89]");
JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok);
REQUIRE(2 == arr.size());
REQUIRE(arr[0].as<double>() == Approx(4.2123456));
REQUIRE(arr[1] == -7E89);
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Reallocate(sizeofPool(), sizeofPool(4)),
});
}
SECTION("Unsigned long") {

View File

@ -8,6 +8,7 @@
#include <string>
#include "Allocators.hpp"
#include "Literals.hpp"
using ArduinoJson::detail::sizeofArray;
using ArduinoJson::detail::sizeofObject;
@ -15,7 +16,7 @@ using ArduinoJson::detail::sizeofObject;
TEST_CASE("deserializeJson(JsonDocument&)") {
SpyingAllocator spy;
JsonDocument doc(&spy);
doc.add(std::string("hello"));
doc.add("hello"_s);
spy.clearLog();
auto err = deserializeJson(doc, "[42]");
@ -34,7 +35,7 @@ TEST_CASE("deserializeJson(JsonVariant)") {
SECTION("variant is bound") {
SpyingAllocator spy;
JsonDocument doc(&spy);
doc.add(std::string("hello"));
doc.add("hello"_s);
spy.clearLog();
JsonVariant variant = doc[0];
@ -60,7 +61,7 @@ TEST_CASE("deserializeJson(JsonVariant)") {
TEST_CASE("deserializeJson(ElementProxy)") {
SpyingAllocator spy;
JsonDocument doc(&spy);
doc.add(std::string("hello"));
doc.add("hello"_s);
spy.clearLog();
SECTION("element already exists") {
@ -85,7 +86,7 @@ TEST_CASE("deserializeJson(ElementProxy)") {
TEST_CASE("deserializeJson(MemberProxy)") {
SpyingAllocator spy;
JsonDocument doc(&spy);
doc[std::string("hello")] = std::string("world");
doc["hello"_s] = "world"_s;
spy.clearLog();
SECTION("member already exists") {

View File

@ -6,6 +6,8 @@
#include <ArduinoJson.h>
#include <catch.hpp>
#include "Allocators.hpp"
TEST_CASE("deserializeJson() returns IncompleteInput") {
const char* testCases[] = {
// strings
@ -118,3 +120,43 @@ TEST_CASE("deserializeJson() returns NoMemory if string length overflows") {
REQUIRE(err == DeserializationError::NoMemory);
}
}
TEST_CASE("deserializeJson() returns NoMemory if extension allocation fails") {
JsonDocument doc(FailingAllocator::instance());
SECTION("uint32_t should pass") {
auto err = deserializeJson(doc, "4294967295");
REQUIRE(err == DeserializationError::Ok);
}
SECTION("uint64_t should fail") {
auto err = deserializeJson(doc, "18446744073709551615");
REQUIRE(err == DeserializationError::NoMemory);
}
SECTION("int32_t should pass") {
auto err = deserializeJson(doc, "-2147483648");
REQUIRE(err == DeserializationError::Ok);
}
SECTION("int64_t should fail") {
auto err = deserializeJson(doc, "-9223372036854775808");
REQUIRE(err == DeserializationError::NoMemory);
}
SECTION("float should pass") {
auto err = deserializeJson(doc, "3.402823e38");
REQUIRE(err == DeserializationError::Ok);
}
SECTION("double should fail") {
auto err = deserializeJson(doc, "1.7976931348623157e308");
REQUIRE(err == DeserializationError::NoMemory);
}
}

View File

@ -10,6 +10,7 @@
#include <string>
#include "Allocators.hpp"
#include "Literals.hpp"
using ArduinoJson::detail::sizeofArray;
using ArduinoJson::detail::sizeofObject;
@ -732,7 +733,7 @@ TEST_CASE("Overloads") {
}
SECTION("const std::string&, Filter") {
deserializeJson(doc, std::string("{}"), Filter(filter));
deserializeJson(doc, "{}"_s, Filter(filter));
}
SECTION("std::istream&, Filter") {
@ -760,7 +761,7 @@ TEST_CASE("Overloads") {
}
SECTION("const std::string&, Filter, NestingLimit") {
deserializeJson(doc, std::string("{}"), Filter(filter), NestingLimit(5));
deserializeJson(doc, "{}"_s, Filter(filter), NestingLimit(5));
}
SECTION("std::istream&, Filter, NestingLimit") {
@ -788,7 +789,7 @@ TEST_CASE("Overloads") {
}
SECTION("const std::string&, NestingLimit, Filter") {
deserializeJson(doc, std::string("{}"), NestingLimit(5), Filter(filter));
deserializeJson(doc, "{}"_s, NestingLimit(5), Filter(filter));
}
SECTION("std::istream&, NestingLimit, Filter") {

View File

@ -9,6 +9,7 @@
#include "Allocators.hpp"
#include "CustomReader.hpp"
#include "Literals.hpp"
using ArduinoJson::detail::sizeofObject;
@ -69,7 +70,7 @@ TEST_CASE("deserializeJson(const std::string&)") {
}
SECTION("should accept temporary string") {
DeserializationError err = deserializeJson(doc, std::string("[42]"));
DeserializationError err = deserializeJson(doc, "[42]"_s);
REQUIRE(err == DeserializationError::Ok);
}
@ -82,7 +83,7 @@ TEST_CASE("deserializeJson(const std::string&)") {
JsonArray array = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok);
REQUIRE(std::string("hello") == array[0]);
REQUIRE("hello"_s == array[0]);
}
}
@ -108,7 +109,7 @@ TEST_CASE("deserializeJson(std::istream&)") {
REQUIRE(err == DeserializationError::Ok);
REQUIRE(1 == obj.size());
REQUIRE(std::string("world") == obj["hello"]);
REQUIRE("world"_s == obj["hello"]);
}
SECTION("Should not read after the closing brace of an empty object") {

View File

@ -7,6 +7,8 @@
#include <sstream>
#include "Literals.hpp"
#define SHOULD_WORK(expression) REQUIRE(DeserializationError::Ok == expression);
#define SHOULD_FAIL(expression) \
REQUIRE(DeserializationError::TooDeep == expression);
@ -63,23 +65,23 @@ TEST_CASE("JsonDeserializer nesting") {
SECTION("Input = std::string") {
SECTION("limit = 0") {
DeserializationOption::NestingLimit nesting(0);
SHOULD_WORK(deserializeJson(doc, std::string("\"toto\""), nesting));
SHOULD_WORK(deserializeJson(doc, std::string("123"), nesting));
SHOULD_WORK(deserializeJson(doc, std::string("true"), nesting));
SHOULD_FAIL(deserializeJson(doc, std::string("[]"), nesting));
SHOULD_FAIL(deserializeJson(doc, std::string("{}"), nesting));
SHOULD_FAIL(deserializeJson(doc, std::string("[\"toto\"]"), nesting));
SHOULD_FAIL(deserializeJson(doc, std::string("{\"toto\":1}"), nesting));
SHOULD_WORK(deserializeJson(doc, "\"toto\""_s, nesting));
SHOULD_WORK(deserializeJson(doc, "123"_s, nesting));
SHOULD_WORK(deserializeJson(doc, "true"_s, nesting));
SHOULD_FAIL(deserializeJson(doc, "[]"_s, nesting));
SHOULD_FAIL(deserializeJson(doc, "{}"_s, nesting));
SHOULD_FAIL(deserializeJson(doc, "[\"toto\"]"_s, nesting));
SHOULD_FAIL(deserializeJson(doc, "{\"toto\":1}"_s, nesting));
}
SECTION("limit = 1") {
DeserializationOption::NestingLimit nesting(1);
SHOULD_WORK(deserializeJson(doc, std::string("[\"toto\"]"), nesting));
SHOULD_WORK(deserializeJson(doc, std::string("{\"toto\":1}"), nesting));
SHOULD_FAIL(deserializeJson(doc, std::string("{\"toto\":{}}"), nesting));
SHOULD_FAIL(deserializeJson(doc, std::string("{\"toto\":[]}"), nesting));
SHOULD_FAIL(deserializeJson(doc, std::string("[[\"toto\"]]"), nesting));
SHOULD_FAIL(deserializeJson(doc, std::string("[{\"toto\":1}]"), nesting));
SHOULD_WORK(deserializeJson(doc, "[\"toto\"]"_s, nesting));
SHOULD_WORK(deserializeJson(doc, "{\"toto\":1}"_s, nesting));
SHOULD_FAIL(deserializeJson(doc, "{\"toto\":{}}"_s, nesting));
SHOULD_FAIL(deserializeJson(doc, "{\"toto\":[]}"_s, nesting));
SHOULD_FAIL(deserializeJson(doc, "[[\"toto\"]]"_s, nesting));
SHOULD_FAIL(deserializeJson(doc, "[{\"toto\":1}]"_s, nesting));
}
}

View File

@ -155,15 +155,27 @@ TEST_CASE("deserialize JSON object") {
REQUIRE(obj["key2"] == -42);
}
SECTION("Double") {
SECTION("Float") {
DeserializationError err =
deserializeJson(doc, "{\"key1\":12.345,\"key2\":-7E89}");
deserializeJson(doc, "{\"key1\":12.345,\"key2\":-7E3}");
JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.is<JsonObject>());
REQUIRE(obj.size() == 2);
REQUIRE(obj["key1"] == 12.345);
REQUIRE(obj["key1"].as<float>() == Approx(12.345f));
REQUIRE(obj["key2"] == -7E3f);
}
SECTION("Double") {
DeserializationError err =
deserializeJson(doc, "{\"key1\":12.3456789,\"key2\":-7E89}");
JsonObject obj = doc.as<JsonObject>();
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.is<JsonObject>());
REQUIRE(obj.size() == 2);
REQUIRE(obj["key1"].as<double>() == Approx(12.3456789));
REQUIRE(obj["key2"] == -7E89);
}
@ -308,12 +320,12 @@ TEST_CASE("deserialize JSON object") {
REQUIRE(doc["a"] == 2);
}
SECTION("NUL in keys") { // we don't support NULs in keys
SECTION("NUL in keys") {
DeserializationError err =
deserializeJson(doc, "{\"x\\u0000a\":1,\"x\\u0000b\":2}");
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<std::string>() == "{\"x\":2}");
REQUIRE(doc.as<std::string>() == "{\"x\\u0000a\":1,\"x\\u0000b\":2}");
}
}

View File

@ -83,6 +83,22 @@ TEST_CASE("Truncated JSON string") {
}
}
TEST_CASE("Escape single quote in single quoted string") {
JsonDocument doc;
DeserializationError err = deserializeJson(doc, "'ab\\\'cd'");
REQUIRE(err == DeserializationError::Ok);
CHECK(doc.as<std::string>() == "ab\'cd");
}
TEST_CASE("Escape double quote in double quoted string") {
JsonDocument doc;
DeserializationError err = deserializeJson(doc, "'ab\\\"cd'");
REQUIRE(err == DeserializationError::Ok);
CHECK(doc.as<std::string>() == "ab\"cd");
}
TEST_CASE("Invalid JSON string") {
const char* testCases[] = {"'\\u'", "'\\u000g'", "'\\u000'",
"'\\u000G'", "'\\u000/'", "'\\x1234'"};

View File

@ -6,9 +6,9 @@ add_executable(JsonDocumentTests
add.cpp
assignment.cpp
cast.cpp
clear.cpp
compare.cpp
constructor.cpp
containsKey.cpp
ElementProxy.cpp
isNull.cpp
issue1120.cpp

View File

@ -5,6 +5,8 @@
#include <ArduinoJson.h>
#include <catch.hpp>
#include "Literals.hpp"
typedef ArduinoJson::detail::ElementProxy<JsonDocument&> ElementProxy;
TEST_CASE("ElementProxy::add()") {
@ -121,7 +123,7 @@ TEST_CASE("ElementProxy::remove()") {
ep["a"] = 1;
ep["b"] = 2;
ep.remove(std::string("b"));
ep.remove("b"_s);
REQUIRE(ep.as<std::string>() == "{\"a\":1}");
}

View File

@ -9,6 +9,7 @@
#include <catch.hpp>
#include "Allocators.hpp"
#include "Literals.hpp"
using ArduinoJson::detail::sizeofArray;
using ArduinoJson::detail::sizeofObject;
@ -92,33 +93,14 @@ TEST_CASE("MemberProxy::operator==()") {
}
}
TEST_CASE("MemberProxy::containsKey()") {
JsonDocument doc;
MemberProxy mp = doc["hello"];
SECTION("containsKey(const char*)") {
mp["key"] = "value";
REQUIRE(mp.containsKey("key") == true);
REQUIRE(mp.containsKey("key") == true);
}
SECTION("containsKey(std::string)") {
mp["key"] = "value";
REQUIRE(mp.containsKey(std::string("key")) == true);
REQUIRE(mp.containsKey(std::string("key")) == true);
}
}
TEST_CASE("MemberProxy::operator|()") {
JsonDocument doc;
SECTION("const char*") {
doc["a"] = "hello";
REQUIRE((doc["a"] | "world") == std::string("hello"));
REQUIRE((doc["b"] | "world") == std::string("world"));
REQUIRE((doc["a"] | "world") == "hello"_s);
REQUIRE((doc["b"] | "world") == "world"_s);
}
SECTION("Issue #1411") {
@ -128,7 +110,7 @@ TEST_CASE("MemberProxy::operator|()") {
// to trigger the bug
const char* sensor = doc["sensor"] | test; // "gps"
REQUIRE(sensor == std::string("gps"));
REQUIRE(sensor == "gps"_s);
}
SECTION("Issue #1415") {
@ -170,7 +152,7 @@ TEST_CASE("MemberProxy::remove()") {
mp["a"] = 1;
mp["b"] = 2;
mp.remove(std::string("b"));
mp.remove("b"_s);
REQUIRE(mp.as<std::string>() == "{\"a\":1}");
}
@ -286,8 +268,8 @@ TEST_CASE("Deduplicate keys") {
JsonDocument doc(&spy);
SECTION("std::string") {
doc[0][std::string("example")] = 1;
doc[1][std::string("example")] = 2;
doc[0]["example"_s] = 1;
doc[1]["example"_s] = 2;
const char* key1 = doc[0].as<JsonObject>().begin()->key().c_str();
const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str();
@ -344,19 +326,51 @@ TEST_CASE("Deduplicate keys") {
}
TEST_CASE("MemberProxy under memory constraints") {
KillswitchAllocator killswitch;
SpyingAllocator spy(&killswitch);
TimebombAllocator timebomb(1);
SpyingAllocator spy(&timebomb);
JsonDocument doc(&spy);
SECTION("key allocation fails") {
killswitch.on();
SECTION("key slot allocation fails") {
timebomb.setCountdown(0);
doc[std::string("hello")] = "world";
doc["hello"_s] = "world";
REQUIRE(doc.is<JsonObject>());
REQUIRE(doc.size() == 0);
REQUIRE(doc.overflowed() == true);
REQUIRE(spy.log() == AllocatorLog{
AllocateFail(sizeofPool()),
});
}
SECTION("value slot allocation fails") {
timebomb.setCountdown(1);
// fill the pool entirely, but leave one slot for the key
doc["foo"][ARDUINOJSON_POOL_CAPACITY - 4] = 1;
REQUIRE(doc.overflowed() == false);
doc["hello"_s] = "world";
REQUIRE(doc.is<JsonObject>());
REQUIRE(doc.size() == 1);
REQUIRE(doc.overflowed() == true);
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
AllocateFail(sizeofPool()),
});
}
SECTION("key string allocation fails") {
timebomb.setCountdown(1);
doc["hello"_s] = "world";
REQUIRE(doc.is<JsonObject>());
REQUIRE(doc.size() == 0);
REQUIRE(doc.overflowed() == true);
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
AllocateFail(sizeofString("hello")),
});
}

View File

@ -9,6 +9,7 @@
#include <catch.hpp>
#include "Allocators.hpp"
#include "Literals.hpp"
using ArduinoJson::detail::sizeofArray;
@ -35,8 +36,8 @@ TEST_CASE("JsonDocument::add(T)") {
}
SECTION("std::string") {
doc.add(std::string("example"));
doc.add(std::string("example"));
doc.add("example"_s);
doc.add("example"_s);
CHECK(doc[0].as<const char*>() == doc[1].as<const char*>());
REQUIRE(spy.log() == AllocatorLog{
@ -90,15 +91,57 @@ TEST_CASE("JsonDocument::add<T>()") {
REQUIRE(doc.as<std::string>() == "[[1,2]]");
}
SECTION("JsonObject") {
JsonObject object = doc.add<JsonObject>();
object["hello"] = "world";
REQUIRE(doc.as<std::string>() == "[{\"hello\":\"world\"}]");
}
SECTION("JsonVariant") {
JsonVariant variant = doc.add<JsonVariant>();
variant.set(42);
REQUIRE(doc.as<std::string>() == "[42]");
}
}
TEST_CASE("JsonObject::add(JsonObject) ") {
JsonDocument doc1;
doc1["hello"_s] = "world"_s;
TimebombAllocator allocator(10);
SpyingAllocator spy(&allocator);
JsonDocument doc2(&spy);
SECTION("success") {
bool result = doc2.add(doc1.as<JsonObject>());
REQUIRE(result == true);
REQUIRE(doc2.as<std::string>() == "[{\"hello\":\"world\"}]");
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("hello")),
Allocate(sizeofString("world")),
});
}
SECTION("partial failure") { // issue #2081
allocator.setCountdown(2);
bool result = doc2.add(doc1.as<JsonObject>());
REQUIRE(result == false);
REQUIRE(doc2.as<std::string>() == "[]");
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("hello")),
AllocateFail(sizeofString("world")),
Deallocate(sizeofString("hello")),
});
}
SECTION("complete failure") {
allocator.setCountdown(0);
bool result = doc2.add(doc1.as<JsonObject>());
REQUIRE(result == false);
REQUIRE(doc2.as<std::string>() == "[]");
REQUIRE(spy.log() == AllocatorLog{
AllocateFail(sizeofPool()),
});
}
}

View File

@ -6,6 +6,7 @@
#include <catch.hpp>
#include "Allocators.hpp"
#include "Literals.hpp"
TEST_CASE("JsonDocument assignment") {
SpyingAllocator spyingAllocator;
@ -21,8 +22,8 @@ TEST_CASE("JsonDocument assignment") {
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
REQUIRE(spyingAllocator.log() == AllocatorLog{
Allocate(sizeofString("hello")),
Allocate(sizeofPool()),
Allocate(sizeofString("hello")),
Allocate(sizeofString("world")),
});
}
@ -53,8 +54,8 @@ TEST_CASE("JsonDocument assignment") {
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
REQUIRE(spyingAllocator.log() == AllocatorLog{
Allocate(sizeofString("hello")),
Allocate(sizeofPool()),
Allocate(sizeofString("hello")),
Allocate(sizeofString("world")),
});
}
@ -62,7 +63,7 @@ TEST_CASE("JsonDocument assignment") {
SECTION("Move assign") {
{
JsonDocument doc1(&spyingAllocator);
doc1[std::string("hello")] = std::string("world");
doc1["hello"_s] = "world"_s;
JsonDocument doc2(&spyingAllocator);
doc2 = std::move(doc1);
@ -71,8 +72,8 @@ TEST_CASE("JsonDocument assignment") {
REQUIRE(doc1.as<std::string>() == "null");
}
REQUIRE(spyingAllocator.log() == AllocatorLog{
Allocate(sizeofString("hello")),
Allocate(sizeofPool()),
Allocate(sizeofString("hello")),
Allocate(sizeofString("world")),
Deallocate(sizeofString("hello")),
Deallocate(sizeofString("world")),

View File

@ -0,0 +1,48 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
#include <stdlib.h> // malloc, free
#include <string>
#include "Allocators.hpp"
#include "Literals.hpp"
TEST_CASE("JsonDocument::clear()") {
SpyingAllocator spy;
JsonDocument doc(&spy);
SECTION("null") {
doc.clear();
REQUIRE(doc.isNull());
REQUIRE(spy.log() == AllocatorLog{});
}
SECTION("releases resources") {
doc["hello"_s] = "world"_s;
spy.clearLog();
doc.clear();
REQUIRE(doc.isNull());
REQUIRE(spy.log() == AllocatorLog{
Deallocate(sizeofPool()),
Deallocate(sizeofString("hello")),
Deallocate(sizeofString("world")),
});
}
SECTION("clear free list") { // issue #2034
JsonObject obj = doc.to<JsonObject>();
obj["a"] = 1;
obj.clear(); // puts the slot in the free list
doc.clear();
doc["b"] = 2; // will it pick from the free list?
}
}

View File

@ -6,6 +6,7 @@
#include <catch.hpp>
#include "Allocators.hpp"
#include "Literals.hpp"
using ArduinoJson::detail::addPadding;
@ -20,7 +21,7 @@ TEST_CASE("JsonDocument constructor") {
SECTION("JsonDocument(const JsonDocument&)") {
{
JsonDocument doc1(&spyingAllocator);
doc1.set(std::string("The size of this string is 32!!"));
doc1.set("The size of this string is 32!!"_s);
JsonDocument doc2(doc1);
@ -38,7 +39,7 @@ TEST_CASE("JsonDocument constructor") {
SECTION("JsonDocument(JsonDocument&&)") {
{
JsonDocument doc1(&spyingAllocator);
doc1.set(std::string("The size of this string is 32!!"));
doc1.set("The size of this string is 32!!"_s);
JsonDocument doc2(std::move(doc1));
@ -117,4 +118,31 @@ TEST_CASE("JsonDocument constructor") {
REQUIRE(doc2.as<std::string>() == "hello");
}
SECTION("JsonDocument(JsonVariantConst)") {
JsonDocument doc1;
deserializeJson(doc1, "\"hello\"");
JsonDocument doc2(doc1.as<JsonVariantConst>());
REQUIRE(doc2.as<std::string>() == "hello");
}
SECTION("JsonDocument(ElementProxy)") {
JsonDocument doc1;
deserializeJson(doc1, "[\"hello\",\"world\"]");
JsonDocument doc2(doc1[1]);
REQUIRE(doc2.as<std::string>() == "world");
}
SECTION("JsonDocument(MemberProxy)") {
JsonDocument doc1;
deserializeJson(doc1, "{\"hello\":\"world\"}");
JsonDocument doc2(doc1["hello"]);
REQUIRE(doc2.as<std::string>() == "world");
}
}

View File

@ -1,44 +0,0 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("JsonDocument::containsKey()") {
JsonDocument doc;
SECTION("returns true on object") {
doc["hello"] = "world";
REQUIRE(doc.containsKey("hello") == true);
}
SECTION("returns true when value is null") {
doc["hello"] = static_cast<const char*>(0);
REQUIRE(doc.containsKey("hello") == true);
}
SECTION("returns true when key is a std::string") {
doc["hello"] = "world";
REQUIRE(doc.containsKey(std::string("hello")) == true);
}
SECTION("returns false on object") {
doc["world"] = "hello";
REQUIRE(doc.containsKey("hello") == false);
}
SECTION("returns false on array") {
doc.add("hello");
REQUIRE(doc.containsKey("hello") == false);
}
SECTION("returns false on null") {
REQUIRE(doc.containsKey("hello") == false);
}
}

View File

@ -2,6 +2,8 @@
#include <catch.hpp>
#include "Literals.hpp"
TEST_CASE("Issue #1120") {
JsonDocument doc;
constexpr char str[] =
@ -10,12 +12,12 @@ TEST_CASE("Issue #1120") {
SECTION("MemberProxy<std::string>::isNull()") {
SECTION("returns false") {
auto value = doc[std::string("contents")];
auto value = doc["contents"_s];
CHECK(value.isNull() == false);
}
SECTION("returns true") {
auto value = doc[std::string("zontents")];
auto value = doc["zontents"_s];
CHECK(value.isNull() == true);
}
}
@ -46,12 +48,12 @@ TEST_CASE("Issue #1120") {
SECTION("MemberProxy<ElementProxy<MemberProxy>, std::string>::isNull()") {
SECTION("returns false") {
auto value = doc["contents"][1][std::string("module")];
auto value = doc["contents"][1]["module"_s];
CHECK(value.isNull() == false);
}
SECTION("returns true") {
auto value = doc["contents"][1][std::string("zodule")];
auto value = doc["contents"][1]["zodule"_s];
CHECK(value.isNull() == true);
}
}

View File

@ -6,6 +6,7 @@
#include <catch.hpp>
#include "Allocators.hpp"
#include "Literals.hpp"
TEST_CASE("JsonDocument::overflowed()") {
TimebombAllocator timebomb(10);
@ -30,13 +31,13 @@ TEST_CASE("JsonDocument::overflowed()") {
SECTION("returns true after a failed string copy") {
timebomb.setCountdown(0);
doc.add(std::string("example"));
doc.add("example"_s);
CHECK(doc.overflowed() == true);
}
SECTION("returns false after a successful string copy") {
timebomb.setCountdown(3);
doc.add(std::string("example"));
doc.add("example"_s);
CHECK(doc.overflowed() == false);
}

View File

@ -5,6 +5,8 @@
#include <ArduinoJson.h>
#include <catch.hpp>
#include "Literals.hpp"
TEST_CASE("JsonDocument::remove()") {
JsonDocument doc;
@ -31,7 +33,7 @@ TEST_CASE("JsonDocument::remove()") {
doc["a"] = 1;
doc["b"] = 2;
doc.remove(std::string("b"));
doc.remove("b"_s);
REQUIRE(doc.as<std::string>() == "{\"a\":1}");
}
@ -49,4 +51,25 @@ TEST_CASE("JsonDocument::remove()") {
REQUIRE(doc.as<std::string>() == "{\"a\":1}");
}
#endif
SECTION("remove(JsonVariant) from object") {
doc["a"] = 1;
doc["b"] = 2;
doc["c"] = "b";
doc.remove(doc["c"]);
REQUIRE(doc.as<std::string>() == "{\"a\":1,\"c\":\"b\"}");
}
SECTION("remove(JsonVariant) from array") {
doc[0] = 3;
doc[1] = 2;
doc[2] = 1;
doc.remove(doc[2]);
doc.remove(doc[3]); // noop
REQUIRE(doc.as<std::string>() == "[3,1]");
}
}

View File

@ -9,6 +9,7 @@
#include <string>
#include "Allocators.hpp"
#include "Literals.hpp"
using ArduinoJson::detail::sizeofArray;
using ArduinoJson::detail::sizeofObject;
@ -78,7 +79,7 @@ TEST_CASE("JsonDocument::shrinkToFit()") {
}
SECTION("owned string") {
doc.set(std::string("abcdefg"));
doc.set("abcdefg"_s);
REQUIRE(doc.as<std::string>() == "abcdefg");
doc.shrinkToFit();
@ -114,15 +115,15 @@ TEST_CASE("JsonDocument::shrinkToFit()") {
}
SECTION("owned key") {
doc[std::string("abcdefg")] = 42;
doc["abcdefg"_s] = 42;
doc.shrinkToFit();
REQUIRE(doc.as<std::string>() == "{\"abcdefg\":42}");
REQUIRE(spyingAllocator.log() ==
AllocatorLog{
Allocate(sizeofString("abcdefg")),
Allocate(sizeofPool()),
Allocate(sizeofString("abcdefg")),
Reallocate(sizeofPool(), sizeofObject(1)),
});
}
@ -141,7 +142,7 @@ TEST_CASE("JsonDocument::shrinkToFit()") {
}
SECTION("owned string in array") {
doc.add(std::string("abcdefg"));
doc.add("abcdefg"_s);
doc.shrinkToFit();
@ -168,7 +169,7 @@ TEST_CASE("JsonDocument::shrinkToFit()") {
}
SECTION("owned string in object") {
doc["key"] = std::string("abcdefg");
doc["key"] = "abcdefg"_s;
doc.shrinkToFit();
@ -177,7 +178,7 @@ TEST_CASE("JsonDocument::shrinkToFit()") {
AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("abcdefg")),
Reallocate(sizeofPool(), sizeofPool(1)),
Reallocate(sizeofPool(), sizeofPool(2)),
});
}
}

View File

@ -5,6 +5,8 @@
#include <ArduinoJson.h>
#include <catch.hpp>
#include "Literals.hpp"
TEST_CASE("JsonDocument::operator[]") {
JsonDocument doc;
const JsonDocument& cdoc = doc;
@ -18,21 +20,35 @@ TEST_CASE("JsonDocument::operator[]") {
}
SECTION("std::string") {
REQUIRE(doc[std::string("hello")] == "world");
REQUIRE(cdoc[std::string("hello")] == "world");
REQUIRE(doc["hello"_s] == "world");
REQUIRE(cdoc["hello"_s] == "world");
}
SECTION("JsonVariant") {
doc["key"] = "hello";
REQUIRE(doc[doc["key"]] == "world");
REQUIRE(cdoc[cdoc["key"]] == "world");
}
SECTION("supports operator|") {
REQUIRE((doc["hello"] | "nope") == std::string("world"));
REQUIRE((doc["world"] | "nope") == std::string("nope"));
REQUIRE((doc["hello"] | "nope") == "world"_s);
REQUIRE((doc["world"] | "nope") == "nope"_s);
}
}
SECTION("array") {
deserializeJson(doc, "[\"hello\",\"world\"]");
REQUIRE(doc[1] == "world");
REQUIRE(cdoc[1] == "world");
SECTION("int") {
REQUIRE(doc[1] == "world");
REQUIRE(cdoc[1] == "world");
}
SECTION("JsonVariant") {
doc[2] = 1;
REQUIRE(doc[doc[2]] == "world");
REQUIRE(cdoc[doc[2]] == "world");
}
}
}

View File

@ -5,13 +5,12 @@
add_executable(JsonObjectTests
clear.cpp
compare.cpp
containsKey.cpp
copy.cpp
equals.cpp
isNull.cpp
iterator.cpp
nesting.cpp
remove.cpp
set.cpp
size.cpp
std_string.cpp
subscript.cpp

View File

@ -1,33 +0,0 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("JsonObject::containsKey()") {
JsonDocument doc;
JsonObject obj = doc.to<JsonObject>();
obj["hello"] = 42;
SECTION("returns true only if key is present") {
REQUIRE(false == obj.containsKey("world"));
REQUIRE(true == obj.containsKey("hello"));
}
SECTION("returns false after remove()") {
obj.remove("hello");
REQUIRE(false == obj.containsKey("hello"));
}
#ifdef HAS_VARIABLE_LENGTH_ARRAY
SECTION("key is a VLA") {
size_t i = 16;
char vla[i];
strcpy(vla, "hello");
REQUIRE(true == obj.containsKey(vla));
}
#endif
}

View File

@ -80,4 +80,10 @@ TEST_CASE("JsonObject::remove()") {
JsonObject unboundObject;
unboundObject.remove(unboundObject.begin());
}
SECTION("remove(JsonVariant)") {
obj["key"] = "b";
obj.remove(obj["key"]);
REQUIRE("{\"a\":0,\"c\":2,\"key\":\"b\"}" == doc.as<std::string>());
}
}

View File

@ -6,6 +6,7 @@
#include <catch.hpp>
#include "Allocators.hpp"
#include "Literals.hpp"
TEST_CASE("JsonObject::set()") {
SpyingAllocator spy;
@ -22,20 +23,20 @@ TEST_CASE("JsonObject::set()") {
bool success = obj2.set(obj1);
REQUIRE(success == true);
REQUIRE(obj2["hello"] == std::string("world"));
REQUIRE(obj2["hello"] == "world"_s);
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
});
}
SECTION("copy local string value") {
obj1["hello"] = std::string("world");
obj1["hello"] = "world"_s;
spy.clearLog();
bool success = obj2.set(obj1);
REQUIRE(success == true);
REQUIRE(obj2["hello"] == std::string("world"));
REQUIRE(obj2["hello"] == "world"_s);
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("world")),
@ -43,16 +44,16 @@ TEST_CASE("JsonObject::set()") {
}
SECTION("copy local key") {
obj1[std::string("hello")] = "world";
obj1["hello"_s] = "world";
spy.clearLog();
bool success = obj2.set(obj1);
REQUIRE(success == true);
REQUIRE(obj2["hello"] == std::string("world"));
REQUIRE(obj2["hello"] == "world"_s);
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofString("hello")),
Allocate(sizeofPool()),
Allocate(sizeofString("hello")),
});
}
@ -63,10 +64,10 @@ TEST_CASE("JsonObject::set()") {
bool success = obj2.set(obj1);
REQUIRE(success == true);
REQUIRE(obj2["hello"] == std::string("world"));
REQUIRE(obj2["hello"] == "world"_s);
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofString("hello")),
Allocate(sizeofPool()),
Allocate(sizeofString("hello")),
Allocate(sizeofString("world")),
});
}
@ -78,10 +79,10 @@ TEST_CASE("JsonObject::set()") {
bool success = obj2.set(obj1);
REQUIRE(success == true);
REQUIRE(obj2["hello"] == std::string("world"));
REQUIRE(obj2["hello"] == "world"_s);
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofString("hello")),
Allocate(sizeofPool()),
Allocate(sizeofString("hello")),
Allocate(sizeofString("world")),
});
}
@ -91,7 +92,7 @@ TEST_CASE("JsonObject::set()") {
obj2.set(static_cast<JsonObjectConst>(obj1));
REQUIRE(obj2["hello"] == std::string("world"));
REQUIRE(obj2["hello"] == "world"_s);
}
SECTION("copy fails in the middle of an object") {
@ -99,8 +100,8 @@ TEST_CASE("JsonObject::set()") {
JsonDocument doc3(&timebomb);
JsonObject obj3 = doc3.to<JsonObject>();
obj1[std::string("a")] = 1;
obj1[std::string("b")] = 2;
obj1["a"_s] = 1;
obj1["b"_s] = 2;
bool success = obj3.set(obj1);
@ -113,12 +114,12 @@ TEST_CASE("JsonObject::set()") {
JsonDocument doc3(&timebomb);
JsonObject obj3 = doc3.to<JsonObject>();
obj1["hello"][0] = std::string("world");
obj1["hello"][0] = "world"_s;
bool success = obj3.set(obj1);
REQUIRE(success == false);
REQUIRE(doc3.as<std::string>() == "{\"hello\":[null]}");
REQUIRE(doc3.as<std::string>() == "{\"hello\":[]}");
}
SECTION("destination is null") {

View File

@ -5,6 +5,8 @@
#include <ArduinoJson.h>
#include <catch.hpp>
#include "Literals.hpp"
static void eraseString(std::string& str) {
char* p = const_cast<char*>(str.c_str());
while (*p)
@ -20,7 +22,7 @@ TEST_CASE("std::string") {
deserializeJson(doc, json);
JsonObject obj = doc.as<JsonObject>();
REQUIRE(std::string("value") == obj[std::string("key")]);
REQUIRE("value"_s == obj["key"_s]);
}
SECTION("operator[] const") {
@ -29,21 +31,14 @@ TEST_CASE("std::string") {
deserializeJson(doc, json);
JsonObject obj = doc.as<JsonObject>();
REQUIRE(std::string("value") == obj[std::string("key")]);
}
SECTION("containsKey()") {
char json[] = "{\"key\":\"value\"}";
deserializeJson(doc, json);
JsonObject obj = doc.as<JsonObject>();
REQUIRE(true == obj.containsKey(std::string("key")));
REQUIRE("value"_s == obj["key"_s]);
}
SECTION("remove()") {
JsonObject obj = doc.to<JsonObject>();
obj["key"] = "value";
obj.remove(std::string("key"));
obj.remove("key"_s);
REQUIRE(0 == obj.size());
}
@ -53,7 +48,7 @@ TEST_CASE("std::string") {
JsonObject obj = doc.to<JsonObject>();
obj[key] = "world";
eraseString(key);
REQUIRE(std::string("world") == obj["hello"]);
REQUIRE("world"_s == obj["hello"]);
}
SECTION("operator[], set value") {
@ -61,6 +56,6 @@ TEST_CASE("std::string") {
JsonObject obj = doc.to<JsonObject>();
obj["hello"] = value;
eraseString(value);
REQUIRE(std::string("world") == obj["hello"]);
REQUIRE("world"_s == obj["hello"]);
}
}

View File

@ -6,6 +6,7 @@
#include <catch.hpp>
#include "Allocators.hpp"
#include "Literals.hpp"
TEST_CASE("JsonObject::operator[]") {
SpyingAllocator spy;
@ -50,7 +51,7 @@ TEST_CASE("JsonObject::operator[]") {
REQUIRE(true == obj["hello"].is<const char*>());
REQUIRE(false == obj["hello"].is<long>());
REQUIRE(std::string("h3110") == obj["hello"].as<const char*>());
REQUIRE("h3110"_s == obj["hello"].as<const char*>());
}
SECTION("array") {
@ -117,22 +118,22 @@ TEST_CASE("JsonObject::operator[]") {
SECTION("should duplicate char* key") {
obj[const_cast<char*>("hello")] = "world";
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofString("hello")),
Allocate(sizeofPool()),
Allocate(sizeofString("hello")),
});
}
SECTION("should duplicate char* key&value") {
obj[const_cast<char*>("hello")] = const_cast<char*>("world");
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofString("hello")),
Allocate(sizeofPool()),
Allocate(sizeofString("hello")),
Allocate(sizeofString("world")),
});
}
SECTION("should duplicate std::string value") {
obj["hello"] = std::string("world");
obj["hello"] = "world"_s;
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("world")),
@ -140,18 +141,18 @@ TEST_CASE("JsonObject::operator[]") {
}
SECTION("should duplicate std::string key") {
obj[std::string("hello")] = "world";
obj["hello"_s] = "world";
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofString("hello")),
Allocate(sizeofPool()),
Allocate(sizeofString("hello")),
});
}
SECTION("should duplicate std::string key&value") {
obj[std::string("hello")] = std::string("world");
obj["hello"_s] = "world"_s;
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofString("hello")),
Allocate(sizeofPool()),
Allocate(sizeofString("hello")),
Allocate(sizeofString("world")),
});
}
@ -159,8 +160,8 @@ TEST_CASE("JsonObject::operator[]") {
SECTION("should duplicate a non-static JsonString key") {
obj[JsonString("hello", JsonString::Copied)] = "world";
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofString("hello")),
Allocate(sizeofPool()),
Allocate(sizeofString("hello")),
});
}
@ -197,7 +198,7 @@ TEST_CASE("JsonObject::operator[]") {
obj[vla] = "world";
REQUIRE(std::string("world") == obj["hello"]);
REQUIRE("world"_s == obj["hello"]);
}
SECTION("obj[str] = VLA") { // issue #416
@ -207,7 +208,7 @@ TEST_CASE("JsonObject::operator[]") {
obj["hello"] = vla;
REQUIRE(std::string("world") == obj["hello"].as<const char*>());
REQUIRE("world"_s == obj["hello"].as<const char*>());
}
SECTION("obj.set(VLA, str)") {
@ -217,7 +218,7 @@ TEST_CASE("JsonObject::operator[]") {
obj[vla] = "world";
REQUIRE(std::string("world") == obj["hello"]);
REQUIRE("world"_s == obj["hello"]);
}
SECTION("obj.set(str, VLA)") {
@ -227,7 +228,7 @@ TEST_CASE("JsonObject::operator[]") {
obj["hello"].set(vla);
REQUIRE(std::string("world") == obj["hello"].as<const char*>());
REQUIRE("world"_s == obj["hello"].as<const char*>());
}
SECTION("obj[VLA]") {
@ -238,7 +239,7 @@ TEST_CASE("JsonObject::operator[]") {
deserializeJson(doc, "{\"hello\":\"world\"}");
obj = doc.as<JsonObject>();
REQUIRE(std::string("world") == obj[vla]);
REQUIRE("world"_s == obj[vla]);
}
#endif
@ -249,4 +250,12 @@ TEST_CASE("JsonObject::operator[]") {
REQUIRE(true == obj["hello"]["world"].is<int>());
REQUIRE(false == obj["hello"]["world"].is<bool>());
}
SECTION("JsonVariant") {
obj["hello"] = "world";
doc["key"] = "hello";
REQUIRE(obj[obj["key"]] == "world");
REQUIRE(obj[obj["foo"]] == nullptr);
}
}

View File

@ -3,7 +3,6 @@
# MIT License
add_executable(JsonObjectConstTests
containsKey.cpp
equals.cpp
isNull.cpp
iterator.cpp

View File

@ -1,32 +0,0 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("JsonObjectConst::containsKey()") {
JsonDocument doc;
doc["hello"] = 42;
auto obj = doc.as<JsonObjectConst>();
SECTION("supports const char*") {
REQUIRE(false == obj.containsKey("world"));
REQUIRE(true == obj.containsKey("hello"));
}
SECTION("supports std::string") {
REQUIRE(false == obj.containsKey(std::string("world")));
REQUIRE(true == obj.containsKey(std::string("hello")));
}
#ifdef HAS_VARIABLE_LENGTH_ARRAY
SECTION("supports VLA") {
size_t i = 16;
char vla[i];
strcpy(vla, "hello");
REQUIRE(true == obj.containsKey(vla));
}
#endif
}

View File

@ -6,6 +6,7 @@
#include <catch.hpp>
#include "Allocators.hpp"
#include "Literals.hpp"
TEST_CASE("JsonObjectConst::operator[]") {
JsonDocument doc;
@ -17,7 +18,7 @@ TEST_CASE("JsonObjectConst::operator[]") {
}
SECTION("supports std::string") {
REQUIRE(obj[std::string("hello")] == "world"); // issue #2019
REQUIRE(obj["hello"_s] == "world"); // issue #2019
}
#if defined(HAS_VARIABLE_LENGTH_ARRAY) && \
@ -27,7 +28,13 @@ TEST_CASE("JsonObjectConst::operator[]") {
char vla[i];
strcpy(vla, "hello");
REQUIRE(std::string("world") == obj[vla]);
REQUIRE("world"_s == obj[vla]);
}
#endif
SECTION("supports JsonVariant") {
doc["key"] = "hello";
REQUIRE(obj[obj["key"]] == "world");
REQUIRE(obj[obj["foo"]] == nullptr);
}
}

View File

@ -6,6 +6,8 @@
#include <catch.hpp>
#include <limits>
#include "Literals.hpp"
template <typename T>
void check(T value, const std::string& expected) {
JsonDocument doc;
@ -30,42 +32,46 @@ TEST_CASE("serializeJson(JsonVariant)") {
}
SECTION("string") {
check(std::string("hello"), "\"hello\"");
check("hello"_s, "\"hello\"");
SECTION("Escape quotation mark") {
check(std::string("hello \"world\""), "\"hello \\\"world\\\"\"");
check("hello \"world\""_s, "\"hello \\\"world\\\"\"");
}
SECTION("Escape reverse solidus") {
check(std::string("hello\\world"), "\"hello\\\\world\"");
check("hello\\world"_s, "\"hello\\\\world\"");
}
SECTION("Don't escape solidus") {
check(std::string("fifty/fifty"), "\"fifty/fifty\"");
check("fifty/fifty"_s, "\"fifty/fifty\"");
}
SECTION("Don't escape single quote") {
check("hello'world"_s, "\"hello'world\"");
}
SECTION("Escape backspace") {
check(std::string("hello\bworld"), "\"hello\\bworld\"");
check("hello\bworld"_s, "\"hello\\bworld\"");
}
SECTION("Escape formfeed") {
check(std::string("hello\fworld"), "\"hello\\fworld\"");
check("hello\fworld"_s, "\"hello\\fworld\"");
}
SECTION("Escape linefeed") {
check(std::string("hello\nworld"), "\"hello\\nworld\"");
check("hello\nworld"_s, "\"hello\\nworld\"");
}
SECTION("Escape carriage return") {
check(std::string("hello\rworld"), "\"hello\\rworld\"");
check("hello\rworld"_s, "\"hello\\rworld\"");
}
SECTION("Escape tab") {
check(std::string("hello\tworld"), "\"hello\\tworld\"");
check("hello\tworld"_s, "\"hello\\tworld\"");
}
SECTION("NUL char") {
check(std::string("hello\0world", 11), "\"hello\\u0000world\"");
check("hello\0world"_s, "\"hello\\u0000world\"");
}
}
@ -74,13 +80,18 @@ TEST_CASE("serializeJson(JsonVariant)") {
}
SECTION("SerializedValue<std::string>") {
check(serialized(std::string("[1,2]")), "[1,2]");
check(serialized("[1,2]"_s), "[1,2]");
}
SECTION("Double") {
check(3.1415927, "3.1415927");
}
SECTION("Float") {
REQUIRE(sizeof(float) == 4);
check(3.1415927f, "3.141593");
}
SECTION("Zero") {
check(0, "0");
}

View File

@ -5,6 +5,8 @@
#include <ArduinoJson.h>
#include <catch.hpp>
#include "Literals.hpp"
TEST_CASE("serialize JsonArray to std::string") {
JsonDocument doc;
JsonArray array = doc.to<JsonArray>();
@ -48,7 +50,7 @@ TEST_CASE("serialize JsonObject to std::string") {
TEST_CASE("serialize an std::string containing a NUL") {
JsonDocument doc;
doc.set(std::string("hello\0world", 11));
doc.set("hello\0world"_s);
std::string json = "erase me";
serializeJson(doc, json);

View File

@ -7,7 +7,6 @@ add_executable(JsonVariantTests
as.cpp
clear.cpp
compare.cpp
containsKey.cpp
converters.cpp
copy.cpp
is.cpp

View File

@ -6,6 +6,9 @@
#include <stdint.h>
#include <catch.hpp>
#include "Allocators.hpp"
#include "Literals.hpp"
TEST_CASE("JsonVariant::add(T)") {
JsonDocument doc;
JsonVariant var = doc.to<JsonVariant>();
@ -23,7 +26,7 @@ TEST_CASE("JsonVariant::add(T)") {
}
SECTION("add std::string to new variant") {
var.add(std::string("hello"));
var.add("hello"_s);
REQUIRE(var.as<std::string>() == "[\"hello\"]");
}
@ -56,15 +59,58 @@ TEST_CASE("JsonVariant::add<T>()") {
REQUIRE(doc.as<std::string>() == "[[1,2]]");
}
SECTION("JsonObject") {
JsonObject object = var.add<JsonObject>();
object["hello"] = "world";
REQUIRE(doc.as<std::string>() == "[{\"hello\":\"world\"}]");
}
SECTION("JsonVariant") {
JsonVariant variant = var.add<JsonVariant>();
variant.set(42);
REQUIRE(doc.as<std::string>() == "[42]");
}
}
TEST_CASE("JsonObject::add(JsonObject) ") {
JsonDocument doc1;
doc1["hello"_s] = "world"_s;
TimebombAllocator allocator(10);
SpyingAllocator spy(&allocator);
JsonDocument doc2(&spy);
JsonVariant variant = doc2.to<JsonVariant>();
SECTION("success") {
bool result = variant.add(doc1.as<JsonObject>());
REQUIRE(result == true);
REQUIRE(doc2.as<std::string>() == "[{\"hello\":\"world\"}]");
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("hello")),
Allocate(sizeofString("world")),
});
}
SECTION("partial failure") { // issue #2081
allocator.setCountdown(2);
bool result = variant.add(doc1.as<JsonObject>());
REQUIRE(result == false);
REQUIRE(doc2.as<std::string>() == "[]");
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("hello")),
AllocateFail(sizeofString("world")),
Deallocate(sizeofString("hello")),
});
}
SECTION("complete failure") {
allocator.setCountdown(0);
bool result = variant.add(doc1.as<JsonObject>());
REQUIRE(result == false);
REQUIRE(doc2.as<std::string>() == "[]");
REQUIRE(spy.log() == AllocatorLog{
AllocateFail(sizeofPool()),
});
}
}

View File

@ -6,6 +6,8 @@
#include <stdint.h>
#include <catch.hpp>
#include "Literals.hpp"
namespace my {
using ArduinoJson::detail::isinf;
} // namespace my
@ -25,17 +27,36 @@ TEST_CASE("JsonVariant::as()") {
REQUIRE(0 == variant.as<const char*>());
REQUIRE("null" == variant.as<std::string>());
REQUIRE(variant.as<JsonString>().isNull());
REQUIRE(variant.as<MsgPackBinary>().data() == nullptr);
REQUIRE(variant.as<MsgPackExtension>().data() == nullptr);
}
SECTION("set(4.2)") {
SECTION("set(float)") {
variant.set(4.2f);
REQUIRE(variant.as<bool>());
REQUIRE(0 == variant.as<const char*>());
REQUIRE(variant.as<std::string>() == "4.2");
REQUIRE(variant.as<long>() == 4L);
REQUIRE(variant.as<float>() == 4.2f);
REQUIRE(variant.as<unsigned>() == 4U);
REQUIRE(variant.as<JsonString>().isNull());
REQUIRE(variant.as<MsgPackBinary>().data() == nullptr);
REQUIRE(variant.as<MsgPackExtension>().data() == nullptr);
}
SECTION("set(double)") {
variant.set(4.2);
REQUIRE(variant.as<bool>());
REQUIRE(0 == variant.as<const char*>());
REQUIRE(variant.as<std::string>() == "4.2");
REQUIRE(variant.as<long>() == 4L);
REQUIRE(variant.as<double>() == 4.2);
REQUIRE(variant.as<unsigned>() == 4U);
REQUIRE(variant.as<JsonString>().isNull());
REQUIRE(variant.as<MsgPackBinary>().data() == nullptr);
REQUIRE(variant.as<MsgPackExtension>().data() == nullptr);
}
SECTION("set(0.0)") {
@ -44,6 +65,8 @@ TEST_CASE("JsonVariant::as()") {
REQUIRE(variant.as<bool>() == false);
REQUIRE(variant.as<long>() == 0L);
REQUIRE(variant.as<JsonString>().isNull());
REQUIRE(variant.as<MsgPackBinary>().data() == nullptr);
REQUIRE(variant.as<MsgPackExtension>().data() == nullptr);
}
SECTION("set(false)") {
@ -54,6 +77,8 @@ TEST_CASE("JsonVariant::as()") {
REQUIRE(variant.as<long>() == 0L);
REQUIRE(variant.as<std::string>() == "false");
REQUIRE(variant.as<JsonString>().isNull());
REQUIRE(variant.as<MsgPackBinary>().data() == nullptr);
REQUIRE(variant.as<MsgPackExtension>().data() == nullptr);
}
SECTION("set(true)") {
@ -64,43 +89,66 @@ TEST_CASE("JsonVariant::as()") {
REQUIRE(variant.as<long>() == 1L);
REQUIRE(variant.as<std::string>() == "true");
REQUIRE(variant.as<JsonString>().isNull());
REQUIRE(variant.as<MsgPackBinary>().data() == nullptr);
REQUIRE(variant.as<MsgPackExtension>().data() == nullptr);
}
SECTION("set(42)") {
variant.set(42);
SECTION("set(uint32_t)") {
variant.set(4294967295U);
REQUIRE(variant.as<bool>() == true);
REQUIRE(variant.as<double>() == 42.0);
REQUIRE(variant.as<int>() == 42);
REQUIRE(variant.as<unsigned int>() == 42U); // issue #1601
REQUIRE(variant.as<std::string>() == "42");
REQUIRE(variant.as<double>() == 4294967295.0);
REQUIRE(variant.as<int32_t>() == 0);
REQUIRE(variant.as<uint32_t>() == 4294967295U);
REQUIRE(variant.as<uint64_t>() == 4294967295U);
REQUIRE(variant.as<std::string>() == "4294967295");
REQUIRE(variant.as<JsonString>().isNull());
REQUIRE(variant.as<MsgPackBinary>().data() == nullptr);
REQUIRE(variant.as<MsgPackExtension>().data() == nullptr);
}
SECTION("set(42L)") {
variant.set(42L);
SECTION("set(int32_t)") {
variant.set(-2147483648LL);
REQUIRE(variant.as<bool>() == true);
REQUIRE(variant.as<double>() == 42.0);
REQUIRE(variant.as<std::string>() == "42");
REQUIRE(variant.as<double>() == -2147483648LL);
REQUIRE(variant.as<int32_t>() == -2147483648LL);
REQUIRE(variant.as<int64_t>() == -2147483648LL);
REQUIRE(variant.as<uint32_t>() == 0);
REQUIRE(variant.as<uint64_t>() == 0);
REQUIRE(variant.as<std::string>() == "-2147483648");
REQUIRE(variant.as<JsonString>().isNull());
REQUIRE(variant.as<MsgPackBinary>().data() == nullptr);
REQUIRE(variant.as<MsgPackExtension>().data() == nullptr);
}
SECTION("set(-42L)") {
variant.set(-42L);
REQUIRE(variant.as<double>() == -42.0);
REQUIRE(variant.as<std::string>() == "-42");
REQUIRE(variant.as<JsonString>().isNull());
}
SECTION("set(42UL)") {
variant.set(42UL);
SECTION("set(uint64_t)") {
variant.set(4294967296U);
REQUIRE(variant.as<bool>() == true);
REQUIRE(variant.as<double>() == 42.0);
REQUIRE(variant.as<std::string>() == "42");
REQUIRE(variant.as<double>() == 4294967296.0);
REQUIRE(variant.as<int32_t>() == 0);
REQUIRE(variant.as<uint32_t>() == 0);
REQUIRE(variant.as<uint64_t>() == 4294967296U);
REQUIRE(variant.as<std::string>() == "4294967296");
REQUIRE(variant.as<JsonString>().isNull());
REQUIRE(variant.as<MsgPackBinary>().data() == nullptr);
REQUIRE(variant.as<MsgPackExtension>().data() == nullptr);
}
SECTION("set(int64_t)") {
variant.set(-2147483649LL);
REQUIRE(variant.as<bool>() == true);
REQUIRE(variant.as<double>() == -2147483649LL);
REQUIRE(variant.as<int32_t>() == 0);
REQUIRE(variant.as<int64_t>() == -2147483649LL);
REQUIRE(variant.as<uint32_t>() == 0);
REQUIRE(variant.as<uint64_t>() == 0);
REQUIRE(variant.as<std::string>() == "-2147483649");
REQUIRE(variant.as<JsonString>().isNull());
REQUIRE(variant.as<MsgPackBinary>().data() == nullptr);
REQUIRE(variant.as<MsgPackExtension>().data() == nullptr);
}
SECTION("set(0L)") {
@ -144,20 +192,20 @@ TEST_CASE("JsonVariant::as()") {
REQUIRE(variant.as<bool>() == true);
REQUIRE(variant.as<long>() == 0L);
REQUIRE(variant.as<const char*>() == std::string("hello"));
REQUIRE(variant.as<const char*>() == std::string("hello"));
REQUIRE(variant.as<std::string>() == std::string("hello"));
REQUIRE(variant.as<const char*>() == "hello"_s);
REQUIRE(variant.as<const char*>() == "hello"_s);
REQUIRE(variant.as<std::string>() == "hello"_s);
REQUIRE(variant.as<JsonString>() == "hello");
}
SECTION("set(std::string(\"4.2\"))") {
variant.set(std::string("4.2"));
variant.set("4.2"_s);
REQUIRE(variant.as<bool>() == true);
REQUIRE(variant.as<long>() == 4L);
REQUIRE(variant.as<double>() == 4.2);
REQUIRE(variant.as<const char*>() == std::string("4.2"));
REQUIRE(variant.as<std::string>() == std::string("4.2"));
REQUIRE(variant.as<double>() == Approx(4.2));
REQUIRE(variant.as<const char*>() == "4.2"_s);
REQUIRE(variant.as<std::string>() == "4.2"_s);
REQUIRE(variant.as<JsonString>() == "4.2");
REQUIRE(variant.as<JsonString>().isLinked() == false);
}
@ -199,6 +247,13 @@ TEST_CASE("JsonVariant::as()") {
REQUIRE(variant.as<JsonString>().isNull());
}
SECTION("set(serialized(\"hello\"))") {
variant.set(serialized("hello"));
REQUIRE(variant.as<MsgPackBinary>().data() == nullptr);
REQUIRE(variant.as<MsgPackExtension>().data() == nullptr);
}
SECTION("to<JsonObject>()") {
JsonObject obj = variant.to<JsonObject>();
obj["key"] = "value";
@ -208,13 +263,13 @@ TEST_CASE("JsonVariant::as()") {
}
SECTION("as<std::string>()") {
REQUIRE(variant.as<std::string>() == std::string("{\"key\":\"value\"}"));
REQUIRE(variant.as<std::string>() == "{\"key\":\"value\"}"_s);
}
SECTION("ObjectAsJsonObject") {
JsonObject o = variant.as<JsonObject>();
REQUIRE(o.size() == 1);
REQUIRE(o["key"] == std::string("value"));
REQUIRE(o["key"] == "value"_s);
}
}
@ -228,7 +283,7 @@ TEST_CASE("JsonVariant::as()") {
}
SECTION("as<std::string>()") {
REQUIRE(variant.as<std::string>() == std::string("[4,2]"));
REQUIRE(variant.as<std::string>() == "[4,2]"_s);
}
SECTION("as<JsonArray>()") {
@ -245,7 +300,7 @@ TEST_CASE("JsonVariant::as()") {
REQUIRE(variant.as<long long>() == -9223372036854775807 - 1);
}
SECTION("Biggerst int64 positive") {
SECTION("Biggest int64 positive") {
variant.set("9223372036854775807");
REQUIRE(variant.as<long long>() == 9223372036854775807);
}

View File

@ -7,6 +7,7 @@
#include <catch.hpp>
#include "Allocators.hpp"
#include "Literals.hpp"
TEST_CASE("JsonVariant::clear()") {
SpyingAllocator spy;
@ -28,7 +29,7 @@ TEST_CASE("JsonVariant::clear()") {
}
SECTION("releases owned string") {
var.set(std::string("hello"));
var.set("hello"_s);
var.clear();
REQUIRE(spy.log() == AllocatorLog{

View File

@ -113,6 +113,42 @@ TEST_CASE("Compare JsonVariant with JsonVariant") {
CHECK_FALSE(a == b);
}
SECTION("MsgPackBinary('abc') vs MsgPackBinary('abc')") {
a.set(MsgPackBinary("abc", 4));
b.set(MsgPackBinary("abc", 4));
CHECK(a == b);
CHECK(a <= b);
CHECK(a >= b);
CHECK_FALSE(a != b);
CHECK_FALSE(a < b);
CHECK_FALSE(a > b);
}
SECTION("MsgPackBinary('abc') vs MsgPackBinary('bcd')") {
a.set(MsgPackBinary("abc", 4));
b.set(MsgPackBinary("bcd", 4));
CHECK(a != b);
CHECK(a < b);
CHECK(a <= b);
CHECK_FALSE(a == b);
CHECK_FALSE(a > b);
CHECK_FALSE(a >= b);
}
SECTION("MsgPackBinary('bcd') vs MsgPackBinary('abc')") {
a.set(MsgPackBinary("bcd", 4));
b.set(MsgPackBinary("abc", 4));
CHECK(a != b);
CHECK(a > b);
CHECK(a >= b);
CHECK_FALSE(a < b);
CHECK_FALSE(a <= b);
CHECK_FALSE(a == b);
}
SECTION("false vs true") {
a.set(false);
b.set(true);

View File

@ -1,26 +0,0 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <stdint.h>
#include <catch.hpp>
TEST_CASE("JsonVariant::containsKey()") {
JsonDocument doc;
JsonVariant var = doc.to<JsonVariant>();
SECTION("containsKey(const char*)") {
var["hello"] = "world";
REQUIRE(var.containsKey("hello") == true);
REQUIRE(var.containsKey("world") == false);
}
SECTION("containsKey(std::string)") {
var["hello"] = "world";
REQUIRE(var.containsKey(std::string("hello")) == true);
REQUIRE(var.containsKey(std::string("world")) == false);
}
}

View File

@ -140,15 +140,3 @@ TEST_CASE("Custom converter with specialization") {
REQUIRE(doc["value"]["imag"] == 3);
}
}
TEST_CASE("ConverterNeedsWriteableRef") {
using namespace ArduinoJson::detail;
CHECK(ConverterNeedsWriteableRef<int>::value == false);
CHECK(ConverterNeedsWriteableRef<float>::value == false);
CHECK(ConverterNeedsWriteableRef<JsonVariant>::value == true);
CHECK(ConverterNeedsWriteableRef<JsonVariantConst>::value == false);
CHECK(ConverterNeedsWriteableRef<JsonObject>::value == true);
CHECK(ConverterNeedsWriteableRef<JsonObjectConst>::value == false);
CHECK(ConverterNeedsWriteableRef<JsonArray>::value == true);
CHECK(ConverterNeedsWriteableRef<JsonArrayConst>::value == false);
}

View File

@ -6,6 +6,8 @@
#include <catch.hpp>
#include "Allocators.hpp"
#include "Literals.hpp"
TEST_CASE("JsonVariant::set(JsonVariant)") {
KillswitchAllocator killswitch;
SpyingAllocator spyingAllocator(&killswitch);
@ -72,7 +74,7 @@ TEST_CASE("JsonVariant::set(JsonVariant)") {
}
SECTION("stores std::string by copy") {
var1.set(std::string("hello!!"));
var1.set("hello!!"_s);
spyingAllocator.clearLog();
var2.set(var1);
@ -106,7 +108,7 @@ TEST_CASE("JsonVariant::set(JsonVariant)") {
}
SECTION("stores Serialized<std::string> by copy") {
var1.set(serialized(std::string("hello!!")));
var1.set(serialized("hello!!"_s));
spyingAllocator.clearLog();
var2.set(var1);
@ -117,7 +119,7 @@ TEST_CASE("JsonVariant::set(JsonVariant)") {
}
SECTION("fails gracefully if raw string allocation fails") {
var1.set(serialized(std::string("hello!!")));
var1.set(serialized("hello!!"_s));
killswitch.on();
spyingAllocator.clearLog();

View File

@ -7,6 +7,7 @@
#include <catch.hpp>
#include "Allocators.hpp"
#include "Literals.hpp"
using ArduinoJson::detail::sizeofArray;
@ -15,9 +16,9 @@ TEST_CASE("JsonVariant::remove(int)") {
JsonDocument doc(&spy);
SECTION("release top level strings") {
doc.add(std::string("hello"));
doc.add(std::string("hello"));
doc.add(std::string("world"));
doc.add("hello"_s);
doc.add("hello"_s);
doc.add("world"_s);
JsonVariant var = doc.as<JsonVariant>();
REQUIRE(var.as<std::string>() == "[\"hello\",\"hello\",\"world\"]");
@ -43,7 +44,7 @@ TEST_CASE("JsonVariant::remove(int)") {
}
SECTION("release strings in nested array") {
doc[0][0] = std::string("hello");
doc[0][0] = "hello"_s;
JsonVariant var = doc.as<JsonVariant>();
REQUIRE(var.as<std::string>() == "[[\"hello\"]]");
@ -77,7 +78,34 @@ TEST_CASE("JsonVariant::remove(std::string)") {
var["a"] = 1;
var["b"] = 2;
var.remove(std::string("b"));
var.remove("b"_s);
REQUIRE(var.as<std::string>() == "{\"a\":1}");
}
TEST_CASE("JsonVariant::remove(JsonVariant) from object") {
JsonDocument doc;
JsonVariant var = doc.to<JsonVariant>();
var["a"] = "a";
var["b"] = 2;
var["c"] = "b";
var.remove(var["c"]);
REQUIRE(var.as<std::string>() == "{\"a\":\"a\",\"c\":\"b\"}");
}
TEST_CASE("JsonVariant::remove(JsonVariant) from array") {
JsonDocument doc;
JsonVariant var = doc.to<JsonVariant>();
var[0] = 3;
var[1] = 2;
var[2] = 1;
var.remove(var[2]);
var.remove(var[3]); // noop
REQUIRE(var.as<std::string>() == "[3,1]");
}

View File

@ -6,13 +6,15 @@
#include <catch.hpp>
#include "Allocators.hpp"
#include "Literals.hpp"
using ArduinoJson::detail::sizeofObject;
enum ErrorCode { ERROR_01 = 1, ERROR_10 = 10 };
TEST_CASE("JsonVariant::set() when there is enough memory") {
JsonDocument doc;
SpyingAllocator spy;
JsonDocument doc(&spy);
JsonVariant variant = doc.to<JsonVariant>();
SECTION("const char*") {
@ -24,6 +26,7 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
REQUIRE(result == true);
REQUIRE(variant == "world"); // stores by pointer
REQUIRE(spy.log() == AllocatorLog{});
}
SECTION("(const char*)0") {
@ -31,6 +34,7 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
REQUIRE(result == true);
REQUIRE(variant.isNull());
REQUIRE(spy.log() == AllocatorLog{});
}
SECTION("char*") {
@ -42,6 +46,9 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
REQUIRE(result == true);
REQUIRE(variant == "hello"); // stores by copy
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofString("hello")),
});
}
SECTION("(char*)0") {
@ -49,6 +56,7 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
REQUIRE(result == true);
REQUIRE(variant.isNull());
REQUIRE(spy.log() == AllocatorLog{});
}
SECTION("unsigned char*") {
@ -60,6 +68,9 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
REQUIRE(result == true);
REQUIRE(variant == "hello"); // stores by copy
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofString("hello")),
});
}
SECTION("signed char*") {
@ -71,6 +82,9 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
REQUIRE(result == true);
REQUIRE(variant == "hello"); // stores by copy
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofString("hello")),
});
}
#ifdef HAS_VARIABLE_LENGTH_ARRAY
@ -84,6 +98,9 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
REQUIRE(result == true);
REQUIRE(variant == "hello"); // stores by copy
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofString("hello")),
});
}
#endif
@ -96,6 +113,9 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
REQUIRE(result == true);
REQUIRE(variant == "hello"); // stores by copy
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofString("hello")),
});
}
SECTION("static JsonString") {
@ -107,6 +127,7 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
REQUIRE(result == true);
REQUIRE(variant == "world"); // stores by pointer
REQUIRE(spy.log() == AllocatorLog{});
}
SECTION("non-static JsonString") {
@ -118,6 +139,9 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
REQUIRE(result == true);
REQUIRE(variant == "hello"); // stores by copy
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofString("hello")),
});
}
SECTION("enum") {
@ -128,6 +152,89 @@ TEST_CASE("JsonVariant::set() when there is enough memory") {
REQUIRE(result == true);
REQUIRE(variant.is<int>() == true);
REQUIRE(variant.as<int>() == 10);
REQUIRE(spy.log() == AllocatorLog{});
}
SECTION("float") {
bool result = variant.set(1.2f);
REQUIRE(result == true);
REQUIRE(variant.is<float>() == true);
REQUIRE(variant.as<float>() == 1.2f);
REQUIRE(spy.log() == AllocatorLog{});
}
SECTION("double") {
bool result = variant.set(1.2);
doc.shrinkToFit();
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
});
}
SECTION("int32_t") {
bool result = variant.set(int32_t(42));
REQUIRE(result == true);
REQUIRE(variant.is<int32_t>() == true);
REQUIRE(variant.as<int32_t>() == 42);
REQUIRE(spy.log() == AllocatorLog{});
}
SECTION("int64_t") {
bool result = variant.set(int64_t(-2147483649LL));
doc.shrinkToFit();
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
});
}
SECTION("uint32_t") {
bool result = variant.set(uint32_t(42));
REQUIRE(result == true);
REQUIRE(variant.is<uint32_t>() == true);
REQUIRE(variant.as<uint32_t>() == 42);
REQUIRE(spy.log() == AllocatorLog{});
}
SECTION("uint64_t") {
bool result = variant.set(uint64_t(4294967296));
doc.shrinkToFit();
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
});
}
SECTION("JsonDocument") {
JsonDocument doc1;
doc1["hello"] = "world";
// Should copy the doc
variant.set(doc1);
doc1.clear();
std::string json;
serializeJson(doc, json);
REQUIRE(json == "{\"hello\":\"world\"}");
}
}
@ -137,14 +244,14 @@ TEST_CASE("JsonVariant::set() with not enough memory") {
JsonVariant v = doc.to<JsonVariant>();
SECTION("std::string") {
bool result = v.set(std::string("hello world!!"));
bool result = v.set("hello world!!"_s);
REQUIRE(result == false);
REQUIRE(v.isNull());
}
SECTION("Serialized<std::string>") {
bool result = v.set(serialized(std::string("hello world!!")));
bool result = v.set(serialized("hello world!!"_s));
REQUIRE(result == false);
REQUIRE(v.isNull());
@ -157,28 +264,54 @@ TEST_CASE("JsonVariant::set() with not enough memory") {
REQUIRE(result == false);
REQUIRE(v.isNull());
}
}
TEST_CASE("JsonVariant::set(JsonDocument)") {
JsonDocument doc1;
doc1["hello"] = "world";
SECTION("float") {
bool result = v.set(1.2f);
JsonDocument doc2;
JsonVariant v = doc2.to<JsonVariant>();
REQUIRE(result == true);
REQUIRE(v.is<float>());
}
// Should copy the doc
v.set(doc1);
doc1.clear();
SECTION("double") {
bool result = v.set(1.2);
std::string json;
serializeJson(doc2, json);
REQUIRE(json == "{\"hello\":\"world\"}");
REQUIRE(result == false);
REQUIRE(v.isNull());
}
SECTION("int32_t") {
bool result = v.set(-42);
REQUIRE(result == true);
REQUIRE(v.is<int32_t>());
}
SECTION("int64_t") {
bool result = v.set(-2147483649LL);
REQUIRE(result == false);
REQUIRE(v.isNull());
}
SECTION("uint32_t") {
bool result = v.set(42);
REQUIRE(result == true);
REQUIRE(v.is<uint32_t>());
}
SECTION("uint64_t") {
bool result = v.set(4294967296U);
REQUIRE(result == false);
REQUIRE(v.isNull());
}
}
TEST_CASE("JsonVariant::set() releases the previous value") {
SpyingAllocator spy;
JsonDocument doc(&spy);
doc["hello"] = std::string("world");
doc["hello"] = "world"_s;
spy.clearLog();
JsonVariant v = doc["hello"];
@ -219,3 +352,34 @@ TEST_CASE("JsonVariant::set() releases the previous value") {
});
}
}
TEST_CASE("JsonVariant::set() reuses extension slot") {
SpyingAllocator spy;
JsonDocument doc(&spy);
JsonVariant variant = doc.to<JsonVariant>();
variant.set(1.2);
doc.shrinkToFit();
spy.clearLog();
SECTION("double") {
bool result = variant.set(3.4);
REQUIRE(result == true);
REQUIRE(spy.log() == AllocatorLog{});
}
SECTION("int64_t") {
bool result = variant.set(-2147483649LL);
REQUIRE(result == true);
REQUIRE(spy.log() == AllocatorLog{});
}
SECTION("uint64_t") {
bool result = variant.set(4294967296U);
REQUIRE(result == true);
REQUIRE(spy.log() == AllocatorLog{});
}
}

View File

@ -5,6 +5,8 @@
#include <ArduinoJson.h>
#include <catch.hpp>
#include "Literals.hpp"
TEST_CASE("JsonVariant::operator[]") {
JsonDocument doc;
JsonVariant var = doc.to<JsonVariant>();
@ -31,9 +33,9 @@ TEST_CASE("JsonVariant::operator[]") {
REQUIRE(2 == var.size());
var[0].as<std::string>();
// REQUIRE(std::string("element at index 0") == );
REQUIRE(std::string("element at index 1") == var[1]);
REQUIRE(std::string("element at index 0") ==
// REQUIRE("element at index 0"_s == );
REQUIRE("element at index 1"_s == var[1]);
REQUIRE("element at index 0"_s ==
var[static_cast<unsigned char>(0)]); // issue #381
REQUIRE(var[666].isNull());
REQUIRE(var[3].isNull());
@ -46,7 +48,7 @@ TEST_CASE("JsonVariant::operator[]") {
var[1] = "world";
REQUIRE(var.size() == 2);
REQUIRE(std::string("world") == var[1]);
REQUIRE("world"_s == var[1]);
}
SECTION("set value in a nested object") {
@ -56,7 +58,7 @@ TEST_CASE("JsonVariant::operator[]") {
REQUIRE(1 == var.size());
REQUIRE(1 == var[0].size());
REQUIRE(std::string("world") == var[0]["hello"]);
REQUIRE("world"_s == var[0]["hello"]);
}
SECTION("variant[0] when variant contains an integer") {
@ -67,6 +69,15 @@ TEST_CASE("JsonVariant::operator[]") {
REQUIRE(var.is<int>());
REQUIRE(var.as<int>() == 123);
}
SECTION("use JsonVariant as index") {
array.add("A");
array.add("B");
array.add(1);
REQUIRE(var[var[2]] == "B");
REQUIRE(var[var[3]].isNull());
}
}
SECTION("The JsonVariant is a JsonObject") {
@ -77,8 +88,8 @@ TEST_CASE("JsonVariant::operator[]") {
object["b"] = "element at key \"b\"";
REQUIRE(2 == var.size());
REQUIRE(std::string("element at key \"a\"") == var["a"]);
REQUIRE(std::string("element at key \"b\"") == var["b"]);
REQUIRE("element at key \"a\""_s == var["a"]);
REQUIRE("element at key \"b\""_s == var["b"]);
REQUIRE(var["c"].isNull());
REQUIRE(var[0].isNull());
}
@ -87,7 +98,7 @@ TEST_CASE("JsonVariant::operator[]") {
var["hello"] = "world";
REQUIRE(1 == var.size());
REQUIRE(std::string("world") == var["hello"]);
REQUIRE("world"_s == var["hello"]);
}
SECTION("set value, key is a char[]") {
@ -96,13 +107,22 @@ TEST_CASE("JsonVariant::operator[]") {
key[0] = '!'; // make sure the key is duplicated
REQUIRE(1 == var.size());
REQUIRE(std::string("world") == var["hello"]);
REQUIRE("world"_s == var["hello"]);
}
SECTION("var[key].to<JsonArray>()") {
JsonArray arr = var["hello"].to<JsonArray>();
REQUIRE(arr.isNull() == false);
}
SECTION("use JsonVariant as key") {
object["a"] = "a";
object["b"] = "b";
object["c"] = "b";
REQUIRE(var[var["c"]] == "b");
REQUIRE(var[var["d"]].isNull());
}
}
#if defined(HAS_VARIABLE_LENGTH_ARRAY) && \
@ -115,7 +135,7 @@ TEST_CASE("JsonVariant::operator[]") {
deserializeJson(doc, "{\"hello\":\"world\"}");
JsonVariant variant = doc.as<JsonVariant>();
REQUIRE(std::string("world") == variant[vla]);
REQUIRE("world"_s == variant[vla]);
}
SECTION("key is a VLA, const JsonVariant") {
@ -126,7 +146,7 @@ TEST_CASE("JsonVariant::operator[]") {
deserializeJson(doc, "{\"hello\":\"world\"}");
const JsonVariant variant = doc.as<JsonVariant>();
REQUIRE(std::string("world") == variant[vla]);
REQUIRE("world"_s == variant[vla]);
}
#endif
}

View File

@ -5,6 +5,8 @@
#include <ArduinoJson.h>
#include <catch.hpp>
#include "Literals.hpp"
TEST_CASE("Unbound JsonVariant") {
JsonVariant variant;
@ -21,6 +23,10 @@ TEST_CASE("Unbound JsonVariant") {
CHECK(variant.as<JsonObject>().isNull());
CHECK(variant.as<JsonObjectConst>().isNull());
CHECK(variant.as<JsonString>().isNull());
CHECK(variant.as<MsgPackBinary>().data() == nullptr);
CHECK(variant.as<MsgPackBinary>().size() == 0);
CHECK(variant.as<MsgPackExtension>().data() == nullptr);
CHECK(variant.as<MsgPackExtension>().size() == 0);
}
SECTION("is<T>()") {
@ -44,8 +50,10 @@ TEST_CASE("Unbound JsonVariant") {
CHECK_FALSE(variant.set(42L));
CHECK_FALSE(variant.set(42U));
CHECK_FALSE(variant.set(serialized("42")));
CHECK_FALSE(variant.set(serialized(std::string("42"))));
CHECK_FALSE(variant.set(serialized("42"_s)));
CHECK_FALSE(variant.set(true));
CHECK_FALSE(variant.set(MsgPackBinary("hello", 5)));
CHECK_FALSE(variant.set(MsgPackExtension(1, "hello", 5)));
}
SECTION("add()") {
@ -62,11 +70,7 @@ TEST_CASE("Unbound JsonVariant") {
CHECK(variant["key"].isNull());
CHECK_FALSE(variant[0].set(1));
CHECK_FALSE(variant["key"].set(1));
CHECK_FALSE(variant[std::string("key")].set(1));
}
SECTION("containsKey()") {
CHECK_FALSE(variant.containsKey("hello"));
CHECK_FALSE(variant["key"_s].set(1));
}
SECTION("remove()") {

View File

@ -4,7 +4,6 @@
add_executable(JsonVariantConstTests
as.cpp
containsKey.cpp
is.cpp
isnull.cpp
nesting.cpp

View File

@ -6,6 +6,8 @@
#include <stdint.h>
#include <catch.hpp>
#include "Literals.hpp"
TEST_CASE("JsonVariantConst::as<T>()") {
JsonDocument doc;
JsonVariantConst var = doc.to<JsonVariant>();
@ -14,6 +16,27 @@ TEST_CASE("JsonVariantConst::as<T>()") {
REQUIRE(var.as<bool>() == true);
REQUIRE(var.as<long>() == 0L);
REQUIRE(var.as<const char*>() == std::string("hello"));
REQUIRE(var.as<std::string>() == std::string("hello"));
REQUIRE(var.as<const char*>() == "hello"_s);
REQUIRE(var.as<std::string>() == "hello"_s);
}
TEST_CASE("Invalid conversions") {
using namespace ArduinoJson::detail;
JsonVariantConst variant;
CHECK(is_same<decltype(variant.as<int>()), int>::value);
CHECK(is_same<decltype(variant.as<float>()), float>::value);
CHECK(is_same<decltype(variant.as<JsonVariantConst>()),
JsonVariantConst>::value);
CHECK(
is_same<decltype(variant.as<JsonObjectConst>()), JsonObjectConst>::value);
CHECK(is_same<decltype(variant.as<JsonArrayConst>()), JsonArrayConst>::value);
CHECK(is_same<decltype(variant.as<JsonVariant>()),
InvalidConversion<JsonVariantConst, JsonVariant>>::value);
CHECK(is_same<decltype(variant.as<JsonObject>()),
InvalidConversion<JsonVariantConst, JsonObject>>::value);
CHECK(is_same<decltype(variant.as<JsonArray>()),
InvalidConversion<JsonVariantConst, JsonArray>>::value);
}

View File

@ -1,33 +0,0 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <stdint.h>
#include <catch.hpp>
TEST_CASE("JsonVariantConst::containsKey()") {
JsonDocument doc;
doc["hello"] = "world";
JsonVariantConst var = doc.as<JsonVariant>();
SECTION("support const char*") {
REQUIRE(var.containsKey("hello") == true);
REQUIRE(var.containsKey("world") == false);
}
SECTION("support std::string") {
REQUIRE(var.containsKey(std::string("hello")) == true);
REQUIRE(var.containsKey(std::string("world")) == false);
}
#ifdef HAS_VARIABLE_LENGTH_ARRAY
SECTION("supports VLA") {
size_t i = 16;
char vla[i];
strcpy(vla, "hello");
REQUIRE(true == var.containsKey(vla));
}
#endif
}

View File

@ -5,6 +5,8 @@
#include <ArduinoJson.h>
#include <catch.hpp>
#include "Literals.hpp"
TEST_CASE("JsonVariantConst::operator[]") {
JsonDocument doc;
JsonVariantConst var = doc.to<JsonVariant>();
@ -27,13 +29,23 @@ TEST_CASE("JsonVariantConst::operator[]") {
array.add("A");
array.add("B");
REQUIRE(std::string("A") == var[0]);
REQUIRE(std::string("B") == var[1]);
REQUIRE(std::string("A") ==
var[static_cast<unsigned char>(0)]); // issue #381
REQUIRE(var[666].isNull());
REQUIRE(var[3].isNull());
REQUIRE(var["0"].isNull());
SECTION("int") {
REQUIRE("A"_s == var[0]);
REQUIRE("B"_s == var[1]);
REQUIRE("A"_s == var[static_cast<unsigned char>(0)]); // issue #381
REQUIRE(var[666].isNull());
REQUIRE(var[3].isNull());
}
SECTION("const char*") {
REQUIRE(var["0"].isNull());
}
SECTION("JsonVariant") {
array.add(1);
REQUIRE(var[var[2]] == "B"_s);
REQUIRE(var[var[3]].isNull());
}
}
SECTION("object") {
@ -42,16 +54,16 @@ TEST_CASE("JsonVariantConst::operator[]") {
object["b"] = "B";
SECTION("supports const char*") {
REQUIRE(std::string("A") == var["a"]);
REQUIRE(std::string("B") == var["b"]);
REQUIRE("A"_s == var["a"]);
REQUIRE("B"_s == var["b"]);
REQUIRE(var["c"].isNull());
REQUIRE(var[0].isNull());
}
SECTION("supports std::string") {
REQUIRE(std::string("A") == var[std::string("a")]);
REQUIRE(std::string("B") == var[std::string("b")]);
REQUIRE(var[std::string("c")].isNull());
REQUIRE("A"_s == var["a"_s]);
REQUIRE("B"_s == var["b"_s]);
REQUIRE(var["c"_s].isNull());
}
#if defined(HAS_VARIABLE_LENGTH_ARRAY) && \
@ -61,8 +73,14 @@ TEST_CASE("JsonVariantConst::operator[]") {
char vla[i];
strcpy(vla, "a");
REQUIRE(std::string("A") == var[vla]);
REQUIRE("A"_s == var[vla]);
}
#endif
SECTION("supports JsonVariant") {
object["c"] = "b";
REQUIRE(var[var["c"]] == "B");
REQUIRE(var[var["d"]].isNull());
}
}
}

View File

@ -5,7 +5,6 @@
add_executable(MiscTests
arithmeticCompare.cpp
conflicts.cpp
FloatParts.cpp
issue1967.cpp
JsonString.cpp
NoArduinoHeader.cpp

View File

@ -9,6 +9,7 @@
#include <catch.hpp>
#include "Literals.hpp"
#include "custom_string.hpp"
using namespace ArduinoJson::detail;
@ -36,13 +37,13 @@ void common_tests(StringWriter& writer, const String& output) {
SECTION("OneString") {
REQUIRE(4 == print(writer, "ABCD"));
REQUIRE(std::string("ABCD") == output);
REQUIRE("ABCD"_s == output);
}
SECTION("TwoStrings") {
REQUIRE(4 == print(writer, "ABCD"));
REQUIRE(4 == print(writer, "EFGH"));
REQUIRE(std::string("ABCDEFGH") == output);
REQUIRE("ABCDEFGH"_s == output);
}
}

View File

@ -212,3 +212,8 @@ TEST_CASE("Polyfills/type_traits") {
CHECK(is_enum<double>::value == false);
}
}
TEST_CASE("is_std_string") {
REQUIRE(is_std_string<std::string>::value == true);
REQUIRE(is_std_string<EmptyClass>::value == false);
}

View File

@ -58,5 +58,10 @@
// issue #1914
#define V7 7
// STM32, Mbed, Particle
#define A0 16
#define A1 17
#define A2 18
// catch.hpp mutes several warnings, this file also allows to detect them
#include "ArduinoJson.h"

View File

@ -7,6 +7,5 @@
#include <string>
struct custom_char_traits : std::char_traits<char> {};
struct custom_allocator : std::allocator<char> {};
typedef std::basic_string<char, custom_char_traits, custom_allocator>
custom_string;
typedef std::basic_string<char, custom_char_traits> custom_string;

View File

@ -5,6 +5,8 @@
#include <ArduinoJson.h>
#include <catch.hpp>
#include "Literals.hpp"
#if defined(__clang__)
# define CONFLICTS_WITH_BUILTIN_OPERATOR
#endif
@ -111,7 +113,7 @@ TEST_CASE("unsigned char[]") {
deserializeJson(doc, "{\"hello\":\"world\"}");
JsonVariant variant = doc.as<JsonVariant>();
REQUIRE(std::string("world") == variant[key]);
REQUIRE("world"_s == variant[key]);
}
#endif
@ -122,7 +124,7 @@ TEST_CASE("unsigned char[]") {
deserializeJson(doc, "{\"hello\":\"world\"}");
const JsonVariant variant = doc.as<JsonVariant>();
REQUIRE(std::string("world") == variant[key]);
REQUIRE("world"_s == variant[key]);
}
#endif
@ -160,7 +162,7 @@ TEST_CASE("unsigned char[]") {
JsonObject obj = doc.to<JsonObject>();
obj[key] = "world";
REQUIRE(std::string("world") == obj["hello"]);
REQUIRE("world"_s == obj["hello"]);
}
SECTION("JsonObject::operator[] const") {
@ -170,19 +172,10 @@ TEST_CASE("unsigned char[]") {
deserializeJson(doc, "{\"hello\":\"world\"}");
JsonObject obj = doc.as<JsonObject>();
REQUIRE(std::string("world") == obj[key]);
REQUIRE("world"_s == obj[key]);
}
#endif
SECTION("containsKey()") {
unsigned char key[] = "hello";
JsonDocument doc;
deserializeJson(doc, "{\"hello\":\"world\"}");
JsonObject obj = doc.as<JsonObject>();
REQUIRE(true == obj.containsKey(key));
}
SECTION("remove()") {
unsigned char key[] = "hello";
@ -203,7 +196,7 @@ TEST_CASE("unsigned char[]") {
JsonObject obj = doc.to<JsonObject>();
obj["hello"] = value;
REQUIRE(std::string("world") == obj["hello"]);
REQUIRE("world"_s == obj["hello"]);
}
SECTION("set()") {
@ -213,7 +206,7 @@ TEST_CASE("unsigned char[]") {
JsonObject obj = doc.to<JsonObject>();
obj["hello"].set(value);
REQUIRE(std::string("world") == obj["hello"]);
REQUIRE("world"_s == obj["hello"]);
}
}
@ -225,7 +218,7 @@ TEST_CASE("unsigned char[]") {
JsonArray arr = doc.to<JsonArray>();
arr.add(value);
REQUIRE(std::string("world") == arr[0]);
REQUIRE("world"_s == arr[0]);
}
}
@ -238,7 +231,7 @@ TEST_CASE("unsigned char[]") {
arr.add("hello");
arr[0].set(value);
REQUIRE(std::string("world") == arr[0]);
REQUIRE("world"_s == arr[0]);
}
SECTION("operator=") {
@ -249,7 +242,7 @@ TEST_CASE("unsigned char[]") {
arr.add("hello");
arr[0] = value;
REQUIRE(std::string("world") == arr[0]);
REQUIRE("world"_s == arr[0]);
}
}
}

View File

@ -15,6 +15,9 @@ add_executable(MixedConfigurationTests
enable_nan_1.cpp
enable_progmem_1.cpp
issue1707.cpp
string_length_size_1.cpp
string_length_size_2.cpp
string_length_size_4.cpp
use_double_0.cpp
use_double_1.cpp
use_long_long_0.cpp

View File

@ -0,0 +1,131 @@
#define ARDUINOJSON_STRING_LENGTH_SIZE 1
#include <ArduinoJson.h>
#include <catch.hpp>
#include <string>
#include "Literals.hpp"
TEST_CASE("ARDUINOJSON_STRING_LENGTH_SIZE == 1") {
JsonDocument doc;
SECTION("set(std::string)") {
SECTION("returns true if len <= 255") {
auto result = doc.set(std::string(255, '?'));
REQUIRE(result == true);
REQUIRE(doc.overflowed() == false);
}
SECTION("returns false if len >= 256") {
auto result = doc.set(std::string(256, '?'));
REQUIRE(result == false);
REQUIRE(doc.overflowed() == true);
}
}
SECTION("set(MsgPackBinary)") {
SECTION("returns true if size <= 253") {
auto str = std::string(253, '?');
auto result = doc.set(MsgPackBinary(str.data(), str.size()));
REQUIRE(result == true);
REQUIRE(doc.overflowed() == false);
}
SECTION("returns false if size >= 254") {
auto str = std::string(254, '?');
auto result = doc.set(MsgPackBinary(str.data(), str.size()));
REQUIRE(result == false);
REQUIRE(doc.overflowed() == true);
}
}
SECTION("set(MsgPackExtension)") {
SECTION("returns true if size <= 252") {
auto str = std::string(252, '?');
auto result = doc.set(MsgPackExtension(1, str.data(), str.size()));
REQUIRE(result == true);
REQUIRE(doc.overflowed() == false);
}
SECTION("returns false if size >= 253") {
auto str = std::string(253, '?');
auto result = doc.set(MsgPackExtension(1, str.data(), str.size()));
REQUIRE(result == false);
REQUIRE(doc.overflowed() == true);
}
}
SECTION("deserializeJson()") {
SECTION("returns Ok if string length <= 255") {
auto input = "\"" + std::string(255, '?') + "\"";
auto err = deserializeJson(doc, input);
REQUIRE(err == DeserializationError::Ok);
}
SECTION("returns NoMemory if string length >= 256") {
auto input = "\"" + std::string(256, '?') + "\"";
auto err = deserializeJson(doc, input);
REQUIRE(err == DeserializationError::NoMemory);
}
}
SECTION("deserializeMsgPack()") {
SECTION("returns Ok if string length <= 255") {
auto input = "\xd9\xff" + std::string(255, '?');
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::Ok);
}
SECTION("returns NoMemory if string length >= 256") {
auto input = "\xda\x01\x00"_s + std::string(256, '?');
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::NoMemory);
}
SECTION("returns Ok if binary size <= 253") {
auto input = "\xc4\xfd" + std::string(253, '?');
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::Ok);
}
SECTION("returns NoMemory if binary size >= 254") {
auto input = "\xc4\xfe" + std::string(254, '?');
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::NoMemory);
}
SECTION("returns Ok if extension size <= 252") {
auto input = "\xc7\xfc\x01" + std::string(252, '?');
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::Ok);
}
SECTION("returns NoMemory if binary size >= 253") {
auto input = "\xc7\xfd\x01" + std::string(253, '?');
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::NoMemory);
}
}
}

View File

@ -0,0 +1,140 @@
#define ARDUINOJSON_STRING_LENGTH_SIZE 2
#include <ArduinoJson.h>
#include <catch.hpp>
#include <string>
#include "Literals.hpp"
TEST_CASE("ARDUINOJSON_STRING_LENGTH_SIZE == 2") {
JsonDocument doc;
SECTION("set(std::string)") {
SECTION("returns true if len <= 65535") {
auto result = doc.set(std::string(65535, '?'));
REQUIRE(result == true);
REQUIRE(doc.overflowed() == false);
}
SECTION("returns false if len >= 65536") {
auto result = doc.set(std::string(65536, '?'));
REQUIRE(result == false);
REQUIRE(doc.overflowed() == true);
}
}
SECTION("set(MsgPackBinary)") {
SECTION("returns true if size <= 65532") {
auto str = std::string(65532, '?');
auto result = doc.set(MsgPackBinary(str.data(), str.size()));
REQUIRE(result == true);
REQUIRE(doc.overflowed() == false);
}
SECTION("returns false if size >= 65533") {
auto str = std::string(65533, '?');
auto result = doc.set(MsgPackBinary(str.data(), str.size()));
REQUIRE(result == false);
REQUIRE(doc.overflowed() == true);
}
}
SECTION("set(MsgPackExtension)") {
SECTION("returns true if size <= 65531") {
auto str = std::string(65531, '?');
auto result = doc.set(MsgPackExtension(1, str.data(), str.size()));
REQUIRE(result == true);
REQUIRE(doc.overflowed() == false);
}
SECTION("returns false if size >= 65532") {
auto str = std::string(65532, '?');
auto result = doc.set(MsgPackExtension(1, str.data(), str.size()));
REQUIRE(result == false);
REQUIRE(doc.overflowed() == true);
}
}
SECTION("deserializeJson()") {
SECTION("returns Ok if string length <= 65535") {
auto input = "\"" + std::string(65535, '?') + "\"";
auto err = deserializeJson(doc, input);
REQUIRE(err == DeserializationError::Ok);
}
SECTION("returns NoMemory if string length >= 65536") {
auto input = "\"" + std::string(65536, '?') + "\"";
auto err = deserializeJson(doc, input);
REQUIRE(err == DeserializationError::NoMemory);
}
}
SECTION("deserializeMsgPack()") {
SECTION("returns Ok if string length <= 65535") {
auto input = "\xda\xff\xff" + std::string(65535, '?');
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::Ok);
}
SECTION("returns NoMemory if string length >= 65536") {
auto input = "\xdb\x00\x01\x00\x00"_s + std::string(65536, '?');
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::NoMemory);
}
SECTION("returns Ok if binary size <= 65532") {
auto input = "\xc5\xff\xfc" + std::string(65532, '?');
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::Ok);
}
SECTION("returns NoMemory if binary size >= 65534") {
auto input = "\xc5\xff\xfd" + std::string(65534, '?');
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::NoMemory);
}
// https://oss-fuzz.com/testcase?key=5354792971993088
SECTION("doesn't overflow if binary size == 0xFFFF") {
auto input = "\xc5\xff\xff"_s;
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::NoMemory);
}
SECTION("returns Ok if extension size <= 65531") {
auto input = "\xc8\xff\xfb\x01" + std::string(65531, '?');
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::Ok);
}
SECTION("returns NoMemory if extension size >= 65532") {
auto input = "\xc8\xff\xfc\x01" + std::string(65532, '?');
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::NoMemory);
}
}
}

View File

@ -0,0 +1,146 @@
#define ARDUINOJSON_STRING_LENGTH_SIZE 4
#include <ArduinoJson.h>
#include <catch.hpp>
#include <string>
#include "Literals.hpp"
TEST_CASE("ARDUINOJSON_STRING_LENGTH_SIZE == 4") {
JsonDocument doc;
SECTION("set(std::string)") {
SECTION("returns true if string length >= 65536") {
auto result = doc.set(std::string(65536, '?'));
REQUIRE(result == true);
REQUIRE(doc.overflowed() == false);
}
}
SECTION("set(MsgPackBinary)") {
SECTION("returns true if size >= 65536") {
auto str = std::string(65536, '?');
auto result = doc.set(MsgPackBinary(str.data(), str.size()));
REQUIRE(result == true);
REQUIRE(doc.overflowed() == false);
}
}
SECTION("set(MsgPackExtension)") {
SECTION("returns true if size >= 65532") {
auto str = std::string(65532, '?');
auto result = doc.set(MsgPackExtension(1, str.data(), str.size()));
REQUIRE(result == true);
REQUIRE(doc.overflowed() == false);
}
}
SECTION("deserializeJson()") {
SECTION("returns Ok if string length >= 65536") {
auto input = "\"" + std::string(65536, '?') + "\"";
auto err = deserializeJson(doc, input);
REQUIRE(err == DeserializationError::Ok);
}
}
SECTION("deserializeMsgPack()") {
SECTION("returns Ok if string size >= 65536") {
auto input = "\xda\xff\xff" + std::string(65536, '?');
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::Ok);
}
SECTION("returns Ok if binary size >= 65536") {
auto input = "\xc5\xff\xff" + std::string(65536, '?');
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::Ok);
}
SECTION("returns Ok if extension size >= 65532") {
auto input = "\xc8\xff\xfb\x01" + std::string(65532, '?');
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::Ok);
}
// https://oss-fuzz.com/testcase?key=5354792971993088
SECTION("doesn't overflow if binary size == 0xFFFFFFFF") {
auto input = "\xc6\xff\xff\xff\xff"_s;
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::NoMemory);
}
SECTION("doesn't overflow if string size == 0xFFFFFFFF") {
auto input = "\xdb\xff\xff\xff\xff???????????????????"_s;
auto err = deserializeMsgPack(doc, input);
REQUIRE(err != DeserializationError::Ok);
}
}
SECTION("bin 32 deserialization") {
auto str = std::string(65536, '?');
auto input = "\xc6\x00\x01\x00\x00"_s + str;
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.is<MsgPackBinary>());
auto binary = doc.as<MsgPackBinary>();
REQUIRE(binary.size() == 65536);
REQUIRE(binary.data() != nullptr);
REQUIRE(std::string(reinterpret_cast<const char*>(binary.data()),
binary.size()) == str);
}
SECTION("bin 32 serialization") {
auto str = std::string(65536, '?');
doc.set(MsgPackBinary(str.data(), str.size()));
std::string output;
auto result = serializeMsgPack(doc, output);
REQUIRE(result == 5 + str.size());
REQUIRE(output == "\xc6\x00\x01\x00\x00"_s + str);
}
SECTION("ext 32 deserialization") {
auto str = std::string(65536, '?');
auto input = "\xc9\x00\x01\x00\x00\x2a"_s + str;
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.is<MsgPackExtension>());
auto value = doc.as<MsgPackExtension>();
REQUIRE(value.type() == 42);
REQUIRE(value.size() == 65536);
REQUIRE(value.data() != nullptr);
REQUIRE(std::string(reinterpret_cast<const char*>(value.data()),
value.size()) == str);
}
SECTION("ext 32 serialization") {
auto str = std::string(65536, '?');
doc.set(MsgPackExtension(42, str.data(), str.size()));
std::string output;
auto result = serializeMsgPack(doc, output);
REQUIRE(result == 6 + str.size());
REQUIRE(output == "\xc9\x00\x01\x00\x00\x2a"_s + str);
}
}

View File

@ -3,15 +3,65 @@
#include <catch.hpp>
TEST_CASE("ARDUINOJSON_USE_DOUBLE == 0") {
JsonDocument doc;
JsonObject root = doc.to<JsonObject>();
namespace my {
using ArduinoJson::detail::isinf;
} // namespace my
root["pi"] = 3.14;
root["e"] = 2.72;
std::string json;
serializeJson(doc, json);
REQUIRE(json == "{\"pi\":3.14,\"e\":2.72}");
void checkFloat(const char* input, float expected) {
using ArduinoJson::detail::NumberType;
using ArduinoJson::detail::parseNumber;
CAPTURE(input);
auto result = parseNumber(input);
REQUIRE(result.type() == NumberType::Float);
REQUIRE(result.asFloat() == Approx(expected));
}
TEST_CASE("ARDUINOJSON_USE_DOUBLE == 0") {
SECTION("serializeJson()") {
JsonDocument doc;
JsonObject root = doc.to<JsonObject>();
root["pi"] = 3.14;
root["e"] = 2.72;
std::string json;
serializeJson(doc, json);
REQUIRE(json == "{\"pi\":3.14,\"e\":2.72}");
}
SECTION("parseNumber()") {
using ArduinoJson::detail::NumberType;
using ArduinoJson::detail::parseNumber;
SECTION("Large positive number") {
auto result = parseNumber("1e300");
REQUIRE(result.type() == NumberType::Float);
REQUIRE(result.asFloat() > 0);
REQUIRE(my::isinf(result.asFloat()));
}
SECTION("Large negative number") {
auto result = parseNumber("-1e300");
REQUIRE(result.type() == NumberType::Float);
REQUIRE(result.asFloat() < 0);
REQUIRE(my::isinf(result.asFloat()));
}
SECTION("Too small to be represented") {
auto result = parseNumber("1e-300");
REQUIRE(result.type() == NumberType::Float);
REQUIRE(result.asFloat() == 0);
}
SECTION("MantissaTooLongToFit") {
checkFloat("0.340282346638528861111111111111", 0.34028234663852886f);
checkFloat("34028234663852886.11111111111111", 34028234663852886.0f);
checkFloat("34028234.66385288611111111111111", 34028234.663852886f);
checkFloat("-0.340282346638528861111111111111", -0.34028234663852886f);
checkFloat("-34028234663852886.11111111111111", -34028234663852886.0f);
checkFloat("-34028234.66385288611111111111111", -34028234.663852886f);
}
}
}

View File

@ -3,14 +3,36 @@
#include <catch.hpp>
#include "Literals.hpp"
TEST_CASE("ARDUINOJSON_USE_LONG_LONG == 0") {
JsonDocument doc;
doc["A"] = 42;
doc["B"] = 84;
SECTION("smoke test") {
doc["A"] = 42;
doc["B"] = 84;
std::string json;
serializeJson(doc, json);
std::string json;
serializeJson(doc, json);
REQUIRE(json == "{\"A\":42,\"B\":84}");
REQUIRE(json == "{\"A\":42,\"B\":84}");
}
SECTION("deserializeMsgPack()") {
SECTION("cf 00 00 00 00 ff ff ff ff") {
auto err =
deserializeMsgPack(doc, "\xcf\x00\x00\x00\x00\xff\xff\xff\xff"_s);
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<uint32_t>() == 0xFFFFFFFF);
}
SECTION("cf 00 00 00 01 00 00 00 00") {
auto err =
deserializeMsgPack(doc, "\xcf\x00\x00\x00\x01\x00\x00\x00\x00"_s);
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.isNull());
}
}
}

View File

@ -6,6 +6,7 @@
#include <catch.hpp>
#include "Allocators.hpp"
#include "Literals.hpp"
template <typename T>
static void checkValue(const char* input, T expected) {
@ -123,21 +124,171 @@ TEST_CASE("deserialize MsgPack value") {
SECTION("fixstr") {
checkValue<std::string>("\xA0", std::string(""));
checkValue<std::string>("\xABhello world", std::string("hello world"));
checkValue<std::string>("\xABhello world", "hello world"_s);
checkValue<std::string>("\xBFhello world hello world hello !",
std::string("hello world hello world hello !"));
"hello world hello world hello !"_s);
}
SECTION("str 8") {
checkValue<std::string>("\xd9\x05hello", std::string("hello"));
checkValue<std::string>("\xd9\x05hello", "hello"_s);
}
SECTION("str 16") {
checkValue<std::string>("\xda\x00\x05hello", std::string("hello"));
checkValue<std::string>("\xda\x00\x05hello", "hello"_s);
}
SECTION("str 32") {
checkValue<std::string>("\xdb\x00\x00\x00\x05hello", std::string("hello"));
checkValue<std::string>("\xdb\x00\x00\x00\x05hello", "hello"_s);
}
SECTION("bin 8") {
JsonDocument doc;
DeserializationError error = deserializeMsgPack(doc, "\xc4\x01?");
REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<MsgPackBinary>());
auto binary = doc.as<MsgPackBinary>();
REQUIRE(binary.size() == 1);
REQUIRE(binary.data() != nullptr);
REQUIRE(reinterpret_cast<const char*>(binary.data())[0] == '?');
}
SECTION("bin 16") {
JsonDocument doc;
auto str = std::string(256, '?');
auto input = "\xc5\x01\x00"_s + str;
DeserializationError error = deserializeMsgPack(doc, input);
REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<MsgPackBinary>());
auto binary = doc.as<MsgPackBinary>();
REQUIRE(binary.size() == 0x100);
REQUIRE(binary.data() != nullptr);
REQUIRE(std::string(reinterpret_cast<const char*>(binary.data()),
binary.size()) == str);
}
SECTION("fixext 1") {
JsonDocument doc;
auto error = deserializeMsgPack(doc, "\xd4\x01\x02");
REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<MsgPackExtension>());
auto ext = doc.as<MsgPackExtension>();
REQUIRE(ext.type() == 1);
REQUIRE(ext.size() == 1);
auto data = reinterpret_cast<const uint8_t*>(ext.data());
REQUIRE(data[0] == 2);
}
SECTION("fixext 2") {
JsonDocument doc;
auto error = deserializeMsgPack(doc, "\xd5\x01\x02\x03");
REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<MsgPackExtension>());
auto ext = doc.as<MsgPackExtension>();
REQUIRE(ext.type() == 1);
REQUIRE(ext.size() == 2);
auto data = reinterpret_cast<const uint8_t*>(ext.data());
REQUIRE(data[0] == 2);
REQUIRE(data[1] == 3);
}
SECTION("fixext 4") {
JsonDocument doc;
auto error = deserializeMsgPack(doc, "\xd6\x01\x02\x03\x04\x05");
REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<MsgPackExtension>());
auto ext = doc.as<MsgPackExtension>();
REQUIRE(ext.type() == 1);
REQUIRE(ext.size() == 4);
auto data = reinterpret_cast<const uint8_t*>(ext.data());
REQUIRE(data[0] == 2);
REQUIRE(data[1] == 3);
REQUIRE(data[2] == 4);
REQUIRE(data[3] == 5);
}
SECTION("fixext 8") {
JsonDocument doc;
auto error = deserializeMsgPack(doc, "\xd7\x01????????");
REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<MsgPackExtension>());
auto ext = doc.as<MsgPackExtension>();
REQUIRE(ext.type() == 1);
REQUIRE(ext.size() == 8);
auto data = reinterpret_cast<const uint8_t*>(ext.data());
REQUIRE(data[0] == '?');
REQUIRE(data[7] == '?');
}
SECTION("fixext 16") {
JsonDocument doc;
auto error = deserializeMsgPack(doc, "\xd8\x01?????????????????");
REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<MsgPackExtension>());
auto ext = doc.as<MsgPackExtension>();
REQUIRE(ext.type() == 1);
REQUIRE(ext.size() == 16);
auto data = reinterpret_cast<const uint8_t*>(ext.data());
REQUIRE(data[0] == '?');
REQUIRE(data[15] == '?');
}
SECTION("ext 8") {
JsonDocument doc;
auto error = deserializeMsgPack(doc, "\xc7\x02\x01\x03\x04");
REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<MsgPackExtension>());
auto ext = doc.as<MsgPackExtension>();
REQUIRE(ext.type() == 1);
REQUIRE(ext.size() == 2);
auto data = reinterpret_cast<const uint8_t*>(ext.data());
REQUIRE(data[0] == 3);
REQUIRE(data[1] == 4);
}
SECTION("ext 16") {
JsonDocument doc;
auto error = deserializeMsgPack(doc, "\xc8\x00\x02\x01\x03\x04");
REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<MsgPackExtension>());
auto ext = doc.as<MsgPackExtension>();
REQUIRE(ext.type() == 1);
REQUIRE(ext.size() == 2);
auto data = reinterpret_cast<const uint8_t*>(ext.data());
REQUIRE(data[0] == 3);
REQUIRE(data[1] == 4);
}
SECTION("ext 32") {
JsonDocument doc;
auto error = deserializeMsgPack(doc, "\xc9\x00\x00\x00\x02\x01\x03\x04");
REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<MsgPackExtension>());
auto ext = doc.as<MsgPackExtension>();
REQUIRE(ext.type() == 1);
REQUIRE(ext.size() == 2);
auto data = reinterpret_cast<const uint8_t*>(ext.data());
REQUIRE(data[0] == 3);
REQUIRE(data[1] == 4);
}
}
@ -198,12 +349,12 @@ TEST_CASE("deserializeMsgPack() under memory constaints") {
checkError(0, "\x80", DeserializationError::Ok);
}
SECTION("{H:1}") {
checkError(0, "\x81\xA1H\x01", DeserializationError::NoMemory);
checkError(3, "\x81\xA1H\x01", DeserializationError::Ok);
checkError(1, "\x81\xA1H\x01", DeserializationError::NoMemory);
checkError(2, "\x81\xA1H\x01", DeserializationError::Ok);
}
SECTION("{H:1,W:2}") {
checkError(3, "\x82\xA1H\x01\xA1W\x02", DeserializationError::NoMemory);
checkError(5, "\x82\xA1H\x01\xA1W\x02", DeserializationError::Ok);
checkError(2, "\x82\xA1H\x01\xA1W\x02", DeserializationError::NoMemory);
checkError(3, "\x82\xA1H\x01\xA1W\x02", DeserializationError::Ok);
}
}
@ -212,13 +363,13 @@ TEST_CASE("deserializeMsgPack() under memory constaints") {
checkError(0, "\xDE\x00\x00", DeserializationError::Ok);
}
SECTION("{H:1}") {
checkError(2, "\xDE\x00\x01\xA1H\x01", DeserializationError::NoMemory);
checkError(3, "\xDE\x00\x01\xA1H\x01", DeserializationError::Ok);
checkError(1, "\xDE\x00\x01\xA1H\x01", DeserializationError::NoMemory);
checkError(2, "\xDE\x00\x01\xA1H\x01", DeserializationError::Ok);
}
SECTION("{H:1,W:2}") {
checkError(3, "\xDE\x00\x02\xA1H\x01\xA1W\x02",
checkError(2, "\xDE\x00\x02\xA1H\x01\xA1W\x02",
DeserializationError::NoMemory);
checkError(5, "\xDE\x00\x02\xA1H\x01\xA1W\x02", DeserializationError::Ok);
checkError(3, "\xDE\x00\x02\xA1H\x01\xA1W\x02", DeserializationError::Ok);
}
}
@ -227,14 +378,14 @@ TEST_CASE("deserializeMsgPack() under memory constaints") {
checkError(0, "\xDF\x00\x00\x00\x00", DeserializationError::Ok);
}
SECTION("{H:1}") {
checkError(2, "\xDF\x00\x00\x00\x01\xA1H\x01",
checkError(1, "\xDF\x00\x00\x00\x01\xA1H\x01",
DeserializationError::NoMemory);
checkError(3, "\xDF\x00\x00\x00\x01\xA1H\x01", DeserializationError::Ok);
checkError(2, "\xDF\x00\x00\x00\x01\xA1H\x01", DeserializationError::Ok);
}
SECTION("{H:1,W:2}") {
checkError(3, "\xDF\x00\x00\x00\x02\xA1H\x01\xA1W\x02",
checkError(2, "\xDF\x00\x00\x00\x02\xA1H\x01\xA1W\x02",
DeserializationError::NoMemory);
checkError(5, "\xDF\x00\x00\x00\x02\xA1H\x01\xA1W\x02",
checkError(3, "\xDF\x00\x00\x00\x02\xA1H\x01\xA1W\x02",
DeserializationError::Ok);
}
}

Some files were not shown because too many files have changed in this diff Show More