8000 Added memoryUsage() to JsonArray, JsonObject, and JsonVariant · java64/ArduinoJson@c51cc91 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit c51cc91

Browse files
committed
Added memoryUsage() to JsonArray, JsonObject, and JsonVariant
1 parent 8b04046 commit c51cc91

File tree

14 files changed

+165
-1
lines changed

14 files changed

+165
-1
lines changed

CHANGELOG.md

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

1415
> ### BREAKING CHANGES
1516
>

src/ArduinoJson/Array/ArrayRef.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ class ArrayRefBase {
3434
return VariantConstRef(_data ? _data->get(index) : 0);
3535
}
3636

37+
FORCE_INLINE size_t memoryUsage() const {
38+
return _data ? _data->memoryUsage() : 0;
39+
}
40+
3741
FORCE_INLINE size_t size() const {
3842
return _data ? _data->size() : 0;
3943
}

src/ArduinoJson/Collection/CollectionData.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class CollectionData {
5555

5656
void remove(VariantSlot *slot);
5757

58+
size_t memoryUsage() const;
5859
size_t size() const;
5960

6061
private:

src/ArduinoJson/Collection/CollectionImpl.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,15 @@ inline void CollectionData::remove(size_t index) {
138138
remove(getSlot(index));
139139
}
140140

141+
inline size_t CollectionData::memoryUsage() const {
142+
size_t total = 0;
143+
for (VariantSlot* s = _head; s; s = s->next()) {
144+
total += sizeof(VariantSlot) + s->data()->memoryUsage();
145+
if (s->ownsKey()) total += strlen(s->key()) + 1;
146+
}
147+
return total;
148+
}
149+
141150
inline size_t CollectionData::size() const {
142151
return slotSize(_head);
143152
}

src/ArduinoJson/Object/ObjectRef.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ class ObjectRefBase {
4242
return _data == 0;
4343
}
4444

45+
FORCE_INLINE size_t memoryUsage() const {
46+
return _data ? _data->memoryUsage() : 0;
47+
}
48+
4549
FORCE_INLINE size_t size() const {
4650
return _data ? _data->size() : 0;
4751
}

src/ArduinoJson/Variant/VariantData.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,20 @@ class VariantData {
266266
return _content.asCollection;
267267
}
268268

269+
size_t memoryUsage() const {
270+
switch (type()) {
271+
case VALUE_IS_OWNED_STRING:
272+
return strlen(_content.asString) + 1;
273+
case VALUE_IS_OWNED_RAW:
274+
return _content.asRaw.size;
275+
case VALUE_IS_OBJECT:
276+
case VALUE_IS_ARRAY:
277+
return _content.asCollection.memoryUsage();
278+
default:
279+
return 0;
280+
}
281+
}
282+
269283
size_t size() const {
270284
if (type() == VALUE_IS_OBJECT || type() == VALUE_IS_ARRAY)
271285
return _content.asCollection.size();

src/ArduinoJson/Variant/VariantRef.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ class VariantRefBase {
9595
return variantIsNull(_data);
9696
}
9797

98+
FORCE_INLINE size_t memoryUsage() const {
99+
return _data ? _data->memoryUsage() : 0;
100+
}
101+
98102
size_t size() const {
99103
return variantSize(_data);
100104
}

test/JsonArray/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ add_executable(JsonArrayTests
1111
get.cpp
1212
isNull.cpp
1313
iterator.cpp
14+
memoryUsage.cpp
1415
remove.cpp
1516
size.cpp
1617
std_string.cpp

test/JsonArray/memoryUsage.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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::memoryUsage()") {
9+
DynamicJsonDocument doc(4096);
10+
JsonArray arr = doc.to<JsonArray>();
11+
12+
SECTION("return 0 if uninitialized") {
13+
JsonArray unitialized;
14+
REQUIRE(unitialized.memoryUsage() == 0);
15+
}
16+
17+
SECTION("JSON_ARRAY_SIZE(0) if empty") {
18+
REQUIRE(arr.memoryUsage() == JSON_ARRAY_SIZE(0));
19+
}
20+
21+
SECTION("JSON_ARRAY_SIZE(1) after add") {
22+
arr.add("hello");
23+
REQUIRE(arr.memoryUsage() == JSON_ARRAY_SIZE(1));
24+
}
25+
26+
SECTION("includes the size of the string") {
27+
arr.add(std::string("hello"));
28+
REQUIRE(arr.memoryUsage() == JSON_ARRAY_SIZE(1) + 6);
29+
}
30+
31+
SECTION("includes the size of the nested array") {
32+
JsonArray nested = arr.createNestedArray();
33+
nested.add(42);
34+
REQUIRE(arr.memoryUsage() == 2 * JSON_ARRAY_SIZE(1));
35+
}
36+
37+
SECTION("includes the size of the nested arrect") {
38+
JsonObject nested = arr.createNestedObject();
39+
nested["hello"] = "world";
40+
REQUIRE(arr.memoryUsage() == JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(1));
41+
}
42+
}

test/JsonArray/size.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ TEST_CASE("JsonArray::size()") {
99
DynamicJsonDocument doc(4096);
1010
JsonArray array = doc.to<JsonArray>();
1111

12-
SECTION("InitialSizeIsZero") {
12+
SECTION("returns 0 is empty") {
1313
REQUIRE(0U == array.size());
1414
}
1515

test/JsonObject/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ add_executable(JsonObjectTests
1111
invalid.cpp
1212
isNull.cpp
1313
iterator.cpp
14+
memoryUsage.cpp
1415
remove.cpp
1516
size.cpp
1617
std_string.cpp

test/JsonObject/memoryUsage.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// ArduinoJson - arduinojson.org
2+
// Copyright Benoit Blanchon 2014-2018
3+
// MIT License
4+
5+
#include <ArduinoJson.h>
6+
#include <catch.hpp>
7+
#include <string>
8+
9+
TEST_CASE("JsonObject::memoryUsage()") {
10+
DynamicJsonDocument doc(4096);
11+
JsonObject obj = doc.to<JsonObject>();
12+
13+
SECTION("return 0 if uninitialized") {
14+
JsonObject unitialized;
15+
REQUIRE(unitialized.memoryUsage() == 0);
16+
}
17+
18+
SECTION("JSON_OBJECT_SIZE(0) for empty object") {
19+
REQUIRE(obj.memoryUsage() == JSON_OBJECT_SIZE(0));
20+
}
21+
22+
SECTION("JSON_OBJECT_SIZE(1) after add") {
23+
obj["hello"] = 42;
24+
REQUIRE(obj.memoryUsage() == JSON_OBJECT_SIZE(1));
25+
}
26+
27+
SECTION("includes the size of the key") {
28+
obj[std::string("hello")] = 42;
29+
REQUIRE(obj.memoryUsage() == JSON_OBJECT_SIZE(1) + 6);
30+
}
31+
32+
SECTION("includes the size of the nested array") {
33+
JsonArray nested = obj.createNestedArray("nested");
34+
nested.add(42);
35+
REQUIRE(obj.memoryUsage() == JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(1));
36+
}
37+
38+
SECTION("includes the size of the nested object") {
39+
JsonObject nested = obj.createNestedObject("nested");
40+
nested["hello"] = "world";
41+
REQUIRE(obj.memoryUsage() == 2 * JSON_OBJECT_SIZE(1));
42+
}
43+
}

test/JsonVariant/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ add_executable(JsonVariantTests
99
get.cpp
1010
is.cpp
1111
isnull.cpp
12+
memoryUsage.cpp
1213
misc.cpp
1314
or.cpp
1415
set.cpp

test/JsonVariant/memoryUsage.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// ArduinoJson - arduinojson.org
2+
// Copyright Benoit Blanchon 2014-2018
3+
// MIT License
4+
5+
#include <ArduinoJson.h>
6+
#include <catch.hpp>
7+
#include <string>
8+
9+
TEST_CASE("JsonVariant::memoryUsage()") {
10+
DynamicJsonDocument doc(4096);
11+
JsonVariant var = doc.to<JsonVariant>();
12+
13+
SECTION("returns 0 if uninitialized") {
14+
JsonVariant unitialized;
15+
REQUIRE(unitialized.memoryUsage() == 0);
16+
}
17+
18+
SECTION("returns size of object") {
19+
JsonObject obj = var.to<JsonObject>();
20+
obj["hello"] = 42;
21+
REQUIRE(var.memoryUsage() == JSON_OBJECT_SIZE(1));
22+
}
23+
24+
SECTION("returns size of array") {
25+
JsonArray arr = var.to<JsonArray>();
26+
arr.add(42);
27+
REQUIRE(var.memoryUsage() == JSON_ARRAY_SIZE(1));
28+
}
29+
30+
SECTION("returns size of owned string") {
31+
var.set(std::string("hello"));
32+
REQUIRE(var.memoryUsage() == 6);
33+
}
34+
35+
SECTION("returns size of owned raw") {
36+
var.set(serialized(std::string("hello")));
37+
REQUIRE(var.memoryUsage() == 5);
38+
}
39+
}

0 commit comments

Comments
 (0)
0