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