Kaynağa Gözat

Added `JsonDocument::remove()` and `JsonVariant::remove()`

Benoit Blanchon 7 yıl önce
ebeveyn
işleme
c9d6bd76c9

+ 2 - 0
CHANGELOG.md

@@ -17,7 +17,9 @@ HEAD
 * Fixed segfault after `variant.set(serialized((char*)0))`
 * Detect `IncompleteInput` in `false`, `true`, and `null`
 * Added `JsonDocument::size()`
+* Added `JsonDocument::remove()`
 * Added `JsonVariant::clear()`
+* Added `JsonVariant::remove()`
 
 v6.8.0-beta (2019-01-30)
 -----------

+ 19 - 0
src/ArduinoJson/Array/ElementProxy.hpp

@@ -124,6 +124,25 @@ class ElementProxy : public VariantOperators<ElementProxy<TArray> >,
     return getUpstreamElement().getElement(index);
   }
 
+  FORCE_INLINE void remove(size_t index) const {
+    getUpstreamElement().remove(index);
+  }
+  // remove(char*) const
+  // remove(const char*) const
+  // remove(const __FlashStringHelper*) const
+  template <typename TChar>
+  FORCE_INLINE typename enable_if<IsString<TChar*>::value>::type remove(
+      TChar* key) const {
+    getUpstreamElement().remove(key);
+  }
+  // remove(const std::string&) const
+  // remove(const String&) const
+  template <typename TString>
+  FORCE_INLINE typename enable_if<IsString<TString>::value>::type remove(
+      const TString& key) const {
+    getUpstreamElement().remove(key);
+  }
+
  private:
   FORCE_INLINE VariantRef getUpstreamElement() const {
     return _array.getElement(_index);

+ 19 - 0
src/ArduinoJson/Document/JsonDocument.hpp

@@ -223,6 +223,25 @@ class JsonDocument : public Visitable {
     return addElement().set(value);
   }
 
+  FORCE_INLINE void remove(size_t index) {
+    _data.remove(index);
+  }
+  // remove(char*)
+  // remove(const char*)
+  // remove(const __FlashStringHelper*)
+  template <typename TChar>
+  FORCE_INLINE typename enable_if<IsString<TChar*>::value>::type remove(
+      TChar* key) {
+    _data.remove(adaptString(key));
+  }
+  // remove(const std::string&)
+  // remove(const String&)
+  template <typename TString>
+  FORCE_INLINE typename enable_if<IsString<TString>::value>::type remove(
+      const TString& key) {
+    _data.remove(adaptString(key));
+  }
+
  protected:
   JsonDocument(MemoryPool pool) : _pool(pool) {
     _data.setNull();

+ 19 - 0
src/ArduinoJson/Object/MemberProxy.hpp

@@ -71,6 +71,25 @@ class MemberProxy : public VariantOperators<MemberProxy<TObject, TStringRef> >,
     return getUpstreamMember().size();
   }
 
+  FORCE_INLINE void remove(size_t index) const {
+    getUpstreamMember().remove(index);
+  }
+  // remove(char*) const
+  // remove(const char*) const
+  // remove(const __FlashStringHelper*) const
+  template <typename TChar>
+  FORCE_INLINE typename enable_if<IsString<TChar *>::value>::type remove(
+      TChar *key) const {
+    getUpstreamMember().remove(key);
+  }
+  // remove(const std::string&) const
+  // remove(const String&) const
+  template <typename TString>
+  FORCE_INLINE typename enable_if<IsString<TString>::value>::type remove(
+      const TString &key) const {
+    getUpstreamMember().remove(key);
+  }
+
   template <typename TValue>
   FORCE_INLINE typename VariantTo<TValue>::type to() {
     return getOrAddUpstreamMember().template to<TValue>();

+ 9 - 0
src/ArduinoJson/Variant/VariantData.hpp

@@ -169,6 +169,15 @@ class VariantData {
     return type() == VALUE_IS_NULL;
   }
 
+  void remove(size_t index) {
+    if (isArray()) _content.asCollection.remove(index);
+  }
+
+  template <typename TAdaptedString>
+  void remove(TAdaptedString key) {
+    if (isObject()) _content.asCollection.remove(key);
+  }
+
   void setBoolean(bool value) {
     setType(VALUE_IS_BOOLEAN);
     _content.asInteger = static_cast<UInt>(value);

+ 20 - 1
src/ArduinoJson/Variant/VariantRef.hpp

@@ -310,9 +310,28 @@ class VariantRef : public VariantRefBase<VariantData>,
   template <typename TString>
   FORCE_INLINE VariantRef getOrAddMember(const TString &) const;
 
+  FORCE_INLINE void remove(size_t index) const {
+    if (_data) _data->remove(index);
+  }
+  // remove(char*) const
+  // remove(const char*) const
+  // remove(const __FlashStringHelper*) const
+  template <typename TChar>
+  FORCE_INLINE typename enable_if<IsString<TChar *>::value>::type remove(
+      TChar *key) const {
+    if (_data) _data->remove(adaptString(key));
+  }
+  // remove(const std::string&) const
+  // remove(const String&) const
+  template <typename TString>
+  FORCE_INLINE typename enable_if<IsString<TString>::value>::type remove(
+      const TString &key) const {
+    if (_data) _data->remove(adaptString(key));
+  }
+
  private:
   MemoryPool *_pool;
-};
+};  // namespace ARDUINOJSON_NAMESPACE
 
 class VariantConstRef : public VariantRefBase<const VariantData>,
                         public VariantOperators<VariantConstRef>,

+ 1 - 0
test/ElementProxy/CMakeLists.txt

@@ -5,6 +5,7 @@
 add_executable(ElementProxyTests
 	add.cpp
 	clear.cpp
+	remove.cpp
 	set.cpp
 	size.cpp
 )

+ 56 - 0
test/ElementProxy/remove.cpp

@@ -0,0 +1,56 @@
+// ArduinoJson - arduinojson.org
+// Copyright Benoit Blanchon 2014-2019
+// MIT License
+
+#include <ArduinoJson.h>
+#include <catch.hpp>
+
+using namespace ARDUINOJSON_NAMESPACE;
+
+TEST_CASE("ElementProxy::remove()") {
+  DynamicJsonDocument doc(4096);
+  doc.addElement();
+  ElementProxy<JsonDocument&> ep = doc[0];
+
+  SECTION("remove(int)") {
+    ep.add(1);
+    ep.add(2);
+    ep.add(3);
+
+    ep.remove(1);
+
+    REQUIRE(ep.as<std::string>() == "[1,3]");
+  }
+
+  SECTION("remove(const char *)") {
+    ep["a"] = 1;
+    ep["b"] = 2;
+
+    ep.remove("a");
+
+    REQUIRE(ep.as<std::string>() == "{\"b\":2}");
+  }
+
+  SECTION("remove(std::string)") {
+    ep["a"] = 1;
+    ep["b"] = 2;
+
+    ep.remove(std::string("b"));
+
+    REQUIRE(ep.as<std::string>() == "{\"a\":1}");
+  }
+
+#ifdef HAS_VARIABLE_LENGTH_ARRAY
+  SECTION("remove(vla)") {
+    ep["a"] = 1;
+    ep["b"] = 2;
+
+    int i = 4;
+    char vla[i];
+    strcpy(vla, "b");
+    ep.remove(vla);
+
+    REQUIRE(ep.as<std::string>() == "{\"a\":1}");
+  }
+#endif
+}

+ 1 - 0
test/JsonDocument/CMakeLists.txt

@@ -8,6 +8,7 @@ add_executable(JsonDocumentTests
 	DynamicJsonDocument.cpp
 	isNull.cpp
 	nesting.cpp
+	remove.cpp
 	size.cpp
 	StaticJsonDocument.cpp
 	subscript.cpp

+ 52 - 0
test/JsonDocument/remove.cpp

@@ -0,0 +1,52 @@
+// ArduinoJson - arduinojson.org
+// Copyright Benoit Blanchon 2014-2019
+// MIT License
+
+#include <ArduinoJson.h>
+#include <catch.hpp>
+
+TEST_CASE("JsonDocument::remove()") {
+  DynamicJsonDocument doc(4096);
+
+  SECTION("remove(int)") {
+    doc.add(1);
+    doc.add(2);
+    doc.add(3);
+
+    doc.remove(1);
+
+    REQUIRE(doc.as<std::string>() == "[1,3]");
+  }
+
+  SECTION("remove(const char *)") {
+    doc["a"] = 1;
+    doc["b"] = 2;
+
+    doc.remove("a");
+
+    REQUIRE(doc.as<std::string>() == "{\"b\":2}");
+  }
+
+  SECTION("remove(std::string)") {
+    doc["a"] = 1;
+    doc["b"] = 2;
+
+    doc.remove(std::string("b"));
+
+    REQUIRE(doc.as<std::string>() == "{\"a\":1}");
+  }
+
+#ifdef HAS_VARIABLE_LENGTH_ARRAY
+  SECTION("remove(vla)") {
+    doc["a"] = 1;
+    doc["b"] = 2;
+
+    int i = 4;
+    char vla[i];
+    strcpy(vla, "b");
+    doc.remove(vla);
+
+    REQUIRE(doc.as<std::string>() == "{\"a\":1}");
+  }
+#endif
+}

+ 1 - 0
test/JsonVariant/CMakeLists.txt

@@ -15,6 +15,7 @@ add_executable(JsonVariantTests
 	misc.cpp
 	nesting.cpp
 	or.cpp
+	remove.cpp
 	set.cpp
 	subscript.cpp
 	types.cpp

+ 42 - 0
test/JsonVariant/remove.cpp

@@ -0,0 +1,42 @@
+// ArduinoJson - arduinojson.org
+// Copyright Benoit Blanchon 2014-2019
+// MIT License
+
+#include <ArduinoJson.h>
+#include <stdint.h>
+#include <catch.hpp>
+
+static const char* null = 0;
+
+TEST_CASE("JsonVariant::remove()") {
+  DynamicJsonDocument doc(4096);
+  JsonVariant var = doc.to<JsonVariant>();
+
+  SECTION("remove(int)") {
+    var.add(1);
+    var.add(2);
+    var.add(3);
+
+    var.remove(1);
+
+    REQUIRE(var.as<std::string>() == "[1,3]");
+  }
+
+  SECTION("remove(const char *)") {
+    var["a"] = 1;
+    var["b"] = 2;
+
+    var.remove("a");
+
+    REQUIRE(var.as<std::string>() == "{\"b\":2}");
+  }
+
+  SECTION("remove(std::string)") {
+    var["a"] = 1;
+    var["b"] = 2;
+
+    var.remove(std::string("b"));
+
+    REQUIRE(var.as<std::string>() == "{\"a\":1}");
+  }
+}

+ 2 - 1
test/MemberProxy/CMakeLists.txt

@@ -5,9 +5,10 @@
 add_executable(MemberProxyTests
 	add.cpp
 	clear.cpp
-	subscript.cpp
+	remove.cpp
 	set.cpp
 	size.cpp
+	subscript.cpp
 )
 
 target_link_libraries(MemberProxyTests catch)

+ 55 - 0
test/MemberProxy/remove.cpp

@@ -0,0 +1,55 @@
+// ArduinoJson - arduinojson.org
+// Copyright Benoit Blanchon 2014-2019
+// MIT License
+
+#include <ArduinoJson.h>
+#include <catch.hpp>
+
+using namespace ARDUINOJSON_NAMESPACE;
+
+TEST_CASE("MemberProxy::remove()") {
+  DynamicJsonDocument doc(4096);
+  MemberProxy<JsonDocument&, const char*> mp = doc["hello"];
+
+  SECTION("remove(int)") {
+    mp.add(1);
+    mp.add(2);
+    mp.add(3);
+
+    mp.remove(1);
+
+    REQUIRE(mp.as<std::string>() == "[1,3]");
+  }
+
+  SECTION("remove(const char *)") {
+    mp["a"] = 1;
+    mp["b"] = 2;
+
+    mp.remove("a");
+
+    REQUIRE(mp.as<std::string>() == "{\"b\":2}");
+  }
+
+  SECTION("remove(std::string)") {
+    mp["a"] = 1;
+    mp["b"] = 2;
+
+    mp.remove(std::string("b"));
+
+    REQUIRE(mp.as<std::string>() == "{\"a\":1}");
+  }
+
+#ifdef HAS_VARIABLE_LENGTH_ARRAY
+  SECTION("remove(vla)") {
+    mp["a"] = 1;
+    mp["b"] = 2;
+
+    int i = 4;
+    char vla[i];
+    strcpy(vla, "b");
+    mp.remove(vla);
+
+    REQUIRE(mp.as<std::string>() == "{\"a\":1}");
+  }
+#endif
+}