-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonObject.cpp
executable file
·85 lines (65 loc) · 1.97 KB
/
JsonObject.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright Benoit Blanchon 2014
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#include "JsonObject.h"
#include <string.h> // for strcmp
#include "StringBuilder.h"
#include "JsonArray.h"
#include "JsonBuffer.h"
using namespace ArduinoJson;
using namespace ArduinoJson::Internals;
JsonObject JsonObject::_invalid(NULL);
JsonVariant &JsonObject::at(const char *key) {
node_type *node = getNodeAt(key);
return node ? node->content.value : JsonVariant::invalid();
}
const JsonVariant &JsonObject::at(const char *key) const {
node_type *node = getNodeAt(key);
return node ? node->content.value : JsonVariant::invalid();
}
JsonVariant &JsonObject::operator[](const char *key) {
// try to find an existing node
node_type *node = getNodeAt(key);
// not fount => create a new one
if (!node) {
node = createNode();
if (!node) return JsonVariant::invalid();
node->content.key = key;
addNode(node);
}
return node->content.value;
}
void JsonObject::remove(char const *key) { removeNode(getNodeAt(key)); }
JsonArray &JsonObject::createNestedArray(char const *key) {
if (!_buffer) return JsonArray::invalid();
JsonArray &array = _buffer->createArray();
add(key, array);
return array;
}
JsonObject &JsonObject::createNestedObject(const char *key) {
if (!_buffer) return JsonObject::invalid();
JsonObject &object = _buffer->createObject();
add(key, object);
return object;
}
JsonObject::node_type *JsonObject::getNodeAt(const char *key) const {
for (node_type *node = _firstNode; node; node = node->next) {
if (!strcmp(node->content.key, key)) return node;
}
return NULL;
}
void JsonObject::writeTo(JsonWriter &writer) const {
writer.beginObject();
const node_type *node = _firstNode;
while (node) {
writer.writeString(node->content.key);
writer.writeColon();
node->content.value.writeTo(writer);
node = node->next;
if (!node) break;
writer.writeComma();
}
writer.endObject();
}