8000 Added nesting() to JsonArray, JsonDocument, JsonObject, and JsonVariant · tschaban/ArduinoJson@30b9449 · GitHub
[go: up one dir, main page]

Skip to content

Commit 30b9449

Browse files
committed
Added nesting() to JsonArray, JsonDocument, JsonObject, and JsonVariant
1 parent c51cc91 commit 30b9449

File tree

16 files changed

+172
-0
lines changed

16 files changed

+172
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ HEAD
1111
* `JsonVariant::set()` accepts `JsonArrayConst` and `JsonObjectConst`
1212
* `JsonDocument` was missing in the ArduinoJson namespace
1313
* Added `memoryUsage()` to `JsonArray`, `JsonObject`, and `JsonVariant`
14+
* Added `nesting()` to `JsonArray`, `JsonDocument`, `JsonObject`, and `JsonVariant`
1415

1516
> ### BREAKING CHANGES
1617
>

src/ArduinoJson/Array/ArrayRef.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ class ArrayRefBase {
3838
return _data ? _data->memoryUsage() : 0;
3939
}
4040

41+
FORCE_INLINE size_t nesting() const {
42+
return _data ? _data->nesting() : 0;
43+
}
44+
4145
FORCE_INLINE size_t size() const {
4246
return _data ? _data->size() : 0;
4347
}

src/ArduinoJson/Collection/CollectionData.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class CollectionData {
5656
void remove(VariantSlot *slot);
5757

5858
size_t memoryUsage() const;
59+
size_t nesting() const;
5960
size_t size() const;
6061

6162
private:

src/ArduinoJson/Collection/CollectionImpl.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,15 @@ inline size_t CollectionData::memoryUsage() const {
147147
return total;
148148
}
149149

150+
inline size_t CollectionData::nesting() const {
151+
size_t maxChildNesting = 0;
152+
for (VariantSlot* s = _head; s; s = s->next()) {
153+
size_t childNesting = s->data()->nesting();
154+
if (childNesting > maxChil 67E6 dNesting) maxChildNesting = childNesting;
155+
}
156+
return maxChildNesting + 1;
157+
}
158+
150159
inline size_t CollectionData::size() const {
151160
return slotSize(_head);
152161
}

src/ArduinoJson/Document/JsonDocument.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ class JsonDocument : public Visitable {
4343
return _pool.size();
4444
}
4545

46+
size_t nesting() const {
47+
return _data.nesting();
48+
}
49+
4650
size_t capacity() const {
4751
return _pool.capacity();
4852
}

src/ArduinoJson/Object/ObjectRef.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ class ObjectRefBase {
4646
return _data ? _data->memoryUsage() : 0;
4747
}
4848

49+
FORCE_INLINE size_t nesting() const {
50+
return _data ? _data->nesting() : 0;
51+
}
52+
4953
FORCE_INLINE size_t size() const {
5054
return _data ? _data->size() : 0;
5155
}

src/ArduinoJson/Variant/VariantData.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,16 @@ class VariantData {
280280
}
281281
}
282282

283+
size_t nesting() const {
284+
switch (type()) {
285+
case VALUE_IS_OBJECT:
286+
case VALUE_IS_ARRAY:
287+
return _content.asCollection.nesting();
288+
default:
289+
return 0;
290+
}
291+
}
292+
283293
size_t size() const {
284294
if (type() == VALUE_IS_OBJECT || type() == VALUE_IS_ARRAY)
285295
return _content.asCollection.size();

src/ArduinoJson/Variant/VariantRef.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ class VariantRefBase {
9999
return _data ? _data->memoryUsage() : 0;
100100
}
101101

102+
FORCE_INLINE size_t nesting() const {
103+
return _data ? _data->nesting() : 0;
104+
}
105+
102106
size_t size() const {
103107
return variantSize(_data);
104108
}

test/JsonArray/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ add_executable(JsonArrayTests
1212
isNull.cpp
1313
iterator.cpp
1414
memoryUsage.cpp
15+
nesting.cpp
1516
remove.cpp
1617
size.cpp
1718
std_string.cpp

test/JsonArray/nesting.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// ArduinoJson - arduinojson.org
2+
// Copyright Benoit Blanchon 2014-2018
3+
// MIT License
4+
5+
#include <ArduinoJson.h>
6+
#include <catch.hpp>
7+
8+
TEST_CASE("JsonArray::nesting()") {
9+
DynamicJsonDocument doc(4096);
10+
JsonArray arr = doc.to<JsonArray>();
11+
12+
SECTION("return 0 if uninitialized") {
13+
JsonArray unitialized;
14+
REQUIRE(unitialized.nesting() == 0);
15+
}
16+
17+
SECTION("returns 1 for empty array") {
18+
REQUIRE(arr.nesting() == 1);
19+
}
20+
21+
SECTION("returns 1 for flat array") {
22+
arr.add("hello");
23+
REQUIRE(arr.nesting() == 1);
24+
}
25+
26+
SECTION("returns 2 with nested array") {
27+
arr.createNestedArray();
28+
REQUIRE(arr.nesting() == 2);
29+
}
30+
31+
SECTION("returns 2 with nested object") {
32+
arr.createNestedObject();
33+
REQUIRE(arr.nesting() == 2);
34+
}
35+
}

0 commit comments

Comments
 (0)
0