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 | #include "../include/ArduinoJson/JsonVariant.hpp"
|
---|
9 |
|
---|
10 | #include "../include/ArduinoJson/JsonArray.hpp"
|
---|
11 | #include "../include/ArduinoJson/JsonObject.hpp"
|
---|
12 |
|
---|
13 | #include <errno.h> // for errno
|
---|
14 | #include <stdlib.h> // for strtol, strtod
|
---|
15 |
|
---|
16 | using namespace ArduinoJson::Internals;
|
---|
17 |
|
---|
18 | namespace ArduinoJson {
|
---|
19 |
|
---|
20 | const char *JsonVariant::asString() const {
|
---|
21 | if (_type == JSON_UNPARSED && _content.asString &&
|
---|
22 | !strcmp("null", _content.asString))
|
---|
23 | return NULL;
|
---|
24 | if (_type == JSON_STRING || _type == JSON_UNPARSED) return _content.asString;
|
---|
25 | return NULL;
|
---|
26 | }
|
---|
27 |
|
---|
28 | JsonFloat JsonVariant::asFloat() const {
|
---|
29 | if (_type >= JSON_FLOAT_0_DECIMALS) return _content.asFloat;
|
---|
30 |
|
---|
31 | if (_type == JSON_INTEGER || _type == JSON_BOOLEAN)
|
---|
32 | return static_cast<JsonFloat>(_content.asInteger);
|
---|
33 |
|
---|
34 | if ((_type == JSON_STRING || _type == JSON_UNPARSED) && _content.asString)
|
---|
35 | return parse<JsonFloat>(_content.asString);
|
---|
36 |
|
---|
37 | return 0.0;
|
---|
38 | }
|
---|
39 |
|
---|
40 | String JsonVariant::toString() const {
|
---|
41 | String s;
|
---|
42 | if ((_type == JSON_STRING || _type == JSON_UNPARSED) &&
|
---|
43 | _content.asString != NULL)
|
---|
44 | s = _content.asString;
|
---|
45 | else
|
---|
46 | printTo(s);
|
---|
47 | return s;
|
---|
48 | }
|
---|
49 |
|
---|
50 | template <>
|
---|
51 | bool JsonVariant::is<bool>() const {
|
---|
52 | if (_type == JSON_BOOLEAN) return true;
|
---|
53 |
|
---|
54 | if (_type != JSON_UNPARSED || _content.asString == NULL) return false;
|
---|
55 |
|
---|
56 | return !strcmp(_content.asString, "true") ||
|
---|
57 | !strcmp(_content.asString, "false");
|
---|
58 | }
|
---|
59 |
|
---|
60 | template <>
|
---|
61 | bool JsonVariant::is<signed long>() const {
|
---|
62 | if (_type == JSON_INTEGER) return true;
|
---|
63 |
|
---|
64 | if (_type != JSON_UNPARSED || _content.asString == NULL) return false;
|
---|
65 |
|
---|
66 | char *end;
|
---|
67 | errno = 0;
|
---|
68 | strtol(_content.asString, &end, 10);
|
---|
69 |
|
---|
70 | return *end == '\0' && errno == 0;
|
---|
71 | }
|
---|
72 |
|
---|
73 | template <>
|
---|
74 | bool JsonVariant::is<double>() const {
|
---|
75 | if (_type >= JSON_FLOAT_0_DECIMALS) return true;
|
---|
76 |
|
---|
77 | if (_type != JSON_UNPARSED || _content.asString == NULL) return false;
|
---|
78 |
|
---|
79 | char *end;
|
---|
80 | errno = 0;
|
---|
81 | strtod(_content.asString, &end);
|
---|
82 |
|
---|
83 | return *end == '\0' && errno == 0 && !is<long>();
|
---|
84 | }
|
---|
85 |
|
---|
86 | void JsonVariant::writeTo(JsonWriter &writer) const {
|
---|
87 | if (_type == JSON_ARRAY)
|
---|
88 | _content.asArray->writeTo(writer);
|
---|
89 |
|
---|
90 | else if (_type == JSON_OBJECT)
|
---|
91 | _content.asObject->writeTo(writer);
|
---|
92 |
|
---|
93 | else if (_type == JSON_STRING)
|
---|
94 | writer.writeString(_content.asString);
|
---|
95 |
|
---|
96 | else if (_type == JSON_UNPARSED)
|
---|
97 | writer.writeRaw(_content.asString);
|
---|
98 |
|
---|
99 | else if (_type == JSON_INTEGER)
|
---|
100 | writer.writeInteger(_content.asInteger);
|
---|
101 |
|
---|
102 | else if (_type == JSON_BOOLEAN)
|
---|
103 | writer.writeBoolean(_content.asInteger != 0);
|
---|
104 |
|
---|
105 | else if (_type >= JSON_FLOAT_0_DECIMALS) {
|
---|
106 | uint8_t decimals = static_cast<uint8_t>(_type - JSON_FLOAT_0_DECIMALS);
|
---|
107 | writer.writeFloat(_content.asFloat, decimals);
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|