mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-19 05:22:24 +02:00
Renamed and moved internal files
This commit is contained in:
@ -6,19 +6,19 @@
|
||||
|
||||
#include "ArduinoJson/Namespace.hpp"
|
||||
|
||||
#include "ArduinoJson/JsonArray.hpp"
|
||||
#include "ArduinoJson/JsonObject.hpp"
|
||||
#include "ArduinoJson/JsonVariant.hpp"
|
||||
#include "ArduinoJson/Array/ArrayRef.hpp"
|
||||
#include "ArduinoJson/Object/ObjectRef.hpp"
|
||||
#include "ArduinoJson/Variant/VariantRef.hpp"
|
||||
|
||||
#include "ArduinoJson/DynamicJsonDocument.hpp"
|
||||
#include "ArduinoJson/StaticJsonDocument.hpp"
|
||||
#include "ArduinoJson/Document/DynamicJsonDocument.hpp"
|
||||
#include "ArduinoJson/Document/StaticJsonDocument.hpp"
|
||||
|
||||
#include "ArduinoJson/Data/VariantAsImpl.hpp"
|
||||
#include "ArduinoJson/JsonArrayImpl.hpp"
|
||||
#include "ArduinoJson/JsonArraySubscript.hpp"
|
||||
#include "ArduinoJson/JsonObjectImpl.hpp"
|
||||
#include "ArduinoJson/JsonObjectSubscript.hpp"
|
||||
#include "ArduinoJson/JsonVariantImpl.hpp"
|
||||
#include "ArduinoJson/Array/ArrayImpl.hpp"
|
||||
#include "ArduinoJson/Array/ArraySubscript.hpp"
|
||||
#include "ArduinoJson/Object/ObjectImpl.hpp"
|
||||
#include "ArduinoJson/Object/ObjectSubscript.hpp"
|
||||
#include "ArduinoJson/Variant/VariantAsImpl.hpp"
|
||||
#include "ArduinoJson/Variant/VariantImpl.hpp"
|
||||
|
||||
#include "ArduinoJson/Json/JsonDeserializer.hpp"
|
||||
#include "ArduinoJson/Json/JsonSerializer.hpp"
|
||||
@ -27,19 +27,19 @@
|
||||
#include "ArduinoJson/MsgPack/MsgPackSerializer.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
typedef ARDUINOJSON_NAMESPACE::ArrayRef JsonArray;
|
||||
typedef ARDUINOJSON_NAMESPACE::ArrayConstRef JsonArrayConst;
|
||||
typedef ARDUINOJSON_NAMESPACE::Float JsonFloat;
|
||||
typedef ARDUINOJSON_NAMESPACE::Integer JsonInteger;
|
||||
typedef ARDUINOJSON_NAMESPACE::ObjectRef JsonObject;
|
||||
typedef ARDUINOJSON_NAMESPACE::ObjectConstRef JsonObjectConst;
|
||||
typedef ARDUINOJSON_NAMESPACE::Pair JsonPair;
|
||||
typedef ARDUINOJSON_NAMESPACE::UInt JsonUInt;
|
||||
typedef ARDUINOJSON_NAMESPACE::VariantRef JsonVariant;
|
||||
typedef ARDUINOJSON_NAMESPACE::VariantConstRef JsonVariantConst;
|
||||
using ARDUINOJSON_NAMESPACE::DeserializationError;
|
||||
using ARDUINOJSON_NAMESPACE::DynamicJsonDocument;
|
||||
using ARDUINOJSON_NAMESPACE::JsonArray;
|
||||
using ARDUINOJSON_NAMESPACE::JsonArrayConst;
|
||||
using ARDUINOJSON_NAMESPACE::JsonFloat;
|
||||
using ARDUINOJSON_NAMESPACE::JsonInteger;
|
||||
using ARDUINOJSON_NAMESPACE::JsonKey;
|
||||
using ARDUINOJSON_NAMESPACE::JsonObject;
|
||||
using ARDUINOJSON_NAMESPACE::JsonObjectConst;
|
||||
using ARDUINOJSON_NAMESPACE::JsonPair;
|
||||
using ARDUINOJSON_NAMESPACE::JsonUInt;
|
||||
using ARDUINOJSON_NAMESPACE::JsonVariant;
|
||||
using ARDUINOJSON_NAMESPACE::JsonVariantConst;
|
||||
using ARDUINOJSON_NAMESPACE::Key;
|
||||
using ARDUINOJSON_NAMESPACE::serialized;
|
||||
using ARDUINOJSON_NAMESPACE::StaticJsonDocument;
|
||||
} // namespace ArduinoJson
|
||||
|
@ -4,12 +4,12 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "JsonVariantData.hpp"
|
||||
#include "SlotFunctions.hpp"
|
||||
#include "../Variant/SlotFunctions.hpp"
|
||||
#include "../Variant/VariantData.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
inline JsonVariantData* arrayAdd(JsonArrayData* arr, MemoryPool* pool) {
|
||||
inline VariantData* arrayAdd(ArrayData* arr, MemoryPool* pool) {
|
||||
if (!arr) return 0;
|
||||
|
||||
VariantSlot* slot = pool->allocVariant();
|
||||
@ -31,17 +31,17 @@ inline JsonVariantData* arrayAdd(JsonArrayData* arr, MemoryPool* pool) {
|
||||
return &slot->value;
|
||||
}
|
||||
|
||||
inline VariantSlot* arrayGetSlot(const JsonArrayData* arr, size_t index) {
|
||||
inline VariantSlot* arrayGetSlot(const ArrayData* arr, size_t index) {
|
||||
if (!arr) return 0;
|
||||
return arr->head->getNext(index);
|
||||
}
|
||||
|
||||
inline JsonVariantData* arrayGet(const JsonArrayData* arr, size_t index) {
|
||||
inline VariantData* arrayGet(const ArrayData* arr, size_t index) {
|
||||
VariantSlot* slot = arrayGetSlot(arr, index);
|
||||
return slot ? &slot->value : 0;
|
||||
}
|
||||
|
||||
inline void arrayRemove(JsonArrayData* arr, VariantSlot* slot) {
|
||||
inline void arrayRemove(ArrayData* arr, VariantSlot* slot) {
|
||||
if (!arr || !slot) return;
|
||||
|
||||
if (slot->prev)
|
||||
@ -54,20 +54,19 @@ inline void arrayRemove(JsonArrayData* arr, VariantSlot* slot) {
|
||||
arr->tail = slot->getPrev();
|
||||
}
|
||||
|
||||
inline void arrayRemove(JsonArrayData* arr, size_t index) {
|
||||
inline void arrayRemove(ArrayData* arr, size_t index) {
|
||||
arrayRemove(arr, arrayGetSlot(arr, index));
|
||||
}
|
||||
|
||||
inline void arrayClear(JsonArrayData* arr) {
|
||||
inline void arrayClear(ArrayData* arr) {
|
||||
if (!arr) return;
|
||||
arr->head = 0;
|
||||
arr->tail = 0;
|
||||
}
|
||||
|
||||
bool variantCopy(JsonVariantData*, const JsonVariantData*, MemoryPool*);
|
||||
bool variantCopy(VariantData*, const VariantData*, MemoryPool*);
|
||||
|
||||
inline bool arrayCopy(JsonArrayData* dst, const JsonArrayData* src,
|
||||
MemoryPool* pool) {
|
||||
inline bool arrayCopy(ArrayData* dst, const ArrayData* src, MemoryPool* pool) {
|
||||
if (!dst || !src) return false;
|
||||
arrayClear(dst);
|
||||
for (VariantSlot* s = src->head; s; s = s->getNext()) {
|
||||
@ -76,9 +75,9 @@ inline bool arrayCopy(JsonArrayData* dst, const JsonArrayData* src,
|
||||
return true;
|
||||
}
|
||||
|
||||
bool variantEquals(const JsonVariantData*, const JsonVariantData*);
|
||||
bool variantEquals(const VariantData*, const VariantData*);
|
||||
|
||||
inline bool arrayEquals(const JsonArrayData* a1, const JsonArrayData* a2) {
|
||||
inline bool arrayEquals(const ArrayData* a1, const ArrayData* a2) {
|
||||
if (a1 == a2) return true;
|
||||
if (!a1 || !a2) return false;
|
||||
VariantSlot* s1 = a1->head;
|
||||
@ -92,7 +91,7 @@ inline bool arrayEquals(const JsonArrayData* a1, const JsonArrayData* a2) {
|
||||
}
|
||||
}
|
||||
|
||||
inline size_t arraySize(const JsonArrayData* arr) {
|
||||
inline size_t arraySize(const ArrayData* arr) {
|
||||
if (!arr) return 0;
|
||||
return slotSize(arr->head);
|
||||
}
|
19
src/ArduinoJson/Array/ArrayImpl.hpp
Normal file
19
src/ArduinoJson/Array/ArrayImpl.hpp
Normal file
@ -0,0 +1,19 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../Object/ObjectRef.hpp"
|
||||
#include "ArrayRef.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
inline ArrayRef ArrayRef::createNestedArray() const {
|
||||
return add().to<ArrayRef>();
|
||||
}
|
||||
|
||||
inline ObjectRef ArrayRef::createNestedObject() const {
|
||||
return add().to<ObjectRef>();
|
||||
}
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
122
src/ArduinoJson/Array/ArrayIterator.hpp
Normal file
122
src/ArduinoJson/Array/ArrayIterator.hpp
Normal file
@ -0,0 +1,122 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../Variant/SlotFunctions.hpp"
|
||||
#include "../Variant/VariantRef.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
class VariantPtr {
|
||||
public:
|
||||
VariantPtr(MemoryPool *memoryPool, VariantData *data)
|
||||
: _variant(memoryPool, data) {}
|
||||
|
||||
VariantRef *operator->() {
|
||||
return &_variant;
|
||||
}
|
||||
|
||||
VariantRef &operator*() {
|
||||
return _variant;
|
||||
}
|
||||
|
||||
private:
|
||||
VariantRef _variant;
|
||||
};
|
||||
|
||||
class ArrayIterator {
|
||||
public:
|
||||
ArrayIterator() : _slot(0) {}
|
||||
explicit ArrayIterator(MemoryPool *memoryPool, VariantSlot *slot)
|
||||
: _memoryPool(memoryPool), _slot(slot) {}
|
||||
|
||||
VariantRef operator*() const {
|
||||
return VariantRef(_memoryPool, &_slot->value);
|
||||
}
|
||||
VariantPtr operator->() {
|
||||
return VariantPtr(_memoryPool, &_slot->value);
|
||||
}
|
||||
|
||||
bool operator==(const ArrayIterator &other) const {
|
||||
return _slot == other._slot;
|
||||
}
|
||||
|
||||
bool operator!=(const ArrayIterator &other) const {
|
||||
return _slot != other._slot;
|
||||
}
|
||||
|
||||
ArrayIterator &operator++() {
|
||||
_slot = _slot->getNext();
|
||||
return *this;
|
||||
}
|
||||
|
||||
ArrayIterator &operator+=(size_t distance) {
|
||||
_slot = _slot->getNext(distance);
|
||||
return *this;
|
||||
}
|
||||
|
||||
VariantSlot *internal() {
|
||||
return _slot;
|
||||
}
|
||||
|
||||
private:
|
||||
MemoryPool *_memoryPool;
|
||||
VariantSlot *_slot;
|
||||
};
|
||||
|
||||
class VariantConstPtr {
|
||||
public:
|
||||
VariantConstPtr(const VariantData *data) : _variant(data) {}
|
||||
|
||||
VariantConstRef *operator->() {
|
||||
return &_variant;
|
||||
}
|
||||
|
||||
VariantConstRef &operator*() {
|
||||
return _variant;
|
||||
}
|
||||
|
||||
private:
|
||||
VariantConstRef _variant;
|
||||
};
|
||||
|
||||
class ArrayConstRefIterator {
|
||||
public:
|
||||
ArrayConstRefIterator() : _slot(0) {}
|
||||
explicit ArrayConstRefIterator(const VariantSlot *slot) : _slot(slot) {}
|
||||
|
||||
VariantConstRef operator*() const {
|
||||
return VariantConstRef(&_slot->value);
|
||||
}
|
||||
VariantConstPtr operator->() {
|
||||
return VariantConstPtr(&_slot->value);
|
||||
}
|
||||
|
||||
bool operator==(const ArrayConstRefIterator &other) const {
|
||||
return _slot == other._slot;
|
||||
}
|
||||
|
||||
bool operator!=(const ArrayConstRefIterator &other) const {
|
||||
return _slot != other._slot;
|
||||
}
|
||||
|
||||
ArrayConstRefIterator &operator++() {
|
||||
_slot = _slot->getNext();
|
||||
return *this;
|
||||
}
|
||||
|
||||
ArrayConstRefIterator &operator+=(size_t distance) {
|
||||
_slot = _slot->getNext(distance);
|
||||
return *this;
|
||||
}
|
||||
|
||||
const VariantSlot *internal() {
|
||||
return _slot;
|
||||
}
|
||||
|
||||
private:
|
||||
const VariantSlot *_slot;
|
||||
};
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
@ -4,9 +4,9 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Data/ArrayFunctions.hpp"
|
||||
#include "Data/JsonVariantData.hpp"
|
||||
#include "JsonArrayIterator.hpp"
|
||||
#include "../Variant/VariantData.hpp"
|
||||
#include "ArrayFunctions.hpp"
|
||||
#include "ArrayIterator.hpp"
|
||||
|
||||
// Returns the size (in bytes) of an array with n elements.
|
||||
// Can be very handy to determine the size of a StaticMemoryPool.
|
||||
@ -15,18 +15,18 @@
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
class JsonObject;
|
||||
class JsonArraySubscript;
|
||||
class ObjectRef;
|
||||
class ArraySubscript;
|
||||
|
||||
template <typename TData>
|
||||
class JsonArrayProxy {
|
||||
class ArrayRefBase {
|
||||
public:
|
||||
FORCE_INLINE bool isNull() const {
|
||||
return _data == 0;
|
||||
}
|
||||
|
||||
FORCE_INLINE JsonVariantConst operator[](size_t index) const {
|
||||
return JsonVariantConst(arrayGet(_data, index));
|
||||
FORCE_INLINE VariantConstRef operator[](size_t index) const {
|
||||
return VariantConstRef(arrayGet(_data, index));
|
||||
}
|
||||
|
||||
FORCE_INLINE size_t size() const {
|
||||
@ -34,17 +34,16 @@ class JsonArrayProxy {
|
||||
}
|
||||
|
||||
protected:
|
||||
JsonArrayProxy(TData* data) : _data(data) {}
|
||||
ArrayRefBase(TData* data) : _data(data) {}
|
||||
TData* _data;
|
||||
};
|
||||
|
||||
class JsonArrayConst : public JsonArrayProxy<const JsonArrayData>,
|
||||
public Visitable {
|
||||
friend class JsonArray;
|
||||
typedef JsonArrayProxy<const JsonArrayData> proxy_type;
|
||||
class ArrayConstRef : public ArrayRefBase<const ArrayData>, public Visitable {
|
||||
friend class ArrayRef;
|
||||
typedef ArrayRefBase<const ArrayData> base_type;
|
||||
|
||||
public:
|
||||
typedef JsonArrayConstIterator iterator;
|
||||
typedef ArrayConstRefIterator iterator;
|
||||
|
||||
template <typename Visitor>
|
||||
FORCE_INLINE void accept(Visitor& visitor) const {
|
||||
@ -63,43 +62,43 @@ class JsonArrayConst : public JsonArrayProxy<const JsonArrayData>,
|
||||
return iterator();
|
||||
}
|
||||
|
||||
FORCE_INLINE JsonArrayConst() : proxy_type(0) {}
|
||||
FORCE_INLINE JsonArrayConst(const JsonArrayData* data) : proxy_type(data) {}
|
||||
FORCE_INLINE ArrayConstRef() : base_type(0) {}
|
||||
FORCE_INLINE ArrayConstRef(const ArrayData* data) : base_type(data) {}
|
||||
|
||||
FORCE_INLINE bool operator==(JsonArrayConst rhs) const {
|
||||
FORCE_INLINE bool operator==(ArrayConstRef rhs) const {
|
||||
return arrayEquals(_data, rhs._data);
|
||||
}
|
||||
};
|
||||
|
||||
class JsonArray : public JsonArrayProxy<JsonArrayData>, public Visitable {
|
||||
typedef JsonArrayProxy<JsonArrayData> proxy_type;
|
||||
class ArrayRef : public ArrayRefBase<ArrayData>, public Visitable {
|
||||
typedef ArrayRefBase<ArrayData> base_type;
|
||||
|
||||
public:
|
||||
typedef JsonArrayIterator iterator;
|
||||
typedef ArrayIterator iterator;
|
||||
|
||||
FORCE_INLINE JsonArray() : proxy_type(0), _memoryPool(0) {}
|
||||
FORCE_INLINE JsonArray(MemoryPool* pool, JsonArrayData* data)
|
||||
: proxy_type(data), _memoryPool(pool) {}
|
||||
FORCE_INLINE ArrayRef() : base_type(0), _memoryPool(0) {}
|
||||
FORCE_INLINE ArrayRef(MemoryPool* pool, ArrayData* data)
|
||||
: base_type(data), _memoryPool(pool) {}
|
||||
|
||||
operator JsonVariant() {
|
||||
return JsonVariant(_memoryPool, getVariantData(_data));
|
||||
operator VariantRef() {
|
||||
return VariantRef(_memoryPool, getVariantData(_data));
|
||||
}
|
||||
|
||||
operator JsonArrayConst() const {
|
||||
return JsonArrayConst(_data);
|
||||
operator ArrayConstRef() const {
|
||||
return ArrayConstRef(_data);
|
||||
}
|
||||
|
||||
// Adds the specified value at the end of the array.
|
||||
//
|
||||
// bool add(TValue);
|
||||
// TValue = bool, long, int, short, float, double, serialized, JsonVariant,
|
||||
// std::string, String, JsonObject
|
||||
// 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(JsonArray value) const {
|
||||
FORCE_INLINE bool add(ArrayRef value) const {
|
||||
return add().set(value);
|
||||
}
|
||||
//
|
||||
@ -110,8 +109,8 @@ class JsonArray : public JsonArrayProxy<JsonArrayData>, public Visitable {
|
||||
return add().set(value);
|
||||
}
|
||||
|
||||
JsonVariant add() const {
|
||||
return JsonVariant(_memoryPool, arrayAdd(_data, _memoryPool));
|
||||
VariantRef add() const {
|
||||
return VariantRef(_memoryPool, arrayAdd(_data, _memoryPool));
|
||||
}
|
||||
|
||||
FORCE_INLINE iterator begin() const {
|
||||
@ -144,7 +143,7 @@ class JsonArray : public JsonArrayProxy<JsonArrayData>, public Visitable {
|
||||
bool copyFrom(T (&array)[N1][N2]) const {
|
||||
bool ok = true;
|
||||
for (size_t i = 0; i < N1; i++) {
|
||||
JsonArray nestedArray = createNestedArray();
|
||||
ArrayRef nestedArray = createNestedArray();
|
||||
for (size_t j = 0; j < N2; j++) {
|
||||
ok &= nestedArray.add(array[i][j]);
|
||||
}
|
||||
@ -152,8 +151,8 @@ class JsonArray : public JsonArrayProxy<JsonArrayData>, public Visitable {
|
||||
return ok;
|
||||
}
|
||||
|
||||
// Copy a JsonArray
|
||||
FORCE_INLINE bool copyFrom(JsonArray src) const {
|
||||
// Copy a ArrayRef
|
||||
FORCE_INLINE bool copyFrom(ArrayRef src) const {
|
||||
return arrayCopy(_data, src._data, _memoryPool);
|
||||
}
|
||||
|
||||
@ -177,22 +176,22 @@ class JsonArray : public JsonArrayProxy<JsonArrayData>, public Visitable {
|
||||
if (!_data) return;
|
||||
size_t i = 0;
|
||||
for (iterator it = begin(); it != end() && i < N1; ++it) {
|
||||
it->as<JsonArray>().copyTo(array[i++]);
|
||||
it->as<ArrayRef>().copyTo(array[i++]);
|
||||
}
|
||||
}
|
||||
|
||||
FORCE_INLINE JsonArray createNestedArray() const;
|
||||
FORCE_INLINE JsonObject createNestedObject() const;
|
||||
FORCE_INLINE ArrayRef createNestedArray() const;
|
||||
FORCE_INLINE ObjectRef createNestedObject() const;
|
||||
|
||||
FORCE_INLINE JsonArraySubscript operator[](size_t index) const;
|
||||
FORCE_INLINE ArraySubscript operator[](size_t index) const;
|
||||
|
||||
FORCE_INLINE bool operator==(JsonArray rhs) const {
|
||||
FORCE_INLINE bool operator==(ArrayRef rhs) const {
|
||||
return arrayEquals(_data, rhs._data);
|
||||
}
|
||||
|
||||
// Gets the value at the specified index.
|
||||
FORCE_INLINE JsonVariant get(size_t index) const {
|
||||
return JsonVariant(_memoryPool, arrayGet(_data, index));
|
||||
FORCE_INLINE VariantRef get(size_t index) const {
|
||||
return VariantRef(_memoryPool, arrayGet(_data, index));
|
||||
}
|
||||
|
||||
// Removes element at specified position.
|
||||
@ -207,7 +206,7 @@ class JsonArray : public JsonArrayProxy<JsonArrayData>, public Visitable {
|
||||
|
||||
template <typename Visitor>
|
||||
FORCE_INLINE void accept(Visitor& visitor) const {
|
||||
JsonArrayConst(_data).accept(visitor);
|
||||
ArrayConstRef(_data).accept(visitor);
|
||||
}
|
||||
|
||||
private:
|
@ -4,8 +4,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Configuration.hpp"
|
||||
#include "JsonVariantBase.hpp"
|
||||
#include "../Configuration.hpp"
|
||||
#include "../Operators/VariantOperators.hpp"
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(push)
|
||||
@ -13,24 +13,24 @@
|
||||
#endif
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
class JsonArraySubscript : public JsonVariantBase<JsonArraySubscript>,
|
||||
public Visitable {
|
||||
class ArraySubscript : public VariantOperators<ArraySubscript>,
|
||||
public Visitable {
|
||||
public:
|
||||
FORCE_INLINE JsonArraySubscript(JsonArray array, size_t index)
|
||||
FORCE_INLINE ArraySubscript(ArrayRef array, size_t index)
|
||||
: _array(array), _index(index) {}
|
||||
|
||||
FORCE_INLINE JsonArraySubscript& operator=(const JsonArraySubscript& src) {
|
||||
get_impl().set(src.as<JsonVariantConst>());
|
||||
FORCE_INLINE ArraySubscript& operator=(const ArraySubscript& src) {
|
||||
get_impl().set(src.as<VariantConstRef>());
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Replaces the value
|
||||
//
|
||||
// operator=(const TValue&)
|
||||
// TValue = bool, long, int, short, float, double, serialized, JsonVariant,
|
||||
// std::string, String, JsonArray, JsonObject
|
||||
// TValue = bool, long, int, short, float, double, serialized, VariantRef,
|
||||
// std::string, String, ArrayRef, ObjectRef
|
||||
template <typename T>
|
||||
FORCE_INLINE JsonArraySubscript& operator=(const T& src) {
|
||||
FORCE_INLINE ArraySubscript& operator=(const T& src) {
|
||||
get_impl().set(src);
|
||||
return *this;
|
||||
}
|
||||
@ -38,7 +38,7 @@ class JsonArraySubscript : public JsonVariantBase<JsonArraySubscript>,
|
||||
// operator=(TValue)
|
||||
// TValue = char*, const char*, const FlashStringHelper*
|
||||
template <typename T>
|
||||
FORCE_INLINE JsonArraySubscript& operator=(T* src) {
|
||||
FORCE_INLINE ArraySubscript& operator=(T* src) {
|
||||
get_impl().set(src);
|
||||
return *this;
|
||||
}
|
||||
@ -48,7 +48,7 @@ class JsonArraySubscript : public JsonVariantBase<JsonArraySubscript>,
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
FORCE_INLINE typename JsonVariantAs<T>::type as() const {
|
||||
FORCE_INLINE typename VariantAs<T>::type as() const {
|
||||
return get_impl().as<T>();
|
||||
}
|
||||
|
||||
@ -58,15 +58,15 @@ class JsonArraySubscript : public JsonVariantBase<JsonArraySubscript>,
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
FORCE_INLINE typename JsonVariantTo<T>::type to() const {
|
||||
FORCE_INLINE typename VariantTo<T>::type to() const {
|
||||
return get_impl().to<T>();
|
||||
}
|
||||
|
||||
// Replaces the value
|
||||
//
|
||||
// bool set(const TValue&)
|
||||
// TValue = bool, long, int, short, float, double, serialized, JsonVariant,
|
||||
// std::string, String, JsonArray, JsonObject
|
||||
// TValue = bool, long, int, short, float, double, serialized, VariantRef,
|
||||
// std::string, String, ArrayRef, ObjectRef
|
||||
template <typename TValue>
|
||||
FORCE_INLINE bool set(const TValue& value) const {
|
||||
return get_impl().set(value);
|
||||
@ -89,22 +89,21 @@ class JsonArraySubscript : public JsonVariantBase<JsonArraySubscript>,
|
||||
}
|
||||
|
||||
private:
|
||||
FORCE_INLINE JsonVariant get_impl() const {
|
||||
FORCE_INLINE VariantRef get_impl() const {
|
||||
return _array.get(_index);
|
||||
}
|
||||
|
||||
JsonArray _array;
|
||||
ArrayRef _array;
|
||||
const size_t _index;
|
||||
};
|
||||
|
||||
template <typename TImpl>
|
||||
inline JsonArraySubscript JsonVariantSubscripts<TImpl>::operator[](
|
||||
size_t index) const {
|
||||
return impl()->template as<JsonArray>()[index];
|
||||
inline ArraySubscript VariantSubscripts<TImpl>::operator[](size_t index) const {
|
||||
return impl()->template as<ArrayRef>()[index];
|
||||
}
|
||||
|
||||
inline JsonArraySubscript JsonArray::operator[](size_t index) const {
|
||||
return JsonArraySubscript(*this, index);
|
||||
inline ArraySubscript ArrayRef::operator[](size_t index) const {
|
||||
return ArraySubscript(*this, index);
|
||||
}
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
||||
|
@ -1,49 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
class JsonArray;
|
||||
class JsonArrayConst;
|
||||
class JsonObject;
|
||||
class JsonObjectConst;
|
||||
class JsonVariant;
|
||||
class JsonVariantConst;
|
||||
|
||||
// A metafunction that returns the type of the value returned by
|
||||
// JsonVariant::as<T>()
|
||||
template <typename T>
|
||||
struct JsonVariantAs {
|
||||
typedef T type;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct JsonVariantAs<char*> {
|
||||
typedef const char* type;
|
||||
};
|
||||
|
||||
// A metafunction that returns the type of the value returned by
|
||||
// JsonVariant::as<T>()
|
||||
template <typename T>
|
||||
struct JsonVariantConstAs {
|
||||
typedef typename JsonVariantAs<T>::type type;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct JsonVariantConstAs<JsonVariant> {
|
||||
typedef JsonVariantConst type;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct JsonVariantConstAs<JsonObject> {
|
||||
typedef JsonObjectConst type;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct JsonVariantConstAs<JsonArray> {
|
||||
typedef JsonArrayConst type;
|
||||
};
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
@ -1,79 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h> // ptrdiff_t, size_t
|
||||
|
||||
#include "JsonFloat.hpp"
|
||||
#include "JsonInteger.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
enum JsonVariantType {
|
||||
JSON_NULL,
|
||||
JSON_LINKED_RAW,
|
||||
JSON_OWNED_RAW,
|
||||
JSON_LINKED_STRING,
|
||||
JSON_OWNED_STRING,
|
||||
JSON_BOOLEAN,
|
||||
JSON_POSITIVE_INTEGER,
|
||||
JSON_NEGATIVE_INTEGER,
|
||||
JSON_ARRAY,
|
||||
JSON_OBJECT,
|
||||
JSON_FLOAT
|
||||
};
|
||||
|
||||
struct JsonObjectData {
|
||||
struct VariantSlot *head;
|
||||
struct VariantSlot *tail;
|
||||
};
|
||||
|
||||
struct JsonArrayData {
|
||||
struct VariantSlot *head;
|
||||
struct VariantSlot *tail;
|
||||
};
|
||||
|
||||
struct RawData {
|
||||
const char *data;
|
||||
size_t size;
|
||||
};
|
||||
|
||||
// A union that defines the actual content of a JsonVariantData.
|
||||
// The enum JsonVariantType determines which member is in use.
|
||||
union JsonVariantContent {
|
||||
JsonFloat asFloat;
|
||||
JsonUInt asInteger;
|
||||
JsonArrayData asArray;
|
||||
JsonObjectData asObject;
|
||||
const char *asString;
|
||||
struct {
|
||||
const char *data;
|
||||
size_t size;
|
||||
} asRaw;
|
||||
};
|
||||
|
||||
// this struct must be a POD type to prevent error calling offsetof on clang
|
||||
struct JsonVariantData {
|
||||
bool keyIsOwned : 1;
|
||||
JsonVariantType type : 7;
|
||||
JsonVariantContent content;
|
||||
};
|
||||
|
||||
inline JsonVariantData *getVariantData(JsonArrayData *arr) {
|
||||
const ptrdiff_t offset = offsetof(JsonVariantData, content) -
|
||||
offsetof(JsonVariantContent, asArray);
|
||||
if (!arr) return 0;
|
||||
return reinterpret_cast<JsonVariantData *>(reinterpret_cast<char *>(arr) -
|
||||
offset);
|
||||
}
|
||||
|
||||
inline JsonVariantData *getVariantData(JsonObjectData *obj) {
|
||||
const ptrdiff_t offset = offsetof(JsonVariantData, content) -
|
||||
offsetof(JsonVariantContent, asObject);
|
||||
if (!obj) return 0;
|
||||
return reinterpret_cast<JsonVariantData *>(reinterpret_cast<char *>(obj) -
|
||||
offset);
|
||||
}
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
@ -1,48 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../Serialization/DynamicStringWriter.hpp"
|
||||
#include "VariantFunctions.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
class JsonVariantConst;
|
||||
|
||||
template <typename T>
|
||||
inline typename enable_if<is_integral<T>::value, T>::type variantAs(
|
||||
const JsonVariantData* _data) {
|
||||
return variantAsIntegral<T>(_data);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline typename enable_if<is_same<T, bool>::value, T>::type variantAs(
|
||||
const JsonVariantData* _data) {
|
||||
return variantAsBoolean(_data);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline typename enable_if<is_floating_point<T>::value, T>::type variantAs(
|
||||
const JsonVariantData* _data) {
|
||||
return variantAsFloat<T>(_data);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline typename enable_if<is_same<T, const char*>::value ||
|
||||
is_same<T, char*>::value,
|
||||
const char*>::type
|
||||
variantAs(const JsonVariantData* _data) {
|
||||
return variantAsString(_data);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline typename enable_if<is_same<JsonVariantConst, T>::value, T>::type
|
||||
variantAs(const JsonVariantData* _data);
|
||||
|
||||
template <typename T>
|
||||
inline typename enable_if<IsWriteableString<T>::value, T>::type variantAs(
|
||||
const JsonVariantData* _data);
|
||||
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
@ -33,7 +33,7 @@ deserialize(TDocument &doc, const TString &input) {
|
||||
return makeDeserializer<TDeserializer>(
|
||||
doc.memoryPool(), makeReader(input),
|
||||
makeStringStorage(doc.memoryPool(), input), doc.nestingLimit)
|
||||
.parse(doc.template to<JsonVariant>());
|
||||
.parse(doc.template to<VariantRef>());
|
||||
}
|
||||
//
|
||||
// DeserializationError deserialize(TDocument& doc, TChar* input);
|
||||
@ -45,7 +45,7 @@ DeserializationError deserialize(TDocument &doc, TChar *input) {
|
||||
return makeDeserializer<TDeserializer>(
|
||||
doc.memoryPool(), makeReader(input),
|
||||
makeStringStorage(doc.memoryPool(), input), doc.nestingLimit)
|
||||
.parse(doc.template to<JsonVariant>());
|
||||
.parse(doc.template to<VariantRef>());
|
||||
}
|
||||
//
|
||||
// DeserializationError deserialize(TDocument& doc, TChar* input, size_t
|
||||
@ -59,7 +59,7 @@ DeserializationError deserialize(TDocument &doc, TChar *input,
|
||||
return makeDeserializer<TDeserializer>(
|
||||
doc.memoryPool(), makeReader(input, inputSize),
|
||||
makeStringStorage(doc.memoryPool(), input), doc.nestingLimit)
|
||||
.parse(doc.template to<JsonVariant>());
|
||||
.parse(doc.template to<VariantRef>());
|
||||
}
|
||||
//
|
||||
// DeserializationError deserialize(TDocument& doc, TStream input);
|
||||
@ -71,6 +71,6 @@ DeserializationError deserialize(TDocument &doc, TStream &input) {
|
||||
return makeDeserializer<TDeserializer>(
|
||||
doc.memoryPool(), makeReader(input),
|
||||
makeStringStorage(doc.memoryPool(), input), doc.nestingLimit)
|
||||
.parse(doc.template to<JsonVariant>());
|
||||
.parse(doc.template to<VariantRef>());
|
||||
}
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
||||
|
@ -4,9 +4,9 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Data/JsonVariantTo.hpp"
|
||||
#include "JsonVariant.hpp"
|
||||
#include "Memory/MemoryPool.hpp"
|
||||
#include "../Memory/MemoryPool.hpp"
|
||||
#include "../Variant/VariantRef.hpp"
|
||||
#include "../Variant/VariantTo.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
@ -20,12 +20,12 @@ class JsonDocument : public Visitable {
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename JsonVariantAs<T>::type as() {
|
||||
typename VariantAs<T>::type as() {
|
||||
return getVariant().template as<T>();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename JsonVariantConstAs<T>::type as() const {
|
||||
typename VariantConstAs<T>::type as() const {
|
||||
return getVariant().template as<T>();
|
||||
}
|
||||
|
||||
@ -53,7 +53,7 @@ class JsonDocument : public Visitable {
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename JsonVariantTo<T>::type to() {
|
||||
typename VariantTo<T>::type to() {
|
||||
clear();
|
||||
return getVariant().template to<T>();
|
||||
}
|
||||
@ -65,20 +65,20 @@ class JsonDocument : public Visitable {
|
||||
|
||||
void copy(const JsonDocument& src) {
|
||||
nestingLimit = src.nestingLimit;
|
||||
to<JsonVariant>().set(src.as<JsonVariant>());
|
||||
to<VariantRef>().set(src.as<VariantRef>());
|
||||
}
|
||||
|
||||
private:
|
||||
JsonVariant getVariant() {
|
||||
return JsonVariant(&_memoryPool, &_rootData);
|
||||
VariantRef getVariant() {
|
||||
return VariantRef(&_memoryPool, &_rootData);
|
||||
}
|
||||
|
||||
JsonVariantConst getVariant() const {
|
||||
return JsonVariantConst(&_rootData);
|
||||
VariantConstRef getVariant() const {
|
||||
return VariantConstRef(&_rootData);
|
||||
}
|
||||
|
||||
MemoryPool _memoryPool;
|
||||
JsonVariantData _rootData;
|
||||
VariantData _rootData;
|
||||
};
|
||||
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
@ -5,12 +5,12 @@
|
||||
#pragma once
|
||||
|
||||
#include "../Deserialization/deserialize.hpp"
|
||||
#include "../JsonVariant.hpp"
|
||||
#include "../Memory/MemoryPool.hpp"
|
||||
#include "../Numbers/isFloat.hpp"
|
||||
#include "../Numbers/isInteger.hpp"
|
||||
#include "../Polyfills/type_traits.hpp"
|
||||
#include "./EscapeSequence.hpp"
|
||||
#include "../Variant/VariantRef.hpp"
|
||||
#include "EscapeSequence.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
@ -28,7 +28,7 @@ class JsonDeserializer {
|
||||
_stringStorage(stringStorage),
|
||||
_nestingLimit(nestingLimit),
|
||||
_loaded(false) {}
|
||||
DeserializationError parse(JsonVariant variant) {
|
||||
DeserializationError parse(VariantRef variant) {
|
||||
DeserializationError err = skipSpacesAndComments();
|
||||
if (err) return err;
|
||||
|
||||
@ -68,10 +68,10 @@ class JsonDeserializer {
|
||||
return true;
|
||||
}
|
||||
|
||||
DeserializationError parseArray(JsonVariant variant) {
|
||||
DeserializationError parseArray(VariantRef variant) {
|
||||
if (_nestingLimit == 0) return DeserializationError::TooDeep;
|
||||
|
||||
JsonArray array = variant.to<JsonArray>();
|
||||
ArrayRef array = variant.to<ArrayRef>();
|
||||
if (array.isNull()) return DeserializationError::NoMemory;
|
||||
|
||||
// Check opening braket
|
||||
@ -87,7 +87,7 @@ class JsonDeserializer {
|
||||
// Read each value
|
||||
for (;;) {
|
||||
// Allocate slot in array
|
||||
JsonVariant value = array.add();
|
||||
VariantRef value = array.add();
|
||||
if (value.isInvalid()) return DeserializationError::NoMemory;
|
||||
|
||||
// 1 - Parse value
|
||||
@ -106,10 +106,10 @@ class JsonDeserializer {
|
||||
}
|
||||
}
|
||||
|
||||
DeserializationError parseObject(JsonVariant variant) {
|
||||
DeserializationError parseObject(VariantRef variant) {
|
||||
if (_nestingLimit == 0) return DeserializationError::TooDeep;
|
||||
|
||||
JsonObject object = variant.to<JsonObject>();
|
||||
ObjectRef object = variant.to<ObjectRef>();
|
||||
if (object.isNull()) return DeserializationError::NoMemory;
|
||||
|
||||
// Check opening brace
|
||||
@ -135,7 +135,7 @@ class JsonDeserializer {
|
||||
if (!eat(':')) return DeserializationError::InvalidInput;
|
||||
|
||||
// Allocate slot in object
|
||||
JsonVariant value = object.set(key);
|
||||
VariantRef value = object.set(key);
|
||||
if (value.isInvalid()) return DeserializationError::NoMemory;
|
||||
|
||||
// Parse value
|
||||
@ -158,7 +158,7 @@ class JsonDeserializer {
|
||||
}
|
||||
}
|
||||
|
||||
DeserializationError parseValue(JsonVariant variant) {
|
||||
DeserializationError parseValue(VariantRef variant) {
|
||||
if (isQuote(current())) {
|
||||
return parseStringValue(variant);
|
||||
} else {
|
||||
@ -174,7 +174,7 @@ class JsonDeserializer {
|
||||
}
|
||||
}
|
||||
|
||||
DeserializationError parseStringValue(JsonVariant variant) {
|
||||
DeserializationError parseStringValue(VariantRef variant) {
|
||||
StringType value;
|
||||
DeserializationError err = parseQuotedString(value);
|
||||
if (err) return err;
|
||||
@ -233,7 +233,7 @@ class JsonDeserializer {
|
||||
return DeserializationError::Ok;
|
||||
}
|
||||
|
||||
DeserializationError parseNumericValue(JsonVariant result) {
|
||||
DeserializationError parseNumericValue(VariantRef result) {
|
||||
char buffer[64];
|
||||
uint8_t n = 0;
|
||||
|
||||
@ -246,9 +246,9 @@ class JsonDeserializer {
|
||||
buffer[n] = 0;
|
||||
|
||||
if (isInteger(buffer)) {
|
||||
result.set(parseInteger<JsonInteger>(buffer));
|
||||
result.set(parseInteger<Integer>(buffer));
|
||||
} else if (isFloat(buffer)) {
|
||||
result.set(parseFloat<JsonFloat>(buffer));
|
||||
result.set(parseFloat<Float>(buffer));
|
||||
} else if (!strcmp(buffer, "true")) {
|
||||
result.set(true);
|
||||
} else if (!strcmp(buffer, "false")) {
|
||||
|
@ -4,9 +4,9 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../Misc/Visitable.hpp"
|
||||
#include "../Serialization/measure.hpp"
|
||||
#include "../Serialization/serialize.hpp"
|
||||
#include "../Visitable.hpp"
|
||||
#include "JsonWriter.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
@ -16,14 +16,14 @@ class JsonSerializer {
|
||||
public:
|
||||
JsonSerializer(TWriter &writer) : _writer(writer) {}
|
||||
|
||||
void visitFloat(JsonFloat value) {
|
||||
void visitFloat(Float value) {
|
||||
_writer.writeFloat(value);
|
||||
}
|
||||
|
||||
void visitArray(JsonArrayConst array) {
|
||||
void visitArray(ArrayConstRef array) {
|
||||
_writer.beginArray();
|
||||
|
||||
JsonArrayConst::iterator it = array.begin();
|
||||
ArrayConstRef::iterator it = array.begin();
|
||||
while (it != array.end()) {
|
||||
it->accept(*this);
|
||||
|
||||
@ -36,10 +36,10 @@ class JsonSerializer {
|
||||
_writer.endArray();
|
||||
}
|
||||
|
||||
void visitObject(JsonObjectConst object) {
|
||||
void visitObject(ObjectConstRef object) {
|
||||
_writer.beginObject();
|
||||
|
||||
JsonObjectConst::iterator it = object.begin();
|
||||
ObjectConstRef::iterator it = object.begin();
|
||||
while (it != object.end()) {
|
||||
_writer.writeString(it->key());
|
||||
_writer.writeColon();
|
||||
@ -63,12 +63,12 @@ class JsonSerializer {
|
||||
for (size_t i = 0; i < n; i++) _writer.writeRaw(data[i]);
|
||||
}
|
||||
|
||||
void visitNegativeInteger(JsonUInt value) {
|
||||
void visitNegativeInteger(UInt value) {
|
||||
_writer.writeRaw('-');
|
||||
_writer.writeInteger(value);
|
||||
}
|
||||
|
||||
void visitPositiveInteger(JsonUInt value) {
|
||||
void visitPositiveInteger(UInt value) {
|
||||
_writer.writeInteger(value);
|
||||
}
|
||||
|
||||
|
@ -6,10 +6,10 @@
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h> // for strlen
|
||||
#include "../Data/JsonInteger.hpp"
|
||||
#include "../Numbers/FloatParts.hpp"
|
||||
#include "../Numbers/Integer.hpp"
|
||||
#include "../Polyfills/attributes.hpp"
|
||||
#include "./EscapeSequence.hpp"
|
||||
#include "EscapeSequence.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
@ -71,8 +71,8 @@ class JsonWriter {
|
||||
}
|
||||
}
|
||||
|
||||
template <typename TFloat>
|
||||
void writeFloat(TFloat value) {
|
||||
template <typename T>
|
||||
void writeFloat(T value) {
|
||||
if (isnan(value)) return writeRaw("NaN");
|
||||
|
||||
if (value < 0.0) {
|
||||
@ -82,7 +82,7 @@ class JsonWriter {
|
||||
|
||||
if (isinf(value)) return writeRaw("Infinity");
|
||||
|
||||
FloatParts<TFloat> parts(value);
|
||||
FloatParts<T> parts(value);
|
||||
|
||||
writeInteger(parts.integral);
|
||||
if (parts.decimalPlaces) writeDecimals(parts.decimal, parts.decimalPlaces);
|
||||
@ -98,8 +98,8 @@ class JsonWriter {
|
||||
}
|
||||
}
|
||||
|
||||
template <typename UInt>
|
||||
void writeInteger(UInt value) {
|
||||
template <typename T>
|
||||
void writeInteger(T value) {
|
||||
char buffer[22];
|
||||
char *end = buffer + sizeof(buffer);
|
||||
char *begin = end;
|
||||
@ -107,7 +107,7 @@ class JsonWriter {
|
||||
// write the string in reverse order
|
||||
do {
|
||||
*--begin = char(value % 10 + '0');
|
||||
value = UInt(value / 10);
|
||||
value = T(value / 10);
|
||||
} while (value);
|
||||
|
||||
// and dump it in the right order
|
||||
|
@ -1,19 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "JsonArray.hpp"
|
||||
#include "JsonObject.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
inline JsonArray JsonArray::createNestedArray() const {
|
||||
return add().to<JsonArray>();
|
||||
}
|
||||
|
||||
inline JsonObject JsonArray::createNestedObject() const {
|
||||
return add().to<JsonObject>();
|
||||
}
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
@ -1,122 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Data/SlotFunctions.hpp"
|
||||
#include "JsonVariant.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
class JsonVariantPtr {
|
||||
public:
|
||||
JsonVariantPtr(MemoryPool *memoryPool, JsonVariantData *data)
|
||||
: _variant(memoryPool, data) {}
|
||||
|
||||
JsonVariant *operator->() {
|
||||
return &_variant;
|
||||
}
|
||||
|
||||
JsonVariant &operator*() {
|
||||
return _variant;
|
||||
}
|
||||
|
||||
private:
|
||||
JsonVariant _variant;
|
||||
};
|
||||
|
||||
class JsonArrayIterator {
|
||||
public:
|
||||
JsonArrayIterator() : _slot(0) {}
|
||||
explicit JsonArrayIterator(MemoryPool *memoryPool, VariantSlot *slot)
|
||||
: _memoryPool(memoryPool), _slot(slot) {}
|
||||
|
||||
JsonVariant operator*() const {
|
||||
return JsonVariant(_memoryPool, &_slot->value);
|
||||
}
|
||||
JsonVariantPtr operator->() {
|
||||
return JsonVariantPtr(_memoryPool, &_slot->value);
|
||||
}
|
||||
|
||||
bool operator==(const JsonArrayIterator &other) const {
|
||||
return _slot == other._slot;
|
||||
}
|
||||
|
||||
bool operator!=(const JsonArrayIterator &other) const {
|
||||
return _slot != other._slot;
|
||||
}
|
||||
|
||||
JsonArrayIterator &operator++() {
|
||||
_slot = _slot->getNext();
|
||||
return *this;
|
||||
}
|
||||
|
||||
JsonArrayIterator &operator+=(size_t distance) {
|
||||
_slot = _slot->getNext(distance);
|
||||
return *this;
|
||||
}
|
||||
|
||||
VariantSlot *internal() {
|
||||
return _slot;
|
||||
}
|
||||
|
||||
private:
|
||||
MemoryPool *_memoryPool;
|
||||
VariantSlot *_slot;
|
||||
};
|
||||
|
||||
class JsonVariantConstPtr {
|
||||
public:
|
||||
JsonVariantConstPtr(const JsonVariantData *data) : _variant(data) {}
|
||||
|
||||
JsonVariantConst *operator->() {
|
||||
return &_variant;
|
||||
}
|
||||
|
||||
JsonVariantConst &operator*() {
|
||||
return _variant;
|
||||
}
|
||||
|
||||
private:
|
||||
JsonVariantConst _variant;
|
||||
};
|
||||
|
||||
class JsonArrayConstIterator {
|
||||
public:
|
||||
JsonArrayConstIterator() : _slot(0) {}
|
||||
explicit JsonArrayConstIterator(const VariantSlot *slot) : _slot(slot) {}
|
||||
|
||||
JsonVariantConst operator*() const {
|
||||
return JsonVariantConst(&_slot->value);
|
||||
}
|
||||
JsonVariantConstPtr operator->() {
|
||||
return JsonVariantConstPtr(&_slot->value);
|
||||
}
|
||||
|
||||
bool operator==(const JsonArrayConstIterator &other) const {
|
||||
return _slot == other._slot;
|
||||
}
|
||||
|
||||
bool operator!=(const JsonArrayConstIterator &other) const {
|
||||
return _slot != other._slot;
|
||||
}
|
||||
|
||||
JsonArrayConstIterator &operator++() {
|
||||
_slot = _slot->getNext();
|
||||
return *this;
|
||||
}
|
||||
|
||||
JsonArrayConstIterator &operator+=(size_t distance) {
|
||||
_slot = _slot->getNext(distance);
|
||||
return *this;
|
||||
}
|
||||
|
||||
const VariantSlot *internal() {
|
||||
return _slot;
|
||||
}
|
||||
|
||||
private:
|
||||
const VariantSlot *_slot;
|
||||
};
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
@ -1,21 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "JsonArray.hpp"
|
||||
#include "JsonObject.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
template <typename TString>
|
||||
inline JsonArray JsonObject::createNestedArray(const TString& key) const {
|
||||
return set(key).template to<JsonArray>();
|
||||
}
|
||||
|
||||
template <typename TString>
|
||||
inline JsonArray JsonObject::createNestedArray(TString* key) const {
|
||||
return set(key).template to<JsonArray>();
|
||||
}
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
@ -1,124 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Data/SlotFunctions.hpp"
|
||||
#include "JsonPair.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
class JsonPairPtr {
|
||||
public:
|
||||
JsonPairPtr(MemoryPool *memoryPool, VariantSlot *slot)
|
||||
: _pair(memoryPool, slot) {}
|
||||
|
||||
const JsonPair *operator->() const {
|
||||
return &_pair;
|
||||
}
|
||||
|
||||
const JsonPair &operator*() const {
|
||||
return _pair;
|
||||
}
|
||||
|
||||
private:
|
||||
JsonPair _pair;
|
||||
};
|
||||
|
||||
class JsonObjectIterator {
|
||||
public:
|
||||
JsonObjectIterator() : _slot(0) {}
|
||||
|
||||
explicit JsonObjectIterator(MemoryPool *memoryPool, VariantSlot *slot)
|
||||
: _memoryPool(memoryPool), _slot(slot) {}
|
||||
|
||||
JsonPair operator*() const {
|
||||
return JsonPair(_memoryPool, _slot);
|
||||
}
|
||||
JsonPairPtr operator->() {
|
||||
return JsonPairPtr(_memoryPool, _slot);
|
||||
}
|
||||
|
||||
bool operator==(const JsonObjectIterator &other) const {
|
||||
return _slot == other._slot;
|
||||
}
|
||||
|
||||
bool operator!=(const JsonObjectIterator &other) const {
|
||||
return _slot != other._slot;
|
||||
}
|
||||
|
||||
JsonObjectIterator &operator++() {
|
||||
_slot = _slot->getNext();
|
||||
return *this;
|
||||
}
|
||||
|
||||
JsonObjectIterator &operator+=(size_t distance) {
|
||||
_slot = _slot->getNext(distance);
|
||||
return *this;
|
||||
}
|
||||
|
||||
VariantSlot *internal() {
|
||||
return _slot;
|
||||
}
|
||||
|
||||
private:
|
||||
MemoryPool *_memoryPool;
|
||||
VariantSlot *_slot;
|
||||
};
|
||||
|
||||
class JsonPairConstPtr {
|
||||
public:
|
||||
JsonPairConstPtr(const VariantSlot *slot) : _pair(slot) {}
|
||||
|
||||
const JsonPairConst *operator->() const {
|
||||
return &_pair;
|
||||
}
|
||||
|
||||
const JsonPairConst &operator*() const {
|
||||
return _pair;
|
||||
}
|
||||
|
||||
private:
|
||||
JsonPairConst _pair;
|
||||
};
|
||||
|
||||
class JsonObjectConstIterator {
|
||||
public:
|
||||
JsonObjectConstIterator() : _slot(0) {}
|
||||
|
||||
explicit JsonObjectConstIterator(const VariantSlot *slot) : _slot(slot) {}
|
||||
|
||||
JsonPairConst operator*() const {
|
||||
return JsonPairConst(_slot);
|
||||
}
|
||||
JsonPairConstPtr operator->() {
|
||||
return JsonPairConstPtr(_slot);
|
||||
}
|
||||
|
||||
bool operator==(const JsonObjectConstIterator &other) const {
|
||||
return _slot == other._slot;
|
||||
}
|
||||
|
||||
bool operator!=(const JsonObjectConstIterator &other) const {
|
||||
return _slot != other._slot;
|
||||
}
|
||||
|
||||
JsonObjectConstIterator &operator++() {
|
||||
_slot = _slot->getNext();
|
||||
return *this;
|
||||
}
|
||||
|
||||
JsonObjectConstIterator &operator+=(size_t distance) {
|
||||
_slot = _slot->getNext(distance);
|
||||
return *this;
|
||||
}
|
||||
|
||||
const VariantSlot *internal() {
|
||||
return _slot;
|
||||
}
|
||||
|
||||
private:
|
||||
const VariantSlot *_slot;
|
||||
};
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
@ -1,53 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "JsonKey.hpp"
|
||||
#include "JsonVariant.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
// A key value pair for JsonObjectData.
|
||||
class JsonPair {
|
||||
public:
|
||||
JsonPair(MemoryPool* memoryPool, VariantSlot* slot) : _key(slot) {
|
||||
if (slot) {
|
||||
_value = JsonVariant(memoryPool, &slot->value);
|
||||
}
|
||||
}
|
||||
|
||||
JsonKey key() const {
|
||||
return _key;
|
||||
}
|
||||
|
||||
JsonVariant value() const {
|
||||
return _value;
|
||||
}
|
||||
|
||||
private:
|
||||
JsonKey _key;
|
||||
JsonVariant _value;
|
||||
};
|
||||
|
||||
class JsonPairConst {
|
||||
public:
|
||||
JsonPairConst(const VariantSlot* slot) : _key(slot) {
|
||||
if (slot) {
|
||||
_value = JsonVariantConst(&slot->value);
|
||||
}
|
||||
}
|
||||
|
||||
JsonKey key() const {
|
||||
return _key;
|
||||
}
|
||||
|
||||
JsonVariantConst value() const {
|
||||
return _value;
|
||||
}
|
||||
|
||||
private:
|
||||
JsonKey _key;
|
||||
JsonVariantConst _value;
|
||||
};
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
@ -1,19 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "JsonVariantCasts.hpp"
|
||||
#include "JsonVariantComparisons.hpp"
|
||||
#include "JsonVariantOr.hpp"
|
||||
#include "JsonVariantSubscripts.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
template <typename TImpl>
|
||||
class JsonVariantBase : public JsonVariantCasts<TImpl>,
|
||||
public JsonVariantComparisons<TImpl>,
|
||||
public JsonVariantOr<TImpl>,
|
||||
public JsonVariantSubscripts<TImpl> {};
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
@ -1,118 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Configuration.hpp"
|
||||
#include "JsonVariant.hpp"
|
||||
#include "Numbers/parseFloat.hpp"
|
||||
#include "Numbers/parseInteger.hpp"
|
||||
|
||||
#include <string.h> // for strcmp
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
inline bool JsonVariant::set(JsonArray array) const {
|
||||
return to<JsonArray>().copyFrom(array);
|
||||
}
|
||||
|
||||
inline bool JsonVariant::set(const JsonArraySubscript& value) const {
|
||||
return set(value.as<JsonVariant>());
|
||||
}
|
||||
|
||||
inline bool JsonVariant::set(JsonObject object) const {
|
||||
return to<JsonObject>().copyFrom(object);
|
||||
}
|
||||
|
||||
template <typename TString>
|
||||
inline bool JsonVariant::set(const JsonObjectSubscript<TString>& value) const {
|
||||
return set(value.template as<JsonVariant>());
|
||||
}
|
||||
|
||||
inline bool JsonVariant::set(JsonVariantConst value) const {
|
||||
return variantCopy(_data, value._data, _memoryPool);
|
||||
}
|
||||
|
||||
inline bool JsonVariant::set(JsonVariant value) const {
|
||||
return variantCopy(_data, value._data, _memoryPool);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline typename enable_if<is_same<T, JsonArray>::value, T>::type
|
||||
JsonVariant::as() const {
|
||||
return JsonArray(_memoryPool, variantAsArray(_data));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline typename enable_if<is_same<T, JsonObject>::value, T>::type
|
||||
JsonVariant::as() const {
|
||||
return JsonObject(_memoryPool, variantAsObject(_data));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline typename enable_if<is_same<T, JsonArray>::value, JsonArray>::type
|
||||
JsonVariant::to() const {
|
||||
return JsonArray(_memoryPool, variantToArray(_data));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename enable_if<is_same<T, JsonObject>::value, JsonObject>::type
|
||||
JsonVariant::to() const {
|
||||
return JsonObject(_memoryPool, variantToObject(_data));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename enable_if<is_same<T, JsonVariant>::value, JsonVariant>::type
|
||||
JsonVariant::to() const {
|
||||
variantSetNull(_data);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename Visitor>
|
||||
inline void JsonVariant::accept(Visitor& visitor) const {
|
||||
return JsonVariantConst(_data).accept(visitor);
|
||||
}
|
||||
|
||||
template <typename Visitor>
|
||||
inline void JsonVariantConst::accept(Visitor& visitor) const {
|
||||
if (!_data) return visitor.visitNull();
|
||||
|
||||
switch (_data->type) {
|
||||
case JSON_FLOAT:
|
||||
return visitor.visitFloat(_data->content.asFloat);
|
||||
|
||||
case JSON_ARRAY:
|
||||
return visitor.visitArray(JsonArrayConst(&_data->content.asArray));
|
||||
|
||||
case JSON_OBJECT:
|
||||
return visitor.visitObject(JsonObjectConst(&_data->content.asObject));
|
||||
|
||||
case JSON_LINKED_STRING:
|
||||
case JSON_OWNED_STRING:
|
||||
return visitor.visitString(_data->content.asString);
|
||||
|
||||
case JSON_OWNED_RAW:
|
||||
case JSON_LINKED_RAW:
|
||||
return visitor.visitRawJson(_data->content.asRaw.data,
|
||||
_data->content.asRaw.size);
|
||||
|
||||
case JSON_NEGATIVE_INTEGER:
|
||||
return visitor.visitNegativeInteger(_data->content.asInteger);
|
||||
|
||||
case JSON_POSITIVE_INTEGER:
|
||||
return visitor.visitPositiveInteger(_data->content.asInteger);
|
||||
|
||||
case JSON_BOOLEAN:
|
||||
return visitor.visitBoolean(_data->content.asInteger != 0);
|
||||
|
||||
default:
|
||||
return visitor.visitNull();
|
||||
}
|
||||
}
|
||||
|
||||
inline JsonVariantConst JsonVariantConst::operator[](size_t index) const {
|
||||
return JsonArrayConst(variantAsArray(_data))[index];
|
||||
}
|
||||
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
@ -7,10 +7,10 @@
|
||||
#include "../Polyfills/assert.hpp"
|
||||
#include "../Polyfills/mpl/max.hpp"
|
||||
#include "../Strings/StringInMemoryPool.hpp"
|
||||
#include "../Variant/VariantSlot.hpp"
|
||||
#include "Alignment.hpp"
|
||||
#include "MemoryPool.hpp"
|
||||
#include "StringSlot.hpp"
|
||||
#include "VariantSlot.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
|
@ -4,7 +4,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Strings/StringTypes.hpp"
|
||||
#include "../Strings/StringTypes.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
@ -4,7 +4,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Polyfills/type_traits.hpp"
|
||||
#include "../Polyfills/type_traits.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
@ -5,11 +5,11 @@
|
||||
#pragma once
|
||||
|
||||
#include "../Deserialization/deserialize.hpp"
|
||||
#include "../JsonVariant.hpp"
|
||||
#include "../Memory/MemoryPool.hpp"
|
||||
#include "../Polyfills/type_traits.hpp"
|
||||
#include "./endianess.hpp"
|
||||
#include "./ieee754.hpp"
|
||||
#include "../Variant/VariantRef.hpp"
|
||||
#include "endianess.hpp"
|
||||
#include "ieee754.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
@ -27,7 +27,7 @@ class MsgPackDeserializer {
|
||||
_stringStorage(stringStorage),
|
||||
_nestingLimit(nestingLimit) {}
|
||||
|
||||
DeserializationError parse(JsonVariant variant) {
|
||||
DeserializationError parse(VariantRef variant) {
|
||||
uint8_t code;
|
||||
if (!readByte(code)) return DeserializationError::IncompleteInput;
|
||||
|
||||
@ -174,7 +174,7 @@ class MsgPackDeserializer {
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
DeserializationError readInteger(JsonVariant variant) {
|
||||
DeserializationError readInteger(VariantRef variant) {
|
||||
T value;
|
||||
if (!readInteger(value)) return DeserializationError::IncompleteInput;
|
||||
variant.set(value);
|
||||
@ -183,7 +183,7 @@ class MsgPackDeserializer {
|
||||
|
||||
template <typename T>
|
||||
typename enable_if<sizeof(T) == 4, DeserializationError>::type readFloat(
|
||||
JsonVariant variant) {
|
||||
VariantRef variant) {
|
||||
T value;
|
||||
if (!readBytes(value)) return DeserializationError::IncompleteInput;
|
||||
fixEndianess(value);
|
||||
@ -193,7 +193,7 @@ class MsgPackDeserializer {
|
||||
|
||||
template <typename T>
|
||||
typename enable_if<sizeof(T) == 8, DeserializationError>::type readDouble(
|
||||
JsonVariant variant) {
|
||||
VariantRef variant) {
|
||||
T value;
|
||||
if (!readBytes(value)) return DeserializationError::IncompleteInput;
|
||||
fixEndianess(value);
|
||||
@ -203,7 +203,7 @@ class MsgPackDeserializer {
|
||||
|
||||
template <typename T>
|
||||
typename enable_if<sizeof(T) == 4, DeserializationError>::type readDouble(
|
||||
JsonVariant variant) {
|
||||
VariantRef variant) {
|
||||
uint8_t i[8]; // input is 8 bytes
|
||||
T value; // output is 4 bytes
|
||||
uint8_t *o = reinterpret_cast<uint8_t *>(&value);
|
||||
@ -215,7 +215,7 @@ class MsgPackDeserializer {
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
DeserializationError readString(JsonVariant variant) {
|
||||
DeserializationError readString(VariantRef variant) {
|
||||
T size;
|
||||
if (!readInteger(size)) return DeserializationError::IncompleteInput;
|
||||
return readString(variant, size);
|
||||
@ -228,7 +228,7 @@ class MsgPackDeserializer {
|
||||
return readString(str, size);
|
||||
}
|
||||
|
||||
DeserializationError readString(JsonVariant variant, size_t n) {
|
||||
DeserializationError readString(VariantRef variant, size_t n) {
|
||||
StringType s;
|
||||
DeserializationError err = readString(s, n);
|
||||
if (!err) variant.set(s);
|
||||
@ -248,23 +248,23 @@ class MsgPackDeserializer {
|
||||
}
|
||||
|
||||
template <typename TSize>
|
||||
DeserializationError readArray(JsonVariant variant) {
|
||||
DeserializationError readArray(VariantRef variant) {
|
||||
TSize size;
|
||||
if (!readInteger(size)) return DeserializationError::IncompleteInput;
|
||||
return readArray(variant, size);
|
||||
}
|
||||
|
||||
DeserializationError readArray(JsonVariant variant, size_t n) {
|
||||
JsonArray array = variant.to<JsonArray>();
|
||||
DeserializationError readArray(VariantRef variant, size_t n) {
|
||||
ArrayRef array = variant.to<ArrayRef>();
|
||||
if (array.isNull()) return DeserializationError::NoMemory;
|
||||
return readArray(array, n);
|
||||
}
|
||||
|
||||
DeserializationError readArray(JsonArray array, size_t n) {
|
||||
DeserializationError readArray(ArrayRef array, size_t n) {
|
||||
if (_nestingLimit == 0) return DeserializationError::TooDeep;
|
||||
--_nestingLimit;
|
||||
for (; n; --n) {
|
||||
JsonVariant value = array.add();
|
||||
VariantRef value = array.add();
|
||||
if (value.isInvalid()) return DeserializationError::NoMemory;
|
||||
|
||||
DeserializationError err = parse(value);
|
||||
@ -275,20 +275,20 @@ class MsgPackDeserializer {
|
||||
}
|
||||
|
||||
template <typename TSize>
|
||||
DeserializationError readObject(JsonVariant variant) {
|
||||
DeserializationError readObject(VariantRef variant) {
|
||||
TSize size;
|
||||
if (!readInteger(size)) return DeserializationError::IncompleteInput;
|
||||
return readObject(variant, size);
|
||||
}
|
||||
|
||||
DeserializationError readObject(JsonVariant variant, size_t n) {
|
||||
JsonObject object = variant.to<JsonObject>();
|
||||
DeserializationError readObject(VariantRef variant, size_t n) {
|
||||
ObjectRef object = variant.to<ObjectRef>();
|
||||
if (object.isNull()) return DeserializationError::NoMemory;
|
||||
|
||||
return readObject(object, n);
|
||||
}
|
||||
|
||||
DeserializationError readObject(JsonObject object, size_t n) {
|
||||
DeserializationError readObject(ObjectRef object, size_t n) {
|
||||
if (_nestingLimit == 0) return DeserializationError::TooDeep;
|
||||
--_nestingLimit;
|
||||
for (; n; --n) {
|
||||
@ -296,7 +296,7 @@ class MsgPackDeserializer {
|
||||
DeserializationError err = parseKey(key);
|
||||
if (err) return err;
|
||||
|
||||
JsonVariant value = object.set(key);
|
||||
VariantRef value = object.set(key);
|
||||
if (value.isInvalid()) return DeserializationError::NoMemory;
|
||||
|
||||
err = parse(value);
|
||||
|
@ -4,11 +4,11 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../JsonVariant.hpp"
|
||||
#include "../Polyfills/type_traits.hpp"
|
||||
#include "../Serialization/measure.hpp"
|
||||
#include "../Serialization/serialize.hpp"
|
||||
#include "./endianess.hpp"
|
||||
#include "../Variant/VariantRef.hpp"
|
||||
#include "endianess.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
@ -35,7 +35,7 @@ class MsgPackSerializer {
|
||||
}
|
||||
}
|
||||
|
||||
void visitArray(JsonArrayConst array) {
|
||||
void visitArray(ArrayConstRef array) {
|
||||
size_t n = array.size();
|
||||
if (n < 0x10) {
|
||||
writeByte(uint8_t(0x90 + array.size()));
|
||||
@ -46,12 +46,12 @@ class MsgPackSerializer {
|
||||
writeByte(0xDD);
|
||||
writeInteger(uint32_t(n));
|
||||
}
|
||||
for (JsonArrayConst::iterator it = array.begin(); it != array.end(); ++it) {
|
||||
for (ArrayConstRef::iterator it = array.begin(); it != array.end(); ++it) {
|
||||
it->accept(*this);
|
||||
}
|
||||
}
|
||||
|
||||
void visitObject(JsonObjectConst object) {
|
||||
void visitObject(ObjectConstRef object) {
|
||||
size_t n = object.size();
|
||||
if (n < 0x10) {
|
||||
writeByte(uint8_t(0x80 + n));
|
||||
@ -62,7 +62,7 @@ class MsgPackSerializer {
|
||||
writeByte(0xDF);
|
||||
writeInteger(uint32_t(n));
|
||||
}
|
||||
for (JsonObjectConst::iterator it = object.begin(); it != object.end();
|
||||
for (ObjectConstRef::iterator it = object.begin(); it != object.end();
|
||||
++it) {
|
||||
visitString(it->key());
|
||||
it->value().accept(*this);
|
||||
@ -93,8 +93,8 @@ class MsgPackSerializer {
|
||||
writeBytes(reinterpret_cast<const uint8_t*>(data), size);
|
||||
}
|
||||
|
||||
void visitNegativeInteger(JsonUInt value) {
|
||||
JsonUInt negated = JsonUInt(~value + 1);
|
||||
void visitNegativeInteger(UInt value) {
|
||||
UInt negated = UInt(~value + 1);
|
||||
if (value <= 0x20) {
|
||||
writeInteger(int8_t(negated));
|
||||
} else if (value <= 0x80) {
|
||||
@ -115,7 +115,7 @@ class MsgPackSerializer {
|
||||
#endif
|
||||
}
|
||||
|
||||
void visitPositiveInteger(JsonUInt value) {
|
||||
void visitPositiveInteger(UInt value) {
|
||||
if (value <= 0x7F) {
|
||||
writeInteger(uint8_t(value));
|
||||
} else if (value <= 0xFF) {
|
||||
|
@ -9,8 +9,8 @@
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
#if ARDUINOJSON_USE_DOUBLE
|
||||
typedef double JsonFloat;
|
||||
typedef double Float;
|
||||
#else
|
||||
typedef float JsonFloat;
|
||||
typedef float Float;
|
||||
#endif
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
@ -11,10 +11,10 @@
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
#if ARDUINOJSON_USE_LONG_LONG
|
||||
typedef int64_t JsonInteger;
|
||||
typedef uint64_t JsonUInt;
|
||||
typedef int64_t Integer;
|
||||
typedef uint64_t UInt;
|
||||
#else
|
||||
typedef long JsonInteger;
|
||||
typedef unsigned long JsonUInt;
|
||||
typedef long Integer;
|
||||
typedef unsigned long UInt;
|
||||
#endif
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
@ -6,9 +6,9 @@
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
class JsonKey {
|
||||
class Key {
|
||||
public:
|
||||
JsonKey(const VariantSlot* slot) : _slot(slot) {}
|
||||
Key(const VariantSlot* slot) : _slot(slot) {}
|
||||
|
||||
operator const char*() const {
|
||||
return c_str();
|
||||
@ -22,7 +22,7 @@ class JsonKey {
|
||||
return _slot == 0 || _slot->key == 0;
|
||||
}
|
||||
|
||||
friend bool operator==(JsonKey lhs, const char* rhs) {
|
||||
friend bool operator==(Key lhs, const char* rhs) {
|
||||
if (lhs.isNull()) return rhs == 0;
|
||||
return rhs ? !strcmp(lhs, rhs) : false;
|
||||
}
|
@ -5,13 +5,13 @@
|
||||
#pragma once
|
||||
|
||||
#include "../Memory/MemoryPool.hpp"
|
||||
#include "JsonVariantData.hpp"
|
||||
#include "SlotFunctions.hpp"
|
||||
#include "../Variant/SlotFunctions.hpp"
|
||||
#include "../Variant/VariantData.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
template <typename TKey>
|
||||
inline VariantSlot* objectFindSlot(const JsonObjectData* obj, TKey key) {
|
||||
inline VariantSlot* objectFindSlot(const ObjectData* obj, TKey key) {
|
||||
if (!obj) return 0;
|
||||
VariantSlot* slot = obj->head;
|
||||
while (slot) {
|
||||
@ -22,13 +22,12 @@ inline VariantSlot* objectFindSlot(const JsonObjectData* obj, TKey key) {
|
||||
}
|
||||
|
||||
template <typename TKey>
|
||||
inline bool objectContainsKey(const JsonObjectData* obj, const TKey& key) {
|
||||
inline bool objectContainsKey(const ObjectData* obj, const TKey& key) {
|
||||
return objectFindSlot(obj, key) != 0;
|
||||
}
|
||||
|
||||
template <typename TKey>
|
||||
inline JsonVariantData* objectAdd(JsonObjectData* obj, TKey key,
|
||||
MemoryPool* pool) {
|
||||
inline VariantData* objectAdd(ObjectData* obj, TKey key, MemoryPool* pool) {
|
||||
VariantSlot* slot = pool->allocVariant();
|
||||
if (!slot) return 0;
|
||||
|
||||
@ -49,8 +48,7 @@ inline JsonVariantData* objectAdd(JsonObjectData* obj, TKey key,
|
||||
}
|
||||
|
||||
template <typename TKey>
|
||||
inline JsonVariantData* objectSet(JsonObjectData* obj, TKey key,
|
||||
MemoryPool* pool) {
|
||||
inline VariantData* objectSet(ObjectData* obj, TKey key, MemoryPool* pool) {
|
||||
if (!obj) return 0;
|
||||
|
||||
// ignore null key
|
||||
@ -64,18 +62,18 @@ inline JsonVariantData* objectSet(JsonObjectData* obj, TKey key,
|
||||
}
|
||||
|
||||
template <typename TKey>
|
||||
inline JsonVariantData* objectGet(const JsonObjectData* obj, TKey key) {
|
||||
inline VariantData* objectGet(const ObjectData* obj, TKey key) {
|
||||
VariantSlot* slot = objectFindSlot(obj, key);
|
||||
return slot ? &slot->value : 0;
|
||||
}
|
||||
|
||||
inline void objectClear(JsonObjectData* obj) {
|
||||
inline void objectClear(ObjectData* obj) {
|
||||
if (!obj) return;
|
||||
obj->head = 0;
|
||||
obj->tail = 0;
|
||||
}
|
||||
|
||||
inline void objectRemove(JsonObjectData* obj, VariantSlot* slot) {
|
||||
inline void objectRemove(ObjectData* obj, VariantSlot* slot) {
|
||||
if (!obj) return;
|
||||
if (!slot) return;
|
||||
VariantSlot* prev = slot->getPrev();
|
||||
@ -90,19 +88,19 @@ inline void objectRemove(JsonObjectData* obj, VariantSlot* slot) {
|
||||
obj->tail = prev;
|
||||
}
|
||||
|
||||
inline size_t objectSize(const JsonObjectData* obj) {
|
||||
inline size_t objectSize(const ObjectData* obj) {
|
||||
if (!obj) return 0;
|
||||
return slotSize(obj->head);
|
||||
}
|
||||
|
||||
// bool variantCopy(JsonVariantData*, const JsonVariantData*, MemoryPool*);
|
||||
// bool variantCopy(VariantData*, const VariantData*, MemoryPool*);
|
||||
|
||||
inline bool objectCopy(JsonObjectData* dst, const JsonObjectData* src,
|
||||
inline bool objectCopy(ObjectData* dst, const ObjectData* src,
|
||||
MemoryPool* pool) {
|
||||
if (!dst || !src) return false;
|
||||
objectClear(dst);
|
||||
for (VariantSlot* s = src->head; s; s = s->getNext()) {
|
||||
JsonVariantData* var;
|
||||
VariantData* var;
|
||||
if (s->value.keyIsOwned)
|
||||
var = objectAdd(dst, ZeroTerminatedRamString(s->key), pool);
|
||||
else
|
||||
@ -112,13 +110,13 @@ inline bool objectCopy(JsonObjectData* dst, const JsonObjectData* src,
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool objectEquals(const JsonObjectData* o1, const JsonObjectData* o2) {
|
||||
inline bool objectEquals(const ObjectData* o1, const ObjectData* o2) {
|
||||
if (o1 == o2) return true;
|
||||
if (!o1 || !o2) return false;
|
||||
|
||||
for (VariantSlot* s = o1->head; s; s = s->getNext()) {
|
||||
JsonVariantData* v1 = &s->value;
|
||||
JsonVariantData* v2 = objectGet(o2, makeString(slotGetKey(s)));
|
||||
VariantData* v1 = &s->value;
|
||||
VariantData* v2 = objectGet(o2, makeString(slotGetKey(s)));
|
||||
if (!variantEquals(v1, v2)) return false;
|
||||
}
|
||||
return true;
|
21
src/ArduinoJson/Object/ObjectImpl.hpp
Normal file
21
src/ArduinoJson/Object/ObjectImpl.hpp
Normal file
@ -0,0 +1,21 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../Array/ArrayRef.hpp"
|
||||
#include "ObjectRef.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
template <typename TString>
|
||||
inline ArrayRef ObjectRef::createNestedArray(const TString& key) const {
|
||||
return set(key).template to<ArrayRef>();
|
||||
}
|
||||
|
||||
template <typename TString>
|
||||
inline ArrayRef ObjectRef::createNestedArray(TString* key) const {
|
||||
return set(key).template to<ArrayRef>();
|
||||
}
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
124
src/ArduinoJson/Object/ObjectIterator.hpp
Normal file
124
src/ArduinoJson/Object/ObjectIterator.hpp
Normal file
@ -0,0 +1,124 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../Variant/SlotFunctions.hpp"
|
||||
#include "Pair.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
class PairPtr {
|
||||
public:
|
||||
PairPtr(MemoryPool *memoryPool, VariantSlot *slot)
|
||||
: _pair(memoryPool, slot) {}
|
||||
|
||||
const Pair *operator->() const {
|
||||
return &_pair;
|
||||
}
|
||||
|
||||
const Pair &operator*() const {
|
||||
return _pair;
|
||||
}
|
||||
|
||||
private:
|
||||
Pair _pair;
|
||||
};
|
||||
|
||||
class ObjectIterator {
|
||||
public:
|
||||
ObjectIterator() : _slot(0) {}
|
||||
|
||||
explicit ObjectIterator(MemoryPool *memoryPool, VariantSlot *slot)
|
||||
: _memoryPool(memoryPool), _slot(slot) {}
|
||||
|
||||
Pair operator*() const {
|
||||
return Pair(_memoryPool, _slot);
|
||||
}
|
||||
PairPtr operator->() {
|
||||
return PairPtr(_memoryPool, _slot);
|
||||
}
|
||||
|
||||
bool operator==(const ObjectIterator &other) const {
|
||||
return _slot == other._slot;
|
||||
}
|
||||
|
||||
bool operator!=(const ObjectIterator &other) const {
|
||||
return _slot != other._slot;
|
||||
}
|
||||
|
||||
ObjectIterator &operator++() {
|
||||
_slot = _slot->getNext();
|
||||
return *this;
|
||||
}
|
||||
|
||||
ObjectIterator &operator+=(size_t distance) {
|
||||
_slot = _slot->getNext(distance);
|
||||
return *this;
|
||||
}
|
||||
|
||||
VariantSlot *internal() {
|
||||
return _slot;
|
||||
}
|
||||
|
||||
private:
|
||||
MemoryPool *_memoryPool;
|
||||
VariantSlot *_slot;
|
||||
};
|
||||
|
||||
class PairConstPtr {
|
||||
public:
|
||||
PairConstPtr(const VariantSlot *slot) : _pair(slot) {}
|
||||
|
||||
const PairConst *operator->() const {
|
||||
return &_pair;
|
||||
}
|
||||
|
||||
const PairConst &operator*() const {
|
||||
return _pair;
|
||||
}
|
||||
|
||||
private:
|
||||
PairConst _pair;
|
||||
};
|
||||
|
||||
class ObjectConstIterator {
|
||||
public:
|
||||
ObjectConstIterator() : _slot(0) {}
|
||||
|
||||
explicit ObjectConstIterator(const VariantSlot *slot) : _slot(slot) {}
|
||||
|
||||
PairConst operator*() const {
|
||||
return PairConst(_slot);
|
||||
}
|
||||
PairConstPtr operator->() {
|
||||
return PairConstPtr(_slot);
|
||||
}
|
||||
|
||||
bool operator==(const ObjectConstIterator &other) const {
|
||||
return _slot == other._slot;
|
||||
}
|
||||
|
||||
bool operator!=(const ObjectConstIterator &other) const {
|
||||
return _slot != other._slot;
|
||||
}
|
||||
|
||||
ObjectConstIterator &operator++() {
|
||||
_slot = _slot->getNext();
|
||||
return *this;
|
||||
}
|
||||
|
||||
ObjectConstIterator &operator+=(size_t distance) {
|
||||
_slot = _slot->getNext(distance);
|
||||
return *this;
|
||||
}
|
||||
|
||||
const VariantSlot *internal() {
|
||||
return _slot;
|
||||
}
|
||||
|
||||
private:
|
||||
const VariantSlot *_slot;
|
||||
};
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
@ -4,8 +4,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Data/ObjectFunctions.hpp"
|
||||
#include "JsonObjectIterator.hpp"
|
||||
#include "ObjectFunctions.hpp"
|
||||
#include "ObjectIterator.hpp"
|
||||
|
||||
// Returns the size (in bytes) of an object with n elements.
|
||||
// Can be very handy to determine the size of a StaticMemoryPool.
|
||||
@ -15,7 +15,7 @@
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
template <typename TData>
|
||||
class JsonObjectProxy {
|
||||
class ObjectRefBase {
|
||||
public:
|
||||
// Tells weither the specified key is present and associated with a value.
|
||||
//
|
||||
@ -42,20 +42,20 @@ class JsonObjectProxy {
|
||||
}
|
||||
|
||||
protected:
|
||||
JsonObjectProxy(TData* data) : _data(data) {}
|
||||
ObjectRefBase(TData* data) : _data(data) {}
|
||||
TData* _data;
|
||||
};
|
||||
|
||||
class JsonObjectConst : public JsonObjectProxy<const JsonObjectData>,
|
||||
public Visitable {
|
||||
friend class JsonObject;
|
||||
typedef JsonObjectProxy<const JsonObjectData> proxy_type;
|
||||
class ObjectConstRef : public ObjectRefBase<const ObjectData>,
|
||||
public Visitable {
|
||||
friend class ObjectRef;
|
||||
typedef ObjectRefBase<const ObjectData> base_type;
|
||||
|
||||
public:
|
||||
typedef JsonObjectConstIterator iterator;
|
||||
typedef ObjectConstIterator iterator;
|
||||
|
||||
JsonObjectConst() : proxy_type(0) {}
|
||||
JsonObjectConst(const JsonObjectData* data) : proxy_type(data) {}
|
||||
ObjectConstRef() : base_type(0) {}
|
||||
ObjectConstRef(const ObjectData* data) : base_type(data) {}
|
||||
|
||||
template <typename Visitor>
|
||||
FORCE_INLINE void accept(Visitor& visitor) const {
|
||||
@ -79,66 +79,65 @@ class JsonObjectConst : public JsonObjectProxy<const JsonObjectData>,
|
||||
// TValue get<TValue>(TKey) const;
|
||||
// TKey = const std::string&, const String&
|
||||
// TValue = bool, char, long, int, short, float, double,
|
||||
// std::string, String, JsonArrayConst, JsonObjectConst
|
||||
// std::string, String, ArrayConstRef, ObjectConstRef
|
||||
template <typename TKey>
|
||||
FORCE_INLINE JsonVariantConst get(const TKey& key) const {
|
||||
FORCE_INLINE VariantConstRef get(const TKey& key) const {
|
||||
return get_impl(makeString(key));
|
||||
}
|
||||
//
|
||||
// TValue get<TValue>(TKey) const;
|
||||
// TKey = char*, const char*, const FlashStringHelper*
|
||||
// TValue = bool, char, long, int, short, float, double,
|
||||
// std::string, String, JsonArrayConst, JsonObjectConst
|
||||
// std::string, String, ArrayConstRef, ObjectConstRef
|
||||
template <typename TKey>
|
||||
FORCE_INLINE JsonVariantConst get(TKey* key) const {
|
||||
FORCE_INLINE VariantConstRef get(TKey* key) const {
|
||||
return get_impl(makeString(key));
|
||||
}
|
||||
|
||||
//
|
||||
// JsonVariantConst operator[](TKey) const;
|
||||
// VariantConstRef operator[](TKey) const;
|
||||
// TKey = const std::string&, const String&
|
||||
template <typename TKey>
|
||||
FORCE_INLINE typename enable_if<IsString<TKey>::value, JsonVariantConst>::type
|
||||
FORCE_INLINE typename enable_if<IsString<TKey>::value, VariantConstRef>::type
|
||||
operator[](const TKey& key) const {
|
||||
return get_impl(makeString(key));
|
||||
}
|
||||
//
|
||||
// JsonVariantConst operator[](TKey) const;
|
||||
// VariantConstRef operator[](TKey) const;
|
||||
// TKey = const char*, const char[N], const FlashStringHelper*
|
||||
template <typename TKey>
|
||||
FORCE_INLINE
|
||||
typename enable_if<IsString<TKey*>::value, JsonVariantConst>::type
|
||||
operator[](TKey* key) const {
|
||||
FORCE_INLINE typename enable_if<IsString<TKey*>::value, VariantConstRef>::type
|
||||
operator[](TKey* key) const {
|
||||
return get_impl(makeString(key));
|
||||
}
|
||||
|
||||
FORCE_INLINE bool operator==(JsonObjectConst rhs) const {
|
||||
FORCE_INLINE bool operator==(ObjectConstRef rhs) const {
|
||||
return objectEquals(_data, rhs._data);
|
||||
}
|
||||
|
||||
private:
|
||||
template <typename TKey>
|
||||
FORCE_INLINE JsonVariantConst get_impl(TKey key) const {
|
||||
return JsonVariantConst(objectGet(_data, key));
|
||||
FORCE_INLINE VariantConstRef get_impl(TKey key) const {
|
||||
return VariantConstRef(objectGet(_data, key));
|
||||
}
|
||||
};
|
||||
|
||||
class JsonObject : public JsonObjectProxy<JsonObjectData>, public Visitable {
|
||||
typedef JsonObjectProxy<JsonObjectData> proxy_type;
|
||||
class ObjectRef : public ObjectRefBase<ObjectData>, public Visitable {
|
||||
typedef ObjectRefBase<ObjectData> base_type;
|
||||
|
||||
public:
|
||||
typedef JsonObjectIterator iterator;
|
||||
typedef ObjectIterator iterator;
|
||||
|
||||
FORCE_INLINE JsonObject() : proxy_type(0), _memoryPool(0) {}
|
||||
FORCE_INLINE JsonObject(MemoryPool* buf, JsonObjectData* data)
|
||||
: proxy_type(data), _memoryPool(buf) {}
|
||||
FORCE_INLINE ObjectRef() : base_type(0), _memoryPool(0) {}
|
||||
FORCE_INLINE ObjectRef(MemoryPool* buf, ObjectData* data)
|
||||
: base_type(data), _memoryPool(buf) {}
|
||||
|
||||
operator JsonVariant() const {
|
||||
return JsonVariant(_memoryPool, getVariantData(_data));
|
||||
operator VariantRef() const {
|
||||
return VariantRef(_memoryPool, getVariantData(_data));
|
||||
}
|
||||
|
||||
operator JsonObjectConst() const {
|
||||
return JsonObjectConst(_data);
|
||||
operator ObjectConstRef() const {
|
||||
return ObjectConstRef(_data);
|
||||
}
|
||||
|
||||
FORCE_INLINE iterator begin() const {
|
||||
@ -154,35 +153,35 @@ class JsonObject : public JsonObjectProxy<JsonObjectData>, public Visitable {
|
||||
objectClear(_data);
|
||||
}
|
||||
|
||||
FORCE_INLINE bool copyFrom(JsonObjectConst src) {
|
||||
FORCE_INLINE bool copyFrom(ObjectConstRef src) {
|
||||
return objectCopy(_data, src._data, _memoryPool);
|
||||
}
|
||||
|
||||
// Creates and adds a JsonArray.
|
||||
// Creates and adds a ArrayRef.
|
||||
//
|
||||
// JsonArray createNestedArray(TKey);
|
||||
// ArrayRef createNestedArray(TKey);
|
||||
// TKey = const std::string&, const String&
|
||||
template <typename TKey>
|
||||
FORCE_INLINE JsonArray createNestedArray(const TKey& key) const;
|
||||
// JsonArray createNestedArray(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 JsonArray createNestedArray(TKey* key) const;
|
||||
FORCE_INLINE ArrayRef createNestedArray(TKey* key) const;
|
||||
|
||||
// Creates and adds a JsonObject.
|
||||
// Creates and adds a ObjectRef.
|
||||
//
|
||||
// JsonObject createNestedObject(TKey);
|
||||
// ObjectRef createNestedObject(TKey);
|
||||
// TKey = const std::string&, const String&
|
||||
template <typename TKey>
|
||||
FORCE_INLINE JsonObject createNestedObject(const TKey& key) const {
|
||||
return set(key).template to<JsonObject>();
|
||||
FORCE_INLINE ObjectRef createNestedObject(const TKey& key) const {
|
||||
return set(key).template to<ObjectRef>();
|
||||
}
|
||||
//
|
||||
// JsonObject createNestedObject(TKey);
|
||||
// ObjectRef createNestedObject(TKey);
|
||||
// TKey = char*, const char*, char[], const char[], const FlashStringHelper*
|
||||
template <typename TKey>
|
||||
FORCE_INLINE JsonObject createNestedObject(TKey* key) const {
|
||||
return set(key).template to<JsonObject>();
|
||||
FORCE_INLINE ObjectRef createNestedObject(TKey* key) const {
|
||||
return set(key).template to<ObjectRef>();
|
||||
}
|
||||
|
||||
// Gets the value associated with the specified key.
|
||||
@ -190,39 +189,38 @@ class JsonObject : public JsonObjectProxy<JsonObjectData>, public Visitable {
|
||||
// TValue get<TValue>(TKey) const;
|
||||
// TKey = const std::string&, const String&
|
||||
// TValue = bool, char, long, int, short, float, double,
|
||||
// std::string, String, JsonArray, JsonObject
|
||||
// std::string, String, ArrayRef, ObjectRef
|
||||
template <typename TKey>
|
||||
FORCE_INLINE JsonVariant get(const TKey& key) const {
|
||||
FORCE_INLINE VariantRef get(const TKey& key) const {
|
||||
return get_impl(makeString(key));
|
||||
}
|
||||
//
|
||||
// TValue get<TValue>(TKey) const;
|
||||
// TKey = char*, const char*, const FlashStringHelper*
|
||||
// TValue = bool, char, long, int, short, float, double,
|
||||
// std::string, String, JsonArray, JsonObject
|
||||
// std::string, String, ArrayRef, ObjectRef
|
||||
template <typename TKey>
|
||||
FORCE_INLINE JsonVariant get(TKey* key) const {
|
||||
FORCE_INLINE VariantRef get(TKey* key) const {
|
||||
return get_impl(makeString(key));
|
||||
}
|
||||
|
||||
// Gets or sets the value associated with the specified key.
|
||||
//
|
||||
// JsonObjectSubscript operator[](TKey)
|
||||
// ObjectSubscript operator[](TKey)
|
||||
// TKey = const std::string&, const String&
|
||||
template <typename TKey>
|
||||
FORCE_INLINE JsonObjectSubscript<const TKey&> operator[](
|
||||
const TKey& key) const {
|
||||
return JsonObjectSubscript<const TKey&>(*this, key);
|
||||
FORCE_INLINE ObjectSubscript<const TKey&> operator[](const TKey& key) const {
|
||||
return ObjectSubscript<const TKey&>(*this, key);
|
||||
}
|
||||
//
|
||||
// JsonObjectSubscript operator[](TKey)
|
||||
// ObjectSubscript operator[](TKey)
|
||||
// TKey = char*, const char*, char[], const char[N], const FlashStringHelper*
|
||||
template <typename TKey>
|
||||
FORCE_INLINE JsonObjectSubscript<TKey*> operator[](TKey* key) const {
|
||||
return JsonObjectSubscript<TKey*>(*this, key);
|
||||
FORCE_INLINE ObjectSubscript<TKey*> operator[](TKey* key) const {
|
||||
return ObjectSubscript<TKey*>(*this, key);
|
||||
}
|
||||
|
||||
FORCE_INLINE bool operator==(JsonObject rhs) const {
|
||||
FORCE_INLINE bool operator==(ObjectRef rhs) const {
|
||||
return objectEquals(_data, rhs._data);
|
||||
}
|
||||
|
||||
@ -247,37 +245,37 @@ class JsonObject : public JsonObjectProxy<JsonObjectData>, public Visitable {
|
||||
}
|
||||
|
||||
template <typename TKey>
|
||||
FORCE_INLINE JsonVariant set(TKey* key) const {
|
||||
FORCE_INLINE VariantRef set(TKey* key) const {
|
||||
return set_impl(makeString(key));
|
||||
}
|
||||
|
||||
template <typename TKey>
|
||||
FORCE_INLINE JsonVariant set(const TKey& key) const {
|
||||
FORCE_INLINE VariantRef set(const TKey& key) const {
|
||||
return set_impl(makeString(key));
|
||||
}
|
||||
|
||||
FORCE_INLINE JsonVariant set(StringInMemoryPool key) const {
|
||||
FORCE_INLINE VariantRef set(StringInMemoryPool key) const {
|
||||
return set_impl(key);
|
||||
}
|
||||
|
||||
FORCE_INLINE JsonVariant set(ZeroTerminatedRamStringConst key) const {
|
||||
FORCE_INLINE VariantRef set(ZeroTerminatedRamStringConst key) const {
|
||||
return set_impl(key);
|
||||
}
|
||||
|
||||
template <typename Visitor>
|
||||
FORCE_INLINE void accept(Visitor& visitor) const {
|
||||
JsonObjectConst(_data).accept(visitor);
|
||||
ObjectConstRef(_data).accept(visitor);
|
||||
}
|
||||
|
||||
private:
|
||||
template <typename TStringRef>
|
||||
FORCE_INLINE JsonVariant get_impl(TStringRef key) const {
|
||||
return JsonVariant(_memoryPool, objectGet(_data, key));
|
||||
FORCE_INLINE VariantRef get_impl(TStringRef key) const {
|
||||
return VariantRef(_memoryPool, objectGet(_data, key));
|
||||
}
|
||||
|
||||
template <typename TKey>
|
||||
FORCE_INLINE JsonVariant set_impl(TKey key) const {
|
||||
return JsonVariant(_memoryPool, objectSet(_data, key, _memoryPool));
|
||||
FORCE_INLINE VariantRef set_impl(TKey key) const {
|
||||
return VariantRef(_memoryPool, objectSet(_data, key, _memoryPool));
|
||||
}
|
||||
|
||||
template <typename TStringRef>
|
@ -4,9 +4,9 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Configuration.hpp"
|
||||
#include "JsonVariantBase.hpp"
|
||||
#include "Polyfills/type_traits.hpp"
|
||||
#include "../Configuration.hpp"
|
||||
#include "../Operators/VariantOperators.hpp"
|
||||
#include "../Polyfills/type_traits.hpp"
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(push)
|
||||
@ -16,16 +16,15 @@
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
template <typename TStringRef>
|
||||
class JsonObjectSubscript
|
||||
: public JsonVariantBase<JsonObjectSubscript<TStringRef> >,
|
||||
public Visitable {
|
||||
typedef JsonObjectSubscript<TStringRef> this_type;
|
||||
class ObjectSubscript : public VariantOperators<ObjectSubscript<TStringRef> >,
|
||||
public Visitable {
|
||||
typedef ObjectSubscript<TStringRef> this_type;
|
||||
|
||||
public:
|
||||
FORCE_INLINE JsonObjectSubscript(JsonObject object, TStringRef key)
|
||||
FORCE_INLINE ObjectSubscript(ObjectRef object, TStringRef key)
|
||||
: _object(object), _key(key) {}
|
||||
|
||||
operator JsonVariantConst() const {
|
||||
operator VariantConstRef() const {
|
||||
return get_impl();
|
||||
}
|
||||
|
||||
@ -38,7 +37,7 @@ class JsonObjectSubscript
|
||||
//
|
||||
// operator=(const TValue&);
|
||||
// TValue = bool, char, long, int, short, float, double,
|
||||
// std::string, String, JsonArray, JsonObject
|
||||
// std::string, String, ArrayRef, ObjectRef
|
||||
template <typename TValue>
|
||||
FORCE_INLINE typename enable_if<!is_array<TValue>::value, this_type &>::type
|
||||
operator=(const TValue &src) {
|
||||
@ -59,7 +58,7 @@ class JsonObjectSubscript
|
||||
}
|
||||
|
||||
template <typename TValue>
|
||||
FORCE_INLINE typename JsonVariantAs<TValue>::type as() const {
|
||||
FORCE_INLINE typename VariantAs<TValue>::type as() const {
|
||||
return get_impl().template as<TValue>();
|
||||
}
|
||||
|
||||
@ -69,7 +68,7 @@ class JsonObjectSubscript
|
||||
}
|
||||
|
||||
template <typename TValue>
|
||||
FORCE_INLINE typename JsonVariantTo<TValue>::type to() {
|
||||
FORCE_INLINE typename VariantTo<TValue>::type to() {
|
||||
return set_impl().template to<TValue>();
|
||||
}
|
||||
|
||||
@ -77,8 +76,8 @@ class JsonObjectSubscript
|
||||
//
|
||||
// bool set(const TValue&);
|
||||
// TValue = bool, char, long, int, short, float, double, serialized,
|
||||
// JsonVariant,
|
||||
// std::string, String, JsonArray, JsonObject
|
||||
// VariantRef,
|
||||
// std::string, String, ArrayRef, ObjectRef
|
||||
template <typename TValue>
|
||||
FORCE_INLINE typename enable_if<!is_array<TValue>::value, bool>::type set(
|
||||
const TValue &value) {
|
||||
@ -98,32 +97,32 @@ class JsonObjectSubscript
|
||||
}
|
||||
|
||||
private:
|
||||
FORCE_INLINE JsonVariant get_impl() const {
|
||||
FORCE_INLINE VariantRef get_impl() const {
|
||||
return _object.get(_key);
|
||||
}
|
||||
|
||||
FORCE_INLINE JsonVariant set_impl() const {
|
||||
FORCE_INLINE VariantRef set_impl() const {
|
||||
return _object.set(_key);
|
||||
}
|
||||
|
||||
JsonObject _object;
|
||||
ObjectRef _object;
|
||||
TStringRef _key;
|
||||
};
|
||||
|
||||
template <typename TImpl>
|
||||
template <typename TString>
|
||||
inline typename enable_if<IsString<TString>::value,
|
||||
JsonObjectSubscript<const TString &> >::type
|
||||
JsonVariantSubscripts<TImpl>::operator[](const TString &key) const {
|
||||
return impl()->template as<JsonObject>()[key];
|
||||
ObjectSubscript<const TString &> >::type
|
||||
VariantSubscripts<TImpl>::operator[](const TString &key) const {
|
||||
return impl()->template as<ObjectRef>()[key];
|
||||
}
|
||||
|
||||
template <typename TImpl>
|
||||
template <typename TString>
|
||||
inline typename enable_if<IsString<TString *>::value,
|
||||
JsonObjectSubscript<TString *> >::type
|
||||
JsonVariantSubscripts<TImpl>::operator[](TString *key) const {
|
||||
return impl()->template as<JsonObject>()[key];
|
||||
ObjectSubscript<TString *> >::type
|
||||
VariantSubscripts<TImpl>::operator[](TString *key) const {
|
||||
return impl()->template as<ObjectRef>()[key];
|
||||
}
|
||||
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
53
src/ArduinoJson/Object/Pair.hpp
Normal file
53
src/ArduinoJson/Object/Pair.hpp
Normal file
@ -0,0 +1,53 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../Variant/VariantRef.hpp"
|
||||
#include "Key.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
// A key value pair for ObjectData.
|
||||
class Pair {
|
||||
public:
|
||||
Pair(MemoryPool* memoryPool, VariantSlot* slot) : _key(slot) {
|
||||
if (slot) {
|
||||
_value = VariantRef(memoryPool, &slot->value);
|
||||
}
|
||||
}
|
||||
|
||||
Key key() const {
|
||||
return _key;
|
||||
}
|
||||
|
||||
VariantRef value() const {
|
||||
return _value;
|
||||
}
|
||||
|
||||
private:
|
||||
Key _key;
|
||||
VariantRef _value;
|
||||
};
|
||||
|
||||
class PairConst {
|
||||
public:
|
||||
PairConst(const VariantSlot* slot) : _key(slot) {
|
||||
if (slot) {
|
||||
_value = VariantConstRef(&slot->value);
|
||||
}
|
||||
}
|
||||
|
||||
Key key() const {
|
||||
return _key;
|
||||
}
|
||||
|
||||
VariantConstRef value() const {
|
||||
return _value;
|
||||
}
|
||||
|
||||
private:
|
||||
Key _key;
|
||||
VariantConstRef _value;
|
||||
};
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
@ -4,13 +4,12 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Data/JsonVariantAs.hpp"
|
||||
#include "Polyfills/attributes.hpp"
|
||||
#include "../Polyfills/attributes.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
template <typename TImpl>
|
||||
class JsonVariantCasts {
|
||||
class VariantCasts {
|
||||
public:
|
||||
template <typename T>
|
||||
FORCE_INLINE operator T() const {
|
@ -4,7 +4,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "JsonVariant.hpp"
|
||||
#include "../Variant/VariantRef.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
template <typename T>
|
||||
@ -15,7 +15,7 @@ struct is_simple_value {
|
||||
};
|
||||
|
||||
template <typename TVariant>
|
||||
class JsonVariantComparisons {
|
||||
class VariantComparisons {
|
||||
public:
|
||||
// const char* == TVariant
|
||||
template <typename T>
|
19
src/ArduinoJson/Operators/VariantOperators.hpp
Normal file
19
src/ArduinoJson/Operators/VariantOperators.hpp
Normal file
@ -0,0 +1,19 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "VariantCasts.hpp"
|
||||
#include "VariantComparisons.hpp"
|
||||
#include "VariantOr.hpp"
|
||||
#include "VariantSubscripts.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
template <typename TImpl>
|
||||
class VariantOperators : public VariantCasts<TImpl>,
|
||||
public VariantComparisons<TImpl>,
|
||||
public VariantOr<TImpl>,
|
||||
public VariantSubscripts<TImpl> {};
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
@ -4,16 +4,16 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Data/JsonVariantAs.hpp"
|
||||
#include "Polyfills/attributes.hpp"
|
||||
#include "Polyfills/type_traits.hpp"
|
||||
#include "../Polyfills/attributes.hpp"
|
||||
#include "../Polyfills/type_traits.hpp"
|
||||
#include "../Variant/VariantAs.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
template <typename TImpl>
|
||||
class JsonVariantOr {
|
||||
class VariantOr {
|
||||
public:
|
||||
// Returns the default value if the JsonVariant is undefined of incompatible
|
||||
// Returns the default value if the VariantRef is undefined of incompatible
|
||||
template <typename T>
|
||||
typename enable_if<!is_integral<T>::value, T>::type operator|(
|
||||
const T &defaultValue) const {
|
||||
@ -23,14 +23,14 @@ class JsonVariantOr {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
// Returns the default value if the JsonVariant is undefined of incompatible
|
||||
// Returns the default value if the VariantRef is undefined of incompatible
|
||||
// Special case for string: null is treated as undefined
|
||||
const char *operator|(const char *defaultValue) const {
|
||||
const char *value = impl()->template as<const char *>();
|
||||
return value ? value : defaultValue;
|
||||
}
|
||||
|
||||
// Returns the default value if the JsonVariant is undefined of incompatible
|
||||
// Returns the default value if the VariantRef is undefined of incompatible
|
||||
// Special case for integers: we also accept double
|
||||
template <typename Integer>
|
||||
typename enable_if<is_integral<Integer>::value, Integer>::type operator|(
|
@ -4,43 +4,43 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Data/JsonVariantAs.hpp"
|
||||
#include "Polyfills/attributes.hpp"
|
||||
#include "Polyfills/type_traits.hpp"
|
||||
#include "Strings/StringTypes.hpp"
|
||||
#include "../Polyfills/attributes.hpp"
|
||||
#include "../Polyfills/type_traits.hpp"
|
||||
#include "../Strings/StringTypes.hpp"
|
||||
#include "../Variant/VariantAs.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
class JsonArray;
|
||||
class JsonObject;
|
||||
class ArrayRef;
|
||||
class ObjectRef;
|
||||
|
||||
// Forward declarations.
|
||||
class JsonArraySubscript;
|
||||
class ArraySubscript;
|
||||
template <typename TKey>
|
||||
class JsonObjectSubscript;
|
||||
class ObjectSubscript;
|
||||
|
||||
template <typename TImpl>
|
||||
class JsonVariantSubscripts {
|
||||
class VariantSubscripts {
|
||||
public:
|
||||
// Mimics an array.
|
||||
// Returns the element at specified index if the variant is an array.
|
||||
FORCE_INLINE JsonArraySubscript operator[](size_t index) const;
|
||||
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.
|
||||
//
|
||||
// JsonObjectSubscript operator[](TKey) const;
|
||||
// ObjectSubscript operator[](TKey) const;
|
||||
// TKey = const std::string&, const String&
|
||||
template <typename TString>
|
||||
FORCE_INLINE typename enable_if<IsString<TString>::value,
|
||||
JsonObjectSubscript<const TString &> >::type
|
||||
ObjectSubscript<const TString &> >::type
|
||||
operator[](const TString &key) const;
|
||||
//
|
||||
// JsonObjectSubscript operator[](TKey) const;
|
||||
// ObjectSubscript operator[](TKey) const;
|
||||
// TKey = const char*, const char[N], const FlashStringHelper*
|
||||
template <typename TString>
|
||||
FORCE_INLINE typename enable_if<IsString<TString *>::value,
|
||||
JsonObjectSubscript<TString *> >::type
|
||||
ObjectSubscript<TString *> >::type
|
||||
operator[](TString *key) const;
|
||||
|
||||
private:
|
@ -7,7 +7,7 @@
|
||||
#include "../Memory/MemoryPool.hpp"
|
||||
#include "../Polyfills/assert.hpp"
|
||||
#include "../Strings/StringTypes.hpp"
|
||||
#include "JsonVariantData.hpp"
|
||||
#include "VariantData.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
89
src/ArduinoJson/Variant/VariantAs.hpp
Normal file
89
src/ArduinoJson/Variant/VariantAs.hpp
Normal file
@ -0,0 +1,89 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../Serialization/DynamicStringWriter.hpp"
|
||||
#include "VariantFunctions.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
class ArrayRef;
|
||||
class ArrayConstRef;
|
||||
class ObjectRef;
|
||||
class ObjectConstRef;
|
||||
class VariantRef;
|
||||
class VariantConstRef;
|
||||
|
||||
// A metafunction that returns the type of the value returned by
|
||||
// VariantRef::as<T>()
|
||||
template <typename T>
|
||||
struct VariantAs {
|
||||
typedef T type;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct VariantAs<char*> {
|
||||
typedef const char* type;
|
||||
};
|
||||
|
||||
// A metafunction that returns the type of the value returned by
|
||||
// VariantRef::as<T>()
|
||||
template <typename T>
|
||||
struct VariantConstAs {
|
||||
typedef typename VariantAs<T>::type type;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct VariantConstAs<VariantRef> {
|
||||
typedef VariantConstRef type;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct VariantConstAs<ObjectRef> {
|
||||
typedef ObjectConstRef type;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct VariantConstAs<ArrayRef> {
|
||||
typedef ArrayConstRef type;
|
||||
};
|
||||
|
||||
// ---
|
||||
|
||||
template <typename T>
|
||||
inline typename enable_if<is_integral<T>::value, T>::type variantAs(
|
||||
const VariantData* _data) {
|
||||
return variantAsIntegral<T>(_data);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline typename enable_if<is_same<T, bool>::value, T>::type variantAs(
|
||||
const VariantData* _data) {
|
||||
return variantAsBoolean(_data);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline typename enable_if<is_floating_point<T>::value, T>::type variantAs(
|
||||
const VariantData* _data) {
|
||||
return variantAsFloat<T>(_data);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline typename enable_if<is_same<T, const char*>::value ||
|
||||
is_same<T, char*>::value,
|
||||
const char*>::type
|
||||
variantAs(const VariantData* _data) {
|
||||
return variantAsString(_data);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline typename enable_if<is_same<VariantConstRef, T>::value, T>::type
|
||||
variantAs(const VariantData* _data);
|
||||
|
||||
template <typename T>
|
||||
inline typename enable_if<IsWriteableString<T>::value, T>::type variantAs(
|
||||
const VariantData* _data);
|
||||
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
@ -4,25 +4,25 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../JsonVariant.hpp"
|
||||
#include "../Serialization/DynamicStringWriter.hpp"
|
||||
#include "VariantFunctions.hpp"
|
||||
#include "VariantRef.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
template <typename T>
|
||||
inline typename enable_if<is_same<JsonVariantConst, T>::value, T>::type
|
||||
variantAs(const JsonVariantData* _data) {
|
||||
return JsonVariantConst(_data);
|
||||
inline typename enable_if<is_same<VariantConstRef, T>::value, T>::type
|
||||
variantAs(const VariantData* _data) {
|
||||
return VariantConstRef(_data);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline typename enable_if<IsWriteableString<T>::value, T>::type variantAs(
|
||||
const JsonVariantData* _data) {
|
||||
const VariantData* _data) {
|
||||
const char* cstr = variantAsString(_data);
|
||||
if (cstr) return T(cstr);
|
||||
T s;
|
||||
serializeJson(JsonVariantConst(_data), s);
|
||||
serializeJson(VariantConstRef(_data), s);
|
||||
return s;
|
||||
}
|
||||
|
79
src/ArduinoJson/Variant/VariantData.hpp
Normal file
79
src/ArduinoJson/Variant/VariantData.hpp
Normal file
@ -0,0 +1,79 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h> // ptrdiff_t, size_t
|
||||
|
||||
#include "../Numbers/Float.hpp"
|
||||
#include "../Numbers/Integer.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
enum VariantType {
|
||||
JSON_NULL,
|
||||
JSON_LINKED_RAW,
|
||||
JSON_OWNED_RAW,
|
||||
JSON_LINKED_STRING,
|
||||
JSON_OWNED_STRING,
|
||||
JSON_BOOLEAN,
|
||||
JSON_POSITIVE_INTEGER,
|
||||
JSON_NEGATIVE_INTEGER,
|
||||
JSON_ARRAY,
|
||||
JSON_OBJECT,
|
||||
JSON_FLOAT
|
||||
};
|
||||
|
||||
struct ObjectData {
|
||||
struct VariantSlot *head;
|
||||
struct VariantSlot *tail;
|
||||
};
|
||||
|
||||
struct ArrayData {
|
||||
struct VariantSlot *head;
|
||||
struct VariantSlot *tail;
|
||||
};
|
||||
|
||||
struct RawData {
|
||||
const char *data;
|
||||
size_t size;
|
||||
};
|
||||
|
||||
// A union that defines the actual content of a VariantData.
|
||||
// The enum VariantType determines which member is in use.
|
||||
union VariantContent {
|
||||
Float asFloat;
|
||||
UInt asInteger;
|
||||
ArrayData asArray;
|
||||
ObjectData asObject;
|
||||
const char *asString;
|
||||
struct {
|
||||
const char *data;
|
||||
size_t size;
|
||||
} asRaw;
|
||||
};
|
||||
|
||||
// this struct must be a POD type to prevent error calling offsetof on clang
|
||||
struct VariantData {
|
||||
bool keyIsOwned : 1;
|
||||
VariantType type : 7;
|
||||
VariantContent content;
|
||||
};
|
||||
|
||||
inline VariantData *getVariantData(ArrayData *arr) {
|
||||
const ptrdiff_t offset =
|
||||
offsetof(VariantData, content) - offsetof(VariantContent, asArray);
|
||||
if (!arr) return 0;
|
||||
return reinterpret_cast<VariantData *>(reinterpret_cast<char *>(arr) -
|
||||
offset);
|
||||
}
|
||||
|
||||
inline VariantData *getVariantData(ObjectData *obj) {
|
||||
const ptrdiff_t offset =
|
||||
offsetof(VariantData, content) - offsetof(VariantContent, asObject);
|
||||
if (!obj) return 0;
|
||||
return reinterpret_cast<VariantData *>(reinterpret_cast<char *>(obj) -
|
||||
offset);
|
||||
}
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
@ -4,16 +4,16 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../Array/ArrayFunctions.hpp"
|
||||
#include "../Misc/SerializedValue.hpp"
|
||||
#include "../Numbers/parseFloat.hpp"
|
||||
#include "../Numbers/parseInteger.hpp"
|
||||
#include "../SerializedValue.hpp"
|
||||
#include "ArrayFunctions.hpp"
|
||||
#include "JsonVariantData.hpp"
|
||||
#include "ObjectFunctions.hpp"
|
||||
#include "../Object/ObjectFunctions.hpp"
|
||||
#include "VariantData.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
template <typename T>
|
||||
inline T variantAsIntegral(const JsonVariantData* var) {
|
||||
inline T variantAsIntegral(const VariantData* var) {
|
||||
if (!var) return 0;
|
||||
switch (var->type) {
|
||||
case JSON_POSITIVE_INTEGER:
|
||||
@ -31,13 +31,13 @@ inline T variantAsIntegral(const JsonVariantData* var) {
|
||||
}
|
||||
}
|
||||
|
||||
inline bool variantAsBoolean(const JsonVariantData* var) {
|
||||
inline bool variantAsBoolean(const VariantData* var) {
|
||||
return variantAsIntegral<int>(var) != 0;
|
||||
}
|
||||
|
||||
// T = float/double
|
||||
template <typename T>
|
||||
inline T variantAsFloat(const JsonVariantData* var) {
|
||||
inline T variantAsFloat(const VariantData* var) {
|
||||
if (!var) return 0;
|
||||
switch (var->type) {
|
||||
case JSON_POSITIVE_INTEGER:
|
||||
@ -55,7 +55,7 @@ inline T variantAsFloat(const JsonVariantData* var) {
|
||||
}
|
||||
}
|
||||
|
||||
inline const char* variantAsString(const JsonVariantData* var) {
|
||||
inline const char* variantAsString(const VariantData* var) {
|
||||
if (!var) return 0;
|
||||
switch (var->type) {
|
||||
case JSON_LINKED_STRING:
|
||||
@ -66,42 +66,42 @@ inline const char* variantAsString(const JsonVariantData* var) {
|
||||
}
|
||||
}
|
||||
|
||||
inline JsonArrayData* variantAsArray(JsonVariantData* var) {
|
||||
inline ArrayData* variantAsArray(VariantData* var) {
|
||||
if (var && var->type == JSON_ARRAY)
|
||||
return &var->content.asArray;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline const JsonArrayData* variantAsArray(const JsonVariantData* var) {
|
||||
inline const ArrayData* variantAsArray(const VariantData* var) {
|
||||
if (var && var->type == JSON_ARRAY)
|
||||
return &var->content.asArray;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline JsonObjectData* variantAsObject(JsonVariantData* var) {
|
||||
inline ObjectData* variantAsObject(VariantData* var) {
|
||||
if (var && var->type == JSON_OBJECT)
|
||||
return &var->content.asObject;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline const JsonObjectData* variantAsObject(const JsonVariantData* var) {
|
||||
inline const ObjectData* variantAsObject(const VariantData* var) {
|
||||
if (var && var->type == JSON_OBJECT)
|
||||
return &var->content.asObject;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline bool variantSetBoolean(JsonVariantData* var, bool value) {
|
||||
inline bool variantSetBoolean(VariantData* var, bool value) {
|
||||
if (!var) return false;
|
||||
var->type = JSON_BOOLEAN;
|
||||
var->content.asInteger = static_cast<JsonUInt>(value);
|
||||
var->content.asInteger = static_cast<UInt>(value);
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool variantSetFloat(JsonVariantData* var, JsonFloat value) {
|
||||
inline bool variantSetFloat(VariantData* var, Float value) {
|
||||
if (!var) return false;
|
||||
var->type = JSON_FLOAT;
|
||||
var->content.asFloat = value;
|
||||
@ -109,26 +109,26 @@ inline bool variantSetFloat(JsonVariantData* var, JsonFloat value) {
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline bool variantSetSignedInteger(JsonVariantData* var, T value) {
|
||||
inline bool variantSetSignedInteger(VariantData* var, T value) {
|
||||
if (!var) return false;
|
||||
if (value >= 0) {
|
||||
var->type = JSON_POSITIVE_INTEGER;
|
||||
var->content.asInteger = static_cast<JsonUInt>(value);
|
||||
var->content.asInteger = static_cast<UInt>(value);
|
||||
} else {
|
||||
var->type = JSON_NEGATIVE_INTEGER;
|
||||
var->content.asInteger = ~static_cast<JsonUInt>(value) + 1;
|
||||
var->content.asInteger = ~static_cast<UInt>(value) + 1;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool variantSetUnsignedInteger(JsonVariantData* var, JsonUInt value) {
|
||||
inline bool variantSetUnsignedInteger(VariantData* var, UInt value) {
|
||||
if (!var) return false;
|
||||
var->type = JSON_POSITIVE_INTEGER;
|
||||
var->content.asInteger = static_cast<JsonUInt>(value);
|
||||
var->content.asInteger = static_cast<UInt>(value);
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool variantSetLinkedRaw(JsonVariantData* var,
|
||||
inline bool variantSetLinkedRaw(VariantData* var,
|
||||
SerializedValue<const char*> value) {
|
||||
if (!var) return false;
|
||||
var->type = JSON_LINKED_RAW;
|
||||
@ -138,7 +138,7 @@ inline bool variantSetLinkedRaw(JsonVariantData* var,
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline bool variantSetOwnedRaw(JsonVariantData* var, SerializedValue<T> value,
|
||||
inline bool variantSetOwnedRaw(VariantData* var, SerializedValue<T> value,
|
||||
MemoryPool* pool) {
|
||||
if (!var) return false;
|
||||
char* dup = makeString(value.data(), value.size()).save(pool);
|
||||
@ -154,7 +154,7 @@ inline bool variantSetOwnedRaw(JsonVariantData* var, SerializedValue<T> value,
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline bool variantSetString(JsonVariantData* var, T value, MemoryPool* pool) {
|
||||
inline bool variantSetString(VariantData* var, T value, MemoryPool* pool) {
|
||||
if (!var) return false;
|
||||
char* dup = value.save(pool);
|
||||
if (dup) {
|
||||
@ -167,26 +167,26 @@ inline bool variantSetString(JsonVariantData* var, T value, MemoryPool* pool) {
|
||||
}
|
||||
}
|
||||
|
||||
inline bool variantSetOwnedString(JsonVariantData* var, char* s) {
|
||||
inline bool variantSetOwnedString(VariantData* var, char* s) {
|
||||
if (!var) return false;
|
||||
var->type = JSON_OWNED_STRING;
|
||||
var->content.asString = s;
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool variantSetString(JsonVariantData* var, const char* value) {
|
||||
inline bool variantSetString(VariantData* var, const char* value) {
|
||||
if (!var) return false;
|
||||
var->type = JSON_LINKED_STRING;
|
||||
var->content.asString = value;
|
||||
return true;
|
||||
}
|
||||
|
||||
inline void variantSetNull(JsonVariantData* var) {
|
||||
inline void variantSetNull(VariantData* var) {
|
||||
if (!var) return;
|
||||
var->type = JSON_NULL;
|
||||
}
|
||||
|
||||
inline JsonArrayData* variantToArray(JsonVariantData* var) {
|
||||
inline ArrayData* variantToArray(VariantData* var) {
|
||||
if (!var) return 0;
|
||||
var->type = JSON_ARRAY;
|
||||
var->content.asArray.head = 0;
|
||||
@ -194,7 +194,7 @@ inline JsonArrayData* variantToArray(JsonVariantData* var) {
|
||||
return &var->content.asArray;
|
||||
}
|
||||
|
||||
inline JsonObjectData* variantToObject(JsonVariantData* var) {
|
||||
inline ObjectData* variantToObject(VariantData* var) {
|
||||
if (!var) return 0;
|
||||
var->type = JSON_OBJECT;
|
||||
var->content.asObject.head = 0;
|
||||
@ -202,7 +202,7 @@ inline JsonObjectData* variantToObject(JsonVariantData* var) {
|
||||
return &var->content.asObject;
|
||||
}
|
||||
|
||||
inline bool variantCopy(JsonVariantData* dst, const JsonVariantData* src,
|
||||
inline bool variantCopy(VariantData* dst, const VariantData* src,
|
||||
MemoryPool* pool) {
|
||||
if (!dst) return false;
|
||||
if (!src) {
|
||||
@ -229,35 +229,35 @@ inline bool variantCopy(JsonVariantData* dst, const JsonVariantData* src,
|
||||
}
|
||||
}
|
||||
|
||||
inline bool variantIsInteger(const JsonVariantData* var) {
|
||||
inline bool variantIsInteger(const VariantData* var) {
|
||||
return var && (var->type == JSON_POSITIVE_INTEGER ||
|
||||
var->type == JSON_NEGATIVE_INTEGER);
|
||||
}
|
||||
|
||||
inline bool variantIsFloat(const JsonVariantData* var) {
|
||||
inline bool variantIsFloat(const VariantData* var) {
|
||||
return var &&
|
||||
(var->type == JSON_FLOAT || var->type == JSON_POSITIVE_INTEGER ||
|
||||
var->type == JSON_NEGATIVE_INTEGER);
|
||||
}
|
||||
|
||||
inline bool variantIsString(const JsonVariantData* var) {
|
||||
inline bool variantIsString(const VariantData* var) {
|
||||
return var &&
|
||||
(var->type == JSON_LINKED_STRING || var->type == JSON_OWNED_STRING);
|
||||
}
|
||||
|
||||
inline bool variantIsArray(const JsonVariantData* var) {
|
||||
inline bool variantIsArray(const VariantData* var) {
|
||||
return var && var->type == JSON_ARRAY;
|
||||
}
|
||||
|
||||
inline bool variantIsObject(const JsonVariantData* var) {
|
||||
inline bool variantIsObject(const VariantData* var) {
|
||||
return var && var->type == JSON_OBJECT;
|
||||
}
|
||||
|
||||
inline bool variantIsNull(const JsonVariantData* var) {
|
||||
inline bool variantIsNull(const VariantData* var) {
|
||||
return var == 0 || var->type == JSON_NULL;
|
||||
}
|
||||
|
||||
inline bool variantEquals(const JsonVariantData* a, const JsonVariantData* b) {
|
||||
inline bool variantEquals(const VariantData* a, const VariantData* b) {
|
||||
if (a == b) return true;
|
||||
if (!a || !b) return false;
|
||||
if (a->type != b->type) return false;
|
118
src/ArduinoJson/Variant/VariantImpl.hpp
Normal file
118
src/ArduinoJson/Variant/VariantImpl.hpp
Normal file
@ -0,0 +1,118 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../Configuration.hpp"
|
||||
#include "../Numbers/parseFloat.hpp"
|
||||
#include "../Numbers/parseInteger.hpp"
|
||||
#include "VariantRef.hpp"
|
||||
|
||||
#include <string.h> // for strcmp
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
inline bool VariantRef::set(ArrayRef 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);
|
||||
}
|
||||
|
||||
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 variantCopy(_data, value._data, _memoryPool);
|
||||
}
|
||||
|
||||
inline bool VariantRef::set(VariantRef value) const {
|
||||
return variantCopy(_data, value._data, _memoryPool);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline typename enable_if<is_same<T, ArrayRef>::value, T>::type VariantRef::as()
|
||||
const {
|
||||
return ArrayRef(_memoryPool, variantAsArray(_data));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline typename enable_if<is_same<T, ObjectRef>::value, T>::type
|
||||
VariantRef::as() const {
|
||||
return ObjectRef(_memoryPool, variantAsObject(_data));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline typename enable_if<is_same<T, ArrayRef>::value, ArrayRef>::type
|
||||
VariantRef::to() const {
|
||||
return ArrayRef(_memoryPool, variantToArray(_data));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename enable_if<is_same<T, ObjectRef>::value, ObjectRef>::type
|
||||
VariantRef::to() const {
|
||||
return ObjectRef(_memoryPool, variantToObject(_data));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename enable_if<is_same<T, VariantRef>::value, VariantRef>::type
|
||||
VariantRef::to() const {
|
||||
variantSetNull(_data);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename Visitor>
|
||||
inline void VariantRef::accept(Visitor& visitor) const {
|
||||
return VariantConstRef(_data).accept(visitor);
|
||||
}
|
||||
|
||||
template <typename Visitor>
|
||||
inline void VariantConstRef::accept(Visitor& visitor) const {
|
||||
if (!_data) return visitor.visitNull();
|
||||
|
||||
switch (_data->type) {
|
||||
case JSON_FLOAT:
|
||||
return visitor.visitFloat(_data->content.asFloat);
|
||||
|
||||
case JSON_ARRAY:
|
||||
return visitor.visitArray(ArrayConstRef(&_data->content.asArray));
|
||||
|
||||
case JSON_OBJECT:
|
||||
return visitor.visitObject(ObjectConstRef(&_data->content.asObject));
|
||||
|
||||
case JSON_LINKED_STRING:
|
||||
case JSON_OWNED_STRING:
|
||||
return visitor.visitString(_data->content.asString);
|
||||
|
||||
case JSON_OWNED_RAW:
|
||||
case JSON_LINKED_RAW:
|
||||
return visitor.visitRawJson(_data->content.asRaw.data,
|
||||
_data->content.asRaw.size);
|
||||
|
||||
case JSON_NEGATIVE_INTEGER:
|
||||
return visitor.visitNegativeInteger(_data->content.asInteger);
|
||||
|
||||
case JSON_POSITIVE_INTEGER:
|
||||
return visitor.visitPositiveInteger(_data->content.asInteger);
|
||||
|
||||
case JSON_BOOLEAN:
|
||||
return visitor.visitBoolean(_data->content.asInteger != 0);
|
||||
|
||||
default:
|
||||
return visitor.visitNull();
|
||||
}
|
||||
}
|
||||
|
||||
inline VariantConstRef VariantConstRef::operator[](size_t index) const {
|
||||
return ArrayConstRef(variantAsArray(_data))[index];
|
||||
}
|
||||
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
@ -7,26 +7,26 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h> // for uint8_t
|
||||
|
||||
#include "Data/JsonVariantData.hpp"
|
||||
#include "Data/VariantAs.hpp"
|
||||
#include "Data/VariantFunctions.hpp"
|
||||
#include "JsonVariant.hpp"
|
||||
#include "JsonVariantBase.hpp"
|
||||
#include "Memory/MemoryPool.hpp"
|
||||
#include "Numbers/parseFloat.hpp"
|
||||
#include "Numbers/parseInteger.hpp"
|
||||
#include "Polyfills/type_traits.hpp"
|
||||
#include "Visitable.hpp"
|
||||
#include "../Memory/MemoryPool.hpp"
|
||||
#include "../Misc/Visitable.hpp"
|
||||
#include "../Numbers/parseFloat.hpp"
|
||||
#include "../Numbers/parseInteger.hpp"
|
||||
#include "../Operators/VariantOperators.hpp"
|
||||
#include "../Polyfills/type_traits.hpp"
|
||||
#include "VariantAs.hpp"
|
||||
#include "VariantData.hpp"
|
||||
#include "VariantFunctions.hpp"
|
||||
#include "VariantRef.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
// Forward declarations.
|
||||
class JsonArray;
|
||||
class JsonObject;
|
||||
class ArrayRef;
|
||||
class ObjectRef;
|
||||
|
||||
// Contains the methods shared by JsonVariant and JsonVariantConst
|
||||
// Contains the methods shared by VariantRef and VariantConstRef
|
||||
template <typename TData>
|
||||
class JsonVariantProxy {
|
||||
class VariantRefBase {
|
||||
public:
|
||||
// Tells wether the variant has the specified type.
|
||||
// Returns true if the variant has type type T, false otherwise.
|
||||
@ -74,20 +74,20 @@ class JsonVariantProxy {
|
||||
return variantIsString(_data);
|
||||
}
|
||||
//
|
||||
// bool is<JsonArray> const;
|
||||
// bool is<const JsonArray> const;
|
||||
// bool is<ArrayRef> const;
|
||||
// bool is<const ArrayRef> const;
|
||||
template <typename T>
|
||||
FORCE_INLINE typename enable_if<
|
||||
is_same<typename remove_const<T>::type, JsonArray>::value, bool>::type
|
||||
is_same<typename remove_const<T>::type, ArrayRef>::value, bool>::type
|
||||
is() const {
|
||||
return variantIsArray(_data);
|
||||
}
|
||||
//
|
||||
// bool is<JsonObject> const;
|
||||
// bool is<const JsonObject> const;
|
||||
// bool is<ObjectRef> const;
|
||||
// bool is<const ObjectRef> const;
|
||||
template <typename T>
|
||||
FORCE_INLINE typename enable_if<
|
||||
is_same<typename remove_const<T>::type, JsonObject>::value, bool>::type
|
||||
is_same<typename remove_const<T>::type, ObjectRef>::value, bool>::type
|
||||
is() const {
|
||||
return variantIsObject(_data);
|
||||
}
|
||||
@ -106,7 +106,7 @@ class JsonVariantProxy {
|
||||
}
|
||||
|
||||
protected:
|
||||
JsonVariantProxy(TData *data) : _data(data) {}
|
||||
VariantRefBase(TData *data) : _data(data) {}
|
||||
TData *_data;
|
||||
};
|
||||
|
||||
@ -116,20 +116,20 @@ class JsonVariantProxy {
|
||||
// - 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 JsonVariantProxy<JsonVariantData>,
|
||||
public JsonVariantBase<JsonVariant>,
|
||||
public Visitable {
|
||||
typedef JsonVariantProxy<JsonVariantData> proxy_type;
|
||||
friend class JsonVariantConst;
|
||||
// - a reference to a ArrayRef or ObjectRef
|
||||
class VariantRef : public VariantRefBase<VariantData>,
|
||||
public VariantOperators<VariantRef>,
|
||||
public Visitable {
|
||||
typedef VariantRefBase<VariantData> base_type;
|
||||
friend class VariantConstRef;
|
||||
|
||||
public:
|
||||
// Intenal use only
|
||||
FORCE_INLINE JsonVariant(MemoryPool *memoryPool, JsonVariantData *data)
|
||||
: proxy_type(data), _memoryPool(memoryPool) {}
|
||||
FORCE_INLINE VariantRef(MemoryPool *memoryPool, VariantData *data)
|
||||
: base_type(data), _memoryPool(memoryPool) {}
|
||||
|
||||
// Creates an uninitialized JsonVariant
|
||||
FORCE_INLINE JsonVariant() : proxy_type(0), _memoryPool(0) {}
|
||||
// Creates an uninitialized VariantRef
|
||||
FORCE_INLINE VariantRef() : base_type(0), _memoryPool(0) {}
|
||||
|
||||
// set(bool value)
|
||||
FORCE_INLINE bool set(bool value) const {
|
||||
@ -142,7 +142,7 @@ class JsonVariant : public JsonVariantProxy<JsonVariantData>,
|
||||
FORCE_INLINE bool set(
|
||||
T value,
|
||||
typename enable_if<is_floating_point<T>::value>::type * = 0) const {
|
||||
return variantSetFloat(_data, static_cast<JsonFloat>(value));
|
||||
return variantSetFloat(_data, static_cast<Float>(value));
|
||||
}
|
||||
|
||||
// set(char)
|
||||
@ -165,7 +165,7 @@ class JsonVariant : public JsonVariantProxy<JsonVariantData>,
|
||||
FORCE_INLINE bool set(
|
||||
T value, typename enable_if<is_integral<T>::value &&
|
||||
is_unsigned<T>::value>::type * = 0) const {
|
||||
return variantSetUnsignedInteger(_data, static_cast<JsonUInt>(value));
|
||||
return variantSetUnsignedInteger(_data, static_cast<UInt>(value));
|
||||
}
|
||||
|
||||
// set(SerializedValue<const char *>)
|
||||
@ -213,43 +213,43 @@ class JsonVariant : public JsonVariantProxy<JsonVariantData>,
|
||||
return variantSetString(_data, value.c_str());
|
||||
}
|
||||
|
||||
bool set(JsonVariantConst value) const;
|
||||
bool set(JsonVariant value) const;
|
||||
bool set(VariantConstRef value) const;
|
||||
bool set(VariantRef value) const;
|
||||
|
||||
FORCE_INLINE bool set(JsonArray array) const;
|
||||
FORCE_INLINE bool set(const JsonArraySubscript &) const;
|
||||
FORCE_INLINE bool set(JsonObject object) const;
|
||||
FORCE_INLINE bool set(ArrayRef array) const;
|
||||
FORCE_INLINE bool set(const ArraySubscript &) const;
|
||||
FORCE_INLINE bool set(ObjectRef object) const;
|
||||
template <typename TString>
|
||||
FORCE_INLINE bool set(const JsonObjectSubscript<TString> &) const;
|
||||
FORCE_INLINE bool set(const ObjectSubscript<TString> &) const;
|
||||
|
||||
// Get the variant as the specified type.
|
||||
//
|
||||
// std::string as<std::string>() const;
|
||||
// String as<String>() const;
|
||||
template <typename T>
|
||||
FORCE_INLINE typename enable_if<!is_same<T, JsonArray>::value &&
|
||||
!is_same<T, JsonObject>::value &&
|
||||
!is_same<T, JsonVariant>::value,
|
||||
typename JsonVariantAs<T>::type>::type
|
||||
FORCE_INLINE typename enable_if<!is_same<T, ArrayRef>::value &&
|
||||
!is_same<T, ObjectRef>::value &&
|
||||
!is_same<T, VariantRef>::value,
|
||||
typename VariantAs<T>::type>::type
|
||||
as() const {
|
||||
return variantAs<T>(_data);
|
||||
}
|
||||
//
|
||||
// JsonArray as<JsonArray>() const;
|
||||
// const JsonArray as<const JsonArray>() const;
|
||||
// ArrayRef as<ArrayRef>() const;
|
||||
// const ArrayRef as<const ArrayRef>() const;
|
||||
template <typename T>
|
||||
FORCE_INLINE typename enable_if<is_same<T, JsonArray>::value, T>::type as()
|
||||
FORCE_INLINE typename enable_if<is_same<T, ArrayRef>::value, T>::type as()
|
||||
const;
|
||||
//
|
||||
// JsonObject as<JsonObject>() const;
|
||||
// const JsonObject as<const JsonObject>() const;
|
||||
// ObjectRef as<ObjectRef>() const;
|
||||
// const ObjectRef as<const ObjectRef>() const;
|
||||
template <typename T>
|
||||
FORCE_INLINE typename enable_if<is_same<T, JsonObject>::value, T>::type as()
|
||||
FORCE_INLINE typename enable_if<is_same<T, ObjectRef>::value, T>::type as()
|
||||
const;
|
||||
//
|
||||
// JsonVariant as<JsonVariant> const;
|
||||
// VariantRef as<VariantRef> const;
|
||||
template <typename T>
|
||||
FORCE_INLINE typename enable_if<is_same<T, JsonVariant>::value, T>::type as()
|
||||
FORCE_INLINE typename enable_if<is_same<T, VariantRef>::value, T>::type as()
|
||||
const {
|
||||
return *this;
|
||||
}
|
||||
@ -257,44 +257,43 @@ class JsonVariant : public JsonVariantProxy<JsonVariantData>,
|
||||
template <typename Visitor>
|
||||
void accept(Visitor &visitor) const;
|
||||
|
||||
FORCE_INLINE bool operator==(JsonVariant lhs) const {
|
||||
FORCE_INLINE bool operator==(VariantRef lhs) const {
|
||||
return variantEquals(_data, lhs._data);
|
||||
}
|
||||
|
||||
FORCE_INLINE bool operator!=(JsonVariant lhs) const {
|
||||
FORCE_INLINE bool operator!=(VariantRef lhs) const {
|
||||
return !variantEquals(_data, lhs._data);
|
||||
}
|
||||
|
||||
// Change the type of the variant
|
||||
//
|
||||
// JsonArray to<JsonArray>()
|
||||
// ArrayRef to<ArrayRef>()
|
||||
template <typename T>
|
||||
typename enable_if<is_same<T, JsonArray>::value, JsonArray>::type to() const;
|
||||
typename enable_if<is_same<T, ArrayRef>::value, ArrayRef>::type to() const;
|
||||
//
|
||||
// JsonObject to<JsonObject>()
|
||||
// ObjectRef to<ObjectRef>()
|
||||
template <typename T>
|
||||
typename enable_if<is_same<T, JsonObject>::value, JsonObject>::type to()
|
||||
const;
|
||||
typename enable_if<is_same<T, ObjectRef>::value, ObjectRef>::type to() const;
|
||||
//
|
||||
// JsonObject to<JsonVariant>()
|
||||
// ObjectRef to<VariantRef>()
|
||||
template <typename T>
|
||||
typename enable_if<is_same<T, JsonVariant>::value, JsonVariant>::type to()
|
||||
typename enable_if<is_same<T, VariantRef>::value, VariantRef>::type to()
|
||||
const;
|
||||
|
||||
private:
|
||||
MemoryPool *_memoryPool;
|
||||
};
|
||||
|
||||
class JsonVariantConst : public JsonVariantProxy<const JsonVariantData>,
|
||||
public JsonVariantBase<JsonVariantConst>,
|
||||
public Visitable {
|
||||
typedef JsonVariantProxy<const JsonVariantData> proxy_type;
|
||||
friend class JsonVariant;
|
||||
class VariantConstRef : public VariantRefBase<const VariantData>,
|
||||
public VariantOperators<VariantConstRef>,
|
||||
public Visitable {
|
||||
typedef VariantRefBase<const VariantData> base_type;
|
||||
friend class VariantRef;
|
||||
|
||||
public:
|
||||
JsonVariantConst() : proxy_type(0) {}
|
||||
JsonVariantConst(const JsonVariantData *data) : proxy_type(data) {}
|
||||
JsonVariantConst(JsonVariant var) : proxy_type(var._data) {}
|
||||
VariantConstRef() : base_type(0) {}
|
||||
VariantConstRef(const VariantData *data) : base_type(data) {}
|
||||
VariantConstRef(VariantRef var) : base_type(var._data) {}
|
||||
|
||||
template <typename Visitor>
|
||||
void accept(Visitor &visitor) const;
|
||||
@ -302,29 +301,29 @@ class JsonVariantConst : public JsonVariantProxy<const JsonVariantData>,
|
||||
// Get the variant as the specified type.
|
||||
//
|
||||
template <typename T>
|
||||
FORCE_INLINE typename JsonVariantConstAs<T>::type as() const {
|
||||
return variantAs<typename JsonVariantConstAs<T>::type>(_data);
|
||||
FORCE_INLINE typename VariantConstAs<T>::type as() const {
|
||||
return variantAs<typename VariantConstAs<T>::type>(_data);
|
||||
}
|
||||
|
||||
FORCE_INLINE JsonVariantConst operator[](size_t index) const;
|
||||
FORCE_INLINE VariantConstRef operator[](size_t index) const;
|
||||
|
||||
//
|
||||
// const JsonVariantConst operator[](TKey) const;
|
||||
// const VariantConstRef operator[](TKey) const;
|
||||
// TKey = const std::string&, const String&
|
||||
template <typename TString>
|
||||
FORCE_INLINE
|
||||
typename enable_if<IsString<TString>::value, JsonVariantConst>::type
|
||||
typename enable_if<IsString<TString>::value, VariantConstRef>::type
|
||||
operator[](const TString &key) const {
|
||||
return JsonVariantConst(objectGet(variantAsObject(_data), makeString(key)));
|
||||
return VariantConstRef(objectGet(variantAsObject(_data), makeString(key)));
|
||||
}
|
||||
//
|
||||
// JsonVariantConst operator[](TKey);
|
||||
// VariantConstRef operator[](TKey);
|
||||
// TKey = const char*, const char[N], const FlashStringHelper*
|
||||
template <typename TString>
|
||||
FORCE_INLINE
|
||||
typename enable_if<IsString<TString *>::value, JsonVariantConst>::type
|
||||
typename enable_if<IsString<TString *>::value, VariantConstRef>::type
|
||||
operator[](TString *key) const {
|
||||
return JsonVariantConst(objectGet(variantAsObject(_data), makeString(key)));
|
||||
return VariantConstRef(objectGet(variantAsObject(_data), makeString(key)));
|
||||
}
|
||||
};
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
@ -4,15 +4,15 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../Data/JsonVariantData.hpp"
|
||||
#include "../Polyfills/type_traits.hpp"
|
||||
#include "../Variant/VariantData.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
typedef conditional<sizeof(void*) <= 2, int8_t, int16_t>::type VariantSlotDiff;
|
||||
|
||||
struct VariantSlot {
|
||||
JsonVariantData value;
|
||||
VariantData value;
|
||||
VariantSlotDiff next;
|
||||
VariantSlotDiff prev;
|
||||
const char* key;
|
@ -5,26 +5,26 @@
|
||||
#pragma once
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
class JsonArray;
|
||||
class JsonObject;
|
||||
class JsonVariant;
|
||||
class ArrayRef;
|
||||
class ObjectRef;
|
||||
class VariantRef;
|
||||
|
||||
// A metafunction that returns the type of the value returned by
|
||||
// JsonVariant::to<T>()
|
||||
// VariantRef::to<T>()
|
||||
template <typename T>
|
||||
struct JsonVariantTo {};
|
||||
struct VariantTo {};
|
||||
|
||||
template <>
|
||||
struct JsonVariantTo<JsonArray> {
|
||||
typedef JsonArray type;
|
||||
struct VariantTo<ArrayRef> {
|
||||
typedef ArrayRef type;
|
||||
};
|
||||
template <>
|
||||
struct JsonVariantTo<JsonObject> {
|
||||
typedef JsonObject type;
|
||||
struct VariantTo<ObjectRef> {
|
||||
typedef ObjectRef type;
|
||||
};
|
||||
template <>
|
||||
struct JsonVariantTo<JsonVariant> {
|
||||
typedef JsonVariant type;
|
||||
struct VariantTo<VariantRef> {
|
||||
typedef VariantRef type;
|
||||
};
|
||||
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
@ -13,9 +13,6 @@ TEST_CASE("Polyfills/type_traits") {
|
||||
static_cast<bool>(is_base_of<std::istream, std::ostringstream>::value));
|
||||
REQUIRE(
|
||||
static_cast<bool>(is_base_of<std::istream, std::istringstream>::value));
|
||||
REQUIRE(static_cast<bool>(
|
||||
is_base_of<JsonVariantBase<JsonObjectSubscript<const char*> >,
|
||||
JsonObjectSubscript<const char*> >::value));
|
||||
}
|
||||
|
||||
SECTION("is_array") {
|
||||
@ -54,14 +51,14 @@ TEST_CASE("Polyfills/type_traits") {
|
||||
SECTION("IsVisitable") {
|
||||
CHECK(IsVisitable<DeserializationError>::value == false);
|
||||
CHECK(IsVisitable<JsonPair>::value == false);
|
||||
CHECK(IsVisitable<JsonVariant>::value == true);
|
||||
CHECK(IsVisitable<JsonVariantConst>::value == true);
|
||||
CHECK(IsVisitable<JsonArray>::value == true);
|
||||
CHECK(IsVisitable<JsonArraySubscript>::value == true);
|
||||
CHECK(IsVisitable<JsonArrayConst>::value == true);
|
||||
CHECK(IsVisitable<JsonObject>::value == true);
|
||||
CHECK(IsVisitable<JsonObjectSubscript<const char*> >::value == true);
|
||||
CHECK(IsVisitable<JsonObjectConst>::value == true);
|
||||
CHECK(IsVisitable<VariantRef>::value == true);
|
||||
CHECK(IsVisitable<VariantConstRef>::value == true);
|
||||
CHECK(IsVisitable<ArrayRef>::value == true);
|
||||
CHECK(IsVisitable<ArraySubscript>::value == true);
|
||||
CHECK(IsVisitable<ArrayConstRef>::value == true);
|
||||
CHECK(IsVisitable<ObjectRef>::value == true);
|
||||
CHECK(IsVisitable<ObjectSubscript<const char*> >::value == true);
|
||||
CHECK(IsVisitable<ObjectConstRef>::value == true);
|
||||
CHECK(IsVisitable<DynamicJsonDocument>::value == true);
|
||||
CHECK(IsVisitable<StaticJsonDocument<10> >::value == true);
|
||||
}
|
||||
|
Reference in New Issue
Block a user