Added support of String class (issue #55, #56, #70, #77)

This commit is contained in:
Benoit Blanchon
2015-05-25 15:38:58 +02:00
parent 756c279cdc
commit 1b5be892b9
22 changed files with 351 additions and 119 deletions

View File

@@ -7,6 +7,7 @@
#pragma once
#include "Internals/ForceInline.hpp"
#include "JsonObjectKey.hpp"
namespace ArduinoJson {
@@ -19,50 +20,45 @@ class JsonVariantBase {
public:
// Gets the variant as a boolean value.
// Returns false if the variant is not a boolean value.
JSON_FORCE_INLINE operator bool() const { return as<bool>(); }
FORCE_INLINE operator bool() const { return as<bool>(); }
// Gets the variant as a floating-point value.
// Returns 0.0 if the variant is not a floating-point value
JSON_FORCE_INLINE operator double() const { return as<double>(); }
JSON_FORCE_INLINE operator float() const { return as<float>(); }
FORCE_INLINE operator double() const { return as<double>(); }
FORCE_INLINE operator float() const { return as<float>(); }
// Gets the variant as an integer value.
// Returns 0 if the variant is not an integer value.
JSON_FORCE_INLINE operator signed long() const { return as<signed long>(); }
JSON_FORCE_INLINE operator signed char() const { return as<signed char>(); }
JSON_FORCE_INLINE operator signed int() const { return as<signed int>(); }
JSON_FORCE_INLINE operator signed short() const { return as<signed short>(); }
JSON_FORCE_INLINE operator unsigned char() const {
return as<unsigned char>();
}
JSON_FORCE_INLINE operator unsigned int() const { return as<unsigned int>(); }
JSON_FORCE_INLINE operator unsigned long() const {
return as<unsigned long>();
}
JSON_FORCE_INLINE operator unsigned short() const {
return as<unsigned short>();
}
FORCE_INLINE operator signed long() const { return as<signed long>(); }
FORCE_INLINE operator signed char() const { return as<signed char>(); }
FORCE_INLINE operator signed int() const { return as<signed int>(); }
FORCE_INLINE operator signed short() const { return as<signed short>(); }
FORCE_INLINE operator unsigned char() const { return as<unsigned char>(); }
FORCE_INLINE operator unsigned int() const { return as<unsigned int>(); }
FORCE_INLINE operator unsigned long() const { return as<unsigned long>(); }
FORCE_INLINE operator unsigned short() const { return as<unsigned short>(); }
// Gets the variant as a string.
// Returns NULL if variant is not a string.
JSON_FORCE_INLINE operator const char *() const { return as<const char *>(); }
JSON_FORCE_INLINE const char *asString() const { return as<const char *>(); }
FORCE_INLINE operator const char *() const { return as<const char *>(); }
FORCE_INLINE const char *asString() const { return as<const char *>(); }
FORCE_INLINE operator String() const { return as<String>(); }
// Gets the variant as an array.
// Returns a reference to the JsonArray or JsonArray::invalid() if the
// variant
// is not an array.
JSON_FORCE_INLINE operator JsonArray &() const { return as<JsonArray &>(); }
JSON_FORCE_INLINE JsonArray &asArray() const { return as<JsonArray &>(); }
FORCE_INLINE operator JsonArray &() const { return as<JsonArray &>(); }
FORCE_INLINE JsonArray &asArray() const { return as<JsonArray &>(); }
// Gets the variant as an object.
// Returns a reference to the JsonObject or JsonObject::invalid() if the
// variant is not an object.
JSON_FORCE_INLINE operator JsonObject &() const { return as<JsonObject &>(); }
JSON_FORCE_INLINE JsonObject &asObject() const { return as<JsonObject &>(); }
FORCE_INLINE operator JsonObject &() const { return as<JsonObject &>(); }
FORCE_INLINE JsonObject &asObject() const { return as<JsonObject &>(); }
template <typename T>
JSON_FORCE_INLINE const T as() const {
FORCE_INLINE const T as() const {
return impl()->template as<T>();
}
@@ -74,13 +70,17 @@ class JsonVariantBase {
// Mimics an array.
// Returns the element at specified index if the variant is an array.
// Returns JsonVariant::invalid() if the variant is not an array.
JSON_FORCE_INLINE const JsonArraySubscript operator[](int index) const;
FORCE_INLINE const JsonArraySubscript operator[](int index) const;
// Mimics an object.
// Returns the value associated with the specified key if the variant is
// an object.
// Return JsonVariant::invalid() if the variant is not an object.
JSON_FORCE_INLINE const JsonObjectSubscript operator[](const char *key) const;
FORCE_INLINE const JsonObjectSubscript operator[](const char *key) const;
FORCE_INLINE const JsonObjectSubscript operator[](const String &key) const;
protected:
JsonVariantBase() {}
private:
const TImpl *impl() const { return static_cast<const TImpl *>(this); }