diff --git a/src/ArduinoJson/DynamicJsonDocument.hpp b/src/ArduinoJson/DynamicJsonDocument.hpp index 9cbda37f..884c615d 100644 --- a/src/ArduinoJson/DynamicJsonDocument.hpp +++ b/src/ArduinoJson/DynamicJsonDocument.hpp @@ -16,8 +16,11 @@ class DynamicJsonDocument { JsonVariant _root; public: - DynamicJsonDocument() {} - DynamicJsonDocument(size_t capacity) : _buffer(capacity) {} + uint8_t nestingLimit; + + DynamicJsonDocument() : nestingLimit(ARDUINOJSON_DEFAULT_NESTING_LIMIT) {} + DynamicJsonDocument(size_t capacity) + : _buffer(capacity), nestingLimit(ARDUINOJSON_DEFAULT_NESTING_LIMIT) {} template bool is() const { diff --git a/src/ArduinoJson/StaticJsonDocument.hpp b/src/ArduinoJson/StaticJsonDocument.hpp index a1821ca3..b111485c 100644 --- a/src/ArduinoJson/StaticJsonDocument.hpp +++ b/src/ArduinoJson/StaticJsonDocument.hpp @@ -15,6 +15,10 @@ class StaticJsonDocument { JsonVariant _root; public: + uint8_t nestingLimit; + + StaticJsonDocument() : nestingLimit(ARDUINOJSON_DEFAULT_NESTING_LIMIT) {} + Internals::StaticJsonBufferBase& buffer() { return _buffer; } diff --git a/src/ArduinoJson/deserializeJson.hpp b/src/ArduinoJson/deserializeJson.hpp index 46ffbfff..c59c8517 100644 --- a/src/ArduinoJson/deserializeJson.hpp +++ b/src/ArduinoJson/deserializeJson.hpp @@ -13,9 +13,8 @@ namespace ArduinoJson { template typename Internals::EnableIf::value, JsonError>::type -deserializeJson(TDocument &doc, const TString &json, - uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) { - return Internals::makeParser(&doc.buffer(), json, nestingLimit) +deserializeJson(TDocument &doc, const TString &json) { + return Internals::makeParser(&doc.buffer(), json, doc.nestingLimit) .parse(doc.template to()); } // @@ -23,10 +22,8 @@ deserializeJson(TDocument &doc, const TString &json, // TDocument = DynamicJsonDocument, StaticJsonDocument // TString = const char*, const char[N], const FlashStringHelper* template -JsonError deserializeJson( - TDocument &doc, TString *json, - uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) { - return Internals::makeParser(&doc.buffer(), json, nestingLimit) +JsonError deserializeJson(TDocument &doc, TString *json) { + return Internals::makeParser(&doc.buffer(), json, doc.nestingLimit) .parse(doc.template to()); } // @@ -34,10 +31,8 @@ JsonError deserializeJson( // TDocument = DynamicJsonDocument, StaticJsonDocument // TString = std::istream&, Stream& template -JsonError deserializeJson( - TDocument &doc, TString &json, - uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) { - return Internals::makeParser(&doc.buffer(), json, nestingLimit) +JsonError deserializeJson(TDocument &doc, TString &json) { + return Internals::makeParser(&doc.buffer(), json, doc.nestingLimit) .parse(doc.template to()); } } // namespace ArduinoJson diff --git a/src/ArduinoJson/deserializeMsgPack.hpp b/src/ArduinoJson/deserializeMsgPack.hpp index 60493d35..42cd83a6 100644 --- a/src/ArduinoJson/deserializeMsgPack.hpp +++ b/src/ArduinoJson/deserializeMsgPack.hpp @@ -13,9 +13,9 @@ namespace ArduinoJson { template typename Internals::EnableIf::value, MsgPackError>::type -deserializeMsgPack(TDocument &doc, const TString &json, - uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) { - return Internals::makeMsgPackDeserializer(&doc.buffer(), json, nestingLimit) +deserializeMsgPack(TDocument &doc, const TString &json) { + return Internals::makeMsgPackDeserializer(&doc.buffer(), json, + doc.nestingLimit) .parse(doc.template to()); } // @@ -23,10 +23,9 @@ deserializeMsgPack(TDocument &doc, const TString &json, // TDocument = DynamicJsonArray | StaticJsonArray // TString = const char*, const char[N], const FlashStringHelper* template -MsgPackError deserializeMsgPack( - TDocument &doc, TString *json, - uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) { - return Internals::makeMsgPackDeserializer(&doc.buffer(), json, nestingLimit) +MsgPackError deserializeMsgPack(TDocument &doc, TString *json) { + return Internals::makeMsgPackDeserializer(&doc.buffer(), json, + doc.nestingLimit) .parse(doc.template to()); } // @@ -34,10 +33,9 @@ MsgPackError deserializeMsgPack( // TDocument = DynamicJsonArray | StaticJsonArray // TString = std::istream&, Stream& template -MsgPackError deserializeMsgPack( - TDocument &doc, TString &json, - uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) { - return Internals::makeMsgPackDeserializer(&doc.buffer(), json, nestingLimit) +MsgPackError deserializeMsgPack(TDocument &doc, TString &json) { + return Internals::makeMsgPackDeserializer(&doc.buffer(), json, + doc.nestingLimit) .parse(doc.template to()); } } // namespace ArduinoJson diff --git a/test/JsonDeserializer/nestingLimit.cpp b/test/JsonDeserializer/nestingLimit.cpp index 0eecb4ff..d2c41d5b 100644 --- a/test/JsonDeserializer/nestingLimit.cpp +++ b/test/JsonDeserializer/nestingLimit.cpp @@ -12,21 +12,23 @@ TEST_CASE("JsonDeserializer nestingLimit") { DynamicJsonDocument doc; SECTION("limit = 0") { - SHOULD_WORK(deserializeJson(doc, "\"toto\"", 0)); - SHOULD_WORK(deserializeJson(doc, "123", 0)); - SHOULD_WORK(deserializeJson(doc, "true", 0)); - SHOULD_FAIL(deserializeJson(doc, "[]", 0)); - SHOULD_FAIL(deserializeJson(doc, "{}", 0)); - SHOULD_FAIL(deserializeJson(doc, "[\"toto\"]", 0)); - SHOULD_FAIL(deserializeJson(doc, "{\"toto\":1}", 0)); + doc.nestingLimit = 0; + SHOULD_WORK(deserializeJson(doc, "\"toto\"")); + SHOULD_WORK(deserializeJson(doc, "123")); + SHOULD_WORK(deserializeJson(doc, "true")); + SHOULD_FAIL(deserializeJson(doc, "[]")); + SHOULD_FAIL(deserializeJson(doc, "{}")); + SHOULD_FAIL(deserializeJson(doc, "[\"toto\"]")); + SHOULD_FAIL(deserializeJson(doc, "{\"toto\":1}")); } SECTION("limit = 1") { - SHOULD_WORK(deserializeJson(doc, "[\"toto\"]", 1)); - SHOULD_WORK(deserializeJson(doc, "{\"toto\":1}", 1)); - SHOULD_FAIL(deserializeJson(doc, "{\"toto\":{}}", 1)); - SHOULD_FAIL(deserializeJson(doc, "{\"toto\":[]}", 1)); - SHOULD_FAIL(deserializeJson(doc, "[[\"toto\"]]", 1)); - SHOULD_FAIL(deserializeJson(doc, "[{\"toto\":1}]", 1)); + doc.nestingLimit = 1; + SHOULD_WORK(deserializeJson(doc, "[\"toto\"]")); + SHOULD_WORK(deserializeJson(doc, "{\"toto\":1}")); + SHOULD_FAIL(deserializeJson(doc, "{\"toto\":{}}")); + SHOULD_FAIL(deserializeJson(doc, "{\"toto\":[]}")); + SHOULD_FAIL(deserializeJson(doc, "[[\"toto\"]]")); + SHOULD_FAIL(deserializeJson(doc, "[{\"toto\":1}]")); } } diff --git a/test/MsgPack/deserializationErrors.cpp b/test/MsgPack/deserializationErrors.cpp index c9e0e94e..1ece18bc 100644 --- a/test/MsgPack/deserializationErrors.cpp +++ b/test/MsgPack/deserializationErrors.cpp @@ -7,9 +7,10 @@ static void check(const char* input, MsgPackError expected, uint8_t nestingLimit = 10) { - DynamicJsonDocument variant; + DynamicJsonDocument doc; + doc.nestingLimit = nestingLimit; - MsgPackError error = deserializeMsgPack(variant, input, nestingLimit); + MsgPackError error = deserializeMsgPack(doc, input); REQUIRE(error == expected); }