2014-10-23 23:39:22 +02:00
|
|
|
// Copyright Benoit Blanchon 2014
|
|
|
|
// MIT License
|
|
|
|
//
|
|
|
|
// Arduino JSON library
|
|
|
|
// https://github.com/bblanchon/ArduinoJson
|
|
|
|
|
2014-10-29 14:24:34 +01:00
|
|
|
#include "ArduinoJson/JsonValue.hpp"
|
2014-10-19 15:46:36 +02:00
|
|
|
#include "ArduinoJson/JsonArray.hpp"
|
2014-10-27 13:34:54 +01:00
|
|
|
#include "ArduinoJson/JsonObject.hpp"
|
2014-10-29 14:24:34 +01:00
|
|
|
#include "ArduinoJson/Internals/JsonWriter.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-10-30 14:03:33 +01:00
|
|
|
JsonValue JsonValue::_invalid(JSON_INVALID);
|
|
|
|
|
2014-10-30 10:49:02 +01:00
|
|
|
JsonValue::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-10-30 10:49:02 +01:00
|
|
|
JsonValue::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-10-30 10:49:02 +01:00
|
|
|
JsonValue::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-10-30 10:49:02 +01:00
|
|
|
JsonValue::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-10-30 10:49:02 +01:00
|
|
|
JsonValue::operator double() const {
|
2014-10-29 14:24:34 +01:00
|
|
|
return _type >= JSON_DOUBLE_0_DECIMALS ? _content.asDouble : 0;
|
|
|
|
}
|
|
|
|
|
2014-10-30 10:49:02 +01:00
|
|
|
JsonValue::operator long() const {
|
2014-10-29 14:24:34 +01:00
|
|
|
return _type == JSON_LONG ? _content.asInteger : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonValue::set(bool value) {
|
|
|
|
if (_type == JSON_INVALID) return;
|
|
|
|
_type = Internals::JSON_BOOLEAN;
|
|
|
|
_content.asBoolean = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonValue::set(const char *value) {
|
|
|
|
if (_type == JSON_INVALID) return;
|
|
|
|
_type = JSON_STRING;
|
|
|
|
_content.asString = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonValue::set(double value, int decimals) {
|
|
|
|
if (_type == JSON_INVALID) return;
|
|
|
|
_type = static_cast<JsonValueType>(JSON_DOUBLE_0_DECIMALS + decimals);
|
|
|
|
_content.asDouble = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonValue::set(long value) {
|
|
|
|
if (_type == JSON_INVALID) return;
|
|
|
|
_type = JSON_LONG;
|
|
|
|
_content.asInteger = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonValue::set(JsonArray &array) {
|
|
|
|
if (_type == JSON_INVALID) return;
|
|
|
|
_type = JSON_ARRAY;
|
|
|
|
_content.asArray = &array;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonValue::set(JsonObject &object) {
|
|
|
|
if (_type == JSON_INVALID) return;
|
|
|
|
_type = JSON_OBJECT;
|
|
|
|
_content.asObject = &object;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonValue::writeTo(JsonWriter &writer) const {
|
|
|
|
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:
|
|
|
|
writer.writeInteger(_content.asInteger);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSON_BOOLEAN:
|
|
|
|
writer.writeBoolean(_content.asBoolean);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: // >= JSON_DOUBLE_0_DECIMALS
|
|
|
|
writer.writeDouble(_content.asDouble, _type - JSON_DOUBLE_0_DECIMALS);
|
|
|
|
break;
|
|
|
|
}
|
2014-10-30 10:49:02 +01:00
|
|
|
}
|