Files
ArduinoJson/include/ArduinoJson/JsonValue.hpp

71 lines
1.4 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>
#include "ForwardDeclarations.hpp"
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-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;
}
JsonValue &operator=(JsonArray& array) {
set(array);
return *this;
}
JsonValue &operator=(JsonObject& object) {
set(object);
return *this;
}
2014-10-29 14:24:34 +01:00
JsonArray &asArray();
JsonObject &asObject();
bool asBool() const;
const char *asString() const;
double asDouble() const;
long asLong() const;
2014-10-27 13:34:54 +01:00
2014-10-27 22:50:50 +01:00
template <typename T>
2014-10-29 21:18:27 +01:00
T as(){}
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-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-29 21:18:27 +01:00
template <>
int JsonValue::as<int>() { return asLong(); }
2014-10-23 23:45:36 +02:00
}