8000 Added nesting() to JsonArray, JsonDocument, JsonObject, and JsonVariant · java64/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 > maxChildNesting) 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+
}

test/JsonDocument/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
add_executable(JsonDocumentTests
66
DynamicJsonDocument.cpp
7+
nesting.cpp
78
StaticJsonDocument.cpp
89
)
910

test/JsonDocument/nesting.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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("JsonDocument::nesting()") {
9+
DynamicJsonDocument doc(4096);
10+
11+
SECTION("return 0 if uninitialized") {
12+
REQUIRE(doc.nesting() == 0);
13+
}
14+
15+
SECTION("returns 0 for string") {
16+
JsonVariant var = doc.to<JsonVariant>();
17+
var.set("hello");
18+
REQUIRE(doc.nesting() == 0);
19+
}
20+
21+
SECTION("returns 1 for empty object") {
22+
doc.to<JsonObject>();
23+
REQUIRE(doc.nesting() == 1);
24+
}
25+
26+
SECTION("returns 1 for empty array") {
27+
doc.to<JsonArray>();
28+
REQUIRE(doc.nesting() == 1);
29+
}
30+
}

test/JsonObject/CMakeLists.txt

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

test/JsonObject/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("JsonObject::nesting()") {
9+
DynamicJsonDocument doc(4096);
10+
JsonObject obj = doc.to<JsonObject>();
11+
12+
SECTION("return 0 if uninitialized") {
13+
JsonObject unitialized;
14+
REQUIRE(unitialized.nesting() == 0);
15+
}
16+
17+
SECTION("returns 1 for empty object") {
18+
REQUIRE(obj.nesting() == 1);
19+
}
20+
21+
SECTION("returns 1 for flat object") {
22+
obj["hello"] = "world";
23+
REQUIRE(obj.nesting() == 1);
24+
}
25+
26+
SECTION("returns 2 with nested array") {
27+
obj.createNestedArray("nested");
28+
REQUIRE(obj.nesting() == 2);
29+
}
30+
31+
SECTION("returns 2 with nested object") {
32+
obj.createNestedObject("nested");
33+
REQUIRE(obj.nesting() == 2);
34+
}
35+
}

test/JsonVariant/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ add_executable(JsonVariantTests
1010
is.cpp
1111
isnull.cpp
1212
memoryUsage.cpp
13+
nesting.cpp
1314
misc.cpp
1415
or.cpp
1516
set.cpp

test/JsonVariant/nesting.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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("JsonVariant::nesting()") {
9+
DynamicJsonDocument doc(4096);
10+
JsonVariant var = doc.to<JsonVariant>();
11+
12+
SECTION("return 0 if uninitialized") {
13+
JsonVariant unitialized;
14+
REQUIRE(unitialized.nesting() == 0);
15+
}
16+
17+
SECTION("returns 0 for string") {
18+
var.set("hello");
19+
REQUIRE(var.nesting() == 0);
20+
}
21+
22+
SECTION("returns 1 for empty object") {
23+
var.to<JsonObject>();
24+
REQUIRE(var.nesting() == 1);
25+
}
26+
27+
SECTION("returns 1 for empty array") {
28+
var.to<JsonArray>();
29+
REQUIRE(var.nesting() == 1);
30+
}
31+
}

0 commit comments

Comments
 (0)
0