-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonVariant.cpp
executable file
·108 lines (88 loc) · 2.82 KB
/
JsonVariant.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright Benoit Blanchon 2014
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#include "JsonVariant.h"
#include "JsonArray.h"
#include "JsonObject.h"
using namespace ArduinoJson;
using namespace ArduinoJson::Internals;
JsonVariant JsonVariant::_invalid(JSON_INVALID);
JsonVariant::operator JsonArray &() const {
return _type == JSON_ARRAY ? *_content.asArray : JsonArray::invalid();
}
JsonVariant::operator JsonObject &() const {
return _type == JSON_OBJECT ? *_content.asObject : JsonObject::invalid();
}
JsonVariant::operator bool() const {
return _type == JSON_BOOLEAN ? _content.asBoolean : false;
}
JsonVariant::operator const char *() const {
return _type == JSON_STRING ? _content.asString : NULL;
}
JsonVariant::operator double() const {
return _type >= JSON_DOUBLE_0_DECIMALS ? _content.asDouble : 0;
}
JsonVariant::operator long() const {
return _type == JSON_LONG ? _content.asLong : 0;
}
void JsonVariant::set(bool value) {
if (_type == JSON_INVALID) return;
_type = Internals::JSON_BOOLEAN;
_content.asBoolean = value;
}
void JsonVariant::set(const char *value) {
if (_type == JSON_INVALID) return;
_type = JSON_STRING;
_content.asString = value;
}
void JsonVariant::set(double value, uint8_t decimals) {
if (_type == JSON_INVALID) return;
_type = static_cast<JsonVariantType>(JSON_DOUBLE_0_DECIMALS + decimals);
_content.asDouble = value;
}
void JsonVariant::set(long value) {
if (_type == JSON_INVALID) return;
_type = JSON_LONG;
_content.asLong = value;
}
void JsonVariant::set(JsonArray &array) {
if (_type == JSON_INVALID) return;
_type = JSON_ARRAY;
_content.asArray = &array;
}
void JsonVariant::set(JsonObject &object) {
if (_type == JSON_INVALID) return;
_type = JSON_OBJECT;
_content.asObject = &object;
}
size_t JsonVariant::size() const {
if (_type == JSON_ARRAY) return _content.asArray->size();
if (_type == JSON_OBJECT) return _content.asObject->size();
return 0;
}
JsonVariant &JsonVariant::operator[](int index) {
if (_type != JSON_ARRAY) return JsonVariant::invalid();
return _content.asArray->operator[](index);
}
JsonVariant &JsonVariant::operator[](const char *key) {
if (_type != JSON_OBJECT) return JsonVariant::invalid();
return _content.asObject->operator[](key);
}
void JsonVariant::writeTo(JsonWriter &writer) const {
if (is<const JsonArray &>())
as<const JsonArray &>().writeTo(writer);
else if (is<const JsonObject &>())
as<const JsonObject &>().writeTo(writer);
else if (is<const char *>())
writer.writeString(as<const char *>());
else if (is<long>())
writer.writeLong(as<long>());
else if (is<bool>())
writer.writeBoolean(as<bool>());
else if (is<double>()) {
uint8_t decimals = static_cast<uint8_t>(_type - JSON_DOUBLE_0_DECIMALS);
writer.writeDouble(as<double>(), decimals);
}
}