2014-10-23 23:39:22 +02:00
|
|
|
// Copyright Benoit Blanchon 2014
|
|
|
|
// MIT License
|
|
|
|
//
|
|
|
|
// Arduino JSON library
|
|
|
|
// https://github.com/bblanchon/ArduinoJson
|
|
|
|
|
2014-11-04 09:51:25 +01:00
|
|
|
#include "../include/ArduinoJson/JsonVariant.hpp"
|
2014-11-04 10:01:21 +01:00
|
|
|
|
|
|
|
#include "../include/ArduinoJson/Internals/PrettyJsonWriter.hpp"
|
2014-11-03 18:35:22 +01:00
|
|
|
#include "../include/ArduinoJson/JsonArray.hpp"
|
|
|
|
#include "../include/ArduinoJson/JsonObject.hpp"
|
2014-10-16 16:25:42 +02:00
|
|
|
|
2014-10-18 23:05:54 +02:00
|
|
|
using namespace ArduinoJson;
|
2014-10-29 14:24:34 +01:00
|
|
|
using namespace ArduinoJson::Internals;
|
|
|
|
|
2014-11-04 09:51:25 +01:00
|
|
|
JsonVariant JsonVariant::_invalid(JSON_INVALID);
|
2014-10-30 14:03:33 +01:00
|
|
|
|
2014-11-04 09:51:25 +01:00
|
|
|
JsonVariant::operator JsonArray &() const {
|
2014-10-29 14:24:34 +01:00
|
|
|
return _type == JSON_ARRAY ? *_content.asArray : JsonArray::invalid();
|
|
|
|
}
|
2014-10-18 23:05:54 +02:00
|
|
|
|
2014-11-04 09:51:25 +01:00
|
|
|
JsonVariant::operator JsonObject &() const {
|
2014-10-29 14:24:34 +01:00
|
|
|
return _type == JSON_OBJECT ? *_content.asObject : JsonObject::invalid();
|
2014-10-16 16:25:42 +02:00
|
|
|
}
|
2014-10-27 13:34:54 +01:00
|
|
|
|
2014-11-04 09:51:25 +01:00
|
|
|
JsonVariant::operator bool() const {
|
2014-10-29 14:24:34 +01:00
|
|
|
return _type == JSON_BOOLEAN ? _content.asBoolean : false;
|
2014-10-27 13:34:54 +01:00
|
|
|
}
|
|
|
|
|
2014-11-04 09:51:25 +01:00
|
|
|
JsonVariant::operator const char *() const {
|
2014-10-29 14:24:34 +01:00
|
|
|
return _type == JSON_STRING ? _content.asString : NULL;
|
2014-10-27 13:34:54 +01:00
|
|
|
}
|
|
|
|
|
2014-11-04 09:51:25 +01:00
|
|
|
JsonVariant::operator double() const {
|
2014-10-29 14:24:34 +01:00
|
|
|
return _type >= JSON_DOUBLE_0_DECIMALS ? _content.asDouble : 0;
|
|
|
|
}
|
|
|
|
|
2014-11-04 09:51:25 +01:00
|
|
|
JsonVariant::operator long() const {
|
2014-10-31 12:02:15 +01:00
|
|
|
return _type == JSON_LONG ? _content.asLong : 0;
|
2014-10-29 14:24:34 +01:00
|
|
|
}
|
|
|
|
|
2014-11-04 09:51:25 +01:00
|
|
|
void JsonVariant::set(bool value) {
|
2014-10-29 14:24:34 +01:00
|
|
|
if (_type == JSON_INVALID) return;
|
|
|
|
_type = Internals::JSON_BOOLEAN;
|
|
|
|
_content.asBoolean = value;
|
|
|
|
}
|
|
|
|
|
2014-11-04 09:51:25 +01:00
|
|
|
void JsonVariant::set(const char *value) {
|
2014-10-29 14:24:34 +01:00
|
|
|
if (_type == JSON_INVALID) return;
|
|
|
|
_type = JSON_STRING;
|
|
|
|
_content.asString = value;
|
|
|
|
}
|
|
|
|
|
2014-11-04 09:51:25 +01:00
|
|
|
void JsonVariant::set(double value, uint8_t decimals) {
|
2014-10-29 14:24:34 +01:00
|
|
|
if (_type == JSON_INVALID) return;
|
2014-11-04 09:51:25 +01:00
|
|
|
_type = static_cast<JsonVariantType>(JSON_DOUBLE_0_DECIMALS + decimals);
|
2014-10-29 14:24:34 +01:00
|
|
|
_content.asDouble = value;
|
|
|
|
}
|
|
|
|
|
2014-11-04 09:51:25 +01:00
|
|
|
void JsonVariant::set(long value) {
|
2014-10-29 14:24:34 +01:00
|
|
|
if (_type == JSON_INVALID) return;
|
|
|
|
_type = JSON_LONG;
|
2014-10-31 12:02:15 +01:00
|
|
|
_content.asLong = value;
|
2014-10-29 14:24:34 +01:00
|
|
|
}
|
|
|
|
|
2014-11-04 09:51:25 +01:00
|
|
|
void JsonVariant::set(JsonArray &array) {
|
2014-10-29 14:24:34 +01:00
|
|
|
if (_type == JSON_INVALID) return;
|
|
|
|
_type = JSON_ARRAY;
|
|
|
|
_content.asArray = &array;
|
|
|
|
}
|
|
|
|
|
2014-11-04 09:51:25 +01:00
|
|
|
void JsonVariant::set(JsonObject &object) {
|
2014-10-29 14:24:34 +01:00
|
|
|
if (_type == JSON_INVALID) return;
|
|
|
|
_type = JSON_OBJECT;
|
|
|
|
_content.asObject = &object;
|
|
|
|
}
|
|
|
|
|
2014-11-04 10:20:47 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-11-04 10:30:45 +01:00
|
|
|
JsonVariant &JsonVariant::operator[](const char *key) {
|
|
|
|
if (_type != JSON_OBJECT) return JsonVariant::invalid();
|
|
|
|
return _content.asObject->operator[](key);
|
|
|
|
}
|
|
|
|
|
2014-11-03 12:51:24 +01:00
|
|
|
template <typename T>
|
2014-11-04 09:51:25 +01:00
|
|
|
void JsonVariant::writeTo(T &writer) const {
|
2014-10-29 14:24:34 +01:00
|
|
|
switch (_type) {
|
|
|
|
case JSON_ARRAY:
|
|
|
|
_content.asArray->writeTo(writer);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSON_OBJECT:
|
|
|
|
_content.asObject->writeTo(writer);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSON_STRING:
|
|
|
|
writer.writeString(_content.asString);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSON_LONG:
|
2014-10-31 12:02:15 +01:00
|
|
|
writer.writeInteger(_content.asLong);
|
2014-10-29 14:24:34 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case JSON_BOOLEAN:
|
|
|
|
writer.writeBoolean(_content.asBoolean);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: // >= JSON_DOUBLE_0_DECIMALS
|
2014-11-07 11:59:07 +01:00
|
|
|
uint8_t decimals = static_cast<uint8_t>(_type - JSON_DOUBLE_0_DECIMALS);
|
|
|
|
writer.writeDouble(_content.asDouble, decimals);
|
2014-10-29 14:24:34 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-10-30 10:49:02 +01:00
|
|
|
}
|
2014-11-03 12:51:24 +01:00
|
|
|
|
2014-11-04 09:51:25 +01:00
|
|
|
template void JsonVariant::writeTo(JsonWriter &) const;
|
|
|
|
template void JsonVariant::writeTo(PrettyJsonWriter &) const;
|