forked from bblanchon/ArduinoJson
Compare commits
92 Commits
Author | SHA1 | Date | |
---|---|---|---|
fffed4fba6 | |||
8d7bbffe10 | |||
488475fe16 | |||
ba8d102432 | |||
e96680eb29 | |||
4c0fb4eb55 | |||
db2fb01795 | |||
18ae8ab7b9 | |||
87c96f9306 | |||
262747b419 | |||
3e0ba2028c | |||
972f665b07 | |||
519e32badb | |||
d90b36c009 | |||
8147625921 | |||
2cf7fc5427 | |||
4871380060 | |||
2a663db3c7 | |||
437307a955 | |||
56b3b4d5a9 | |||
f5355a9eb5 | |||
779ee07de9 | |||
ab4e8547cb | |||
585795d002 | |||
68a167b167 | |||
78d4f721ff | |||
a97bcb6b2d | |||
2da1f2553d | |||
c267b55dec | |||
3a73ccf027 | |||
8ab45e6f82 | |||
da45c4bc4f | |||
48acf963fb | |||
ccccd1da11 | |||
78a30496be | |||
636c8c36eb | |||
5070fa6562 | |||
d9e035a288 | |||
aba8974148 | |||
044a4753d2 | |||
ff0deee793 | |||
37357086e2 | |||
9321f8fdab | |||
167ea08c53 | |||
5c0338970c | |||
6b4dd3ff2f | |||
fead19560c | |||
86772d33bc | |||
a035116018 | |||
806fa907ab | |||
95f5d9d134 | |||
03139a08af | |||
acfbf26e37 | |||
461cdaa818 | |||
dd46813dc0 | |||
5d796781fb | |||
f5e7570f84 | |||
003087406c | |||
b7c8e0d25c | |||
30c111fd3d | |||
9d0714efdf | |||
d8f3058efa | |||
7c0fa7c276 | |||
6eb4f45fb9 | |||
b3132cac3a | |||
d95a3bd19a | |||
22e4f216c3 | |||
e9850152a7 | |||
5e0e35615c | |||
c4b879645a | |||
6afa6b647c | |||
acd465b365 | |||
e858570afb | |||
0643c2e708 | |||
bcf1339e89 | |||
dc463a2f72 | |||
a7cdf638e7 | |||
57810af2ac | |||
2eb726b744 | |||
912137ccfb | |||
035c913c72 | |||
3f43c2b816 | |||
0328f66340 | |||
b3eada9c7f | |||
8516b368ad | |||
d0fff5a0b5 | |||
4c8d4b4e20 | |||
5faa3df43f | |||
540901e219 | |||
db9258bcd7 | |||
24aaab6e3e | |||
17a482a9b1 |
2
.github/ISSUE_TEMPLATE/bug_report.md
vendored
2
.github/ISSUE_TEMPLATE/bug_report.md
vendored
@ -27,7 +27,7 @@ Here is the environment that I used:
|
||||
Here is a small snippet that reproduces the issue.
|
||||
|
||||
```c++
|
||||
DynamicJsonDocument doc(1024);
|
||||
JsonDocument doc(1024);
|
||||
|
||||
DeserializationError error = deserializeJson(doc, "{\"hello\":\"world\"}");
|
||||
|
||||
|
2
.github/ISSUE_TEMPLATE/help.md
vendored
2
.github/ISSUE_TEMPLATE/help.md
vendored
@ -28,7 +28,7 @@ Here is the environment that I'm using':
|
||||
Here is a small snippet that demonstrate the problem.
|
||||
|
||||
```c++
|
||||
DynamicJsonDocument doc(1024);
|
||||
JsonDocument doc(1024);
|
||||
|
||||
DeserializationError error = deserializeJson(doc, "{\"hello\":\"world\"}");
|
||||
|
||||
|
9
.github/workflows/ci.yml
vendored
9
.github/workflows/ci.yml
vendored
@ -33,6 +33,7 @@ jobs:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
include:
|
||||
- gcc: "5"
|
||||
- gcc: "6"
|
||||
- gcc: "7"
|
||||
cxxflags: -fsanitize=leak -fno-sanitize-recover=all
|
||||
@ -131,9 +132,11 @@ jobs:
|
||||
- name: Install
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y g++-multilib
|
||||
sudo apt-get install -y g++-multilib gcc-avr avr-libc
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v3
|
||||
- name: AVR
|
||||
run: avr-g++ -std=c++11 -Isrc extras/conf_test/avr.cpp
|
||||
- name: GCC 32-bit
|
||||
run: g++ -std=c++11 -m32 -Isrc extras/conf_test/x86.cpp
|
||||
- name: GCC 64-bit
|
||||
@ -481,7 +484,7 @@ jobs:
|
||||
g++ -x c++ - <<END
|
||||
#include "${{ steps.amalgamate.outputs.filename }}"
|
||||
int main() {
|
||||
StaticJsonDocument<300> doc;
|
||||
JsonDocument doc(300);
|
||||
deserializeJson(doc, "{}");
|
||||
}
|
||||
END
|
||||
@ -515,7 +518,7 @@ jobs:
|
||||
g++ -x c++ - <<END
|
||||
#include "${{ steps.amalgamate.outputs.filename }}"
|
||||
int main() {
|
||||
ArduinoJson::StaticJsonDocument<300> doc;
|
||||
ArduinoJson::JsonDocument doc(300);
|
||||
deserializeJson(doc, "{}");
|
||||
}
|
||||
END
|
||||
|
843
CHANGELOG.md
843
CHANGELOG.md
@ -1,832 +1,17 @@
|
||||
ArduinoJson: change log
|
||||
=======================
|
||||
|
||||
v6.21.0 (2023-03-14)
|
||||
-------
|
||||
|
||||
* Drop support for C++98/C++03. Minimum required is C++11.
|
||||
* Remove `ARDUINOJSON_NAMESPACE`; use `ArduinoJson` instead.
|
||||
* Make string support generic (issue #1807)
|
||||
|
||||
v6.20.1 (2023-02-08)
|
||||
-------
|
||||
|
||||
* Remove explicit exclusion of `as<char*>()` and `as<char>()` (issue #1860)
|
||||
If you try to call them, you'll now get the same error message as any unsupported type.
|
||||
You could also add a custom converter for `char*` and `char`.
|
||||
|
||||
v6.20.0 (2022-12-26)
|
||||
-------
|
||||
|
||||
* Add `JsonVariant::shallowCopy()` (issue #1343)
|
||||
* Fix `9.22337e+18 is outside the range of representable values of type 'long'`
|
||||
* Fix comparison operators for `JsonArray`, `JsonArrayConst`, `JsonObject`, and `JsonObjectConst`
|
||||
* Fix lax parsing of `true`, `false`, and `null` (issue #1781)
|
||||
* Remove undocumented `accept()` functions
|
||||
* Rename `addElement()` to `add()`
|
||||
* Remove `getElement()`, `getOrAddElement()`, `getMember()`, and `getOrAddMember()`
|
||||
* Remove undocumented `JsonDocument::data()` and `JsonDocument::memoryPool()`
|
||||
* Remove undocumented `JsonArrayIterator::internal()` and `JsonObjectIterator::internal()`
|
||||
* Rename things in `ARDUINOJSON_NAMESPACE` to match the public names
|
||||
* Add documentation to most public symbols
|
||||
* Remove support for naked `char` (was deprecated since 6.18.0)
|
||||
|
||||
> ### BREAKING CHANGES
|
||||
>
|
||||
> This release hides `JsonVariant`'s functions that were only intended for internal use.
|
||||
> If you were using them in your programs, you must replace with `operator[]` and `to<JsonVariant>()`, like so:
|
||||
>
|
||||
> ```c++
|
||||
> // before
|
||||
> JsonVariant a = variant.getElement(idx);
|
||||
> JsonVariant b = variant.getOrAddElement(idx);
|
||||
> JsonVariant c = variant.getMember(key);
|
||||
> JsonVariant d = variant.getOrAddMember(key);
|
||||
>
|
||||
> // after
|
||||
> JsonVariant a = variant[idx];
|
||||
> JsonVariant b = idx < variant.size() ? variant[idx] : variant[idx].to<JsonVariant>();
|
||||
> JsonVariant c = variant[key];
|
||||
> JsonVariant d = variant.containsKey(key) ? variant[key] : variant[key].to<JsonVariant>();
|
||||
> ```
|
||||
|
||||
v6.19.4 (2022-04-05)
|
||||
-------
|
||||
|
||||
* Add `ElementProxy::memoryUsage()`
|
||||
* Add `MemberProxy::memoryUsage()` (issue #1730)
|
||||
* Add implicit conversion from `JsonDocument` to `JsonVariant`
|
||||
* Fix comparisons operators with `const JsonDocument&`
|
||||
|
||||
v6.19.3 (2022-03-08)
|
||||
-------
|
||||
|
||||
* Fix `call of overloaded 'String(const char*, int)' is ambiguous`
|
||||
* Fix `JsonString` operator `==` and `!=` for non-zero-terminated string
|
||||
* Fix `-Wsign-conversion` on GCC 8 (issue #1715)
|
||||
* MessagePack: serialize round floats as integers (issue #1718)
|
||||
|
||||
v6.19.2 (2022-02-14)
|
||||
-------
|
||||
|
||||
* Fix `cannot convert 'pgm_p' to 'const void*'` (issue #1707)
|
||||
|
||||
v6.19.1 (2022-01-14)
|
||||
-------
|
||||
|
||||
* Fix crash when adding an object member in a too small `JsonDocument`
|
||||
* Fix filter not working in zero-copy mode (issue #1697)
|
||||
|
||||
v6.19.0 (2022-01-08)
|
||||
-------
|
||||
|
||||
* Remove `ARDUINOJSON_EMBEDDED_MODE` and assume we run on an embedded platform.
|
||||
Dependent settings (like `ARDUINOJSON_DEFAULT_NESTING_LIMIT`) must be set individually.
|
||||
* Change the default of `ARDUINOJSON_USE_DOUBLE` to `1`
|
||||
* Change the default of `ARDUINOJSON_USE_LONG_LONG` to `1` on 32-bit platforms
|
||||
* Add `as<JsonString>()` and `is<JsonString>()`
|
||||
* Add safe bool idiom in `JsonString`
|
||||
* Add support for NUL in string values (issue #1646)
|
||||
* Add support for arbitrary array rank in `copyArray()`
|
||||
* Add support for `char[][]` in `copyArray()`
|
||||
* Remove `DeserializationError == bool` and `DeserializationError != bool`
|
||||
* Renamed undocumented function `isUndefined()` to `isUnbound()`
|
||||
* Fix `JsonVariant::memoryUsage()` for raw strings
|
||||
* Fix `call of overloaded 'swap(BasicJsonDocument&, BasicJsonDocument&)' is ambiguous` (issue #1678)
|
||||
* Fix inconsistent pool capacity between `BasicJsonDocument`'s copy and move constructors
|
||||
* Fix inconsistent pool capacity between `BasicJsonDocument`'s copy and move assignments
|
||||
* Fix return type of `StaticJsonDocument::operator=`
|
||||
* Avoid pool reallocation in `BasicJsonDocument`'s copy assignment if capacity is the same
|
||||
* Avoid including `Arduino.h` when all its features are disabled (issue #1692, PR #1693 by @paulocsanz)
|
||||
* Assume `PROGMEM` is available as soon as `ARDUINO` is defined (consequence of #1693)
|
||||
|
||||
v6.18.5 (2021-09-28)
|
||||
-------
|
||||
|
||||
* Set `ARDUINOJSON_EMBEDDED_MODE` to `1` on Nios II (issue #1657)
|
||||
|
||||
v6.18.4 (2021-09-06)
|
||||
-------
|
||||
|
||||
* Fixed error `'dummy' may be used uninitialized` on GCC 11
|
||||
* Fixed error `expected unqualified-id before 'const'` on GCC 11 (issue #1622)
|
||||
* Filter: exact match takes precedence over wildcard (issue #1628)
|
||||
* Fixed deserialization of `\u0000` (issue #1646)
|
||||
|
||||
v6.18.3 (2021-07-27)
|
||||
-------
|
||||
|
||||
* Changed return type of `convertToJson()` and `Converter<T>::toJson()` to `void`
|
||||
* Added `as<std::string_view>()` and `is<std::string_view>()`
|
||||
|
||||
v6.18.2 (2021-07-19)
|
||||
-------
|
||||
|
||||
* Removed a symlink because the Arduino Library Specification forbids it
|
||||
|
||||
v6.18.1 (2021-07-03)
|
||||
-------
|
||||
|
||||
* Fixed support for `volatile float` and `volatile double` (issue #1557)
|
||||
* Fixed error `[Pe070]: incomplete type is not allowed` on IAR (issue #1560)
|
||||
* Fixed `serializeJson(doc, String)` when allocation fails (issue #1572)
|
||||
* Fixed clang-tidy warnings (issue #1574, PR #1577 by @armandas)
|
||||
* Added fake class `InvalidConversion<T1,T2>` to easily identify invalid conversions (issue #1585)
|
||||
* Added support for `std::string_view` (issue #1578, PR #1554 by @0xFEEDC0DE64)
|
||||
* Fixed warning `definition of implicit copy constructor for 'MsgPackDeserializer' is deprecated because it has a user-declared copy assignment operator`
|
||||
* Added `JsonArray::clear()` (issue #1597)
|
||||
* Fixed `JsonVariant::as<unsigned>()` (issue #1601)
|
||||
* Added support for ESP-IDF component build (PR #1562 by @qt1, PR #1599 by @andreaskuster)
|
||||
|
||||
v6.18.0 (2021-05-05)
|
||||
-------
|
||||
|
||||
* Added support for custom converters (issue #687)
|
||||
* Added support for `Printable` (issue #1444)
|
||||
* Removed support for `char` values, see below (issue #1498)
|
||||
* `deserializeJson()` leaves `\uXXXX` unchanged instead of returning `NotSupported`
|
||||
* `deserializeMsgPack()` inserts `null` instead of returning `NotSupported`
|
||||
* Removed `DeserializationError::NotSupported`
|
||||
* Added `JsonVariant::is<JsonArrayConst/JsonObjectConst>()` (issue #1412)
|
||||
* Added `JsonVariant::is<JsonVariant/JsonVariantConst>()` (issue #1412)
|
||||
* Changed `JsonVariantConst::is<JsonArray/JsonObject>()` to return `false` (issue #1412)
|
||||
* Simplified `JsonVariant::as<T>()` to always return `T` (see below)
|
||||
* Updated folders list in `.mbedignore` (PR #1515 by @AGlass0fMilk)
|
||||
* Fixed member-call-on-null-pointer in `getMember()` when array is empty
|
||||
* `serializeMsgPack(doc, buffer, size)` doesn't add null-terminator anymore (issue #1545)
|
||||
* `serializeJson(doc, buffer, size)` adds null-terminator only if there is enough room
|
||||
* PlatformIO: set `build.libArchive` to `false` (PR #1550 by @askreet)
|
||||
|
||||
> ### BREAKING CHANGES
|
||||
>
|
||||
> #### Support for `char` removed
|
||||
>
|
||||
> We cannot cast a `JsonVariant` to a `char` anymore, so the following will break:
|
||||
> ```c++
|
||||
> char age = doc["age"]; // error: no matching function for call to 'variantAs(VariantData*&)'
|
||||
> ```
|
||||
> Instead, you must use another integral type, such as `int8_t`:
|
||||
> ```c++
|
||||
> int8_t age = doc["age"]; // OK
|
||||
> ```
|
||||
>
|
||||
> Similarly, we cannot assign from a `char` anymore, so the following will break:
|
||||
> ```c++
|
||||
> char age;
|
||||
> doc["age"] = age; // error: no matching function for call to 'VariantRef::set(const char&)'
|
||||
> ```
|
||||
> Instead, you must use another integral type, such as `int8_t`:
|
||||
> ```c++
|
||||
> int8_t age;
|
||||
> doc["age"] = age; // OK
|
||||
> ```
|
||||
> A deprecation warning with the message "Support for `char` is deprecated, use `int8_t` or `uint8_t` instead" was added to allow a smooth transition.
|
||||
>
|
||||
> #### `as<T>()` always returns `T`
|
||||
>
|
||||
> Previously, `JsonVariant::as<T>()` could return a type different from `T`.
|
||||
> The most common example is `as<char*>()` that returned a `const char*`.
|
||||
> While this feature simplified a few use cases, it was confusing and complicated the
|
||||
> implementation of custom converters.
|
||||
>
|
||||
> Starting from this version, `as<T>` doesn't try to auto-correct the return type and always return `T`,
|
||||
> which means that you cannot write this anymore:
|
||||
>
|
||||
> ```c++
|
||||
> Serial.println(doc["sensor"].as<char*>()); // error: invalid conversion from 'const char*' to 'char*' [-fpermissive]
|
||||
> ```
|
||||
>
|
||||
> Instead, you must write:
|
||||
>
|
||||
> ```c++
|
||||
> Serial.println(doc["sensor"].as<const char*>()); // OK
|
||||
> ```
|
||||
>
|
||||
> A deprecation warning with the message "Replace `as<char*>()` with `as<const char*>()`" was added to allow a smooth transition.
|
||||
>
|
||||
> #### `DeserializationError::NotSupported` removed
|
||||
>
|
||||
> On a different topic, `DeserializationError::NotSupported` has been removed.
|
||||
> Instead of returning this error:
|
||||
>
|
||||
> * `deserializeJson()` leaves `\uXXXX` unchanged (only when `ARDUINOJSON_DECODE_UNICODE` is `0`)
|
||||
> * `deserializeMsgPack()` replaces unsupported values with `null`s
|
||||
>
|
||||
> #### Const-aware `is<T>()`
|
||||
>
|
||||
> Lastly, a very minor change concerns `JsonVariantConst::is<T>()`.
|
||||
> It used to return `true` for `JsonArray` and `JsonOject`, but now it returns `false`.
|
||||
> Instead, you must use `JsonArrayConst` and `JsonObjectConst`.
|
||||
|
||||
v6.17.3 (2021-02-15)
|
||||
-------
|
||||
|
||||
* Made `JsonDocument`'s destructor protected (issue #1480)
|
||||
* Added missing calls to `client.stop()` in `JsonHttpClient.ino` (issue #1485)
|
||||
* Fixed error `expected ')' before 'char'` when `isdigit()` is a macro (issue #1487)
|
||||
* Fixed error `definition of implicit copy constructor is deprecated` on Clang 10
|
||||
* PlatformIO: set framework compatibility to `*` (PR #1490 by @maxgerhardt)
|
||||
|
||||
v6.17.2 (2020-11-14)
|
||||
-------
|
||||
|
||||
* Fixed invalid conversion error in `operator|(JsonVariant, char*)` (issue #1432)
|
||||
* Changed the default value of `ARDUINOJSON_ENABLE_PROGMEM` (issue #1433).
|
||||
It now checks that the `pgm_read_XXX` macros are defined before enabling `PROGMEM`.
|
||||
|
||||
v6.17.1 (2020-11-07)
|
||||
-------
|
||||
|
||||
* Fixed error `ambiguous overload for 'operator|'` (issue #1411)
|
||||
* Fixed `operator|(MemberProxy, JsonObject)` (issue #1415)
|
||||
* Allowed more than 32767 values in non-embedded mode (issue #1414)
|
||||
|
||||
v6.17.0 (2020-10-19)
|
||||
-------
|
||||
|
||||
* Added a build failure when nullptr is defined as a macro (issue #1355)
|
||||
* Added `JsonDocument::overflowed()` which tells if the memory pool was too small (issue #1358)
|
||||
* Added `DeserializationError::EmptyInput` which tells if the input was empty
|
||||
* Added `DeserializationError::f_str()` which returns a `const __FlashStringHelper*` (issue #846)
|
||||
* Added `operator|(JsonVariantConst, JsonVariantConst)`
|
||||
* Added filtering for MessagePack (issue #1298, PR #1394 by Luca Passarella)
|
||||
* Moved float convertion tables to PROGMEM
|
||||
* Fixed `JsonVariant::set((char*)0)` which returned false instead of true (issue #1368)
|
||||
* Fixed error `No such file or directory #include <WString.h>` (issue #1381)
|
||||
|
||||
v6.16.1 (2020-08-04)
|
||||
-------
|
||||
|
||||
* Fixed `deserializeJson()` that stopped reading after `{}` (issue #1335)
|
||||
|
||||
v6.16.0 (2020-08-01)
|
||||
-------
|
||||
|
||||
* Added comparisons (`>`, `>=`, `==`, `!=`, `<`, and `<=`) between `JsonVariant`s
|
||||
* Added string deduplication (issue #1303)
|
||||
* Added `JsonString::operator!=`
|
||||
* Added wildcard key (`*`) for filters (issue #1309)
|
||||
* Set `ARDUINOJSON_DECODE_UNICODE` to `1` by default
|
||||
* Fixed `copyArray()` not working with `String`, `ElementProxy`, and `MemberProxy`
|
||||
* Fixed error `getOrAddElement is not a member of ElementProxy` (issue #1311)
|
||||
* Fixed excessive stack usage when compiled with `-Og` (issues #1210 and #1314)
|
||||
* Fixed `Warning[Pa093]: implicit conversion from floating point to integer` on IAR compiler (PR #1328 by @stawiski)
|
||||
|
||||
v6.15.2 (2020-05-15)
|
||||
-------
|
||||
|
||||
* CMake: don't build tests when imported in another project
|
||||
* CMake: made project arch-independent
|
||||
* Visual Studio: fixed error C2766 with flag `/Zc:__cplusplus` (issue #1250)
|
||||
* Added support for `JsonDocument` to `copyArray()` (issue #1255)
|
||||
* Added support for `enum`s in `as<T>()` and `is<T>()` (issue #1256)
|
||||
* Added `JsonVariant` as an input type for `deserializeXxx()`
|
||||
For example, you can do: `deserializeJson(doc2, doc1["payload"])`
|
||||
* Break the build if using 64-bit integers with ARDUINOJSON_USE_LONG_LONG==0
|
||||
|
||||
v6.15.1 (2020-04-08)
|
||||
-------
|
||||
|
||||
* Fixed "maybe-uninitialized" warning (issue #1217)
|
||||
* Fixed "statement is unreachable" warning on IAR (issue #1233)
|
||||
* Fixed "pointless integer comparison" warning on IAR (issue #1233)
|
||||
* Added CMake "install" target (issue #1209)
|
||||
* Disabled alignment on AVR (issue #1231)
|
||||
|
||||
v6.15.0 (2020-03-22)
|
||||
-------
|
||||
|
||||
* Added `DeserializationOption::Filter` (issue #959)
|
||||
* Added example `JsonFilterExample.ino`
|
||||
* Changed the array subscript operator to automatically add missing elements
|
||||
* Fixed "deprecated-copy" warning on GCC 9 (fixes #1184)
|
||||
* Fixed `MemberProxy::set(char[])` not duplicating the string (issue #1191)
|
||||
* Fixed enums serialized as booleans (issue #1197)
|
||||
* Fixed incorrect string comparison on some platforms (issue #1198)
|
||||
* Added move-constructor and move-assignment to `BasicJsonDocument`
|
||||
* Added `BasicJsonDocument::garbageCollect()` (issue #1195)
|
||||
* Added `StaticJsonDocument::garbageCollect()`
|
||||
* Changed copy-constructor of `BasicJsonDocument` to preserve the capacity of the source.
|
||||
* Removed copy-constructor of `JsonDocument` (issue #1189)
|
||||
|
||||
> ### BREAKING CHANGES
|
||||
>
|
||||
> #### Copy-constructor of `BasicJsonDocument`
|
||||
>
|
||||
> In previous versions, the copy constructor of `BasicJsonDocument` looked at the source's `memoryUsage()` to choose its capacity.
|
||||
> Now, the copy constructor of `BasicJsonDocument` uses the same capacity as the source.
|
||||
>
|
||||
> Example:
|
||||
>
|
||||
> ```c++
|
||||
> DynamicJsonDocument doc1(64);
|
||||
> doc1.set(String("example"));
|
||||
>
|
||||
> DynamicJsonDocument doc2 = doc1;
|
||||
> Serial.print(doc2.capacity()); // 8 with ArduinoJson 6.14
|
||||
> // 64 with ArduinoJson 6.15
|
||||
> ```
|
||||
>
|
||||
> I made this change to get consistent results between copy-constructor and move-constructor, and whether RVO applies or not.
|
||||
>
|
||||
> If you use the copy-constructor to optimize your documents, you can use `garbageCollect()` or `shrinkToFit()` instead.
|
||||
>
|
||||
> #### Copy-constructor of `JsonDocument`
|
||||
>
|
||||
> In previous versions, it was possible to create a function that take a `JsonDocument` by value.
|
||||
>
|
||||
> ```c++
|
||||
> void myFunction(JsonDocument doc) {}
|
||||
> ```
|
||||
>
|
||||
> This function gives the wrong clues because it doesn't receive a copy of the `JsonDocument`, only a sliced version.
|
||||
> It worked because the copy constructor copied the internal pointers, but it was an accident.
|
||||
>
|
||||
> From now, if you need to pass a `JsonDocument` to a function, you must use a reference:
|
||||
>
|
||||
> ```c++
|
||||
> void myFunction(JsonDocument& doc) {}
|
||||
> ```
|
||||
|
||||
v6.14.1 (2020-01-27)
|
||||
-------
|
||||
|
||||
* Fixed regression in UTF16 decoding (issue #1173)
|
||||
* Fixed `containsKey()` on `JsonVariantConst`
|
||||
* Added `getElement()` and `getMember()` to `JsonVariantConst`
|
||||
|
||||
v6.14.0 (2020-01-16)
|
||||
-------
|
||||
|
||||
* Added `BasicJsonDocument::shrinkToFit()`
|
||||
* Added support of `uint8_t` for `serializeJson()`, `serializeJsonPretty()`, and `serializeMsgPack()` (issue #1142)
|
||||
* Added `ARDUINOJSON_ENABLE_COMMENTS` to enable support for comments (defaults to 0)
|
||||
* Auto enable support for `std::string` and `std::stream` on modern compilers (issue #1156)
|
||||
(No need to define `ARDUINOJSON_ENABLE_STD_STRING` and `ARDUINOJSON_ENABLE_STD_STREAM` anymore)
|
||||
* Improved decoding of UTF-16 surrogate pairs (PR #1157 by @kaysievers)
|
||||
(ArduinoJson now produces standard UTF-8 instead of CESU-8)
|
||||
* Added `measureJson`, `measureJsonPretty`, and `measureMsgPack` to `keywords.txt`
|
||||
(This file is used for syntax highlighting in the Arduino IDE)
|
||||
* Fixed `variant.is<nullptr_t>()`
|
||||
* Fixed value returned by `serializeJson()`, `serializeJsonPretty()`, and `serializeMsgPack()` when writing to a `String`
|
||||
* Improved speed of `serializeJson()`, `serializeJsonPretty()`, and `serializeMsgPack()` when writing to a `String`
|
||||
|
||||
> ### BREAKING CHANGES
|
||||
>
|
||||
> #### Comments
|
||||
>
|
||||
> Support for comments in input is now optional and disabled by default.
|
||||
>
|
||||
> If you need support for comments, you must defined `ARDUINOJSON_ENABLE_COMMENTS` to `1`; otherwise, you'll receive `InvalidInput` errors.
|
||||
>
|
||||
> ```c++
|
||||
> #define ARDUINOJSON_ENABLE_COMMENTS 1
|
||||
> #include <ArduinoJson.h>
|
||||
> ```
|
||||
|
||||
v6.13.0 (2019-11-01)
|
||||
-------
|
||||
|
||||
* Added support for custom writer/reader classes (issue #1088)
|
||||
* Added conversion from `JsonArray` and `JsonObject` to `bool`, to be consistent with `JsonVariant`
|
||||
* Fixed `deserializeJson()` when input contains duplicate keys (issue #1095)
|
||||
* Improved `deserializeMsgPack()` speed by reading several bytes at once
|
||||
* Added detection of Atmel AVR8/GNU C Compiler (issue #1112)
|
||||
* Fixed deserializer that stopped reading at the first `0xFF` (PR #1118 by @mikee47)
|
||||
* Fixed dangling reference in copies of `MemberProxy` and `ElementProxy` (issue #1120)
|
||||
|
||||
v6.12.0 (2019-09-05)
|
||||
-------
|
||||
|
||||
* Use absolute instead of relative includes (issue #1072)
|
||||
* Changed `JsonVariant::as<bool>()` to return `true` for any non-null value (issue #1005)
|
||||
* Moved ancillary files to `extras/` (issue #1011)
|
||||
|
||||
v6.11.5 (2019-08-23)
|
||||
-------
|
||||
|
||||
* Added fallback implementations of `strlen_P()`, `strncmp_P()`, `strcmp_P()`, and `memcpy_P()` (issue #1073)
|
||||
|
||||
v6.11.4 (2019-08-12)
|
||||
-------
|
||||
|
||||
* Added `measureJson()` to the `ArduinoJson` namespace (PR #1069 by @nomis)
|
||||
* Added support for `basic_string<char, traits, allocator>` (issue #1045)
|
||||
* Fixed example `JsonConfigFile.ino` for ESP8266
|
||||
* Include `Arduino.h` if `ARDUINO` is defined (PR #1071 by @nomis)
|
||||
|
||||
v6.11.3 (2019-07-22)
|
||||
-------
|
||||
|
||||
* Added operators `==` and `!=` for `JsonDocument`, `ElementProxy`, and `MemberProxy`
|
||||
* Fixed comparison of `JsonVariant` when one contains a linked string and the other contains an owned string (issue #1051)
|
||||
|
||||
v6.11.2 (2019-07-08)
|
||||
-------
|
||||
|
||||
* Fixed assignment of `JsonDocument` to `JsonVariant` (issue #1023)
|
||||
* Fix invalid conversion error on Particle Argon (issue #1035)
|
||||
|
||||
v6.11.1 (2019-06-21)
|
||||
-------
|
||||
|
||||
* Fixed `serialized()` not working with Flash strings (issue #1030)
|
||||
|
||||
v6.11.0 (2019-05-26)
|
||||
-------
|
||||
|
||||
* Fixed `deserializeJson()` silently accepting a `Stream*` (issue #978)
|
||||
* Fixed invalid result from `operator|` (issue #981)
|
||||
* Made `deserializeJson()` more picky about trailing characters (issue #980)
|
||||
* Added `ARDUINOJSON_ENABLE_NAN` (default=0) to enable NaN in JSON (issue #973)
|
||||
* Added `ARDUINOJSON_ENABLE_INFINITY` (default=0) to enable Infinity in JSON
|
||||
* Removed implicit conversion in comparison operators (issue #998)
|
||||
* Added lexicographical comparison for `JsonVariant`
|
||||
* Added support for `nullptr` (issue #998)
|
||||
|
||||
> ### BREAKING CHANGES
|
||||
>
|
||||
> #### NaN and Infinity
|
||||
>
|
||||
> The JSON specification allows neither NaN not Infinity, but previous
|
||||
> versions of ArduinoJson supported it. Now, ArduinoJson behaves like most
|
||||
> other libraries: a NaN or and Infinity in the `JsonDocument`, becomes
|
||||
> a `null` in the output JSON. Also, `deserializeJson()` returns
|
||||
> `InvalidInput` if the JSON document contains NaN or Infinity.
|
||||
>
|
||||
> This version still supports NaN and Infinity in JSON documents, but
|
||||
> it's disabled by default to be compatible with other JSON parsers.
|
||||
> If you need the old behavior back, define `ARDUINOJSON_ENABLE_NAN` and
|
||||
> `ARDUINOJSON_ENABLE_INFINITY` to `1`;:
|
||||
>
|
||||
> ```c++
|
||||
> #define ARDUINOJSON_ENABLE_NAN 1
|
||||
> #define ARDUINOJSON_ENABLE_INFINITY 1
|
||||
> #include <ArduinoJson.h>
|
||||
> ```
|
||||
>
|
||||
> #### The "or" operator
|
||||
>
|
||||
> This version slightly changes the behavior of the | operator when the
|
||||
> variant contains a float and the user requests an integer.
|
||||
>
|
||||
> Older versions returned the floating point value truncated.
|
||||
> Now, it returns the default value.
|
||||
>
|
||||
> ```c++
|
||||
> // suppose variant contains 1.2
|
||||
> int value = variant | 3;
|
||||
>
|
||||
> // old behavior:
|
||||
> value == 1
|
||||
>
|
||||
> // new behavior
|
||||
> value == 3
|
||||
> ```
|
||||
>
|
||||
> If you need the old behavior, you must add `if (variant.is<float>())`.
|
||||
|
||||
v6.10.1 (2019-04-23)
|
||||
-------
|
||||
|
||||
* Fixed error "attributes are not allowed on a function-definition"
|
||||
* Fixed `deserializeJson()` not being picky enough (issue #969)
|
||||
* Fixed error "no matching function for call to write(uint8_t)" (issue #972)
|
||||
|
||||
v6.10.0 (2019-03-22)
|
||||
-------
|
||||
|
||||
* Fixed an integer overflow in the JSON deserializer
|
||||
* Added overflow handling in `JsonVariant::as<T>()` and `JsonVariant::is<T>()`.
|
||||
- `as<T>()` returns `0` if the integer `T` overflows
|
||||
- `is<T>()` returns `false` if the integer `T` overflows
|
||||
* Added `BasicJsonDocument` to support custom allocator (issue #876)
|
||||
* Added `JsonDocument::containsKey()` (issue #938)
|
||||
* Added `JsonVariant::containsKey()`
|
||||
|
||||
v6.9.1 (2019-03-01)
|
||||
------
|
||||
|
||||
* Fixed warning "unused variable" with GCC 4.4 (issue #912)
|
||||
* Fixed warning "cast increases required alignment" (issue #914)
|
||||
* Fixed warning "conversion may alter value" (issue #914)
|
||||
* Fixed naming conflict with "CAPACITY" (issue #839)
|
||||
* Muted warning "will change in GCC 7.1" (issue #914)
|
||||
* Added a clear error message for `StaticJsonBuffer` and `DynamicJsonBuffer`
|
||||
* Marked ArduinoJson.h as a "system header"
|
||||
|
||||
v6.9.0 (2019-02-26)
|
||||
------
|
||||
|
||||
* Decode escaped Unicode characters like \u00DE (issue #304, PR #791)
|
||||
Many thanks to Daniel Schulte (aka @trilader) who implemented this feature.
|
||||
* Added option ARDUINOJSON_DECODE_UNICODE to enable it
|
||||
* Converted `JsonArray::copyFrom()/copyTo()` to free functions `copyArray()`
|
||||
* Renamed `JsonArray::copyFrom()` and `JsonObject::copyFrom()` to `set()`
|
||||
* Renamed `JsonArray::get()` to `getElement()`
|
||||
* Renamed `JsonArray::add()` (without arg) to `addElement()`
|
||||
* Renamed `JsonObject::get()` to `getMember()`
|
||||
* Renamed `JsonObject::getOrCreate()` to `getOrAddMember()`
|
||||
* Fixed `JsonVariant::isNull()` not returning `true` after `set((char*)0)`
|
||||
* Fixed segfault after `variant.set(serialized((char*)0))`
|
||||
* Detect `IncompleteInput` in `false`, `true`, and `null`
|
||||
* Added `JsonDocument::size()`
|
||||
* Added `JsonDocument::remove()`
|
||||
* Added `JsonVariant::clear()`
|
||||
* Added `JsonVariant::remove()`
|
||||
|
||||
v6.8.0-beta (2019-01-30)
|
||||
-----------
|
||||
|
||||
* Import functions in the ArduinoJson namespace to get clearer errors
|
||||
* Improved syntax highlighting in Arduino IDE
|
||||
* Removed default capacity of `DynamicJsonDocument`
|
||||
* `JsonArray::copyFrom()` accepts `JsonArrayConst`
|
||||
* `JsonVariant::set()` accepts `JsonArrayConst` and `JsonObjectConst`
|
||||
* `JsonDocument` was missing in the ArduinoJson namespace
|
||||
* Added `memoryUsage()` to `JsonArray`, `JsonObject`, and `JsonVariant`
|
||||
* Added `nesting()` to `JsonArray`, `JsonDocument`, `JsonObject`, and `JsonVariant`
|
||||
* Replaced `JsonDocument::nestingLimit` with an additional parameter
|
||||
to `deserializeJson()` and `deserializeMsgPack()`
|
||||
* Fixed uninitialized variant in `JsonDocument`
|
||||
* Fixed `StaticJsonDocument` copy constructor and copy assignment
|
||||
* The copy constructor of `DynamicJsonDocument` chooses the capacity according to the memory usage of the source, not from the capacity of the source.
|
||||
* Added the ability to create/assign a `StaticJsonDocument`/`DynamicJsonDocument` from a `JsonArray`/`JsonObject`/`JsonVariant`
|
||||
* Added `JsonDocument::isNull()`
|
||||
* Added `JsonDocument::operator[]`
|
||||
* Added `ARDUINOJSON_TAB` to configure the indentation character
|
||||
* Reduced the size of the pretty JSON serializer
|
||||
* Added `add()`, `createNestedArray()` and `createNestedObject()` to `JsonVariant`
|
||||
* `JsonVariant` automatically promotes to `JsonObject` or `JsonArray` on write.
|
||||
Calling `JsonVariant::to<T>()` is not required anymore.
|
||||
* `JsonDocument` now support the same operations as `JsonVariant`.
|
||||
Calling `JsonDocument::as<T>()` is not required anymore.
|
||||
* Fixed example `JsonHttpClient.ino`
|
||||
* User can now use a `JsonString` as a key or a value
|
||||
|
||||
> ### BREAKING CHANGES
|
||||
>
|
||||
> #### `DynamicJsonDocument`'s constructor
|
||||
>
|
||||
> The parameter to the constructor of `DynamicJsonDocument` is now mandatory
|
||||
>
|
||||
> Old code:
|
||||
>
|
||||
> ```c++
|
||||
> DynamicJsonDocument doc;
|
||||
> ```
|
||||
>
|
||||
> New code:
|
||||
>
|
||||
> ```c++
|
||||
> DynamicJsonDocument doc(1024);
|
||||
> ```
|
||||
>
|
||||
> #### Nesting limit
|
||||
>
|
||||
> `JsonDocument::nestingLimit` was replaced with a new parameter to `deserializeJson()` and `deserializeMsgPack()`.
|
||||
>
|
||||
> Old code:
|
||||
>
|
||||
> ```c++
|
||||
> doc.nestingLimit = 15;
|
||||
> deserializeJson(doc, input);
|
||||
> ```
|
||||
>
|
||||
> New code:
|
||||
>
|
||||
> ```c++
|
||||
> deserializeJson(doc, input, DeserializationOption::NestingLimit(15));
|
||||
> ```
|
||||
|
||||
v6.7.0-beta (2018-12-07)
|
||||
-----------
|
||||
|
||||
* Removed the automatic expansion of `DynamicJsonDocument`, it now has a fixed capacity.
|
||||
* Restored the monotonic allocator because the code was getting too big
|
||||
* Reduced the memory usage
|
||||
* Reduced the code size
|
||||
* Renamed `JsonKey` to `JsonString`
|
||||
* Removed spurious files in the Particle library
|
||||
|
||||
v6.6.0-beta (2018-11-13)
|
||||
-----------
|
||||
|
||||
* Removed `JsonArray::is<T>(i)` and `JsonArray::set(i,v)`
|
||||
* Removed `JsonObject::is<T>(k)` and `JsonObject::set(k,v)`
|
||||
* Replaced `T JsonArray::get<T>(i)` with `JsonVariant JsonArray::get(i)`
|
||||
* Replaced `T JsonObject::get<T>(k)` with `JsonVariant JsonObject::get(k)`
|
||||
* Added `JSON_STRING_SIZE()`
|
||||
* ~~Replacing or removing a value now releases the memory~~
|
||||
* Added `DeserializationError::code()` to be used in switch statements (issue #846)
|
||||
|
||||
v6.5.0-beta (2018-10-13)
|
||||
-----------
|
||||
|
||||
* Added implicit conversion from `JsonArray` and `JsonObject` to `JsonVariant`
|
||||
* Allow mixed configuration in compilation units (issue #809)
|
||||
* Fixed object keys not being duplicated
|
||||
* `JsonPair::key()` now returns a `JsonKey`
|
||||
* Increased the default capacity of `DynamicJsonDocument`
|
||||
* Fixed `JsonVariant::is<String>()` (closes #763)
|
||||
* Added `JsonArrayConst`, `JsonObjectConst`, and `JsonVariantConst`
|
||||
* Added copy-constructor and copy-assignment-operator for `JsonDocument` (issue #827)
|
||||
|
||||
v6.4.0-beta (2018-09-11)
|
||||
-----------
|
||||
|
||||
* Copy `JsonArray` and `JsonObject`, instead of storing pointers (issue #780)
|
||||
* Added `JsonVariant::to<JsonArray>()` and `JsonVariant::to<JsonObject>()`
|
||||
|
||||
v6.3.0-beta (2018-08-31)
|
||||
-----------
|
||||
|
||||
* Implemented reference semantics for `JsonVariant`
|
||||
* Replaced `JsonPair`'s `key` and `value` with `key()` and `value()`
|
||||
* Fixed `serializeJson(obj[key], dst)` (issue #794)
|
||||
|
||||
> ### BREAKING CHANGES
|
||||
>
|
||||
> #### JsonVariant
|
||||
>
|
||||
> `JsonVariant` now has a semantic similar to `JsonObject` and `JsonArray`.
|
||||
> It's a reference to a value stored in the `JsonDocument`.
|
||||
> As a consequence, a `JsonVariant` cannot be used as a standalone variable anymore.
|
||||
>
|
||||
> Old code:
|
||||
>
|
||||
> ```c++
|
||||
> JsonVariant myValue = 42;
|
||||
> ```
|
||||
>
|
||||
> New code:
|
||||
>
|
||||
> ```c++
|
||||
> DynamicJsonDocument doc;
|
||||
> JsonVariant myValue = doc.to<JsonVariant>();
|
||||
> myValue.set(42);
|
||||
> ```
|
||||
>
|
||||
> #### JsonPair
|
||||
>
|
||||
> Old code:
|
||||
>
|
||||
> ```c++
|
||||
> for(JsonPair p : myObject) {
|
||||
> Serial.println(p.key);
|
||||
> Serial.println(p.value.as<int>());
|
||||
> }
|
||||
> ```
|
||||
>
|
||||
> New code:
|
||||
>
|
||||
> ```c++
|
||||
> for(JsonPair p : myObject) {
|
||||
> Serial.println(p.key());
|
||||
> Serial.println(p.value().as<int>());
|
||||
> }
|
||||
> ```
|
||||
>
|
||||
> CAUTION: the key is now read only!
|
||||
|
||||
v6.2.3-beta (2018-07-19)
|
||||
-----------
|
||||
|
||||
* Fixed exception when using Flash strings as object keys (issue #784)
|
||||
|
||||
v6.2.2-beta (2018-07-18)
|
||||
-----------
|
||||
|
||||
* Fixed `invalid application of 'sizeof' to incomplete type '__FlashStringHelper'` (issue #783)
|
||||
* Fixed `char[]` not duplicated when passed to `JsonVariant::operator[]`
|
||||
|
||||
v6.2.1-beta (2018-07-17)
|
||||
-----------
|
||||
|
||||
* Fixed `JsonObject` not inserting keys of type `String` (issue #782)
|
||||
|
||||
v6.2.0-beta (2018-07-12)
|
||||
-----------
|
||||
|
||||
* Disabled lazy number deserialization (issue #772)
|
||||
* Fixed `JsonVariant::is<int>()` that returned true for empty strings
|
||||
* Improved float serialization when `-fsingle-precision-constant` is used
|
||||
* Renamed function `RawJson()` to `serialized()`
|
||||
* `serializeMsgPack()` now supports values marked with `serialized()`
|
||||
|
||||
> ### BREAKING CHANGES
|
||||
>
|
||||
> #### Non quoted strings
|
||||
>
|
||||
> Non quoted strings are now forbidden in values, but they are still allowed in keys.
|
||||
> For example, `{key:"value"}` is accepted, but `{key:value}` is not.
|
||||
>
|
||||
> #### Preformatted values
|
||||
>
|
||||
> Old code:
|
||||
>
|
||||
> ```c++
|
||||
> object["values"] = RawJson("[1,2,3,4]");
|
||||
> ```
|
||||
>
|
||||
> New code:
|
||||
>
|
||||
> ```c++
|
||||
> object["values"] = serialized("[1,2,3,4]");
|
||||
> ```
|
||||
|
||||
v6.1.0-beta (2018-07-02)
|
||||
-----------
|
||||
|
||||
* Return `JsonArray` and `JsonObject` by value instead of reference (issue #309)
|
||||
* Replaced `success()` with `isNull()`
|
||||
|
||||
> ### BREAKING CHANGES
|
||||
>
|
||||
> Old code:
|
||||
>
|
||||
> ```c++
|
||||
> JsonObject& obj = doc.to<JsonObject>();
|
||||
> JsonArray& arr = obj.createNestedArray("key");
|
||||
> if (!arr.success()) {
|
||||
> Serial.println("Not enough memory");
|
||||
> return;
|
||||
> }
|
||||
> ```
|
||||
>
|
||||
> New code:
|
||||
>
|
||||
> ```c++
|
||||
> JsonObject obj = doc.to<JsonObject>();
|
||||
> JsonArray arr = obj.createNestedArray("key");
|
||||
> if (arr.isNull()) {
|
||||
> Serial.println("Not enough memory");
|
||||
> return;
|
||||
> }
|
||||
> ```
|
||||
|
||||
v6.0.1-beta (2018-06-11)
|
||||
-----------
|
||||
|
||||
* Fixed conflicts with `isnan()` and `isinf()` macros (issue #752)
|
||||
|
||||
v6.0.0-beta (2018-06-07)
|
||||
-----------
|
||||
|
||||
* Added `DynamicJsonDocument` and `StaticJsonDocument`
|
||||
* Added `deserializeJson()`
|
||||
* Added `serializeJson()` and `serializeJsonPretty()`
|
||||
* Added `measureJson()` and `measureJsonPretty()`
|
||||
* Added `serializeMsgPack()`, `deserializeMsgPack()` and `measureMsgPack()` (issue #358)
|
||||
* Added example `MsgPackParser.ino` (issue #358)
|
||||
* Added support for non zero-terminated strings (issue #704)
|
||||
* Removed `JsonBuffer::parseArray()`, `parseObject()` and `parse()`
|
||||
* Removed `JsonBuffer::createArray()` and `createObject()`
|
||||
* Removed `printTo()` and `prettyPrintTo()`
|
||||
* Removed `measureLength()` and `measurePrettyLength()`
|
||||
* Removed all deprecated features
|
||||
|
||||
> ### BREAKING CHANGES
|
||||
>
|
||||
> #### Deserialization
|
||||
>
|
||||
> Old code:
|
||||
>
|
||||
> ```c++
|
||||
> DynamicJsonBuffer jb;
|
||||
> JsonObject& obj = jb.parseObject(json);
|
||||
> if (obj.success()) {
|
||||
>
|
||||
> }
|
||||
> ```
|
||||
>
|
||||
> New code:
|
||||
>
|
||||
> ```c++
|
||||
> DynamicJsonDocument doc;
|
||||
> DeserializationError error = deserializeJson(doc, json);
|
||||
> if (error) {
|
||||
>
|
||||
> }
|
||||
> JsonObject& obj = doc.as<JsonObject>();
|
||||
> ```
|
||||
>
|
||||
> #### Serialization
|
||||
>
|
||||
> Old code:
|
||||
>
|
||||
> ```c++
|
||||
> DynamicJsonBuffer jb;
|
||||
> JsonObject& obj = jb.createObject();
|
||||
> obj["key"] = "value";
|
||||
> obj.printTo(Serial);
|
||||
> ```
|
||||
>
|
||||
> New code:
|
||||
>
|
||||
> ```c++
|
||||
> DynamicJsonDocument obj;
|
||||
> JsonObject& obj = doc.to<JsonObject>();
|
||||
> obj["key"] = "value";
|
||||
> serializeJson(doc, Serial);
|
||||
> ```
|
||||
HEAD
|
||||
----
|
||||
|
||||
* Remove `BasicJsonDocument`
|
||||
* Remove `StaticJsonDocument`
|
||||
* Add abstract `Allocator` class
|
||||
* Merge `DynamicJsonDocument` with `JsonDocument`
|
||||
* Remove `JSON_ARRAY_SIZE()`, `JSON_OBJECT_SIZE()`, and `JSON_STRING_SIZE()`
|
||||
* Remove `ARDUINOJSON_ENABLE_STRING_DEDUPLICATION` (string deduplication cannot be enabled anymore)
|
||||
* Remove `JsonDocument::capacity()`
|
||||
* Store the strings in the heap
|
||||
* Reference-count shared strings
|
||||
* Always store `serialized("string")` by copy (#1915)
|
||||
* Remove the zero-copy mode of `deserializeJson()` and `deserializeMsgPack()`
|
||||
|
@ -10,7 +10,7 @@ if(ESP_PLATFORM)
|
||||
return()
|
||||
endif()
|
||||
|
||||
project(ArduinoJson VERSION 6.21.0)
|
||||
project(ArduinoJson VERSION 7.0.0)
|
||||
|
||||
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
|
||||
include(CTest)
|
||||
|
21
README.md
21
README.md
@ -4,13 +4,10 @@
|
||||
|
||||
---
|
||||
|
||||
[](https://github.com/bblanchon/ArduinoJson/actions?query=workflow%3A%22Continuous+Integration%22+branch%3A6.x)
|
||||
[](https://ci.appveyor.com/project/bblanchon/arduinojson/branch/6.x)
|
||||
[](https://github.com/bblanchon/ArduinoJson/actions?query=workflow%3A%22Continuous+Integration%22+branch%3A7.x)
|
||||
[](https://ci.appveyor.com/project/bblanchon/arduinojson/branch/7.x)
|
||||
[](https://bugs.chromium.org/p/oss-fuzz/issues/list?sort=-opened&can=1&q=proj:arduinojson)
|
||||
[](https://coveralls.io/github/bblanchon/ArduinoJson?branch=6.x)
|
||||
[](https://www.ardu-badge.com/ArduinoJson/6.21.0)
|
||||
[](https://registry.platformio.org/packages/libraries/bblanchon/ArduinoJson?version=6.21.0)
|
||||
[](https://components.espressif.com/components/bblanchon/arduinojson)
|
||||
[](https://coveralls.io/github/bblanchon/ArduinoJson?branch=7.x)
|
||||
[](https://github.com/bblanchon/ArduinoJson/stargazers)
|
||||
[](https://github.com/sponsors/bblanchon)
|
||||
|
||||
@ -34,8 +31,6 @@ ArduinoJson is a C++ JSON library for Arduino and IoT (Internet Of Things).
|
||||
* [Twice smaller than the "official" Arduino_JSON library](https://arduinojson.org/2019/11/19/arduinojson-vs-arduino_json/)
|
||||
* [Almost 10% faster than the "official" Arduino_JSON library](https://arduinojson.org/2019/11/19/arduinojson-vs-arduino_json/)
|
||||
* [Consumes roughly 10% less RAM than the "official" Arduino_JSON library](https://arduinojson.org/2019/11/19/arduinojson-vs-arduino_json/)
|
||||
* [Fixed memory allocation, no heap fragmentation](https://arduinojson.org/v6/api/jsondocument/)
|
||||
* [Optionally works without heap memory (zero malloc)](https://arduinojson.org/v6/api/staticjsondocument/)
|
||||
* [Deduplicates strings](https://arduinojson.org/news/2020/08/01/version-6-16-0/)
|
||||
* Versatile
|
||||
* Supports [custom allocators (to use external RAM chip, for example)](https://arduinojson.org/v6/how-to/use-external-ram-on-esp32/)
|
||||
@ -82,10 +77,10 @@ ArduinoJson is a C++ JSON library for Arduino and IoT (Internet Of Things).
|
||||
* [TMP friendly](https://en.wikipedia.org/wiki/Template_metaprogramming)
|
||||
* Handles [integer overflows](https://arduinojson.org/v6/api/jsonvariant/as/#integer-overflows)
|
||||
* Well tested
|
||||
* [Unit test coverage close to 100%](https://coveralls.io/github/bblanchon/ArduinoJson?branch=6.x)
|
||||
* [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/6.x)
|
||||
* [GCC 6, 7, 8, 9, 10, 11](https://github.com/bblanchon/ArduinoJson/actions?query=workflow%3A%22Continuous+Integration%22)
|
||||
* [Visual Studio 2017, 2019, 2022](https://ci.appveyor.com/project/bblanchon/arduinojson/branch/7.x)
|
||||
* [GCC 5, 6, 7, 8, 9, 10, 11](https://github.com/bblanchon/ArduinoJson/actions?query=workflow%3A%22Continuous+Integration%22)
|
||||
* [Clang 3.8, 3.9, 4.0, 5.0, 6.0, 7, 8, 9, 10](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/)
|
||||
@ -111,7 +106,7 @@ Here is a program that parses a JSON document with ArduinoJson.
|
||||
```c++
|
||||
char json[] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
|
||||
|
||||
DynamicJsonDocument doc(1024);
|
||||
JsonDocument doc(1024);
|
||||
deserializeJson(doc, json);
|
||||
|
||||
const char* sensor = doc["sensor"];
|
||||
@ -127,7 +122,7 @@ See the [tutorial on arduinojson.org](https://arduinojson.org/v6/doc/deserializa
|
||||
Here is a program that generates a JSON document with ArduinoJson:
|
||||
|
||||
```c++
|
||||
DynamicJsonDocument doc(1024);
|
||||
JsonDocument doc(1024);
|
||||
|
||||
doc["sensor"] = "gps";
|
||||
doc["time"] = 1351824120;
|
||||
|
@ -1,4 +1,4 @@
|
||||
version: 6.21.0.{build}
|
||||
version: 7.0.0.{build}
|
||||
environment:
|
||||
matrix:
|
||||
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2022
|
||||
|
@ -34,18 +34,18 @@ struct Config {
|
||||
int port;
|
||||
};
|
||||
|
||||
const char *filename = "/config.txt"; // <- SD library uses 8.3 filenames
|
||||
const char* filename = "/config.txt"; // <- SD library uses 8.3 filenames
|
||||
Config config; // <- global configuration object
|
||||
|
||||
// Loads the configuration from a file
|
||||
void loadConfiguration(const char *filename, Config &config) {
|
||||
void loadConfiguration(const char* filename, Config& config) {
|
||||
// Open file for reading
|
||||
File file = SD.open(filename);
|
||||
|
||||
// Allocate a temporary JsonDocument
|
||||
// Don't forget to change the capacity to match your requirements.
|
||||
// Use https://arduinojson.org/v6/assistant to compute the capacity.
|
||||
StaticJsonDocument<512> doc;
|
||||
JsonDocument doc(512);
|
||||
|
||||
// Deserialize the JSON document
|
||||
DeserializationError error = deserializeJson(doc, file);
|
||||
@ -63,7 +63,7 @@ void loadConfiguration(const char *filename, Config &config) {
|
||||
}
|
||||
|
||||
// Saves the configuration to a file
|
||||
void saveConfiguration(const char *filename, const Config &config) {
|
||||
void saveConfiguration(const char* filename, const Config& config) {
|
||||
// Delete existing file, otherwise the configuration is appended to the file
|
||||
SD.remove(filename);
|
||||
|
||||
@ -77,7 +77,7 @@ void saveConfiguration(const char *filename, const Config &config) {
|
||||
// Allocate a temporary JsonDocument
|
||||
// Don't forget to change the capacity to match your requirements.
|
||||
// Use https://arduinojson.org/assistant to compute the capacity.
|
||||
StaticJsonDocument<256> doc;
|
||||
JsonDocument doc(256);
|
||||
|
||||
// Set the values in the document
|
||||
doc["hostname"] = config.hostname;
|
||||
@ -93,7 +93,7 @@ void saveConfiguration(const char *filename, const Config &config) {
|
||||
}
|
||||
|
||||
// Prints the content of a file to the Serial
|
||||
void printFile(const char *filename) {
|
||||
void printFile(const char* filename) {
|
||||
// Open file for reading
|
||||
File file = SD.open(filename);
|
||||
if (!file) {
|
||||
@ -114,7 +114,8 @@ void printFile(const char *filename) {
|
||||
void setup() {
|
||||
// Initialize serial port
|
||||
Serial.begin(9600);
|
||||
while (!Serial) continue;
|
||||
while (!Serial)
|
||||
continue;
|
||||
|
||||
// Initialize SD library
|
||||
const int chipSelect = 4;
|
||||
|
@ -11,10 +11,11 @@
|
||||
void setup() {
|
||||
// Initialize serial port
|
||||
Serial.begin(9600);
|
||||
while (!Serial) continue;
|
||||
while (!Serial)
|
||||
continue;
|
||||
|
||||
// The huge input: an extract from OpenWeatherMap response
|
||||
const __FlashStringHelper* input_json = F(
|
||||
auto input_json = F(
|
||||
"{\"cod\":\"200\",\"message\":0,\"list\":[{\"dt\":1581498000,\"main\":{"
|
||||
"\"temp\":3.23,\"feels_like\":-3.63,\"temp_min\":3.23,\"temp_max\":4.62,"
|
||||
"\"pressure\":1014,\"sea_level\":1014,\"grnd_level\":1010,\"humidity\":"
|
||||
@ -33,12 +34,12 @@ void setup() {
|
||||
"1000000,\"timezone\":0,\"sunrise\":1581492085,\"sunset\":1581527294}}");
|
||||
|
||||
// The filter: it contains "true" for each value we want to keep
|
||||
StaticJsonDocument<200> filter;
|
||||
JsonDocument filter(200);
|
||||
filter["list"][0]["dt"] = true;
|
||||
filter["list"][0]["main"]["temp"] = true;
|
||||
|
||||
// Deserialize the document
|
||||
StaticJsonDocument<400> doc;
|
||||
JsonDocument doc(400);
|
||||
deserializeJson(doc, input_json, DeserializationOption::Filter(filter));
|
||||
|
||||
// Print the result
|
||||
|
@ -11,19 +11,15 @@
|
||||
void setup() {
|
||||
// Initialize Serial port
|
||||
Serial.begin(9600);
|
||||
while (!Serial) continue;
|
||||
while (!Serial)
|
||||
continue;
|
||||
|
||||
// Allocate the JSON document
|
||||
//
|
||||
// Inside the brackets, 200 is the RAM allocated to this document.
|
||||
// Inside the parentheses, 200 is the RAM allocated to this document.
|
||||
// Don't forget to change this value to match your requirement.
|
||||
// Use https://arduinojson.org/v6/assistant to compute the capacity.
|
||||
StaticJsonDocument<200> doc;
|
||||
|
||||
// StaticJsonObject allocates memory on the stack, it can be
|
||||
// replaced by DynamicJsonDocument which allocates in the heap.
|
||||
//
|
||||
// DynamicJsonDocument doc(200);
|
||||
JsonDocument doc(200);
|
||||
|
||||
// Add values in the document
|
||||
//
|
||||
|
@ -78,8 +78,7 @@ void setup() {
|
||||
|
||||
// Allocate the JSON document
|
||||
// Use https://arduinojson.org/v6/assistant to compute the capacity.
|
||||
const size_t capacity = JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2) + 60;
|
||||
DynamicJsonDocument doc(capacity);
|
||||
JsonDocument doc(256);
|
||||
|
||||
// Parse JSON object
|
||||
DeserializationError error = deserializeJson(doc, client);
|
||||
|
@ -11,19 +11,15 @@
|
||||
void setup() {
|
||||
// Initialize serial port
|
||||
Serial.begin(9600);
|
||||
while (!Serial) continue;
|
||||
while (!Serial)
|
||||
continue;
|
||||
|
||||
// Allocate the JSON document
|
||||
//
|
||||
// Inside the brackets, 200 is the capacity of the memory pool in bytes.
|
||||
// Inside the parentheses, 200 is the capacity of the memory pool in bytes.
|
||||
// Don't forget to change this value to match your JSON document.
|
||||
// Use https://arduinojson.org/v6/assistant to compute the capacity.
|
||||
StaticJsonDocument<200> doc;
|
||||
|
||||
// StaticJsonDocument<N> allocates memory on the stack, it can be
|
||||
// replaced by DynamicJsonDocument which allocates in the heap.
|
||||
//
|
||||
// DynamicJsonDocument doc(200);
|
||||
JsonDocument doc(200);
|
||||
|
||||
// JSON input string.
|
||||
//
|
||||
|
@ -25,7 +25,8 @@ EthernetServer server(80);
|
||||
void setup() {
|
||||
// Initialize serial port
|
||||
Serial.begin(9600);
|
||||
while (!Serial) continue;
|
||||
while (!Serial)
|
||||
continue;
|
||||
|
||||
// Initialize Ethernet libary
|
||||
if (!Ethernet.begin(mac)) {
|
||||
@ -52,11 +53,12 @@ void loop() {
|
||||
Serial.println(F("New client"));
|
||||
|
||||
// Read the request (we ignore the content in this example)
|
||||
while (client.available()) client.read();
|
||||
while (client.available())
|
||||
client.read();
|
||||
|
||||
// Allocate a temporary JsonDocument
|
||||
// Use https://arduinojson.org/v6/assistant to compute the capacity.
|
||||
StaticJsonDocument<500> doc;
|
||||
JsonDocument doc(500);
|
||||
|
||||
// Create the "analog" array
|
||||
JsonArray analogValues = doc.createNestedArray("analog");
|
||||
|
@ -32,7 +32,8 @@ EthernetUDP udp;
|
||||
void setup() {
|
||||
// Initialize serial port
|
||||
Serial.begin(9600);
|
||||
while (!Serial) continue;
|
||||
while (!Serial)
|
||||
continue;
|
||||
|
||||
// Initialize Ethernet libary
|
||||
if (!Ethernet.begin(mac)) {
|
||||
@ -47,7 +48,7 @@ void setup() {
|
||||
void loop() {
|
||||
// Allocate a temporary JsonDocument
|
||||
// Use https://arduinojson.org/v6/assistant to compute the capacity.
|
||||
StaticJsonDocument<500> doc;
|
||||
JsonDocument doc(500);
|
||||
|
||||
// Create the "analog" array
|
||||
JsonArray analogValues = doc.createNestedArray("analog");
|
||||
|
@ -12,19 +12,15 @@
|
||||
void setup() {
|
||||
// Initialize serial port
|
||||
Serial.begin(9600);
|
||||
while (!Serial) continue;
|
||||
while (!Serial)
|
||||
continue;
|
||||
|
||||
// Allocate the JSON document
|
||||
//
|
||||
// Inside the brackets, 200 is the capacity of the memory pool in bytes.
|
||||
// Inside the parentheses, 200 is the capacity of the memory pool in bytes.
|
||||
// Don't forget to change this value to match your JSON document.
|
||||
// Use https://arduinojson.org/v6/assistant to compute the capacity.
|
||||
StaticJsonDocument<200> doc;
|
||||
|
||||
// StaticJsonObject allocates memory on the stack, it can be
|
||||
// replaced by DynamicJsonObject which allocates in the heap.
|
||||
//
|
||||
// DynamicJsonObject doc(200);
|
||||
JsonDocument doc(200);
|
||||
|
||||
// MessagePack input string.
|
||||
//
|
||||
|
@ -14,7 +14,7 @@
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
void setup() {
|
||||
DynamicJsonDocument doc(1024);
|
||||
JsonDocument doc(1024);
|
||||
|
||||
// You can use a Flash String as your JSON input.
|
||||
// WARNING: the strings in the input will be duplicated in the JsonDocument.
|
||||
|
@ -13,7 +13,7 @@
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
void setup() {
|
||||
DynamicJsonDocument doc(1024);
|
||||
JsonDocument doc(1024);
|
||||
|
||||
// You can use a String as your JSON input.
|
||||
// WARNING: the string in the input will be duplicated in the JsonDocument.
|
||||
|
@ -6,7 +6,7 @@
|
||||
|
||||
extern "C" void app_main() {
|
||||
char buffer[256];
|
||||
StaticJsonDocument<200> doc;
|
||||
JsonDocument doc(200);
|
||||
|
||||
doc["hello"] = "world";
|
||||
serializeJson(doc, buffer);
|
||||
|
@ -1,5 +1,7 @@
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
static_assert(ARDUINOJSON_ENABLE_PROGMEM == 1, "ARDUINOJSON_ENABLE_PROGMEM");
|
||||
|
||||
static_assert(ARDUINOJSON_USE_LONG_LONG == 0, "ARDUINOJSON_USE_LONG_LONG");
|
||||
|
||||
static_assert(ARDUINOJSON_SLOT_OFFSET_SIZE == 1,
|
||||
|
@ -1,7 +1,7 @@
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
DeserializationError error = deserializeJson(doc, data, size);
|
||||
if (!error) {
|
||||
std::string json;
|
||||
|
@ -1,7 +1,7 @@
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
DeserializationError error = deserializeMsgPack(doc, data, size);
|
||||
if (!error) {
|
||||
std::string json;
|
||||
|
@ -46,6 +46,7 @@ update_version_in_source () {
|
||||
-e "s/ARDUINOJSON_VERSION_MAJOR .*$/ARDUINOJSON_VERSION_MAJOR $MAJOR/" \
|
||||
-e "s/ARDUINOJSON_VERSION_MINOR .*$/ARDUINOJSON_VERSION_MINOR $MINOR/" \
|
||||
-e "s/ARDUINOJSON_VERSION_REVISION .*$/ARDUINOJSON_VERSION_REVISION $REVISION/" \
|
||||
-e "s/ARDUINOJSON_VERSION_MACRO .*$/ARDUINOJSON_VERSION_MACRO V$MAJOR$MINOR$REVISION/" \
|
||||
src/ArduinoJson/version.hpp
|
||||
rm src/ArduinoJson/version.hpp*~
|
||||
}
|
||||
|
@ -10,20 +10,10 @@
|
||||
int main() {
|
||||
// Allocate the JSON document
|
||||
//
|
||||
// Inside the brackets, 200 is the RAM allocated to this document.
|
||||
// Inside the parentheses, 200 is the RAM allocated to this document.
|
||||
// Don't forget to change this value to match your requirement.
|
||||
// Use https://arduinojson.org/v6/assistant to compute the capacity.
|
||||
StaticJsonDocument<200> doc;
|
||||
|
||||
// StaticJsonObject allocates memory on the stack, it can be
|
||||
// replaced by DynamicJsonDocument which allocates in the heap.
|
||||
//
|
||||
// DynamicJsonDocument doc(200);
|
||||
|
||||
// StaticJsonObject allocates memory on the stack, it can be
|
||||
// replaced by DynamicJsonDocument which allocates in the heap.
|
||||
//
|
||||
// DynamicJsonDocument doc(200);
|
||||
JsonDocument doc(200);
|
||||
|
||||
// Add values in the document
|
||||
//
|
||||
|
@ -10,15 +10,10 @@
|
||||
int main() {
|
||||
// Allocate the JSON document
|
||||
//
|
||||
// Inside the brackets, 200 is the capacity of the memory pool in bytes.
|
||||
// Inside the parentheses, 200 is the capacity of the memory pool in bytes.
|
||||
// Don't forget to change this value to match your JSON document.
|
||||
// Use https://arduinojson.org/v6/assistant to compute the capacity.
|
||||
StaticJsonDocument<300> doc;
|
||||
|
||||
// StaticJsonDocument<N> allocates memory on the stack, it can be
|
||||
// replaced by DynamicJsonDocument which allocates in the heap.
|
||||
//
|
||||
// DynamicJsonDocument doc(200);
|
||||
JsonDocument doc(300);
|
||||
|
||||
// JSON input string.
|
||||
//
|
||||
|
@ -10,15 +10,10 @@
|
||||
int main() {
|
||||
// Allocate the JSON document
|
||||
//
|
||||
// Inside the brackets, 300 is the size of the memory pool in bytes.
|
||||
// Inside the parentheses, 300 is the size of the memory pool in bytes.
|
||||
// Don't forget to change this value to match your JSON document.
|
||||
// Use https://arduinojson.org/assistant to compute the capacity.
|
||||
StaticJsonDocument<300> doc;
|
||||
|
||||
// StaticJsonObject allocates memory on the stack, it can be
|
||||
// replaced by DynamicJsonObject which allocates in the heap.
|
||||
//
|
||||
// DynamicJsonObject doc(200);
|
||||
JsonDocument doc(300);
|
||||
|
||||
// MessagePack input string.
|
||||
//
|
||||
|
@ -14,8 +14,8 @@ compile() {
|
||||
{
|
||||
"code":$(read_string "$FILE_PATH"),
|
||||
"codes": [{"file":"ArduinoJson.h","code":$(read_string "$ARDUINOJSON_H")}],
|
||||
"options": "warning",
|
||||
"compiler": "gcc-4.9.4",
|
||||
"options": "warning,c++11",
|
||||
"compiler": "gcc-5.5.0",
|
||||
"save": true
|
||||
}
|
||||
END
|
||||
|
@ -20,7 +20,7 @@ add_subdirectory(JsonDocument)
|
||||
add_subdirectory(JsonObject)
|
||||
add_subdirectory(JsonSerializer)
|
||||
add_subdirectory(JsonVariant)
|
||||
add_subdirectory(MemoryPool)
|
||||
add_subdirectory(ResourceManager)
|
||||
add_subdirectory(Misc)
|
||||
add_subdirectory(MixedConfiguration)
|
||||
add_subdirectory(MsgPackDeserializer)
|
||||
|
@ -7,8 +7,11 @@
|
||||
# error ARDUINOJSON_ENABLE_STRING_VIEW must be set to 1
|
||||
#endif
|
||||
|
||||
using ArduinoJson::detail::sizeofArray;
|
||||
using ArduinoJson::detail::sizeofString;
|
||||
|
||||
TEST_CASE("string_view") {
|
||||
StaticJsonDocument<256> doc;
|
||||
JsonDocument doc(256);
|
||||
JsonVariant variant = doc.to<JsonVariant>();
|
||||
|
||||
SECTION("deserializeJson()") {
|
||||
@ -53,16 +56,18 @@ TEST_CASE("string_view") {
|
||||
|
||||
SECTION("String deduplication") {
|
||||
doc.add(std::string_view("example one", 7));
|
||||
REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(1) + 8);
|
||||
REQUIRE(doc.memoryUsage() == sizeofArray(1) + sizeofString(7));
|
||||
|
||||
doc.add(std::string_view("example two", 7));
|
||||
REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(2) + 8);
|
||||
REQUIRE(doc.memoryUsage() == sizeofArray(2) + sizeofString(7));
|
||||
|
||||
doc.add(std::string_view("example\0tree", 12));
|
||||
REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(3) + 21);
|
||||
REQUIRE(doc.memoryUsage() ==
|
||||
sizeofArray(3) + sizeofString(7) + sizeofString(12));
|
||||
|
||||
doc.add(std::string_view("example\0tree and a half", 12));
|
||||
REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(4) + 21);
|
||||
REQUIRE(doc.memoryUsage() ==
|
||||
sizeofArray(4) + sizeofString(7) + sizeofString(12));
|
||||
}
|
||||
|
||||
SECTION("as<std::string_view>()") {
|
||||
|
@ -4,7 +4,7 @@
|
||||
#include <string>
|
||||
|
||||
TEST_CASE("C++20 smoke test") {
|
||||
StaticJsonDocument<128> doc;
|
||||
JsonDocument doc(128);
|
||||
|
||||
deserializeJson(doc, "{\"hello\":\"world\"}");
|
||||
REQUIRE(doc["hello"] == "world");
|
||||
|
@ -23,18 +23,12 @@ endmacro()
|
||||
add_executable(Issue978 Issue978.cpp)
|
||||
build_should_fail(Issue978)
|
||||
|
||||
add_executable(Issue1189 Issue1189.cpp)
|
||||
build_should_fail(Issue1189)
|
||||
|
||||
add_executable(read_long_long read_long_long.cpp)
|
||||
build_should_fail(read_long_long)
|
||||
|
||||
add_executable(write_long_long write_long_long.cpp)
|
||||
build_should_fail(write_long_long)
|
||||
|
||||
add_executable(delete_jsondocument delete_jsondocument.cpp)
|
||||
build_should_fail(delete_jsondocument)
|
||||
|
||||
add_executable(variant_as_char variant_as_char.cpp)
|
||||
build_should_fail(variant_as_char)
|
||||
|
||||
|
@ -1,13 +0,0 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
// a function should not be able to get a JsonDocument by value
|
||||
void f(JsonDocument) {}
|
||||
|
||||
int main() {
|
||||
DynamicJsonDocument doc(1024);
|
||||
f(doc);
|
||||
}
|
@ -8,6 +8,6 @@ struct Stream {};
|
||||
|
||||
int main() {
|
||||
Stream* stream = 0;
|
||||
DynamicJsonDocument doc(1024);
|
||||
JsonDocument doc(1024);
|
||||
deserializeJson(doc, stream);
|
||||
}
|
||||
|
@ -7,6 +7,6 @@
|
||||
// See issue #1498
|
||||
|
||||
int main() {
|
||||
DynamicJsonDocument doc(1024);
|
||||
JsonDocument doc(1024);
|
||||
doc["dummy"] = 'A';
|
||||
}
|
||||
|
@ -1,12 +0,0 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
struct Stream {};
|
||||
|
||||
int main() {
|
||||
JsonDocument* doc = new DynamicJsonDocument(42);
|
||||
delete doc;
|
||||
}
|
@ -11,6 +11,6 @@
|
||||
|
||||
ARDUINOJSON_ASSERT_INTEGER_TYPE_IS_SUPPORTED(long long)
|
||||
int main() {
|
||||
DynamicJsonDocument doc(1024);
|
||||
JsonDocument doc(1024);
|
||||
doc["dummy"].as<long long>();
|
||||
}
|
||||
|
@ -7,6 +7,6 @@
|
||||
// See issue #1498
|
||||
|
||||
int main() {
|
||||
DynamicJsonDocument doc(1024);
|
||||
JsonDocument doc(1024);
|
||||
doc["dummy"].as<char>();
|
||||
}
|
||||
|
@ -10,6 +10,6 @@
|
||||
#endif
|
||||
|
||||
int main() {
|
||||
DynamicJsonDocument doc(1024);
|
||||
JsonDocument doc(1024);
|
||||
doc["dummy"] = static_cast<long long>(42);
|
||||
}
|
||||
|
219
extras/tests/Helpers/Allocators.hpp
Normal file
219
extras/tests/Helpers/Allocators.hpp
Normal file
@ -0,0 +1,219 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson/Memory/Allocator.hpp>
|
||||
|
||||
#include <sstream>
|
||||
|
||||
struct FailingAllocator : ArduinoJson::Allocator {
|
||||
static FailingAllocator* instance() {
|
||||
static FailingAllocator allocator;
|
||||
return &allocator;
|
||||
}
|
||||
|
||||
private:
|
||||
FailingAllocator() = default;
|
||||
~FailingAllocator() = default;
|
||||
|
||||
void* allocate(size_t) override {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void deallocate(void*) override {}
|
||||
|
||||
void* reallocate(void*, size_t) override {
|
||||
return nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
class AllocatorLog {
|
||||
public:
|
||||
static std::string Allocate(size_t s) {
|
||||
char buffer[32];
|
||||
sprintf(buffer, "allocate(%zu)", s);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
static std::string AllocateFail(size_t s) {
|
||||
char buffer[32];
|
||||
sprintf(buffer, "allocate(%zu) -> nullptr", s);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
static std::string Reallocate(size_t s1, size_t s2) {
|
||||
char buffer[32];
|
||||
sprintf(buffer, "reallocate(%zu, %zu)", s1, s2);
|
||||
return buffer;
|
||||
};
|
||||
|
||||
static std::string ReallocateFail(size_t s1, size_t s2) {
|
||||
char buffer[32];
|
||||
sprintf(buffer, "reallocate(%zu, %zu) -> nullptr", s1, s2);
|
||||
return buffer;
|
||||
};
|
||||
|
||||
static std::string Deallocate(size_t s) {
|
||||
char buffer[32];
|
||||
sprintf(buffer, "deallocate(%zu)", s);
|
||||
return buffer;
|
||||
};
|
||||
|
||||
AllocatorLog& operator<<(const std::string& s) {
|
||||
log_ << s << "\n";
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::string str() const {
|
||||
auto s = log_.str();
|
||||
if (s.empty())
|
||||
return "(empty)";
|
||||
s.pop_back(); // remove the trailing '\n'
|
||||
return s;
|
||||
}
|
||||
|
||||
bool operator==(const AllocatorLog& other) const {
|
||||
return str() == other.str();
|
||||
}
|
||||
|
||||
friend std::ostream& operator<<(std::ostream& os, const AllocatorLog& log) {
|
||||
os << log.str();
|
||||
return os;
|
||||
}
|
||||
|
||||
private:
|
||||
std::ostringstream log_;
|
||||
};
|
||||
|
||||
class SpyingAllocator : public ArduinoJson::Allocator {
|
||||
public:
|
||||
SpyingAllocator(
|
||||
Allocator* upstream = ArduinoJson::detail::DefaultAllocator::instance())
|
||||
: upstream_(upstream) {}
|
||||
virtual ~SpyingAllocator() {}
|
||||
|
||||
void* allocate(size_t n) override {
|
||||
auto block = reinterpret_cast<AllocatedBlock*>(
|
||||
upstream_->allocate(sizeof(AllocatedBlock) + n - 1));
|
||||
if (block) {
|
||||
log_ << AllocatorLog::Allocate(n);
|
||||
block->size = n;
|
||||
return block->payload;
|
||||
} else {
|
||||
log_ << AllocatorLog::AllocateFail(n);
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void deallocate(void* p) override {
|
||||
auto block = AllocatedBlock::fromPayload(p);
|
||||
log_ << AllocatorLog::Deallocate(block->size);
|
||||
upstream_->deallocate(block);
|
||||
}
|
||||
|
||||
void* reallocate(void* p, size_t n) override {
|
||||
auto block = AllocatedBlock::fromPayload(p);
|
||||
auto oldSize = block->size;
|
||||
block = reinterpret_cast<AllocatedBlock*>(
|
||||
upstream_->reallocate(block, sizeof(AllocatedBlock) + n - 1));
|
||||
if (block) {
|
||||
log_ << AllocatorLog::Reallocate(oldSize, n);
|
||||
ARDUINOJSON_ASSERT(block->size == oldSize);
|
||||
block->size = n;
|
||||
return block->payload;
|
||||
} else {
|
||||
log_ << AllocatorLog::ReallocateFail(oldSize, n);
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void clearLog() {
|
||||
log_ = AllocatorLog();
|
||||
}
|
||||
|
||||
const AllocatorLog& log() const {
|
||||
return log_;
|
||||
}
|
||||
|
||||
private:
|
||||
struct AllocatedBlock {
|
||||
size_t size;
|
||||
char payload[1];
|
||||
|
||||
static AllocatedBlock* fromPayload(void* p) {
|
||||
return reinterpret_cast<AllocatedBlock*>(
|
||||
// Cast to void* to silence "cast increases required alignment of
|
||||
// target type [-Werror=cast-align]"
|
||||
reinterpret_cast<void*>(reinterpret_cast<char*>(p) -
|
||||
offsetof(AllocatedBlock, payload)));
|
||||
}
|
||||
};
|
||||
|
||||
AllocatorLog log_;
|
||||
Allocator* upstream_;
|
||||
};
|
||||
|
||||
class ControllableAllocator : public ArduinoJson::Allocator {
|
||||
public:
|
||||
ControllableAllocator(
|
||||
Allocator* upstream = ArduinoJson::detail::DefaultAllocator::instance())
|
||||
: enabled_(true), upstream_(upstream) {}
|
||||
virtual ~ControllableAllocator() {}
|
||||
|
||||
void* allocate(size_t n) override {
|
||||
return enabled_ ? upstream_->allocate(n) : 0;
|
||||
}
|
||||
|
||||
void deallocate(void* p) override {
|
||||
upstream_->deallocate(p);
|
||||
}
|
||||
|
||||
void* reallocate(void* ptr, size_t n) override {
|
||||
return enabled_ ? upstream_->reallocate(ptr, n) : 0;
|
||||
}
|
||||
|
||||
void disable() {
|
||||
enabled_ = false;
|
||||
}
|
||||
|
||||
private:
|
||||
bool enabled_;
|
||||
Allocator* upstream_;
|
||||
};
|
||||
|
||||
class TimebombAllocator : public ArduinoJson::Allocator {
|
||||
public:
|
||||
TimebombAllocator(
|
||||
size_t initialCountdown,
|
||||
Allocator* upstream = ArduinoJson::detail::DefaultAllocator::instance())
|
||||
: countdown_(initialCountdown), upstream_(upstream) {}
|
||||
virtual ~TimebombAllocator() {}
|
||||
|
||||
void* allocate(size_t n) override {
|
||||
if (!countdown_)
|
||||
return nullptr;
|
||||
countdown_--;
|
||||
return upstream_->allocate(n);
|
||||
}
|
||||
|
||||
void deallocate(void* p) override {
|
||||
upstream_->deallocate(p);
|
||||
}
|
||||
|
||||
void* reallocate(void* ptr, size_t n) override {
|
||||
if (!countdown_)
|
||||
return nullptr;
|
||||
countdown_--;
|
||||
return upstream_->reallocate(ptr, n);
|
||||
}
|
||||
|
||||
void setCountdown(size_t value) {
|
||||
countdown_ = value;
|
||||
}
|
||||
|
||||
private:
|
||||
size_t countdown_ = 0;
|
||||
Allocator* upstream_;
|
||||
};
|
@ -7,5 +7,7 @@
|
||||
#include "api/Print.h"
|
||||
#include "api/Stream.h"
|
||||
#include "api/String.h"
|
||||
#include "avr/pgmspace.h"
|
||||
|
||||
#define ARDUINO
|
||||
#define ARDUINO_H_INCLUDED 1
|
||||
|
@ -7,18 +7,18 @@
|
||||
#include <sstream>
|
||||
|
||||
class CustomReader {
|
||||
std::stringstream _stream;
|
||||
std::stringstream stream_;
|
||||
|
||||
public:
|
||||
CustomReader(const char* input) : _stream(input) {}
|
||||
CustomReader(const char* input) : stream_(input) {}
|
||||
CustomReader(const CustomReader&) = delete;
|
||||
|
||||
int read() {
|
||||
return _stream.get();
|
||||
return stream_.get();
|
||||
}
|
||||
|
||||
size_t readBytes(char* buffer, size_t length) {
|
||||
_stream.read(buffer, static_cast<std::streamsize>(length));
|
||||
return static_cast<size_t>(_stream.gcount());
|
||||
stream_.read(buffer, static_cast<std::streamsize>(length));
|
||||
return static_cast<size_t>(stream_.gcount());
|
||||
}
|
||||
};
|
||||
|
@ -9,11 +9,11 @@
|
||||
// Reproduces Arduino's String class
|
||||
class String {
|
||||
public:
|
||||
String() : _maxCapacity(1024) {}
|
||||
explicit String(const char* s) : _str(s), _maxCapacity(1024) {}
|
||||
String() : maxCapacity_(1024) {}
|
||||
explicit String(const char* s) : str_(s), maxCapacity_(1024) {}
|
||||
|
||||
void limitCapacityTo(size_t maxCapacity) {
|
||||
_maxCapacity = maxCapacity;
|
||||
maxCapacity_ = maxCapacity;
|
||||
}
|
||||
|
||||
unsigned char concat(const char* s) {
|
||||
@ -21,45 +21,45 @@ class String {
|
||||
}
|
||||
|
||||
size_t length() const {
|
||||
return _str.size();
|
||||
return str_.size();
|
||||
}
|
||||
|
||||
const char* c_str() const {
|
||||
return _str.c_str();
|
||||
return str_.c_str();
|
||||
}
|
||||
|
||||
bool operator==(const char* s) const {
|
||||
return _str == s;
|
||||
return str_ == s;
|
||||
}
|
||||
|
||||
String& operator=(const char* s) {
|
||||
_str.assign(s);
|
||||
str_.assign(s);
|
||||
return *this;
|
||||
}
|
||||
|
||||
char operator[](unsigned int index) const {
|
||||
if (index >= _str.size())
|
||||
if (index >= str_.size())
|
||||
return 0;
|
||||
return _str[index];
|
||||
return str_[index];
|
||||
}
|
||||
|
||||
friend std::ostream& operator<<(std::ostream& lhs, const ::String& rhs) {
|
||||
lhs << rhs._str;
|
||||
lhs << rhs.str_;
|
||||
return lhs;
|
||||
}
|
||||
|
||||
protected:
|
||||
// This function is protected in most Arduino cores
|
||||
unsigned char concat(const char* s, size_t n) {
|
||||
if (_str.size() + n > _maxCapacity)
|
||||
if (str_.size() + n > maxCapacity_)
|
||||
return 0;
|
||||
_str.append(s, n);
|
||||
str_.append(s, n);
|
||||
return 1;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string _str;
|
||||
size_t _maxCapacity;
|
||||
std::string str_;
|
||||
size_t maxCapacity_;
|
||||
};
|
||||
|
||||
class StringSumHelper : public ::String {};
|
||||
|
@ -2,6 +2,8 @@
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h> // uint8_t
|
||||
|
||||
#define PROGMEM
|
@ -9,7 +9,7 @@ add_executable(IntegrationTests
|
||||
openweathermap.cpp
|
||||
)
|
||||
|
||||
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
|
||||
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 6)
|
||||
target_compile_options(IntegrationTests
|
||||
PUBLIC
|
||||
-fsingle-precision-constant # issue 544
|
||||
|
@ -6,7 +6,7 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("Gbathree") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
|
||||
DeserializationError error = deserializeJson(
|
||||
doc,
|
||||
|
@ -8,8 +8,8 @@
|
||||
// https://github.com/bblanchon/ArduinoJson/issues/772
|
||||
|
||||
TEST_CASE("Issue772") {
|
||||
DynamicJsonDocument doc1(4096);
|
||||
DynamicJsonDocument doc2(4096);
|
||||
JsonDocument doc1(4096);
|
||||
JsonDocument doc2(4096);
|
||||
DeserializationError err;
|
||||
std::string data =
|
||||
"{\"state\":{\"reported\":{\"timestamp\":\"2018-07-02T09:40:12Z\","
|
||||
|
@ -53,12 +53,12 @@ TEST_CASE("OpenWeatherMap") {
|
||||
"]}";
|
||||
// clang-format on
|
||||
|
||||
StaticJsonDocument<512> filter;
|
||||
JsonDocument filter(512);
|
||||
filter["list"][0]["dt"] = true;
|
||||
filter["list"][0]["main"]["temp"] = true;
|
||||
filter["list"][0]["weather"][0]["description"] = true;
|
||||
|
||||
DynamicJsonDocument doc(16384);
|
||||
JsonDocument doc(16384);
|
||||
|
||||
REQUIRE(
|
||||
deserializeJson(doc, input_json, DeserializationOption::Filter(filter)) ==
|
||||
|
@ -6,7 +6,7 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
void check(std::string originalJson) {
|
||||
DynamicJsonDocument doc(16384);
|
||||
JsonDocument doc(16384);
|
||||
|
||||
std::string prettyJson;
|
||||
deserializeJson(doc, originalJson);
|
||||
|
@ -5,8 +5,11 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
using ArduinoJson::detail::sizeofArray;
|
||||
using ArduinoJson::detail::sizeofString;
|
||||
|
||||
TEST_CASE("JsonArray::add()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
|
||||
SECTION("int") {
|
||||
@ -51,7 +54,7 @@ TEST_CASE("JsonArray::add()") {
|
||||
#endif
|
||||
|
||||
SECTION("nested array") {
|
||||
DynamicJsonDocument doc2(4096);
|
||||
JsonDocument doc2(4096);
|
||||
JsonArray arr = doc2.to<JsonArray>();
|
||||
|
||||
array.add(arr);
|
||||
@ -62,7 +65,7 @@ TEST_CASE("JsonArray::add()") {
|
||||
}
|
||||
|
||||
SECTION("nested object") {
|
||||
DynamicJsonDocument doc2(4096);
|
||||
JsonDocument doc2(4096);
|
||||
JsonObject obj = doc2.to<JsonObject>();
|
||||
|
||||
array.add(obj);
|
||||
@ -74,7 +77,7 @@ TEST_CASE("JsonArray::add()") {
|
||||
|
||||
SECTION("array subscript") {
|
||||
const char* str = "hello";
|
||||
DynamicJsonDocument doc2(4096);
|
||||
JsonDocument doc2(4096);
|
||||
JsonArray arr = doc2.to<JsonArray>();
|
||||
arr.add(str);
|
||||
|
||||
@ -85,7 +88,7 @@ TEST_CASE("JsonArray::add()") {
|
||||
|
||||
SECTION("object subscript") {
|
||||
const char* str = "hello";
|
||||
DynamicJsonDocument doc2(4096);
|
||||
JsonDocument doc2(4096);
|
||||
JsonObject obj = doc2.to<JsonObject>();
|
||||
obj["x"] = str;
|
||||
|
||||
@ -96,43 +99,43 @@ TEST_CASE("JsonArray::add()") {
|
||||
|
||||
SECTION("should not duplicate const char*") {
|
||||
array.add("world");
|
||||
const size_t expectedSize = JSON_ARRAY_SIZE(1);
|
||||
const size_t expectedSize = sizeofArray(1);
|
||||
REQUIRE(expectedSize == doc.memoryUsage());
|
||||
}
|
||||
|
||||
SECTION("should duplicate char*") {
|
||||
array.add(const_cast<char*>("world"));
|
||||
const size_t expectedSize = JSON_ARRAY_SIZE(1) + JSON_STRING_SIZE(5);
|
||||
const size_t expectedSize = sizeofArray(1) + sizeofString(5);
|
||||
REQUIRE(expectedSize == doc.memoryUsage());
|
||||
}
|
||||
|
||||
SECTION("should duplicate std::string") {
|
||||
array.add(std::string("world"));
|
||||
const size_t expectedSize = JSON_ARRAY_SIZE(1) + JSON_STRING_SIZE(5);
|
||||
const size_t expectedSize = sizeofArray(1) + sizeofString(5);
|
||||
REQUIRE(expectedSize == doc.memoryUsage());
|
||||
}
|
||||
|
||||
SECTION("should not duplicate serialized(const char*)") {
|
||||
SECTION("should duplicate serialized(const char*)") {
|
||||
array.add(serialized("{}"));
|
||||
const size_t expectedSize = JSON_ARRAY_SIZE(1);
|
||||
const size_t expectedSize = sizeofArray(1) + sizeofString(2);
|
||||
REQUIRE(expectedSize == doc.memoryUsage());
|
||||
}
|
||||
|
||||
SECTION("should duplicate serialized(char*)") {
|
||||
array.add(serialized(const_cast<char*>("{}")));
|
||||
const size_t expectedSize = JSON_ARRAY_SIZE(1) + JSON_STRING_SIZE(2);
|
||||
const size_t expectedSize = sizeofArray(1) + sizeofString(2);
|
||||
REQUIRE(expectedSize == doc.memoryUsage());
|
||||
}
|
||||
|
||||
SECTION("should duplicate serialized(std::string)") {
|
||||
array.add(serialized(std::string("{}")));
|
||||
const size_t expectedSize = JSON_ARRAY_SIZE(1) + JSON_STRING_SIZE(2);
|
||||
const size_t expectedSize = sizeofArray(1) + sizeofString(2);
|
||||
REQUIRE(expectedSize == doc.memoryUsage());
|
||||
}
|
||||
|
||||
SECTION("should duplicate serialized(std::string)") {
|
||||
array.add(serialized(std::string("\0XX", 3)));
|
||||
const size_t expectedSize = JSON_ARRAY_SIZE(1) + JSON_STRING_SIZE(3);
|
||||
const size_t expectedSize = sizeofArray(1) + sizeofString(3);
|
||||
REQUIRE(expectedSize == doc.memoryUsage());
|
||||
}
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ TEST_CASE("JsonArray::clear()") {
|
||||
}
|
||||
|
||||
SECTION("Removes all elements") {
|
||||
StaticJsonDocument<64> doc;
|
||||
JsonDocument doc(64);
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
array.add(1);
|
||||
array.add(2);
|
||||
|
@ -6,7 +6,7 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("Compare JsonArray with JsonArray") {
|
||||
StaticJsonDocument<256> doc;
|
||||
JsonDocument doc(256);
|
||||
|
||||
SECTION("Compare with unbound") {
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
@ -82,7 +82,7 @@ TEST_CASE("Compare JsonArray with JsonArray") {
|
||||
}
|
||||
|
||||
TEST_CASE("Compare JsonArray with JsonVariant") {
|
||||
StaticJsonDocument<256> doc;
|
||||
JsonDocument doc(256);
|
||||
|
||||
SECTION("Compare with self") {
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
@ -153,7 +153,7 @@ TEST_CASE("Compare JsonArray with JsonVariant") {
|
||||
}
|
||||
|
||||
TEST_CASE("Compare JsonArray with JsonVariantConst") {
|
||||
StaticJsonDocument<256> doc;
|
||||
JsonDocument doc(256);
|
||||
|
||||
SECTION("Compare with unbound") {
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
@ -247,7 +247,7 @@ TEST_CASE("Compare JsonArray with JsonVariantConst") {
|
||||
}
|
||||
|
||||
TEST_CASE("Compare JsonArray with JsonArrayConst") {
|
||||
StaticJsonDocument<256> doc;
|
||||
JsonDocument doc(256);
|
||||
|
||||
SECTION("Compare with unbound") {
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
@ -347,7 +347,7 @@ TEST_CASE("Compare JsonArray with JsonArrayConst") {
|
||||
}
|
||||
|
||||
TEST_CASE("Compare JsonArrayConst with JsonArrayConst") {
|
||||
StaticJsonDocument<256> doc;
|
||||
JsonDocument doc(256);
|
||||
|
||||
SECTION("Compare with unbound") {
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
@ -430,7 +430,7 @@ TEST_CASE("Compare JsonArrayConst with JsonArrayConst") {
|
||||
}
|
||||
|
||||
TEST_CASE("Compare JsonArrayConst with JsonVariant") {
|
||||
StaticJsonDocument<256> doc;
|
||||
JsonDocument doc(256);
|
||||
|
||||
SECTION("Compare with self") {
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
|
@ -5,9 +5,11 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
using ArduinoJson::detail::sizeofArray;
|
||||
|
||||
TEST_CASE("copyArray()") {
|
||||
SECTION("int[] -> JsonArray") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
char json[32];
|
||||
int source[] = {1, 2, 3};
|
||||
@ -20,7 +22,7 @@ TEST_CASE("copyArray()") {
|
||||
}
|
||||
|
||||
SECTION("std::string[] -> JsonArray") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
char json[32];
|
||||
std::string source[] = {"a", "b", "c"};
|
||||
@ -33,7 +35,7 @@ TEST_CASE("copyArray()") {
|
||||
}
|
||||
|
||||
SECTION("const char*[] -> JsonArray") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
char json[32];
|
||||
const char* source[] = {"a", "b", "c"};
|
||||
@ -46,7 +48,7 @@ TEST_CASE("copyArray()") {
|
||||
}
|
||||
|
||||
SECTION("const char[][] -> JsonArray") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
char json[32];
|
||||
char source[][2] = {"a", "b", "c"};
|
||||
@ -59,7 +61,7 @@ TEST_CASE("copyArray()") {
|
||||
}
|
||||
|
||||
SECTION("const char[][] -> JsonDocument") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
char json[32];
|
||||
char source[][2] = {"a", "b", "c"};
|
||||
|
||||
@ -71,7 +73,7 @@ TEST_CASE("copyArray()") {
|
||||
}
|
||||
|
||||
SECTION("const char[][] -> MemberProxy") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
char json[32];
|
||||
char source[][2] = {"a", "b", "c"};
|
||||
|
||||
@ -83,7 +85,7 @@ TEST_CASE("copyArray()") {
|
||||
}
|
||||
|
||||
SECTION("int[] -> JsonDocument") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
char json[32];
|
||||
int source[] = {1, 2, 3};
|
||||
|
||||
@ -95,7 +97,7 @@ TEST_CASE("copyArray()") {
|
||||
}
|
||||
|
||||
SECTION("int[] -> MemberProxy") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
char json[32];
|
||||
int source[] = {1, 2, 3};
|
||||
|
||||
@ -107,8 +109,8 @@ TEST_CASE("copyArray()") {
|
||||
}
|
||||
|
||||
SECTION("int[] -> JsonArray, but not enough memory") {
|
||||
const size_t SIZE = JSON_ARRAY_SIZE(2);
|
||||
StaticJsonDocument<SIZE> doc;
|
||||
const size_t SIZE = sizeofArray(2);
|
||||
JsonDocument doc(SIZE);
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
char json[32];
|
||||
int source[] = {1, 2, 3};
|
||||
@ -121,7 +123,7 @@ TEST_CASE("copyArray()") {
|
||||
}
|
||||
|
||||
SECTION("int[][] -> JsonArray") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
char json[32];
|
||||
int source[][3] = {{1, 2, 3}, {4, 5, 6}};
|
||||
@ -134,7 +136,7 @@ TEST_CASE("copyArray()") {
|
||||
}
|
||||
|
||||
SECTION("int[][] -> MemberProxy") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
char json[32];
|
||||
int source[][3] = {{1, 2, 3}, {4, 5, 6}};
|
||||
|
||||
@ -146,7 +148,7 @@ TEST_CASE("copyArray()") {
|
||||
}
|
||||
|
||||
SECTION("int[][] -> JsonDocument") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
char json[32];
|
||||
int source[][3] = {{1, 2, 3}, {4, 5, 6}};
|
||||
|
||||
@ -158,9 +160,8 @@ TEST_CASE("copyArray()") {
|
||||
}
|
||||
|
||||
SECTION("int[][] -> JsonArray, but not enough memory") {
|
||||
const size_t SIZE =
|
||||
JSON_ARRAY_SIZE(2) + JSON_ARRAY_SIZE(3) + JSON_ARRAY_SIZE(2);
|
||||
StaticJsonDocument<SIZE> doc;
|
||||
const size_t SIZE = sizeofArray(2) + sizeofArray(3) + sizeofArray(2);
|
||||
JsonDocument doc(SIZE);
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
char json[32] = "";
|
||||
int source[][3] = {{1, 2, 3}, {4, 5, 6}};
|
||||
@ -176,7 +177,7 @@ TEST_CASE("copyArray()") {
|
||||
}
|
||||
|
||||
SECTION("JsonArray -> int[], with more space than needed") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
char json[] = "[1,2,3]";
|
||||
DeserializationError err = deserializeJson(doc, json);
|
||||
CHECK(err == DeserializationError::Ok);
|
||||
@ -193,7 +194,7 @@ TEST_CASE("copyArray()") {
|
||||
}
|
||||
|
||||
SECTION("JsonArray -> int[], without enough space") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
char json[] = "[1,2,3]";
|
||||
DeserializationError err = deserializeJson(doc, json);
|
||||
CHECK(err == DeserializationError::Ok);
|
||||
@ -208,7 +209,7 @@ TEST_CASE("copyArray()") {
|
||||
}
|
||||
|
||||
SECTION("JsonArray -> std::string[]") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
char json[] = "[\"a\",\"b\",\"c\"]";
|
||||
DeserializationError err = deserializeJson(doc, json);
|
||||
CHECK(err == DeserializationError::Ok);
|
||||
@ -225,7 +226,7 @@ TEST_CASE("copyArray()") {
|
||||
}
|
||||
|
||||
SECTION("JsonArray -> char[N][]") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
char json[] = "[\"a12345\",\"b123456\",\"c1234567\"]";
|
||||
DeserializationError err = deserializeJson(doc, json);
|
||||
CHECK(err == DeserializationError::Ok);
|
||||
@ -242,7 +243,7 @@ TEST_CASE("copyArray()") {
|
||||
}
|
||||
|
||||
SECTION("JsonDocument -> int[]") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
char json[] = "[1,2,3]";
|
||||
DeserializationError err = deserializeJson(doc, json);
|
||||
CHECK(err == DeserializationError::Ok);
|
||||
@ -258,7 +259,7 @@ TEST_CASE("copyArray()") {
|
||||
}
|
||||
|
||||
SECTION("MemberProxy -> int[]") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
char json[] = "{\"data\":[1,2,3]}";
|
||||
DeserializationError err = deserializeJson(doc, json);
|
||||
CHECK(err == DeserializationError::Ok);
|
||||
@ -274,7 +275,7 @@ TEST_CASE("copyArray()") {
|
||||
}
|
||||
|
||||
SECTION("ElementProxy -> int[]") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
char json[] = "[[1,2,3]]";
|
||||
DeserializationError err = deserializeJson(doc, json);
|
||||
CHECK(err == DeserializationError::Ok);
|
||||
@ -290,7 +291,7 @@ TEST_CASE("copyArray()") {
|
||||
}
|
||||
|
||||
SECTION("JsonArray -> int[][]") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
char json[] = "[[1,2],[3],[4]]";
|
||||
|
||||
DeserializationError err = deserializeJson(doc, json);
|
||||
@ -309,7 +310,7 @@ TEST_CASE("copyArray()") {
|
||||
}
|
||||
|
||||
SECTION("JsonDocument -> int[][]") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
char json[] = "[[1,2],[3],[4]]";
|
||||
|
||||
DeserializationError err = deserializeJson(doc, json);
|
||||
@ -327,7 +328,7 @@ TEST_CASE("copyArray()") {
|
||||
}
|
||||
|
||||
SECTION("MemberProxy -> int[][]") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
char json[] = "{\"data\":[[1,2],[3],[4]]}";
|
||||
|
||||
DeserializationError err = deserializeJson(doc, json);
|
||||
|
@ -6,7 +6,7 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonArray basics") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
|
||||
SECTION("CreateNestedArray") {
|
||||
|
@ -6,11 +6,11 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonArray::operator==()") {
|
||||
DynamicJsonDocument doc1(4096);
|
||||
JsonDocument doc1(4096);
|
||||
JsonArray array1 = doc1.to<JsonArray>();
|
||||
JsonArrayConst array1c = array1;
|
||||
|
||||
DynamicJsonDocument doc2(4096);
|
||||
JsonDocument doc2(4096);
|
||||
JsonArray array2 = doc2.to<JsonArray>();
|
||||
JsonArrayConst array2c = array2;
|
||||
|
||||
|
@ -12,7 +12,7 @@ TEST_CASE("JsonArray::isNull()") {
|
||||
}
|
||||
|
||||
SECTION("returns false") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
JsonArray arr = doc.to<JsonArray>();
|
||||
REQUIRE(arr.isNull() == false);
|
||||
}
|
||||
@ -25,7 +25,7 @@ TEST_CASE("JsonArrayConst::isNull()") {
|
||||
}
|
||||
|
||||
SECTION("returns false") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
JsonArrayConst arr = doc.to<JsonArray>();
|
||||
REQUIRE(arr.isNull() == false);
|
||||
}
|
||||
@ -38,7 +38,7 @@ TEST_CASE("JsonArray::operator bool()") {
|
||||
}
|
||||
|
||||
SECTION("returns true") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
JsonArray arr = doc.to<JsonArray>();
|
||||
REQUIRE(static_cast<bool>(arr) == true);
|
||||
}
|
||||
@ -51,7 +51,7 @@ TEST_CASE("JsonArrayConst::operator bool()") {
|
||||
}
|
||||
|
||||
SECTION("returns true") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
JsonArrayConst arr = doc.to<JsonArray>();
|
||||
REQUIRE(static_cast<bool>(arr) == true);
|
||||
}
|
||||
|
@ -5,9 +5,11 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
using ArduinoJson::detail::sizeofArray;
|
||||
|
||||
template <typename TArray>
|
||||
static void run_iterator_test() {
|
||||
StaticJsonDocument<JSON_ARRAY_SIZE(2)> doc;
|
||||
JsonDocument doc(sizeofArray(2));
|
||||
JsonArray tmp = doc.to<JsonArray>();
|
||||
tmp.add(12);
|
||||
tmp.add(34);
|
||||
|
@ -5,8 +5,12 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
using ArduinoJson::detail::sizeofArray;
|
||||
using ArduinoJson::detail::sizeofObject;
|
||||
using ArduinoJson::detail::sizeofString;
|
||||
|
||||
TEST_CASE("JsonArray::memoryUsage()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
JsonArray arr = doc.to<JsonArray>();
|
||||
|
||||
SECTION("return 0 if uninitialized") {
|
||||
@ -14,29 +18,29 @@ TEST_CASE("JsonArray::memoryUsage()") {
|
||||
REQUIRE(unitialized.memoryUsage() == 0);
|
||||
}
|
||||
|
||||
SECTION("JSON_ARRAY_SIZE(0) if empty") {
|
||||
REQUIRE(arr.memoryUsage() == JSON_ARRAY_SIZE(0));
|
||||
SECTION("sizeofArray(0) if empty") {
|
||||
REQUIRE(arr.memoryUsage() == sizeofArray(0));
|
||||
}
|
||||
|
||||
SECTION("JSON_ARRAY_SIZE(1) after add") {
|
||||
SECTION("sizeofArray(1) after add") {
|
||||
arr.add("hello");
|
||||
REQUIRE(arr.memoryUsage() == JSON_ARRAY_SIZE(1));
|
||||
REQUIRE(arr.memoryUsage() == sizeofArray(1));
|
||||
}
|
||||
|
||||
SECTION("includes the size of the string") {
|
||||
arr.add(std::string("hello"));
|
||||
REQUIRE(arr.memoryUsage() == JSON_ARRAY_SIZE(1) + 6);
|
||||
REQUIRE(arr.memoryUsage() == sizeofArray(1) + sizeofString(5));
|
||||
}
|
||||
|
||||
SECTION("includes the size of the nested array") {
|
||||
JsonArray nested = arr.createNestedArray();
|
||||
nested.add(42);
|
||||
REQUIRE(arr.memoryUsage() == 2 * JSON_ARRAY_SIZE(1));
|
||||
REQUIRE(arr.memoryUsage() == 2 * sizeofArray(1));
|
||||
}
|
||||
|
||||
SECTION("includes the size of the nested arrect") {
|
||||
JsonObject nested = arr.createNestedObject();
|
||||
nested["hello"] = "world";
|
||||
REQUIRE(arr.memoryUsage() == JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(1));
|
||||
REQUIRE(arr.memoryUsage() == sizeofObject(1) + sizeofArray(1));
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonArray::nesting()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
JsonArray arr = doc.to<JsonArray>();
|
||||
|
||||
SECTION("return 0 if uninitialized") {
|
||||
|
@ -6,7 +6,7 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonArray::remove()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
array.add(1);
|
||||
array.add(2);
|
||||
|
@ -6,7 +6,7 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonArray::size()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
|
||||
SECTION("returns 0 is empty") {
|
||||
|
@ -12,7 +12,7 @@ static void eraseString(std::string& str) {
|
||||
}
|
||||
|
||||
TEST_CASE("std::string") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
|
||||
SECTION("add()") {
|
||||
|
@ -6,8 +6,11 @@
|
||||
#include <stdint.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
using ArduinoJson::detail::sizeofArray;
|
||||
using ArduinoJson::detail::sizeofString;
|
||||
|
||||
TEST_CASE("JsonArray::operator[]") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
|
||||
SECTION("Pad with null") {
|
||||
@ -65,7 +68,7 @@ TEST_CASE("JsonArray::operator[]") {
|
||||
}
|
||||
|
||||
SECTION("nested array") {
|
||||
DynamicJsonDocument doc2(4096);
|
||||
JsonDocument doc2(4096);
|
||||
JsonArray arr2 = doc2.to<JsonArray>();
|
||||
|
||||
array[0] = arr2;
|
||||
@ -76,7 +79,7 @@ TEST_CASE("JsonArray::operator[]") {
|
||||
}
|
||||
|
||||
SECTION("nested object") {
|
||||
DynamicJsonDocument doc2(4096);
|
||||
JsonDocument doc2(4096);
|
||||
JsonObject obj = doc2.to<JsonObject>();
|
||||
|
||||
array[0] = obj;
|
||||
@ -87,7 +90,7 @@ TEST_CASE("JsonArray::operator[]") {
|
||||
}
|
||||
|
||||
SECTION("array subscript") {
|
||||
DynamicJsonDocument doc2(4096);
|
||||
JsonDocument doc2(4096);
|
||||
JsonArray arr2 = doc2.to<JsonArray>();
|
||||
const char* str = "hello";
|
||||
|
||||
@ -100,7 +103,7 @@ TEST_CASE("JsonArray::operator[]") {
|
||||
|
||||
SECTION("object subscript") {
|
||||
const char* str = "hello";
|
||||
DynamicJsonDocument doc2(4096);
|
||||
JsonDocument doc2(4096);
|
||||
JsonObject obj = doc2.to<JsonObject>();
|
||||
|
||||
obj["x"] = str;
|
||||
@ -112,19 +115,19 @@ TEST_CASE("JsonArray::operator[]") {
|
||||
|
||||
SECTION("should not duplicate const char*") {
|
||||
array[0] = "world";
|
||||
const size_t expectedSize = JSON_ARRAY_SIZE(1);
|
||||
const size_t expectedSize = sizeofArray(1);
|
||||
REQUIRE(expectedSize == doc.memoryUsage());
|
||||
}
|
||||
|
||||
SECTION("should duplicate char*") {
|
||||
array[0] = const_cast<char*>("world");
|
||||
const size_t expectedSize = JSON_ARRAY_SIZE(1) + JSON_STRING_SIZE(5);
|
||||
const size_t expectedSize = sizeofArray(1) + sizeofString(5);
|
||||
REQUIRE(expectedSize == doc.memoryUsage());
|
||||
}
|
||||
|
||||
SECTION("should duplicate std::string") {
|
||||
array[0] = std::string("world");
|
||||
const size_t expectedSize = JSON_ARRAY_SIZE(1) + JSON_STRING_SIZE(5);
|
||||
const size_t expectedSize = sizeofArray(1) + sizeofString(5);
|
||||
REQUIRE(expectedSize == doc.memoryUsage());
|
||||
}
|
||||
|
||||
@ -159,7 +162,7 @@ TEST_CASE("JsonArray::operator[]") {
|
||||
}
|
||||
|
||||
TEST_CASE("JsonArrayConst::operator[]") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
array.add(0);
|
||||
|
||||
|
@ -4,7 +4,6 @@
|
||||
|
||||
add_executable(JsonDeserializerTests
|
||||
array.cpp
|
||||
array_static.cpp
|
||||
DeserializationError.cpp
|
||||
filter.cpp
|
||||
incomplete_input.cpp
|
||||
@ -14,7 +13,6 @@ add_executable(JsonDeserializerTests
|
||||
nestingLimit.cpp
|
||||
number.cpp
|
||||
object.cpp
|
||||
object_static.cpp
|
||||
string.cpp
|
||||
)
|
||||
|
||||
|
@ -5,8 +5,12 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
using ArduinoJson::detail::sizeofArray;
|
||||
using ArduinoJson::detail::sizeofObject;
|
||||
using ArduinoJson::detail::sizeofString;
|
||||
|
||||
TEST_CASE("deserialize JSON array") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
|
||||
SECTION("An empty array") {
|
||||
DeserializationError err = deserializeJson(doc, "[]");
|
||||
@ -248,6 +252,87 @@ TEST_CASE("deserialize JSON array") {
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(arr.size() == 0);
|
||||
REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(0));
|
||||
REQUIRE(doc.memoryUsage() == sizeofArray(0));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("deserialize JSON array under memory constraints") {
|
||||
SECTION("buffer of the right size for an empty array") {
|
||||
JsonDocument doc(sizeofArray(0));
|
||||
char input[] = "[]";
|
||||
|
||||
DeserializationError err = deserializeJson(doc, input);
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
}
|
||||
|
||||
SECTION("buffer too small for an array with one element") {
|
||||
JsonDocument doc(sizeofArray(0));
|
||||
char input[] = "[1]";
|
||||
|
||||
DeserializationError err = deserializeJson(doc, input);
|
||||
|
||||
REQUIRE(err == DeserializationError::NoMemory);
|
||||
}
|
||||
|
||||
SECTION("buffer of the right size for an array with one element") {
|
||||
JsonDocument doc(sizeofArray(1));
|
||||
char input[] = "[1]";
|
||||
|
||||
DeserializationError err = deserializeJson(doc, input);
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
}
|
||||
|
||||
SECTION("buffer too small for an array with a nested object") {
|
||||
JsonDocument doc(sizeofArray(0) + sizeofObject(0));
|
||||
char input[] = "[{}]";
|
||||
|
||||
DeserializationError err = deserializeJson(doc, input);
|
||||
|
||||
REQUIRE(err == DeserializationError::NoMemory);
|
||||
}
|
||||
|
||||
SECTION("buffer of the right size for an array with a nested object") {
|
||||
JsonDocument doc(sizeofArray(1) + sizeofObject(0));
|
||||
char input[] = "[{}]";
|
||||
|
||||
DeserializationError err = deserializeJson(doc, input);
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
}
|
||||
|
||||
SECTION("don't store space characters") {
|
||||
JsonDocument doc(100);
|
||||
|
||||
deserializeJson(doc, " [ \"1234567\" ] ");
|
||||
|
||||
REQUIRE(sizeofArray(1) + sizeofString(7) == doc.memoryUsage());
|
||||
}
|
||||
|
||||
SECTION("Should clear the JsonArray") {
|
||||
JsonDocument doc(sizeofArray(4));
|
||||
char input[] = "[1,2,3,4]";
|
||||
|
||||
deserializeJson(doc, input);
|
||||
deserializeJson(doc, "[]");
|
||||
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
REQUIRE(arr.size() == 0);
|
||||
REQUIRE(doc.memoryUsage() == sizeofArray(0));
|
||||
}
|
||||
|
||||
SECTION("buffer of the right size for an array with two element") {
|
||||
JsonDocument doc(sizeofArray(2));
|
||||
char input[] = "[1,2]";
|
||||
|
||||
DeserializationError err = deserializeJson(doc, input);
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<JsonArray>());
|
||||
REQUIRE(doc.memoryUsage() == sizeofArray(2));
|
||||
REQUIRE(arr[0] == 1);
|
||||
REQUIRE(arr[1] == 2);
|
||||
}
|
||||
}
|
||||
|
@ -1,89 +0,0 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("deserialize JSON array with a StaticJsonDocument") {
|
||||
SECTION("BufferOfTheRightSizeForEmptyArray") {
|
||||
StaticJsonDocument<JSON_ARRAY_SIZE(0)> doc;
|
||||
char input[] = "[]";
|
||||
|
||||
DeserializationError err = deserializeJson(doc, input);
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
}
|
||||
|
||||
SECTION("TooSmallBufferForArrayWithOneValue") {
|
||||
StaticJsonDocument<JSON_ARRAY_SIZE(0)> doc;
|
||||
char input[] = "[1]";
|
||||
|
||||
DeserializationError err = deserializeJson(doc, input);
|
||||
|
||||
REQUIRE(err == DeserializationError::NoMemory);
|
||||
}
|
||||
|
||||
SECTION("BufferOfTheRightSizeForArrayWithOneValue") {
|
||||
StaticJsonDocument<JSON_ARRAY_SIZE(1)> doc;
|
||||
char input[] = "[1]";
|
||||
|
||||
DeserializationError err = deserializeJson(doc, input);
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
}
|
||||
|
||||
SECTION("TooSmallBufferForArrayWithNestedObject") {
|
||||
StaticJsonDocument<JSON_ARRAY_SIZE(0) + JSON_OBJECT_SIZE(0)> doc;
|
||||
char input[] = "[{}]";
|
||||
|
||||
DeserializationError err = deserializeJson(doc, input);
|
||||
|
||||
REQUIRE(err == DeserializationError::NoMemory);
|
||||
}
|
||||
|
||||
SECTION("BufferOfTheRightSizeForArrayWithNestedObject") {
|
||||
StaticJsonDocument<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0)> doc;
|
||||
char input[] = "[{}]";
|
||||
|
||||
DeserializationError err = deserializeJson(doc, input);
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
}
|
||||
|
||||
SECTION("CopyStringNotSpaces") {
|
||||
StaticJsonDocument<100> doc;
|
||||
|
||||
deserializeJson(doc, " [ \"1234567\" ] ");
|
||||
|
||||
REQUIRE(JSON_ARRAY_SIZE(1) + JSON_STRING_SIZE(7) == doc.memoryUsage());
|
||||
// note: we use a string of 8 bytes to be sure that the StaticMemoryPool
|
||||
// will not insert bytes to enforce alignement
|
||||
}
|
||||
|
||||
SECTION("Should clear the JsonArray") {
|
||||
StaticJsonDocument<JSON_ARRAY_SIZE(4)> doc;
|
||||
char input[] = "[1,2,3,4]";
|
||||
|
||||
deserializeJson(doc, input);
|
||||
deserializeJson(doc, "[]");
|
||||
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
REQUIRE(arr.size() == 0);
|
||||
REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(0));
|
||||
}
|
||||
|
||||
SECTION("Array") {
|
||||
StaticJsonDocument<JSON_ARRAY_SIZE(2)> doc;
|
||||
char input[] = "[1,2]";
|
||||
|
||||
DeserializationError err = deserializeJson(doc, input);
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<JsonArray>());
|
||||
REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(2));
|
||||
REQUIRE(arr[0] == 1);
|
||||
REQUIRE(arr[1] == 2);
|
||||
}
|
||||
}
|
@ -9,6 +9,10 @@
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
using ArduinoJson::detail::sizeofArray;
|
||||
using ArduinoJson::detail::sizeofObject;
|
||||
using ArduinoJson::detail::sizeofString;
|
||||
|
||||
TEST_CASE("Filtering") {
|
||||
struct TestCase {
|
||||
const char* input;
|
||||
@ -43,7 +47,7 @@ TEST_CASE("Filtering") {
|
||||
10,
|
||||
DeserializationError::Ok,
|
||||
"{\"abcdefg\":\"hijklmn\"}",
|
||||
JSON_OBJECT_SIZE(1) + 16
|
||||
sizeofObject(1) + 2*sizeofString(7)
|
||||
},
|
||||
{
|
||||
"{\"hello\":\"world\"}",
|
||||
@ -51,7 +55,7 @@ TEST_CASE("Filtering") {
|
||||
10,
|
||||
DeserializationError::Ok,
|
||||
"{}",
|
||||
JSON_OBJECT_SIZE(0)
|
||||
sizeofObject(0)
|
||||
},
|
||||
{
|
||||
// Input in an object, but filter wants an array
|
||||
@ -69,7 +73,7 @@ TEST_CASE("Filtering") {
|
||||
10,
|
||||
DeserializationError::Ok,
|
||||
"{\"example\":null}",
|
||||
JSON_OBJECT_SIZE(1) + 8
|
||||
sizeofObject(1) + sizeofString(7)
|
||||
},
|
||||
{
|
||||
// Member is a number, but filter wants an array
|
||||
@ -78,7 +82,7 @@ TEST_CASE("Filtering") {
|
||||
10,
|
||||
DeserializationError::Ok,
|
||||
"{\"example\":null}",
|
||||
JSON_OBJECT_SIZE(1) + 8
|
||||
sizeofObject(1) + sizeofString(7)
|
||||
},
|
||||
{
|
||||
// Input is an array, but filter wants an object
|
||||
@ -114,7 +118,7 @@ TEST_CASE("Filtering") {
|
||||
10,
|
||||
DeserializationError::Ok,
|
||||
"{\"example\":42}",
|
||||
JSON_OBJECT_SIZE(1) + 8
|
||||
sizeofObject(1) + sizeofString(7)
|
||||
},
|
||||
{
|
||||
// skip a float
|
||||
@ -123,7 +127,7 @@ TEST_CASE("Filtering") {
|
||||
10,
|
||||
DeserializationError::Ok,
|
||||
"{\"example\":42}",
|
||||
JSON_OBJECT_SIZE(1) + 8
|
||||
sizeofObject(1) + sizeofString(7)
|
||||
},
|
||||
{
|
||||
// skip false
|
||||
@ -132,7 +136,7 @@ TEST_CASE("Filtering") {
|
||||
10,
|
||||
DeserializationError::Ok,
|
||||
"{\"example\":42}",
|
||||
JSON_OBJECT_SIZE(1) + 8
|
||||
sizeofObject(1) + sizeofString(7)
|
||||
},
|
||||
{
|
||||
// skip true
|
||||
@ -141,7 +145,7 @@ TEST_CASE("Filtering") {
|
||||
10,
|
||||
DeserializationError::Ok,
|
||||
"{\"example\":42}",
|
||||
JSON_OBJECT_SIZE(1) + 8
|
||||
sizeofObject(1) + sizeofString(7)
|
||||
},
|
||||
{
|
||||
// skip null
|
||||
@ -150,7 +154,7 @@ TEST_CASE("Filtering") {
|
||||
10,
|
||||
DeserializationError::Ok,
|
||||
"{\"example\":42}",
|
||||
JSON_OBJECT_SIZE(1) + 8
|
||||
sizeofObject(1) + sizeofString(7)
|
||||
},
|
||||
{
|
||||
// can skip a double-quoted string
|
||||
@ -159,7 +163,7 @@ TEST_CASE("Filtering") {
|
||||
10,
|
||||
DeserializationError::Ok,
|
||||
"{\"example\":42}",
|
||||
JSON_OBJECT_SIZE(1) + 8
|
||||
sizeofObject(1) + sizeofString(7)
|
||||
},
|
||||
{
|
||||
// can skip a single-quoted string
|
||||
@ -168,7 +172,7 @@ TEST_CASE("Filtering") {
|
||||
10,
|
||||
DeserializationError::Ok,
|
||||
"{\"example\":42}",
|
||||
JSON_OBJECT_SIZE(1) + 8
|
||||
sizeofObject(1) + sizeofString(7)
|
||||
},
|
||||
{
|
||||
// can skip an empty array
|
||||
@ -177,7 +181,7 @@ TEST_CASE("Filtering") {
|
||||
10,
|
||||
DeserializationError::Ok,
|
||||
"{\"example\":42}",
|
||||
JSON_OBJECT_SIZE(1) + 8
|
||||
sizeofObject(1) + sizeofString(7)
|
||||
},
|
||||
{
|
||||
// can skip an empty array with spaces in it
|
||||
@ -186,7 +190,7 @@ TEST_CASE("Filtering") {
|
||||
10,
|
||||
DeserializationError::Ok,
|
||||
"{\"example\":42}",
|
||||
JSON_OBJECT_SIZE(1) + 8
|
||||
sizeofObject(1) + sizeofString(7)
|
||||
},
|
||||
{
|
||||
// can skip an array
|
||||
@ -195,7 +199,7 @@ TEST_CASE("Filtering") {
|
||||
10,
|
||||
DeserializationError::Ok,
|
||||
"{\"example\":42}",
|
||||
JSON_OBJECT_SIZE(1) + 8
|
||||
sizeofObject(1) + sizeofString(7)
|
||||
},
|
||||
{
|
||||
// can skip an array with spaces in it
|
||||
@ -204,7 +208,7 @@ TEST_CASE("Filtering") {
|
||||
10,
|
||||
DeserializationError::Ok,
|
||||
"{\"example\":42}",
|
||||
JSON_OBJECT_SIZE(1) + 8
|
||||
sizeofObject(1) + sizeofString(7)
|
||||
},
|
||||
{
|
||||
// can skip an empty object
|
||||
@ -213,7 +217,7 @@ TEST_CASE("Filtering") {
|
||||
10,
|
||||
DeserializationError::Ok,
|
||||
"{\"example\":42}",
|
||||
JSON_OBJECT_SIZE(1) + 8
|
||||
sizeofObject(1) + sizeofString(7)
|
||||
},
|
||||
{
|
||||
// can skip an empty object with spaces in it
|
||||
@ -222,7 +226,7 @@ TEST_CASE("Filtering") {
|
||||
10,
|
||||
DeserializationError::Ok,
|
||||
"{\"example\":42}",
|
||||
JSON_OBJECT_SIZE(1) + 8
|
||||
sizeofObject(1) + sizeofString(7)
|
||||
},
|
||||
{
|
||||
// can skip an object
|
||||
@ -231,7 +235,7 @@ TEST_CASE("Filtering") {
|
||||
10,
|
||||
DeserializationError::Ok,
|
||||
"{\"example\":42}",
|
||||
JSON_OBJECT_SIZE(1) + 8
|
||||
sizeofObject(1) + sizeofString(7)
|
||||
},
|
||||
{
|
||||
// skip an object with spaces in it
|
||||
@ -240,7 +244,7 @@ TEST_CASE("Filtering") {
|
||||
10,
|
||||
DeserializationError::Ok,
|
||||
"{\"example\":42}",
|
||||
JSON_OBJECT_SIZE(1) + 8
|
||||
sizeofObject(1) + sizeofString(7)
|
||||
},
|
||||
{
|
||||
"{\"an_integer\": 0,\"example\":{\"type\":\"int\",\"outcome\":42}}",
|
||||
@ -248,7 +252,7 @@ TEST_CASE("Filtering") {
|
||||
10,
|
||||
DeserializationError::Ok,
|
||||
"{\"example\":{\"outcome\":42}}",
|
||||
2 * JSON_OBJECT_SIZE(1) + 16
|
||||
2 * sizeofObject(1) + 2*sizeofString(7)
|
||||
},
|
||||
{
|
||||
// wildcard
|
||||
@ -257,7 +261,7 @@ TEST_CASE("Filtering") {
|
||||
10,
|
||||
DeserializationError::Ok,
|
||||
"{\"example\":{\"outcome\":42}}",
|
||||
2 * JSON_OBJECT_SIZE(1) + 16
|
||||
2 * sizeofObject(1) + 2*sizeofString(7)
|
||||
},
|
||||
{
|
||||
// exclusion filter (issue #1628)
|
||||
@ -266,7 +270,7 @@ TEST_CASE("Filtering") {
|
||||
10,
|
||||
DeserializationError::Ok,
|
||||
"{\"example\":1}",
|
||||
JSON_OBJECT_SIZE(1) + 8
|
||||
sizeofObject(1) + sizeofString(7)
|
||||
},
|
||||
{
|
||||
// only the first element of array counts
|
||||
@ -275,7 +279,7 @@ TEST_CASE("Filtering") {
|
||||
10,
|
||||
DeserializationError::Ok,
|
||||
"[1,2,3]",
|
||||
JSON_ARRAY_SIZE(3)
|
||||
sizeofArray(3)
|
||||
},
|
||||
{
|
||||
// only the first element of array counts
|
||||
@ -284,7 +288,7 @@ TEST_CASE("Filtering") {
|
||||
10,
|
||||
DeserializationError::Ok,
|
||||
"[]",
|
||||
JSON_ARRAY_SIZE(0)
|
||||
sizeofArray(0)
|
||||
},
|
||||
{
|
||||
// filter members of object in array
|
||||
@ -293,7 +297,7 @@ TEST_CASE("Filtering") {
|
||||
10,
|
||||
DeserializationError::Ok,
|
||||
"[{\"example\":1},{\"example\":3}]",
|
||||
JSON_ARRAY_SIZE(2) + 2 * JSON_OBJECT_SIZE(1) + 8
|
||||
sizeofArray(2) + 2 * sizeofObject(1) + sizeofString(7)
|
||||
},
|
||||
{
|
||||
"[',2,3]",
|
||||
@ -301,7 +305,7 @@ TEST_CASE("Filtering") {
|
||||
10,
|
||||
DeserializationError::IncompleteInput,
|
||||
"[]",
|
||||
JSON_ARRAY_SIZE(0)
|
||||
sizeofArray(0)
|
||||
},
|
||||
{
|
||||
"[\",2,3]",
|
||||
@ -309,7 +313,7 @@ TEST_CASE("Filtering") {
|
||||
10,
|
||||
DeserializationError::IncompleteInput,
|
||||
"[]",
|
||||
JSON_ARRAY_SIZE(0)
|
||||
sizeofArray(0)
|
||||
},
|
||||
{
|
||||
// detect errors in skipped value
|
||||
@ -318,7 +322,7 @@ TEST_CASE("Filtering") {
|
||||
10,
|
||||
DeserializationError::InvalidInput,
|
||||
"[]",
|
||||
JSON_ARRAY_SIZE(0)
|
||||
sizeofArray(0)
|
||||
},
|
||||
{
|
||||
// detect incomplete string event if it's skipped
|
||||
@ -471,7 +475,7 @@ TEST_CASE("Filtering") {
|
||||
1,
|
||||
DeserializationError::TooDeep,
|
||||
"{}",
|
||||
JSON_OBJECT_SIZE(0)
|
||||
sizeofObject(0)
|
||||
},
|
||||
{
|
||||
// check nesting limit even for ignored arrays
|
||||
@ -498,7 +502,7 @@ TEST_CASE("Filtering") {
|
||||
1,
|
||||
DeserializationError::TooDeep,
|
||||
"[]",
|
||||
JSON_ARRAY_SIZE(0)
|
||||
sizeofArray(0)
|
||||
},
|
||||
{
|
||||
// supports back-slash at the end of skipped string
|
||||
@ -543,7 +547,7 @@ TEST_CASE("Filtering") {
|
||||
10,
|
||||
DeserializationError::InvalidInput,
|
||||
"[]",
|
||||
JSON_ARRAY_SIZE(0)
|
||||
sizeofArray(0)
|
||||
},
|
||||
{
|
||||
// incomplete comment at the begining of an array
|
||||
@ -552,7 +556,7 @@ TEST_CASE("Filtering") {
|
||||
10,
|
||||
DeserializationError::IncompleteInput,
|
||||
"[]",
|
||||
JSON_ARRAY_SIZE(0)
|
||||
sizeofArray(0)
|
||||
},
|
||||
{
|
||||
// invalid comment before key
|
||||
@ -561,7 +565,7 @@ TEST_CASE("Filtering") {
|
||||
10,
|
||||
DeserializationError::InvalidInput,
|
||||
"{}",
|
||||
JSON_OBJECT_SIZE(0)
|
||||
sizeofObject(0)
|
||||
},
|
||||
{
|
||||
// incomplete comment before key
|
||||
@ -570,7 +574,7 @@ TEST_CASE("Filtering") {
|
||||
10,
|
||||
DeserializationError::IncompleteInput,
|
||||
"{}",
|
||||
JSON_OBJECT_SIZE(0)
|
||||
sizeofObject(0)
|
||||
},
|
||||
{
|
||||
// invalid comment after key
|
||||
@ -579,7 +583,7 @@ TEST_CASE("Filtering") {
|
||||
10,
|
||||
DeserializationError::InvalidInput,
|
||||
"{}",
|
||||
JSON_OBJECT_SIZE(0)
|
||||
sizeofObject(0)
|
||||
},
|
||||
{
|
||||
// incomplete comment after key
|
||||
@ -588,7 +592,7 @@ TEST_CASE("Filtering") {
|
||||
10,
|
||||
DeserializationError::IncompleteInput,
|
||||
"{}",
|
||||
JSON_OBJECT_SIZE(0)
|
||||
sizeofObject(0)
|
||||
},
|
||||
{
|
||||
// invalid comment after colon
|
||||
@ -597,7 +601,7 @@ TEST_CASE("Filtering") {
|
||||
10,
|
||||
DeserializationError::InvalidInput,
|
||||
"{}",
|
||||
JSON_OBJECT_SIZE(0)
|
||||
sizeofObject(0)
|
||||
},
|
||||
{
|
||||
// incomplete comment after colon
|
||||
@ -606,7 +610,7 @@ TEST_CASE("Filtering") {
|
||||
10,
|
||||
DeserializationError::IncompleteInput,
|
||||
"{}",
|
||||
JSON_OBJECT_SIZE(0)
|
||||
sizeofObject(0)
|
||||
},
|
||||
{
|
||||
// comment next to an integer
|
||||
@ -615,7 +619,7 @@ TEST_CASE("Filtering") {
|
||||
10,
|
||||
DeserializationError::Ok,
|
||||
"{}",
|
||||
JSON_OBJECT_SIZE(0)
|
||||
sizeofObject(0)
|
||||
},
|
||||
{
|
||||
// invalid comment after opening brace of a skipped object
|
||||
@ -685,8 +689,8 @@ TEST_CASE("Filtering") {
|
||||
for (size_t i = 0; i < sizeof(testCases) / sizeof(testCases[0]); i++) {
|
||||
CAPTURE(i);
|
||||
|
||||
DynamicJsonDocument filter(256);
|
||||
DynamicJsonDocument doc(256);
|
||||
JsonDocument filter(256);
|
||||
JsonDocument doc(256);
|
||||
TestCase& tc = testCases[i];
|
||||
|
||||
CAPTURE(tc.filter);
|
||||
@ -706,10 +710,10 @@ TEST_CASE("Filtering") {
|
||||
TEST_CASE("Zero-copy mode") { // issue #1697
|
||||
char input[] = "{\"include\":42,\"exclude\":666}";
|
||||
|
||||
StaticJsonDocument<256> filter;
|
||||
JsonDocument filter(256);
|
||||
filter["include"] = true;
|
||||
|
||||
StaticJsonDocument<256> doc;
|
||||
JsonDocument doc(256);
|
||||
DeserializationError err =
|
||||
deserializeJson(doc, input, DeserializationOption::Filter(filter));
|
||||
|
||||
@ -718,8 +722,8 @@ TEST_CASE("Zero-copy mode") { // issue #1697
|
||||
}
|
||||
|
||||
TEST_CASE("Overloads") {
|
||||
StaticJsonDocument<256> doc;
|
||||
StaticJsonDocument<256> filter;
|
||||
JsonDocument doc(256);
|
||||
JsonDocument filter(256);
|
||||
|
||||
using namespace DeserializationOption;
|
||||
|
||||
|
@ -18,7 +18,7 @@ TEST_CASE("Truncated JSON input") {
|
||||
"{", "{a", "{a:", "{a:1", "{a:1,", "{a:1,"};
|
||||
const size_t testCount = sizeof(testCases) / sizeof(testCases[0]);
|
||||
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
|
||||
for (size_t i = 0; i < testCount; i++) {
|
||||
const char* input = testCases[i];
|
||||
|
@ -9,23 +9,46 @@
|
||||
|
||||
#include "CustomReader.hpp"
|
||||
|
||||
using ArduinoJson::detail::sizeofObject;
|
||||
using ArduinoJson::detail::sizeofString;
|
||||
|
||||
TEST_CASE("deserializeJson(char*)") {
|
||||
StaticJsonDocument<1024> doc;
|
||||
JsonDocument doc(1024);
|
||||
|
||||
SECTION("should not duplicate strings") {
|
||||
char input[] = "{\"hello\":\"world\"}";
|
||||
char input[] = "{\"hello\":\"world\"}";
|
||||
|
||||
DeserializationError err = deserializeJson(doc, input);
|
||||
DeserializationError err = deserializeJson(doc, input);
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
CHECK(doc.memoryUsage() == JSON_OBJECT_SIZE(1));
|
||||
CHECK(doc.as<JsonVariant>().memoryUsage() ==
|
||||
JSON_OBJECT_SIZE(1)); // issue #1318
|
||||
}
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
CHECK(doc.memoryUsage() == sizeofObject(1) + 2 * sizeofString(5));
|
||||
}
|
||||
|
||||
TEST_CASE("deserializeJson(unsigned char*, unsigned int)") { // issue #1897
|
||||
JsonDocument doc(1024);
|
||||
|
||||
unsigned char input[] = "{\"hello\":\"world\"}";
|
||||
unsigned char* input_ptr = input;
|
||||
unsigned int size = sizeof(input);
|
||||
|
||||
DeserializationError err = deserializeJson(doc, input_ptr, size);
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
}
|
||||
|
||||
TEST_CASE("deserializeJson(uint8_t*, size_t)") { // issue #1898
|
||||
JsonDocument doc(1024);
|
||||
|
||||
uint8_t input[] = "{\"hello\":\"world\"}";
|
||||
uint8_t* input_ptr = input;
|
||||
size_t size = sizeof(input);
|
||||
|
||||
DeserializationError err = deserializeJson(doc, input_ptr, size);
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
}
|
||||
|
||||
TEST_CASE("deserializeJson(const std::string&)") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
|
||||
SECTION("should accept const string") {
|
||||
const std::string input("[42]");
|
||||
@ -54,7 +77,7 @@ TEST_CASE("deserializeJson(const std::string&)") {
|
||||
}
|
||||
|
||||
TEST_CASE("deserializeJson(std::istream&)") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
|
||||
SECTION("array") {
|
||||
std::istringstream json(" [ 42 ] ");
|
||||
@ -125,7 +148,7 @@ TEST_CASE("deserializeJson(VLA)") {
|
||||
char vla[i];
|
||||
strcpy(vla, "{\"a\":42}");
|
||||
|
||||
StaticJsonDocument<JSON_OBJECT_SIZE(1)> doc;
|
||||
JsonDocument doc(sizeofObject(1));
|
||||
DeserializationError err = deserializeJson(doc, vla);
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
@ -133,7 +156,7 @@ TEST_CASE("deserializeJson(VLA)") {
|
||||
#endif
|
||||
|
||||
TEST_CASE("deserializeJson(CustomReader)") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
CustomReader reader("[4,2]");
|
||||
DeserializationError err = deserializeJson(doc, reader);
|
||||
|
||||
@ -144,10 +167,10 @@ TEST_CASE("deserializeJson(CustomReader)") {
|
||||
}
|
||||
|
||||
TEST_CASE("deserializeJson(JsonDocument&, MemberProxy)") {
|
||||
DynamicJsonDocument doc1(4096);
|
||||
JsonDocument doc1(4096);
|
||||
doc1["payload"] = "[4,2]";
|
||||
|
||||
DynamicJsonDocument doc2(4096);
|
||||
JsonDocument doc2(4096);
|
||||
DeserializationError err = deserializeJson(doc2, doc1["payload"]);
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
@ -157,10 +180,10 @@ TEST_CASE("deserializeJson(JsonDocument&, MemberProxy)") {
|
||||
}
|
||||
|
||||
TEST_CASE("deserializeJson(JsonDocument&, JsonVariant)") {
|
||||
DynamicJsonDocument doc1(4096);
|
||||
JsonDocument doc1(4096);
|
||||
doc1["payload"] = "[4,2]";
|
||||
|
||||
DynamicJsonDocument doc2(4096);
|
||||
JsonDocument doc2(4096);
|
||||
DeserializationError err =
|
||||
deserializeJson(doc2, doc1["payload"].as<JsonVariant>());
|
||||
|
||||
@ -171,10 +194,10 @@ TEST_CASE("deserializeJson(JsonDocument&, JsonVariant)") {
|
||||
}
|
||||
|
||||
TEST_CASE("deserializeJson(JsonDocument&, JsonVariantConst)") {
|
||||
DynamicJsonDocument doc1(4096);
|
||||
JsonDocument doc1(4096);
|
||||
doc1["payload"] = "[4,2]";
|
||||
|
||||
DynamicJsonDocument doc2(4096);
|
||||
JsonDocument doc2(4096);
|
||||
DeserializationError err =
|
||||
deserializeJson(doc2, doc1["payload"].as<JsonVariantConst>());
|
||||
|
||||
@ -185,10 +208,10 @@ TEST_CASE("deserializeJson(JsonDocument&, JsonVariantConst)") {
|
||||
}
|
||||
|
||||
TEST_CASE("deserializeJson(JsonDocument&, ElementProxy)") {
|
||||
DynamicJsonDocument doc1(4096);
|
||||
JsonDocument doc1(4096);
|
||||
doc1[0] = "[4,2]";
|
||||
|
||||
DynamicJsonDocument doc2(4096);
|
||||
JsonDocument doc2(4096);
|
||||
DeserializationError err = deserializeJson(doc2, doc1[0]);
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
|
@ -13,7 +13,7 @@ TEST_CASE("Invalid JSON input") {
|
||||
"3}"};
|
||||
const size_t testCount = sizeof(testCases) / sizeof(testCases[0]);
|
||||
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
|
||||
for (size_t i = 0; i < testCount; i++) {
|
||||
const char* input = testCases[i];
|
||||
@ -30,7 +30,7 @@ TEST_CASE("Invalid JSON input that should pass") {
|
||||
};
|
||||
const size_t testCount = sizeof(testCases) / sizeof(testCases[0]);
|
||||
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
|
||||
for (size_t i = 0; i < testCount; i++) {
|
||||
const char* input = testCases[i];
|
||||
|
@ -7,8 +7,10 @@
|
||||
|
||||
using namespace Catch::Matchers;
|
||||
|
||||
TEST_CASE("deserializeJson(DynamicJsonDocument&)") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
using ArduinoJson::detail::sizeofObject;
|
||||
|
||||
TEST_CASE("deserializeJson(JsonDocument&)") {
|
||||
JsonDocument doc(4096);
|
||||
|
||||
SECTION("Edge cases") {
|
||||
SECTION("null char*") {
|
||||
@ -112,6 +114,6 @@ TEST_CASE("deserializeJson(DynamicJsonDocument&)") {
|
||||
deserializeJson(doc, "{}");
|
||||
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(0));
|
||||
REQUIRE(doc.memoryUsage() == sizeofObject(0));
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,7 @@
|
||||
REQUIRE(DeserializationError::TooDeep == expression);
|
||||
|
||||
TEST_CASE("JsonDeserializer nesting") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
|
||||
SECTION("Input = const char*") {
|
||||
SECTION("limit = 0") {
|
||||
|
@ -16,7 +16,7 @@ using ArduinoJson::detail::isnan;
|
||||
} // namespace my
|
||||
|
||||
TEST_CASE("deserialize an integer") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
|
||||
SECTION("Integer") {
|
||||
SECTION("0") {
|
||||
|
@ -5,8 +5,12 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
using ArduinoJson::detail::sizeofArray;
|
||||
using ArduinoJson::detail::sizeofObject;
|
||||
using ArduinoJson::detail::sizeofString;
|
||||
|
||||
TEST_CASE("deserialize JSON object") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
|
||||
SECTION("An empty object") {
|
||||
DeserializationError err = deserializeJson(doc, "{}");
|
||||
@ -278,6 +282,7 @@ TEST_CASE("deserialize JSON object") {
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc["a"] == 2);
|
||||
REQUIRE(doc.memoryUsage() == 3 * sizeofObject(1) + sizeofString(1));
|
||||
}
|
||||
|
||||
SECTION("Repeated key with zero copy mode") { // issue #1697
|
||||
@ -304,7 +309,7 @@ TEST_CASE("deserialize JSON object") {
|
||||
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
REQUIRE(obj.size() == 0);
|
||||
REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(0));
|
||||
REQUIRE(doc.memoryUsage() == sizeofObject(0));
|
||||
}
|
||||
|
||||
SECTION("Issue #1335") {
|
||||
@ -313,3 +318,61 @@ TEST_CASE("deserialize JSON object") {
|
||||
CHECK(doc.as<std::string>() == json);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("deserialize JSON object under memory constraints") {
|
||||
SECTION("buffer for the right size for an empty object") {
|
||||
JsonDocument doc(sizeofObject(0));
|
||||
char input[] = "{}";
|
||||
|
||||
DeserializationError err = deserializeJson(doc, input);
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
}
|
||||
|
||||
SECTION("buffer too small for an empty object") {
|
||||
JsonDocument doc(sizeofObject(0));
|
||||
char input[] = "{\"a\":1}";
|
||||
|
||||
DeserializationError err = deserializeJson(doc, input);
|
||||
|
||||
REQUIRE(err == DeserializationError::NoMemory);
|
||||
}
|
||||
|
||||
SECTION("buffer of the right size for an object with one member") {
|
||||
JsonDocument doc(sizeofObject(1));
|
||||
char input[] = "{\"a\":1}";
|
||||
|
||||
DeserializationError err = deserializeJson(doc, input);
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
}
|
||||
|
||||
SECTION("buffer too small for an object with a nested array") {
|
||||
JsonDocument doc(sizeofObject(0) + sizeofArray(0));
|
||||
char input[] = "{\"a\":[]}";
|
||||
|
||||
DeserializationError err = deserializeJson(doc, input);
|
||||
|
||||
REQUIRE(err == DeserializationError::NoMemory);
|
||||
}
|
||||
|
||||
SECTION("buffer of the right size for an object with a nested array") {
|
||||
JsonDocument doc(sizeofObject(1) + sizeofArray(0));
|
||||
char input[] = "{\"a\":[]}";
|
||||
|
||||
DeserializationError err = deserializeJson(doc, input);
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
}
|
||||
|
||||
SECTION("Should clear the JsonObject") {
|
||||
JsonDocument doc(sizeofObject(1));
|
||||
char input[] = "{\"hello\":\"world\"}";
|
||||
|
||||
deserializeJson(doc, input);
|
||||
deserializeJson(doc, "{}");
|
||||
|
||||
REQUIRE(doc.as<JsonObject>().size() == 0);
|
||||
REQUIRE(doc.memoryUsage() == sizeofObject(0));
|
||||
}
|
||||
}
|
||||
|
@ -1,64 +0,0 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("deserialize JSON object with StaticJsonDocument") {
|
||||
SECTION("BufferOfTheRightSizeForEmptyObject") {
|
||||
StaticJsonDocument<JSON_OBJECT_SIZE(0)> doc;
|
||||
char input[] = "{}";
|
||||
|
||||
DeserializationError err = deserializeJson(doc, input);
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
}
|
||||
|
||||
SECTION("TooSmallBufferForObjectWithOneValue") {
|
||||
StaticJsonDocument<JSON_OBJECT_SIZE(0)> doc;
|
||||
char input[] = "{\"a\":1}";
|
||||
|
||||
DeserializationError err = deserializeJson(doc, input);
|
||||
|
||||
REQUIRE(err == DeserializationError::NoMemory);
|
||||
}
|
||||
|
||||
SECTION("BufferOfTheRightSizeForObjectWithOneValue") {
|
||||
StaticJsonDocument<JSON_OBJECT_SIZE(1)> doc;
|
||||
char input[] = "{\"a\":1}";
|
||||
|
||||
DeserializationError err = deserializeJson(doc, input);
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
}
|
||||
|
||||
SECTION("TooSmallBufferForObjectWithNestedObject") {
|
||||
StaticJsonDocument<JSON_OBJECT_SIZE(0) + JSON_ARRAY_SIZE(0)> doc;
|
||||
char input[] = "{\"a\":[]}";
|
||||
|
||||
DeserializationError err = deserializeJson(doc, input);
|
||||
|
||||
REQUIRE(err == DeserializationError::NoMemory);
|
||||
}
|
||||
|
||||
SECTION("BufferOfTheRightSizeForObjectWithNestedObject") {
|
||||
StaticJsonDocument<JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(0)> doc;
|
||||
char input[] = "{\"a\":[]}";
|
||||
|
||||
DeserializationError err = deserializeJson(doc, input);
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
}
|
||||
|
||||
SECTION("Should clear the JsonObject") {
|
||||
StaticJsonDocument<JSON_OBJECT_SIZE(1)> doc;
|
||||
char input[] = "{\"hello\":\"world\"}";
|
||||
|
||||
deserializeJson(doc, input);
|
||||
deserializeJson(doc, "{}");
|
||||
|
||||
REQUIRE(doc.as<JsonObject>().size() == 0);
|
||||
REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(0));
|
||||
}
|
||||
}
|
@ -6,6 +6,12 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
|
||||
using ArduinoJson::detail::sizeofArray;
|
||||
using ArduinoJson::detail::sizeofObject;
|
||||
using ArduinoJson::detail::sizeofString;
|
||||
|
||||
TEST_CASE("Valid JSON strings value") {
|
||||
struct TestCase {
|
||||
const char* input;
|
||||
@ -35,7 +41,7 @@ TEST_CASE("Valid JSON strings value") {
|
||||
};
|
||||
const size_t testCount = sizeof(testCases) / sizeof(testCases[0]);
|
||||
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
|
||||
for (size_t i = 0; i < testCount; i++) {
|
||||
const TestCase& testCase = testCases[i];
|
||||
@ -47,7 +53,7 @@ TEST_CASE("Valid JSON strings value") {
|
||||
}
|
||||
|
||||
TEST_CASE("\\u0000") {
|
||||
StaticJsonDocument<200> doc;
|
||||
JsonDocument doc(200);
|
||||
|
||||
DeserializationError err = deserializeJson(doc, "\"wx\\u0000yz\"");
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
@ -68,7 +74,7 @@ TEST_CASE("Truncated JSON string") {
|
||||
const char* testCases[] = {"\"hello", "\'hello", "'\\u", "'\\u00", "'\\u000"};
|
||||
const size_t testCount = sizeof(testCases) / sizeof(testCases[0]);
|
||||
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
|
||||
for (size_t i = 0; i < testCount; i++) {
|
||||
const char* input = testCases[i];
|
||||
@ -83,7 +89,7 @@ TEST_CASE("Invalid JSON string") {
|
||||
"'\\u000G'", "'\\u000/'", "'\\x1234'"};
|
||||
const size_t testCount = sizeof(testCases) / sizeof(testCases[0]);
|
||||
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
|
||||
for (size_t i = 0; i < testCount; i++) {
|
||||
const char* input = testCases[i];
|
||||
@ -92,41 +98,80 @@ TEST_CASE("Invalid JSON string") {
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Not enough room to save the key") {
|
||||
DynamicJsonDocument doc(JSON_OBJECT_SIZE(1) + 8);
|
||||
TEST_CASE("Allocation of the key fails") {
|
||||
TimebombAllocator timebombAllocator(1);
|
||||
SpyingAllocator spyingAllocator(&timebombAllocator);
|
||||
JsonDocument doc(1024, &spyingAllocator);
|
||||
|
||||
SECTION("Quoted string") {
|
||||
SECTION("Quoted string, first member") {
|
||||
REQUIRE(deserializeJson(doc, "{\"example\":1}") ==
|
||||
DeserializationError::Ok);
|
||||
REQUIRE(deserializeJson(doc, "{\"accuracy\":1}") ==
|
||||
DeserializationError::NoMemory);
|
||||
REQUIRE(deserializeJson(doc, "{\"hello\":1,\"world\"}") ==
|
||||
DeserializationError::NoMemory); // fails in the second string
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::Allocate(1024)
|
||||
<< AllocatorLog::AllocateFail(sizeofString(31)));
|
||||
}
|
||||
|
||||
SECTION("Non-quoted string") {
|
||||
REQUIRE(deserializeJson(doc, "{example:1}") == DeserializationError::Ok);
|
||||
REQUIRE(deserializeJson(doc, "{accuracy:1}") ==
|
||||
SECTION("Quoted string, second member") {
|
||||
timebombAllocator.setCountdown(2);
|
||||
REQUIRE(deserializeJson(doc, "{\"hello\":1,\"world\"}") ==
|
||||
DeserializationError::NoMemory);
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::Allocate(1024)
|
||||
<< AllocatorLog::Allocate(sizeofString(31))
|
||||
<< AllocatorLog::Reallocate(sizeofString(31),
|
||||
sizeofString(5))
|
||||
<< AllocatorLog::AllocateFail(sizeofString(31)));
|
||||
}
|
||||
|
||||
SECTION("Non-Quoted string, first member") {
|
||||
REQUIRE(deserializeJson(doc, "{example:1}") ==
|
||||
DeserializationError::NoMemory);
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::Allocate(1024)
|
||||
<< AllocatorLog::AllocateFail(sizeofString(31)));
|
||||
}
|
||||
|
||||
SECTION("Non-Quoted string, second member") {
|
||||
timebombAllocator.setCountdown(2);
|
||||
REQUIRE(deserializeJson(doc, "{hello:1,world}") ==
|
||||
DeserializationError::NoMemory); // fails in the second string
|
||||
DeserializationError::NoMemory);
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::Allocate(1024)
|
||||
<< AllocatorLog::Allocate(sizeofString(31))
|
||||
<< AllocatorLog::Reallocate(sizeofString(31),
|
||||
sizeofString(5))
|
||||
<< AllocatorLog::AllocateFail(sizeofString(31)));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Empty memory pool") {
|
||||
// NOLINTNEXTLINE(clang-analyzer-optin.portability.UnixAPI)
|
||||
DynamicJsonDocument doc(0);
|
||||
TEST_CASE("String allocation fails") {
|
||||
SpyingAllocator spyingAllocator(FailingAllocator::instance());
|
||||
JsonDocument doc(0, &spyingAllocator);
|
||||
|
||||
SECTION("Input is const char*") {
|
||||
REQUIRE(deserializeJson(doc, "\"hello\"") ==
|
||||
DeserializationError::NoMemory);
|
||||
REQUIRE(deserializeJson(doc, "\"\"") == DeserializationError::NoMemory);
|
||||
}
|
||||
|
||||
SECTION("Input is const char*") {
|
||||
char hello[] = "\"hello\"";
|
||||
REQUIRE(deserializeJson(doc, hello) == DeserializationError::Ok);
|
||||
char empty[] = "\"hello\"";
|
||||
REQUIRE(deserializeJson(doc, empty) == DeserializationError::Ok);
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::AllocateFail(sizeofString(31)));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Deduplicate values") {
|
||||
JsonDocument doc(1024);
|
||||
deserializeJson(doc, "[\"example\",\"example\"]");
|
||||
|
||||
CHECK(doc.memoryUsage() == sizeofArray(2) + sizeofString(7));
|
||||
CHECK(doc[0].as<const char*>() == doc[1].as<const char*>());
|
||||
}
|
||||
|
||||
TEST_CASE("Deduplicate keys") {
|
||||
JsonDocument doc(1024);
|
||||
deserializeJson(doc, "[{\"example\":1},{\"example\":2}]");
|
||||
|
||||
CHECK(doc.memoryUsage() ==
|
||||
2 * sizeofObject(1) + sizeofArray(2) + sizeofString(7));
|
||||
|
||||
const char* key1 = doc[0].as<JsonObject>().begin()->key().c_str();
|
||||
const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str();
|
||||
CHECK(key1 == key2);
|
||||
}
|
||||
|
@ -1,180 +0,0 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <stdlib.h> // malloc, free
|
||||
#include <catch.hpp>
|
||||
#include <sstream>
|
||||
#include <utility>
|
||||
|
||||
class SpyingAllocator {
|
||||
public:
|
||||
SpyingAllocator(const SpyingAllocator& src) : _log(src._log) {}
|
||||
SpyingAllocator(std::ostream& log) : _log(log) {}
|
||||
SpyingAllocator& operator=(const SpyingAllocator& src) = delete;
|
||||
|
||||
void* allocate(size_t n) {
|
||||
_log << "A" << n;
|
||||
return malloc(n);
|
||||
}
|
||||
void deallocate(void* p) {
|
||||
_log << "F";
|
||||
free(p);
|
||||
}
|
||||
|
||||
private:
|
||||
std::ostream& _log;
|
||||
};
|
||||
|
||||
class ControllableAllocator {
|
||||
public:
|
||||
ControllableAllocator() : _enabled(true) {}
|
||||
|
||||
void* allocate(size_t n) {
|
||||
return _enabled ? malloc(n) : 0;
|
||||
}
|
||||
|
||||
void deallocate(void* p) {
|
||||
free(p);
|
||||
}
|
||||
|
||||
void disable() {
|
||||
_enabled = false;
|
||||
}
|
||||
|
||||
private:
|
||||
bool _enabled;
|
||||
};
|
||||
|
||||
TEST_CASE("BasicJsonDocument") {
|
||||
std::stringstream log;
|
||||
|
||||
SECTION("Construct/Destruct") {
|
||||
{ BasicJsonDocument<SpyingAllocator> doc(4096, log); }
|
||||
REQUIRE(log.str() == "A4096F");
|
||||
}
|
||||
|
||||
SECTION("Copy construct") {
|
||||
{
|
||||
BasicJsonDocument<SpyingAllocator> doc1(4096, log);
|
||||
doc1.set(std::string("The size of this string is 32!!"));
|
||||
|
||||
BasicJsonDocument<SpyingAllocator> doc2(doc1);
|
||||
|
||||
REQUIRE(doc1.as<std::string>() == "The size of this string is 32!!");
|
||||
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
|
||||
REQUIRE(doc2.capacity() == 4096);
|
||||
}
|
||||
REQUIRE(log.str() == "A4096A4096FF");
|
||||
}
|
||||
|
||||
SECTION("Move construct") {
|
||||
{
|
||||
BasicJsonDocument<SpyingAllocator> doc1(4096, log);
|
||||
doc1.set(std::string("The size of this string is 32!!"));
|
||||
|
||||
BasicJsonDocument<SpyingAllocator> doc2(std::move(doc1));
|
||||
|
||||
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
|
||||
REQUIRE(doc1.as<std::string>() == "null");
|
||||
REQUIRE(doc1.capacity() == 0);
|
||||
REQUIRE(doc2.capacity() == 4096);
|
||||
}
|
||||
REQUIRE(log.str() == "A4096F");
|
||||
}
|
||||
|
||||
SECTION("Copy assign larger") {
|
||||
{
|
||||
BasicJsonDocument<SpyingAllocator> doc1(4096, log);
|
||||
doc1.set(std::string("The size of this string is 32!!"));
|
||||
BasicJsonDocument<SpyingAllocator> doc2(8, log);
|
||||
|
||||
doc2 = doc1;
|
||||
|
||||
REQUIRE(doc1.as<std::string>() == "The size of this string is 32!!");
|
||||
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
|
||||
REQUIRE(doc2.capacity() == 4096);
|
||||
}
|
||||
REQUIRE(log.str() == "A4096A8FA4096FF");
|
||||
}
|
||||
|
||||
SECTION("Copy assign smaller") {
|
||||
{
|
||||
BasicJsonDocument<SpyingAllocator> doc1(1024, log);
|
||||
doc1.set(std::string("The size of this string is 32!!"));
|
||||
BasicJsonDocument<SpyingAllocator> doc2(4096, log);
|
||||
|
||||
doc2 = doc1;
|
||||
|
||||
REQUIRE(doc1.as<std::string>() == "The size of this string is 32!!");
|
||||
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
|
||||
REQUIRE(doc2.capacity() == 1024);
|
||||
}
|
||||
REQUIRE(log.str() == "A1024A4096FA1024FF");
|
||||
}
|
||||
|
||||
SECTION("Copy assign same size") {
|
||||
{
|
||||
BasicJsonDocument<SpyingAllocator> doc1(1024, log);
|
||||
doc1.set(std::string("The size of this string is 32!!"));
|
||||
BasicJsonDocument<SpyingAllocator> doc2(1024, log);
|
||||
|
||||
doc2 = doc1;
|
||||
|
||||
REQUIRE(doc1.as<std::string>() == "The size of this string is 32!!");
|
||||
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
|
||||
REQUIRE(doc2.capacity() == 1024);
|
||||
}
|
||||
REQUIRE(log.str() == "A1024A1024FF");
|
||||
}
|
||||
|
||||
SECTION("Move assign") {
|
||||
{
|
||||
BasicJsonDocument<SpyingAllocator> doc1(4096, log);
|
||||
doc1.set(std::string("The size of this string is 32!!"));
|
||||
BasicJsonDocument<SpyingAllocator> doc2(8, log);
|
||||
|
||||
doc2 = std::move(doc1);
|
||||
|
||||
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
|
||||
REQUIRE(doc1.as<std::string>() == "null");
|
||||
REQUIRE(doc1.capacity() == 0);
|
||||
REQUIRE(doc2.capacity() == 4096);
|
||||
}
|
||||
REQUIRE(log.str() == "A4096A8FF");
|
||||
}
|
||||
|
||||
SECTION("garbageCollect()") {
|
||||
BasicJsonDocument<ControllableAllocator> doc(4096);
|
||||
|
||||
SECTION("when allocation succeeds") {
|
||||
deserializeJson(doc, "{\"blanket\":1,\"dancing\":2}");
|
||||
REQUIRE(doc.capacity() == 4096);
|
||||
REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(2) + 16);
|
||||
doc.remove("blanket");
|
||||
|
||||
bool result = doc.garbageCollect();
|
||||
|
||||
REQUIRE(result == true);
|
||||
REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(1) + 8);
|
||||
REQUIRE(doc.capacity() == 4096);
|
||||
REQUIRE(doc.as<std::string>() == "{\"dancing\":2}");
|
||||
}
|
||||
|
||||
SECTION("when allocation fails") {
|
||||
deserializeJson(doc, "{\"blanket\":1,\"dancing\":2}");
|
||||
REQUIRE(doc.capacity() == 4096);
|
||||
REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(2) + 16);
|
||||
doc.remove("blanket");
|
||||
doc.allocator().disable();
|
||||
|
||||
bool result = doc.garbageCollect();
|
||||
|
||||
REQUIRE(result == false);
|
||||
REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(2) + 16);
|
||||
REQUIRE(doc.capacity() == 4096);
|
||||
REQUIRE(doc.as<std::string>() == "{\"dancing\":2}");
|
||||
}
|
||||
}
|
||||
}
|
@ -4,22 +4,23 @@
|
||||
|
||||
add_executable(JsonDocumentTests
|
||||
add.cpp
|
||||
BasicJsonDocument.cpp
|
||||
assignment.cpp
|
||||
cast.cpp
|
||||
compare.cpp
|
||||
constructor.cpp
|
||||
containsKey.cpp
|
||||
createNested.cpp
|
||||
DynamicJsonDocument.cpp
|
||||
ElementProxy.cpp
|
||||
garbageCollect.cpp
|
||||
isNull.cpp
|
||||
issue1120.cpp
|
||||
MemberProxy.cpp
|
||||
memoryUsage.cpp
|
||||
nesting.cpp
|
||||
overflowed.cpp
|
||||
remove.cpp
|
||||
shrinkToFit.cpp
|
||||
size.cpp
|
||||
StaticJsonDocument.cpp
|
||||
subscript.cpp
|
||||
swap.cpp
|
||||
)
|
||||
|
@ -1,230 +0,0 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
using ArduinoJson::detail::addPadding;
|
||||
|
||||
static void REQUIRE_JSON(JsonDocument& doc, const std::string& expected) {
|
||||
std::string json;
|
||||
serializeJson(doc, json);
|
||||
REQUIRE(json == expected);
|
||||
}
|
||||
|
||||
TEST_CASE("DynamicJsonDocument") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
|
||||
SECTION("serializeJson()") {
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
obj["hello"] = "world";
|
||||
|
||||
std::string json;
|
||||
serializeJson(doc, json);
|
||||
|
||||
REQUIRE(json == "{\"hello\":\"world\"}");
|
||||
}
|
||||
|
||||
SECTION("memoryUsage()") {
|
||||
SECTION("starts at zero") {
|
||||
REQUIRE(doc.memoryUsage() == 0);
|
||||
}
|
||||
|
||||
SECTION("JSON_ARRAY_SIZE(0)") {
|
||||
doc.to<JsonArray>();
|
||||
REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(0));
|
||||
}
|
||||
|
||||
SECTION("JSON_ARRAY_SIZE(1)") {
|
||||
doc.to<JsonArray>().add(42);
|
||||
REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(1));
|
||||
}
|
||||
|
||||
SECTION("JSON_ARRAY_SIZE(1) + JSON_ARRAY_SIZE(0)") {
|
||||
doc.to<JsonArray>().createNestedArray();
|
||||
REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(1) + JSON_ARRAY_SIZE(0));
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("capacity()") {
|
||||
SECTION("matches constructor argument") {
|
||||
DynamicJsonDocument doc2(256);
|
||||
REQUIRE(doc2.capacity() == 256);
|
||||
}
|
||||
|
||||
SECTION("rounds up constructor argument") {
|
||||
DynamicJsonDocument doc2(253);
|
||||
REQUIRE(doc2.capacity() == 256);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("memoryUsage()") {
|
||||
SECTION("Increases after adding value to array") {
|
||||
JsonArray arr = doc.to<JsonArray>();
|
||||
|
||||
REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(0));
|
||||
arr.add(42);
|
||||
REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(1));
|
||||
arr.add(43);
|
||||
REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(2));
|
||||
}
|
||||
|
||||
SECTION("Increases after adding value to object") {
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
|
||||
REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(0));
|
||||
obj["a"] = 1;
|
||||
REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(1));
|
||||
obj["b"] = 2;
|
||||
REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(2));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("DynamicJsonDocument constructor") {
|
||||
SECTION("Copy constructor") {
|
||||
DynamicJsonDocument doc1(1234);
|
||||
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
||||
|
||||
DynamicJsonDocument doc2 = doc1;
|
||||
|
||||
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
|
||||
|
||||
REQUIRE(doc2.capacity() == doc1.capacity());
|
||||
}
|
||||
|
||||
SECTION("Construct from StaticJsonDocument") {
|
||||
StaticJsonDocument<200> doc1;
|
||||
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
||||
|
||||
DynamicJsonDocument doc2 = doc1;
|
||||
|
||||
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
|
||||
REQUIRE(doc2.capacity() == doc1.capacity());
|
||||
}
|
||||
|
||||
SECTION("Construct from JsonObject") {
|
||||
StaticJsonDocument<200> doc1;
|
||||
JsonObject obj = doc1.to<JsonObject>();
|
||||
obj["hello"] = "world";
|
||||
|
||||
DynamicJsonDocument doc2 = obj;
|
||||
|
||||
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
|
||||
REQUIRE(doc2.capacity() == addPadding(doc1.memoryUsage()));
|
||||
}
|
||||
|
||||
SECTION("Construct from JsonArray") {
|
||||
StaticJsonDocument<200> doc1;
|
||||
JsonArray arr = doc1.to<JsonArray>();
|
||||
arr.add("hello");
|
||||
|
||||
DynamicJsonDocument doc2 = arr;
|
||||
|
||||
REQUIRE_JSON(doc2, "[\"hello\"]");
|
||||
REQUIRE(doc2.capacity() == addPadding(doc1.memoryUsage()));
|
||||
}
|
||||
|
||||
SECTION("Construct from JsonVariant") {
|
||||
StaticJsonDocument<200> doc1;
|
||||
deserializeJson(doc1, "42");
|
||||
|
||||
DynamicJsonDocument doc2 = doc1.as<JsonVariant>();
|
||||
|
||||
REQUIRE_JSON(doc2, "42");
|
||||
REQUIRE(doc2.capacity() == addPadding(doc1.memoryUsage()));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("DynamicJsonDocument assignment") {
|
||||
SECTION("Copy assignment reallocates when capacity is smaller") {
|
||||
DynamicJsonDocument doc1(1234);
|
||||
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
||||
DynamicJsonDocument doc2(8);
|
||||
|
||||
doc2 = doc1;
|
||||
|
||||
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
|
||||
REQUIRE(doc2.capacity() == doc1.capacity());
|
||||
}
|
||||
|
||||
SECTION("Copy assignment reallocates when capacity is larger") {
|
||||
DynamicJsonDocument doc1(100);
|
||||
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
||||
DynamicJsonDocument doc2(1234);
|
||||
|
||||
doc2 = doc1;
|
||||
|
||||
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
|
||||
REQUIRE(doc2.capacity() == doc1.capacity());
|
||||
}
|
||||
|
||||
SECTION("Assign from StaticJsonDocument") {
|
||||
StaticJsonDocument<200> doc1;
|
||||
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
||||
DynamicJsonDocument doc2(4096);
|
||||
doc2.to<JsonVariant>().set(666);
|
||||
|
||||
doc2 = doc1;
|
||||
|
||||
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
|
||||
}
|
||||
|
||||
SECTION("Assign from JsonObject") {
|
||||
StaticJsonDocument<200> doc1;
|
||||
JsonObject obj = doc1.to<JsonObject>();
|
||||
obj["hello"] = "world";
|
||||
|
||||
DynamicJsonDocument doc2(4096);
|
||||
doc2 = obj;
|
||||
|
||||
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
|
||||
REQUIRE(doc2.capacity() == 4096);
|
||||
}
|
||||
|
||||
SECTION("Assign from JsonArray") {
|
||||
StaticJsonDocument<200> doc1;
|
||||
JsonArray arr = doc1.to<JsonArray>();
|
||||
arr.add("hello");
|
||||
|
||||
DynamicJsonDocument doc2(4096);
|
||||
doc2 = arr;
|
||||
|
||||
REQUIRE_JSON(doc2, "[\"hello\"]");
|
||||
REQUIRE(doc2.capacity() == 4096);
|
||||
}
|
||||
|
||||
SECTION("Assign from JsonVariant") {
|
||||
StaticJsonDocument<200> doc1;
|
||||
deserializeJson(doc1, "42");
|
||||
|
||||
DynamicJsonDocument doc2(4096);
|
||||
doc2 = doc1.as<JsonVariant>();
|
||||
|
||||
REQUIRE_JSON(doc2, "42");
|
||||
REQUIRE(doc2.capacity() == 4096);
|
||||
}
|
||||
|
||||
SECTION("Assign from MemberProxy") {
|
||||
StaticJsonDocument<200> doc1;
|
||||
doc1["value"] = 42;
|
||||
|
||||
DynamicJsonDocument doc2(4096);
|
||||
doc2 = doc1["value"];
|
||||
|
||||
REQUIRE_JSON(doc2, "42");
|
||||
REQUIRE(doc2.capacity() == 4096);
|
||||
}
|
||||
|
||||
SECTION("Assign from ElementProxy") {
|
||||
StaticJsonDocument<200> doc1;
|
||||
doc1[0] = 42;
|
||||
|
||||
DynamicJsonDocument doc2(4096);
|
||||
doc2 = doc1[0];
|
||||
|
||||
REQUIRE_JSON(doc2, "42");
|
||||
REQUIRE(doc2.capacity() == 4096);
|
||||
}
|
||||
}
|
@ -6,9 +6,10 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
typedef ArduinoJson::detail::ElementProxy<JsonDocument&> ElementProxy;
|
||||
using ArduinoJson::detail::sizeofString;
|
||||
|
||||
TEST_CASE("ElementProxy::add()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
doc.add();
|
||||
ElementProxy ep = doc[0];
|
||||
|
||||
@ -34,7 +35,7 @@ TEST_CASE("ElementProxy::add()") {
|
||||
}
|
||||
|
||||
TEST_CASE("ElementProxy::clear()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
doc.add();
|
||||
ElementProxy ep = doc[0];
|
||||
|
||||
@ -54,7 +55,7 @@ TEST_CASE("ElementProxy::clear()") {
|
||||
}
|
||||
|
||||
TEST_CASE("ElementProxy::operator==()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
|
||||
SECTION("1 vs 1") {
|
||||
doc.add(1);
|
||||
@ -94,7 +95,7 @@ TEST_CASE("ElementProxy::operator==()") {
|
||||
}
|
||||
|
||||
TEST_CASE("ElementProxy::remove()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
doc.add();
|
||||
ElementProxy ep = doc[0];
|
||||
|
||||
@ -142,7 +143,7 @@ TEST_CASE("ElementProxy::remove()") {
|
||||
}
|
||||
|
||||
TEST_CASE("ElementProxy::set()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
ElementProxy ep = doc[0];
|
||||
|
||||
SECTION("set(int)") {
|
||||
@ -167,7 +168,7 @@ TEST_CASE("ElementProxy::set()") {
|
||||
}
|
||||
|
||||
TEST_CASE("ElementProxy::size()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
doc.add();
|
||||
ElementProxy ep = doc[0];
|
||||
|
||||
@ -189,7 +190,7 @@ TEST_CASE("ElementProxy::size()") {
|
||||
}
|
||||
|
||||
TEST_CASE("ElementProxy::memoryUsage()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
doc.add();
|
||||
ElementProxy ep = doc[0];
|
||||
|
||||
@ -199,12 +200,12 @@ TEST_CASE("ElementProxy::memoryUsage()") {
|
||||
|
||||
SECTION("returns size for string") {
|
||||
ep.set(std::string("hello"));
|
||||
REQUIRE(ep.memoryUsage() == 6);
|
||||
REQUIRE(ep.memoryUsage() == sizeofString(5));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("ElementProxy::operator[]") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
ElementProxy ep = doc[1];
|
||||
|
||||
SECTION("set member") {
|
||||
@ -221,7 +222,7 @@ TEST_CASE("ElementProxy::operator[]") {
|
||||
}
|
||||
|
||||
TEST_CASE("ElementProxy cast to JsonVariantConst") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
doc[0] = "world";
|
||||
|
||||
const ElementProxy ep = doc[0];
|
||||
@ -232,7 +233,7 @@ TEST_CASE("ElementProxy cast to JsonVariantConst") {
|
||||
}
|
||||
|
||||
TEST_CASE("ElementProxy cast to JsonVariant") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
doc[0] = "world";
|
||||
|
||||
ElementProxy ep = doc[0];
|
||||
@ -247,7 +248,7 @@ TEST_CASE("ElementProxy cast to JsonVariant") {
|
||||
}
|
||||
|
||||
TEST_CASE("ElementProxy::shallowCopy()") {
|
||||
StaticJsonDocument<1024> doc1, doc2;
|
||||
JsonDocument doc1(1024), doc2(1024);
|
||||
doc2["hello"] = "world";
|
||||
doc1[0].shallowCopy(doc2);
|
||||
|
||||
|
@ -2,14 +2,23 @@
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#define ARDUINOJSON_ENABLE_ARDUINO_STRING 1
|
||||
#define ARDUINOJSON_ENABLE_PROGMEM 1
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
|
||||
using ArduinoJson::detail::sizeofArray;
|
||||
using ArduinoJson::detail::sizeofObject;
|
||||
using ArduinoJson::detail::sizeofString;
|
||||
|
||||
typedef ArduinoJson::detail::MemberProxy<JsonDocument&, const char*>
|
||||
MemberProxy;
|
||||
|
||||
TEST_CASE("MemberProxy::add()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
MemberProxy mp = doc["hello"];
|
||||
|
||||
SECTION("add(int)") {
|
||||
@ -26,7 +35,7 @@ TEST_CASE("MemberProxy::add()") {
|
||||
}
|
||||
|
||||
TEST_CASE("MemberProxy::clear()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
MemberProxy mp = doc["hello"];
|
||||
|
||||
SECTION("size goes back to zero") {
|
||||
@ -45,7 +54,7 @@ TEST_CASE("MemberProxy::clear()") {
|
||||
}
|
||||
|
||||
TEST_CASE("MemberProxy::operator==()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
|
||||
SECTION("1 vs 1") {
|
||||
doc["a"] = 1;
|
||||
@ -85,7 +94,7 @@ TEST_CASE("MemberProxy::operator==()") {
|
||||
}
|
||||
|
||||
TEST_CASE("MemberProxy::containsKey()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
MemberProxy mp = doc["hello"];
|
||||
|
||||
SECTION("containsKey(const char*)") {
|
||||
@ -104,7 +113,7 @@ TEST_CASE("MemberProxy::containsKey()") {
|
||||
}
|
||||
|
||||
TEST_CASE("MemberProxy::operator|()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
|
||||
SECTION("const char*") {
|
||||
doc["a"] = "hello";
|
||||
@ -127,7 +136,7 @@ TEST_CASE("MemberProxy::operator|()") {
|
||||
JsonObject object = doc.to<JsonObject>();
|
||||
object["hello"] = "world";
|
||||
|
||||
StaticJsonDocument<0> emptyDoc;
|
||||
JsonDocument emptyDoc(0);
|
||||
JsonObject anotherObject = object["hello"] | emptyDoc.to<JsonObject>();
|
||||
|
||||
REQUIRE(anotherObject.isNull() == false);
|
||||
@ -136,7 +145,7 @@ TEST_CASE("MemberProxy::operator|()") {
|
||||
}
|
||||
|
||||
TEST_CASE("MemberProxy::remove()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
MemberProxy mp = doc["hello"];
|
||||
|
||||
SECTION("remove(int)") {
|
||||
@ -183,7 +192,7 @@ TEST_CASE("MemberProxy::remove()") {
|
||||
}
|
||||
|
||||
TEST_CASE("MemberProxy::set()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
MemberProxy mp = doc["hello"];
|
||||
|
||||
SECTION("set(int)") {
|
||||
@ -208,7 +217,7 @@ TEST_CASE("MemberProxy::set()") {
|
||||
}
|
||||
|
||||
TEST_CASE("MemberProxy::size()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
MemberProxy mp = doc["hello"];
|
||||
|
||||
SECTION("returns 0") {
|
||||
@ -231,7 +240,7 @@ TEST_CASE("MemberProxy::size()") {
|
||||
}
|
||||
|
||||
TEST_CASE("MemberProxy::memoryUsage()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
MemberProxy mp = doc["hello"];
|
||||
|
||||
SECTION("returns 0 when null") {
|
||||
@ -240,12 +249,12 @@ TEST_CASE("MemberProxy::memoryUsage()") {
|
||||
|
||||
SECTION("return the size for a string") {
|
||||
mp.set(std::string("hello"));
|
||||
REQUIRE(mp.memoryUsage() == 6);
|
||||
REQUIRE(mp.memoryUsage() == sizeofString(5));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("MemberProxy::operator[]") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
MemberProxy mp = doc["hello"];
|
||||
|
||||
SECTION("set member") {
|
||||
@ -262,7 +271,7 @@ TEST_CASE("MemberProxy::operator[]") {
|
||||
}
|
||||
|
||||
TEST_CASE("MemberProxy cast to JsonVariantConst") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
doc["hello"] = "world";
|
||||
|
||||
const MemberProxy mp = doc["hello"];
|
||||
@ -273,7 +282,7 @@ TEST_CASE("MemberProxy cast to JsonVariantConst") {
|
||||
}
|
||||
|
||||
TEST_CASE("MemberProxy cast to JsonVariant") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
doc["hello"] = "world";
|
||||
|
||||
MemberProxy mp = doc["hello"];
|
||||
@ -288,7 +297,7 @@ TEST_CASE("MemberProxy cast to JsonVariant") {
|
||||
}
|
||||
|
||||
TEST_CASE("MemberProxy::createNestedArray()") {
|
||||
StaticJsonDocument<1024> doc;
|
||||
JsonDocument doc(1024);
|
||||
JsonArray arr = doc["items"].createNestedArray();
|
||||
arr.add(42);
|
||||
|
||||
@ -296,7 +305,7 @@ TEST_CASE("MemberProxy::createNestedArray()") {
|
||||
}
|
||||
|
||||
TEST_CASE("MemberProxy::createNestedArray(key)") {
|
||||
StaticJsonDocument<1024> doc;
|
||||
JsonDocument doc(1024);
|
||||
JsonArray arr = doc["weather"].createNestedArray("temp");
|
||||
arr.add(42);
|
||||
|
||||
@ -304,7 +313,7 @@ TEST_CASE("MemberProxy::createNestedArray(key)") {
|
||||
}
|
||||
|
||||
TEST_CASE("MemberProxy::createNestedObject()") {
|
||||
StaticJsonDocument<1024> doc;
|
||||
JsonDocument doc(1024);
|
||||
JsonObject obj = doc["items"].createNestedObject();
|
||||
obj["value"] = 42;
|
||||
|
||||
@ -312,7 +321,7 @@ TEST_CASE("MemberProxy::createNestedObject()") {
|
||||
}
|
||||
|
||||
TEST_CASE("MemberProxy::createNestedObject(key)") {
|
||||
StaticJsonDocument<1024> doc;
|
||||
JsonDocument doc(1024);
|
||||
JsonObject obj = doc["status"].createNestedObject("weather");
|
||||
obj["temp"] = 42;
|
||||
|
||||
@ -320,9 +329,78 @@ TEST_CASE("MemberProxy::createNestedObject(key)") {
|
||||
}
|
||||
|
||||
TEST_CASE("MemberProxy::shallowCopy()") {
|
||||
StaticJsonDocument<1024> doc1, doc2;
|
||||
JsonDocument doc1(1024), doc2(1024);
|
||||
doc2["hello"] = "world";
|
||||
doc1["obj"].shallowCopy(doc2);
|
||||
|
||||
CHECK(doc1.as<std::string>() == "{\"obj\":{\"hello\":\"world\"}}");
|
||||
}
|
||||
|
||||
TEST_CASE("Deduplicate keys") {
|
||||
JsonDocument doc(1024);
|
||||
|
||||
SECTION("std::string") {
|
||||
doc[0][std::string("example")] = 1;
|
||||
doc[1][std::string("example")] = 2;
|
||||
|
||||
CHECK(doc.memoryUsage() ==
|
||||
sizeofArray(2) + 2 * sizeofObject(1) + sizeofString(7));
|
||||
|
||||
const char* key1 = doc[0].as<JsonObject>().begin()->key().c_str();
|
||||
const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str();
|
||||
CHECK(key1 == key2);
|
||||
}
|
||||
|
||||
SECTION("char*") {
|
||||
char key[] = "example";
|
||||
doc[0][key] = 1;
|
||||
doc[1][key] = 2;
|
||||
|
||||
CHECK(doc.memoryUsage() ==
|
||||
sizeofArray(2) + 2 * sizeofObject(1) + sizeofString(7));
|
||||
|
||||
const char* key1 = doc[0].as<JsonObject>().begin()->key().c_str();
|
||||
const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str();
|
||||
CHECK(key1 == key2);
|
||||
}
|
||||
|
||||
SECTION("Arduino String") {
|
||||
doc[0][String("example")] = 1;
|
||||
doc[1][String("example")] = 2;
|
||||
|
||||
CHECK(doc.memoryUsage() ==
|
||||
sizeofArray(2) + 2 * sizeofObject(1) + sizeofString(7));
|
||||
|
||||
const char* key1 = doc[0].as<JsonObject>().begin()->key().c_str();
|
||||
const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str();
|
||||
CHECK(key1 == key2);
|
||||
}
|
||||
|
||||
SECTION("Flash string") {
|
||||
doc[0][F("example")] = 1;
|
||||
doc[1][F("example")] = 2;
|
||||
|
||||
CHECK(doc.memoryUsage() ==
|
||||
sizeofArray(2) + 2 * sizeofObject(1) + sizeofString(7));
|
||||
|
||||
const char* key1 = doc[0].as<JsonObject>().begin()->key().c_str();
|
||||
const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str();
|
||||
CHECK(key1 == key2);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("MemberProxy under memory constraints") {
|
||||
ControllableAllocator allocator;
|
||||
JsonDocument doc(4096, &allocator);
|
||||
|
||||
SECTION("key allocation fails") {
|
||||
allocator.disable();
|
||||
|
||||
doc[std::string("hello")] = "world";
|
||||
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
REQUIRE(doc.size() == 0);
|
||||
REQUIRE(doc.memoryUsage() == sizeofObject(1));
|
||||
REQUIRE(doc.overflowed() == true);
|
||||
}
|
||||
}
|
||||
|
@ -1,224 +0,0 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
static void REQUIRE_JSON(JsonDocument& doc, const std::string& expected) {
|
||||
std::string json;
|
||||
serializeJson(doc, json);
|
||||
REQUIRE(json == expected);
|
||||
}
|
||||
|
||||
TEST_CASE("StaticJsonDocument") {
|
||||
SECTION("capacity()") {
|
||||
SECTION("matches template argument") {
|
||||
StaticJsonDocument<256> doc;
|
||||
REQUIRE(doc.capacity() == 256);
|
||||
}
|
||||
|
||||
SECTION("rounds up template argument") {
|
||||
StaticJsonDocument<253> doc;
|
||||
REQUIRE(doc.capacity() == 256);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("serializeJson()") {
|
||||
StaticJsonDocument<200> doc;
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
obj["hello"] = "world";
|
||||
|
||||
std::string json;
|
||||
serializeJson(doc, json);
|
||||
|
||||
REQUIRE(json == "{\"hello\":\"world\"}");
|
||||
}
|
||||
|
||||
SECTION("Copy assignment") {
|
||||
StaticJsonDocument<200> doc1, doc2;
|
||||
doc1.to<JsonVariant>().set(666);
|
||||
deserializeJson(doc2, "{\"hello\":\"world\"}");
|
||||
|
||||
doc1 = doc2;
|
||||
|
||||
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
|
||||
}
|
||||
|
||||
SECTION("Contructor") {
|
||||
SECTION("Copy constructor") {
|
||||
StaticJsonDocument<200> doc1;
|
||||
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
||||
|
||||
StaticJsonDocument<200> doc2 = doc1;
|
||||
|
||||
deserializeJson(doc1, "{\"HELLO\":\"WORLD\"}");
|
||||
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
|
||||
}
|
||||
|
||||
SECTION("Construct from StaticJsonDocument of different size") {
|
||||
StaticJsonDocument<300> doc1;
|
||||
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
||||
|
||||
StaticJsonDocument<200> doc2 = doc1;
|
||||
|
||||
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
|
||||
}
|
||||
|
||||
SECTION("Construct from DynamicJsonDocument") {
|
||||
DynamicJsonDocument doc1(4096);
|
||||
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
||||
|
||||
StaticJsonDocument<200> doc2 = doc1;
|
||||
|
||||
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
|
||||
}
|
||||
|
||||
SECTION("Construct from JsonObject") {
|
||||
DynamicJsonDocument doc1(4096);
|
||||
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
||||
|
||||
StaticJsonDocument<200> doc2 = doc1.as<JsonObject>();
|
||||
|
||||
deserializeJson(doc1, "{\"HELLO\":\"WORLD\"}");
|
||||
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
|
||||
}
|
||||
|
||||
SECTION("Construct from JsonArray") {
|
||||
DynamicJsonDocument doc1(4096);
|
||||
deserializeJson(doc1, "[\"hello\",\"world\"]");
|
||||
|
||||
StaticJsonDocument<200> doc2 = doc1.as<JsonArray>();
|
||||
|
||||
deserializeJson(doc1, "[\"HELLO\",\"WORLD\"]");
|
||||
REQUIRE_JSON(doc2, "[\"hello\",\"world\"]");
|
||||
}
|
||||
|
||||
SECTION("Construct from JsonVariant") {
|
||||
DynamicJsonDocument doc1(4096);
|
||||
deserializeJson(doc1, "42");
|
||||
|
||||
StaticJsonDocument<200> doc2 = doc1.as<JsonVariant>();
|
||||
|
||||
REQUIRE_JSON(doc2, "42");
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("Assignment") {
|
||||
SECTION("Copy assignment") {
|
||||
StaticJsonDocument<200> doc1, doc2;
|
||||
doc1.to<JsonVariant>().set(666);
|
||||
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
||||
|
||||
doc2 = doc1;
|
||||
|
||||
deserializeJson(doc1, "{\"HELLO\":\"WORLD\"}");
|
||||
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
|
||||
}
|
||||
|
||||
SECTION("Assign from StaticJsonDocument of different capacity") {
|
||||
StaticJsonDocument<200> doc1;
|
||||
StaticJsonDocument<300> doc2;
|
||||
doc1.to<JsonVariant>().set(666);
|
||||
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
||||
|
||||
doc2 = doc1;
|
||||
|
||||
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
|
||||
}
|
||||
|
||||
SECTION("Assign from DynamicJsonDocument") {
|
||||
StaticJsonDocument<200> doc1;
|
||||
DynamicJsonDocument doc2(4096);
|
||||
doc1.to<JsonVariant>().set(666);
|
||||
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
||||
|
||||
doc2 = doc1;
|
||||
|
||||
deserializeJson(doc1, "{\"HELLO\":\"WORLD\"}");
|
||||
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
|
||||
}
|
||||
|
||||
SECTION("Assign from JsonArray") {
|
||||
StaticJsonDocument<200> doc1;
|
||||
DynamicJsonDocument doc2(4096);
|
||||
doc1.to<JsonVariant>().set(666);
|
||||
deserializeJson(doc1, "[\"hello\",\"world\"]");
|
||||
|
||||
doc2 = doc1.as<JsonArray>();
|
||||
|
||||
deserializeJson(doc1, "[\"HELLO\",\"WORLD\"]");
|
||||
REQUIRE_JSON(doc2, "[\"hello\",\"world\"]");
|
||||
}
|
||||
|
||||
SECTION("Assign from JsonArrayConst") {
|
||||
StaticJsonDocument<200> doc1;
|
||||
DynamicJsonDocument doc2(4096);
|
||||
doc1.to<JsonVariant>().set(666);
|
||||
deserializeJson(doc1, "[\"hello\",\"world\"]");
|
||||
|
||||
doc2 = doc1.as<JsonArrayConst>();
|
||||
|
||||
deserializeJson(doc1, "[\"HELLO\",\"WORLD\"]");
|
||||
REQUIRE_JSON(doc2, "[\"hello\",\"world\"]");
|
||||
}
|
||||
|
||||
SECTION("Assign from JsonObject") {
|
||||
StaticJsonDocument<200> doc1;
|
||||
DynamicJsonDocument doc2(4096);
|
||||
doc1.to<JsonVariant>().set(666);
|
||||
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
||||
|
||||
doc2 = doc1.as<JsonObject>();
|
||||
|
||||
deserializeJson(doc1, "{\"HELLO\":\"WORLD\"}");
|
||||
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
|
||||
}
|
||||
|
||||
SECTION("Assign from JsonObjectConst") {
|
||||
StaticJsonDocument<200> doc1;
|
||||
DynamicJsonDocument doc2(4096);
|
||||
doc1.to<JsonVariant>().set(666);
|
||||
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
||||
|
||||
doc2 = doc1.as<JsonObjectConst>();
|
||||
|
||||
deserializeJson(doc1, "{\"HELLO\":\"WORLD\"}");
|
||||
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
|
||||
}
|
||||
|
||||
SECTION("Assign from JsonVariant") {
|
||||
DynamicJsonDocument doc1(4096);
|
||||
doc1.to<JsonVariant>().set(666);
|
||||
deserializeJson(doc1, "42");
|
||||
|
||||
StaticJsonDocument<200> doc2;
|
||||
doc2 = doc1.as<JsonVariant>();
|
||||
|
||||
REQUIRE_JSON(doc2, "42");
|
||||
}
|
||||
|
||||
SECTION("Assign from JsonVariantConst") {
|
||||
DynamicJsonDocument doc1(4096);
|
||||
doc1.to<JsonVariant>().set(666);
|
||||
deserializeJson(doc1, "42");
|
||||
|
||||
StaticJsonDocument<200> doc2;
|
||||
doc2 = doc1.as<JsonVariantConst>();
|
||||
|
||||
REQUIRE_JSON(doc2, "42");
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("garbageCollect()") {
|
||||
StaticJsonDocument<256> doc;
|
||||
doc[std::string("example")] = std::string("jukebox");
|
||||
doc.remove("example");
|
||||
REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(1) + 16);
|
||||
|
||||
doc.garbageCollect();
|
||||
|
||||
REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(0));
|
||||
REQUIRE_JSON(doc, "{}");
|
||||
}
|
||||
}
|
@ -2,11 +2,17 @@
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#define ARDUINOJSON_ENABLE_ARDUINO_STRING 1
|
||||
#define ARDUINOJSON_ENABLE_PROGMEM 1
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
#include <catch.hpp>
|
||||
|
||||
using ArduinoJson::detail::sizeofArray;
|
||||
using ArduinoJson::detail::sizeofString;
|
||||
|
||||
TEST_CASE("JsonDocument::add()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
|
||||
SECTION("integer") {
|
||||
doc.add(42);
|
||||
@ -19,4 +25,37 @@ TEST_CASE("JsonDocument::add()") {
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "[\"hello\"]");
|
||||
}
|
||||
|
||||
SECTION("std::string") {
|
||||
doc.add(std::string("example"));
|
||||
doc.add(std::string("example"));
|
||||
|
||||
CHECK(doc.memoryUsage() == sizeofArray(2) + sizeofString(7));
|
||||
CHECK(doc[0].as<const char*>() == doc[1].as<const char*>());
|
||||
}
|
||||
|
||||
SECTION("char*") {
|
||||
char value[] = "example";
|
||||
doc.add(value);
|
||||
doc.add(value);
|
||||
|
||||
CHECK(doc.memoryUsage() == sizeofArray(2) + sizeofString(7));
|
||||
CHECK(doc[0].as<const char*>() == doc[1].as<const char*>());
|
||||
}
|
||||
|
||||
SECTION("Arduino String") {
|
||||
doc.add(String("example"));
|
||||
doc.add(String("example"));
|
||||
|
||||
CHECK(doc.memoryUsage() == sizeofArray(2) + sizeofString(7));
|
||||
CHECK(doc[0].as<const char*>() == doc[1].as<const char*>());
|
||||
}
|
||||
|
||||
SECTION("Flash string") {
|
||||
doc.add(F("example"));
|
||||
doc.add(F("example"));
|
||||
|
||||
CHECK(doc.memoryUsage() == sizeofArray(2) + sizeofString(7));
|
||||
CHECK(doc[0].as<const char*>() == doc[1].as<const char*>());
|
||||
}
|
||||
}
|
||||
|
138
extras/tests/JsonDocument/assignment.cpp
Normal file
138
extras/tests/JsonDocument/assignment.cpp
Normal file
@ -0,0 +1,138 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
|
||||
using ArduinoJson::detail::sizeofArray;
|
||||
using ArduinoJson::detail::sizeofObject;
|
||||
using ArduinoJson::detail::sizeofString;
|
||||
|
||||
TEST_CASE("JsonDocument assignment") {
|
||||
SpyingAllocator spyingAllocator;
|
||||
|
||||
SECTION("Copy assignment same capacity") {
|
||||
JsonDocument doc1(1024, &spyingAllocator);
|
||||
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
||||
JsonDocument doc2(1024, &spyingAllocator);
|
||||
spyingAllocator.clearLog();
|
||||
|
||||
doc2 = doc1;
|
||||
|
||||
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
|
||||
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::Allocate(sizeofString(5)) // hello
|
||||
<< AllocatorLog::Allocate(sizeofString(5)) // world
|
||||
);
|
||||
}
|
||||
|
||||
SECTION("Copy assignment reallocates when capacity is smaller") {
|
||||
JsonDocument doc1(4096, &spyingAllocator);
|
||||
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
||||
JsonDocument doc2(8, &spyingAllocator);
|
||||
spyingAllocator.clearLog();
|
||||
|
||||
doc2 = doc1;
|
||||
|
||||
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::Deallocate(8)
|
||||
<< AllocatorLog::Allocate(4096)
|
||||
<< AllocatorLog::Allocate(sizeofString(5)) // hello
|
||||
<< AllocatorLog::Allocate(sizeofString(5)) // world
|
||||
);
|
||||
}
|
||||
|
||||
SECTION("Copy assignment reallocates when capacity is larger") {
|
||||
JsonDocument doc1(1024, &spyingAllocator);
|
||||
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
||||
JsonDocument doc2(4096, &spyingAllocator);
|
||||
spyingAllocator.clearLog();
|
||||
|
||||
doc2 = doc1;
|
||||
|
||||
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::Deallocate(4096)
|
||||
<< AllocatorLog::Allocate(1024)
|
||||
<< AllocatorLog::Allocate(sizeofString(5)) // hello
|
||||
<< AllocatorLog::Allocate(sizeofString(5)) // world
|
||||
);
|
||||
}
|
||||
|
||||
SECTION("Move assign") {
|
||||
{
|
||||
JsonDocument doc1(4096, &spyingAllocator);
|
||||
doc1.set(std::string("The size of this string is 32!!"));
|
||||
JsonDocument doc2(8, &spyingAllocator);
|
||||
|
||||
doc2 = std::move(doc1);
|
||||
|
||||
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
|
||||
REQUIRE(doc1.as<std::string>() == "null");
|
||||
}
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::Allocate(4096)
|
||||
<< AllocatorLog::Allocate(sizeofString(31))
|
||||
<< AllocatorLog::Allocate(8)
|
||||
<< AllocatorLog::Deallocate(8)
|
||||
<< AllocatorLog::Deallocate(sizeofString(31))
|
||||
<< AllocatorLog::Deallocate(4096));
|
||||
}
|
||||
|
||||
SECTION("Assign from JsonObject") {
|
||||
JsonDocument doc1(200);
|
||||
JsonObject obj = doc1.to<JsonObject>();
|
||||
obj["hello"] = "world";
|
||||
|
||||
JsonDocument doc2(4096);
|
||||
doc2 = obj;
|
||||
|
||||
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
|
||||
}
|
||||
|
||||
SECTION("Assign from JsonArray") {
|
||||
JsonDocument doc1(200);
|
||||
JsonArray arr = doc1.to<JsonArray>();
|
||||
arr.add("hello");
|
||||
|
||||
JsonDocument doc2(4096);
|
||||
doc2 = arr;
|
||||
|
||||
REQUIRE(doc2.as<std::string>() == "[\"hello\"]");
|
||||
}
|
||||
|
||||
SECTION("Assign from JsonVariant") {
|
||||
JsonDocument doc1(200);
|
||||
deserializeJson(doc1, "42");
|
||||
|
||||
JsonDocument doc2(4096);
|
||||
doc2 = doc1.as<JsonVariant>();
|
||||
|
||||
REQUIRE(doc2.as<std::string>() == "42");
|
||||
}
|
||||
|
||||
SECTION("Assign from MemberProxy") {
|
||||
JsonDocument doc1(200);
|
||||
doc1["value"] = 42;
|
||||
|
||||
JsonDocument doc2(4096);
|
||||
doc2 = doc1["value"];
|
||||
|
||||
REQUIRE(doc2.as<std::string>() == "42");
|
||||
}
|
||||
|
||||
SECTION("Assign from ElementProxy") {
|
||||
JsonDocument doc1(200);
|
||||
doc1[0] = 42;
|
||||
|
||||
JsonDocument doc2(4096);
|
||||
doc2 = doc1[0];
|
||||
|
||||
REQUIRE(doc2.as<std::string>() == "42");
|
||||
}
|
||||
}
|
@ -8,7 +8,7 @@
|
||||
#include <string>
|
||||
|
||||
TEST_CASE("Implicit cast to JsonVariant") {
|
||||
StaticJsonDocument<128> doc;
|
||||
JsonDocument doc(128);
|
||||
|
||||
doc["hello"] = "world";
|
||||
|
||||
|
@ -5,99 +5,25 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("DynamicJsonDocument::operator==(const DynamicJsonDocument&)") {
|
||||
DynamicJsonDocument doc1(4096);
|
||||
DynamicJsonDocument doc2(4096);
|
||||
|
||||
SECTION("Empty") {
|
||||
REQUIRE(doc1 == doc2);
|
||||
REQUIRE_FALSE(doc1 != doc2);
|
||||
}
|
||||
|
||||
SECTION("With same object") {
|
||||
doc1["hello"] = "world";
|
||||
doc2["hello"] = "world";
|
||||
REQUIRE(doc1 == doc2);
|
||||
REQUIRE_FALSE(doc1 != doc2);
|
||||
}
|
||||
SECTION("With different object") {
|
||||
doc1["hello"] = "world";
|
||||
doc2["world"] = "hello";
|
||||
REQUIRE_FALSE(doc1 == doc2);
|
||||
REQUIRE(doc1 != doc2);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("DynamicJsonDocument::operator==(const StaticJsonDocument&)") {
|
||||
DynamicJsonDocument doc1(4096);
|
||||
StaticJsonDocument<256> doc2;
|
||||
|
||||
SECTION("Empty") {
|
||||
REQUIRE(doc1 == doc2);
|
||||
REQUIRE_FALSE(doc1 != doc2);
|
||||
}
|
||||
|
||||
SECTION("With same object") {
|
||||
doc1["hello"] = "world";
|
||||
doc2["hello"] = "world";
|
||||
REQUIRE(doc1 == doc2);
|
||||
REQUIRE_FALSE(doc1 != doc2);
|
||||
}
|
||||
|
||||
SECTION("With different object") {
|
||||
doc1["hello"] = "world";
|
||||
doc2["world"] = "hello";
|
||||
REQUIRE_FALSE(doc1 == doc2);
|
||||
REQUIRE(doc1 != doc2);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("StaticJsonDocument::operator==(const DynamicJsonDocument&)") {
|
||||
StaticJsonDocument<256> doc1;
|
||||
DynamicJsonDocument doc2(4096);
|
||||
|
||||
SECTION("Empty") {
|
||||
REQUIRE(doc1 == doc2);
|
||||
REQUIRE_FALSE(doc1 != doc2);
|
||||
}
|
||||
|
||||
SECTION("With same object") {
|
||||
doc1["hello"] = "world";
|
||||
doc2["hello"] = "world";
|
||||
REQUIRE(doc1 == doc2);
|
||||
REQUIRE_FALSE(doc1 != doc2);
|
||||
}
|
||||
|
||||
SECTION("With different object") {
|
||||
doc1["hello"] = "world";
|
||||
doc2["world"] = "hello";
|
||||
REQUIRE_FALSE(doc1 == doc2);
|
||||
REQUIRE(doc1 != doc2);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("JsonDocument::operator==(const JsonDocument&)") {
|
||||
StaticJsonDocument<256> doc1;
|
||||
StaticJsonDocument<256> doc2;
|
||||
const JsonDocument& ref1 = doc1;
|
||||
const JsonDocument& ref2 = doc2;
|
||||
JsonDocument doc1(4096);
|
||||
JsonDocument doc2(4096);
|
||||
|
||||
SECTION("Empty") {
|
||||
REQUIRE(ref1 == ref2);
|
||||
REQUIRE_FALSE(ref1 != ref2);
|
||||
REQUIRE(doc1 == doc2);
|
||||
REQUIRE_FALSE(doc1 != doc2);
|
||||
}
|
||||
|
||||
SECTION("With same object") {
|
||||
doc1["hello"] = "world";
|
||||
doc2["hello"] = "world";
|
||||
REQUIRE(ref1 == ref2);
|
||||
REQUIRE_FALSE(ref1 != ref2);
|
||||
REQUIRE(doc1 == doc2);
|
||||
REQUIRE_FALSE(doc1 != doc2);
|
||||
}
|
||||
|
||||
SECTION("With different object") {
|
||||
doc1["hello"] = "world";
|
||||
doc2["world"] = "hello";
|
||||
REQUIRE_FALSE(ref1 == ref2);
|
||||
REQUIRE(ref1 != ref2);
|
||||
REQUIRE_FALSE(doc1 == doc2);
|
||||
REQUIRE(doc1 != doc2);
|
||||
}
|
||||
}
|
||||
|
97
extras/tests/JsonDocument/constructor.cpp
Normal file
97
extras/tests/JsonDocument/constructor.cpp
Normal file
@ -0,0 +1,97 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
|
||||
using ArduinoJson::detail::addPadding;
|
||||
using ArduinoJson::detail::sizeofString;
|
||||
|
||||
TEST_CASE("JsonDocument constructor") {
|
||||
SpyingAllocator spyingAllocator;
|
||||
|
||||
SECTION("JsonDocument(size_t)") {
|
||||
{ JsonDocument doc(4096, &spyingAllocator); }
|
||||
REQUIRE(spyingAllocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(4096)
|
||||
<< AllocatorLog::Deallocate(4096));
|
||||
}
|
||||
|
||||
SECTION("JsonDocument(const JsonDocument&)") {
|
||||
{
|
||||
JsonDocument doc1(4096, &spyingAllocator);
|
||||
doc1.set(std::string("The size of this string is 32!!"));
|
||||
|
||||
JsonDocument doc2(doc1);
|
||||
|
||||
REQUIRE(doc1.as<std::string>() == "The size of this string is 32!!");
|
||||
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
|
||||
}
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::Allocate(4096)
|
||||
<< AllocatorLog::Allocate(sizeofString(31))
|
||||
<< AllocatorLog::Allocate(4096)
|
||||
<< AllocatorLog::Allocate(sizeofString(31))
|
||||
<< AllocatorLog::Deallocate(sizeofString(31))
|
||||
<< AllocatorLog::Deallocate(4096)
|
||||
<< AllocatorLog::Deallocate(sizeofString(31))
|
||||
<< AllocatorLog::Deallocate(4096));
|
||||
}
|
||||
|
||||
SECTION("JsonDocument(JsonDocument&&)") {
|
||||
{
|
||||
JsonDocument doc1(4096, &spyingAllocator);
|
||||
doc1.set(std::string("The size of this string is 32!!"));
|
||||
|
||||
JsonDocument doc2(std::move(doc1));
|
||||
|
||||
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
|
||||
REQUIRE(doc1.as<std::string>() == "null");
|
||||
}
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::Allocate(4096)
|
||||
<< AllocatorLog::Allocate(sizeofString(31))
|
||||
<< AllocatorLog::Deallocate(sizeofString(31))
|
||||
<< AllocatorLog::Deallocate(4096));
|
||||
}
|
||||
|
||||
SECTION("JsonDocument(JsonObject)") {
|
||||
JsonDocument doc1(200);
|
||||
JsonObject obj = doc1.to<JsonObject>();
|
||||
obj["hello"] = "world";
|
||||
|
||||
JsonDocument doc2(obj, &spyingAllocator);
|
||||
|
||||
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
|
||||
REQUIRE(spyingAllocator.log() == AllocatorLog() << AllocatorLog::Allocate(
|
||||
addPadding(doc1.memoryUsage())));
|
||||
}
|
||||
|
||||
SECTION("Construct from JsonArray") {
|
||||
JsonDocument doc1(200);
|
||||
JsonArray arr = doc1.to<JsonArray>();
|
||||
arr.add("hello");
|
||||
|
||||
JsonDocument doc2(arr, &spyingAllocator);
|
||||
|
||||
REQUIRE(doc2.as<std::string>() == "[\"hello\"]");
|
||||
REQUIRE(spyingAllocator.log() == AllocatorLog() << AllocatorLog::Allocate(
|
||||
addPadding(doc1.memoryUsage())));
|
||||
}
|
||||
|
||||
SECTION("Construct from JsonVariant") {
|
||||
JsonDocument doc1(200);
|
||||
deserializeJson(doc1, "\"hello\"");
|
||||
|
||||
JsonDocument doc2(doc1.as<JsonVariant>(), &spyingAllocator);
|
||||
|
||||
REQUIRE(doc2.as<std::string>() == "hello");
|
||||
REQUIRE(
|
||||
spyingAllocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::Allocate(addPadding(doc1.memoryUsage()))
|
||||
<< AllocatorLog::Allocate(sizeofString(5)));
|
||||
}
|
||||
}
|
@ -6,7 +6,7 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonDocument::containsKey()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
|
||||
SECTION("returns true on object") {
|
||||
doc["hello"] = "world";
|
||||
|
@ -6,7 +6,7 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonDocument::createNestedArray()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
|
||||
SECTION("promotes to array") {
|
||||
doc.createNestedArray();
|
||||
@ -16,7 +16,7 @@ TEST_CASE("JsonDocument::createNestedArray()") {
|
||||
}
|
||||
|
||||
TEST_CASE("JsonDocument::createNestedArray(key)") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
|
||||
SECTION("key is const char*") {
|
||||
SECTION("promotes to object") {
|
||||
@ -36,7 +36,7 @@ TEST_CASE("JsonDocument::createNestedArray(key)") {
|
||||
}
|
||||
|
||||
TEST_CASE("JsonDocument::createNestedObject()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
|
||||
SECTION("promotes to array") {
|
||||
doc.createNestedObject();
|
||||
@ -46,7 +46,7 @@ TEST_CASE("JsonDocument::createNestedObject()") {
|
||||
}
|
||||
|
||||
TEST_CASE("JsonDocument::createNestedObject(key)") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
|
||||
SECTION("key is const char*") {
|
||||
SECTION("promotes to object") {
|
||||
|
54
extras/tests/JsonDocument/garbageCollect.cpp
Normal file
54
extras/tests/JsonDocument/garbageCollect.cpp
Normal file
@ -0,0 +1,54 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <stdlib.h> // malloc, free
|
||||
#include <catch.hpp>
|
||||
#include <utility>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
|
||||
using ArduinoJson::detail::sizeofObject;
|
||||
using ArduinoJson::detail::sizeofString;
|
||||
|
||||
TEST_CASE("JsonDocument::garbageCollect()") {
|
||||
ControllableAllocator controllableAllocator;
|
||||
SpyingAllocator spyingAllocator(&controllableAllocator);
|
||||
JsonDocument doc(4096, &spyingAllocator);
|
||||
|
||||
SECTION("when allocation succeeds") {
|
||||
deserializeJson(doc, "{\"blanket\":1,\"dancing\":2}");
|
||||
REQUIRE(doc.memoryUsage() == sizeofObject(2) + 2 * sizeofString(7));
|
||||
doc.remove("blanket");
|
||||
spyingAllocator.clearLog();
|
||||
|
||||
bool result = doc.garbageCollect();
|
||||
|
||||
REQUIRE(result == true);
|
||||
REQUIRE(doc.memoryUsage() == sizeofObject(1) + sizeofString(7));
|
||||
REQUIRE(doc.as<std::string>() == "{\"dancing\":2}");
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::Allocate(4096)
|
||||
<< AllocatorLog::Allocate(sizeofString(7))
|
||||
<< AllocatorLog::Deallocate(sizeofString(7))
|
||||
<< AllocatorLog::Deallocate(4096));
|
||||
}
|
||||
|
||||
SECTION("when allocation fails") {
|
||||
deserializeJson(doc, "{\"blanket\":1,\"dancing\":2}");
|
||||
REQUIRE(doc.memoryUsage() == sizeofObject(2) + 2 * sizeofString(7));
|
||||
doc.remove("blanket");
|
||||
controllableAllocator.disable();
|
||||
spyingAllocator.clearLog();
|
||||
|
||||
bool result = doc.garbageCollect();
|
||||
|
||||
REQUIRE(result == false);
|
||||
REQUIRE(doc.memoryUsage() == sizeofObject(2) + sizeofString(7));
|
||||
REQUIRE(doc.as<std::string>() == "{\"dancing\":2}");
|
||||
|
||||
REQUIRE(spyingAllocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::AllocateFail(4096));
|
||||
}
|
||||
}
|
@ -6,7 +6,7 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonDocument::isNull()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
|
||||
SECTION("returns true if uninitialized") {
|
||||
REQUIRE(doc.isNull() == true);
|
||||
|
@ -3,7 +3,7 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("Issue #1120") {
|
||||
StaticJsonDocument<500> doc;
|
||||
JsonDocument doc(500);
|
||||
constexpr char str[] =
|
||||
"{\"contents\":[{\"module\":\"Packet\"},{\"module\":\"Analog\"}]}";
|
||||
deserializeJson(doc, str);
|
||||
|
52
extras/tests/JsonDocument/memoryUsage.cpp
Normal file
52
extras/tests/JsonDocument/memoryUsage.cpp
Normal file
@ -0,0 +1,52 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
using ArduinoJson::detail::sizeofArray;
|
||||
using ArduinoJson::detail::sizeofObject;
|
||||
|
||||
TEST_CASE("JsonDocument::memoryUsage()") {
|
||||
JsonDocument doc(4096);
|
||||
|
||||
SECTION("starts at zero") {
|
||||
REQUIRE(doc.memoryUsage() == 0);
|
||||
}
|
||||
|
||||
SECTION("sizeofArray(0)") {
|
||||
doc.to<JsonArray>();
|
||||
REQUIRE(doc.memoryUsage() == sizeofArray(0));
|
||||
}
|
||||
|
||||
SECTION("sizeofArray(1)") {
|
||||
doc.to<JsonArray>().add(42);
|
||||
REQUIRE(doc.memoryUsage() == sizeofArray(1));
|
||||
}
|
||||
|
||||
SECTION("sizeofArray(1) + sizeofArray(0)") {
|
||||
doc.to<JsonArray>().createNestedArray();
|
||||
REQUIRE(doc.memoryUsage() == sizeofArray(1) + sizeofArray(0));
|
||||
}
|
||||
|
||||
SECTION("Increases after adding value to array") {
|
||||
JsonArray arr = doc.to<JsonArray>();
|
||||
|
||||
REQUIRE(doc.memoryUsage() == sizeofArray(0));
|
||||
arr.add(42);
|
||||
REQUIRE(doc.memoryUsage() == sizeofArray(1));
|
||||
arr.add(43);
|
||||
REQUIRE(doc.memoryUsage() == sizeofArray(2));
|
||||
}
|
||||
|
||||
SECTION("Increases after adding value to object") {
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
|
||||
REQUIRE(doc.memoryUsage() == sizeofObject(0));
|
||||
obj["a"] = 1;
|
||||
REQUIRE(doc.memoryUsage() == sizeofObject(1));
|
||||
obj["b"] = 2;
|
||||
REQUIRE(doc.memoryUsage() == sizeofObject(2));
|
||||
}
|
||||
}
|
@ -6,7 +6,7 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonDocument::nesting()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
|
||||
SECTION("return 0 if uninitialized") {
|
||||
REQUIRE(doc.nesting() == 0);
|
||||
|
@ -5,70 +5,76 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
|
||||
using ArduinoJson::detail::sizeofArray;
|
||||
|
||||
TEST_CASE("JsonDocument::overflowed()") {
|
||||
SECTION("returns false on a fresh object") {
|
||||
StaticJsonDocument<0> doc;
|
||||
JsonDocument doc(0);
|
||||
CHECK(doc.overflowed() == false);
|
||||
}
|
||||
|
||||
SECTION("returns true after a failed insertion") {
|
||||
StaticJsonDocument<0> doc;
|
||||
JsonDocument doc(0);
|
||||
doc.add(0);
|
||||
CHECK(doc.overflowed() == true);
|
||||
}
|
||||
|
||||
SECTION("returns false after successful insertion") {
|
||||
StaticJsonDocument<JSON_ARRAY_SIZE(1)> doc;
|
||||
JsonDocument doc(sizeofArray(1));
|
||||
doc.add(0);
|
||||
CHECK(doc.overflowed() == false);
|
||||
}
|
||||
|
||||
SECTION("returns true after a failed string copy") {
|
||||
StaticJsonDocument<JSON_ARRAY_SIZE(1)> doc;
|
||||
ControllableAllocator allocator;
|
||||
JsonDocument doc(sizeofArray(1), &allocator);
|
||||
allocator.disable();
|
||||
doc.add(std::string("example"));
|
||||
CHECK(doc.overflowed() == true);
|
||||
}
|
||||
|
||||
SECTION("returns false after a successful string copy") {
|
||||
StaticJsonDocument<JSON_ARRAY_SIZE(1) + 8> doc;
|
||||
JsonDocument doc(sizeofArray(1));
|
||||
doc.add(std::string("example"));
|
||||
CHECK(doc.overflowed() == false);
|
||||
}
|
||||
|
||||
SECTION("returns true after a failed member add") {
|
||||
StaticJsonDocument<1> doc;
|
||||
JsonDocument doc(1);
|
||||
doc["example"] = true;
|
||||
CHECK(doc.overflowed() == true);
|
||||
}
|
||||
|
||||
SECTION("returns true after a failed deserialization") {
|
||||
StaticJsonDocument<JSON_ARRAY_SIZE(1)> doc;
|
||||
deserializeJson(doc, "[\"example\"]");
|
||||
JsonDocument doc(sizeofArray(1));
|
||||
deserializeJson(doc, "[1, 2]");
|
||||
CHECK(doc.overflowed() == true);
|
||||
}
|
||||
|
||||
SECTION("returns false after a successful deserialization") {
|
||||
StaticJsonDocument<JSON_ARRAY_SIZE(1) + 8> doc;
|
||||
JsonDocument doc(sizeofArray(1));
|
||||
deserializeJson(doc, "[\"example\"]");
|
||||
CHECK(doc.overflowed() == false);
|
||||
}
|
||||
|
||||
SECTION("returns false after clear()") {
|
||||
StaticJsonDocument<0> doc;
|
||||
JsonDocument doc(0);
|
||||
doc.add(0);
|
||||
doc.clear();
|
||||
CHECK(doc.overflowed() == false);
|
||||
}
|
||||
|
||||
SECTION("remains false after shrinkToFit()") {
|
||||
DynamicJsonDocument doc(JSON_ARRAY_SIZE(1));
|
||||
JsonDocument doc(sizeofArray(1));
|
||||
doc.add(0);
|
||||
doc.shrinkToFit();
|
||||
CHECK(doc.overflowed() == false);
|
||||
}
|
||||
|
||||
SECTION("remains true after shrinkToFit()") {
|
||||
DynamicJsonDocument doc(JSON_ARRAY_SIZE(1));
|
||||
JsonDocument doc(sizeofArray(1));
|
||||
doc.add(0);
|
||||
doc.add(0);
|
||||
doc.shrinkToFit();
|
||||
@ -76,7 +82,7 @@ TEST_CASE("JsonDocument::overflowed()") {
|
||||
}
|
||||
|
||||
SECTION("return false after garbageCollect()") {
|
||||
DynamicJsonDocument doc(JSON_ARRAY_SIZE(1));
|
||||
JsonDocument doc(sizeofArray(1));
|
||||
doc.add(0);
|
||||
doc.add(0);
|
||||
doc.garbageCollect();
|
||||
|
@ -6,7 +6,7 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonDocument::remove()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
|
||||
SECTION("remove(int)") {
|
||||
doc.add(1);
|
||||
|
@ -8,145 +8,173 @@
|
||||
#include <stdlib.h> // malloc, free
|
||||
#include <string>
|
||||
|
||||
class ArmoredAllocator {
|
||||
#include "Allocators.hpp"
|
||||
|
||||
using ArduinoJson::detail::sizeofArray;
|
||||
using ArduinoJson::detail::sizeofObject;
|
||||
using ArduinoJson::detail::sizeofString;
|
||||
|
||||
class ArmoredAllocator : public Allocator {
|
||||
public:
|
||||
ArmoredAllocator() : _ptr(0), _size(0) {}
|
||||
virtual ~ArmoredAllocator() {}
|
||||
|
||||
void* allocate(size_t size) {
|
||||
_ptr = malloc(size);
|
||||
_size = size;
|
||||
return _ptr;
|
||||
void* allocate(size_t size) override {
|
||||
return malloc(size);
|
||||
}
|
||||
|
||||
void deallocate(void* ptr) {
|
||||
REQUIRE(ptr == _ptr);
|
||||
void deallocate(void* ptr) override {
|
||||
free(ptr);
|
||||
_ptr = 0;
|
||||
_size = 0;
|
||||
}
|
||||
|
||||
void* reallocate(void* ptr, size_t new_size) {
|
||||
REQUIRE(ptr == _ptr);
|
||||
void* reallocate(void* ptr, size_t new_size) override {
|
||||
// don't call realloc, instead alloc a new buffer and erase the old one
|
||||
// this way we make sure we support relocation
|
||||
void* new_ptr = malloc(new_size);
|
||||
memcpy(new_ptr, _ptr, std::min(new_size, _size));
|
||||
memset(_ptr, '#', _size); // erase
|
||||
free(_ptr);
|
||||
_ptr = new_ptr;
|
||||
memset(new_ptr, '#', new_size); // erase
|
||||
memcpy(new_ptr, ptr, std::min(new_size, new_size));
|
||||
free(ptr);
|
||||
return new_ptr;
|
||||
}
|
||||
|
||||
private:
|
||||
void* _ptr;
|
||||
size_t _size;
|
||||
};
|
||||
|
||||
typedef BasicJsonDocument<ArmoredAllocator> ShrinkToFitTestDocument;
|
||||
|
||||
void testShrinkToFit(ShrinkToFitTestDocument& doc, std::string expected_json,
|
||||
size_t expected_size) {
|
||||
// test twice: shrinkToFit() should be idempotent
|
||||
for (int i = 0; i < 2; i++) {
|
||||
doc.shrinkToFit();
|
||||
|
||||
REQUIRE(doc.capacity() == expected_size);
|
||||
REQUIRE(doc.memoryUsage() == expected_size);
|
||||
|
||||
std::string json;
|
||||
serializeJson(doc, json);
|
||||
REQUIRE(json == expected_json);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("BasicJsonDocument::shrinkToFit()") {
|
||||
ShrinkToFitTestDocument doc(4096);
|
||||
TEST_CASE("JsonDocument::shrinkToFit()") {
|
||||
ArmoredAllocator armoredAllocator;
|
||||
SpyingAllocator spyingAllocator(&armoredAllocator);
|
||||
JsonDocument doc(4096, &spyingAllocator);
|
||||
|
||||
SECTION("null") {
|
||||
testShrinkToFit(doc, "null", 0);
|
||||
doc.shrinkToFit();
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "null");
|
||||
REQUIRE(spyingAllocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(4096)
|
||||
<< AllocatorLog::Reallocate(4096, 0));
|
||||
}
|
||||
|
||||
SECTION("empty object") {
|
||||
deserializeJson(doc, "{}");
|
||||
testShrinkToFit(doc, "{}", JSON_OBJECT_SIZE(0));
|
||||
|
||||
doc.shrinkToFit();
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "{}");
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::Allocate(4096)
|
||||
<< AllocatorLog::Reallocate(4096, sizeofObject(0)));
|
||||
}
|
||||
|
||||
SECTION("empty array") {
|
||||
deserializeJson(doc, "[]");
|
||||
testShrinkToFit(doc, "[]", JSON_ARRAY_SIZE(0));
|
||||
|
||||
doc.shrinkToFit();
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "[]");
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::Allocate(4096)
|
||||
<< AllocatorLog::Reallocate(4096, sizeofArray(0)));
|
||||
}
|
||||
|
||||
SECTION("linked string") {
|
||||
doc.set("hello");
|
||||
testShrinkToFit(doc, "\"hello\"", 0);
|
||||
|
||||
doc.shrinkToFit();
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "hello");
|
||||
REQUIRE(spyingAllocator.log() == AllocatorLog()
|
||||
<< AllocatorLog::Allocate(4096)
|
||||
<< AllocatorLog::Reallocate(4096, 0));
|
||||
}
|
||||
|
||||
SECTION("owned string") {
|
||||
doc.set(std::string("abcdefg"));
|
||||
testShrinkToFit(doc, "\"abcdefg\"", 8);
|
||||
REQUIRE(doc.as<std::string>() == "abcdefg");
|
||||
|
||||
doc.shrinkToFit();
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "abcdefg");
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::Allocate(4096)
|
||||
<< AllocatorLog::Allocate(sizeofString(7))
|
||||
<< AllocatorLog::Reallocate(4096, 0));
|
||||
}
|
||||
|
||||
SECTION("linked raw") {
|
||||
doc.set(serialized("[{},123]"));
|
||||
testShrinkToFit(doc, "[{},123]", 0);
|
||||
}
|
||||
SECTION("raw string") {
|
||||
doc.set(serialized("[{},12]"));
|
||||
|
||||
SECTION("owned raw") {
|
||||
doc.set(serialized(std::string("[{},12]")));
|
||||
testShrinkToFit(doc, "[{},12]", 8);
|
||||
doc.shrinkToFit();
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "[{},12]");
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::Allocate(4096)
|
||||
<< AllocatorLog::Allocate(sizeofString(7))
|
||||
<< AllocatorLog::Reallocate(4096, 0));
|
||||
}
|
||||
|
||||
SECTION("linked key") {
|
||||
doc["key"] = 42;
|
||||
testShrinkToFit(doc, "{\"key\":42}", JSON_OBJECT_SIZE(1));
|
||||
|
||||
doc.shrinkToFit();
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "{\"key\":42}");
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::Allocate(4096)
|
||||
<< AllocatorLog::Reallocate(4096, sizeofObject(1)));
|
||||
}
|
||||
|
||||
SECTION("owned key") {
|
||||
doc[std::string("abcdefg")] = 42;
|
||||
testShrinkToFit(doc, "{\"abcdefg\":42}", JSON_OBJECT_SIZE(1) + 8);
|
||||
|
||||
doc.shrinkToFit();
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "{\"abcdefg\":42}");
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::Allocate(4096)
|
||||
<< AllocatorLog::Allocate(sizeofString(7))
|
||||
<< AllocatorLog::Reallocate(4096, sizeofObject(1)));
|
||||
}
|
||||
|
||||
SECTION("linked string in array") {
|
||||
doc.add("hello");
|
||||
testShrinkToFit(doc, "[\"hello\"]", JSON_ARRAY_SIZE(1));
|
||||
|
||||
doc.shrinkToFit();
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "[\"hello\"]");
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::Allocate(4096)
|
||||
<< AllocatorLog::Reallocate(4096, sizeofArray(1)));
|
||||
}
|
||||
|
||||
SECTION("owned string in array") {
|
||||
doc.add(std::string("abcdefg"));
|
||||
testShrinkToFit(doc, "[\"abcdefg\"]", JSON_ARRAY_SIZE(1) + 8);
|
||||
|
||||
doc.shrinkToFit();
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "[\"abcdefg\"]");
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::Allocate(4096)
|
||||
<< AllocatorLog::Allocate(sizeofString(7))
|
||||
<< AllocatorLog::Reallocate(4096, sizeofArray(1)));
|
||||
}
|
||||
|
||||
SECTION("linked string in object") {
|
||||
doc["key"] = "hello";
|
||||
testShrinkToFit(doc, "{\"key\":\"hello\"}", JSON_OBJECT_SIZE(1));
|
||||
|
||||
doc.shrinkToFit();
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "{\"key\":\"hello\"}");
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::Allocate(4096)
|
||||
<< AllocatorLog::Reallocate(4096, sizeofObject(1)));
|
||||
}
|
||||
|
||||
SECTION("owned string in object") {
|
||||
doc["key"] = std::string("abcdefg");
|
||||
testShrinkToFit(doc, "{\"key\":\"abcdefg\"}", JSON_ARRAY_SIZE(1) + 8);
|
||||
}
|
||||
|
||||
SECTION("unaligned") {
|
||||
doc.add(std::string("?")); // two bytes in the string pool
|
||||
REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(1) + 2);
|
||||
|
||||
doc.shrinkToFit();
|
||||
|
||||
// the new capacity should be padded to align the pointers
|
||||
REQUIRE(doc.capacity() == JSON_OBJECT_SIZE(1) + sizeof(void*));
|
||||
REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(1) + 2);
|
||||
REQUIRE(doc[0] == "?");
|
||||
REQUIRE(doc.as<std::string>() == "{\"key\":\"abcdefg\"}");
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::Allocate(4096)
|
||||
<< AllocatorLog::Allocate(sizeofString(7))
|
||||
<< AllocatorLog::Reallocate(4096, sizeofObject(1)));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("DynamicJsonDocument::shrinkToFit()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
|
||||
deserializeJson(doc, "{\"hello\":[\"world\"]");
|
||||
|
||||
doc.shrinkToFit();
|
||||
|
||||
std::string json;
|
||||
serializeJson(doc, json);
|
||||
REQUIRE(json == "{\"hello\":[\"world\"]}");
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonDocument::size()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
|
||||
SECTION("returns 0") {
|
||||
REQUIRE(doc.size() == 0);
|
||||
|
@ -6,7 +6,7 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonDocument::operator[]") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
const JsonDocument& cdoc = doc;
|
||||
|
||||
SECTION("object") {
|
||||
@ -37,7 +37,7 @@ TEST_CASE("JsonDocument::operator[]") {
|
||||
}
|
||||
|
||||
TEST_CASE("JsonDocument automatically promotes to object") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
|
||||
doc["one"]["two"]["three"] = 4;
|
||||
|
||||
@ -45,7 +45,7 @@ TEST_CASE("JsonDocument automatically promotes to object") {
|
||||
}
|
||||
|
||||
TEST_CASE("JsonDocument automatically promotes to array") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonDocument doc(4096);
|
||||
|
||||
doc[2] = 2;
|
||||
|
||||
|
@ -7,21 +7,19 @@
|
||||
using namespace std;
|
||||
|
||||
TEST_CASE("std::swap") {
|
||||
SECTION("DynamicJsonDocument*") {
|
||||
DynamicJsonDocument *p1, *p2;
|
||||
SECTION("JsonDocument*") {
|
||||
JsonDocument *p1, *p2;
|
||||
swap(p1, p2); // issue #1678
|
||||
}
|
||||
|
||||
SECTION("DynamicJsonDocument") {
|
||||
DynamicJsonDocument doc1(0x10), doc2(0x20);
|
||||
SECTION("JsonDocument") {
|
||||
JsonDocument doc1(0x10), doc2(0x20);
|
||||
doc1.set("hello");
|
||||
doc2.set("world");
|
||||
|
||||
swap(doc1, doc2);
|
||||
|
||||
CHECK(doc1.capacity() == 0x20);
|
||||
CHECK(doc1.as<string>() == "world");
|
||||
CHECK(doc2.capacity() == 0x10);
|
||||
CHECK(doc2.as<string>() == "hello");
|
||||
}
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ TEST_CASE("JsonObject::clear()") {
|
||||
}
|
||||
|
||||
SECTION("Removes all elements") {
|
||||
StaticJsonDocument<64> doc;
|
||||
JsonDocument doc(64);
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
obj["hello"] = 1;
|
||||
obj["world"] = 2;
|
||||
|
@ -6,7 +6,7 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("Compare JsonObject with JsonObject") {
|
||||
StaticJsonDocument<512> doc;
|
||||
JsonDocument doc(512);
|
||||
|
||||
SECTION("Compare with unbound") {
|
||||
JsonObject object = doc.to<JsonObject>();
|
||||
@ -82,7 +82,7 @@ TEST_CASE("Compare JsonObject with JsonObject") {
|
||||
}
|
||||
|
||||
TEST_CASE("Compare JsonObject with JsonVariant") {
|
||||
StaticJsonDocument<512> doc;
|
||||
JsonDocument doc(512);
|
||||
|
||||
SECTION("Compare with self") {
|
||||
JsonObject object = doc.to<JsonObject>();
|
||||
@ -153,7 +153,7 @@ TEST_CASE("Compare JsonObject with JsonVariant") {
|
||||
}
|
||||
|
||||
TEST_CASE("Compare JsonObject with JsonVariantConst") {
|
||||
StaticJsonDocument<512> doc;
|
||||
JsonDocument doc(512);
|
||||
|
||||
SECTION("Compare with unbound") {
|
||||
JsonObject object = doc.to<JsonObject>();
|
||||
@ -247,7 +247,7 @@ TEST_CASE("Compare JsonObject with JsonVariantConst") {
|
||||
}
|
||||
|
||||
TEST_CASE("Compare JsonObject with JsonObjectConst") {
|
||||
StaticJsonDocument<512> doc;
|
||||
JsonDocument doc(512);
|
||||
|
||||
SECTION("Compare with unbound") {
|
||||
JsonObject object = doc.to<JsonObject>();
|
||||
@ -347,7 +347,7 @@ TEST_CASE("Compare JsonObject with JsonObjectConst") {
|
||||
}
|
||||
|
||||
TEST_CASE("Compare JsonObjectConst with JsonObjectConst") {
|
||||
StaticJsonDocument<512> doc;
|
||||
JsonDocument doc(512);
|
||||
|
||||
SECTION("Compare with unbound") {
|
||||
JsonObject object = doc.to<JsonObject>();
|
||||
@ -430,7 +430,7 @@ TEST_CASE("Compare JsonObjectConst with JsonObjectConst") {
|
||||
}
|
||||
|
||||
TEST_CASE("Compare JsonObjectConst with JsonVariant") {
|
||||
StaticJsonDocument<512> doc;
|
||||
JsonDocument doc(512);
|
||||
|
||||
SECTION("Compare with self") {
|
||||
JsonObject object = doc.to<JsonObject>();
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user