JsonVariant automatically promotes to JsonObject or JsonArray on write

This commit is contained in:
Benoit Blanchon
2019-01-29 14:09:09 +01:00
parent 5aea1363cc
commit 6f55d1e58f
53 changed files with 1197 additions and 541 deletions

View File

@ -14,10 +14,10 @@
#include "ArduinoJson/Document/StaticJsonDocument.hpp"
#include "ArduinoJson/Array/ArrayImpl.hpp"
#include "ArduinoJson/Array/ArraySubscript.hpp"
#include "ArduinoJson/Array/ElementProxy.hpp"
#include "ArduinoJson/Collection/CollectionImpl.hpp"
#include "ArduinoJson/Object/MemberProxy.hpp"
#include "ArduinoJson/Object/ObjectImpl.hpp"
#include "ArduinoJson/Object/ObjectSubscript.hpp"
#include "ArduinoJson/Variant/VariantAsImpl.hpp"
#include "ArduinoJson/Variant/VariantImpl.hpp"

View File

@ -9,11 +9,14 @@
namespace ARDUINOJSON_NAMESPACE {
inline ArrayRef ArrayRef::createNestedArray() const {
return add().to<ArrayRef>();
template <typename TArray>
inline ArrayRef ArrayShortcuts<TArray>::createNestedArray() const {
return impl()->add().template to<ArrayRef>();
}
inline ObjectRef ArrayRef::createNestedObject() const {
return add().to<ObjectRef>();
template <typename TArray>
inline ObjectRef ArrayShortcuts<TArray>::createNestedObject() const {
return impl()->add().template to<ObjectRef>();
}
} // namespace ARDUINOJSON_NAMESPACE

View File

@ -16,11 +16,16 @@
namespace ARDUINOJSON_NAMESPACE {
class ObjectRef;
class ArraySubscript;
template <typename>
class ElementProxy;
template <typename TData>
class ArrayRefBase {
public:
operator VariantConstRef() const {
return VariantConstRef(reinterpret_cast<const VariantData*>(_data));
}
template <typename Visitor>
FORCE_INLINE void accept(Visitor& visitor) const {
arrayAccept(_data, visitor);
@ -30,10 +35,6 @@ class ArrayRefBase {
return _data == 0;
}
FORCE_INLINE VariantConstRef operator[](size_t index) const {
return VariantConstRef(_data ? _data->get(index) : 0);
}
FORCE_INLINE size_t memoryUsage() const {
return _data ? _data->memoryUsage() : 0;
}
@ -74,9 +75,15 @@ class ArrayConstRef : public ArrayRefBase<const CollectionData>,
FORCE_INLINE bool operator==(ArrayConstRef rhs) const {
return arrayEquals(_data, rhs._data);
}
FORCE_INLINE VariantConstRef operator[](size_t index) const {
return VariantConstRef(_data ? _data->get(index) : 0);
}
};
class ArrayRef : public ArrayRefBase<CollectionData>, public Visitable {
class ArrayRef : public ArrayRefBase<CollectionData>,
public ArrayShortcuts<ArrayRef>,
public Visitable {
typedef ArrayRefBase<CollectionData> base_type;
public:
@ -94,27 +101,7 @@ class ArrayRef : public ArrayRefBase<CollectionData>, public Visitable {
return ArrayConstRef(_data);
}
// Adds the specified value at the end of the array.
//
// bool add(TValue);
// TValue = bool, long, int, short, float, double, serialized, VariantRef,
// std::string, String, ObjectRef
template <typename T>
FORCE_INLINE bool add(const T& value) const {
return add().set(value);
}
// Adds the specified value at the end of the array.
FORCE_INLINE bool add(ArrayConstRef value) const {
return add().set(value);
}
//
// bool add(TValue);
// TValue = char*, const char*, const __FlashStringHelper*
template <typename T>
FORCE_INLINE bool add(T* value) const {
return add().set(value);
}
using ArrayShortcuts::add;
VariantRef add() const {
return VariantRef(_pool, arrayAdd(_data, _pool));
}
@ -187,11 +174,6 @@ class ArrayRef : public ArrayRefBase<CollectionData>, public Visitable {
}
}
FORCE_INLINE ArrayRef createNestedArray() const;
FORCE_INLINE ObjectRef createNestedObject() const;
FORCE_INLINE ArraySubscript operator[](size_t index) const;
FORCE_INLINE bool operator==(ArrayRef rhs) const {
return arrayEquals(_data, rhs._data);
}

View File

@ -0,0 +1,47 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#pragma once
#include "../Polyfills/attributes.hpp"
#include "../Polyfills/type_traits.hpp"
namespace ARDUINOJSON_NAMESPACE {
// Forward declarations.
template <typename>
class ElementProxy;
template <typename TArray>
class ArrayShortcuts {
public:
// Returns the element at specified index if the variant is an array.
FORCE_INLINE ElementProxy<const TArray &> operator[](size_t index) const;
FORCE_INLINE ObjectRef createNestedObject() const;
FORCE_INLINE ArrayRef createNestedArray() const;
// Adds the specified value at the end of the array.
//
// bool add(TValue);
// TValue = bool, long, int, short, float, double, serialized, VariantRef,
// std::string, String, ObjectRef
template <typename T>
FORCE_INLINE bool add(const T &value) const {
return impl()->add().set(value);
}
//
// bool add(TValue);
// TValue = char*, const char*, const __FlashStringHelper*
template <typename T>
FORCE_INLINE bool add(T *value) const {
return impl()->add().set(value);
}
private:
const TArray *impl() const {
return static_cast<const TArray *>(this);
}
};
} // namespace ARDUINOJSON_NAMESPACE

View File

@ -13,14 +13,18 @@
#endif
namespace ARDUINOJSON_NAMESPACE {
class ArraySubscript : public VariantOperators<ArraySubscript>,
public Visitable {
template <typename TArray>
class ElementProxy : public VariantOperators<ElementProxy<TArray> >,
public Visitable {
typedef ElementProxy<TArray> this_type;
public:
FORCE_INLINE ArraySubscript(ArrayRef array, size_t index)
FORCE_INLINE ElementProxy(TArray array, size_t index)
: _array(array), _index(index) {}
FORCE_INLINE ArraySubscript& operator=(const ArraySubscript& src) {
get_impl().set(src.as<VariantConstRef>());
FORCE_INLINE this_type& operator=(const this_type& src) {
getElement().set(src.as<VariantConstRef>());
return *this;
}
@ -30,36 +34,36 @@ class ArraySubscript : public VariantOperators<ArraySubscript>,
// TValue = bool, long, int, short, float, double, serialized, VariantRef,
// std::string, String, ArrayRef, ObjectRef
template <typename T>
FORCE_INLINE ArraySubscript& operator=(const T& src) {
get_impl().set(src);
FORCE_INLINE this_type& operator=(const T& src) {
getElement().set(src);
return *this;
}
//
// operator=(TValue)
// TValue = char*, const char*, const __FlashStringHelper*
template <typename T>
FORCE_INLINE ArraySubscript& operator=(T* src) {
get_impl().set(src);
FORCE_INLINE this_type& operator=(T* src) {
getElement().set(src);
return *this;
}
FORCE_INLINE bool isNull() const {
return get_impl().isNull();
return getElement().isNull();
}
template <typename T>
FORCE_INLINE typename VariantAs<T>::type as() const {
return get_impl().as<T>();
return getElement().template as<T>();
}
template <typename T>
FORCE_INLINE bool is() const {
return get_impl().is<T>();
return getElement().template is<T>();
}
template <typename T>
FORCE_INLINE typename VariantTo<T>::type to() const {
return get_impl().to<T>();
return getElement().template to<T>();
}
// Replaces the value
@ -69,42 +73,65 @@ class ArraySubscript : public VariantOperators<ArraySubscript>,
// std::string, String, ArrayRef, ObjectRef
template <typename TValue>
FORCE_INLINE bool set(const TValue& value) const {
return get_impl().set(value);
return getElement().set(value);
}
//
// bool set(TValue)
// TValue = char*, const char*, const __FlashStringHelper*
template <typename TValue>
FORCE_INLINE bool set(TValue* value) const {
return get_impl().set(value);
return getElement().set(value);
}
template <typename Visitor>
void accept(Visitor& visitor) const {
return get_impl().accept(visitor);
return getElement().accept(visitor);
}
FORCE_INLINE size_t size() const {
return get_impl().size();
return getElement().size();
}
template <typename TNestedKey>
VariantRef get(TNestedKey* key) const {
return getElement().get(key);
}
template <typename TNestedKey>
VariantRef get(const TNestedKey& key) const {
return getElement().get(key);
}
template <typename TNestedKey>
VariantRef getOrCreate(TNestedKey* key) const {
return getElement().getOrCreate(key);
}
template <typename TNestedKey>
VariantRef getOrCreate(const TNestedKey& key) const {
return getElement().getOrCreate(key);
}
using ArrayShortcuts<ElementProxy>::add;
VariantRef add() const {
return getElement().add();
}
private:
FORCE_INLINE VariantRef get_impl() const {
FORCE_INLINE VariantRef getElement() const {
return _array.get(_index);
}
ArrayRef _array;
TArray _array;
const size_t _index;
};
template <typename TImpl>
inline ArraySubscript VariantSubscripts<TImpl>::operator[](size_t index) const {
return impl()->template as<ArrayRef>()[index];
template <typename TArray>
inline ElementProxy<const TArray&> ArrayShortcuts<TArray>::operator[](
size_t index) const {
return ElementProxy<const TArray&>(*impl(), index);
}
inline ArraySubscript ArrayRef::operator[](size_t index) const {
return ArraySubscript(*this, index);
}
} // namespace ARDUINOJSON_NAMESPACE
#ifdef _MSC_VER

View File

@ -9,7 +9,8 @@
#include "../Variant/VariantRef.hpp"
#include "../Variant/VariantTo.hpp"
#include "../Array/ArraySubscript.hpp"
#include "../Array/ElementProxy.hpp"
#include "../Object/MemberProxy.hpp"
namespace ARDUINOJSON_NAMESPACE {
@ -81,22 +82,51 @@ class JsonDocument : public Visitable {
return _data;
}
// ObjectSubscript operator[](TKey)
// TKey = const std::string&, const String&
template <typename TKey>
FORCE_INLINE typename enable_if<IsString<TKey>::value,
ObjectSubscript<const TKey&> >::type
operator[](const TKey& key) {
return getVariant()[key];
ArrayRef createNestedArray() {
return add().to<ArrayRef>();
}
// ObjectSubscript operator[](TKey);
// TKey = const char*, const char[N], const __FlashStringHelper*
template <typename TKey>
ArrayRef createNestedArray(TKey* key) {
return getOrCreate(key).template to<ArrayRef>();
}
template <typename TKey>
ArrayRef createNestedArray(const TKey& key) {
return getOrCreate(key).template to<ArrayRef>();
}
ObjectRef createNestedObject() {
return add().to<ObjectRef>();
}
template <typename TKey>
ObjectRef createNestedObject(TKey* key) {
return getOrCreate(key).template to<ObjectRef>();
}
template <typename TKey>
ObjectRef createNestedObject(const TKey& key) {
return getOrCreate(key).template to<ObjectRef>();
}
// MemberProxy operator[](TKey)
// TKey = const std::string&, const String&
template <typename TKey>
FORCE_INLINE
typename enable_if<IsString<TKey*>::value, ObjectSubscript<TKey*> >::type
operator[](TKey* key) {
return getVariant()[key];
typename enable_if<IsString<TKey>::value,
MemberProxy<JsonDocument&, const TKey&> >::type
operator[](const TKey& key) {
return MemberProxy<JsonDocument&, const TKey&>(*this, key);
}
// MemberProxy operator[](TKey);
// TKey = const char*, const char[N], const __FlashStringHelper*
template <typename TKey>
FORCE_INLINE typename enable_if<IsString<TKey*>::value,
MemberProxy<JsonDocument&, TKey*> >::type
operator[](TKey* key) {
return MemberProxy<JsonDocument&, TKey*>(*this, key);
}
// VariantConstRef operator[](TKey) const
@ -115,12 +145,56 @@ class JsonDocument : public Visitable {
return getVariant()[key];
}
FORCE_INLINE ArraySubscript operator[](size_t index) {
return getVariant()[index];
FORCE_INLINE ElementProxy<JsonDocument&> operator[](size_t index) {
return ElementProxy<JsonDocument&>(*this, index);
}
FORCE_INLINE VariantConstRef operator[](size_t index) const {
return getVariant()[index];
return VariantConstRef(_data.get(index));
}
FORCE_INLINE VariantRef get(size_t index) {
return VariantRef(&_pool, _data.get(index));
}
template <typename TKey>
FORCE_INLINE VariantRef get(TKey* key) {
return VariantRef(&_pool, _data.get(wrapString(key)));
}
template <typename TKey>
FORCE_INLINE typename enable_if<IsString<TKey>::value, VariantRef>::type get(
const TKey& key) {
return VariantRef(&_pool, _data.get(wrapString(key)));
}
template <typename TKey>
FORCE_INLINE VariantRef getOrCreate(TKey* key) {
return VariantRef(&_pool, _data.getOrCreate(wrapString(key), &_pool));
}
template <typename TKey>
FORCE_INLINE VariantRef getOrCreate(const TKey& key) {
return VariantRef(&_pool, _data.getOrCreate(wrapString(key), &_pool));
}
FORCE_INLINE VariantRef add() {
return VariantRef(&_pool, _data.add(&_pool));
}
//
// bool add(TValue);
// TValue = bool, long, int, short, float, double, serialized, VariantRef,
// std::string, String, ObjectRef
template <typename T>
FORCE_INLINE bool add(const T& value) {
return add().set(value);
}
//
// bool add(TValue);
// TValue = char*, const char*, const __FlashStringHelper*
template <typename T>
FORCE_INLINE bool add(T* value) {
return add().set(value);
}
protected:

View File

@ -15,4 +15,7 @@ struct Visitable {
template <typename T>
struct IsVisitable : is_base_of<Visitable, T> {};
template <typename T>
struct IsVisitable<T&> : IsVisitable<T> {};
} // namespace ARDUINOJSON_NAMESPACE

View File

@ -15,21 +15,21 @@
namespace ARDUINOJSON_NAMESPACE {
template <typename TStringRef>
class ObjectSubscript : public VariantOperators<ObjectSubscript<TStringRef> >,
public Visitable {
typedef ObjectSubscript<TStringRef> this_type;
template <typename TObject, typename TString>
class MemberProxy : public VariantOperators<MemberProxy<TObject, TString> >,
public Visitable {
typedef MemberProxy<TObject, TString> this_type;
public:
FORCE_INLINE ObjectSubscript(ObjectRef object, TStringRef key)
: _object(object), _key(key) {}
FORCE_INLINE MemberProxy(TObject variant, TString key)
: _object(variant), _key(key) {}
operator VariantConstRef() const {
return get_impl();
FORCE_INLINE operator VariantConstRef() const {
return getMember();
}
FORCE_INLINE this_type &operator=(const this_type &src) {
set_impl().set(src);
getOrCreateMember().set(src);
return *this;
}
@ -41,7 +41,7 @@ class ObjectSubscript : public VariantOperators<ObjectSubscript<TStringRef> >,
template <typename TValue>
FORCE_INLINE typename enable_if<!is_array<TValue>::value, this_type &>::type
operator=(const TValue &src) {
set_impl().set(src);
getOrCreateMember().set(src);
return *this;
}
//
@ -49,27 +49,27 @@ class ObjectSubscript : public VariantOperators<ObjectSubscript<TStringRef> >,
// TValue = char*, const char*, const __FlashStringHelper*
template <typename TValue>
FORCE_INLINE this_type &operator=(TValue *src) {
set_impl().set(src);
getOrCreateMember().set(src);
return *this;
}
FORCE_INLINE bool isNull() const {
return get_impl().isNull();
return getMember().isNull();
}
template <typename TValue>
FORCE_INLINE typename VariantAs<TValue>::type as() const {
return get_impl().template as<TValue>();
return getMember().template as<TValue>();
}
template <typename TValue>
FORCE_INLINE bool is() const {
return get_impl().template is<TValue>();
return getMember().template is<TValue>();
}
template <typename TValue>
FORCE_INLINE typename VariantTo<TValue>::type to() {
return set_impl().template to<TValue>();
return getOrCreateMember().template to<TValue>();
}
// Sets the specified value.
@ -81,48 +81,73 @@ class ObjectSubscript : public VariantOperators<ObjectSubscript<TStringRef> >,
template <typename TValue>
FORCE_INLINE typename enable_if<!is_array<TValue>::value, bool>::type set(
const TValue &value) {
return set_impl().set(value);
return getOrCreateMember().set(value);
}
//
// bool set(TValue);
// TValue = char*, const char, const __FlashStringHelper*
template <typename TValue>
FORCE_INLINE bool set(const TValue *value) {
return set_impl().set(value);
return getOrCreateMember().set(value);
}
template <typename Visitor>
void accept(Visitor &visitor) const {
return get_impl().accept(visitor);
return getMember().accept(visitor);
}
using ArrayShortcuts<MemberProxy>::add;
FORCE_INLINE VariantRef add() const {
return getOrCreateMember().add();
}
template <typename TNestedKey>
FORCE_INLINE VariantRef get(TNestedKey *key) const {
return getMember().get(key);
}
template <typename TNestedKey>
FORCE_INLINE VariantRef get(const TNestedKey &key) const {
return getMember().get(key);
}
template <typename TNestedKey>
FORCE_INLINE VariantRef getOrCreate(TNestedKey *key) const {
return getOrCreateMember().getOrCreate(key);
}
template <typename TNestedKey>
FORCE_INLINE VariantRef getOrCreate(const TNestedKey &key) const {
return getOrCreateMember().getOrCreate(key);
}
private:
FORCE_INLINE VariantRef get_impl() const {
FORCE_INLINE VariantRef getMember() const {
return _object.get(_key);
}
FORCE_INLINE VariantRef set_impl() const {
return _object.set(_key);
FORCE_INLINE VariantRef getOrCreateMember() const {
return _object.getOrCreate(_key);
}
ObjectRef _object;
TStringRef _key;
TObject _object;
TString _key;
};
template <typename TImpl>
template <typename TObject>
template <typename TString>
inline typename enable_if<IsString<TString>::value,
ObjectSubscript<const TString &> >::type
VariantSubscripts<TImpl>::operator[](const TString &key) const {
return impl()->template as<ObjectRef>()[key];
MemberProxy<const TObject &, const TString &> >::type
ObjectShortcuts<TObject>::operator[](const TString &key) const {
return MemberProxy<const TObject &, const TString &>(*impl(), key);
}
template <typename TImpl>
template <typename TObject>
template <typename TString>
inline typename enable_if<IsString<TString *>::value,
ObjectSubscript<TString *> >::type
VariantSubscripts<TImpl>::operator[](TString *key) const {
return impl()->template as<ObjectRef>()[key];
MemberProxy<const TObject &, TString *> >::type
ObjectShortcuts<TObject>::operator[](TString *key) const {
return MemberProxy<const TObject &, TString *>(*impl(), key);
}
} // namespace ARDUINOJSON_NAMESPACE

View File

@ -40,7 +40,8 @@ void objectRemove(CollectionData *obj, TKey key) {
}
template <typename TKey>
inline VariantData *objectSet(CollectionData *obj, TKey key, MemoryPool *pool) {
inline VariantData *objectGetOrCreate(CollectionData *obj, TKey key,
MemoryPool *pool) {
if (!obj) return 0;
// ignore null key

View File

@ -9,13 +9,31 @@
namespace ARDUINOJSON_NAMESPACE {
template <typename TObject>
template <typename TString>
inline ArrayRef ObjectRef::createNestedArray(const TString& key) const {
return set(key).template to<ArrayRef>();
inline ArrayRef ObjectShortcuts<TObject>::createNestedArray(
const TString& key) const {
return impl()->getOrCreate(key).template to<ArrayRef>();
}
template <typename TObject>
template <typename TString>
inline ArrayRef ObjectRef::createNestedArray(TString* key) const {
return set(key).template to<ArrayRef>();
inline ArrayRef ObjectShortcuts<TObject>::createNestedArray(
TString* key) const {
return impl()->getOrCreate(key).template to<ArrayRef>();
}
template <typename TObject>
template <typename TKey>
ObjectRef ObjectShortcuts<TObject>::createNestedObject(const TKey& key) const {
return impl()->getOrCreate(key).template to<ObjectRef>();
}
//
// ObjectRef createNestedObject(TKey);
// TKey = char*, const char*, char[], const char[], const __FlashStringHelper*
template <typename TObject>
template <typename TKey>
ObjectRef ObjectShortcuts<TObject>::createNestedObject(TKey* key) const {
return impl()->getOrCreate(key).template to<ObjectRef>();
}
} // namespace ARDUINOJSON_NAMESPACE

View File

@ -17,6 +17,10 @@ namespace ARDUINOJSON_NAMESPACE {
template <typename TData>
class ObjectRefBase {
public:
operator VariantConstRef() const {
return VariantConstRef(reinterpret_cast<const VariantData*>(_data));
}
template <typename Visitor>
FORCE_INLINE void accept(Visitor& visitor) const {
objectAccept(_data, visitor);
@ -127,7 +131,9 @@ class ObjectConstRef : public ObjectRefBase<const CollectionData>,
}
};
class ObjectRef : public ObjectRefBase<CollectionData>, public Visitable {
class ObjectRef : public ObjectRefBase<CollectionData>,
public ObjectShortcuts<ObjectRef>,
public Visitable {
typedef ObjectRefBase<CollectionData> base_type;
public:
@ -164,68 +170,30 @@ class ObjectRef : public ObjectRefBase<CollectionData>, public Visitable {
return _data->copyFrom(*src._data, _pool);
}
// Creates and adds a ArrayRef.
//
// ArrayRef createNestedArray(TKey);
// TKey = const std::string&, const String&
template <typename TKey>
FORCE_INLINE ArrayRef createNestedArray(const TKey& key) const;
// ArrayRef createNestedArray(TKey);
// TKey = char*, const char*, char[], const char[], const __FlashStringHelper*
template <typename TKey>
FORCE_INLINE ArrayRef createNestedArray(TKey* key) const;
// Creates and adds a ObjectRef.
//
// ObjectRef createNestedObject(TKey);
// TKey = const std::string&, const String&
template <typename TKey>
FORCE_INLINE ObjectRef createNestedObject(const TKey& key) const {
return set(key).template to<ObjectRef>();
}
//
// ObjectRef createNestedObject(TKey);
// TKey = char*, const char*, char[], const char[], const __FlashStringHelper*
template <typename TKey>
FORCE_INLINE ObjectRef createNestedObject(TKey* key) const {
return set(key).template to<ObjectRef>();
}
// Gets the value associated with the specified key.
//
// TValue get<TValue>(TKey) const;
// VariantRef get<TValue>(TKey) const;
// TKey = const std::string&, const String&
// TValue = bool, char, long, int, short, float, double,
// std::string, String, ArrayRef, ObjectRef
template <typename TKey>
FORCE_INLINE VariantRef get(const TKey& key) const {
return get_impl(wrapString(key));
}
//
// TValue get<TValue>(TKey) const;
// VariantRef get<TValue>(TKey) const;
// TKey = char*, const char*, const __FlashStringHelper*
// TValue = bool, char, long, int, short, float, double,
// std::string, String, ArrayRef, ObjectRef
template <typename TKey>
FORCE_INLINE VariantRef get(TKey* key) const {
return get_impl(wrapString(key));
}
// Gets or sets the value associated with the specified key.
//
// ObjectSubscript operator[](TKey)
// TKey = const std::string&, const String&
template <typename TKey>
FORCE_INLINE ObjectSubscript<const TKey&> operator[](const TKey& key) const {
return ObjectSubscript<const TKey&>(*this, key);
FORCE_INLINE VariantRef getOrCreate(TKey* key) const {
return getOrCreate_impl(wrapString(key));
}
//
// ObjectSubscript operator[](TKey)
// TKey = char*, const char*, char[], const char[N], const
// __FlashStringHelper*
template <typename TKey>
FORCE_INLINE ObjectSubscript<TKey*> operator[](TKey* key) const {
return ObjectSubscript<TKey*>(*this, key);
FORCE_INLINE VariantRef getOrCreate(const TKey& key) const {
return getOrCreate_impl(wrapString(key));
}
FORCE_INLINE bool operator==(ObjectRef rhs) const {
@ -253,16 +221,6 @@ class ObjectRef : public ObjectRefBase<CollectionData>, public Visitable {
objectRemove(_data, wrapString(key));
}
template <typename TKey>
FORCE_INLINE VariantRef set(TKey* key) const {
return set_impl(wrapString(key));
}
template <typename TKey>
FORCE_INLINE VariantRef set(const TKey& key) const {
return set_impl(wrapString(key));
}
private:
template <typename TKey>
FORCE_INLINE VariantRef get_impl(TKey key) const {
@ -270,8 +228,8 @@ class ObjectRef : public ObjectRefBase<CollectionData>, public Visitable {
}
template <typename TKey>
FORCE_INLINE VariantRef set_impl(TKey key) const {
return VariantRef(_pool, objectSet(_data, key, _pool));
FORCE_INLINE VariantRef getOrCreate_impl(TKey key) const {
return VariantRef(_pool, objectGetOrCreate(_data, key, _pool));
}
MemoryPool* _pool;

View File

@ -0,0 +1,61 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#pragma once
#include "../Polyfills/attributes.hpp"
#include "../Polyfills/type_traits.hpp"
#include "../Strings/StringWrappers.hpp"
namespace ARDUINOJSON_NAMESPACE {
template <typename TParent, typename TKey>
class MemberProxy;
template <typename TObject>
class ObjectShortcuts {
public:
// MemberProxy operator[](TKey) const;
// TKey = const std::string&, const String&
template <typename TKey>
FORCE_INLINE
typename enable_if<IsString<TKey>::value,
MemberProxy<const TObject &, const TKey &> >::type
operator[](const TKey &key) const;
//
// MemberProxy operator[](TKey) const;
// TKey = const char*, const char[N], const __FlashStringHelper*
template <typename TKey>
FORCE_INLINE typename enable_if<IsString<TKey *>::value,
MemberProxy<const TObject &, TKey *> >::type
operator[](TKey *key) const;
// Creates and adds a ArrayRef.
//
// ArrayRef createNestedArray(TKey);
// TKey = const std::string&, const String&
template <typename TKey>
FORCE_INLINE ArrayRef createNestedArray(const TKey &key) const;
// ArrayRef createNestedArray(TKey);
// TKey = char*, const char*, char[], const char[], const __FlashStringHelper*
template <typename TKey>
FORCE_INLINE ArrayRef createNestedArray(TKey *key) const;
// Creates and adds a ObjectRef.
//
// ObjectRef createNestedObject(TKey);
// TKey = const std::string&, const String&
template <typename TKey>
ObjectRef createNestedObject(const TKey &key) const;
//
// ObjectRef createNestedObject(TKey);
// TKey = char*, const char*, char[], const char[], const __FlashStringHelper*
template <typename TKey>
ObjectRef createNestedObject(TKey *key) const;
private:
const TObject *impl() const {
return static_cast<const TObject *>(this);
}
};
} // namespace ARDUINOJSON_NAMESPACE

View File

@ -7,7 +7,7 @@
#include "VariantCasts.hpp"
#include "VariantComparisons.hpp"
#include "VariantOr.hpp"
#include "VariantSubscripts.hpp"
#include "VariantShortcuts.hpp"
namespace ARDUINOJSON_NAMESPACE {
@ -15,5 +15,5 @@ template <typename TImpl>
class VariantOperators : public VariantCasts<TImpl>,
public VariantComparisons<TImpl>,
public VariantOr<TImpl>,
public VariantSubscripts<TImpl> {};
public VariantShortcuts<TImpl> {};
} // namespace ARDUINOJSON_NAMESPACE

View File

@ -0,0 +1,23 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#pragma once
#include "../Array/ArrayShortcuts.hpp"
#include "../Object/ObjectShortcuts.hpp"
namespace ARDUINOJSON_NAMESPACE {
template <typename TVariant>
class VariantShortcuts : public ObjectShortcuts<TVariant>,
public ArrayShortcuts<TVariant> {
public:
using ArrayShortcuts<TVariant>::createNestedArray;
using ArrayShortcuts<TVariant>::createNestedObject;
using ArrayShortcuts<TVariant>::operator[];
using ObjectShortcuts<TVariant>::createNestedArray;
using ObjectShortcuts<TVariant>::createNestedObject;
using ObjectShortcuts<TVariant>::operator[];
};
} // namespace ARDUINOJSON_NAMESPACE

View File

@ -1,51 +0,0 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#pragma once
#include "../Polyfills/attributes.hpp"
#include "../Polyfills/type_traits.hpp"
#include "../Strings/StringWrappers.hpp"
#include "../Variant/VariantAs.hpp"
namespace ARDUINOJSON_NAMESPACE {
class ArrayRef;
class ObjectRef;
// Forward declarations.
class ArraySubscript;
template <typename TKey>
class ObjectSubscript;
template <typename TImpl>
class VariantSubscripts {
public:
// Mimics an array.
// Returns the element at specified index if the variant is an array.
FORCE_INLINE ArraySubscript operator[](size_t index) const;
// Mimics an object.
// Returns the value associated with the specified key if the variant is
// an object.
//
// ObjectSubscript operator[](TKey) const;
// TKey = const std::string&, const String&
template <typename TKey>
FORCE_INLINE typename enable_if<IsString<TKey>::value,
ObjectSubscript<const TKey &> >::type
operator[](const TKey &key) const;
//
// ObjectSubscript operator[](TKey) const;
// TKey = const char*, const char[N], const __FlashStringHelper*
template <typename TKey>
FORCE_INLINE typename enable_if<IsString<TKey *>::value,
ObjectSubscript<TKey *> >::type
operator[](TKey *key) const;
private:
const TImpl *impl() const {
return static_cast<const TImpl *>(this);
}
};
} // namespace ARDUINOJSON_NAMESPACE

View File

@ -12,19 +12,24 @@
namespace ARDUINOJSON_NAMESPACE {
//
enum {
VALUE_IS_NULL = 0,
VALUE_IS_LINKED_RAW,
VALUE_IS_OWNED_RAW,
VALUE_IS_LINKED_STRING,
VALUE_IS_OWNED_STRING,
VALUE_IS_BOOLEAN,
VALUE_IS_POSITIVE_INTEGER,
VALUE_IS_NEGATIVE_INTEGER,
VALUE_IS_ARRAY,
VALUE_IS_OBJECT,
VALUE_IS_FLOAT,
VALUE_MASK = 0x7F,
VALUE_IS_NULL = 0,
VALUE_IS_LINKED_RAW = 0x01,
VALUE_IS_OWNED_RAW = 0x02,
VALUE_IS_LINKED_STRING = 0x03,
VALUE_IS_OWNED_STRING = 0x04,
VALUE_IS_BOOLEAN = 0x05,
VALUE_IS_POSITIVE_INTEGER = 0x06,
VALUE_IS_NEGATIVE_INTEGER = 0x07,
VALUE_IS_FLOAT = 0x08,
COLLECTION_MASK = 0x60,
VALUE_IS_OBJECT = 0x20,
VALUE_IS_ARRAY = 0x40,
KEY_IS_OWNED = 0x80
};

View File

@ -67,7 +67,7 @@ class VariantData {
}
CollectionData *asArray() {
return type() == VALUE_IS_ARRAY ? &_content.asCollection : 0;
return isArray() ? &_content.asCollection : 0;
}
const CollectionData *asArray() const {
@ -75,7 +75,7 @@ class VariantData {
}
CollectionData *asObject() {
return type() == VALUE_IS_OBJECT ? &_content.asCollection : 0;
return isObject() ? &_content.asCollection : 0;
}
const CollectionData *asObject() const {
@ -135,13 +135,17 @@ class VariantData {
}
bool isArray() const {
return type() == VALUE_IS_ARRAY;
return (_flags & VALUE_IS_ARRAY) != 0;
}
bool isBoolean() const {
return type() == VALUE_IS_BOOLEAN;
}
bool isCollection() const {
return (_flags & COLLECTION_MASK) != 0;
}
bool isInteger() const {
return type() == VALUE_IS_POSITIVE_INTEGER ||
type() == VALUE_IS_NEGATIVE_INTEGER;
@ -153,12 +157,11 @@ class VariantData {
}
bool isString() const {
return (type() == VALUE_IS_LINKED_STRING ||
type() == VALUE_IS_OWNED_STRING);
return type() == VALUE_IS_LINKED_STRING || type() == VALUE_IS_OWNED_STRING;
}
bool isObject() const {
return type() == VALUE_IS_OBJECT;
return (_flags & VALUE_IS_OBJECT) != 0;
}
bool isNull() const {
@ -275,20 +278,35 @@ class VariantData {
}
size_t nesting() const {
switch (type()) {
case VALUE_IS_OBJECT:
case VALUE_IS_ARRAY:
return _content.asCollection.nesting();
default:
return 0;
}
return isCollection() ? _content.asCollection.nesting() : 0;
}
size_t size() const {
if (type() == VALUE_IS_OBJECT || type() == VALUE_IS_ARRAY)
return _content.asCollection.size();
else
return 0;
return isCollection() ? _content.asCollection.size() : 0;
}
VariantData *get(size_t index) const {
return isArray() ? _content.asCollection.get(index) : 0;
}
template <typename TKey>
VariantData *get(TKey key) const {
return isObject() ? _content.asCollection.get(key) : 0;
}
template <typename TKey>
VariantData *getOrCreate(TKey key, MemoryPool *pool) {
if (isNull()) toObject();
if (!isObject()) return 0;
VariantData *var = _content.asCollection.get(key);
if (var) return var;
return _content.asCollection.add(key, pool);
}
VariantData *add(MemoryPool *pool) {
if (isNull()) toArray();
if (!isArray()) return 0;
return _content.asCollection.add(pool);
}
private:

View File

@ -146,4 +146,20 @@ inline CollectionData *variantToObject(VariantData *var) {
return &var->toObject();
}
inline NO_INLINE VariantData *variantAdd(VariantData *var, MemoryPool *pool) {
return var != 0 ? var->add(pool) : 0;
}
template <typename TKey>
NO_INLINE VariantData *variantGetOrCreate(VariantData *var, TKey *key,
MemoryPool *pool) {
return var != 0 ? var->getOrCreate(wrapString(key), pool) : 0;
}
template <typename TKey>
NO_INLINE VariantData *variantGetOrCreate(VariantData *var, const TKey &key,
MemoryPool *pool) {
return var != 0 ? var->getOrCreate(wrapString(key), pool) : 0;
}
} // namespace ARDUINOJSON_NAMESPACE

View File

@ -50,7 +50,7 @@ inline T VariantData::asFloat() const {
}
}
inline const char* VariantData::asString() const {
inline const char *VariantData::asString() const {
switch (type()) {
case VALUE_IS_LINKED_STRING:
case VALUE_IS_OWNED_STRING:
@ -60,37 +60,11 @@ inline const char* VariantData::asString() const {
}
}
inline bool VariantRef::set(ArrayRef array) const {
return to<ArrayRef>().copyFrom(array);
}
inline bool VariantRef::set(ArrayConstRef array) const {
return to<ArrayRef>().copyFrom(array);
}
inline bool VariantRef::set(const ArraySubscript& value) const {
return set(value.as<VariantRef>());
}
inline bool VariantRef::set(ObjectRef object) const {
return to<ObjectRef>().copyFrom(object);
}
inline bool VariantRef::set(ObjectConstRef object) const {
return to<ObjectRef>().copyFrom(object);
}
template <typename TString>
inline bool VariantRef::set(const ObjectSubscript<TString>& value) const {
return set(value.template as<VariantRef>());
}
inline bool VariantRef::set(VariantConstRef value) const {
return variantCopyFrom(_data, value._data, _pool);
}
inline bool VariantRef::set(VariantRef value) const {
return variantCopyFrom(_data, value._data, _pool);
template <typename TVariant>
typename enable_if<IsVisitable<TVariant>::value, bool>::type VariantRef::set(
const TVariant &value) const {
VariantConstRef v = value;
return variantCopyFrom(_data, v._data, _pool);
}
template <typename T>
@ -128,4 +102,32 @@ inline VariantConstRef VariantConstRef::operator[](size_t index) const {
return ArrayConstRef(_data != 0 ? _data->asArray() : 0)[index];
}
inline VariantRef VariantRef::add() const {
return VariantRef(_pool, variantAdd(_data, _pool));
}
inline VariantRef VariantRef::get(size_t index) const {
return VariantRef(_pool, _data != 0 ? _data->get(index) : 0);
}
template <typename TKey>
inline VariantRef VariantRef::get(TKey *key) const {
return VariantRef(_pool, _data != 0 ? _data->get(wrapString(key)) : 0);
}
template <typename TKey>
inline typename enable_if<IsString<TKey>::value, VariantRef>::type
VariantRef::get(const TKey &key) const {
return VariantRef(_pool, _data != 0 ? _data->get(wrapString(key)) : 0);
}
template <typename TKey>
inline VariantRef VariantRef::getOrCreate(TKey *key) const {
return VariantRef(_pool, variantGetOrCreate(_data, key, _pool));
}
template <typename TKey>
inline VariantRef VariantRef::getOrCreate(const TKey &key) const {
return VariantRef(_pool, variantGetOrCreate(_data, key, _pool));
}
} // namespace ARDUINOJSON_NAMESPACE

View File

@ -23,6 +23,9 @@ namespace ARDUINOJSON_NAMESPACE {
class ArrayRef;
class ObjectRef;
template <typename, typename>
class MemberProxy;
// Contains the methods shared by VariantRef and VariantConstRef
template <typename TData>
class VariantRefBase {
@ -207,16 +210,15 @@ class VariantRef : public VariantRefBase<VariantData>,
return variantSetLinkedString(_data, value);
}
bool set(VariantConstRef value) const;
bool set(VariantRef value) const;
FORCE_INLINE bool set(ArrayRef array) const;
FORCE_INLINE bool set(ArrayConstRef array) const;
FORCE_INLINE bool set(const ArraySubscript &) const;
FORCE_INLINE bool set(ObjectRef object) const;
FORCE_INLINE bool set(ObjectConstRef object) const;
template <typename TString>
FORCE_INLINE bool set(const ObjectSubscript<TString> &) const;
// set(VariantRef)
// set(VariantConstRef)
// set(ArrayRef)
// set(ArrayConstRef)
// set(ObjectRef)
// set(ObjecConstRef)
template <typename TVariant>
typename enable_if<IsVisitable<TVariant>::value, bool>::type set(
const TVariant &value) const;
// Get the variant as the specified type.
//
@ -278,6 +280,26 @@ class VariantRef : public VariantRefBase<VariantData>,
typename enable_if<is_same<T, VariantRef>::value, VariantRef>::type to()
const;
VariantRef add() const;
using ArrayShortcuts::add;
FORCE_INLINE VariantRef get(size_t) const;
template <typename TKey>
FORCE_INLINE VariantRef get(TKey *) const;
// get(const char*)
// get(const __FlashStringHelper*)
template <typename TKey>
FORCE_INLINE typename enable_if<IsString<TKey>::value, VariantRef>::type get(
const TKey &) const;
template <typename TKey>
FORCE_INLINE VariantRef getOrCreate(TKey *) const;
template <typename TKey>
FORCE_INLINE VariantRef getOrCreate(const TKey &) const;
private:
MemoryPool *_pool;
};