| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- // Copyright Benoit Blanchon 2014-2015
- // MIT License
- //
- // Arduino JSON library
- // https://github.com/bblanchon/ArduinoJson
- #pragma once
- #include "JsonVariant.hpp"
- namespace ArduinoJson {
- inline JsonVariant::JsonVariant(bool value) {
- _type = Internals::JSON_BOOLEAN;
- _content.asBoolean = value;
- }
- inline JsonVariant::JsonVariant(const char *value) {
- _type = Internals::JSON_STRING;
- _content.asString = value;
- }
- inline JsonVariant::JsonVariant(const String &value) {
- _type = Internals::JSON_STRING;
- _content.asString = value.c_str();
- }
- inline JsonVariant::JsonVariant(double value, uint8_t decimals) {
- _type = static_cast<Internals::JsonVariantType>(
- Internals::JSON_DOUBLE_0_DECIMALS + decimals);
- _content.asDouble = value;
- }
- inline JsonVariant::JsonVariant(float value, uint8_t decimals) {
- _type = static_cast<Internals::JsonVariantType>(
- Internals::JSON_DOUBLE_0_DECIMALS + decimals);
- _content.asDouble = value;
- }
- inline JsonVariant::JsonVariant(JsonArray &array) {
- _type = Internals::JSON_ARRAY;
- _content.asArray = &array;
- }
- inline JsonVariant::JsonVariant(JsonObject &object) {
- _type = Internals::JSON_OBJECT;
- _content.asObject = &object;
- }
- inline JsonVariant::JsonVariant(signed char value) {
- _type = Internals::JSON_LONG;
- _content.asLong = value;
- }
- inline JsonVariant::JsonVariant(signed int value) {
- _type = Internals::JSON_LONG;
- _content.asLong = value;
- }
- inline JsonVariant::JsonVariant(signed long value) {
- _type = Internals::JSON_LONG;
- _content.asLong = value;
- }
- inline JsonVariant::JsonVariant(signed short value) {
- _type = Internals::JSON_LONG;
- _content.asLong = value;
- }
- inline JsonVariant::JsonVariant(unsigned char value) {
- _type = Internals::JSON_LONG;
- _content.asLong = value;
- }
- inline JsonVariant::JsonVariant(unsigned int value) {
- _type = Internals::JSON_LONG;
- _content.asLong = value;
- }
- inline JsonVariant::JsonVariant(unsigned long value) {
- _type = Internals::JSON_LONG;
- _content.asLong = value;
- }
- inline JsonVariant::JsonVariant(unsigned short value) {
- _type = Internals::JSON_LONG;
- _content.asLong = value;
- }
- template <typename T>
- inline T JsonVariant::as() const {
- return is<T>() ? _content.as<T>() : invalid<T>();
- }
- template <typename T>
- inline T JsonVariant::invalid() {
- return T();
- }
- template <typename T>
- inline bool JsonVariant::is() const {
- return false;
- }
- template <>
- inline bool JsonVariant::is<bool>() const {
- return _type == Internals::JSON_BOOLEAN;
- }
- template <>
- inline bool JsonVariant::is<char const *>() const {
- return _type == Internals::JSON_STRING;
- }
- template <>
- inline bool JsonVariant::is<String>() const {
- return _type == Internals::JSON_STRING;
- }
- template <>
- inline bool JsonVariant::is<double>() const {
- return _type >= Internals::JSON_DOUBLE_0_DECIMALS;
- }
- template <>
- inline bool JsonVariant::is<float>() const {
- return _type >= Internals::JSON_DOUBLE_0_DECIMALS;
- }
- template <>
- inline bool JsonVariant::is<JsonArray &>() const {
- return _type == Internals::JSON_ARRAY;
- }
- template <>
- inline bool JsonVariant::is<JsonArray const &>() const {
- return _type == Internals::JSON_ARRAY;
- }
- template <>
- inline bool JsonVariant::is<JsonObject &>() const {
- return _type == Internals::JSON_OBJECT;
- }
- template <>
- inline bool JsonVariant::is<JsonObject const &>() const {
- return _type == Internals::JSON_OBJECT;
- }
- template <>
- inline bool JsonVariant::is<signed char>() const {
- return _type == Internals::JSON_LONG;
- }
- template <>
- inline bool JsonVariant::is<signed int>() const {
- return _type == Internals::JSON_LONG;
- }
- template <>
- inline bool JsonVariant::is<signed long>() const {
- return _type == Internals::JSON_LONG;
- }
- template <>
- inline bool JsonVariant::is<signed short>() const {
- return _type == Internals::JSON_LONG;
- }
- template <>
- inline bool JsonVariant::is<unsigned char>() const {
- return _type == Internals::JSON_LONG;
- }
- template <>
- inline bool JsonVariant::is<unsigned int>() const {
- return _type == Internals::JSON_LONG;
- }
- template <>
- inline bool JsonVariant::is<unsigned long>() const {
- return _type == Internals::JSON_LONG;
- }
- template <>
- inline bool JsonVariant::is<unsigned short>() const {
- return _type == Internals::JSON_LONG;
- }
- #ifdef ARDUINOJSON_ENABLE_STD_STREAM
- inline std::ostream& operator<<(std::ostream& os, const JsonVariant& source) {
- return source.printTo(os);
- }
- #endif
- } // namespace ArduinoJson
|