1 | // Copyright Benoit Blanchon 2014-2016
|
---|
2 | // MIT License
|
---|
3 | //
|
---|
4 | // Arduino JSON library
|
---|
5 | // https://github.com/bblanchon/ArduinoJson
|
---|
6 | // If you like this project, please add a star!
|
---|
7 |
|
---|
8 | #pragma once
|
---|
9 |
|
---|
10 | #include "JsonArray.hpp"
|
---|
11 | #include "JsonObject.hpp"
|
---|
12 | #include "JsonObjectSubscript.hpp"
|
---|
13 |
|
---|
14 | namespace ArduinoJson {
|
---|
15 |
|
---|
16 | inline JsonVariant JsonObject::get(JsonObjectKey key) const {
|
---|
17 | node_type *node = getNodeAt(key.c_str());
|
---|
18 | return node ? node->content.value : JsonVariant();
|
---|
19 | }
|
---|
20 |
|
---|
21 | template <typename T>
|
---|
22 | inline T JsonObject::get(JsonObjectKey key) const {
|
---|
23 | node_type *node = getNodeAt(key.c_str());
|
---|
24 | return node ? node->content.value.as<T>() : JsonVariant::invalid<T>();
|
---|
25 | }
|
---|
26 |
|
---|
27 | template <typename T>
|
---|
28 | inline bool JsonObject::is(JsonObjectKey key) const {
|
---|
29 | node_type *node = getNodeAt(key.c_str());
|
---|
30 | return node ? node->content.value.is<T>() : false;
|
---|
31 | }
|
---|
32 |
|
---|
33 | inline JsonObjectSubscript<const char *> JsonObject::operator[](
|
---|
34 | const char *key) {
|
---|
35 | return JsonObjectSubscript<const char *>(*this, key);
|
---|
36 | }
|
---|
37 |
|
---|
38 | inline JsonObjectSubscript<const String &> JsonObject::operator[](
|
---|
39 | const String &key) {
|
---|
40 | return JsonObjectSubscript<const String &>(*this, key);
|
---|
41 | }
|
---|
42 |
|
---|
43 | inline JsonVariant JsonObject::operator[](JsonObjectKey key) const {
|
---|
44 | return get(key);
|
---|
45 | }
|
---|
46 |
|
---|
47 | inline bool JsonObject::containsKey(JsonObjectKey key) const {
|
---|
48 | return getNodeAt(key.c_str()) != NULL;
|
---|
49 | }
|
---|
50 |
|
---|
51 | inline void JsonObject::remove(JsonObjectKey key) {
|
---|
52 | removeNode(getNodeAt(key.c_str()));
|
---|
53 | }
|
---|
54 |
|
---|
55 | template <typename T>
|
---|
56 | inline bool JsonObject::setNodeAt(JsonObjectKey key, T value) {
|
---|
57 | node_type *node = getNodeAt(key.c_str());
|
---|
58 | if (!node) {
|
---|
59 | node = addNewNode();
|
---|
60 | if (!node || !setNodeKey(node, key))
|
---|
61 | return false;
|
---|
62 | }
|
---|
63 | return setNodeValue<T>(node, value);
|
---|
64 | }
|
---|
65 |
|
---|
66 | inline bool JsonObject::setNodeKey(node_type *node, JsonObjectKey key) {
|
---|
67 | if (key.needs_copy()) {
|
---|
68 | node->content.key = _buffer->strdup(key.c_str());
|
---|
69 | if (node->content.key == NULL) return false;
|
---|
70 | } else {
|
---|
71 | node->content.key = key.c_str();
|
---|
72 | }
|
---|
73 | return true;
|
---|
74 | }
|
---|
75 |
|
---|
76 | template <typename TValue>
|
---|
77 | inline bool JsonObject::setNodeValue(node_type *node, TValue value) {
|
---|
78 | node->content.value = value;
|
---|
79 | return true;
|
---|
80 | }
|
---|
81 |
|
---|
82 | template <>
|
---|
83 | inline bool JsonObject::setNodeValue(node_type *node, String &value) {
|
---|
84 | node->content.value = _buffer->strdup(value);
|
---|
85 | return node->content.value;
|
---|
86 | }
|
---|
87 |
|
---|
88 | template <>
|
---|
89 | inline bool JsonObject::setNodeValue(node_type *node, const String &value) {
|
---|
90 | node->content.value = _buffer->strdup(value);
|
---|
91 | return node->content.value;
|
---|
92 | }
|
---|
93 |
|
---|
94 | template <typename TImplem>
|
---|
95 | inline const JsonObjectSubscript<const char *> JsonVariantBase<TImplem>::
|
---|
96 | operator[](const char *key) const {
|
---|
97 | return asObject()[key];
|
---|
98 | }
|
---|
99 |
|
---|
100 | template <typename TImplem>
|
---|
101 | inline const JsonObjectSubscript<const String &> JsonVariantBase<TImplem>::
|
---|
102 | operator[](const String &key) const {
|
---|
103 | return asObject()[key];
|
---|
104 | }
|
---|
105 |
|
---|
106 | template <>
|
---|
107 | inline JsonObject const &JsonVariant::invalid<JsonObject const &>() {
|
---|
108 | return JsonObject::invalid();
|
---|
109 | }
|
---|
110 |
|
---|
111 | template <>
|
---|
112 | inline JsonObject &JsonVariant::invalid<JsonObject &>() {
|
---|
113 | return JsonObject::invalid();
|
---|
114 | }
|
---|
115 |
|
---|
116 | inline JsonObject &JsonVariant::asObject() const {
|
---|
117 | if (_type == Internals::JSON_OBJECT) return *_content.asObject;
|
---|
118 | return JsonObject::invalid();
|
---|
119 | }
|
---|
120 |
|
---|
121 | inline JsonObject &JsonObject::createNestedObject(JsonObjectKey key) {
|
---|
122 | if (!_buffer) return JsonObject::invalid();
|
---|
123 | JsonObject &array = _buffer->createObject();
|
---|
124 | setNodeAt<const JsonVariant &>(key, array);
|
---|
125 | return array;
|
---|
126 | }
|
---|
127 |
|
---|
128 | inline JsonObject &JsonArray::createNestedObject() {
|
---|
129 | if (!_buffer) return JsonObject::invalid();
|
---|
130 | JsonObject &object = _buffer->createObject();
|
---|
131 | add(object);
|
---|
132 | return object;
|
---|
133 | }
|
---|
134 | }
|
---|