Files
ArduinoJson/include/ArduinoJson/JsonVariant.hpp

108 lines
3.2 KiB
C++
Raw Normal View History

// Copyright Benoit Blanchon 2014-2016
2014-10-23 23:39:22 +02:00
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
2014-10-23 23:39:22 +02:00
2014-10-16 00:11:23 +02:00
#pragma once
2014-10-26 21:18:09 +01:00
#include <stddef.h>
2014-11-03 18:28:24 +01:00
#include <stdint.h> // for uint8_t
2014-10-26 21:18:09 +01:00
2014-11-05 08:53:41 +01:00
#include "Internals/JsonPrintable.hpp"
2014-11-04 09:51:25 +01:00
#include "Internals/JsonVariantContent.hpp"
#include "Internals/JsonVariantType.hpp"
#include "Internals/Unparsed.hpp"
#include "JsonVariantBase.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
2014-11-06 16:58:24 +01:00
// Forward declarations.
2014-10-31 12:27:33 +01:00
class JsonArray;
class JsonObject;
2014-11-06 16:58:24 +01:00
// A variant that can be a any value serializable to a JSON value.
//
// It can be set to:
// - a boolean
// - a char, short, int or a long (signed or unsigned)
// - a string (const char*)
// - a reference to a JsonArray or JsonObject
class JsonVariant : public JsonVariantBase<JsonVariant> {
public:
2014-11-06 16:58:24 +01:00
// Creates an uninitialized JsonVariant
FORCE_INLINE JsonVariant() : _type(Internals::JSON_UNDEFINED) {}
2014-10-29 14:24:34 +01:00
// Create a JsonVariant containing a boolean value.
2014-11-06 16:58:24 +01:00
// It will be serialized as "true" or "false" in JSON.
FORCE_INLINE JsonVariant(bool value);
2014-11-06 16:58:24 +01:00
// Create a JsonVariant containing a floating point value.
2014-11-06 16:58:24 +01:00
// The second argument specifies the number of decimal digits to write in
// the JSON string.
FORCE_INLINE JsonVariant(float value, uint8_t decimals = 2);
FORCE_INLINE JsonVariant(double value, uint8_t decimals = 2);
2014-10-29 14:24:34 +01:00
// Create a JsonVariant containing an integer value.
FORCE_INLINE JsonVariant(signed char value);
FORCE_INLINE JsonVariant(signed long value);
FORCE_INLINE JsonVariant(signed int value);
FORCE_INLINE JsonVariant(signed short value);
FORCE_INLINE JsonVariant(unsigned char value);
FORCE_INLINE JsonVariant(unsigned long value);
FORCE_INLINE JsonVariant(unsigned int value);
FORCE_INLINE JsonVariant(unsigned short value);
2014-11-06 16:58:24 +01:00
// Create a JsonVariant containing a string.
FORCE_INLINE JsonVariant(const char *value);
2014-11-06 16:58:24 +01:00
// Create a JsonVariant containing an unparsed string
FORCE_INLINE JsonVariant(Internals::Unparsed value);
// Create a JsonVariant containing a reference to an array.
FORCE_INLINE JsonVariant(JsonArray &array);
2014-11-06 16:58:24 +01:00
// Create a JsonVariant containing a reference to an object.
FORCE_INLINE JsonVariant(JsonObject &object);
2014-10-27 13:34:54 +01:00
2014-11-06 16:58:24 +01:00
// Get the variant as the specified type.
// See cast operators for details.
2014-10-27 22:50:50 +01:00
template <typename T>
T as() const;
2014-10-23 19:54:00 +02:00
2014-11-06 16:58:24 +01:00
// Tells weither the variant has the specified type.
// Returns true if the variant has type type T, false otherwise.
2014-11-03 18:23:39 +01:00
template <typename T>
bool is() const;
2014-10-24 16:12:05 +02:00
2014-11-06 16:58:24 +01:00
// Serialize the variant to a JsonWriter
void writeTo(Internals::JsonWriter &writer) const;
2014-10-26 21:18:09 +01:00
// TODO: rename
2014-11-06 16:58:24 +01:00
template <typename T>
static T invalid();
2014-10-30 14:03:33 +01:00
private:
Internals::JsonFloat asFloat() const;
Internals::JsonInteger asInteger() const;
2014-11-06 16:58:24 +01:00
// The current type of the variant
2014-11-04 09:51:25 +01:00
Internals::JsonVariantType _type;
2014-11-06 16:58:24 +01:00
// The various alternatives for the value of the variant.
2014-11-04 09:51:25 +01:00
Internals::JsonVariantContent _content;
2014-10-23 19:54:00 +02:00
};
2014-11-03 14:37:50 +01:00
inline JsonVariant float_with_n_digits(float value, uint8_t digits) {
return JsonVariant(value, digits);
2014-11-03 18:23:39 +01:00
}
inline JsonVariant double_with_n_digits(double value, uint8_t digits) {
return JsonVariant(value, digits);
2014-11-03 18:23:39 +01:00
}
}
// Include inline implementations
#include "JsonVariant.ipp"