File tree Expand file tree Collapse file tree 14 files changed +165
-1
lines changed Expand file tree Collapse file tree 14 files changed +165
-1
lines changed Original file line number Diff line number Diff line change 10
10
* ` JsonArray::copyFrom() ` accepts ` JsonArrayConst `
11
11
* ` JsonVariant::set() ` accepts ` JsonArrayConst ` and ` JsonObjectConst `
12
12
* ` JsonDocument ` was missing in the ArduinoJson namespace
13
+ * Added ` memoryUsage() ` to ` JsonArray ` , ` JsonObject ` , and ` JsonVariant `
13
14
14
15
> ### BREAKING CHANGES
15
16
>
Original file line number Diff line number Diff line change @@ -34,6 +34,10 @@ class ArrayRefBase {
34
34
return VariantConstRef (_data ? _data->get (index) : 0 );
35
35
}
36
36
37
+ FORCE_INLINE size_t memoryUsage () const {
38
+ return _data ? _data->memoryUsage () : 0 ;
39
+ }
40
+
37
41
FORCE_INLINE size_t size () const {
38
42
return _data ? _data->size () : 0 ;
39
43
}
Original file line number Diff line number Diff line change @@ -55,6 +55,7 @@ class CollectionData {
55
55
56
56
void remove (VariantSlot *slot);
57
57
58
+ size_t memoryUsage () const ;
58
59
size_t size () const ;
59
60
60
61
private:
Original file line number Diff line number Diff line change @@ -138,6 +138,15 @@ inline void CollectionData::remove(size_t index) {
138
138
remove (getSlot (index));
139
139
}
140
140
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
+
141
150
inline size_t CollectionData::size () const {
142
151
return slotSize (_head);
143
152
}
Original file line number Diff line number Diff line change @@ -42,6 +42,10 @@ class ObjectRefBase {
42
42
return _data == 0 ;
43
43
}
44
44
45
+ FORCE_INLINE size_t memoryUsage () const {
46
+ return _data ? _data->memoryUsage () : 0 ;
47
+ }
48
+
45
49
FORCE_INLINE size_t size () const {
46
50
return _data ? _data->size () : 0 ;
47
51
}
Original file line number Diff line number Diff line change @@ -266,6 +266,20 @@ class VariantData {
266
266
return _content.asCollection ;
267
267
}
268
268
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
+
269
283
size_t size () const {
270
284
if (type () == VALUE_IS_OBJECT || type () == VALUE_IS_ARRAY)
271
285
return _content.asCollection .size ();
Original file line number Diff line number Diff line change @@ -95,6 +95,10 @@ class VariantRefBase {
95
95
return variantIsNull (_data);
96
96
}
97
97
98
+ FORCE_INLINE size_t memoryUsage () const {
99
+ return _data ? _data->memoryUsage () : 0 ;
100
+ }
101
+
98
102
size_t size () const {
99
103
return variantSize (_data);
100
104
}
Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ add_executable(JsonArrayTests
11
11
get .cpp
12
12
isNull.cpp
13
13
iterator.cpp
14
+ memoryUsage.cpp
14
15
remove .cpp
15
16
size.cpp
16
17
std_string.cpp
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ TEST_CASE("JsonArray::size()") {
9
9
DynamicJsonDocument doc (4096 );
10
10
JsonArray array = doc.to <JsonArray>();
11
11
12
- SECTION (" InitialSizeIsZero " ) {
12
+ SECTION (" returns 0 is empty " ) {
13
13
REQUIRE (0U == array.size ());
14
14
}
15
15
Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ add_executable(JsonObjectTests
11
11
invalid.cpp
12
12
isNull.cpp
13
13
iterator.cpp
14
+ memoryUsage.cpp
14
15
remove .cpp
15
16
size.cpp
16
17
std_string.cpp
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ add_executable(JsonVariantTests
9
9
get .cpp
10
10
is.cpp
11
11
isnull.cpp
12
+ memoryUsage.cpp
12
13
misc.cpp
13
14
or .cpp
14
15
set .cpp
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments