| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831 |
- // ArduinoJson - https://arduinojson.org
- // Copyright © 2014-2025, Benoit BLANCHON
- // MIT License
- #define ARDUINOJSON_ENABLE_COMMENTS 1
- #include <ArduinoJson.h>
- #include <catch.hpp>
- #include <sstream>
- #include <string>
- #include "Allocators.hpp"
- #include "Literals.hpp"
- using ArduinoJson::detail::sizeofArray;
- using ArduinoJson::detail::sizeofObject;
- TEST_CASE("Filtering") {
- struct TestCase {
- const char* description;
- const char* input;
- const char* filter;
- uint8_t nestingLimit;
- DeserializationError error;
- const char* output;
- size_t memoryUsage;
- };
- TestCase testCases[] = {
- {
- "Input is object, filter is null", // description
- "{\"hello\":\"world\"}", // input
- "null", // filter
- 10, // nestingLimit
- DeserializationError::Ok, // error
- "null", // output
- 0, // memoryUsage
- },
- {
- "Input is object, filter is false",
- "{\"hello\":\"world\"}",
- "false",
- 10,
- DeserializationError::Ok,
- "null",
- 0,
- },
- {
- "Input is object, filter is true",
- "{\"abcdefg\":\"hijklmn\"}",
- "true",
- 10,
- DeserializationError::Ok,
- "{\"abcdefg\":\"hijklmn\"}",
- sizeofObject(1) + sizeofString("abcdefg") + sizeofString("hijklmn"),
- },
- {
- "Input is object, filter is empty object",
- "{\"hello\":\"world\"}",
- "{}",
- 10,
- DeserializationError::Ok,
- "{}",
- sizeofObject(0),
- },
- {
- "Input in an object, but filter wants an array",
- "{\"hello\":\"world\"}",
- "[]",
- 10,
- DeserializationError::Ok,
- "null",
- 0,
- },
- {
- "Member is a string, but filter wants an array",
- "{\"example\":\"example\"}",
- "{\"example\":[true]}",
- 10,
- DeserializationError::Ok,
- "{\"example\":null}",
- sizeofObject(1) + sizeofString("example"),
- },
- {
- "Member is a number, but filter wants an array",
- "{\"example\":42}",
- "{\"example\":[true]}",
- 10,
- DeserializationError::Ok,
- "{\"example\":null}",
- sizeofObject(1) + sizeofString("example"),
- },
- {
- "Input is an array, but filter wants an object",
- "[\"hello\",\"world\"]",
- "{}",
- 10,
- DeserializationError::Ok,
- "null",
- 0,
- },
- {
- "Input is a bool, but filter wants an object",
- "true",
- "{}",
- 10,
- DeserializationError::Ok,
- "null",
- 0,
- },
- {
- "Input is a string, but filter wants an object",
- "\"hello\"",
- "{}",
- 10,
- DeserializationError::Ok,
- "null",
- 0,
- },
- {
- "Skip an integer",
- "{\"an_integer\":666,example:42}",
- "{\"example\":true}",
- 10,
- DeserializationError::Ok,
- "{\"example\":42}",
- sizeofObject(1) + sizeofString("example"),
- },
- {
- "Skip a float",
- "{\"a_float\":12.34e-6,example:42}",
- "{\"example\":true}",
- 10,
- DeserializationError::Ok,
- "{\"example\":42}",
- sizeofObject(1) + sizeofString("example"),
- },
- {
- "Skip false",
- "{\"a_bool\":false,example:42}",
- "{\"example\":true}",
- 10,
- DeserializationError::Ok,
- "{\"example\":42}",
- sizeofObject(1) + sizeofString("example"),
- },
- {
- "Skip true",
- "{\"a_bool\":true,example:42}",
- "{\"example\":true}",
- 10,
- DeserializationError::Ok,
- "{\"example\":42}",
- sizeofObject(1) + sizeofString("example"),
- },
- {
- "Skip null",
- "{\"a_bool\":null,example:42}",
- "{\"example\":true}",
- 10,
- DeserializationError::Ok,
- "{\"example\":42}",
- sizeofObject(1) + sizeofString("example"),
- },
- {
- "Skip a double-quoted string",
- "{\"a_double_quoted_string\":\"hello\",example:42}",
- "{\"example\":true}",
- 10,
- DeserializationError::Ok,
- "{\"example\":42}",
- sizeofObject(1) + sizeofString("example"),
- },
- {
- "Skip a single-quoted string",
- "{\"a_single_quoted_string\":'hello',example:42}",
- "{\"example\":true}",
- 10,
- DeserializationError::Ok,
- "{\"example\":42}",
- sizeofObject(1) + sizeofString("example"),
- },
- {
- "Skip an empty array",
- "{\"an_empty_array\":[],example:42}",
- "{\"example\":true}",
- 10,
- DeserializationError::Ok,
- "{\"example\":42}",
- sizeofObject(1) + sizeofString("example"),
- },
- {
- "Skip an empty array with spaces in it",
- "{\"an_empty_array\":[\t],example:42}",
- "{\"example\":true}",
- 10,
- DeserializationError::Ok,
- "{\"example\":42}",
- sizeofObject(1) + sizeofString("example"),
- },
- {
- "Skip an array",
- "{\"an_array\":[1,2,3],example:42}",
- "{\"example\":true}",
- 10,
- DeserializationError::Ok,
- "{\"example\":42}",
- sizeofObject(1) + sizeofString("example"),
- },
- {
- "Skip an array with spaces in it",
- "{\"an_array\": [ 1 , 2 , 3 ] ,example:42}",
- "{\"example\":true}",
- 10,
- DeserializationError::Ok,
- "{\"example\":42}",
- sizeofObject(1) + sizeofString("example"),
- },
- {
- "Skip an empty nested object",
- "{\"an_empty_object\":{},example:42}",
- "{\"example\":true}",
- 10,
- DeserializationError::Ok,
- "{\"example\":42}",
- sizeofObject(1) + sizeofString("example"),
- },
- {
- "Skip an empty nested object with spaces in it",
- "{\"an_empty_object\":{ },example:42}",
- "{\"example\":true}",
- 10,
- DeserializationError::Ok,
- "{\"example\":42}",
- sizeofObject(1) + sizeofString("example"),
- },
- {
- "Skip a nested object",
- "{\"an_object\":{a:1,'b':2,\"c\":3},example:42}",
- "{\"example\":true}",
- 10,
- DeserializationError::Ok,
- "{\"example\":42}",
- sizeofObject(1) + sizeofString("example"),
- },
- {
- "Skip an object with spaces in it",
- "{\"an_object\" : { a : 1 , 'b' : 2 , \"c\" : 3 } ,example:42}",
- "{\"example\":true}",
- 10,
- DeserializationError::Ok,
- "{\"example\":42}",
- sizeofObject(1) + sizeofString("example"),
- },
- {
- "Skip a string in a nested object",
- "{\"an_integer\": 0,\"example\":{\"type\":\"int\",\"outcome\":42}}",
- "{\"example\":{\"outcome\":true}}",
- 10,
- DeserializationError::Ok,
- "{\"example\":{\"outcome\":42}}",
- 2 * sizeofObject(1) + 2 * sizeofString("example"),
- },
- {
- "wildcard",
- "{\"example\":{\"type\":\"int\",\"outcome\":42}}",
- "{\"*\":{\"outcome\":true}}",
- 10,
- DeserializationError::Ok,
- "{\"example\":{\"outcome\":42}}",
- 2 * sizeofObject(1) + 2 * sizeofString("example"),
- },
- {
- "exclusion filter (issue #1628)",
- "{\"example\":1,\"ignored\":2}",
- "{\"*\":true,\"ignored\":false}",
- 10,
- DeserializationError::Ok,
- "{\"example\":1}",
- sizeofObject(1) + sizeofString("example"),
- },
- {
- "only the first element of array counts",
- "[1,2,3]",
- "[true, false]",
- 10,
- DeserializationError::Ok,
- "[1,2,3]",
- sizeofArray(3),
- },
- {
- "only the first element of array counts",
- "[1,2,3]",
- "[false, true]",
- 10,
- DeserializationError::Ok,
- "[]",
- sizeofArray(0),
- },
- {
- "filter members of object in array",
- "[{\"example\":1,\"ignore\":2},{\"example\":3,\"ignore\":4}]",
- "[{\"example\":true}]",
- 10,
- DeserializationError::Ok,
- "[{\"example\":1},{\"example\":3}]",
- sizeofArray(2) + 2 * sizeofObject(1) + sizeofString("example"),
- },
- {
- "Unclosed single quote in skipped element",
- "[',2,3]",
- "[false,true]",
- 10,
- DeserializationError::IncompleteInput,
- "[]",
- sizeofArray(0),
- },
- {
- "Unclosed double quote in skipped element",
- "[\",2,3]",
- "[false,true]",
- 10,
- DeserializationError::IncompleteInput,
- "[]",
- sizeofArray(0),
- },
- {
- "Detect errors in skipped value",
- "[!,2,\\]",
- "[false]",
- 10,
- DeserializationError::InvalidInput,
- "[]",
- sizeofArray(0),
- },
- {
- "Detect incomplete string event if it's skipped",
- "\"ABC",
- "false",
- 10,
- DeserializationError::IncompleteInput,
- "null",
- 0,
- },
- {
- "Detect incomplete string event if it's skipped",
- "'ABC",
- "false",
- 10,
- DeserializationError::IncompleteInput,
- "null",
- 0,
- },
- {
- "Handle escaped quotes",
- "'A\\'BC'",
- "false",
- 10,
- DeserializationError::Ok,
- "null",
- 0,
- },
- {
- "Handle escaped quotes",
- "\"A\\\"BC\"",
- "false",
- 10,
- DeserializationError::Ok,
- "null",
- 0,
- },
- {
- "Detect incomplete string in presence of escaped quotes",
- "'A\\'BC",
- "false",
- 10,
- DeserializationError::IncompleteInput,
- "null",
- 0,
- },
- {
- "Detect incomplete string in presence of escaped quotes",
- "\"A\\\"BC",
- "false",
- 10,
- DeserializationError::IncompleteInput,
- "null",
- 0,
- },
- {
- "skip empty array",
- "[]",
- "false",
- 10,
- DeserializationError::Ok,
- "null",
- 0,
- },
- {
- "Skip empty array with spaces",
- " [ ] ",
- "false",
- 10,
- DeserializationError::Ok,
- "null",
- 0,
- },
- {
- "Bubble up element error even if array is skipped",
- "[1,'2,3]",
- "false",
- 10,
- DeserializationError::IncompleteInput,
- "null",
- 0,
- },
- {
- "Bubble up member error even if object is skipped",
- "{'hello':'worl}",
- "false",
- 10,
- DeserializationError::IncompleteInput,
- "null",
- 0,
- },
- {
- "Bubble up colon error even if object is skipped",
- "{'hello','world'}",
- "false",
- 10,
- DeserializationError::InvalidInput,
- "null",
- 0,
- },
- {
- "Bubble up key error even if object is skipped",
- "{'hello:1}",
- "false",
- 10,
- DeserializationError::IncompleteInput,
- "null",
- 0,
- },
- {
- "Detect invalid value in skipped object",
- "{'hello':!}",
- "false",
- 10,
- DeserializationError::InvalidInput,
- "null",
- 0,
- },
- {
- "Ignore invalid value in skipped object",
- "{'hello':\\}",
- "false",
- 10,
- DeserializationError::InvalidInput,
- "null",
- 0,
- },
- {
- "Check nesting limit even for ignored objects",
- "{}",
- "false",
- 0,
- DeserializationError::TooDeep,
- "null",
- 0,
- },
- {
- "Check nesting limit even for ignored objects",
- "{'hello':{}}",
- "false",
- 1,
- DeserializationError::TooDeep,
- "null",
- 0,
- },
- {
- "Check nesting limit even for ignored values in objects",
- "{'hello':{}}",
- "{}",
- 1,
- DeserializationError::TooDeep,
- "{}",
- sizeofObject(0),
- },
- {
- "Check nesting limit even for ignored arrays",
- "[]",
- "false",
- 0,
- DeserializationError::TooDeep,
- "null",
- 0,
- },
- {
- "Check nesting limit even for ignored arrays",
- "[[]]",
- "false",
- 1,
- DeserializationError::TooDeep,
- "null",
- 0,
- },
- {
- "Check nesting limit even for ignored values in arrays",
- "[[]]",
- "[]",
- 1,
- DeserializationError::TooDeep,
- "[]",
- sizeofArray(0),
- },
- {
- "Supports back-slash at the end of skipped string",
- "\"hell\\",
- "false",
- 1,
- DeserializationError::IncompleteInput,
- "null",
- 0,
- },
- {
- "Invalid comment at after an element in a skipped array",
- "[1/]",
- "false",
- 10,
- DeserializationError::InvalidInput,
- "null",
- 0,
- },
- {
- "Incomplete comment at after an element in a skipped array",
- "[1/*]",
- "false",
- 10,
- DeserializationError::IncompleteInput,
- "null",
- 0,
- },
- {
- "Missing comma in a skipped array",
- "[1 2]",
- "false",
- 10,
- DeserializationError::InvalidInput,
- "null",
- 0,
- },
- {
- "Invalid comment at the beginning of array",
- "[/1]",
- "[false]",
- 10,
- DeserializationError::InvalidInput,
- "[]",
- sizeofArray(0),
- },
- {
- "Incomplete comment at the begining of an array",
- "[/*]",
- "[false]",
- 10,
- DeserializationError::IncompleteInput,
- "[]",
- sizeofArray(0),
- },
- {
- "Invalid comment before key",
- "{/1:2}",
- "{}",
- 10,
- DeserializationError::InvalidInput,
- "{}",
- sizeofObject(0),
- },
- {
- "Incomplete comment before key",
- "{/*:2}",
- "{}",
- 10,
- DeserializationError::IncompleteInput,
- "{}",
- sizeofObject(0),
- },
- {
- "Invalid comment after key",
- "{\"example\"/1:2}",
- "{}",
- 10,
- DeserializationError::InvalidInput,
- "{}",
- sizeofObject(0),
- },
- {
- "Incomplete comment after key",
- "{\"example\"/*:2}",
- "{}",
- 10,
- DeserializationError::IncompleteInput,
- "{}",
- sizeofObject(0),
- },
- {
- "Invalid comment after colon",
- "{\"example\":/12}",
- "{}",
- 10,
- DeserializationError::InvalidInput,
- "{}",
- sizeofObject(0),
- },
- {
- "Incomplete comment after colon",
- "{\"example\":/*2}",
- "{}",
- 10,
- DeserializationError::IncompleteInput,
- "{}",
- sizeofObject(0),
- },
- {
- "Comment next to an integer",
- "{\"ignore\":1//,\"example\":2\n}",
- "{\"example\":true}",
- 10,
- DeserializationError::Ok,
- "{}",
- sizeofObject(0),
- },
- {
- "Invalid comment after opening brace of a skipped object",
- "{/1:2}",
- "false",
- 10,
- DeserializationError::InvalidInput,
- "null",
- 0,
- },
- {
- "Incomplete after opening brace of a skipped object",
- "{/*:2}",
- "false",
- 10,
- DeserializationError::IncompleteInput,
- "null",
- 0,
- },
- {
- "Invalid comment after key of a skipped object",
- "{\"example\"/:2}",
- "false",
- 10,
- DeserializationError::InvalidInput,
- "null",
- 0,
- },
- {
- "Incomplete comment after key of a skipped object",
- "{\"example\"/*:2}",
- "false",
- 10,
- DeserializationError::IncompleteInput,
- "null",
- 0,
- },
- {
- "Invalid comment after value in a skipped object",
- "{\"example\":2/}",
- "false",
- 10,
- DeserializationError::InvalidInput,
- "null",
- 0,
- },
- {
- "Incomplete comment after value of a skipped object",
- "{\"example\":2/*}",
- "false",
- 10,
- DeserializationError::IncompleteInput,
- "null",
- 0,
- },
- {
- "Incomplete comment after comma in skipped object",
- "{\"example\":2,/*}",
- "false",
- 10,
- DeserializationError::IncompleteInput,
- "null",
- 0,
- },
- {
- "NUL character in key",
- "{\"x\":0,\"x\\u0000a\":1,\"x\\u0000b\":2}",
- "{\"x\\u0000a\":true}",
- 10,
- DeserializationError::Ok,
- "{\"x\\u0000a\":1}",
- sizeofObject(1) + sizeofString("x?a"),
- },
- };
- for (auto& tc : testCases) {
- SECTION(tc.description) {
- SpyingAllocator spy;
- JsonDocument filter;
- JsonDocument doc(&spy);
- REQUIRE(deserializeJson(filter, tc.filter) == DeserializationError::Ok);
- CHECK(deserializeJson(
- doc, tc.input, DeserializationOption::Filter(filter),
- DeserializationOption::NestingLimit(tc.nestingLimit)) ==
- tc.error);
- CHECK(doc.as<std::string>() == tc.output);
- doc.shrinkToFit();
- CHECK(spy.allocatedBytes() == tc.memoryUsage);
- }
- }
- }
- TEST_CASE("Overloads") {
- JsonDocument doc;
- JsonDocument filter;
- using namespace DeserializationOption;
- // deserializeJson(..., Filter)
- SECTION("const char*, Filter") {
- deserializeJson(doc, "{}", Filter(filter));
- }
- SECTION("const char*, size_t, Filter") {
- deserializeJson(doc, "{}", 2, Filter(filter));
- }
- SECTION("const std::string&, Filter") {
- deserializeJson(doc, "{}"_s, Filter(filter));
- }
- SECTION("std::istream&, Filter") {
- std::stringstream s("{}");
- deserializeJson(doc, s, Filter(filter));
- }
- #ifdef HAS_VARIABLE_LENGTH_ARRAY
- SECTION("char[n], Filter") {
- size_t i = 4;
- char vla[i];
- strcpy(vla, "{}");
- deserializeJson(doc, vla, Filter(filter));
- }
- #endif
- // deserializeJson(..., Filter, NestingLimit)
- SECTION("const char*, Filter, NestingLimit") {
- deserializeJson(doc, "{}", Filter(filter), NestingLimit(5));
- }
- SECTION("const char*, size_t, Filter, NestingLimit") {
- deserializeJson(doc, "{}", 2, Filter(filter), NestingLimit(5));
- }
- SECTION("const std::string&, Filter, NestingLimit") {
- deserializeJson(doc, "{}"_s, Filter(filter), NestingLimit(5));
- }
- SECTION("std::istream&, Filter, NestingLimit") {
- std::stringstream s("{}");
- deserializeJson(doc, s, Filter(filter), NestingLimit(5));
- }
- #ifdef HAS_VARIABLE_LENGTH_ARRAY
- SECTION("char[n], Filter, NestingLimit") {
- size_t i = 4;
- char vla[i];
- strcpy(vla, "{}");
- deserializeJson(doc, vla, Filter(filter), NestingLimit(5));
- }
- #endif
- // deserializeJson(..., NestingLimit, Filter)
- SECTION("const char*, NestingLimit, Filter") {
- deserializeJson(doc, "{}", NestingLimit(5), Filter(filter));
- }
- SECTION("const char*, size_t, NestingLimit, Filter") {
- deserializeJson(doc, "{}", 2, NestingLimit(5), Filter(filter));
- }
- SECTION("const std::string&, NestingLimit, Filter") {
- deserializeJson(doc, "{}"_s, NestingLimit(5), Filter(filter));
- }
- SECTION("std::istream&, NestingLimit, Filter") {
- std::stringstream s("{}");
- deserializeJson(doc, s, NestingLimit(5), Filter(filter));
- }
- #ifdef HAS_VARIABLE_LENGTH_ARRAY
- SECTION("char[n], NestingLimit, Filter") {
- size_t i = 4;
- char vla[i];
- strcpy(vla, "{}");
- deserializeJson(doc, vla, NestingLimit(5), Filter(filter));
- }
- #endif
- }
- TEST_CASE("shrink filter") {
- JsonDocument doc;
- SpyingAllocator spy;
- JsonDocument filter(&spy);
- filter["a"] = true;
- spy.clearLog();
- deserializeJson(doc, "{}", DeserializationOption::Filter(filter));
- REQUIRE(spy.log() == AllocatorLog{
- Reallocate(sizeofPool(), sizeofObject(1)),
- });
- }
|