forked from bblanchon/ArduinoJson
Don't use JsonBuffer to create or parse objects and arrays.
* Added DynamicJsonArray and StaticJsonArray * Added DynamicJsonObject and StaticJsonObject * Added DynamicJsonVariant and StaticJsonVariant * Added deserializeJson() * Removed JsonBuffer::parseArray(), parseObject() and parse() * Removed JsonBuffer::createArray() and createObject()
This commit is contained in:
@ -4,14 +4,18 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "ArduinoJson/DynamicJsonArray.hpp"
|
||||
#include "ArduinoJson/DynamicJsonBuffer.hpp"
|
||||
#include "ArduinoJson/JsonArray.hpp"
|
||||
#include "ArduinoJson/JsonObject.hpp"
|
||||
#include "ArduinoJson/DynamicJsonObject.hpp"
|
||||
#include "ArduinoJson/DynamicJsonVariant.hpp"
|
||||
#include "ArduinoJson/StaticJsonArray.hpp"
|
||||
#include "ArduinoJson/StaticJsonBuffer.hpp"
|
||||
#include "ArduinoJson/StaticJsonObject.hpp"
|
||||
#include "ArduinoJson/StaticJsonVariant.hpp"
|
||||
#include "ArduinoJson/deserializeJson.hpp"
|
||||
|
||||
#include "ArduinoJson/Deserialization/JsonParserImpl.hpp"
|
||||
#include "ArduinoJson/JsonArrayImpl.hpp"
|
||||
#include "ArduinoJson/JsonBufferImpl.hpp"
|
||||
#include "ArduinoJson/JsonObjectImpl.hpp"
|
||||
#include "ArduinoJson/JsonVariantImpl.hpp"
|
||||
#include "ArduinoJson/Serialization/JsonSerializerImpl.hpp"
|
||||
|
@ -27,7 +27,7 @@ class List {
|
||||
// When buffer is NULL, the List is not able to grow and success() returns
|
||||
// false. This is used to identify bad memory allocations and parsing
|
||||
// failures.
|
||||
explicit List(JsonBuffer *buffer) : _buffer(buffer), _firstNode(NULL) {}
|
||||
explicit List(JsonBuffer *buf) : _buffer(buf), _firstNode(NULL) {}
|
||||
|
||||
// Returns true if the object is valid
|
||||
// Would return false in the following situation:
|
||||
@ -84,11 +84,15 @@ class List {
|
||||
}
|
||||
}
|
||||
|
||||
JsonBuffer &buffer() const {
|
||||
return *_buffer;
|
||||
}
|
||||
|
||||
protected:
|
||||
JsonBuffer *_buffer;
|
||||
|
||||
private:
|
||||
node_type *_firstNode;
|
||||
};
|
||||
}
|
||||
}
|
||||
} // namespace Internals
|
||||
} // namespace ArduinoJson
|
||||
|
@ -24,15 +24,9 @@ class JsonParser {
|
||||
_reader(reader),
|
||||
_writer(writer),
|
||||
_nestingLimit(nestingLimit) {}
|
||||
|
||||
JsonArray &parseArray();
|
||||
JsonObject &parseObject();
|
||||
|
||||
JsonVariant parseVariant() {
|
||||
JsonVariant result;
|
||||
parseAnythingTo(&result);
|
||||
return result;
|
||||
}
|
||||
bool parse(JsonArray &destination);
|
||||
bool parse(JsonObject &destination);
|
||||
bool parse(JsonVariant &destination);
|
||||
|
||||
private:
|
||||
JsonParser &operator=(const JsonParser &); // non-copiable
|
||||
|
@ -46,11 +46,8 @@ ArduinoJson::Internals::JsonParser<TReader, TWriter>::parseAnythingToUnsafe(
|
||||
}
|
||||
|
||||
template <typename TReader, typename TWriter>
|
||||
inline ArduinoJson::JsonArray &
|
||||
ArduinoJson::Internals::JsonParser<TReader, TWriter>::parseArray() {
|
||||
// Create an empty array
|
||||
JsonArray &array = _buffer->createArray();
|
||||
|
||||
inline bool ArduinoJson::Internals::JsonParser<TReader, TWriter>::parse(
|
||||
JsonArray &array) {
|
||||
// Check opening braket
|
||||
if (!eat('[')) goto ERROR_MISSING_BRACKET;
|
||||
if (eat(']')) goto SUCCESS_EMPTY_ARRAY;
|
||||
@ -69,31 +66,18 @@ ArduinoJson::Internals::JsonParser<TReader, TWriter>::parseArray() {
|
||||
|
||||
SUCCESS_EMPTY_ARRAY:
|
||||
SUCCES_NON_EMPTY_ARRAY:
|
||||
return array;
|
||||
return true;
|
||||
|
||||
ERROR_INVALID_VALUE:
|
||||
ERROR_MISSING_BRACKET:
|
||||
ERROR_MISSING_COMMA:
|
||||
ERROR_NO_MEMORY:
|
||||
return JsonArray::invalid();
|
||||
return false;
|
||||
}
|
||||
|
||||
template <typename TReader, typename TWriter>
|
||||
inline bool ArduinoJson::Internals::JsonParser<TReader, TWriter>::parseArrayTo(
|
||||
JsonVariant *destination) {
|
||||
JsonArray &array = parseArray();
|
||||
if (!array.success()) return false;
|
||||
|
||||
*destination = array;
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename TReader, typename TWriter>
|
||||
inline ArduinoJson::JsonObject &
|
||||
ArduinoJson::Internals::JsonParser<TReader, TWriter>::parseObject() {
|
||||
// Create an empty object
|
||||
JsonObject &object = _buffer->createObject();
|
||||
|
||||
inline bool ArduinoJson::Internals::JsonParser<TReader, TWriter>::parse(
|
||||
JsonObject &object) {
|
||||
// Check opening brace
|
||||
if (!eat('{')) goto ERROR_MISSING_BRACE;
|
||||
if (eat('}')) goto SUCCESS_EMPTY_OBJECT;
|
||||
@ -117,7 +101,7 @@ ArduinoJson::Internals::JsonParser<TReader, TWriter>::parseObject() {
|
||||
|
||||
SUCCESS_EMPTY_OBJECT:
|
||||
SUCCESS_NON_EMPTY_OBJECT:
|
||||
return object;
|
||||
return true;
|
||||
|
||||
ERROR_INVALID_KEY:
|
||||
ERROR_INVALID_VALUE:
|
||||
@ -125,17 +109,31 @@ ERROR_MISSING_BRACE:
|
||||
ERROR_MISSING_COLON:
|
||||
ERROR_MISSING_COMMA:
|
||||
ERROR_NO_MEMORY:
|
||||
return JsonObject::invalid();
|
||||
return false;
|
||||
}
|
||||
|
||||
template <typename TReader, typename TWriter>
|
||||
inline bool ArduinoJson::Internals::JsonParser<TReader, TWriter>::parse(
|
||||
JsonVariant &variant) {
|
||||
return parseAnythingTo(&variant);
|
||||
}
|
||||
|
||||
template <typename TReader, typename TWriter>
|
||||
inline bool ArduinoJson::Internals::JsonParser<TReader, TWriter>::parseArrayTo(
|
||||
JsonVariant *destination) {
|
||||
JsonArray *array = new (_buffer) JsonArray(_buffer);
|
||||
if (!array) return false;
|
||||
*destination = array;
|
||||
return parse(*array);
|
||||
}
|
||||
|
||||
template <typename TReader, typename TWriter>
|
||||
inline bool ArduinoJson::Internals::JsonParser<TReader, TWriter>::parseObjectTo(
|
||||
JsonVariant *destination) {
|
||||
JsonObject &object = parseObject();
|
||||
if (!object.success()) return false;
|
||||
|
||||
JsonObject *object = new (_buffer) JsonObject(_buffer);
|
||||
if (!object) return false;
|
||||
*destination = object;
|
||||
return true;
|
||||
return parse(*object);
|
||||
}
|
||||
|
||||
template <typename TReader, typename TWriter>
|
||||
|
27
src/ArduinoJson/DynamicJsonArray.hpp
Normal file
27
src/ArduinoJson/DynamicJsonArray.hpp
Normal file
@ -0,0 +1,27 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "DynamicJsonBuffer.hpp"
|
||||
#include "JsonArray.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
class DynamicJsonArray : public JsonArray {
|
||||
DynamicJsonBuffer _buffer;
|
||||
|
||||
public:
|
||||
DynamicJsonArray() : JsonArray(&_buffer) {}
|
||||
DynamicJsonArray(size_t capacity)
|
||||
: JsonArray(&_buffer), _buffer(capacity - sizeof(JsonArray)) {}
|
||||
|
||||
size_t memoryUsage() const {
|
||||
return _buffer.size() + sizeof(JsonArray);
|
||||
}
|
||||
|
||||
DynamicJsonBuffer& buffer() {
|
||||
return _buffer;
|
||||
}
|
||||
};
|
||||
} // namespace ArduinoJson
|
@ -4,7 +4,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "JsonBufferBase.hpp"
|
||||
#include "JsonBuffer.hpp"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
@ -31,8 +31,7 @@ class DefaultAllocator {
|
||||
};
|
||||
|
||||
template <typename TAllocator>
|
||||
class DynamicJsonBufferBase
|
||||
: public JsonBufferBase<DynamicJsonBufferBase<TAllocator> > {
|
||||
class DynamicJsonBufferBase : public JsonBuffer {
|
||||
struct Block;
|
||||
struct EmptyBlock {
|
||||
Block* next;
|
||||
@ -152,7 +151,7 @@ class DynamicJsonBufferBase
|
||||
Block* _head;
|
||||
size_t _nextBlockCapacity;
|
||||
};
|
||||
}
|
||||
} // namespace Internals
|
||||
|
||||
#if defined(__clang__)
|
||||
#pragma clang diagnostic pop
|
||||
@ -167,4 +166,4 @@ class DynamicJsonBufferBase
|
||||
// more suitable for embedded systems.
|
||||
typedef Internals::DynamicJsonBufferBase<Internals::DefaultAllocator>
|
||||
DynamicJsonBuffer;
|
||||
}
|
||||
} // namespace ArduinoJson
|
||||
|
27
src/ArduinoJson/DynamicJsonObject.hpp
Normal file
27
src/ArduinoJson/DynamicJsonObject.hpp
Normal file
@ -0,0 +1,27 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "DynamicJsonBuffer.hpp"
|
||||
#include "JsonObject.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
class DynamicJsonObject : public JsonObject {
|
||||
DynamicJsonBuffer _buffer;
|
||||
|
||||
public:
|
||||
DynamicJsonObject() : JsonObject(&_buffer) {}
|
||||
DynamicJsonObject(size_t capacity)
|
||||
: JsonObject(&_buffer), _buffer(capacity - sizeof(JsonObject)) {}
|
||||
|
||||
DynamicJsonBuffer& buffer() {
|
||||
return _buffer;
|
||||
}
|
||||
|
||||
size_t memoryUsage() const {
|
||||
return _buffer.size() + sizeof(JsonObject);
|
||||
}
|
||||
};
|
||||
} // namespace ArduinoJson
|
40
src/ArduinoJson/DynamicJsonVariant.hpp
Normal file
40
src/ArduinoJson/DynamicJsonVariant.hpp
Normal file
@ -0,0 +1,40 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "DynamicJsonBuffer.hpp"
|
||||
#include "JsonVariant.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
|
||||
class DynamicJsonVariant : public JsonVariant {
|
||||
DynamicJsonBuffer _buffer;
|
||||
|
||||
public:
|
||||
DynamicJsonVariant() : JsonVariant() {}
|
||||
|
||||
template <typename T>
|
||||
DynamicJsonVariant& operator=(const T& value) {
|
||||
_buffer.clear();
|
||||
JsonVariant::operator=(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
DynamicJsonVariant& operator=(const T* value) {
|
||||
_buffer.clear();
|
||||
JsonVariant::operator=(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
DynamicJsonBuffer& buffer() {
|
||||
return _buffer;
|
||||
}
|
||||
|
||||
size_t memoryUsage() const {
|
||||
return _buffer.size() + sizeof(JsonVariant);
|
||||
}
|
||||
};
|
||||
} // namespace ArduinoJson
|
@ -30,23 +30,14 @@ namespace Internals {
|
||||
class JsonArraySubscript;
|
||||
}
|
||||
|
||||
// An array of JsonVariant.
|
||||
//
|
||||
// The constructor is private, instances must be created via
|
||||
// JsonBuffer::createArray() or JsonBuffer::parseArray().
|
||||
// A JsonArray can be serialized to a JSON string via JsonArray::printTo().
|
||||
// It can also be deserialized from a JSON string via JsonBuffer::parseArray().
|
||||
class JsonArray : public Internals::JsonPrintable<JsonArray>,
|
||||
public Internals::ReferenceType,
|
||||
public Internals::NonCopyable,
|
||||
public Internals::List<JsonVariant>,
|
||||
public Internals::JsonBufferAllocated {
|
||||
public:
|
||||
// Create an empty JsonArray attached to the specified JsonBuffer.
|
||||
// You should not call this constructor directly.
|
||||
// Instead, use JsonBuffer::createArray() or JsonBuffer::parseArray().
|
||||
explicit JsonArray(JsonBuffer *buffer) throw()
|
||||
: Internals::List<JsonVariant>(buffer) {}
|
||||
explicit JsonArray(JsonBuffer *buf) throw()
|
||||
: Internals::List<JsonVariant>(buf) {}
|
||||
|
||||
// Gets the value at the specified index
|
||||
const Internals::JsonArraySubscript operator[](size_t index) const;
|
||||
@ -119,11 +110,9 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
|
||||
}
|
||||
|
||||
// Creates a JsonArray and adds a reference at the end of the array.
|
||||
// It's a shortcut for JsonBuffer::createArray() and JsonArray::add()
|
||||
JsonArray &createNestedArray();
|
||||
|
||||
// Creates a JsonObject and adds a reference at the end of the array.
|
||||
// It's a shortcut for JsonBuffer::createObject() and JsonArray::add()
|
||||
JsonObject &createNestedObject();
|
||||
|
||||
// Removes element at specified index.
|
||||
@ -223,5 +212,5 @@ struct JsonVariantDefault<JsonArray> {
|
||||
return JsonArray::invalid();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
} // namespace Internals
|
||||
} // namespace ArduinoJson
|
||||
|
@ -11,16 +11,18 @@
|
||||
namespace ArduinoJson {
|
||||
|
||||
inline JsonArray &JsonArray::createNestedArray() {
|
||||
if (!_buffer) return JsonArray::invalid();
|
||||
JsonArray &array = _buffer->createArray();
|
||||
JsonArray *array = new (_buffer) JsonArray(_buffer);
|
||||
if (!array) return JsonArray::invalid();
|
||||
|
||||
add(array);
|
||||
return array;
|
||||
return *array;
|
||||
}
|
||||
|
||||
inline JsonObject &JsonArray::createNestedObject() {
|
||||
if (!_buffer) return JsonObject::invalid();
|
||||
JsonObject &object = _buffer->createObject();
|
||||
JsonObject *object = new (_buffer) JsonObject(_buffer);
|
||||
if (!object) return JsonObject::invalid();
|
||||
|
||||
add(object);
|
||||
return object;
|
||||
}
|
||||
return *object;
|
||||
}
|
||||
} // namespace ArduinoJson
|
||||
|
@ -24,18 +24,6 @@ class JsonObject;
|
||||
// fixed memory allocation.
|
||||
class JsonBuffer : Internals::NonCopyable {
|
||||
public:
|
||||
// Allocates an empty JsonArray.
|
||||
//
|
||||
// Returns a reference to the new JsonArray or JsonArray::invalid() if the
|
||||
// allocation fails.
|
||||
JsonArray &createArray();
|
||||
|
||||
// Allocates an empty JsonObject.
|
||||
//
|
||||
// Returns a reference to the new JsonObject or JsonObject::invalid() if the
|
||||
// allocation fails.
|
||||
JsonObject &createObject();
|
||||
|
||||
// Duplicates a string
|
||||
//
|
||||
// const char* strdup(TValue);
|
||||
@ -75,4 +63,4 @@ class JsonBuffer : Internals::NonCopyable {
|
||||
#endif
|
||||
}
|
||||
};
|
||||
}
|
||||
} // namespace ArduinoJson
|
||||
|
@ -1,127 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Deserialization/JsonParser.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
namespace Internals {
|
||||
template <typename TDerived>
|
||||
class JsonBufferBase : public JsonBuffer {
|
||||
public:
|
||||
// Allocates and populate a JsonArray from a JSON string.
|
||||
//
|
||||
// The First argument is a pointer to the JSON string, the memory must be
|
||||
// writable
|
||||
// because the parser will insert null-terminators and replace escaped chars.
|
||||
//
|
||||
// The second argument set the nesting limit
|
||||
//
|
||||
// Returns a reference to the new JsonObject or JsonObject::invalid() if the
|
||||
// allocation fails.
|
||||
// With this overload, the JsonBuffer will make a copy of the string
|
||||
//
|
||||
// JsonArray& parseArray(TString);
|
||||
// TString = const std::string&, const String&
|
||||
template <typename TString>
|
||||
typename Internals::EnableIf<!Internals::IsArray<TString>::value,
|
||||
JsonArray &>::type
|
||||
parseArray(const TString &json,
|
||||
uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) {
|
||||
return Internals::makeParser(that(), json, nestingLimit).parseArray();
|
||||
}
|
||||
//
|
||||
// JsonArray& parseArray(TString);
|
||||
// TString = const char*, const char[N], const FlashStringHelper*
|
||||
template <typename TString>
|
||||
JsonArray &parseArray(
|
||||
TString *json, uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) {
|
||||
return Internals::makeParser(that(), json, nestingLimit).parseArray();
|
||||
}
|
||||
//
|
||||
// JsonArray& parseArray(TString);
|
||||
// TString = std::istream&, Stream&
|
||||
template <typename TString>
|
||||
JsonArray &parseArray(
|
||||
TString &json, uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) {
|
||||
return Internals::makeParser(that(), json, nestingLimit).parseArray();
|
||||
}
|
||||
|
||||
// Allocates and populate a JsonObject from a JSON string.
|
||||
//
|
||||
// The First argument is a pointer to the JSON string, the memory must be
|
||||
// writable
|
||||
// because the parser will insert null-terminators and replace escaped chars.
|
||||
//
|
||||
// The second argument set the nesting limit
|
||||
//
|
||||
// Returns a reference to the new JsonObject or JsonObject::invalid() if the
|
||||
// allocation fails.
|
||||
//
|
||||
// JsonObject& parseObject(TString);
|
||||
// TString = const std::string&, const String&
|
||||
template <typename TString>
|
||||
typename Internals::EnableIf<!Internals::IsArray<TString>::value,
|
||||
JsonObject &>::type
|
||||
parseObject(const TString &json,
|
||||
uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) {
|
||||
return Internals::makeParser(that(), json, nestingLimit).parseObject();
|
||||
}
|
||||
//
|
||||
// JsonObject& parseObject(TString);
|
||||
// TString = const char*, const char[N], const FlashStringHelper*
|
||||
template <typename TString>
|
||||
JsonObject &parseObject(
|
||||
TString *json, uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) {
|
||||
return Internals::makeParser(that(), json, nestingLimit).parseObject();
|
||||
}
|
||||
//
|
||||
// JsonObject& parseObject(TString);
|
||||
// TString = std::istream&, Stream&
|
||||
template <typename TString>
|
||||
JsonObject &parseObject(
|
||||
TString &json, uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) {
|
||||
return Internals::makeParser(that(), json, nestingLimit).parseObject();
|
||||
}
|
||||
|
||||
// Generalized version of parseArray() and parseObject(), also works for
|
||||
// integral types.
|
||||
//
|
||||
// JsonVariant parse(TString);
|
||||
// TString = const std::string&, const String&
|
||||
template <typename TString>
|
||||
typename Internals::EnableIf<!Internals::IsArray<TString>::value,
|
||||
JsonVariant>::type
|
||||
parse(const TString &json,
|
||||
uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) {
|
||||
return Internals::makeParser(that(), json, nestingLimit).parseVariant();
|
||||
}
|
||||
//
|
||||
// JsonVariant parse(TString);
|
||||
// TString = const char*, const char[N], const FlashStringHelper*
|
||||
template <typename TString>
|
||||
JsonVariant parse(TString *json,
|
||||
uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) {
|
||||
return Internals::makeParser(that(), json, nestingLimit).parseVariant();
|
||||
}
|
||||
//
|
||||
// JsonVariant parse(TString);
|
||||
// TString = std::istream&, Stream&
|
||||
template <typename TString>
|
||||
JsonVariant parse(TString &json,
|
||||
uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) {
|
||||
return Internals::makeParser(that(), json, nestingLimit).parseVariant();
|
||||
}
|
||||
|
||||
protected:
|
||||
~JsonBufferBase() {}
|
||||
|
||||
private:
|
||||
TDerived *that() {
|
||||
return static_cast<TDerived *>(this);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Deserialization/JsonParser.hpp"
|
||||
|
||||
inline ArduinoJson::JsonArray &ArduinoJson::JsonBuffer::createArray() {
|
||||
JsonArray *ptr = new (this) JsonArray(this);
|
||||
return ptr ? *ptr : JsonArray::invalid();
|
||||
}
|
||||
|
||||
inline ArduinoJson::JsonObject &ArduinoJson::JsonBuffer::createObject() {
|
||||
JsonObject *ptr = new (this) JsonObject(this);
|
||||
return ptr ? *ptr : JsonObject::invalid();
|
||||
}
|
@ -31,12 +31,6 @@ template <typename>
|
||||
class JsonObjectSubscript;
|
||||
}
|
||||
|
||||
// A dictionary of JsonVariant indexed by string (char*)
|
||||
//
|
||||
// The constructor is private, instances must be created via
|
||||
// JsonBuffer::createObject() or JsonBuffer::parseObject().
|
||||
// A JsonObject can be serialized to a JSON string via JsonObject::printTo().
|
||||
// It can also be deserialized from a JSON string via JsonBuffer::parseObject().
|
||||
class JsonObject : public Internals::JsonPrintable<JsonObject>,
|
||||
public Internals::ReferenceType,
|
||||
public Internals::NonCopyable,
|
||||
@ -45,9 +39,8 @@ class JsonObject : public Internals::JsonPrintable<JsonObject>,
|
||||
public:
|
||||
// Create an empty JsonArray attached to the specified JsonBuffer.
|
||||
// You should not use this constructor directly.
|
||||
// Instead, use JsonBuffer::createObject() or JsonBuffer.parseObject().
|
||||
explicit JsonObject(JsonBuffer* buffer) throw()
|
||||
: Internals::List<JsonPair>(buffer) {}
|
||||
explicit JsonObject(JsonBuffer* buf) throw()
|
||||
: Internals::List<JsonPair>(buf) {}
|
||||
|
||||
// Gets or sets the value associated with the specified key.
|
||||
//
|
||||
@ -318,5 +311,5 @@ struct JsonVariantDefault<JsonObject> {
|
||||
return JsonObject::invalid();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
} // namespace Internals
|
||||
} // namespace ArduinoJson
|
||||
|
@ -12,17 +12,17 @@ namespace ArduinoJson {
|
||||
|
||||
template <typename TStringRef>
|
||||
inline JsonArray &JsonObject::createNestedArray_impl(TStringRef key) {
|
||||
if (!_buffer) return JsonArray::invalid();
|
||||
JsonArray &array = _buffer->createArray();
|
||||
JsonArray *array = new (_buffer) JsonArray(_buffer);
|
||||
if (!array) return JsonArray::invalid();
|
||||
set(key, array);
|
||||
return array;
|
||||
return *array;
|
||||
}
|
||||
|
||||
template <typename TStringRef>
|
||||
inline JsonObject &JsonObject::createNestedObject_impl(TStringRef key) {
|
||||
if (!_buffer) return JsonObject::invalid();
|
||||
JsonObject &object = _buffer->createObject();
|
||||
JsonObject *object = new (_buffer) JsonObject(_buffer);
|
||||
if (!object) return JsonObject::invalid();
|
||||
set(key, object);
|
||||
return object;
|
||||
}
|
||||
return *object;
|
||||
}
|
||||
} // namespace ArduinoJson
|
||||
|
@ -134,6 +134,16 @@ class JsonVariant : public Internals::JsonVariantBase<JsonVariant> {
|
||||
// if the variant is converted back to a JsonObject&
|
||||
JsonVariant(const JsonObject &object);
|
||||
|
||||
JsonVariant(JsonArray *array) {
|
||||
_content.asArray = array;
|
||||
_type = Internals::JSON_ARRAY;
|
||||
}
|
||||
|
||||
JsonVariant(JsonObject *object) {
|
||||
_content.asObject = object;
|
||||
_type = Internals::JSON_OBJECT;
|
||||
}
|
||||
|
||||
// Get the variant as the specified type.
|
||||
//
|
||||
// char as<char>() const;
|
||||
@ -352,4 +362,4 @@ DEPRECATED("Decimal places are ignored, use the double value instead")
|
||||
inline JsonVariant double_with_n_digits(double value, uint8_t) {
|
||||
return JsonVariant(value);
|
||||
}
|
||||
}
|
||||
} // namespace ArduinoJson
|
||||
|
27
src/ArduinoJson/StaticJsonArray.hpp
Normal file
27
src/ArduinoJson/StaticJsonArray.hpp
Normal file
@ -0,0 +1,27 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "JsonArray.hpp"
|
||||
#include "StaticJsonBuffer.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
|
||||
template <size_t CAPACITY>
|
||||
class StaticJsonArray : public JsonArray {
|
||||
StaticJsonBuffer<CAPACITY - sizeof(JsonArray)> _buffer;
|
||||
|
||||
public:
|
||||
StaticJsonArray() : JsonArray(&_buffer) {}
|
||||
|
||||
size_t memoryUsage() const {
|
||||
return _buffer.size() + sizeof(JsonArray);
|
||||
}
|
||||
|
||||
Internals::StaticJsonBufferBase& buffer() {
|
||||
return _buffer;
|
||||
}
|
||||
};
|
||||
} // namespace ArduinoJson
|
@ -4,12 +4,13 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "JsonBufferBase.hpp"
|
||||
#include "JsonBuffer.hpp"
|
||||
#include "TypeTraits/Max.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
namespace Internals {
|
||||
|
||||
class StaticJsonBufferBase : public JsonBufferBase<StaticJsonBufferBase> {
|
||||
class StaticJsonBufferBase : public JsonBuffer {
|
||||
public:
|
||||
class String {
|
||||
public:
|
||||
@ -91,7 +92,7 @@ class StaticJsonBufferBase : public JsonBufferBase<StaticJsonBufferBase> {
|
||||
size_t _capacity;
|
||||
size_t _size;
|
||||
};
|
||||
}
|
||||
} // namespace Internals
|
||||
|
||||
#if defined(__clang__)
|
||||
#pragma clang diagnostic push
|
||||
@ -108,14 +109,16 @@ class StaticJsonBufferBase : public JsonBufferBase<StaticJsonBufferBase> {
|
||||
// bytes.
|
||||
template <size_t CAPACITY>
|
||||
class StaticJsonBuffer : public Internals::StaticJsonBufferBase {
|
||||
static const size_t ACTUAL_CAPACITY = Internals::Max<1, CAPACITY>::value;
|
||||
|
||||
public:
|
||||
explicit StaticJsonBuffer()
|
||||
: Internals::StaticJsonBufferBase(_buffer, CAPACITY) {}
|
||||
: Internals::StaticJsonBufferBase(_buffer, ACTUAL_CAPACITY) {}
|
||||
|
||||
private:
|
||||
char _buffer[CAPACITY];
|
||||
char _buffer[ACTUAL_CAPACITY];
|
||||
};
|
||||
}
|
||||
} // namespace ArduinoJson
|
||||
|
||||
#if defined(__clang__)
|
||||
#pragma clang diagnostic pop
|
||||
|
27
src/ArduinoJson/StaticJsonObject.hpp
Normal file
27
src/ArduinoJson/StaticJsonObject.hpp
Normal file
@ -0,0 +1,27 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "JsonObject.hpp"
|
||||
#include "StaticJsonBuffer.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
|
||||
template <size_t CAPACITY>
|
||||
class StaticJsonObject : public JsonObject {
|
||||
StaticJsonBuffer<CAPACITY - sizeof(JsonObject)> _buffer;
|
||||
|
||||
public:
|
||||
StaticJsonObject() : JsonObject(&_buffer) {}
|
||||
|
||||
size_t memoryUsage() const {
|
||||
return _buffer.size() + sizeof(JsonObject);
|
||||
}
|
||||
|
||||
Internals::StaticJsonBufferBase& buffer() {
|
||||
return _buffer;
|
||||
}
|
||||
};
|
||||
} // namespace ArduinoJson
|
39
src/ArduinoJson/StaticJsonVariant.hpp
Normal file
39
src/ArduinoJson/StaticJsonVariant.hpp
Normal file
@ -0,0 +1,39 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "JsonVariant.hpp"
|
||||
#include "StaticJsonBuffer.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
|
||||
template <size_t CAPACITY = sizeof(JsonVariant)>
|
||||
class StaticJsonVariant : public JsonVariant {
|
||||
StaticJsonBuffer<CAPACITY - sizeof(JsonVariant)> _buffer;
|
||||
|
||||
public:
|
||||
template <typename T>
|
||||
StaticJsonVariant& operator=(const T& value) {
|
||||
_buffer.clear();
|
||||
JsonVariant::operator=(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
StaticJsonVariant& operator=(const T* value) {
|
||||
_buffer.clear();
|
||||
JsonVariant::operator=(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Internals::StaticJsonBufferBase& buffer() {
|
||||
return _buffer;
|
||||
}
|
||||
|
||||
size_t memoryUsage() const {
|
||||
return _buffer.size() + sizeof(JsonVariant);
|
||||
}
|
||||
};
|
||||
} // namespace ArduinoJson
|
24
src/ArduinoJson/TypeTraits/Max.hpp
Normal file
24
src/ArduinoJson/TypeTraits/Max.hpp
Normal file
@ -0,0 +1,24 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace ArduinoJson {
|
||||
namespace Internals {
|
||||
|
||||
// A meta-function that returns the highest value
|
||||
template <size_t X, size_t Y, bool MaxIsX = (X > Y)>
|
||||
struct Max {};
|
||||
|
||||
template <size_t X, size_t Y>
|
||||
struct Max<X, Y, true> {
|
||||
static const size_t value = X;
|
||||
};
|
||||
|
||||
template <size_t X, size_t Y>
|
||||
struct Max<X, Y, false> {
|
||||
static const size_t value = Y;
|
||||
};
|
||||
} // namespace Internals
|
||||
} // namespace ArduinoJson
|
40
src/ArduinoJson/deserializeJson.hpp
Normal file
40
src/ArduinoJson/deserializeJson.hpp
Normal file
@ -0,0 +1,40 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Deserialization/JsonParser.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
// bool deserializeJson(TDestination& destination, TString json);
|
||||
// TDestination = JsonArray, JsonObject, JsonVariant
|
||||
// TString = const std::string&, const String&
|
||||
template <typename TDestination, typename TString>
|
||||
typename Internals::EnableIf<!Internals::IsArray<TString>::value, bool>::type
|
||||
deserializeJson(TDestination &destination, const TString &json,
|
||||
uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) {
|
||||
return Internals::makeParser(&destination.buffer(), json, nestingLimit)
|
||||
.parse(destination);
|
||||
}
|
||||
//
|
||||
// bool deserializeJson(TDestination& destination, TString json);
|
||||
// TDestination = JsonArray, JsonObject, JsonVariant
|
||||
// TString = const char*, const char[N], const FlashStringHelper*
|
||||
template <typename TDestination, typename TString>
|
||||
bool deserializeJson(TDestination &destination, TString *json,
|
||||
uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) {
|
||||
return Internals::makeParser(&destination.buffer(), json, nestingLimit)
|
||||
.parse(destination);
|
||||
}
|
||||
//
|
||||
// bool deserializeJson(TDestination& destination, TString json);
|
||||
// TDestination = JsonArray, JsonObject, JsonVariant
|
||||
// TString = std::istream&, Stream&
|
||||
template <typename TDestination, typename TString>
|
||||
bool deserializeJson(TDestination &destination, TString &json,
|
||||
uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) {
|
||||
return Internals::makeParser(&destination.buffer(), json, nestingLimit)
|
||||
.parse(destination);
|
||||
}
|
||||
} // namespace ArduinoJson
|
Reference in New Issue
Block a user