mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-18 13:02:25 +02:00
Extracted a base class for JsonDocument
This commit is contained in:
@ -6,16 +6,13 @@
|
||||
|
||||
#include "ArduinoJson/Namespace.hpp"
|
||||
|
||||
#include "ArduinoJson/DynamicJsonDocument.hpp"
|
||||
#include "ArduinoJson/StaticJsonDocument.hpp"
|
||||
|
||||
#include "ArduinoJson/JsonObjectImpl.hpp"
|
||||
|
||||
#include "ArduinoJson/JsonArray.hpp"
|
||||
#include "ArduinoJson/JsonDocument.hpp"
|
||||
#include "ArduinoJson/JsonObject.hpp"
|
||||
|
||||
#include "ArduinoJson/JsonArrayImpl.hpp"
|
||||
#include "ArduinoJson/JsonArraySubscript.hpp"
|
||||
#include "ArduinoJson/JsonObjectImpl.hpp"
|
||||
#include "ArduinoJson/JsonObjectSubscript.hpp"
|
||||
#include "ArduinoJson/JsonVariantImpl.hpp"
|
||||
|
||||
|
@ -6,6 +6,8 @@
|
||||
|
||||
#include "../Configuration.hpp"
|
||||
|
||||
#include <stdint.h> // int64_t
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
#if ARDUINOJSON_USE_LONG_LONG
|
||||
|
@ -4,7 +4,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdlib.h> // size_t
|
||||
#include <stddef.h> // ptrdiff_t, size_t
|
||||
|
||||
#include "JsonFloat.hpp"
|
||||
#include "JsonInteger.hpp"
|
||||
|
@ -1,64 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Data/JsonVariantTo.hpp"
|
||||
#include "JsonVariant.hpp"
|
||||
#include "Memory/DynamicMemoryPool.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
class DynamicJsonDocument {
|
||||
public:
|
||||
uint8_t nestingLimit;
|
||||
|
||||
DynamicJsonDocument() : nestingLimit(ARDUINOJSON_DEFAULT_NESTING_LIMIT) {}
|
||||
DynamicJsonDocument(size_t capacity)
|
||||
: nestingLimit(ARDUINOJSON_DEFAULT_NESTING_LIMIT),
|
||||
_memoryPool(capacity) {}
|
||||
|
||||
template <typename T>
|
||||
bool is() const {
|
||||
return getVariant().is<T>();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename JsonVariantAs<T>::type as() const {
|
||||
return getVariant().as<T>();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename JsonVariantTo<T>::type to() {
|
||||
_memoryPool.clear();
|
||||
return getVariant().to<T>();
|
||||
}
|
||||
|
||||
void clear() {
|
||||
_memoryPool.clear();
|
||||
_rootData.type = JSON_NULL;
|
||||
}
|
||||
|
||||
size_t memoryUsage() const {
|
||||
return _memoryPool.size();
|
||||
}
|
||||
|
||||
template <typename Visitor>
|
||||
void accept(Visitor& visitor) const {
|
||||
return getVariant().accept(visitor);
|
||||
}
|
||||
|
||||
DynamicMemoryPool& memoryPool() {
|
||||
return _memoryPool;
|
||||
}
|
||||
|
||||
private:
|
||||
JsonVariant getVariant() const {
|
||||
return JsonVariant(&_memoryPool, &_rootData);
|
||||
}
|
||||
|
||||
mutable DynamicMemoryPool _memoryPool;
|
||||
mutable JsonVariantData _rootData;
|
||||
};
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
@ -6,20 +6,17 @@
|
||||
|
||||
#include "Data/JsonVariantTo.hpp"
|
||||
#include "JsonVariant.hpp"
|
||||
#include "Memory/DynamicMemoryPool.hpp"
|
||||
#include "Memory/StaticMemoryPool.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
template <size_t CAPACITY>
|
||||
class StaticJsonDocument {
|
||||
template <typename TMemoryPool>
|
||||
class JsonDocument {
|
||||
public:
|
||||
uint8_t nestingLimit;
|
||||
|
||||
StaticJsonDocument() : nestingLimit(ARDUINOJSON_DEFAULT_NESTING_LIMIT) {}
|
||||
|
||||
StaticMemoryPoolBase& memoryPool() {
|
||||
return _memoryPool;
|
||||
}
|
||||
JsonDocument() : nestingLimit(ARDUINOJSON_DEFAULT_NESTING_LIMIT) {}
|
||||
|
||||
template <typename T>
|
||||
bool is() const {
|
||||
@ -51,13 +48,33 @@ class StaticJsonDocument {
|
||||
return getVariant().accept(visitor);
|
||||
}
|
||||
|
||||
TMemoryPool& memoryPool() {
|
||||
return _memoryPool;
|
||||
}
|
||||
|
||||
private:
|
||||
JsonVariant getVariant() const {
|
||||
return JsonVariant(&_memoryPool, &_rootData);
|
||||
}
|
||||
|
||||
mutable StaticMemoryPool<CAPACITY> _memoryPool;
|
||||
mutable TMemoryPool _memoryPool;
|
||||
mutable JsonVariantData _rootData;
|
||||
};
|
||||
|
||||
class DynamicJsonDocument : public JsonDocument<DynamicMemoryPool> {
|
||||
public:
|
||||
DynamicJsonDocument() {}
|
||||
DynamicJsonDocument(size_t capacity) {
|
||||
memoryPool().reserve(capacity);
|
||||
}
|
||||
};
|
||||
|
||||
template <size_t CAPACITY>
|
||||
class StaticJsonDocument : public JsonDocument<StaticMemoryPool<CAPACITY> > {
|
||||
public:
|
||||
StaticMemoryPoolBase& memoryPool() {
|
||||
return JsonDocument<StaticMemoryPool<CAPACITY> >::memoryPool();
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
@ -7,7 +7,7 @@
|
||||
#include "../Strings/StringInMemoryPool.hpp"
|
||||
#include "MemoryPool.hpp"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdlib.h> // malloc, free
|
||||
|
||||
#if defined(__clang__)
|
||||
#pragma clang diagnostic push
|
||||
@ -52,6 +52,10 @@ class DynamicMemoryPoolBase : public MemoryPool {
|
||||
clear();
|
||||
}
|
||||
|
||||
void reserve(size_t capacity) {
|
||||
_nextBlockCapacity = capacity;
|
||||
}
|
||||
|
||||
// Gets the number of bytes occupied in the memoryPool
|
||||
size_t size() const {
|
||||
size_t total = 0;
|
||||
|
@ -40,9 +40,6 @@ class StaticMemoryPoolBase : public MemoryPool {
|
||||
char* _start;
|
||||
};
|
||||
|
||||
StaticMemoryPoolBase(char* memoryPool, size_t capa)
|
||||
: _buffer(memoryPool), _capacity(capa), _size(0) {}
|
||||
|
||||
// Gets the capacity of the memoryPool in bytes
|
||||
size_t capacity() const {
|
||||
return _capacity;
|
||||
@ -71,6 +68,9 @@ class StaticMemoryPoolBase : public MemoryPool {
|
||||
}
|
||||
|
||||
protected:
|
||||
StaticMemoryPoolBase(char* memoryPool, size_t capa)
|
||||
: _buffer(memoryPool), _capacity(capa), _size(0) {}
|
||||
|
||||
~StaticMemoryPoolBase() {}
|
||||
|
||||
private:
|
||||
|
@ -4,8 +4,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h> // for size_t
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h> // for size_t
|
||||
#include "../Configuration.hpp"
|
||||
#include "../Polyfills/math.hpp"
|
||||
|
||||
|
@ -4,8 +4,6 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "../Configuration.hpp"
|
||||
#include "../Polyfills/ctype.hpp"
|
||||
|
||||
|
@ -4,7 +4,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdlib.h> // for size_t
|
||||
#include <stddef.h> // for size_t
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
|
@ -4,7 +4,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdlib.h> // size_t
|
||||
#include <stddef.h> // size_t
|
||||
#include <string.h> // strcmp
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
Reference in New Issue
Block a user