8000 Added `JsonDocument::remove()` and `JsonVariant::remove()` · Cube-Line/ArduinoJson@c9d6bd7 · GitHub
[go: up one dir, main page]

Skip to content

Commit c9d6bd7

Browse files
committed
Added JsonDocument::remove() and JsonVariant::remove()
1 parent bc2ce17 commit c9d6bd7

File tree

14 files changed

+298
-2
lines changed

14 files changed

+298
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ HEAD
1717
* Fixed segfault after `variant.set(serialized((char*)0))`
1818
* Detect `IncompleteInput` in `false`, `true`, and `null`
1919
* Added `JsonDocument::size()`
20+
* Added `JsonDocument::remove()`
2021
* Added `JsonVariant::clear()`
22+
* Added `JsonVariant::remove()`
2123

2224
v6.8.0-beta (2019-01-30)
2325
-----------

src/ArduinoJson/Array/ElementProxy.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,25 @@ class ElementProxy : public VariantOperators<ElementProxy<TArray> >,
124124
return getUpstreamElement().getElement(index);
125125
}
126126

127+
FORCE_INLINE void remove(size_t index) const {
128+
getUpstreamElement().remove(index);
129+
}
130+
// remove(char*) const
131+
// remove(const char*) const
132+
// remove(const __FlashStringHelper*) const
133+
template <typename TChar>
134+
FORCE_INLINE typename enable_if<IsString<TChar*>::value>::type remove(
135+
TChar* key) const {
136+
getUpstreamElement().remove(key);
137+
}
138+
// remove(const std::string&) const
139+
// remove(const String&) const
140+
template <typename TString>
141+
FORCE_INLINE typename enable_if<IsString<TString>::value>::type remove(
142+
const TString& key) const {
143+
getUpstreamElement().remove(key);
144+
}
145+
127146
private:
128147
FORCE_INLINE VariantRef getUpstreamElement() const {
129148
return _array.getElement(_index);

src/ArduinoJson/Document/JsonDocument.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,25 @@ class JsonDocument : public Visitable {
223223
return addElement().set(value);
224224
}
225225

226+
FORCE_INLINE void remove(size_t index) {
227+
_data.remove(index);
228+
}
229+
// remove(char*)
230+
// remove(const char*)
231+
// remove(const __FlashStringHelper*)
232+
template <typename TChar>
233+
FORCE_INLINE typename enable_if<IsString<TChar*>::value>::type remove(
234+
TChar* key) {
235+
_data.remove(adaptString(key));
236+
}
237+
// remove(const std::string&)
238+
// remove(const String&)
239+
template <typename TString>
240+
FORCE_INLINE typename enable_if<IsString<TString>::value>::type remove(
241+
const TString& key) {
242+
_data.remove(adaptString(key));
243+
}
244+
226245
protected:
227246
JsonDocument(MemoryPool pool) : _pool(pool) {
228247
_data.setNull();

src/ArduinoJson/Object/MemberProxy.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,25 @@ class MemberProxy : public VariantOperators<MemberProxy<TObject, TStringRef> >,
7171
return getUpstreamMember().size();
7272
}
7373

74+
FORCE_INLINE void remove(size_t index) const {
75+
getUpstreamMember().remove(index);
76+
}
77+
// remove(char*) const
78+
// remove(const char*) const
79+
// remove(const __FlashStringHelper*) const
80+
template <typename TChar>
81+
FORCE_INLINE typename enable_if<IsString<TChar *>::value>::type remove(
82+
TChar *key) const {
83+
getUpstreamMember().remove(key);
84+
}
85+
// remove(const std::string&) const
86+
// remove(const String&) const
87+
template <typename TString>
88+
FORCE_INLINE typename enable_if<IsString<TString>::value>::type remove(
89+
const TString &key) const {
90+
getUpstreamMember().remove(key);
91+
}
92+
7493
template <typename TValue>
7594
FORCE_INLINE typename VariantTo<TValue>::type to() {
7695
return getOrAddUpstreamMember().template to<TValue>();

src/ArduinoJson/Variant/VariantData.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,15 @@ class VariantData {
169169
return type() == VALUE_IS_NULL;
170170
}
171171

172+
void remove(size_t index) {
173+
if (isArray()) _content.asCollection.remove(index);
174+
}
175+
176+
template <typename TAdaptedString>
177+
void remove(TAdaptedString key) {
178+
if (isObject()) _content.asCollection.remove(key);
179+
}
180+
172181
void setBoolean(bool value) {
173182
setType(VALUE_IS_BOOLEAN);
174183
_content.asInteger = static_cast<UInt>(value);

src/ArduinoJson/Variant/VariantRef.hpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,28 @@ class VariantRef : public VariantRefBase<VariantData>,
310310
template <typename TString>
311311
FORCE_INLINE VariantRef getOrAddMember(const TString &) const;
312312

313+
FORCE_INLINE void remove(size_t index) const {
314+
if (_data) _data->remove(index);
315+
}
316+
// remove(char*) const
317+
// remove(const char*) const
318+
// remove(const __FlashStringHelper*) const
319+
template <typename TChar>
320+
FORCE_INLINE typename enable_if<IsString<TChar *>::value>::type remove(
321+
TChar *key) const {
322+
if (_data) _data->remove(adaptString(key));
323+
}
324+
// remove(const std::string&) const
325+
// remove(const String&) const
326+
template <typename TString>
327+
FORCE_INLINE typename enable_if<IsString<TString>::value>::type remove(
328+
const TString &key) const {
329+
if (_data) _data->remove(adaptString(key));
330+
}
331+
313332
private:
314333
MemoryPool *_pool;
315-
};
334+
}; // namespace ARDUINOJSON_NAMESPACE
316335

317336
class VariantConstRef : public VariantRefBase<const VariantData>,
318337
public VariantOperators<VariantConstRef>,

test/ElementProxy/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
add_executable(ElementProxyTests
66
add.cpp
77
clear.cpp
8+
remove.cpp
89
set.cpp
910
size.cpp
1011
)

test/ElementProxy/remove.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// ArduinoJson - arduinojson.org
2+
// Copyright Benoit Blanchon 2014-2019
3+
// MIT License
4+
5+
#include <ArduinoJson.h>
6+
#include <catch.hpp>
7+
8+
using namespace ARDUINOJSON_NAMESPACE;
9+
10+
TEST_CASE("ElementProxy::remove()") {
11+
DynamicJsonDocument doc(4096);
12+
doc.addElement();
13+
ElementProxy<JsonDocument&> ep = doc[0];
14+
15+
SECTION("remove(int)") {
16+
ep.add(1);
17+
ep.add(2);
18+
ep.add(3);
19+
20+
ep.remove(1);
21+
22+
REQUIRE(ep.as<std::string>() == "[1,3]");
23+
}
24+
25+
SECTION("remove(const char *)") {
26+
ep["a"] = 1;
27+
ep["b"] = 2;
28+
29+
ep.remove("a");
30+
31+
REQUIRE(ep.as<std::string>() == "{\"b\":2}");
32+
}
33+
34+
SECTION("remove(std::string)") {
35+
ep["a"] = 1;
36+
ep["b"] = 2;
37+
38+
ep.remove(std::string("b"));
39+
40+
REQUIRE(ep.as<std::string>() == "{\"a\":1}");
41+
}
42+
43+
#ifdef HAS_VARIABLE_LENGTH_ARRAY
44+
SECTION("remove(vla)") {
45+
ep["a"] = 1;
46+
ep["b"] = 2;
47+
48+
int i = 4;
49+
char vla[i];
50+
strcpy(vla, "b");
51+
ep.remove(vla);
52+
53+
REQUIRE(ep.as<std::string>() == "{\"a\":1}");
54+
}
55+
#endif
56+
}

test/JsonDocument/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ add_executable(JsonDocumentTests
88
DynamicJsonDocument.cpp
99
isNull.cpp
1010
nesting.cpp
11+
remove.cpp
1112
size.cpp
1213
StaticJsonDocument.cpp
1314
subscript.cpp

test/JsonDocument/remove.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// ArduinoJson - arduinojson.org
2+
// Copyright Benoit Blanchon 2014-2019
3+
// MIT License
4+
5+
#include <ArduinoJson.h>
6+
#include <catch.hpp>
7+
8+
TEST_CASE("JsonDocument::remove()") {
9+
DynamicJsonDocument doc(4096);
10+
11+
SECTION("remove(int)") {
12+
doc.add(1);
13+
doc.add(2);
14+
doc.add(3);
15+
16+
doc.remove(1);
17+
18+
REQUIRE(doc.as<std::string>() == "[1,3]");
19+
}
20+
21+
SECTION("remove(const char *)") {
22+
doc["a"] = 1;
23+
doc["b"] = 2;
24+
25+
doc.remove("a");
26+
27+
REQUIRE(doc.as<std::string>() == "{\"b\":2}");
28+
}
29+
30+
SECTION("remove(std::string)") {
31+
doc["a"] = 1;
32+
doc["b"] = 2;
33+
34+
doc.remove(std::string("b"));
35+
36+
REQUIRE(doc.as<std::string>() == "{\"a\":1}");
37+
}
38+
39+
#ifdef HAS_VARIABLE_LENGTH_ARRAY
40+
SECTION("remove(vla)") {
41+
doc["a"] = 1;
42+
doc["b"] = 2;
43+
44+
int i = 4;
45+
char vla[i];
46+
strcpy(vla, "b");
47+
doc.remove(vla);
48+
49+
REQUIRE(doc.as<std::string>() == "{\"a\":1}");
50+
}
51+
#endif
52+
}

0 commit comments

Comments
 (0)
0