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

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

BlueMix用のフィアルを追加

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