Made nestingLimit a member of the document

This commit is contained in:
Benoit Blanchon
2018-04-18 21:42:14 +02:00
parent 1feb92679d
commit 4c9c047ddf
6 changed files with 42 additions and 39 deletions

View File

@ -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 <typename T>
bool is() const {

View File

@ -15,6 +15,10 @@ class StaticJsonDocument {
JsonVariant _root;
public:
uint8_t nestingLimit;
StaticJsonDocument() : nestingLimit(ARDUINOJSON_DEFAULT_NESTING_LIMIT) {}
Internals::StaticJsonBufferBase& buffer() {
return _buffer;
}

View File

@ -13,9 +13,8 @@ namespace ArduinoJson {
template <typename TDocument, typename TString>
typename Internals::EnableIf<!Internals::IsArray<TString>::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<JsonVariant>());
}
//
@ -23,10 +22,8 @@ deserializeJson(TDocument &doc, const TString &json,
// TDocument = DynamicJsonDocument, StaticJsonDocument
// TString = const char*, const char[N], const FlashStringHelper*
template <typename TDocument, typename TString>
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<JsonVariant>());
}
//
@ -34,10 +31,8 @@ JsonError deserializeJson(
// TDocument = DynamicJsonDocument, StaticJsonDocument
// TString = std::istream&, Stream&
template <typename TDocument, typename TString>
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<JsonVariant>());
}
} // namespace ArduinoJson

View File

@ -13,9 +13,9 @@ namespace ArduinoJson {
template <typename TDocument, typename TString>
typename Internals::EnableIf<!Internals::IsArray<TString>::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<JsonVariant>());
}
//
@ -23,10 +23,9 @@ deserializeMsgPack(TDocument &doc, const TString &json,
// TDocument = DynamicJsonArray | StaticJsonArray
// TString = const char*, const char[N], const FlashStringHelper*
template <typename TDocument, typename TString>
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<JsonVariant>());
}
//
@ -34,10 +33,9 @@ MsgPackError deserializeMsgPack(
// TDocument = DynamicJsonArray | StaticJsonArray
// TString = std::istream&, Stream&
template <typename TDocument, typename TString>
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<JsonVariant>());
}
} // namespace ArduinoJson

View File

@ -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}]"));
}
}

View File

@ -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);
}