source: rtos_arduino/trunk/arduino_lib/libraries/ArduinoJson/include/ArduinoJson/JsonObject.ipp@ 209

Last change on this file since 209 was 209, checked in by ertl-honda, 8 years ago

BlueMix用のフィアルを追加

File size: 3.5 KB
Line 
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
14namespace ArduinoJson {
15
16inline JsonVariant JsonObject::get(JsonObjectKey key) const {
17 node_type *node = getNodeAt(key.c_str());
18 return node ? node->content.value : JsonVariant();
19}
20
21template <typename T>
22inline 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
27template <typename T>
28inline 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
33inline JsonObjectSubscript<const char *> JsonObject::operator[](
34 const char *key) {
35 return JsonObjectSubscript<const char *>(*this, key);
36}
37
38inline JsonObjectSubscript<const String &> JsonObject::operator[](
39 const String &key) {
40 return JsonObjectSubscript<const String &>(*this, key);
41}
42
43inline JsonVariant JsonObject::operator[](JsonObjectKey key) const {
44 return get(key);
45}
46
47inline bool JsonObject::containsKey(JsonObjectKey key) const {
48 return getNodeAt(key.c_str()) != NULL;
49}
50
51inline void JsonObject::remove(JsonObjectKey key) {
52 removeNode(getNodeAt(key.c_str()));
53}
54
55template <typename T>
56inline 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
66inline 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
76template <typename TValue>
77inline bool JsonObject::setNodeValue(node_type *node, TValue value) {
78 node->content.value = value;
79 return true;
80}
81
82template <>
83inline bool JsonObject::setNodeValue(node_type *node, String &value) {
84 node->content.value = _buffer->strdup(value);
85 return node->content.value;
86}
87
88template <>
89inline bool JsonObject::setNodeValue(node_type *node, const String &value) {
90 node->content.value = _buffer->strdup(value);
91 return node->content.value;
92}
93
94template <typename TImplem>
95inline const JsonObjectSubscript<const char *> JsonVariantBase<TImplem>::
96operator[](const char *key) const {
97 return asObject()[key];
98}
99
100template <typename TImplem>
101inline const JsonObjectSubscript<const String &> JsonVariantBase<TImplem>::
102operator[](const String &key) const {
103 return asObject()[key];
104}
105
106template <>
107inline JsonObject const &JsonVariant::invalid<JsonObject const &>() {
108 return JsonObject::invalid();
109}
110
111template <>
112inline JsonObject &JsonVariant::invalid<JsonObject &>() {
113 return JsonObject::invalid();
114}
115
116inline JsonObject &JsonVariant::asObject() const {
117 if (_type == Internals::JSON_OBJECT) return *_content.asObject;
118 return JsonObject::invalid();
119}
120
121inline 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
128inline JsonObject &JsonArray::createNestedObject() {
129 if (!_buffer) return JsonObject::invalid();
130 JsonObject &object = _buffer->createObject();
131 add(object);
132 return object;
133}
134}
Note: See TracBrowser for help on using the repository browser.