Files
ArduinoJson/include/ArduinoJson/JsonValue.hpp

81 lines
1.7 KiB
C++
Raw Normal View History

2014-10-23 23:39:22 +02:00
// Copyright Benoit Blanchon 2014
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
2014-10-16 00:11:23 +02:00
#pragma once
2014-10-26 21:18:09 +01:00
#include <stddef.h>
2014-10-29 14:24:34 +01:00
#include "Internals/JsonValueContent.hpp"
#include "Internals/JsonValueType.hpp"
2014-10-16 00:11:23 +02:00
2014-10-23 19:54:00 +02:00
namespace ArduinoJson {
2014-10-31 12:27:33 +01:00
class JsonArray;
class JsonObject;
namespace Internals {
class JsonWriter;
}
2014-10-26 21:18:09 +01:00
class JsonValue {
public:
2014-10-29 14:24:34 +01:00
JsonValue() : _type(Internals::JSON_UNDEFINED) {}
void set(bool value);
void set(const char *value);
void set(double value, int decimals = 2);
void set(int value) { set(static_cast<long>(value)); }
void set(long value);
void set(JsonArray &array);
void set(JsonObject &object);
2014-10-29 21:18:27 +01:00
template <typename T>
JsonValue &operator=(T value) {
set(value);
return *this;
}
2014-10-30 10:49:02 +01:00
JsonValue &operator=(JsonArray &array) {
2014-10-29 21:18:27 +01:00
set(array);
return *this;
}
2014-10-30 10:49:02 +01:00
JsonValue &operator=(JsonObject &object) {
2014-10-29 21:18:27 +01:00
set(object);
return *this;
}
2014-10-30 10:49:02 +01:00
operator bool() const;
operator const char *() const;
operator double() const;
operator long() const;
2014-10-30 15:31:27 +01:00
operator int() const { return operator long(); }
2014-10-30 10:49:02 +01:00
operator JsonArray &() const;
operator JsonObject &() const;
JsonArray &asArray() { return static_cast<JsonArray &>(*this); };
JsonObject &asObject() { return static_cast<JsonObject &>(*this); };
2014-10-27 13:34:54 +01:00
2014-10-27 22:50:50 +01:00
template <typename T>
2014-10-30 10:49:02 +01:00
T as() {
return static_cast<T>(*this);
}
2014-10-23 19:54:00 +02:00
2014-10-29 14:24:34 +01:00
static JsonValue &invalid() { return _invalid; }
2014-10-24 16:12:05 +02:00
2014-10-29 14:24:34 +01:00
bool success() { return _type != Internals::JSON_INVALID; }
void writeTo(Internals::JsonWriter &writer) const;
2014-10-26 21:18:09 +01:00
private:
2014-10-30 14:03:33 +01:00
JsonValue(Internals::JsonValueType type) : _type(type) {}
2014-10-29 14:24:34 +01:00
Internals::JsonValueType _type;
Internals::JsonValueContent _content;
static JsonValue _invalid;
2014-10-23 19:54:00 +02:00
};
2014-10-30 14:03:33 +01:00
}