8000 Added JsonDocument::operator[] · Cube-Line/ArduinoJson@933a66a · GitHub
[go: up one dir, main page]

Skip to content

Commit 933a66a

Browse files
committed
Added JsonDocument::operator[]
1 parent 4167b11 commit 933a66a

File tree

5 files changed

+86
-12
lines changed

5 files changed

+86
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ HEAD
1919
* The copy constructor of `DynamicJsonDocument` chooses the capacity according to the memory usage of the source, not from the capacity of the source.
2020
* Added the ability to create/assign a `StaticJsonDocument`/`DynamicJsonDocument` from a `JsonArray`/`JsonObject`/`JsonVariant`
2121
* Added `JsonDocument::isNull()`
22+
* Added `JsonDocument::operator[]`
2223

2324
> ### BREAKING CHANGES
2425
>

src/ArduinoJson/Document/JsonDocument.hpp

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include "../Variant/VariantRef.hpp"
1010
#include "../Variant/VariantTo.hpp"
1111

12+
#include "../Array/ArraySubscript.hpp"
13+
1214
namespace ARDUINOJSON_NAMESPACE {
1315

1416
class JsonDocument : public Visitable {
@@ -79,6 +81,48 @@ class JsonDocument : public Visitable {
7981
return _data;
8082
}
8183

84+
// ObjectSubscript operator[](TKey)
85+
// TKey = const std::string&, const String&
86+
template <typename TKey>
87+
FORCE_INLINE typename enable_if<IsString<TKey>::value,
88+
ObjectSubscript<const TKey&> >::type
89+
operator[](const TKey& key) {
90+
return getVariant()[key];
91+
}
92+
93+
// ObjectSubscript operator[](TKey);
94+
// TKey = const char*, const char[N], const __FlashStringHelper*
95+
template <typename TKey>
96+
FORCE_INLINE
97+
typename enable_if<IsString<TKey*>::value, ObjectSubscript<TKey*> >::type
98+
operator[](TKey* key) {
99+
return getVariant()[key];
100+
}
101+
102+
// VariantConstRef operator[](TKey) const
103+
// TKey = const std::string&, const String&
104+
template <typename TKey>
105+
FORCE_INLINE typename enable_if<IsString<TKey>::value, VariantConstRef>::type
106+
operator[](const TKey& key) const {
107+
return getVariant()[key];
108+
}
109+
110+
// VariantConstRef operator[](TKey) const;
111+
// TKey = const char*, const char[N], const __FlashStringHelper*
112+
template <typename TKey>
113+
FORCE_INLINE typename enable_if<IsString<TKey*>::value, VariantConstRef>::type
114+
operator[](TKey* key) const {
115+
return getVariant()[key];
116+
}
117+
118+
FORCE_INLINE ArraySubscript operator[](size_t index) {
119+
return getVariant()[index];
120+
}
121+
122+
FORCE_INLINE VariantConstRef operator[](size_t index) const {
123+
return getVariant()[index];
124+
}
125+
82126
protected:
83127
JsonDocument(MemoryPool pool) : _pool(pool) {
84128
_data.setNull();
@@ -88,10 +132,6 @@ class JsonDocument : public Visitable {
88132
_data.setNull();
89133
}
90134

91-
void copy(const JsonDocument& src) {
92-
to<VariantRef>().set(src.as<VariantRef>());
93-
}
94-
95135
void replacePool(MemoryPool pool) {
96136
_pool = pool;
97137
}

src/ArduinoJson/Operators/VariantSubscripts.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ class VariantSubscripts {
3131
//
3232
// ObjectSubscript operator[](TKey) const;
3333
// TKey = const std::string&, const String&
34-
template <typename TString>
35-
FORCE_INLINE typename enable_if<IsString<TString>::value,
36-
ObjectSubscript<const TString &> >::type
37-
operator[](const TString &key) const;
34+
template <typename TKey>
35+
FORCE_INLINE typename enable_if<IsString<TKey>::value,
36+
ObjectSubscript<const TKey &> >::type
37+
operator[](const TKey &key) const;
3838
//
3939
// ObjectSubscript operator[](TKey) const;
4040
// TKey = const char*, const char[N], const __FlashStringHelper*
41-
template <typename TString>
42-
FORCE_INLINE typename enable_if<IsString<TString *>::value,
43-
ObjectSubscript<TString *> >::type
44-
operator[](TString *key) const;
41+
template <typename TKey>
42+
FORCE_INLINE typename enable_if<IsString<TKey *>::value,
43+
ObjectSubscript<TKey *> >::type
44+
operator[](TKey *key) const;
4545

4646
private:
4747
const TImpl *impl() const {

test/JsonDocument/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ add_executable(JsonDocumentTests
77
nesting.cpp
88
isNull.cpp
99
StaticJsonDocument.cpp
10+
subscript.cpp
1011
)
1112

1213
target_link_libraries(JsonDocumentTests catch)

test/JsonDocument/subscript.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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("JsonDocument::operator[]") {
9+
DynamicJsonDocument doc(4096);
10+
const JsonDocument& cdoc = doc;
11+
12+
SECTION("object") {
13+
deserializeJson(doc, "{\"hello\":\"world\"}");
14+
15+
SECTION("const char*") {
16+
REQUIRE(doc["hello"] == "world");
17+
REQUIRE(cdoc["hello"] == "world");
18+
}
19+
20+
SECTION("std::string") {
21+
REQUIRE(doc[std::string("hello")] == "world");
22+
REQUIRE(cdoc[std::string("hello")] == "world");
23+
}
24+
}
25+
26+
SECTION("array") {
27+
deserializeJson(doc, "[\"hello\",\"world\"]");
28+
29+
REQUIRE(doc[1] == "world");
30+
REQUIRE(cdoc[1] == "world");
31+
}
32+
}

0 commit comments

Comments
 (0)
0