forked from bblanchon/ArduinoJson
Added JsonDocument::operator[]
This commit is contained in:
@ -19,6 +19,7 @@ HEAD
|
|||||||
* The copy constructor of `DynamicJsonDocument` chooses the capacity according to the memory usage of the source, not from the capacity of the source.
|
* The copy constructor of `DynamicJsonDocument` chooses the capacity according to the memory usage of the source, not from the capacity of the source.
|
||||||
* Added the ability to create/assign a `StaticJsonDocument`/`DynamicJsonDocument` from a `JsonArray`/`JsonObject`/`JsonVariant`
|
* Added the ability to create/assign a `StaticJsonDocument`/`DynamicJsonDocument` from a `JsonArray`/`JsonObject`/`JsonVariant`
|
||||||
* Added `JsonDocument::isNull()`
|
* Added `JsonDocument::isNull()`
|
||||||
|
* Added `JsonDocument::operator[]`
|
||||||
|
|
||||||
> ### BREAKING CHANGES
|
> ### BREAKING CHANGES
|
||||||
>
|
>
|
||||||
|
@ -9,6 +9,8 @@
|
|||||||
#include "../Variant/VariantRef.hpp"
|
#include "../Variant/VariantRef.hpp"
|
||||||
#include "../Variant/VariantTo.hpp"
|
#include "../Variant/VariantTo.hpp"
|
||||||
|
|
||||||
|
#include "../Array/ArraySubscript.hpp"
|
||||||
|
|
||||||
namespace ARDUINOJSON_NAMESPACE {
|
namespace ARDUINOJSON_NAMESPACE {
|
||||||
|
|
||||||
class JsonDocument : public Visitable {
|
class JsonDocument : public Visitable {
|
||||||
@ -79,6 +81,48 @@ class JsonDocument : public Visitable {
|
|||||||
return _data;
|
return _data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ObjectSubscript operator[](TKey)
|
||||||
|
// TKey = const std::string&, const String&
|
||||||
|
template <typename TKey>
|
||||||
|
FORCE_INLINE typename enable_if<IsString<TKey>::value,
|
||||||
|
ObjectSubscript<const TKey&> >::type
|
||||||
|
operator[](const TKey& key) {
|
||||||
|
return getVariant()[key];
|
||||||
|
}
|
||||||
|
|
||||||
|
// ObjectSubscript operator[](TKey);
|
||||||
|
// TKey = const char*, const char[N], const __FlashStringHelper*
|
||||||
|
template <typename TKey>
|
||||||
|
FORCE_INLINE
|
||||||
|
typename enable_if<IsString<TKey*>::value, ObjectSubscript<TKey*> >::type
|
||||||
|
operator[](TKey* key) {
|
||||||
|
return getVariant()[key];
|
||||||
|
}
|
||||||
|
|
||||||
|
// VariantConstRef operator[](TKey) const
|
||||||
|
// TKey = const std::string&, const String&
|
||||||
|
template <typename TKey>
|
||||||
|
FORCE_INLINE typename enable_if<IsString<TKey>::value, VariantConstRef>::type
|
||||||
|
operator[](const TKey& key) const {
|
||||||
|
return getVariant()[key];
|
||||||
|
}
|
||||||
|
|
||||||
|
// VariantConstRef operator[](TKey) const;
|
||||||
|
// TKey = const char*, const char[N], const __FlashStringHelper*
|
||||||
|
template <typename TKey>
|
||||||
|
FORCE_INLINE typename enable_if<IsString<TKey*>::value, VariantConstRef>::type
|
||||||
|
operator[](TKey* key) const {
|
||||||
|
return getVariant()[key];
|
||||||
|
}
|
||||||
|
|
||||||
|
FORCE_INLINE ArraySubscript operator[](size_t index) {
|
||||||
|
return getVariant()[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
FORCE_INLINE VariantConstRef operator[](size_t index) const {
|
||||||
|
return getVariant()[index];
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
JsonDocument(MemoryPool pool) : _pool(pool) {
|
JsonDocument(MemoryPool pool) : _pool(pool) {
|
||||||
_data.setNull();
|
_data.setNull();
|
||||||
@ -88,10 +132,6 @@ class JsonDocument : public Visitable {
|
|||||||
_data.setNull();
|
_data.setNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
void copy(const JsonDocument& src) {
|
|
||||||
to<VariantRef>().set(src.as<VariantRef>());
|
|
||||||
}
|
|
||||||
|
|
||||||
void replacePool(MemoryPool pool) {
|
void replacePool(MemoryPool pool) {
|
||||||
_pool = pool;
|
_pool = pool;
|
||||||
}
|
}
|
||||||
|
@ -31,17 +31,17 @@ class VariantSubscripts {
|
|||||||
//
|
//
|
||||||
// ObjectSubscript operator[](TKey) const;
|
// ObjectSubscript operator[](TKey) const;
|
||||||
// TKey = const std::string&, const String&
|
// TKey = const std::string&, const String&
|
||||||
template <typename TString>
|
template <typename TKey>
|
||||||
FORCE_INLINE typename enable_if<IsString<TString>::value,
|
FORCE_INLINE typename enable_if<IsString<TKey>::value,
|
||||||
ObjectSubscript<const TString &> >::type
|
ObjectSubscript<const TKey &> >::type
|
||||||
operator[](const TString &key) const;
|
operator[](const TKey &key) const;
|
||||||
//
|
//
|
||||||
// ObjectSubscript operator[](TKey) const;
|
// ObjectSubscript operator[](TKey) const;
|
||||||
// TKey = const char*, const char[N], const __FlashStringHelper*
|
// TKey = const char*, const char[N], const __FlashStringHelper*
|
||||||
template <typename TString>
|
template <typename TKey>
|
||||||
FORCE_INLINE typename enable_if<IsString<TString *>::value,
|
FORCE_INLINE typename enable_if<IsString<TKey *>::value,
|
||||||
ObjectSubscript<TString *> >::type
|
ObjectSubscript<TKey *> >::type
|
||||||
operator[](TString *key) const;
|
operator[](TKey *key) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const TImpl *impl() const {
|
const TImpl *impl() const {
|
||||||
|
@ -7,6 +7,7 @@ add_executable(JsonDocumentTests
|
|||||||
nesting.cpp
|
nesting.cpp
|
||||||
isNull.cpp
|
isNull.cpp
|
||||||
StaticJsonDocument.cpp
|
StaticJsonDocument.cpp
|
||||||
|
subscript.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(JsonDocumentTests catch)
|
target_link_libraries(JsonDocumentTests catch)
|
||||||
|
32
test/JsonDocument/subscript.cpp
Normal file
32
test/JsonDocument/subscript.cpp
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
// ArduinoJson - arduinojson.org
|
||||||
|
// Copyright Benoit Blanchon 2014-2018
|
||||||
|
// MIT License
|
||||||
|
|
||||||
|
#include <ArduinoJson.h>
|
||||||
|
#include <catch.hpp>
|
||||||
|
|
||||||
|
TEST_CASE("JsonDocument::operator[]") {
|
||||||
|
DynamicJsonDocument doc(4096);
|
||||||
|
const JsonDocument& cdoc = doc;
|
||||||
|
|
||||||
|
SECTION("object") {
|
||||||
|
deserializeJson(doc, "{\"hello\":\"world\"}");
|
||||||
|
|
||||||
|
SECTION("const char*") {
|
||||||
|
REQUIRE(doc["hello"] == "world");
|
||||||
|
REQUIRE(cdoc["hello"] == "world");
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("std::string") {
|
||||||
|
REQUIRE(doc[std::string("hello")] == "world");
|
||||||
|
REQUIRE(cdoc[std::string("hello")] == "world");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("array") {
|
||||||
|
deserializeJson(doc, "[\"hello\",\"world\"]");
|
||||||
|
|
||||||
|
REQUIRE(doc[1] == "world");
|
||||||
|
REQUIRE(cdoc[1] == "world");
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user