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 "Configuration.hpp"
|
---|
11 | #include "JsonVariant.hpp"
|
---|
12 | #include "Internals/Parse.hpp"
|
---|
13 |
|
---|
14 | #include <string.h>
|
---|
15 |
|
---|
16 | namespace ArduinoJson {
|
---|
17 |
|
---|
18 | inline JsonVariant::JsonVariant(bool value) {
|
---|
19 | using namespace Internals;
|
---|
20 | _type = JSON_BOOLEAN;
|
---|
21 | _content.asInteger = static_cast<JsonInteger>(value);
|
---|
22 | }
|
---|
23 |
|
---|
24 | inline JsonVariant::JsonVariant(const char *value) {
|
---|
25 | _type = Internals::JSON_STRING;
|
---|
26 | _content.asString = value;
|
---|
27 | }
|
---|
28 |
|
---|
29 | inline JsonVariant::JsonVariant(Internals::Unparsed value) {
|
---|
30 | _type = Internals::JSON_UNPARSED;
|
---|
31 | _content.asString = value;
|
---|
32 | }
|
---|
33 |
|
---|
34 | inline JsonVariant::JsonVariant(JsonArray &array) {
|
---|
35 | _type = Internals::JSON_ARRAY;
|
---|
36 | _content.asArray = &array;
|
---|
37 | }
|
---|
38 |
|
---|
39 | inline JsonVariant::JsonVariant(JsonObject &object) {
|
---|
40 | _type = Internals::JSON_OBJECT;
|
---|
41 | _content.asObject = &object;
|
---|
42 | }
|
---|
43 |
|
---|
44 | template <typename T>
|
---|
45 | inline T JsonVariant::invalid() {
|
---|
46 | return T();
|
---|
47 | }
|
---|
48 |
|
---|
49 | template <typename T>
|
---|
50 | inline bool JsonVariant::is() const {
|
---|
51 | return false;
|
---|
52 | }
|
---|
53 |
|
---|
54 | template <> // in .cpp
|
---|
55 | bool JsonVariant::is<signed long>() const;
|
---|
56 |
|
---|
57 | template <> // in .cpp
|
---|
58 | bool JsonVariant::is<double>() const;
|
---|
59 |
|
---|
60 | template <> // int .cpp
|
---|
61 | bool JsonVariant::is<bool>() const;
|
---|
62 |
|
---|
63 | template <>
|
---|
64 | inline bool JsonVariant::is<char const *>() const {
|
---|
65 | return _type == Internals::JSON_STRING;
|
---|
66 | }
|
---|
67 |
|
---|
68 | template <>
|
---|
69 | inline bool JsonVariant::is<float>() const {
|
---|
70 | return is<double>();
|
---|
71 | }
|
---|
72 |
|
---|
73 | template <>
|
---|
74 | inline bool JsonVariant::is<JsonArray &>() const {
|
---|
75 | return _type == Internals::JSON_ARRAY;
|
---|
76 | }
|
---|
77 |
|
---|
78 | template <>
|
---|
79 | inline bool JsonVariant::is<JsonArray const &>() const {
|
---|
80 | return _type == Internals::JSON_ARRAY;
|
---|
81 | }
|
---|
82 |
|
---|
83 | template <>
|
---|
84 | inline bool JsonVariant::is<JsonObject &>() const {
|
---|
85 | return _type == Internals::JSON_OBJECT;
|
---|
86 | }
|
---|
87 |
|
---|
88 | template <>
|
---|
89 | inline bool JsonVariant::is<JsonObject const &>() const {
|
---|
90 | return _type == Internals::JSON_OBJECT;
|
---|
91 | }
|
---|
92 |
|
---|
93 | template <>
|
---|
94 | inline bool JsonVariant::is<signed char>() const {
|
---|
95 | return is<signed long>();
|
---|
96 | }
|
---|
97 |
|
---|
98 | template <>
|
---|
99 | inline bool JsonVariant::is<signed int>() const {
|
---|
100 | return is<signed long>();
|
---|
101 | }
|
---|
102 |
|
---|
103 | template <>
|
---|
104 | inline bool JsonVariant::is<signed short>() const {
|
---|
105 | return is<signed long>();
|
---|
106 | }
|
---|
107 |
|
---|
108 | template <>
|
---|
109 | inline bool JsonVariant::is<unsigned char>() const {
|
---|
110 | return is<signed long>();
|
---|
111 | }
|
---|
112 |
|
---|
113 | template <>
|
---|
114 | inline bool JsonVariant::is<unsigned int>() const {
|
---|
115 | return is<signed long>();
|
---|
116 | }
|
---|
117 |
|
---|
118 | template <>
|
---|
119 | inline bool JsonVariant::is<unsigned long>() const {
|
---|
120 | return is<signed long>();
|
---|
121 | }
|
---|
122 |
|
---|
123 | template <>
|
---|
124 | inline bool JsonVariant::is<unsigned short>() const {
|
---|
125 | return is<signed long>();
|
---|
126 | }
|
---|
127 |
|
---|
128 | inline Internals::JsonInteger JsonVariant::asInteger() const {
|
---|
129 | if (_type == Internals::JSON_INTEGER || _type == Internals::JSON_BOOLEAN)
|
---|
130 | return _content.asInteger;
|
---|
131 |
|
---|
132 | if (_type >= Internals::JSON_FLOAT_0_DECIMALS)
|
---|
133 | return static_cast<Internals::JsonInteger>(_content.asFloat);
|
---|
134 |
|
---|
135 | if ((_type == Internals::JSON_STRING || _type == Internals::JSON_UNPARSED) &&
|
---|
136 | _content.asString) {
|
---|
137 | if (!strcmp("true", _content.asString)) return 1;
|
---|
138 | return Internals::parse<Internals::JsonInteger>(_content.asString);
|
---|
139 | }
|
---|
140 |
|
---|
141 | return 0L;
|
---|
142 | }
|
---|
143 |
|
---|
144 | #if ARDUINOJSON_ENABLE_STD_STREAM
|
---|
145 | inline std::ostream &operator<<(std::ostream &os, const JsonVariant &source) {
|
---|
146 | return source.printTo(os);
|
---|
147 | }
|
---|
148 | #endif
|
---|
149 |
|
---|
150 | } // namespace ArduinoJson
|
---|