forked from bblanchon/ArduinoJson
Change naming convention from _member
to member_
(fixes #1905)
Ported from 31ce648e63
This commit is contained in:
@ -62,12 +62,12 @@ class AllocatorLog {
|
|||||||
};
|
};
|
||||||
|
|
||||||
AllocatorLog& operator<<(const std::string& s) {
|
AllocatorLog& operator<<(const std::string& s) {
|
||||||
_log << s << "\n";
|
log_ << s << "\n";
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string str() const {
|
std::string str() const {
|
||||||
auto s = _log.str();
|
auto s = log_.str();
|
||||||
if (s.empty())
|
if (s.empty())
|
||||||
return "(empty)";
|
return "(empty)";
|
||||||
s.pop_back(); // remove the trailing '\n'
|
s.pop_back(); // remove the trailing '\n'
|
||||||
@ -84,57 +84,57 @@ class AllocatorLog {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::ostringstream _log;
|
std::ostringstream log_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class SpyingAllocator : public ArduinoJson::Allocator {
|
class SpyingAllocator : public ArduinoJson::Allocator {
|
||||||
public:
|
public:
|
||||||
SpyingAllocator(
|
SpyingAllocator(
|
||||||
Allocator* upstream = ArduinoJson::detail::DefaultAllocator::instance())
|
Allocator* upstream = ArduinoJson::detail::DefaultAllocator::instance())
|
||||||
: _upstream(upstream) {}
|
: upstream_(upstream) {}
|
||||||
virtual ~SpyingAllocator() {}
|
virtual ~SpyingAllocator() {}
|
||||||
|
|
||||||
void* allocate(size_t n) override {
|
void* allocate(size_t n) override {
|
||||||
auto block = reinterpret_cast<AllocatedBlock*>(
|
auto block = reinterpret_cast<AllocatedBlock*>(
|
||||||
_upstream->allocate(sizeof(AllocatedBlock) + n - 1));
|
upstream_->allocate(sizeof(AllocatedBlock) + n - 1));
|
||||||
if (block) {
|
if (block) {
|
||||||
_log << AllocatorLog::Allocate(n);
|
log_ << AllocatorLog::Allocate(n);
|
||||||
block->size = n;
|
block->size = n;
|
||||||
return block->payload;
|
return block->payload;
|
||||||
} else {
|
} else {
|
||||||
_log << AllocatorLog::AllocateFail(n);
|
log_ << AllocatorLog::AllocateFail(n);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void deallocate(void* p) override {
|
void deallocate(void* p) override {
|
||||||
auto block = AllocatedBlock::fromPayload(p);
|
auto block = AllocatedBlock::fromPayload(p);
|
||||||
_log << AllocatorLog::Deallocate(block->size);
|
log_ << AllocatorLog::Deallocate(block->size);
|
||||||
_upstream->deallocate(block);
|
upstream_->deallocate(block);
|
||||||
}
|
}
|
||||||
|
|
||||||
void* reallocate(void* p, size_t n) override {
|
void* reallocate(void* p, size_t n) override {
|
||||||
auto block = AllocatedBlock::fromPayload(p);
|
auto block = AllocatedBlock::fromPayload(p);
|
||||||
auto oldSize = block->size;
|
auto oldSize = block->size;
|
||||||
block = reinterpret_cast<AllocatedBlock*>(
|
block = reinterpret_cast<AllocatedBlock*>(
|
||||||
_upstream->reallocate(block, sizeof(AllocatedBlock) + n - 1));
|
upstream_->reallocate(block, sizeof(AllocatedBlock) + n - 1));
|
||||||
if (block) {
|
if (block) {
|
||||||
_log << AllocatorLog::Reallocate(oldSize, n);
|
log_ << AllocatorLog::Reallocate(oldSize, n);
|
||||||
ARDUINOJSON_ASSERT(block->size == oldSize);
|
ARDUINOJSON_ASSERT(block->size == oldSize);
|
||||||
block->size = n;
|
block->size = n;
|
||||||
return block->payload;
|
return block->payload;
|
||||||
} else {
|
} else {
|
||||||
_log << AllocatorLog::ReallocateFail(oldSize, n);
|
log_ << AllocatorLog::ReallocateFail(oldSize, n);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void clearLog() {
|
void clearLog() {
|
||||||
_log = AllocatorLog();
|
log_ = AllocatorLog();
|
||||||
}
|
}
|
||||||
|
|
||||||
const AllocatorLog& log() const {
|
const AllocatorLog& log() const {
|
||||||
return _log;
|
return log_;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -151,36 +151,36 @@ class SpyingAllocator : public ArduinoJson::Allocator {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
AllocatorLog _log;
|
AllocatorLog log_;
|
||||||
Allocator* _upstream;
|
Allocator* upstream_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class ControllableAllocator : public ArduinoJson::Allocator {
|
class ControllableAllocator : public ArduinoJson::Allocator {
|
||||||
public:
|
public:
|
||||||
ControllableAllocator(
|
ControllableAllocator(
|
||||||
Allocator* upstream = ArduinoJson::detail::DefaultAllocator::instance())
|
Allocator* upstream = ArduinoJson::detail::DefaultAllocator::instance())
|
||||||
: _enabled(true), _upstream(upstream) {}
|
: enabled_(true), upstream_(upstream) {}
|
||||||
virtual ~ControllableAllocator() {}
|
virtual ~ControllableAllocator() {}
|
||||||
|
|
||||||
void* allocate(size_t n) override {
|
void* allocate(size_t n) override {
|
||||||
return _enabled ? _upstream->allocate(n) : 0;
|
return enabled_ ? upstream_->allocate(n) : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void deallocate(void* p) override {
|
void deallocate(void* p) override {
|
||||||
_upstream->deallocate(p);
|
upstream_->deallocate(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
void* reallocate(void* ptr, size_t n) override {
|
void* reallocate(void* ptr, size_t n) override {
|
||||||
return _enabled ? _upstream->reallocate(ptr, n) : 0;
|
return enabled_ ? upstream_->reallocate(ptr, n) : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void disable() {
|
void disable() {
|
||||||
_enabled = false;
|
enabled_ = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool _enabled;
|
bool enabled_;
|
||||||
Allocator* _upstream;
|
Allocator* upstream_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class TimebombAllocator : public ArduinoJson::Allocator {
|
class TimebombAllocator : public ArduinoJson::Allocator {
|
||||||
@ -188,32 +188,32 @@ class TimebombAllocator : public ArduinoJson::Allocator {
|
|||||||
TimebombAllocator(
|
TimebombAllocator(
|
||||||
size_t initialCountdown,
|
size_t initialCountdown,
|
||||||
Allocator* upstream = ArduinoJson::detail::DefaultAllocator::instance())
|
Allocator* upstream = ArduinoJson::detail::DefaultAllocator::instance())
|
||||||
: _countdown(initialCountdown), _upstream(upstream) {}
|
: countdown_(initialCountdown), upstream_(upstream) {}
|
||||||
virtual ~TimebombAllocator() {}
|
virtual ~TimebombAllocator() {}
|
||||||
|
|
||||||
void* allocate(size_t n) override {
|
void* allocate(size_t n) override {
|
||||||
if (!_countdown)
|
if (!countdown_)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
_countdown--;
|
countdown_--;
|
||||||
return _upstream->allocate(n);
|
return upstream_->allocate(n);
|
||||||
}
|
}
|
||||||
|
|
||||||
void deallocate(void* p) override {
|
void deallocate(void* p) override {
|
||||||
_upstream->deallocate(p);
|
upstream_->deallocate(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
void* reallocate(void* ptr, size_t n) override {
|
void* reallocate(void* ptr, size_t n) override {
|
||||||
if (!_countdown)
|
if (!countdown_)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
_countdown--;
|
countdown_--;
|
||||||
return _upstream->reallocate(ptr, n);
|
return upstream_->reallocate(ptr, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setCountdown(size_t value) {
|
void setCountdown(size_t value) {
|
||||||
_countdown = value;
|
countdown_ = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
size_t _countdown = 0;
|
size_t countdown_ = 0;
|
||||||
Allocator* _upstream;
|
Allocator* upstream_;
|
||||||
};
|
};
|
||||||
|
@ -7,18 +7,18 @@
|
|||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
class CustomReader {
|
class CustomReader {
|
||||||
std::stringstream _stream;
|
std::stringstream stream_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
CustomReader(const char* input) : _stream(input) {}
|
CustomReader(const char* input) : stream_(input) {}
|
||||||
CustomReader(const CustomReader&) = delete;
|
CustomReader(const CustomReader&) = delete;
|
||||||
|
|
||||||
int read() {
|
int read() {
|
||||||
return _stream.get();
|
return stream_.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t readBytes(char* buffer, size_t length) {
|
size_t readBytes(char* buffer, size_t length) {
|
||||||
_stream.read(buffer, static_cast<std::streamsize>(length));
|
stream_.read(buffer, static_cast<std::streamsize>(length));
|
||||||
return static_cast<size_t>(_stream.gcount());
|
return static_cast<size_t>(stream_.gcount());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -9,11 +9,11 @@
|
|||||||
// Reproduces Arduino's String class
|
// Reproduces Arduino's String class
|
||||||
class String {
|
class String {
|
||||||
public:
|
public:
|
||||||
String() : _maxCapacity(1024) {}
|
String() : maxCapacity_(1024) {}
|
||||||
explicit String(const char* s) : _str(s), _maxCapacity(1024) {}
|
explicit String(const char* s) : str_(s), maxCapacity_(1024) {}
|
||||||
|
|
||||||
void limitCapacityTo(size_t maxCapacity) {
|
void limitCapacityTo(size_t maxCapacity) {
|
||||||
_maxCapacity = maxCapacity;
|
maxCapacity_ = maxCapacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char concat(const char* s) {
|
unsigned char concat(const char* s) {
|
||||||
@ -21,45 +21,45 @@ class String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
size_t length() const {
|
size_t length() const {
|
||||||
return _str.size();
|
return str_.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* c_str() const {
|
const char* c_str() const {
|
||||||
return _str.c_str();
|
return str_.c_str();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator==(const char* s) const {
|
bool operator==(const char* s) const {
|
||||||
return _str == s;
|
return str_ == s;
|
||||||
}
|
}
|
||||||
|
|
||||||
String& operator=(const char* s) {
|
String& operator=(const char* s) {
|
||||||
_str.assign(s);
|
str_.assign(s);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
char operator[](unsigned int index) const {
|
char operator[](unsigned int index) const {
|
||||||
if (index >= _str.size())
|
if (index >= str_.size())
|
||||||
return 0;
|
return 0;
|
||||||
return _str[index];
|
return str_[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
friend std::ostream& operator<<(std::ostream& lhs, const ::String& rhs) {
|
friend std::ostream& operator<<(std::ostream& lhs, const ::String& rhs) {
|
||||||
lhs << rhs._str;
|
lhs << rhs.str_;
|
||||||
return lhs;
|
return lhs;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// This function is protected in most Arduino cores
|
// This function is protected in most Arduino cores
|
||||||
unsigned char concat(const char* s, size_t n) {
|
unsigned char concat(const char* s, size_t n) {
|
||||||
if (_str.size() + n > _maxCapacity)
|
if (str_.size() + n > maxCapacity_)
|
||||||
return 0;
|
return 0;
|
||||||
_str.append(s, n);
|
str_.append(s, n);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string _str;
|
std::string str_;
|
||||||
size_t _maxCapacity;
|
size_t maxCapacity_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class StringSumHelper : public ::String {};
|
class StringSumHelper : public ::String {};
|
||||||
|
@ -12,21 +12,21 @@ class CustomWriter {
|
|||||||
CustomWriter& operator=(const CustomWriter&) = delete;
|
CustomWriter& operator=(const CustomWriter&) = delete;
|
||||||
|
|
||||||
size_t write(uint8_t c) {
|
size_t write(uint8_t c) {
|
||||||
_str.append(1, static_cast<char>(c));
|
str_.append(1, static_cast<char>(c));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t write(const uint8_t* s, size_t n) {
|
size_t write(const uint8_t* s, size_t n) {
|
||||||
_str.append(reinterpret_cast<const char*>(s), n);
|
str_.append(reinterpret_cast<const char*>(s), n);
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string& str() const {
|
const std::string& str() const {
|
||||||
return _str;
|
return str_;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string _str;
|
std::string str_;
|
||||||
};
|
};
|
||||||
|
|
||||||
TEST_CASE("CustomWriter") {
|
TEST_CASE("CustomWriter") {
|
||||||
|
@ -74,18 +74,18 @@ TEST_CASE("Custom converter with overloading") {
|
|||||||
|
|
||||||
class Complex {
|
class Complex {
|
||||||
public:
|
public:
|
||||||
explicit Complex(double r, double i) : _real(r), _imag(i) {}
|
explicit Complex(double r, double i) : real_(r), imag_(i) {}
|
||||||
|
|
||||||
double real() const {
|
double real() const {
|
||||||
return _real;
|
return real_;
|
||||||
}
|
}
|
||||||
|
|
||||||
double imag() const {
|
double imag() const {
|
||||||
return _imag;
|
return imag_;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
double _real, _imag;
|
double real_, imag_;
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace ArduinoJson {
|
namespace ArduinoJson {
|
||||||
|
@ -170,19 +170,19 @@ TEST_CASE("IteratorReader") {
|
|||||||
|
|
||||||
class StreamStub : public Stream {
|
class StreamStub : public Stream {
|
||||||
public:
|
public:
|
||||||
StreamStub(const char* s) : _stream(s) {}
|
StreamStub(const char* s) : stream_(s) {}
|
||||||
|
|
||||||
int read() {
|
int read() {
|
||||||
return _stream.get();
|
return stream_.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t readBytes(char* buffer, size_t length) {
|
size_t readBytes(char* buffer, size_t length) {
|
||||||
_stream.read(buffer, static_cast<std::streamsize>(length));
|
stream_.read(buffer, static_cast<std::streamsize>(length));
|
||||||
return static_cast<size_t>(_stream.gcount());
|
return static_cast<size_t>(stream_.gcount());
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::istringstream _stream;
|
std::istringstream stream_;
|
||||||
};
|
};
|
||||||
|
|
||||||
TEST_CASE("Reader<Stream>") {
|
TEST_CASE("Reader<Stream>") {
|
||||||
|
@ -52,5 +52,8 @@
|
|||||||
#define BLOCKSIZE
|
#define BLOCKSIZE
|
||||||
#define CAPACITY
|
#define CAPACITY
|
||||||
|
|
||||||
|
// issue #1905
|
||||||
|
#define _current
|
||||||
|
|
||||||
// catch.hpp mutes several warnings, this file also allows to detect them
|
// catch.hpp mutes several warnings, this file also allows to detect them
|
||||||
#include "ArduinoJson.h"
|
#include "ArduinoJson.h"
|
||||||
|
@ -34,21 +34,21 @@ struct PrintAllAtOnce {
|
|||||||
|
|
||||||
template <typename PrintPolicy>
|
template <typename PrintPolicy>
|
||||||
struct PrintableString : public Printable {
|
struct PrintableString : public Printable {
|
||||||
PrintableString(const char* s) : _str(s), _total(0) {}
|
PrintableString(const char* s) : str_(s), total_(0) {}
|
||||||
|
|
||||||
virtual size_t printTo(Print& p) const {
|
virtual size_t printTo(Print& p) const {
|
||||||
size_t result = PrintPolicy::printStringTo(_str, p);
|
size_t result = PrintPolicy::printStringTo(str_, p);
|
||||||
_total += result;
|
total_ += result;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t totalBytesWritten() const {
|
size_t totalBytesWritten() const {
|
||||||
return _total;
|
return total_;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string _str;
|
std::string str_;
|
||||||
mutable size_t _total;
|
mutable size_t total_;
|
||||||
};
|
};
|
||||||
|
|
||||||
TEST_CASE("Printable") {
|
TEST_CASE("Printable") {
|
||||||
|
@ -17,10 +17,10 @@ class ElementProxy : public VariantRefBase<ElementProxy<TUpstream>>,
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
ElementProxy(TUpstream upstream, size_t index)
|
ElementProxy(TUpstream upstream, size_t index)
|
||||||
: _upstream(upstream), _index(index) {}
|
: upstream_(upstream), index_(index) {}
|
||||||
|
|
||||||
ElementProxy(const ElementProxy& src)
|
ElementProxy(const ElementProxy& src)
|
||||||
: _upstream(src._upstream), _index(src._index) {}
|
: upstream_(src.upstream_), index_(src.index_) {}
|
||||||
|
|
||||||
FORCE_INLINE ElementProxy& operator=(const ElementProxy& src) {
|
FORCE_INLINE ElementProxy& operator=(const ElementProxy& src) {
|
||||||
this->set(src);
|
this->set(src);
|
||||||
@ -41,20 +41,20 @@ class ElementProxy : public VariantRefBase<ElementProxy<TUpstream>>,
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
FORCE_INLINE MemoryPool* getPool() const {
|
FORCE_INLINE MemoryPool* getPool() const {
|
||||||
return VariantAttorney::getPool(_upstream);
|
return VariantAttorney::getPool(upstream_);
|
||||||
}
|
}
|
||||||
|
|
||||||
FORCE_INLINE VariantData* getData() const {
|
FORCE_INLINE VariantData* getData() const {
|
||||||
return variantGetElement(VariantAttorney::getData(_upstream), _index);
|
return variantGetElement(VariantAttorney::getData(upstream_), index_);
|
||||||
}
|
}
|
||||||
|
|
||||||
FORCE_INLINE VariantData* getOrCreateData() const {
|
FORCE_INLINE VariantData* getOrCreateData() const {
|
||||||
return variantGetOrAddElement(VariantAttorney::getOrCreateData(_upstream),
|
return variantGetOrAddElement(VariantAttorney::getOrCreateData(upstream_),
|
||||||
_index, VariantAttorney::getPool(_upstream));
|
index_, VariantAttorney::getPool(upstream_));
|
||||||
}
|
}
|
||||||
|
|
||||||
TUpstream _upstream;
|
TUpstream upstream_;
|
||||||
size_t _index;
|
size_t index_;
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -21,30 +21,30 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
|
|||||||
typedef JsonArrayIterator iterator;
|
typedef JsonArrayIterator iterator;
|
||||||
|
|
||||||
// Constructs an unbound reference.
|
// Constructs an unbound reference.
|
||||||
FORCE_INLINE JsonArray() : _data(0), _pool(0) {}
|
FORCE_INLINE JsonArray() : data_(0), pool_(0) {}
|
||||||
|
|
||||||
// INTERNAL USE ONLY
|
// INTERNAL USE ONLY
|
||||||
FORCE_INLINE JsonArray(detail::MemoryPool* pool, detail::CollectionData* data)
|
FORCE_INLINE JsonArray(detail::MemoryPool* pool, detail::CollectionData* data)
|
||||||
: _data(data), _pool(pool) {}
|
: data_(data), pool_(pool) {}
|
||||||
|
|
||||||
// Returns a JsonVariant pointing to the array.
|
// Returns a JsonVariant pointing to the array.
|
||||||
// https://arduinojson.org/v6/api/jsonvariant/
|
// https://arduinojson.org/v6/api/jsonvariant/
|
||||||
operator JsonVariant() {
|
operator JsonVariant() {
|
||||||
void* data = _data; // prevent warning cast-align
|
void* data = data_; // prevent warning cast-align
|
||||||
return JsonVariant(_pool, reinterpret_cast<detail::VariantData*>(data));
|
return JsonVariant(pool_, reinterpret_cast<detail::VariantData*>(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns a read-only reference to the array.
|
// Returns a read-only reference to the array.
|
||||||
// https://arduinojson.org/v6/api/jsonarrayconst/
|
// https://arduinojson.org/v6/api/jsonarrayconst/
|
||||||
operator JsonArrayConst() const {
|
operator JsonArrayConst() const {
|
||||||
return JsonArrayConst(_data);
|
return JsonArrayConst(data_);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Appends a new (null) element to the array.
|
// Appends a new (null) element to the array.
|
||||||
// Returns a reference to the new element.
|
// Returns a reference to the new element.
|
||||||
// https://arduinojson.org/v6/api/jsonarray/add/
|
// https://arduinojson.org/v6/api/jsonarray/add/
|
||||||
JsonVariant add() const {
|
JsonVariant add() const {
|
||||||
return JsonVariant(_pool, collectionAddElement(_data, _pool));
|
return JsonVariant(pool_, collectionAddElement(data_, pool_));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Appends a value to the array.
|
// Appends a value to the array.
|
||||||
@ -64,9 +64,9 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
|
|||||||
// Returns an iterator to the first element of the array.
|
// Returns an iterator to the first element of the array.
|
||||||
// https://arduinojson.org/v6/api/jsonarray/begin/
|
// https://arduinojson.org/v6/api/jsonarray/begin/
|
||||||
FORCE_INLINE iterator begin() const {
|
FORCE_INLINE iterator begin() const {
|
||||||
if (!_data)
|
if (!data_)
|
||||||
return iterator();
|
return iterator();
|
||||||
return iterator(_pool, _data->head());
|
return iterator(pool_, data_->head());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns an iterator following the last element of the array.
|
// Returns an iterator following the last element of the array.
|
||||||
@ -78,33 +78,33 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
|
|||||||
// Copies an array.
|
// Copies an array.
|
||||||
// https://arduinojson.org/v6/api/jsonarray/set/
|
// https://arduinojson.org/v6/api/jsonarray/set/
|
||||||
FORCE_INLINE bool set(JsonArrayConst src) const {
|
FORCE_INLINE bool set(JsonArrayConst src) const {
|
||||||
return collectionCopy(_data, src._data, _pool);
|
return collectionCopy(data_, src.data_, pool_);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compares the content of two arrays.
|
// Compares the content of two arrays.
|
||||||
FORCE_INLINE bool operator==(JsonArray rhs) const {
|
FORCE_INLINE bool operator==(JsonArray rhs) const {
|
||||||
return JsonArrayConst(_data) == JsonArrayConst(rhs._data);
|
return JsonArrayConst(data_) == JsonArrayConst(rhs.data_);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Removes the element at the specified iterator.
|
// Removes the element at the specified iterator.
|
||||||
// ⚠️ Doesn't release the memory associated with the removed element.
|
// ⚠️ Doesn't release the memory associated with the removed element.
|
||||||
// https://arduinojson.org/v6/api/jsonarray/remove/
|
// https://arduinojson.org/v6/api/jsonarray/remove/
|
||||||
FORCE_INLINE void remove(iterator it) const {
|
FORCE_INLINE void remove(iterator it) const {
|
||||||
collectionRemove(_data, it._slot, _pool);
|
collectionRemove(data_, it.slot_, pool_);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Removes the element at the specified index.
|
// Removes the element at the specified index.
|
||||||
// ⚠️ Doesn't release the memory associated with the removed element.
|
// ⚠️ Doesn't release the memory associated with the removed element.
|
||||||
// https://arduinojson.org/v6/api/jsonarray/remove/
|
// https://arduinojson.org/v6/api/jsonarray/remove/
|
||||||
FORCE_INLINE void remove(size_t index) const {
|
FORCE_INLINE void remove(size_t index) const {
|
||||||
collectionRemoveElement(_data, index, _pool);
|
collectionRemoveElement(data_, index, pool_);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Removes all the elements of the array.
|
// Removes all the elements of the array.
|
||||||
// ⚠️ Doesn't release the memory associated with the removed elements.
|
// ⚠️ Doesn't release the memory associated with the removed elements.
|
||||||
// https://arduinojson.org/v6/api/jsonarray/clear/
|
// https://arduinojson.org/v6/api/jsonarray/clear/
|
||||||
void clear() const {
|
void clear() const {
|
||||||
collectionClear(_data, _pool);
|
collectionClear(data_, pool_);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gets or sets the element at the specified index.
|
// Gets or sets the element at the specified index.
|
||||||
@ -124,54 +124,54 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
operator JsonVariantConst() const {
|
operator JsonVariantConst() const {
|
||||||
return JsonVariantConst(collectionToVariant(_data));
|
return JsonVariantConst(collectionToVariant(data_));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if the reference is unbound.
|
// Returns true if the reference is unbound.
|
||||||
// https://arduinojson.org/v6/api/jsonarray/isnull/
|
// https://arduinojson.org/v6/api/jsonarray/isnull/
|
||||||
FORCE_INLINE bool isNull() const {
|
FORCE_INLINE bool isNull() const {
|
||||||
return _data == 0;
|
return data_ == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if the reference is bound.
|
// Returns true if the reference is bound.
|
||||||
// https://arduinojson.org/v6/api/jsonarray/isnull/
|
// https://arduinojson.org/v6/api/jsonarray/isnull/
|
||||||
FORCE_INLINE operator bool() const {
|
FORCE_INLINE operator bool() const {
|
||||||
return _data != 0;
|
return data_ != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the number of bytes occupied by the array.
|
// Returns the number of bytes occupied by the array.
|
||||||
// https://arduinojson.org/v6/api/jsonarray/memoryusage/
|
// https://arduinojson.org/v6/api/jsonarray/memoryusage/
|
||||||
FORCE_INLINE size_t memoryUsage() const {
|
FORCE_INLINE size_t memoryUsage() const {
|
||||||
return _data ? _data->memoryUsage() : 0;
|
return data_ ? data_->memoryUsage() : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the depth (nesting level) of the array.
|
// Returns the depth (nesting level) of the array.
|
||||||
// https://arduinojson.org/v6/api/jsonarray/nesting/
|
// https://arduinojson.org/v6/api/jsonarray/nesting/
|
||||||
FORCE_INLINE size_t nesting() const {
|
FORCE_INLINE size_t nesting() const {
|
||||||
return variantNesting(collectionToVariant(_data));
|
return variantNesting(collectionToVariant(data_));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the number of elements in the array.
|
// Returns the number of elements in the array.
|
||||||
// https://arduinojson.org/v6/api/jsonarray/size/
|
// https://arduinojson.org/v6/api/jsonarray/size/
|
||||||
FORCE_INLINE size_t size() const {
|
FORCE_INLINE size_t size() const {
|
||||||
return _data ? _data->size() : 0;
|
return data_ ? data_->size() : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
detail::MemoryPool* getPool() const {
|
detail::MemoryPool* getPool() const {
|
||||||
return _pool;
|
return pool_;
|
||||||
}
|
}
|
||||||
|
|
||||||
detail::VariantData* getData() const {
|
detail::VariantData* getData() const {
|
||||||
return collectionToVariant(_data);
|
return collectionToVariant(data_);
|
||||||
}
|
}
|
||||||
|
|
||||||
detail::VariantData* getOrCreateData() const {
|
detail::VariantData* getOrCreateData() const {
|
||||||
return collectionToVariant(_data);
|
return collectionToVariant(data_);
|
||||||
}
|
}
|
||||||
|
|
||||||
detail::CollectionData* _data;
|
detail::CollectionData* data_;
|
||||||
detail::MemoryPool* _pool;
|
detail::MemoryPool* pool_;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
|
@ -24,9 +24,9 @@ class JsonArrayConst : public detail::VariantOperators<JsonArrayConst> {
|
|||||||
// Returns an iterator to the first element of the array.
|
// Returns an iterator to the first element of the array.
|
||||||
// https://arduinojson.org/v6/api/jsonarrayconst/begin/
|
// https://arduinojson.org/v6/api/jsonarrayconst/begin/
|
||||||
FORCE_INLINE iterator begin() const {
|
FORCE_INLINE iterator begin() const {
|
||||||
if (!_data)
|
if (!data_)
|
||||||
return iterator();
|
return iterator();
|
||||||
return iterator(_data->head());
|
return iterator(data_->head());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns an iterator to the element following the last element of the array.
|
// Returns an iterator to the element following the last element of the array.
|
||||||
@ -36,18 +36,18 @@ class JsonArrayConst : public detail::VariantOperators<JsonArrayConst> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Creates an unbound reference.
|
// Creates an unbound reference.
|
||||||
FORCE_INLINE JsonArrayConst() : _data(0) {}
|
FORCE_INLINE JsonArrayConst() : data_(0) {}
|
||||||
|
|
||||||
// INTERNAL USE ONLY
|
// INTERNAL USE ONLY
|
||||||
FORCE_INLINE JsonArrayConst(const detail::CollectionData* data)
|
FORCE_INLINE JsonArrayConst(const detail::CollectionData* data)
|
||||||
: _data(data) {}
|
: data_(data) {}
|
||||||
|
|
||||||
// Compares the content of two arrays.
|
// Compares the content of two arrays.
|
||||||
// Returns true if the two arrays are equal.
|
// Returns true if the two arrays are equal.
|
||||||
FORCE_INLINE bool operator==(JsonArrayConst rhs) const {
|
FORCE_INLINE bool operator==(JsonArrayConst rhs) const {
|
||||||
if (_data == rhs._data)
|
if (data_ == rhs.data_)
|
||||||
return true;
|
return true;
|
||||||
if (!_data || !rhs._data)
|
if (!data_ || !rhs.data_)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
iterator it1 = begin();
|
iterator it1 = begin();
|
||||||
@ -70,49 +70,49 @@ class JsonArrayConst : public detail::VariantOperators<JsonArrayConst> {
|
|||||||
// Returns the element at the specified index.
|
// Returns the element at the specified index.
|
||||||
// https://arduinojson.org/v6/api/jsonarrayconst/subscript/
|
// https://arduinojson.org/v6/api/jsonarrayconst/subscript/
|
||||||
FORCE_INLINE JsonVariantConst operator[](size_t index) const {
|
FORCE_INLINE JsonVariantConst operator[](size_t index) const {
|
||||||
return JsonVariantConst(_data ? slotData(_data->get(index)) : 0);
|
return JsonVariantConst(data_ ? slotData(data_->get(index)) : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
operator JsonVariantConst() const {
|
operator JsonVariantConst() const {
|
||||||
return JsonVariantConst(collectionToVariant(_data));
|
return JsonVariantConst(collectionToVariant(data_));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if the reference is unbound.
|
// Returns true if the reference is unbound.
|
||||||
// https://arduinojson.org/v6/api/jsonarrayconst/isnull/
|
// https://arduinojson.org/v6/api/jsonarrayconst/isnull/
|
||||||
FORCE_INLINE bool isNull() const {
|
FORCE_INLINE bool isNull() const {
|
||||||
return _data == 0;
|
return data_ == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if the reference is bound.
|
// Returns true if the reference is bound.
|
||||||
// https://arduinojson.org/v6/api/jsonarrayconst/isnull/
|
// https://arduinojson.org/v6/api/jsonarrayconst/isnull/
|
||||||
FORCE_INLINE operator bool() const {
|
FORCE_INLINE operator bool() const {
|
||||||
return _data != 0;
|
return data_ != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the number of bytes occupied by the array.
|
// Returns the number of bytes occupied by the array.
|
||||||
// https://arduinojson.org/v6/api/jsonarrayconst/memoryusage/
|
// https://arduinojson.org/v6/api/jsonarrayconst/memoryusage/
|
||||||
FORCE_INLINE size_t memoryUsage() const {
|
FORCE_INLINE size_t memoryUsage() const {
|
||||||
return _data ? _data->memoryUsage() : 0;
|
return data_ ? data_->memoryUsage() : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the depth (nesting level) of the array.
|
// Returns the depth (nesting level) of the array.
|
||||||
// https://arduinojson.org/v6/api/jsonarrayconst/nesting/
|
// https://arduinojson.org/v6/api/jsonarrayconst/nesting/
|
||||||
FORCE_INLINE size_t nesting() const {
|
FORCE_INLINE size_t nesting() const {
|
||||||
return variantNesting(collectionToVariant(_data));
|
return variantNesting(collectionToVariant(data_));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the number of elements in the array.
|
// Returns the number of elements in the array.
|
||||||
// https://arduinojson.org/v6/api/jsonarrayconst/size/
|
// https://arduinojson.org/v6/api/jsonarrayconst/size/
|
||||||
FORCE_INLINE size_t size() const {
|
FORCE_INLINE size_t size() const {
|
||||||
return _data ? _data->size() : 0;
|
return data_ ? data_->size() : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const detail::VariantData* getData() const {
|
const detail::VariantData* getData() const {
|
||||||
return collectionToVariant(_data);
|
return collectionToVariant(data_);
|
||||||
}
|
}
|
||||||
|
|
||||||
const detail::CollectionData* _data;
|
const detail::CollectionData* data_;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
|
@ -12,110 +12,110 @@ ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE
|
|||||||
class VariantPtr {
|
class VariantPtr {
|
||||||
public:
|
public:
|
||||||
VariantPtr(detail::MemoryPool* pool, detail::VariantData* data)
|
VariantPtr(detail::MemoryPool* pool, detail::VariantData* data)
|
||||||
: _variant(pool, data) {}
|
: variant_(pool, data) {}
|
||||||
|
|
||||||
JsonVariant* operator->() {
|
JsonVariant* operator->() {
|
||||||
return &_variant;
|
return &variant_;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonVariant& operator*() {
|
JsonVariant& operator*() {
|
||||||
return _variant;
|
return variant_;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
JsonVariant _variant;
|
JsonVariant variant_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class JsonArrayIterator {
|
class JsonArrayIterator {
|
||||||
friend class JsonArray;
|
friend class JsonArray;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
JsonArrayIterator() : _slot(0) {}
|
JsonArrayIterator() : slot_(0) {}
|
||||||
explicit JsonArrayIterator(detail::MemoryPool* pool,
|
explicit JsonArrayIterator(detail::MemoryPool* pool,
|
||||||
detail::VariantSlot* slot)
|
detail::VariantSlot* slot)
|
||||||
: _pool(pool), _slot(slot) {}
|
: pool_(pool), slot_(slot) {}
|
||||||
|
|
||||||
JsonVariant operator*() const {
|
JsonVariant operator*() const {
|
||||||
return JsonVariant(_pool, _slot->data());
|
return JsonVariant(pool_, slot_->data());
|
||||||
}
|
}
|
||||||
VariantPtr operator->() {
|
VariantPtr operator->() {
|
||||||
return VariantPtr(_pool, _slot->data());
|
return VariantPtr(pool_, slot_->data());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator==(const JsonArrayIterator& other) const {
|
bool operator==(const JsonArrayIterator& other) const {
|
||||||
return _slot == other._slot;
|
return slot_ == other.slot_;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator!=(const JsonArrayIterator& other) const {
|
bool operator!=(const JsonArrayIterator& other) const {
|
||||||
return _slot != other._slot;
|
return slot_ != other.slot_;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonArrayIterator& operator++() {
|
JsonArrayIterator& operator++() {
|
||||||
_slot = _slot->next();
|
slot_ = slot_->next();
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonArrayIterator& operator+=(size_t distance) {
|
JsonArrayIterator& operator+=(size_t distance) {
|
||||||
_slot = _slot->next(distance);
|
slot_ = slot_->next(distance);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
detail::MemoryPool* _pool;
|
detail::MemoryPool* pool_;
|
||||||
detail::VariantSlot* _slot;
|
detail::VariantSlot* slot_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class VariantConstPtr {
|
class VariantConstPtr {
|
||||||
public:
|
public:
|
||||||
VariantConstPtr(const detail::VariantData* data) : _variant(data) {}
|
VariantConstPtr(const detail::VariantData* data) : variant_(data) {}
|
||||||
|
|
||||||
JsonVariantConst* operator->() {
|
JsonVariantConst* operator->() {
|
||||||
return &_variant;
|
return &variant_;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonVariantConst& operator*() {
|
JsonVariantConst& operator*() {
|
||||||
return _variant;
|
return variant_;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
JsonVariantConst _variant;
|
JsonVariantConst variant_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class JsonArrayConstIterator {
|
class JsonArrayConstIterator {
|
||||||
friend class JsonArray;
|
friend class JsonArray;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
JsonArrayConstIterator() : _slot(0) {}
|
JsonArrayConstIterator() : slot_(0) {}
|
||||||
explicit JsonArrayConstIterator(const detail::VariantSlot* slot)
|
explicit JsonArrayConstIterator(const detail::VariantSlot* slot)
|
||||||
: _slot(slot) {}
|
: slot_(slot) {}
|
||||||
|
|
||||||
JsonVariantConst operator*() const {
|
JsonVariantConst operator*() const {
|
||||||
return JsonVariantConst(_slot->data());
|
return JsonVariantConst(slot_->data());
|
||||||
}
|
}
|
||||||
VariantConstPtr operator->() {
|
VariantConstPtr operator->() {
|
||||||
return VariantConstPtr(_slot->data());
|
return VariantConstPtr(slot_->data());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator==(const JsonArrayConstIterator& other) const {
|
bool operator==(const JsonArrayConstIterator& other) const {
|
||||||
return _slot == other._slot;
|
return slot_ == other.slot_;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator!=(const JsonArrayConstIterator& other) const {
|
bool operator!=(const JsonArrayConstIterator& other) const {
|
||||||
return _slot != other._slot;
|
return slot_ != other.slot_;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonArrayConstIterator& operator++() {
|
JsonArrayConstIterator& operator++() {
|
||||||
_slot = _slot->next();
|
slot_ = slot_->next();
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonArrayConstIterator& operator+=(size_t distance) {
|
JsonArrayConstIterator& operator+=(size_t distance) {
|
||||||
_slot = _slot->next(distance);
|
slot_ = slot_->next(distance);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const detail::VariantSlot* _slot;
|
const detail::VariantSlot* slot_;
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
||||||
|
@ -15,8 +15,8 @@ class VariantData;
|
|||||||
class VariantSlot;
|
class VariantSlot;
|
||||||
|
|
||||||
class CollectionData {
|
class CollectionData {
|
||||||
VariantSlot* _head;
|
VariantSlot* head_;
|
||||||
VariantSlot* _tail;
|
VariantSlot* tail_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void clear();
|
void clear();
|
||||||
@ -27,7 +27,7 @@ class CollectionData {
|
|||||||
void remove(VariantSlot* slot);
|
void remove(VariantSlot* slot);
|
||||||
|
|
||||||
VariantSlot* head() const {
|
VariantSlot* head() const {
|
||||||
return _head;
|
return head_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void movePointers(ptrdiff_t variantDistance);
|
void movePointers(ptrdiff_t variantDistance);
|
||||||
|
@ -14,25 +14,25 @@ ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
|||||||
inline void CollectionData::add(VariantSlot* slot) {
|
inline void CollectionData::add(VariantSlot* slot) {
|
||||||
ARDUINOJSON_ASSERT(slot != nullptr);
|
ARDUINOJSON_ASSERT(slot != nullptr);
|
||||||
|
|
||||||
if (_tail) {
|
if (tail_) {
|
||||||
_tail->setNextNotNull(slot);
|
tail_->setNextNotNull(slot);
|
||||||
_tail = slot;
|
tail_ = slot;
|
||||||
} else {
|
} else {
|
||||||
_head = slot;
|
head_ = slot;
|
||||||
_tail = slot;
|
tail_ = slot;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void CollectionData::clear() {
|
inline void CollectionData::clear() {
|
||||||
_head = 0;
|
head_ = 0;
|
||||||
_tail = 0;
|
tail_ = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TAdaptedString>
|
template <typename TAdaptedString>
|
||||||
inline VariantSlot* CollectionData::get(TAdaptedString key) const {
|
inline VariantSlot* CollectionData::get(TAdaptedString key) const {
|
||||||
if (key.isNull())
|
if (key.isNull())
|
||||||
return 0;
|
return 0;
|
||||||
VariantSlot* slot = _head;
|
VariantSlot* slot = head_;
|
||||||
while (slot) {
|
while (slot) {
|
||||||
if (stringEquals(key, adaptString(slot->key())))
|
if (stringEquals(key, adaptString(slot->key())))
|
||||||
break;
|
break;
|
||||||
@ -42,13 +42,13 @@ inline VariantSlot* CollectionData::get(TAdaptedString key) const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
inline VariantSlot* CollectionData::get(size_t index) const {
|
inline VariantSlot* CollectionData::get(size_t index) const {
|
||||||
if (!_head)
|
if (!head_)
|
||||||
return 0;
|
return 0;
|
||||||
return _head->next(index);
|
return head_->next(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline VariantSlot* CollectionData::getPrevious(VariantSlot* target) const {
|
inline VariantSlot* CollectionData::getPrevious(VariantSlot* target) const {
|
||||||
VariantSlot* current = _head;
|
VariantSlot* current = head_;
|
||||||
while (current) {
|
while (current) {
|
||||||
VariantSlot* next = current->next();
|
VariantSlot* next = current->next();
|
||||||
if (next == target)
|
if (next == target)
|
||||||
@ -65,14 +65,14 @@ inline void CollectionData::remove(VariantSlot* slot) {
|
|||||||
if (prev)
|
if (prev)
|
||||||
prev->setNext(next);
|
prev->setNext(next);
|
||||||
else
|
else
|
||||||
_head = next;
|
head_ = next;
|
||||||
if (!next)
|
if (!next)
|
||||||
_tail = prev;
|
tail_ = prev;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline size_t CollectionData::memoryUsage() const {
|
inline size_t CollectionData::memoryUsage() const {
|
||||||
size_t total = 0;
|
size_t total = 0;
|
||||||
for (VariantSlot* s = _head; s; s = s->next()) {
|
for (VariantSlot* s = head_; s; s = s->next()) {
|
||||||
total += sizeof(VariantSlot) + s->data()->memoryUsage();
|
total += sizeof(VariantSlot) + s->data()->memoryUsage();
|
||||||
if (s->ownsKey())
|
if (s->ownsKey())
|
||||||
total += sizeofString(strlen(s->key()));
|
total += sizeofString(strlen(s->key()));
|
||||||
@ -81,7 +81,7 @@ inline size_t CollectionData::memoryUsage() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
inline size_t CollectionData::size() const {
|
inline size_t CollectionData::size() const {
|
||||||
return slotSize(_head);
|
return slotSize(head_);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
@ -94,9 +94,9 @@ inline void movePointer(T*& p, ptrdiff_t offset) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
inline void CollectionData::movePointers(ptrdiff_t variantDistance) {
|
inline void CollectionData::movePointers(ptrdiff_t variantDistance) {
|
||||||
movePointer(_head, variantDistance);
|
movePointer(head_, variantDistance);
|
||||||
movePointer(_tail, variantDistance);
|
movePointer(tail_, variantDistance);
|
||||||
for (VariantSlot* slot = _head; slot; slot = slot->next())
|
for (VariantSlot* slot = head_; slot; slot = slot->next())
|
||||||
slot->movePointers(variantDistance);
|
slot->movePointers(variantDistance);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,49 +26,49 @@ class DeserializationError {
|
|||||||
};
|
};
|
||||||
|
|
||||||
DeserializationError() {}
|
DeserializationError() {}
|
||||||
DeserializationError(Code c) : _code(c) {}
|
DeserializationError(Code c) : code_(c) {}
|
||||||
|
|
||||||
// Compare with DeserializationError
|
// Compare with DeserializationError
|
||||||
friend bool operator==(const DeserializationError& lhs,
|
friend bool operator==(const DeserializationError& lhs,
|
||||||
const DeserializationError& rhs) {
|
const DeserializationError& rhs) {
|
||||||
return lhs._code == rhs._code;
|
return lhs.code_ == rhs.code_;
|
||||||
}
|
}
|
||||||
friend bool operator!=(const DeserializationError& lhs,
|
friend bool operator!=(const DeserializationError& lhs,
|
||||||
const DeserializationError& rhs) {
|
const DeserializationError& rhs) {
|
||||||
return lhs._code != rhs._code;
|
return lhs.code_ != rhs.code_;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compare with Code
|
// Compare with Code
|
||||||
friend bool operator==(const DeserializationError& lhs, Code rhs) {
|
friend bool operator==(const DeserializationError& lhs, Code rhs) {
|
||||||
return lhs._code == rhs;
|
return lhs.code_ == rhs;
|
||||||
}
|
}
|
||||||
friend bool operator==(Code lhs, const DeserializationError& rhs) {
|
friend bool operator==(Code lhs, const DeserializationError& rhs) {
|
||||||
return lhs == rhs._code;
|
return lhs == rhs.code_;
|
||||||
}
|
}
|
||||||
friend bool operator!=(const DeserializationError& lhs, Code rhs) {
|
friend bool operator!=(const DeserializationError& lhs, Code rhs) {
|
||||||
return lhs._code != rhs;
|
return lhs.code_ != rhs;
|
||||||
}
|
}
|
||||||
friend bool operator!=(Code lhs, const DeserializationError& rhs) {
|
friend bool operator!=(Code lhs, const DeserializationError& rhs) {
|
||||||
return lhs != rhs._code;
|
return lhs != rhs.code_;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if there is an error
|
// Returns true if there is an error
|
||||||
explicit operator bool() const {
|
explicit operator bool() const {
|
||||||
return _code != Ok;
|
return code_ != Ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns internal enum, useful for switch statement
|
// Returns internal enum, useful for switch statement
|
||||||
Code code() const {
|
Code code() const {
|
||||||
return _code;
|
return code_;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* c_str() const {
|
const char* c_str() const {
|
||||||
static const char* messages[] = {
|
static const char* messages[] = {
|
||||||
"Ok", "EmptyInput", "IncompleteInput",
|
"Ok", "EmptyInput", "IncompleteInput",
|
||||||
"InvalidInput", "NoMemory", "TooDeep"};
|
"InvalidInput", "NoMemory", "TooDeep"};
|
||||||
ARDUINOJSON_ASSERT(static_cast<size_t>(_code) <
|
ARDUINOJSON_ASSERT(static_cast<size_t>(code_) <
|
||||||
sizeof(messages) / sizeof(messages[0]));
|
sizeof(messages) / sizeof(messages[0]));
|
||||||
return messages[_code];
|
return messages[code_];
|
||||||
}
|
}
|
||||||
|
|
||||||
#if ARDUINOJSON_ENABLE_PROGMEM
|
#if ARDUINOJSON_ENABLE_PROGMEM
|
||||||
@ -82,12 +82,12 @@ class DeserializationError {
|
|||||||
ARDUINOJSON_DEFINE_PROGMEM_ARRAY(const char*, messages,
|
ARDUINOJSON_DEFINE_PROGMEM_ARRAY(const char*, messages,
|
||||||
{s0, s1, s2, s3, s4, s5});
|
{s0, s1, s2, s3, s4, s5});
|
||||||
return reinterpret_cast<const __FlashStringHelper*>(
|
return reinterpret_cast<const __FlashStringHelper*>(
|
||||||
detail::pgm_read(messages + _code));
|
detail::pgm_read(messages + code_));
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Code _code;
|
Code code_;
|
||||||
};
|
};
|
||||||
|
|
||||||
#if ARDUINOJSON_ENABLE_STD_STREAM
|
#if ARDUINOJSON_ENABLE_STD_STREAM
|
||||||
|
@ -11,34 +11,34 @@ ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE
|
|||||||
namespace DeserializationOption {
|
namespace DeserializationOption {
|
||||||
class Filter {
|
class Filter {
|
||||||
public:
|
public:
|
||||||
explicit Filter(JsonVariantConst v) : _variant(v) {}
|
explicit Filter(JsonVariantConst v) : variant_(v) {}
|
||||||
|
|
||||||
bool allow() const {
|
bool allow() const {
|
||||||
return _variant;
|
return variant_;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool allowArray() const {
|
bool allowArray() const {
|
||||||
return _variant == true || _variant.is<JsonArrayConst>();
|
return variant_ == true || variant_.is<JsonArrayConst>();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool allowObject() const {
|
bool allowObject() const {
|
||||||
return _variant == true || _variant.is<JsonObjectConst>();
|
return variant_ == true || variant_.is<JsonObjectConst>();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool allowValue() const {
|
bool allowValue() const {
|
||||||
return _variant == true;
|
return variant_ == true;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TKey>
|
template <typename TKey>
|
||||||
Filter operator[](const TKey& key) const {
|
Filter operator[](const TKey& key) const {
|
||||||
if (_variant == true) // "true" means "allow recursively"
|
if (variant_ == true) // "true" means "allow recursively"
|
||||||
return *this;
|
return *this;
|
||||||
JsonVariantConst member = _variant[key];
|
JsonVariantConst member = variant_[key];
|
||||||
return Filter(member.isNull() ? _variant["*"] : member);
|
return Filter(member.isNull() ? variant_["*"] : member);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
JsonVariantConst _variant;
|
JsonVariantConst variant_;
|
||||||
};
|
};
|
||||||
} // namespace DeserializationOption
|
} // namespace DeserializationOption
|
||||||
|
|
||||||
|
@ -12,20 +12,20 @@ ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE
|
|||||||
namespace DeserializationOption {
|
namespace DeserializationOption {
|
||||||
class NestingLimit {
|
class NestingLimit {
|
||||||
public:
|
public:
|
||||||
NestingLimit() : _value(ARDUINOJSON_DEFAULT_NESTING_LIMIT) {}
|
NestingLimit() : value_(ARDUINOJSON_DEFAULT_NESTING_LIMIT) {}
|
||||||
explicit NestingLimit(uint8_t n) : _value(n) {}
|
explicit NestingLimit(uint8_t n) : value_(n) {}
|
||||||
|
|
||||||
NestingLimit decrement() const {
|
NestingLimit decrement() const {
|
||||||
ARDUINOJSON_ASSERT(_value > 0);
|
ARDUINOJSON_ASSERT(value_ > 0);
|
||||||
return NestingLimit(static_cast<uint8_t>(_value - 1));
|
return NestingLimit(static_cast<uint8_t>(value_ - 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool reached() const {
|
bool reached() const {
|
||||||
return _value == 0;
|
return value_ == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint8_t _value;
|
uint8_t value_;
|
||||||
};
|
};
|
||||||
} // namespace DeserializationOption
|
} // namespace DeserializationOption
|
||||||
|
|
||||||
|
@ -15,18 +15,18 @@ ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
|||||||
template <typename TSource, typename Enable = void>
|
template <typename TSource, typename Enable = void>
|
||||||
struct Reader {
|
struct Reader {
|
||||||
public:
|
public:
|
||||||
Reader(TSource& source) : _source(&source) {}
|
Reader(TSource& source) : source_(&source) {}
|
||||||
|
|
||||||
int read() {
|
int read() {
|
||||||
return _source->read(); // Error here? You passed an unsupported input type
|
return source_->read(); // Error here? You passed an unsupported input type
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t readBytes(char* buffer, size_t length) {
|
size_t readBytes(char* buffer, size_t length) {
|
||||||
return _source->readBytes(buffer, length);
|
return source_->readBytes(buffer, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
TSource* _source;
|
TSource* source_;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename TSource, typename Enable = void>
|
template <typename TSource, typename Enable = void>
|
||||||
|
@ -12,20 +12,20 @@ template <typename TSource>
|
|||||||
struct Reader<TSource,
|
struct Reader<TSource,
|
||||||
typename enable_if<is_base_of<Stream, TSource>::value>::type> {
|
typename enable_if<is_base_of<Stream, TSource>::value>::type> {
|
||||||
public:
|
public:
|
||||||
explicit Reader(Stream& stream) : _stream(&stream) {}
|
explicit Reader(Stream& stream) : stream_(&stream) {}
|
||||||
|
|
||||||
int read() {
|
int read() {
|
||||||
// don't use _stream.read() as it ignores the timeout
|
// don't use stream_->read() as it ignores the timeout
|
||||||
char c;
|
char c;
|
||||||
return _stream->readBytes(&c, 1) ? static_cast<unsigned char>(c) : -1;
|
return stream_->readBytes(&c, 1) ? static_cast<unsigned char>(c) : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t readBytes(char* buffer, size_t length) {
|
size_t readBytes(char* buffer, size_t length) {
|
||||||
return _stream->readBytes(buffer, length);
|
return stream_->readBytes(buffer, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Stream* _stream;
|
Stream* stream_;
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -10,45 +10,45 @@ ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
|||||||
|
|
||||||
template <>
|
template <>
|
||||||
struct Reader<const __FlashStringHelper*, void> {
|
struct Reader<const __FlashStringHelper*, void> {
|
||||||
const char* _ptr;
|
const char* ptr_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit Reader(const __FlashStringHelper* ptr)
|
explicit Reader(const __FlashStringHelper* ptr)
|
||||||
: _ptr(reinterpret_cast<const char*>(ptr)) {}
|
: ptr_(reinterpret_cast<const char*>(ptr)) {}
|
||||||
|
|
||||||
int read() {
|
int read() {
|
||||||
return pgm_read_byte(_ptr++);
|
return pgm_read_byte(ptr_++);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t readBytes(char* buffer, size_t length) {
|
size_t readBytes(char* buffer, size_t length) {
|
||||||
memcpy_P(buffer, _ptr, length);
|
memcpy_P(buffer, ptr_, length);
|
||||||
_ptr += length;
|
ptr_ += length;
|
||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
struct BoundedReader<const __FlashStringHelper*, void> {
|
struct BoundedReader<const __FlashStringHelper*, void> {
|
||||||
const char* _ptr;
|
const char* ptr_;
|
||||||
const char* _end;
|
const char* end_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit BoundedReader(const __FlashStringHelper* ptr, size_t size)
|
explicit BoundedReader(const __FlashStringHelper* ptr, size_t size)
|
||||||
: _ptr(reinterpret_cast<const char*>(ptr)), _end(_ptr + size) {}
|
: ptr_(reinterpret_cast<const char*>(ptr)), end_(ptr_ + size) {}
|
||||||
|
|
||||||
int read() {
|
int read() {
|
||||||
if (_ptr < _end)
|
if (ptr_ < end_)
|
||||||
return pgm_read_byte(_ptr++);
|
return pgm_read_byte(ptr_++);
|
||||||
else
|
else
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t readBytes(char* buffer, size_t length) {
|
size_t readBytes(char* buffer, size_t length) {
|
||||||
size_t available = static_cast<size_t>(_end - _ptr);
|
size_t available = static_cast<size_t>(end_ - ptr_);
|
||||||
if (available < length)
|
if (available < length)
|
||||||
length = available;
|
length = available;
|
||||||
memcpy_P(buffer, _ptr, length);
|
memcpy_P(buffer, ptr_, length);
|
||||||
_ptr += length;
|
ptr_ += length;
|
||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -8,23 +8,23 @@ ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
|||||||
|
|
||||||
template <typename TIterator>
|
template <typename TIterator>
|
||||||
class IteratorReader {
|
class IteratorReader {
|
||||||
TIterator _ptr, _end;
|
TIterator ptr_, end_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit IteratorReader(TIterator begin, TIterator end)
|
explicit IteratorReader(TIterator begin, TIterator end)
|
||||||
: _ptr(begin), _end(end) {}
|
: ptr_(begin), end_(end) {}
|
||||||
|
|
||||||
int read() {
|
int read() {
|
||||||
if (_ptr < _end)
|
if (ptr_ < end_)
|
||||||
return static_cast<unsigned char>(*_ptr++);
|
return static_cast<unsigned char>(*ptr_++);
|
||||||
else
|
else
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t readBytes(char* buffer, size_t length) {
|
size_t readBytes(char* buffer, size_t length) {
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
while (i < length && _ptr < _end)
|
while (i < length && ptr_ < end_)
|
||||||
buffer[i++] = *_ptr++;
|
buffer[i++] = *ptr_++;
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -21,19 +21,19 @@ struct IsCharOrVoid<const T> : IsCharOrVoid<T> {};
|
|||||||
template <typename TSource>
|
template <typename TSource>
|
||||||
struct Reader<TSource*,
|
struct Reader<TSource*,
|
||||||
typename enable_if<IsCharOrVoid<TSource>::value>::type> {
|
typename enable_if<IsCharOrVoid<TSource>::value>::type> {
|
||||||
const char* _ptr;
|
const char* ptr_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit Reader(const void* ptr)
|
explicit Reader(const void* ptr)
|
||||||
: _ptr(ptr ? reinterpret_cast<const char*>(ptr) : "") {}
|
: ptr_(ptr ? reinterpret_cast<const char*>(ptr) : "") {}
|
||||||
|
|
||||||
int read() {
|
int read() {
|
||||||
return static_cast<unsigned char>(*_ptr++);
|
return static_cast<unsigned char>(*ptr_++);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t readBytes(char* buffer, size_t length) {
|
size_t readBytes(char* buffer, size_t length) {
|
||||||
for (size_t i = 0; i < length; i++)
|
for (size_t i = 0; i < length; i++)
|
||||||
buffer[i] = *_ptr++;
|
buffer[i] = *ptr_++;
|
||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -12,19 +12,19 @@ template <typename TSource>
|
|||||||
struct Reader<TSource, typename enable_if<
|
struct Reader<TSource, typename enable_if<
|
||||||
is_base_of<std::istream, TSource>::value>::type> {
|
is_base_of<std::istream, TSource>::value>::type> {
|
||||||
public:
|
public:
|
||||||
explicit Reader(std::istream& stream) : _stream(&stream) {}
|
explicit Reader(std::istream& stream) : stream_(&stream) {}
|
||||||
|
|
||||||
int read() {
|
int read() {
|
||||||
return _stream->get();
|
return stream_->get();
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t readBytes(char* buffer, size_t length) {
|
size_t readBytes(char* buffer, size_t length) {
|
||||||
_stream->read(buffer, static_cast<std::streamsize>(length));
|
stream_->read(buffer, static_cast<std::streamsize>(length));
|
||||||
return static_cast<size_t>(_stream->gcount());
|
return static_cast<size_t>(stream_->gcount());
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::istream* _stream;
|
std::istream* stream_;
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -24,16 +24,16 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
|||||||
public:
|
public:
|
||||||
explicit JsonDocument(size_t capa,
|
explicit JsonDocument(size_t capa,
|
||||||
Allocator* alloc = detail::DefaultAllocator::instance())
|
Allocator* alloc = detail::DefaultAllocator::instance())
|
||||||
: _pool(capa, alloc) {}
|
: pool_(capa, alloc) {}
|
||||||
|
|
||||||
// Copy-constructor
|
// Copy-constructor
|
||||||
JsonDocument(const JsonDocument& src)
|
JsonDocument(const JsonDocument& src)
|
||||||
: JsonDocument(src._pool.capacity(), src.allocator()) {
|
: JsonDocument(src.pool_.capacity(), src.allocator()) {
|
||||||
set(src);
|
set(src);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Move-constructor
|
// Move-constructor
|
||||||
JsonDocument(JsonDocument&& src) : _pool(0, src.allocator()) {
|
JsonDocument(JsonDocument&& src) : pool_(0, src.allocator()) {
|
||||||
// TODO: use the copy and swap idiom
|
// TODO: use the copy and swap idiom
|
||||||
moveAssignFrom(src);
|
moveAssignFrom(src);
|
||||||
}
|
}
|
||||||
@ -74,20 +74,20 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
JsonDocument& operator=(const T& src) {
|
JsonDocument& operator=(const T& src) {
|
||||||
size_t requiredSize = src.memoryUsage();
|
size_t requiredSize = src.memoryUsage();
|
||||||
if (requiredSize > _pool.capacity())
|
if (requiredSize > pool_.capacity())
|
||||||
_pool.reallocPool(requiredSize);
|
pool_.reallocPool(requiredSize);
|
||||||
set(src);
|
set(src);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Allocator* allocator() const {
|
Allocator* allocator() const {
|
||||||
return _pool.allocator();
|
return pool_.allocator();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reduces the capacity of the memory pool to match the current usage.
|
// Reduces the capacity of the memory pool to match the current usage.
|
||||||
// https://arduinojson.org/v6/api/JsonDocument/shrinktofit/
|
// https://arduinojson.org/v6/api/JsonDocument/shrinktofit/
|
||||||
void shrinkToFit() {
|
void shrinkToFit() {
|
||||||
_pool.shrinkToFit(_data);
|
pool_.shrinkToFit(data_);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reclaims the memory leaked when removing and replacing values.
|
// Reclaims the memory leaked when removing and replacing values.
|
||||||
@ -95,7 +95,7 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
|||||||
bool garbageCollect() {
|
bool garbageCollect() {
|
||||||
// make a temporary clone and move assign
|
// make a temporary clone and move assign
|
||||||
JsonDocument tmp(*this);
|
JsonDocument tmp(*this);
|
||||||
if (!tmp._pool.capacity())
|
if (!tmp.pool_.capacity())
|
||||||
return false;
|
return false;
|
||||||
moveAssignFrom(tmp);
|
moveAssignFrom(tmp);
|
||||||
return true;
|
return true;
|
||||||
@ -118,8 +118,8 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
|||||||
// Empties the document and resets the memory pool
|
// Empties the document and resets the memory pool
|
||||||
// https://arduinojson.org/v6/api/jsondocument/clear/
|
// https://arduinojson.org/v6/api/jsondocument/clear/
|
||||||
void clear() {
|
void clear() {
|
||||||
_pool.clear();
|
pool_.clear();
|
||||||
_data.reset();
|
data_.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if the root is of the specified type.
|
// Returns true if the root is of the specified type.
|
||||||
@ -145,25 +145,25 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
|||||||
// Returns the number of used bytes in the memory pool.
|
// Returns the number of used bytes in the memory pool.
|
||||||
// https://arduinojson.org/v6/api/jsondocument/memoryusage/
|
// https://arduinojson.org/v6/api/jsondocument/memoryusage/
|
||||||
size_t memoryUsage() const {
|
size_t memoryUsage() const {
|
||||||
return _pool.size();
|
return pool_.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns trues if the memory pool was too small.
|
// Returns trues if the memory pool was too small.
|
||||||
// https://arduinojson.org/v6/api/jsondocument/overflowed/
|
// https://arduinojson.org/v6/api/jsondocument/overflowed/
|
||||||
bool overflowed() const {
|
bool overflowed() const {
|
||||||
return _pool.overflowed();
|
return pool_.overflowed();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the depth (nesting level) of the array.
|
// Returns the depth (nesting level) of the array.
|
||||||
// https://arduinojson.org/v6/api/jsondocument/nesting/
|
// https://arduinojson.org/v6/api/jsondocument/nesting/
|
||||||
size_t nesting() const {
|
size_t nesting() const {
|
||||||
return variantNesting(&_data);
|
return variantNesting(&data_);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the number of elements in the root array or object.
|
// Returns the number of elements in the root array or object.
|
||||||
// https://arduinojson.org/v6/api/jsondocument/size/
|
// https://arduinojson.org/v6/api/jsondocument/size/
|
||||||
size_t size() const {
|
size_t size() const {
|
||||||
return _data.size();
|
return data_.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copies the specified document.
|
// Copies the specified document.
|
||||||
@ -233,14 +233,14 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
|||||||
// https://arduinojson.org/v6/api/jsondocument/containskey/
|
// https://arduinojson.org/v6/api/jsondocument/containskey/
|
||||||
template <typename TChar>
|
template <typename TChar>
|
||||||
bool containsKey(TChar* key) const {
|
bool containsKey(TChar* key) const {
|
||||||
return _data.getMember(detail::adaptString(key)) != 0;
|
return data_.getMember(detail::adaptString(key)) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if the root object contains the specified key.
|
// Returns true if the root object contains the specified key.
|
||||||
// https://arduinojson.org/v6/api/jsondocument/containskey/
|
// https://arduinojson.org/v6/api/jsondocument/containskey/
|
||||||
template <typename TString>
|
template <typename TString>
|
||||||
bool containsKey(const TString& key) const {
|
bool containsKey(const TString& key) const {
|
||||||
return _data.getMember(detail::adaptString(key)) != 0;
|
return data_.getMember(detail::adaptString(key)) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gets or sets a root object's member.
|
// Gets or sets a root object's member.
|
||||||
@ -269,7 +269,7 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
|||||||
FORCE_INLINE typename detail::enable_if<detail::IsString<TString>::value,
|
FORCE_INLINE typename detail::enable_if<detail::IsString<TString>::value,
|
||||||
JsonVariantConst>::type
|
JsonVariantConst>::type
|
||||||
operator[](const TString& key) const {
|
operator[](const TString& key) const {
|
||||||
return JsonVariantConst(_data.getMember(detail::adaptString(key)));
|
return JsonVariantConst(data_.getMember(detail::adaptString(key)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gets a root object's member.
|
// Gets a root object's member.
|
||||||
@ -278,7 +278,7 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
|||||||
FORCE_INLINE typename detail::enable_if<detail::IsString<TChar*>::value,
|
FORCE_INLINE typename detail::enable_if<detail::IsString<TChar*>::value,
|
||||||
JsonVariantConst>::type
|
JsonVariantConst>::type
|
||||||
operator[](TChar* key) const {
|
operator[](TChar* key) const {
|
||||||
return JsonVariantConst(_data.getMember(detail::adaptString(key)));
|
return JsonVariantConst(data_.getMember(detail::adaptString(key)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gets or sets a root array's element.
|
// Gets or sets a root array's element.
|
||||||
@ -290,14 +290,14 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
|||||||
// Gets a root array's member.
|
// Gets a root array's member.
|
||||||
// https://arduinojson.org/v6/api/jsondocument/subscript/
|
// https://arduinojson.org/v6/api/jsondocument/subscript/
|
||||||
FORCE_INLINE JsonVariantConst operator[](size_t index) const {
|
FORCE_INLINE JsonVariantConst operator[](size_t index) const {
|
||||||
return JsonVariantConst(_data.getElement(index));
|
return JsonVariantConst(data_.getElement(index));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Appends a new (null) element to the root array.
|
// Appends a new (null) element to the root array.
|
||||||
// Returns a reference to the new element.
|
// Returns a reference to the new element.
|
||||||
// https://arduinojson.org/v6/api/jsondocument/add/
|
// https://arduinojson.org/v6/api/jsondocument/add/
|
||||||
FORCE_INLINE JsonVariant add() {
|
FORCE_INLINE JsonVariant add() {
|
||||||
return JsonVariant(&_pool, variantAddElement(&_data, &_pool));
|
return JsonVariant(&pool_, variantAddElement(&data_, &pool_));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Appends a value to the root array.
|
// Appends a value to the root array.
|
||||||
@ -350,42 +350,42 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
JsonVariant getVariant() {
|
JsonVariant getVariant() {
|
||||||
return JsonVariant(&_pool, &_data);
|
return JsonVariant(&pool_, &data_);
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonVariantConst getVariant() const {
|
JsonVariantConst getVariant() const {
|
||||||
return JsonVariantConst(&_data);
|
return JsonVariantConst(&data_);
|
||||||
}
|
}
|
||||||
|
|
||||||
void copyAssignFrom(const JsonDocument& src) {
|
void copyAssignFrom(const JsonDocument& src) {
|
||||||
_pool.reallocPool(src._pool.capacity());
|
pool_.reallocPool(src.pool_.capacity());
|
||||||
set(src);
|
set(src);
|
||||||
}
|
}
|
||||||
|
|
||||||
void moveAssignFrom(JsonDocument& src) {
|
void moveAssignFrom(JsonDocument& src) {
|
||||||
_data = src._data;
|
data_ = src.data_;
|
||||||
src._data.reset();
|
src.data_.reset();
|
||||||
_pool = move(src._pool);
|
pool_ = move(src.pool_);
|
||||||
}
|
}
|
||||||
|
|
||||||
detail::MemoryPool* getPool() {
|
detail::MemoryPool* getPool() {
|
||||||
return &_pool;
|
return &pool_;
|
||||||
}
|
}
|
||||||
|
|
||||||
detail::VariantData* getData() {
|
detail::VariantData* getData() {
|
||||||
return &_data;
|
return &data_;
|
||||||
}
|
}
|
||||||
|
|
||||||
const detail::VariantData* getData() const {
|
const detail::VariantData* getData() const {
|
||||||
return &_data;
|
return &data_;
|
||||||
}
|
}
|
||||||
|
|
||||||
detail::VariantData* getOrCreateData() {
|
detail::VariantData* getOrCreateData() {
|
||||||
return &_data;
|
return &data_;
|
||||||
}
|
}
|
||||||
|
|
||||||
detail::MemoryPool _pool;
|
detail::MemoryPool pool_;
|
||||||
detail::VariantData _data;
|
detail::VariantData data_;
|
||||||
};
|
};
|
||||||
|
|
||||||
inline void convertToJson(const JsonDocument& src, JsonVariant dst) {
|
inline void convertToJson(const JsonDocument& src, JsonVariant dst) {
|
||||||
|
@ -23,10 +23,10 @@ class JsonDeserializer {
|
|||||||
public:
|
public:
|
||||||
JsonDeserializer(MemoryPool* pool, TReader reader,
|
JsonDeserializer(MemoryPool* pool, TReader reader,
|
||||||
TStringStorage stringStorage)
|
TStringStorage stringStorage)
|
||||||
: _stringStorage(stringStorage),
|
: stringStorage_(stringStorage),
|
||||||
_foundSomething(false),
|
foundSomething_(false),
|
||||||
_latch(reader),
|
latch_(reader),
|
||||||
_pool(pool) {}
|
pool_(pool) {}
|
||||||
|
|
||||||
template <typename TFilter>
|
template <typename TFilter>
|
||||||
DeserializationError parse(VariantData& variant, TFilter filter,
|
DeserializationError parse(VariantData& variant, TFilter filter,
|
||||||
@ -35,7 +35,7 @@ class JsonDeserializer {
|
|||||||
|
|
||||||
err = parseVariant(variant, filter, nestingLimit);
|
err = parseVariant(variant, filter, nestingLimit);
|
||||||
|
|
||||||
if (!err && _latch.last() != 0 && !variant.isEnclosed()) {
|
if (!err && latch_.last() != 0 && !variant.isEnclosed()) {
|
||||||
// We don't detect trailing characters earlier, so we need to check now
|
// We don't detect trailing characters earlier, so we need to check now
|
||||||
return DeserializationError::InvalidInput;
|
return DeserializationError::InvalidInput;
|
||||||
}
|
}
|
||||||
@ -45,11 +45,11 @@ class JsonDeserializer {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
char current() {
|
char current() {
|
||||||
return _latch.current();
|
return latch_.current();
|
||||||
}
|
}
|
||||||
|
|
||||||
void move() {
|
void move() {
|
||||||
_latch.clear();
|
latch_.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool eat(char charToSkip) {
|
bool eat(char charToSkip) {
|
||||||
@ -173,7 +173,7 @@ class JsonDeserializer {
|
|||||||
for (;;) {
|
for (;;) {
|
||||||
if (memberFilter.allow()) {
|
if (memberFilter.allow()) {
|
||||||
// Allocate slot in array
|
// Allocate slot in array
|
||||||
VariantData* value = collectionAddElement(&array, _pool);
|
VariantData* value = collectionAddElement(&array, pool_);
|
||||||
if (!value)
|
if (!value)
|
||||||
return DeserializationError::NoMemory;
|
return DeserializationError::NoMemory;
|
||||||
|
|
||||||
@ -269,7 +269,7 @@ class JsonDeserializer {
|
|||||||
if (!eat(':'))
|
if (!eat(':'))
|
||||||
return DeserializationError::InvalidInput;
|
return DeserializationError::InvalidInput;
|
||||||
|
|
||||||
JsonString key = _stringStorage.str();
|
JsonString key = stringStorage_.str();
|
||||||
|
|
||||||
TFilter memberFilter = filter[key.c_str()];
|
TFilter memberFilter = filter[key.c_str()];
|
||||||
|
|
||||||
@ -277,10 +277,10 @@ class JsonDeserializer {
|
|||||||
VariantSlot* slot = object.get(adaptString(key.c_str()));
|
VariantSlot* slot = object.get(adaptString(key.c_str()));
|
||||||
if (!slot) {
|
if (!slot) {
|
||||||
// Save key in memory pool.
|
// Save key in memory pool.
|
||||||
key = _stringStorage.save();
|
key = stringStorage_.save();
|
||||||
|
|
||||||
// Allocate slot in object
|
// Allocate slot in object
|
||||||
slot = _pool->allocVariant();
|
slot = pool_->allocVariant();
|
||||||
if (!slot)
|
if (!slot)
|
||||||
return DeserializationError::NoMemory;
|
return DeserializationError::NoMemory;
|
||||||
|
|
||||||
@ -376,7 +376,7 @@ class JsonDeserializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
DeserializationError::Code parseKey() {
|
DeserializationError::Code parseKey() {
|
||||||
_stringStorage.startString();
|
stringStorage_.startString();
|
||||||
if (isQuote(current())) {
|
if (isQuote(current())) {
|
||||||
return parseQuotedString();
|
return parseQuotedString();
|
||||||
} else {
|
} else {
|
||||||
@ -387,13 +387,13 @@ class JsonDeserializer {
|
|||||||
DeserializationError::Code parseStringValue(VariantData& variant) {
|
DeserializationError::Code parseStringValue(VariantData& variant) {
|
||||||
DeserializationError::Code err;
|
DeserializationError::Code err;
|
||||||
|
|
||||||
_stringStorage.startString();
|
stringStorage_.startString();
|
||||||
|
|
||||||
err = parseQuotedString();
|
err = parseQuotedString();
|
||||||
if (err)
|
if (err)
|
||||||
return err;
|
return err;
|
||||||
|
|
||||||
variant.setString(_stringStorage.save());
|
variant.setString(stringStorage_.save());
|
||||||
|
|
||||||
return DeserializationError::Ok;
|
return DeserializationError::Ok;
|
||||||
}
|
}
|
||||||
@ -429,9 +429,9 @@ class JsonDeserializer {
|
|||||||
if (err)
|
if (err)
|
||||||
return err;
|
return err;
|
||||||
if (codepoint.append(codeunit))
|
if (codepoint.append(codeunit))
|
||||||
Utf8::encodeCodepoint(codepoint.value(), _stringStorage);
|
Utf8::encodeCodepoint(codepoint.value(), stringStorage_);
|
||||||
#else
|
#else
|
||||||
_stringStorage.append('\\');
|
stringStorage_.append('\\');
|
||||||
#endif
|
#endif
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -443,10 +443,10 @@ class JsonDeserializer {
|
|||||||
move();
|
move();
|
||||||
}
|
}
|
||||||
|
|
||||||
_stringStorage.append(c);
|
stringStorage_.append(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!_stringStorage.isValid())
|
if (!stringStorage_.isValid())
|
||||||
return DeserializationError::NoMemory;
|
return DeserializationError::NoMemory;
|
||||||
|
|
||||||
return DeserializationError::Ok;
|
return DeserializationError::Ok;
|
||||||
@ -459,14 +459,14 @@ class JsonDeserializer {
|
|||||||
if (canBeInNonQuotedString(c)) { // no quotes
|
if (canBeInNonQuotedString(c)) { // no quotes
|
||||||
do {
|
do {
|
||||||
move();
|
move();
|
||||||
_stringStorage.append(c);
|
stringStorage_.append(c);
|
||||||
c = current();
|
c = current();
|
||||||
} while (canBeInNonQuotedString(c));
|
} while (canBeInNonQuotedString(c));
|
||||||
} else {
|
} else {
|
||||||
return DeserializationError::InvalidInput;
|
return DeserializationError::InvalidInput;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!_stringStorage.isValid())
|
if (!stringStorage_.isValid())
|
||||||
return DeserializationError::NoMemory;
|
return DeserializationError::NoMemory;
|
||||||
|
|
||||||
return DeserializationError::Ok;
|
return DeserializationError::Ok;
|
||||||
@ -515,12 +515,12 @@ class JsonDeserializer {
|
|||||||
char c = current();
|
char c = current();
|
||||||
while (canBeInNumber(c) && n < 63) {
|
while (canBeInNumber(c) && n < 63) {
|
||||||
move();
|
move();
|
||||||
_buffer[n++] = c;
|
buffer_[n++] = c;
|
||||||
c = current();
|
c = current();
|
||||||
}
|
}
|
||||||
_buffer[n] = 0;
|
buffer_[n] = 0;
|
||||||
|
|
||||||
if (!parseNumber(_buffer, result))
|
if (!parseNumber(buffer_, result))
|
||||||
return DeserializationError::InvalidInput;
|
return DeserializationError::InvalidInput;
|
||||||
|
|
||||||
return DeserializationError::Ok;
|
return DeserializationError::Ok;
|
||||||
@ -584,7 +584,7 @@ class JsonDeserializer {
|
|||||||
switch (current()) {
|
switch (current()) {
|
||||||
// end of string
|
// end of string
|
||||||
case '\0':
|
case '\0':
|
||||||
return _foundSomething ? DeserializationError::IncompleteInput
|
return foundSomething_ ? DeserializationError::IncompleteInput
|
||||||
: DeserializationError::EmptyInput;
|
: DeserializationError::EmptyInput;
|
||||||
|
|
||||||
// spaces
|
// spaces
|
||||||
@ -639,7 +639,7 @@ class JsonDeserializer {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
default:
|
default:
|
||||||
_foundSomething = true;
|
foundSomething_ = true;
|
||||||
return DeserializationError::Ok;
|
return DeserializationError::Ok;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -658,11 +658,11 @@ class JsonDeserializer {
|
|||||||
return DeserializationError::Ok;
|
return DeserializationError::Ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
TStringStorage _stringStorage;
|
TStringStorage stringStorage_;
|
||||||
bool _foundSomething;
|
bool foundSomething_;
|
||||||
Latch<TReader> _latch;
|
Latch<TReader> latch_;
|
||||||
MemoryPool* _pool;
|
MemoryPool* pool_;
|
||||||
char _buffer[64]; // using a member instead of a local variable because it
|
char buffer_[64]; // using a member instead of a local variable because it
|
||||||
// ended in the recursive path after compiler inlined the
|
// ended in the recursive path after compiler inlined the
|
||||||
// code
|
// code
|
||||||
};
|
};
|
||||||
|
@ -16,7 +16,7 @@ class JsonSerializer : public Visitor<size_t> {
|
|||||||
public:
|
public:
|
||||||
static const bool producesText = true;
|
static const bool producesText = true;
|
||||||
|
|
||||||
JsonSerializer(TWriter writer) : _formatter(writer) {}
|
JsonSerializer(TWriter writer) : formatter_(writer) {}
|
||||||
|
|
||||||
FORCE_INLINE size_t visitArray(const CollectionData& array) {
|
FORCE_INLINE size_t visitArray(const CollectionData& array) {
|
||||||
write('[');
|
write('[');
|
||||||
@ -43,7 +43,7 @@ class JsonSerializer : public Visitor<size_t> {
|
|||||||
const VariantSlot* slot = object.head();
|
const VariantSlot* slot = object.head();
|
||||||
|
|
||||||
while (slot != 0) {
|
while (slot != 0) {
|
||||||
_formatter.writeString(slot->key());
|
formatter_.writeString(slot->key());
|
||||||
write(':');
|
write(':');
|
||||||
slot->data()->accept(*this);
|
slot->data()->accept(*this);
|
||||||
|
|
||||||
@ -59,60 +59,60 @@ class JsonSerializer : public Visitor<size_t> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
size_t visitFloat(JsonFloat value) {
|
size_t visitFloat(JsonFloat value) {
|
||||||
_formatter.writeFloat(value);
|
formatter_.writeFloat(value);
|
||||||
return bytesWritten();
|
return bytesWritten();
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t visitString(const char* value) {
|
size_t visitString(const char* value) {
|
||||||
_formatter.writeString(value);
|
formatter_.writeString(value);
|
||||||
return bytesWritten();
|
return bytesWritten();
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t visitString(const char* value, size_t n) {
|
size_t visitString(const char* value, size_t n) {
|
||||||
_formatter.writeString(value, n);
|
formatter_.writeString(value, n);
|
||||||
return bytesWritten();
|
return bytesWritten();
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t visitRawJson(const char* data, size_t n) {
|
size_t visitRawJson(const char* data, size_t n) {
|
||||||
_formatter.writeRaw(data, n);
|
formatter_.writeRaw(data, n);
|
||||||
return bytesWritten();
|
return bytesWritten();
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t visitSignedInteger(JsonInteger value) {
|
size_t visitSignedInteger(JsonInteger value) {
|
||||||
_formatter.writeInteger(value);
|
formatter_.writeInteger(value);
|
||||||
return bytesWritten();
|
return bytesWritten();
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t visitUnsignedInteger(JsonUInt value) {
|
size_t visitUnsignedInteger(JsonUInt value) {
|
||||||
_formatter.writeInteger(value);
|
formatter_.writeInteger(value);
|
||||||
return bytesWritten();
|
return bytesWritten();
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t visitBoolean(bool value) {
|
size_t visitBoolean(bool value) {
|
||||||
_formatter.writeBoolean(value);
|
formatter_.writeBoolean(value);
|
||||||
return bytesWritten();
|
return bytesWritten();
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t visitNull() {
|
size_t visitNull() {
|
||||||
_formatter.writeRaw("null");
|
formatter_.writeRaw("null");
|
||||||
return bytesWritten();
|
return bytesWritten();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
size_t bytesWritten() const {
|
size_t bytesWritten() const {
|
||||||
return _formatter.bytesWritten();
|
return formatter_.bytesWritten();
|
||||||
}
|
}
|
||||||
|
|
||||||
void write(char c) {
|
void write(char c) {
|
||||||
_formatter.writeRaw(c);
|
formatter_.writeRaw(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
void write(const char* s) {
|
void write(const char* s) {
|
||||||
_formatter.writeRaw(s);
|
formatter_.writeRaw(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
TextFormatter<TWriter> _formatter;
|
TextFormatter<TWriter> formatter_;
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -11,45 +11,45 @@ ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
|||||||
template <typename TReader>
|
template <typename TReader>
|
||||||
class Latch {
|
class Latch {
|
||||||
public:
|
public:
|
||||||
Latch(TReader reader) : _reader(reader), _loaded(false) {
|
Latch(TReader reader) : reader_(reader), loaded_(false) {
|
||||||
#if ARDUINOJSON_DEBUG
|
#if ARDUINOJSON_DEBUG
|
||||||
_ended = false;
|
ended_ = false;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void clear() {
|
void clear() {
|
||||||
_loaded = false;
|
loaded_ = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int last() const {
|
int last() const {
|
||||||
return _current;
|
return current_;
|
||||||
}
|
}
|
||||||
|
|
||||||
FORCE_INLINE char current() {
|
FORCE_INLINE char current() {
|
||||||
if (!_loaded) {
|
if (!loaded_) {
|
||||||
load();
|
load();
|
||||||
}
|
}
|
||||||
return _current;
|
return current_;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void load() {
|
void load() {
|
||||||
ARDUINOJSON_ASSERT(!_ended);
|
ARDUINOJSON_ASSERT(!ended_);
|
||||||
int c = _reader.read();
|
int c = reader_.read();
|
||||||
#if ARDUINOJSON_DEBUG
|
#if ARDUINOJSON_DEBUG
|
||||||
if (c <= 0)
|
if (c <= 0)
|
||||||
_ended = true;
|
ended_ = true;
|
||||||
#endif
|
#endif
|
||||||
_current = static_cast<char>(c > 0 ? c : 0);
|
current_ = static_cast<char>(c > 0 ? c : 0);
|
||||||
_loaded = true;
|
loaded_ = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
TReader _reader;
|
TReader reader_;
|
||||||
char _current; // NOLINT(clang-analyzer-optin.cplusplus.UninitializedObject)
|
char current_; // NOLINT(clang-analyzer-optin.cplusplus.UninitializedObject)
|
||||||
// Not initialized in constructor (+10 bytes on AVR)
|
// Not initialized in constructor (+10 bytes on AVR)
|
||||||
bool _loaded;
|
bool loaded_;
|
||||||
#if ARDUINOJSON_DEBUG
|
#if ARDUINOJSON_DEBUG
|
||||||
bool _ended;
|
bool ended_;
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -16,13 +16,13 @@ class PrettyJsonSerializer : public JsonSerializer<TWriter> {
|
|||||||
typedef JsonSerializer<TWriter> base;
|
typedef JsonSerializer<TWriter> base;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
PrettyJsonSerializer(TWriter writer) : base(writer), _nesting(0) {}
|
PrettyJsonSerializer(TWriter writer) : base(writer), nesting_(0) {}
|
||||||
|
|
||||||
size_t visitArray(const CollectionData& array) {
|
size_t visitArray(const CollectionData& array) {
|
||||||
const VariantSlot* slot = array.head();
|
const VariantSlot* slot = array.head();
|
||||||
if (slot) {
|
if (slot) {
|
||||||
base::write("[\r\n");
|
base::write("[\r\n");
|
||||||
_nesting++;
|
nesting_++;
|
||||||
while (slot != 0) {
|
while (slot != 0) {
|
||||||
indent();
|
indent();
|
||||||
slot->data()->accept(*this);
|
slot->data()->accept(*this);
|
||||||
@ -30,7 +30,7 @@ class PrettyJsonSerializer : public JsonSerializer<TWriter> {
|
|||||||
slot = slot->next();
|
slot = slot->next();
|
||||||
base::write(slot ? ",\r\n" : "\r\n");
|
base::write(slot ? ",\r\n" : "\r\n");
|
||||||
}
|
}
|
||||||
_nesting--;
|
nesting_--;
|
||||||
indent();
|
indent();
|
||||||
base::write("]");
|
base::write("]");
|
||||||
} else {
|
} else {
|
||||||
@ -43,7 +43,7 @@ class PrettyJsonSerializer : public JsonSerializer<TWriter> {
|
|||||||
const VariantSlot* slot = object.head();
|
const VariantSlot* slot = object.head();
|
||||||
if (slot) {
|
if (slot) {
|
||||||
base::write("{\r\n");
|
base::write("{\r\n");
|
||||||
_nesting++;
|
nesting_++;
|
||||||
while (slot != 0) {
|
while (slot != 0) {
|
||||||
indent();
|
indent();
|
||||||
base::visitString(slot->key());
|
base::visitString(slot->key());
|
||||||
@ -53,7 +53,7 @@ class PrettyJsonSerializer : public JsonSerializer<TWriter> {
|
|||||||
slot = slot->next();
|
slot = slot->next();
|
||||||
base::write(slot ? ",\r\n" : "\r\n");
|
base::write(slot ? ",\r\n" : "\r\n");
|
||||||
}
|
}
|
||||||
_nesting--;
|
nesting_--;
|
||||||
indent();
|
indent();
|
||||||
base::write("}");
|
base::write("}");
|
||||||
} else {
|
} else {
|
||||||
@ -64,11 +64,11 @@ class PrettyJsonSerializer : public JsonSerializer<TWriter> {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
void indent() {
|
void indent() {
|
||||||
for (uint8_t i = 0; i < _nesting; i++)
|
for (uint8_t i = 0; i < nesting_; i++)
|
||||||
base::write(ARDUINOJSON_TAB);
|
base::write(ARDUINOJSON_TAB);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t _nesting;
|
uint8_t nesting_;
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -20,13 +20,13 @@ ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
|||||||
template <typename TWriter>
|
template <typename TWriter>
|
||||||
class TextFormatter {
|
class TextFormatter {
|
||||||
public:
|
public:
|
||||||
explicit TextFormatter(TWriter writer) : _writer(writer) {}
|
explicit TextFormatter(TWriter writer) : writer_(writer) {}
|
||||||
|
|
||||||
TextFormatter& operator=(const TextFormatter&) = delete;
|
TextFormatter& operator=(const TextFormatter&) = delete;
|
||||||
|
|
||||||
// Returns the number of bytes sent to the TWriter implementation.
|
// Returns the number of bytes sent to the TWriter implementation.
|
||||||
size_t bytesWritten() const {
|
size_t bytesWritten() const {
|
||||||
return _writer.count();
|
return writer_.count();
|
||||||
}
|
}
|
||||||
|
|
||||||
void writeBoolean(bool value) {
|
void writeBoolean(bool value) {
|
||||||
@ -146,28 +146,28 @@ class TextFormatter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void writeRaw(const char* s) {
|
void writeRaw(const char* s) {
|
||||||
_writer.write(reinterpret_cast<const uint8_t*>(s), strlen(s));
|
writer_.write(reinterpret_cast<const uint8_t*>(s), strlen(s));
|
||||||
}
|
}
|
||||||
|
|
||||||
void writeRaw(const char* s, size_t n) {
|
void writeRaw(const char* s, size_t n) {
|
||||||
_writer.write(reinterpret_cast<const uint8_t*>(s), n);
|
writer_.write(reinterpret_cast<const uint8_t*>(s), n);
|
||||||
}
|
}
|
||||||
|
|
||||||
void writeRaw(const char* begin, const char* end) {
|
void writeRaw(const char* begin, const char* end) {
|
||||||
_writer.write(reinterpret_cast<const uint8_t*>(begin),
|
writer_.write(reinterpret_cast<const uint8_t*>(begin),
|
||||||
static_cast<size_t>(end - begin));
|
static_cast<size_t>(end - begin));
|
||||||
}
|
}
|
||||||
|
|
||||||
template <size_t N>
|
template <size_t N>
|
||||||
void writeRaw(const char (&s)[N]) {
|
void writeRaw(const char (&s)[N]) {
|
||||||
_writer.write(reinterpret_cast<const uint8_t*>(s), N - 1);
|
writer_.write(reinterpret_cast<const uint8_t*>(s), N - 1);
|
||||||
}
|
}
|
||||||
void writeRaw(char c) {
|
void writeRaw(char c) {
|
||||||
_writer.write(static_cast<uint8_t>(c));
|
writer_.write(static_cast<uint8_t>(c));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
CountingDecorator<TWriter> _writer;
|
CountingDecorator<TWriter> writer_;
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -31,31 +31,31 @@ inline bool isLowSurrogate(uint16_t codeunit) {
|
|||||||
|
|
||||||
class Codepoint {
|
class Codepoint {
|
||||||
public:
|
public:
|
||||||
Codepoint() : _highSurrogate(0), _codepoint(0) {}
|
Codepoint() : highSurrogate_(0), codepoint_(0) {}
|
||||||
|
|
||||||
bool append(uint16_t codeunit) {
|
bool append(uint16_t codeunit) {
|
||||||
if (isHighSurrogate(codeunit)) {
|
if (isHighSurrogate(codeunit)) {
|
||||||
_highSurrogate = codeunit & 0x3FF;
|
highSurrogate_ = codeunit & 0x3FF;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isLowSurrogate(codeunit)) {
|
if (isLowSurrogate(codeunit)) {
|
||||||
_codepoint =
|
codepoint_ =
|
||||||
uint32_t(0x10000 + ((_highSurrogate << 10) | (codeunit & 0x3FF)));
|
uint32_t(0x10000 + ((highSurrogate_ << 10) | (codeunit & 0x3FF)));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
_codepoint = codeunit;
|
codepoint_ = codeunit;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t value() const {
|
uint32_t value() const {
|
||||||
return _codepoint;
|
return codepoint_;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint16_t _highSurrogate;
|
uint16_t highSurrogate_;
|
||||||
uint32_t _codepoint;
|
uint32_t codepoint_;
|
||||||
};
|
};
|
||||||
} // namespace Utf16
|
} // namespace Utf16
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -40,7 +40,7 @@ constexpr size_t sizeofString(size_t n) {
|
|||||||
class MemoryPool {
|
class MemoryPool {
|
||||||
public:
|
public:
|
||||||
MemoryPool(size_t capa, Allocator* allocator = DefaultAllocator::instance())
|
MemoryPool(size_t capa, Allocator* allocator = DefaultAllocator::instance())
|
||||||
: _allocator(allocator), _overflowed(false) {
|
: allocator_(allocator), overflowed_(false) {
|
||||||
allocPool(addPadding(capa));
|
allocPool(addPadding(capa));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,48 +55,48 @@ class MemoryPool {
|
|||||||
MemoryPool& operator=(MemoryPool&& src) {
|
MemoryPool& operator=(MemoryPool&& src) {
|
||||||
deallocAllStrings();
|
deallocAllStrings();
|
||||||
deallocPool();
|
deallocPool();
|
||||||
_allocator = src._allocator;
|
allocator_ = src.allocator_;
|
||||||
_begin = src._begin;
|
begin_ = src.begin_;
|
||||||
_end = src._end;
|
end_ = src.end_;
|
||||||
_right = src._right;
|
right_ = src.right_;
|
||||||
_overflowed = src._overflowed;
|
overflowed_ = src.overflowed_;
|
||||||
src._begin = src._end = src._right = nullptr;
|
src.begin_ = src.end_ = src.right_ = nullptr;
|
||||||
_strings = src._strings;
|
strings_ = src.strings_;
|
||||||
src._strings = nullptr;
|
src.strings_ = nullptr;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Allocator* allocator() const {
|
Allocator* allocator() const {
|
||||||
return _allocator;
|
return allocator_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void reallocPool(size_t requiredSize) {
|
void reallocPool(size_t requiredSize) {
|
||||||
size_t capa = addPadding(requiredSize);
|
size_t capa = addPadding(requiredSize);
|
||||||
if (capa == capacity())
|
if (capa == capacity())
|
||||||
return;
|
return;
|
||||||
_allocator->deallocate(_begin);
|
allocator_->deallocate(begin_);
|
||||||
allocPool(requiredSize);
|
allocPool(requiredSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
void* buffer() {
|
void* buffer() {
|
||||||
return _begin; // NOLINT(clang-analyzer-unix.Malloc)
|
return begin_; // NOLINT(clang-analyzer-unix.Malloc)
|
||||||
// movePointers() alters this pointer
|
// movePointers() alters this pointer
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gets the capacity of the memoryPool in bytes
|
// Gets the capacity of the memoryPool in bytes
|
||||||
size_t capacity() const {
|
size_t capacity() const {
|
||||||
return size_t(_end - _begin);
|
return size_t(end_ - begin_);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t size() const {
|
size_t size() const {
|
||||||
size_t total = size_t(_end - _right);
|
size_t total = size_t(end_ - right_);
|
||||||
for (auto node = _strings; node; node = node->next)
|
for (auto node = strings_; node; node = node->next)
|
||||||
total += sizeofString(node->length);
|
total += sizeofString(node->length);
|
||||||
return total;
|
return total;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool overflowed() const {
|
bool overflowed() const {
|
||||||
return _overflowed;
|
return overflowed_;
|
||||||
}
|
}
|
||||||
|
|
||||||
VariantSlot* allocVariant() {
|
VariantSlot* allocVariant() {
|
||||||
@ -131,13 +131,13 @@ class MemoryPool {
|
|||||||
|
|
||||||
void addStringToList(StringNode* node) {
|
void addStringToList(StringNode* node) {
|
||||||
ARDUINOJSON_ASSERT(node != nullptr);
|
ARDUINOJSON_ASSERT(node != nullptr);
|
||||||
node->next = _strings;
|
node->next = strings_;
|
||||||
_strings = node;
|
strings_ = node;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TAdaptedString>
|
template <typename TAdaptedString>
|
||||||
StringNode* findString(const TAdaptedString& str) const {
|
StringNode* findString(const TAdaptedString& str) const {
|
||||||
for (auto node = _strings; node; node = node->next) {
|
for (auto node = strings_; node; node = node->next) {
|
||||||
if (stringEquals(str, adaptString(node->data, node->length)))
|
if (stringEquals(str, adaptString(node->data, node->length)))
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
@ -146,12 +146,12 @@ class MemoryPool {
|
|||||||
|
|
||||||
StringNode* allocString(size_t length) {
|
StringNode* allocString(size_t length) {
|
||||||
auto node = reinterpret_cast<StringNode*>(
|
auto node = reinterpret_cast<StringNode*>(
|
||||||
_allocator->allocate(sizeofString(length)));
|
allocator_->allocate(sizeofString(length)));
|
||||||
if (node) {
|
if (node) {
|
||||||
node->length = uint16_t(length);
|
node->length = uint16_t(length);
|
||||||
node->references = 1;
|
node->references = 1;
|
||||||
} else {
|
} else {
|
||||||
_overflowed = true;
|
overflowed_ = true;
|
||||||
}
|
}
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
@ -159,30 +159,30 @@ class MemoryPool {
|
|||||||
StringNode* reallocString(StringNode* node, size_t length) {
|
StringNode* reallocString(StringNode* node, size_t length) {
|
||||||
ARDUINOJSON_ASSERT(node != nullptr);
|
ARDUINOJSON_ASSERT(node != nullptr);
|
||||||
auto newNode = reinterpret_cast<StringNode*>(
|
auto newNode = reinterpret_cast<StringNode*>(
|
||||||
_allocator->reallocate(node, sizeofString(length)));
|
allocator_->reallocate(node, sizeofString(length)));
|
||||||
if (newNode) {
|
if (newNode) {
|
||||||
newNode->length = uint16_t(length);
|
newNode->length = uint16_t(length);
|
||||||
} else {
|
} else {
|
||||||
_overflowed = true;
|
overflowed_ = true;
|
||||||
_allocator->deallocate(node);
|
allocator_->deallocate(node);
|
||||||
}
|
}
|
||||||
return newNode;
|
return newNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
void deallocString(StringNode* node) {
|
void deallocString(StringNode* node) {
|
||||||
_allocator->deallocate(node);
|
allocator_->deallocate(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
void dereferenceString(const char* s) {
|
void dereferenceString(const char* s) {
|
||||||
StringNode* prev = nullptr;
|
StringNode* prev = nullptr;
|
||||||
for (auto node = _strings; node; node = node->next) {
|
for (auto node = strings_; node; node = node->next) {
|
||||||
if (node->data == s) {
|
if (node->data == s) {
|
||||||
if (--node->references == 0) {
|
if (--node->references == 0) {
|
||||||
if (prev)
|
if (prev)
|
||||||
prev->next = node->next;
|
prev->next = node->next;
|
||||||
else
|
else
|
||||||
_strings = node->next;
|
strings_ = node->next;
|
||||||
_allocator->deallocate(node);
|
allocator_->deallocate(node);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -191,17 +191,17 @@ class MemoryPool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void clear() {
|
void clear() {
|
||||||
_right = _end;
|
right_ = end_;
|
||||||
_overflowed = false;
|
overflowed_ = false;
|
||||||
deallocAllStrings();
|
deallocAllStrings();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool canAlloc(size_t bytes) const {
|
bool canAlloc(size_t bytes) const {
|
||||||
return _begin + bytes <= _right;
|
return begin_ + bytes <= right_;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool owns(void* p) const {
|
bool owns(void* p) const {
|
||||||
return _begin <= p && p < _end;
|
return begin_ <= p && p < end_;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Workaround for missing placement new
|
// Workaround for missing placement new
|
||||||
@ -214,8 +214,8 @@ class MemoryPool {
|
|||||||
if (bytes_reclaimed == 0)
|
if (bytes_reclaimed == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
void* old_ptr = _begin;
|
void* old_ptr = begin_;
|
||||||
void* new_ptr = _allocator->reallocate(old_ptr, capacity());
|
void* new_ptr = allocator_->reallocate(old_ptr, capacity());
|
||||||
|
|
||||||
ptrdiff_t ptr_offset =
|
ptrdiff_t ptr_offset =
|
||||||
static_cast<char*>(new_ptr) - static_cast<char*>(old_ptr);
|
static_cast<char*>(new_ptr) - static_cast<char*>(old_ptr);
|
||||||
@ -227,37 +227,37 @@ class MemoryPool {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
ptrdiff_t squash() {
|
ptrdiff_t squash() {
|
||||||
char* new_right = addPadding(_begin);
|
char* new_right = addPadding(begin_);
|
||||||
if (new_right >= _right)
|
if (new_right >= right_)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
size_t right_size = static_cast<size_t>(_end - _right);
|
size_t right_size = static_cast<size_t>(end_ - right_);
|
||||||
memmove(new_right, _right, right_size);
|
memmove(new_right, right_, right_size);
|
||||||
|
|
||||||
ptrdiff_t bytes_reclaimed = _right - new_right;
|
ptrdiff_t bytes_reclaimed = right_ - new_right;
|
||||||
_right = new_right;
|
right_ = new_right;
|
||||||
_end = new_right + right_size;
|
end_ = new_right + right_size;
|
||||||
return bytes_reclaimed;
|
return bytes_reclaimed;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Move all pointers together
|
// Move all pointers together
|
||||||
// This funcion is called after a realloc.
|
// This funcion is called after a realloc.
|
||||||
void movePointers(ptrdiff_t offset) {
|
void movePointers(ptrdiff_t offset) {
|
||||||
_begin += offset;
|
begin_ += offset;
|
||||||
_right += offset;
|
right_ += offset;
|
||||||
_end += offset;
|
end_ += offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
void checkInvariants() {
|
void checkInvariants() {
|
||||||
ARDUINOJSON_ASSERT(_begin <= _right);
|
ARDUINOJSON_ASSERT(begin_ <= right_);
|
||||||
ARDUINOJSON_ASSERT(_right <= _end);
|
ARDUINOJSON_ASSERT(right_ <= end_);
|
||||||
ARDUINOJSON_ASSERT(isAligned(_right));
|
ARDUINOJSON_ASSERT(isAligned(right_));
|
||||||
}
|
}
|
||||||
|
|
||||||
void deallocAllStrings() {
|
void deallocAllStrings() {
|
||||||
while (_strings) {
|
while (strings_) {
|
||||||
auto node = _strings;
|
auto node = strings_;
|
||||||
_strings = node->next;
|
strings_ = node->next;
|
||||||
deallocString(node);
|
deallocString(node);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -269,31 +269,31 @@ class MemoryPool {
|
|||||||
|
|
||||||
void* allocRight(size_t bytes) {
|
void* allocRight(size_t bytes) {
|
||||||
if (!canAlloc(bytes)) {
|
if (!canAlloc(bytes)) {
|
||||||
_overflowed = true;
|
overflowed_ = true;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
_right -= bytes;
|
right_ -= bytes;
|
||||||
return _right;
|
return right_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void allocPool(size_t capa) {
|
void allocPool(size_t capa) {
|
||||||
auto buf = capa ? reinterpret_cast<char*>(_allocator->allocate(capa)) : 0;
|
auto buf = capa ? reinterpret_cast<char*>(allocator_->allocate(capa)) : 0;
|
||||||
_begin = buf;
|
begin_ = buf;
|
||||||
_end = _right = buf ? buf + capa : 0;
|
end_ = right_ = buf ? buf + capa : 0;
|
||||||
ARDUINOJSON_ASSERT(isAligned(_begin));
|
ARDUINOJSON_ASSERT(isAligned(begin_));
|
||||||
ARDUINOJSON_ASSERT(isAligned(_right));
|
ARDUINOJSON_ASSERT(isAligned(right_));
|
||||||
ARDUINOJSON_ASSERT(isAligned(_end));
|
ARDUINOJSON_ASSERT(isAligned(end_));
|
||||||
}
|
}
|
||||||
|
|
||||||
void deallocPool() {
|
void deallocPool() {
|
||||||
if (_begin)
|
if (begin_)
|
||||||
_allocator->deallocate(_begin);
|
allocator_->deallocate(begin_);
|
||||||
}
|
}
|
||||||
|
|
||||||
Allocator* _allocator;
|
Allocator* allocator_;
|
||||||
char *_begin, *_right, *_end;
|
char *begin_, *right_, *end_;
|
||||||
bool _overflowed;
|
bool overflowed_;
|
||||||
StringNode* _strings = nullptr;
|
StringNode* strings_ = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename TAdaptedString>
|
template <typename TAdaptedString>
|
||||||
|
@ -12,43 +12,43 @@ ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
class SerializedValue {
|
class SerializedValue {
|
||||||
public:
|
public:
|
||||||
explicit SerializedValue(T str) : _str(str) {}
|
explicit SerializedValue(T str) : str_(str) {}
|
||||||
operator T() const {
|
operator T() const {
|
||||||
return _str;
|
return str_;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* data() const {
|
const char* data() const {
|
||||||
return _str.c_str();
|
return str_.c_str();
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t size() const {
|
size_t size() const {
|
||||||
// CAUTION: the old Arduino String doesn't have size()
|
// CAUTION: the old Arduino String doesn't have size()
|
||||||
return _str.length();
|
return str_.length();
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
T _str;
|
T str_;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename TChar>
|
template <typename TChar>
|
||||||
class SerializedValue<TChar*> {
|
class SerializedValue<TChar*> {
|
||||||
public:
|
public:
|
||||||
explicit SerializedValue(TChar* p, size_t n) : _data(p), _size(n) {}
|
explicit SerializedValue(TChar* p, size_t n) : data_(p), size_(n) {}
|
||||||
operator TChar*() const {
|
operator TChar*() const {
|
||||||
return _data;
|
return data_;
|
||||||
}
|
}
|
||||||
|
|
||||||
TChar* data() const {
|
TChar* data() const {
|
||||||
return _data;
|
return data_;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t size() const {
|
size_t size() const {
|
||||||
return _size;
|
return size_;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
TChar* _data;
|
TChar* data_;
|
||||||
size_t _size;
|
size_t size_;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
|
@ -18,17 +18,17 @@ class MsgPackDeserializer {
|
|||||||
public:
|
public:
|
||||||
MsgPackDeserializer(MemoryPool* pool, TReader reader,
|
MsgPackDeserializer(MemoryPool* pool, TReader reader,
|
||||||
TStringStorage stringStorage)
|
TStringStorage stringStorage)
|
||||||
: _pool(pool),
|
: pool_(pool),
|
||||||
_reader(reader),
|
reader_(reader),
|
||||||
_stringStorage(stringStorage),
|
stringStorage_(stringStorage),
|
||||||
_foundSomething(false) {}
|
foundSomething_(false) {}
|
||||||
|
|
||||||
template <typename TFilter>
|
template <typename TFilter>
|
||||||
DeserializationError parse(VariantData& variant, TFilter filter,
|
DeserializationError parse(VariantData& variant, TFilter filter,
|
||||||
DeserializationOption::NestingLimit nestingLimit) {
|
DeserializationOption::NestingLimit nestingLimit) {
|
||||||
DeserializationError::Code err;
|
DeserializationError::Code err;
|
||||||
err = parseVariant(&variant, filter, nestingLimit);
|
err = parseVariant(&variant, filter, nestingLimit);
|
||||||
return _foundSomething ? err : DeserializationError::EmptyInput;
|
return foundSomething_ ? err : DeserializationError::EmptyInput;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -43,7 +43,7 @@ class MsgPackDeserializer {
|
|||||||
if (err)
|
if (err)
|
||||||
return err;
|
return err;
|
||||||
|
|
||||||
_foundSomething = true;
|
foundSomething_ = true;
|
||||||
|
|
||||||
bool allowValue = filter.allowValue();
|
bool allowValue = filter.allowValue();
|
||||||
|
|
||||||
@ -224,7 +224,7 @@ class MsgPackDeserializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
DeserializationError::Code readByte(uint8_t& value) {
|
DeserializationError::Code readByte(uint8_t& value) {
|
||||||
int c = _reader.read();
|
int c = reader_.read();
|
||||||
if (c < 0)
|
if (c < 0)
|
||||||
return DeserializationError::IncompleteInput;
|
return DeserializationError::IncompleteInput;
|
||||||
value = static_cast<uint8_t>(c);
|
value = static_cast<uint8_t>(c);
|
||||||
@ -232,7 +232,7 @@ class MsgPackDeserializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
DeserializationError::Code readBytes(uint8_t* p, size_t n) {
|
DeserializationError::Code readBytes(uint8_t* p, size_t n) {
|
||||||
if (_reader.readBytes(reinterpret_cast<char*>(p), n) == n)
|
if (reader_.readBytes(reinterpret_cast<char*>(p), n) == n)
|
||||||
return DeserializationError::Ok;
|
return DeserializationError::Ok;
|
||||||
return DeserializationError::IncompleteInput;
|
return DeserializationError::IncompleteInput;
|
||||||
}
|
}
|
||||||
@ -244,7 +244,7 @@ class MsgPackDeserializer {
|
|||||||
|
|
||||||
DeserializationError::Code skipBytes(size_t n) {
|
DeserializationError::Code skipBytes(size_t n) {
|
||||||
for (; n; --n) {
|
for (; n; --n) {
|
||||||
if (_reader.read() < 0)
|
if (reader_.read() < 0)
|
||||||
return DeserializationError::IncompleteInput;
|
return DeserializationError::IncompleteInput;
|
||||||
}
|
}
|
||||||
return DeserializationError::Ok;
|
return DeserializationError::Ok;
|
||||||
@ -371,14 +371,14 @@ class MsgPackDeserializer {
|
|||||||
if (err)
|
if (err)
|
||||||
return err;
|
return err;
|
||||||
|
|
||||||
variant->setString(_stringStorage.save());
|
variant->setString(stringStorage_.save());
|
||||||
return DeserializationError::Ok;
|
return DeserializationError::Ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
DeserializationError::Code readString(size_t n) {
|
DeserializationError::Code readString(size_t n) {
|
||||||
DeserializationError::Code err;
|
DeserializationError::Code err;
|
||||||
|
|
||||||
_stringStorage.startString();
|
stringStorage_.startString();
|
||||||
for (; n; --n) {
|
for (; n; --n) {
|
||||||
uint8_t c;
|
uint8_t c;
|
||||||
|
|
||||||
@ -386,10 +386,10 @@ class MsgPackDeserializer {
|
|||||||
if (err)
|
if (err)
|
||||||
return err;
|
return err;
|
||||||
|
|
||||||
_stringStorage.append(static_cast<char>(c));
|
stringStorage_.append(static_cast<char>(c));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!_stringStorage.isValid())
|
if (!stringStorage_.isValid())
|
||||||
return DeserializationError::NoMemory;
|
return DeserializationError::NoMemory;
|
||||||
|
|
||||||
return DeserializationError::Ok;
|
return DeserializationError::Ok;
|
||||||
@ -435,7 +435,7 @@ class MsgPackDeserializer {
|
|||||||
|
|
||||||
if (memberFilter.allow()) {
|
if (memberFilter.allow()) {
|
||||||
ARDUINOJSON_ASSERT(array != 0);
|
ARDUINOJSON_ASSERT(array != 0);
|
||||||
value = collectionAddElement(array, _pool);
|
value = collectionAddElement(array, pool_);
|
||||||
if (!value)
|
if (!value)
|
||||||
return DeserializationError::NoMemory;
|
return DeserializationError::NoMemory;
|
||||||
} else {
|
} else {
|
||||||
@ -486,7 +486,7 @@ class MsgPackDeserializer {
|
|||||||
if (err)
|
if (err)
|
||||||
return err;
|
return err;
|
||||||
|
|
||||||
JsonString key = _stringStorage.str();
|
JsonString key = stringStorage_.str();
|
||||||
TFilter memberFilter = filter[key.c_str()];
|
TFilter memberFilter = filter[key.c_str()];
|
||||||
VariantData* member;
|
VariantData* member;
|
||||||
|
|
||||||
@ -494,9 +494,9 @@ class MsgPackDeserializer {
|
|||||||
ARDUINOJSON_ASSERT(object != 0);
|
ARDUINOJSON_ASSERT(object != 0);
|
||||||
|
|
||||||
// Save key in memory pool.
|
// Save key in memory pool.
|
||||||
key = _stringStorage.save();
|
key = stringStorage_.save();
|
||||||
|
|
||||||
VariantSlot* slot = _pool->allocVariant();
|
VariantSlot* slot = pool_->allocVariant();
|
||||||
if (!slot)
|
if (!slot)
|
||||||
return DeserializationError::NoMemory;
|
return DeserializationError::NoMemory;
|
||||||
|
|
||||||
@ -554,10 +554,10 @@ class MsgPackDeserializer {
|
|||||||
return skipBytes(size + 1U);
|
return skipBytes(size + 1U);
|
||||||
}
|
}
|
||||||
|
|
||||||
MemoryPool* _pool;
|
MemoryPool* pool_;
|
||||||
TReader _reader;
|
TReader reader_;
|
||||||
TStringStorage _stringStorage;
|
TStringStorage stringStorage_;
|
||||||
bool _foundSomething;
|
bool foundSomething_;
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -19,7 +19,7 @@ class MsgPackSerializer : public Visitor<size_t> {
|
|||||||
public:
|
public:
|
||||||
static const bool producesText = false;
|
static const bool producesText = false;
|
||||||
|
|
||||||
MsgPackSerializer(TWriter writer) : _writer(writer) {}
|
MsgPackSerializer(TWriter writer) : writer_(writer) {}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
typename enable_if<sizeof(T) == 4, size_t>::type visitFloat(T value32) {
|
typename enable_if<sizeof(T) == 4, size_t>::type visitFloat(T value32) {
|
||||||
@ -177,15 +177,15 @@ class MsgPackSerializer : public Visitor<size_t> {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
size_t bytesWritten() const {
|
size_t bytesWritten() const {
|
||||||
return _writer.count();
|
return writer_.count();
|
||||||
}
|
}
|
||||||
|
|
||||||
void writeByte(uint8_t c) {
|
void writeByte(uint8_t c) {
|
||||||
_writer.write(c);
|
writer_.write(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
void writeBytes(const uint8_t* p, size_t n) {
|
void writeBytes(const uint8_t* p, size_t n) {
|
||||||
_writer.write(p, n);
|
writer_.write(p, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
@ -194,7 +194,7 @@ class MsgPackSerializer : public Visitor<size_t> {
|
|||||||
writeBytes(reinterpret_cast<uint8_t*>(&value), sizeof(value));
|
writeBytes(reinterpret_cast<uint8_t*>(&value), sizeof(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
CountingDecorator<TWriter> _writer;
|
CountingDecorator<TWriter> writer_;
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -20,61 +20,61 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
|
|||||||
typedef JsonObjectIterator iterator;
|
typedef JsonObjectIterator iterator;
|
||||||
|
|
||||||
// Creates an unbound reference.
|
// Creates an unbound reference.
|
||||||
FORCE_INLINE JsonObject() : _data(0), _pool(0) {}
|
FORCE_INLINE JsonObject() : data_(0), pool_(0) {}
|
||||||
|
|
||||||
// INTERNAL USE ONLY
|
// INTERNAL USE ONLY
|
||||||
FORCE_INLINE JsonObject(detail::MemoryPool* buf, detail::CollectionData* data)
|
FORCE_INLINE JsonObject(detail::MemoryPool* buf, detail::CollectionData* data)
|
||||||
: _data(data), _pool(buf) {}
|
: data_(data), pool_(buf) {}
|
||||||
|
|
||||||
operator JsonVariant() const {
|
operator JsonVariant() const {
|
||||||
void* data = _data; // prevent warning cast-align
|
void* data = data_; // prevent warning cast-align
|
||||||
return JsonVariant(_pool, reinterpret_cast<detail::VariantData*>(data));
|
return JsonVariant(pool_, reinterpret_cast<detail::VariantData*>(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
operator JsonObjectConst() const {
|
operator JsonObjectConst() const {
|
||||||
return JsonObjectConst(_data);
|
return JsonObjectConst(data_);
|
||||||
}
|
}
|
||||||
|
|
||||||
operator JsonVariantConst() const {
|
operator JsonVariantConst() const {
|
||||||
return JsonVariantConst(collectionToVariant(_data));
|
return JsonVariantConst(collectionToVariant(data_));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if the reference is unbound.
|
// Returns true if the reference is unbound.
|
||||||
// https://arduinojson.org/v6/api/jsonobject/isnull/
|
// https://arduinojson.org/v6/api/jsonobject/isnull/
|
||||||
FORCE_INLINE bool isNull() const {
|
FORCE_INLINE bool isNull() const {
|
||||||
return _data == 0;
|
return data_ == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if the reference is bound.
|
// Returns true if the reference is bound.
|
||||||
// https://arduinojson.org/v6/api/jsonobject/isnull/
|
// https://arduinojson.org/v6/api/jsonobject/isnull/
|
||||||
FORCE_INLINE operator bool() const {
|
FORCE_INLINE operator bool() const {
|
||||||
return _data != 0;
|
return data_ != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the number of bytes occupied by the object.
|
// Returns the number of bytes occupied by the object.
|
||||||
// https://arduinojson.org/v6/api/jsonobject/memoryusage/
|
// https://arduinojson.org/v6/api/jsonobject/memoryusage/
|
||||||
FORCE_INLINE size_t memoryUsage() const {
|
FORCE_INLINE size_t memoryUsage() const {
|
||||||
return _data ? _data->memoryUsage() : 0;
|
return data_ ? data_->memoryUsage() : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the depth (nesting level) of the object.
|
// Returns the depth (nesting level) of the object.
|
||||||
// https://arduinojson.org/v6/api/jsonobject/nesting/
|
// https://arduinojson.org/v6/api/jsonobject/nesting/
|
||||||
FORCE_INLINE size_t nesting() const {
|
FORCE_INLINE size_t nesting() const {
|
||||||
return variantNesting(collectionToVariant(_data));
|
return variantNesting(collectionToVariant(data_));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the number of members in the object.
|
// Returns the number of members in the object.
|
||||||
// https://arduinojson.org/v6/api/jsonobject/size/
|
// https://arduinojson.org/v6/api/jsonobject/size/
|
||||||
FORCE_INLINE size_t size() const {
|
FORCE_INLINE size_t size() const {
|
||||||
return _data ? _data->size() : 0;
|
return data_ ? data_->size() : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns an iterator to the first key-value pair of the object.
|
// Returns an iterator to the first key-value pair of the object.
|
||||||
// https://arduinojson.org/v6/api/jsonobject/begin/
|
// https://arduinojson.org/v6/api/jsonobject/begin/
|
||||||
FORCE_INLINE iterator begin() const {
|
FORCE_INLINE iterator begin() const {
|
||||||
if (!_data)
|
if (!data_)
|
||||||
return iterator();
|
return iterator();
|
||||||
return iterator(_pool, _data->head());
|
return iterator(pool_, data_->head());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns an iterator following the last key-value pair of the object.
|
// Returns an iterator following the last key-value pair of the object.
|
||||||
@ -87,18 +87,18 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
|
|||||||
// ⚠️ Doesn't release the memory associated with the removed members.
|
// ⚠️ Doesn't release the memory associated with the removed members.
|
||||||
// https://arduinojson.org/v6/api/jsonobject/clear/
|
// https://arduinojson.org/v6/api/jsonobject/clear/
|
||||||
void clear() const {
|
void clear() const {
|
||||||
collectionClear(_data, _pool);
|
collectionClear(data_, pool_);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copies an object.
|
// Copies an object.
|
||||||
// https://arduinojson.org/v6/api/jsonobject/set/
|
// https://arduinojson.org/v6/api/jsonobject/set/
|
||||||
FORCE_INLINE bool set(JsonObjectConst src) {
|
FORCE_INLINE bool set(JsonObjectConst src) {
|
||||||
return collectionCopy(_data, src._data, _pool);
|
return collectionCopy(data_, src.data_, pool_);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compares the content of two objects.
|
// Compares the content of two objects.
|
||||||
FORCE_INLINE bool operator==(JsonObject rhs) const {
|
FORCE_INLINE bool operator==(JsonObject rhs) const {
|
||||||
return JsonObjectConst(_data) == JsonObjectConst(rhs._data);
|
return JsonObjectConst(data_) == JsonObjectConst(rhs.data_);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gets or sets the member with specified key.
|
// Gets or sets the member with specified key.
|
||||||
@ -125,7 +125,7 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
|
|||||||
// ⚠️ Doesn't release the memory associated with the removed member.
|
// ⚠️ Doesn't release the memory associated with the removed member.
|
||||||
// https://arduinojson.org/v6/api/jsonobject/remove/
|
// https://arduinojson.org/v6/api/jsonobject/remove/
|
||||||
FORCE_INLINE void remove(iterator it) const {
|
FORCE_INLINE void remove(iterator it) const {
|
||||||
collectionRemove(_data, it._slot, _pool);
|
collectionRemove(data_, it.slot_, pool_);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Removes the member with the specified key.
|
// Removes the member with the specified key.
|
||||||
@ -133,7 +133,7 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
|
|||||||
// https://arduinojson.org/v6/api/jsonobject/remove/
|
// https://arduinojson.org/v6/api/jsonobject/remove/
|
||||||
template <typename TString>
|
template <typename TString>
|
||||||
FORCE_INLINE void remove(const TString& key) const {
|
FORCE_INLINE void remove(const TString& key) const {
|
||||||
collectionRemoveMember(_data, detail::adaptString(key), _pool);
|
collectionRemoveMember(data_, detail::adaptString(key), pool_);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Removes the member with the specified key.
|
// Removes the member with the specified key.
|
||||||
@ -141,7 +141,7 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
|
|||||||
// https://arduinojson.org/v6/api/jsonobject/remove/
|
// https://arduinojson.org/v6/api/jsonobject/remove/
|
||||||
template <typename TChar>
|
template <typename TChar>
|
||||||
FORCE_INLINE void remove(TChar* key) const {
|
FORCE_INLINE void remove(TChar* key) const {
|
||||||
collectionRemoveMember(_data, detail::adaptString(key), _pool);
|
collectionRemoveMember(data_, detail::adaptString(key), pool_);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if the object contains the specified key.
|
// Returns true if the object contains the specified key.
|
||||||
@ -150,7 +150,7 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
|
|||||||
FORCE_INLINE
|
FORCE_INLINE
|
||||||
typename detail::enable_if<detail::IsString<TString>::value, bool>::type
|
typename detail::enable_if<detail::IsString<TString>::value, bool>::type
|
||||||
containsKey(const TString& key) const {
|
containsKey(const TString& key) const {
|
||||||
return collectionGetMember(_data, detail::adaptString(key)) != 0;
|
return collectionGetMember(data_, detail::adaptString(key)) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if the object contains the specified key.
|
// Returns true if the object contains the specified key.
|
||||||
@ -159,7 +159,7 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
|
|||||||
FORCE_INLINE
|
FORCE_INLINE
|
||||||
typename detail::enable_if<detail::IsString<TChar*>::value, bool>::type
|
typename detail::enable_if<detail::IsString<TChar*>::value, bool>::type
|
||||||
containsKey(TChar* key) const {
|
containsKey(TChar* key) const {
|
||||||
return collectionGetMember(_data, detail::adaptString(key)) != 0;
|
return collectionGetMember(data_, detail::adaptString(key)) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Creates an array and adds it to the object.
|
// Creates an array and adds it to the object.
|
||||||
@ -188,24 +188,24 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
detail::MemoryPool* getPool() const {
|
detail::MemoryPool* getPool() const {
|
||||||
return _pool;
|
return pool_;
|
||||||
}
|
}
|
||||||
|
|
||||||
detail::VariantData* getData() const {
|
detail::VariantData* getData() const {
|
||||||
return detail::collectionToVariant(_data);
|
return detail::collectionToVariant(data_);
|
||||||
}
|
}
|
||||||
|
|
||||||
detail::VariantData* getOrCreateData() const {
|
detail::VariantData* getOrCreateData() const {
|
||||||
return detail::collectionToVariant(_data);
|
return detail::collectionToVariant(data_);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TAdaptedString>
|
template <typename TAdaptedString>
|
||||||
void removeMember(TAdaptedString key) const {
|
void removeMember(TAdaptedString key) const {
|
||||||
collectionRemove(_data, _data->get(key), _pool);
|
collectionRemove(data_, data_->get(key), pool_);
|
||||||
}
|
}
|
||||||
|
|
||||||
detail::CollectionData* _data;
|
detail::CollectionData* data_;
|
||||||
detail::MemoryPool* _pool;
|
detail::MemoryPool* pool_;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
|
@ -19,51 +19,51 @@ class JsonObjectConst : public detail::VariantOperators<JsonObjectConst> {
|
|||||||
typedef JsonObjectConstIterator iterator;
|
typedef JsonObjectConstIterator iterator;
|
||||||
|
|
||||||
// Creates an unbound reference.
|
// Creates an unbound reference.
|
||||||
JsonObjectConst() : _data(0) {}
|
JsonObjectConst() : data_(0) {}
|
||||||
|
|
||||||
// INTERNAL USE ONLY
|
// INTERNAL USE ONLY
|
||||||
JsonObjectConst(const detail::CollectionData* data) : _data(data) {}
|
JsonObjectConst(const detail::CollectionData* data) : data_(data) {}
|
||||||
|
|
||||||
operator JsonVariantConst() const {
|
operator JsonVariantConst() const {
|
||||||
return JsonVariantConst(collectionToVariant(_data));
|
return JsonVariantConst(collectionToVariant(data_));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if the reference is unbound.
|
// Returns true if the reference is unbound.
|
||||||
// https://arduinojson.org/v6/api/jsonobjectconst/isnull/
|
// https://arduinojson.org/v6/api/jsonobjectconst/isnull/
|
||||||
FORCE_INLINE bool isNull() const {
|
FORCE_INLINE bool isNull() const {
|
||||||
return _data == 0;
|
return data_ == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if the reference is bound.
|
// Returns true if the reference is bound.
|
||||||
// https://arduinojson.org/v6/api/jsonobjectconst/isnull/
|
// https://arduinojson.org/v6/api/jsonobjectconst/isnull/
|
||||||
FORCE_INLINE operator bool() const {
|
FORCE_INLINE operator bool() const {
|
||||||
return _data != 0;
|
return data_ != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the number of bytes occupied by the object.
|
// Returns the number of bytes occupied by the object.
|
||||||
// https://arduinojson.org/v6/api/jsonobjectconst/memoryusage/
|
// https://arduinojson.org/v6/api/jsonobjectconst/memoryusage/
|
||||||
FORCE_INLINE size_t memoryUsage() const {
|
FORCE_INLINE size_t memoryUsage() const {
|
||||||
return _data ? _data->memoryUsage() : 0;
|
return data_ ? data_->memoryUsage() : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the depth (nesting level) of the object.
|
// Returns the depth (nesting level) of the object.
|
||||||
// https://arduinojson.org/v6/api/jsonobjectconst/nesting/
|
// https://arduinojson.org/v6/api/jsonobjectconst/nesting/
|
||||||
FORCE_INLINE size_t nesting() const {
|
FORCE_INLINE size_t nesting() const {
|
||||||
return variantNesting(collectionToVariant(_data));
|
return variantNesting(collectionToVariant(data_));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the number of members in the object.
|
// Returns the number of members in the object.
|
||||||
// https://arduinojson.org/v6/api/jsonobjectconst/size/
|
// https://arduinojson.org/v6/api/jsonobjectconst/size/
|
||||||
FORCE_INLINE size_t size() const {
|
FORCE_INLINE size_t size() const {
|
||||||
return _data ? _data->size() : 0;
|
return data_ ? data_->size() : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns an iterator to the first key-value pair of the object.
|
// Returns an iterator to the first key-value pair of the object.
|
||||||
// https://arduinojson.org/v6/api/jsonobjectconst/begin/
|
// https://arduinojson.org/v6/api/jsonobjectconst/begin/
|
||||||
FORCE_INLINE iterator begin() const {
|
FORCE_INLINE iterator begin() const {
|
||||||
if (!_data)
|
if (!data_)
|
||||||
return iterator();
|
return iterator();
|
||||||
return iterator(_data->head());
|
return iterator(data_->head());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns an iterator following the last key-value pair of the object.
|
// Returns an iterator following the last key-value pair of the object.
|
||||||
@ -76,14 +76,14 @@ class JsonObjectConst : public detail::VariantOperators<JsonObjectConst> {
|
|||||||
// https://arduinojson.org/v6/api/jsonobjectconst/containskey/
|
// https://arduinojson.org/v6/api/jsonobjectconst/containskey/
|
||||||
template <typename TString>
|
template <typename TString>
|
||||||
FORCE_INLINE bool containsKey(const TString& key) const {
|
FORCE_INLINE bool containsKey(const TString& key) const {
|
||||||
return collectionGetMember(_data, detail::adaptString(key)) != 0;
|
return collectionGetMember(data_, detail::adaptString(key)) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if the object contains the specified key.
|
// Returns true if the object contains the specified key.
|
||||||
// https://arduinojson.org/v6/api/jsonobjectconst/containskey/
|
// https://arduinojson.org/v6/api/jsonobjectconst/containskey/
|
||||||
template <typename TChar>
|
template <typename TChar>
|
||||||
FORCE_INLINE bool containsKey(TChar* key) const {
|
FORCE_INLINE bool containsKey(TChar* key) const {
|
||||||
return collectionGetMember(_data, detail::adaptString(key)) != 0;
|
return collectionGetMember(data_, detail::adaptString(key)) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gets the member with specified key.
|
// Gets the member with specified key.
|
||||||
@ -93,7 +93,7 @@ class JsonObjectConst : public detail::VariantOperators<JsonObjectConst> {
|
|||||||
JsonVariantConst>::type
|
JsonVariantConst>::type
|
||||||
operator[](const TString& key) const {
|
operator[](const TString& key) const {
|
||||||
return JsonVariantConst(
|
return JsonVariantConst(
|
||||||
collectionGetMember(_data, detail::adaptString(key)));
|
collectionGetMember(data_, detail::adaptString(key)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gets the member with specified key.
|
// Gets the member with specified key.
|
||||||
@ -103,15 +103,15 @@ class JsonObjectConst : public detail::VariantOperators<JsonObjectConst> {
|
|||||||
JsonVariantConst>::type
|
JsonVariantConst>::type
|
||||||
operator[](TChar* key) const {
|
operator[](TChar* key) const {
|
||||||
return JsonVariantConst(
|
return JsonVariantConst(
|
||||||
collectionGetMember(_data, detail::adaptString(key)));
|
collectionGetMember(data_, detail::adaptString(key)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compares objects.
|
// Compares objects.
|
||||||
FORCE_INLINE bool operator==(JsonObjectConst rhs) const {
|
FORCE_INLINE bool operator==(JsonObjectConst rhs) const {
|
||||||
if (_data == rhs._data)
|
if (data_ == rhs.data_)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
if (!_data || !rhs._data)
|
if (!data_ || !rhs.data_)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
size_t count = 0;
|
size_t count = 0;
|
||||||
@ -125,10 +125,10 @@ class JsonObjectConst : public detail::VariantOperators<JsonObjectConst> {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
const detail::VariantData* getData() const {
|
const detail::VariantData* getData() const {
|
||||||
return collectionToVariant(_data);
|
return collectionToVariant(data_);
|
||||||
}
|
}
|
||||||
|
|
||||||
const detail::CollectionData* _data;
|
const detail::CollectionData* data_;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
|
@ -12,112 +12,112 @@ ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE
|
|||||||
class JsonPairPtr {
|
class JsonPairPtr {
|
||||||
public:
|
public:
|
||||||
JsonPairPtr(detail::MemoryPool* pool, detail::VariantSlot* slot)
|
JsonPairPtr(detail::MemoryPool* pool, detail::VariantSlot* slot)
|
||||||
: _pair(pool, slot) {}
|
: pair_(pool, slot) {}
|
||||||
|
|
||||||
const JsonPair* operator->() const {
|
const JsonPair* operator->() const {
|
||||||
return &_pair;
|
return &pair_;
|
||||||
}
|
}
|
||||||
|
|
||||||
const JsonPair& operator*() const {
|
const JsonPair& operator*() const {
|
||||||
return _pair;
|
return pair_;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
JsonPair _pair;
|
JsonPair pair_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class JsonObjectIterator {
|
class JsonObjectIterator {
|
||||||
friend class JsonObject;
|
friend class JsonObject;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
JsonObjectIterator() : _slot(0) {}
|
JsonObjectIterator() : slot_(0) {}
|
||||||
|
|
||||||
explicit JsonObjectIterator(detail::MemoryPool* pool,
|
explicit JsonObjectIterator(detail::MemoryPool* pool,
|
||||||
detail::VariantSlot* slot)
|
detail::VariantSlot* slot)
|
||||||
: _pool(pool), _slot(slot) {}
|
: pool_(pool), slot_(slot) {}
|
||||||
|
|
||||||
JsonPair operator*() const {
|
JsonPair operator*() const {
|
||||||
return JsonPair(_pool, _slot);
|
return JsonPair(pool_, slot_);
|
||||||
}
|
}
|
||||||
JsonPairPtr operator->() {
|
JsonPairPtr operator->() {
|
||||||
return JsonPairPtr(_pool, _slot);
|
return JsonPairPtr(pool_, slot_);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator==(const JsonObjectIterator& other) const {
|
bool operator==(const JsonObjectIterator& other) const {
|
||||||
return _slot == other._slot;
|
return slot_ == other.slot_;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator!=(const JsonObjectIterator& other) const {
|
bool operator!=(const JsonObjectIterator& other) const {
|
||||||
return _slot != other._slot;
|
return slot_ != other.slot_;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonObjectIterator& operator++() {
|
JsonObjectIterator& operator++() {
|
||||||
_slot = _slot->next();
|
slot_ = slot_->next();
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonObjectIterator& operator+=(size_t distance) {
|
JsonObjectIterator& operator+=(size_t distance) {
|
||||||
_slot = _slot->next(distance);
|
slot_ = slot_->next(distance);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
detail::MemoryPool* _pool;
|
detail::MemoryPool* pool_;
|
||||||
detail::VariantSlot* _slot;
|
detail::VariantSlot* slot_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class JsonPairConstPtr {
|
class JsonPairConstPtr {
|
||||||
public:
|
public:
|
||||||
JsonPairConstPtr(const detail::VariantSlot* slot) : _pair(slot) {}
|
JsonPairConstPtr(const detail::VariantSlot* slot) : pair_(slot) {}
|
||||||
|
|
||||||
const JsonPairConst* operator->() const {
|
const JsonPairConst* operator->() const {
|
||||||
return &_pair;
|
return &pair_;
|
||||||
}
|
}
|
||||||
|
|
||||||
const JsonPairConst& operator*() const {
|
const JsonPairConst& operator*() const {
|
||||||
return _pair;
|
return pair_;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
JsonPairConst _pair;
|
JsonPairConst pair_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class JsonObjectConstIterator {
|
class JsonObjectConstIterator {
|
||||||
friend class JsonObject;
|
friend class JsonObject;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
JsonObjectConstIterator() : _slot(0) {}
|
JsonObjectConstIterator() : slot_(0) {}
|
||||||
|
|
||||||
explicit JsonObjectConstIterator(const detail::VariantSlot* slot)
|
explicit JsonObjectConstIterator(const detail::VariantSlot* slot)
|
||||||
: _slot(slot) {}
|
: slot_(slot) {}
|
||||||
|
|
||||||
JsonPairConst operator*() const {
|
JsonPairConst operator*() const {
|
||||||
return JsonPairConst(_slot);
|
return JsonPairConst(slot_);
|
||||||
}
|
}
|
||||||
JsonPairConstPtr operator->() {
|
JsonPairConstPtr operator->() {
|
||||||
return JsonPairConstPtr(_slot);
|
return JsonPairConstPtr(slot_);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator==(const JsonObjectConstIterator& other) const {
|
bool operator==(const JsonObjectConstIterator& other) const {
|
||||||
return _slot == other._slot;
|
return slot_ == other.slot_;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator!=(const JsonObjectConstIterator& other) const {
|
bool operator!=(const JsonObjectConstIterator& other) const {
|
||||||
return _slot != other._slot;
|
return slot_ != other.slot_;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonObjectConstIterator& operator++() {
|
JsonObjectConstIterator& operator++() {
|
||||||
_slot = _slot->next();
|
slot_ = slot_->next();
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonObjectConstIterator& operator+=(size_t distance) {
|
JsonObjectConstIterator& operator+=(size_t distance) {
|
||||||
_slot = _slot->next(distance);
|
slot_ = slot_->next(distance);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const detail::VariantSlot* _slot;
|
const detail::VariantSlot* slot_;
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
||||||
|
@ -17,25 +17,25 @@ class JsonPair {
|
|||||||
// INTERNAL USE ONLY
|
// INTERNAL USE ONLY
|
||||||
JsonPair(detail::MemoryPool* pool, detail::VariantSlot* slot) {
|
JsonPair(detail::MemoryPool* pool, detail::VariantSlot* slot) {
|
||||||
if (slot) {
|
if (slot) {
|
||||||
_key = JsonString(slot->key(), slot->ownsKey() ? JsonString::Copied
|
key_ = JsonString(slot->key(), slot->ownsKey() ? JsonString::Copied
|
||||||
: JsonString::Linked);
|
: JsonString::Linked);
|
||||||
_value = JsonVariant(pool, slot->data());
|
value_ = JsonVariant(pool, slot->data());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the key.
|
// Returns the key.
|
||||||
JsonString key() const {
|
JsonString key() const {
|
||||||
return _key;
|
return key_;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the value.
|
// Returns the value.
|
||||||
JsonVariant value() const {
|
JsonVariant value() const {
|
||||||
return _value;
|
return value_;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
JsonString _key;
|
JsonString key_;
|
||||||
JsonVariant _value;
|
JsonVariant value_;
|
||||||
};
|
};
|
||||||
|
|
||||||
// A read-only key-value pair.
|
// A read-only key-value pair.
|
||||||
@ -44,25 +44,25 @@ class JsonPairConst {
|
|||||||
public:
|
public:
|
||||||
JsonPairConst(const detail::VariantSlot* slot) {
|
JsonPairConst(const detail::VariantSlot* slot) {
|
||||||
if (slot) {
|
if (slot) {
|
||||||
_key = JsonString(slot->key(), slot->ownsKey() ? JsonString::Copied
|
key_ = JsonString(slot->key(), slot->ownsKey() ? JsonString::Copied
|
||||||
: JsonString::Linked);
|
: JsonString::Linked);
|
||||||
_value = JsonVariantConst(slot->data());
|
value_ = JsonVariantConst(slot->data());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the key.
|
// Returns the key.
|
||||||
JsonString key() const {
|
JsonString key() const {
|
||||||
return _key;
|
return key_;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the value.
|
// Returns the value.
|
||||||
JsonVariantConst value() const {
|
JsonVariantConst value() const {
|
||||||
return _value;
|
return value_;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
JsonString _key;
|
JsonString key_;
|
||||||
JsonVariantConst _value;
|
JsonVariantConst value_;
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
||||||
|
@ -18,10 +18,10 @@ class MemberProxy
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
FORCE_INLINE MemberProxy(TUpstream upstream, TStringRef key)
|
FORCE_INLINE MemberProxy(TUpstream upstream, TStringRef key)
|
||||||
: _upstream(upstream), _key(key) {}
|
: upstream_(upstream), key_(key) {}
|
||||||
|
|
||||||
MemberProxy(const MemberProxy& src)
|
MemberProxy(const MemberProxy& src)
|
||||||
: _upstream(src._upstream), _key(src._key) {}
|
: upstream_(src.upstream_), key_(src.key_) {}
|
||||||
|
|
||||||
FORCE_INLINE MemberProxy& operator=(const MemberProxy& src) {
|
FORCE_INLINE MemberProxy& operator=(const MemberProxy& src) {
|
||||||
this->set(src);
|
this->set(src);
|
||||||
@ -42,23 +42,23 @@ class MemberProxy
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
FORCE_INLINE MemoryPool* getPool() const {
|
FORCE_INLINE MemoryPool* getPool() const {
|
||||||
return VariantAttorney::getPool(_upstream);
|
return VariantAttorney::getPool(upstream_);
|
||||||
}
|
}
|
||||||
|
|
||||||
FORCE_INLINE VariantData* getData() const {
|
FORCE_INLINE VariantData* getData() const {
|
||||||
return variantGetMember(VariantAttorney::getData(_upstream),
|
return variantGetMember(VariantAttorney::getData(upstream_),
|
||||||
adaptString(_key));
|
adaptString(key_));
|
||||||
}
|
}
|
||||||
|
|
||||||
FORCE_INLINE VariantData* getOrCreateData() const {
|
FORCE_INLINE VariantData* getOrCreateData() const {
|
||||||
return variantGetOrAddMember(VariantAttorney::getOrCreateData(_upstream),
|
return variantGetOrAddMember(VariantAttorney::getOrCreateData(upstream_),
|
||||||
adaptString(_key),
|
adaptString(key_),
|
||||||
VariantAttorney::getPool(_upstream));
|
VariantAttorney::getPool(upstream_));
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
TUpstream _upstream;
|
TUpstream upstream_;
|
||||||
TStringRef _key;
|
TStringRef key_;
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -54,14 +54,14 @@ inline T pgm_read(const T* p) {
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
class pgm_ptr {
|
class pgm_ptr {
|
||||||
public:
|
public:
|
||||||
explicit pgm_ptr(const T* ptr) : _ptr(ptr) {}
|
explicit pgm_ptr(const T* ptr) : ptr_(ptr) {}
|
||||||
|
|
||||||
T operator[](intptr_t index) const {
|
T operator[](intptr_t index) const {
|
||||||
return pgm_read(_ptr + index);
|
return pgm_read(ptr_ + index);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const T* _ptr;
|
const T* ptr_;
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -27,10 +27,10 @@ struct is_convertible {
|
|||||||
static int probe(To);
|
static int probe(To);
|
||||||
static char probe(...);
|
static char probe(...);
|
||||||
|
|
||||||
static From& _from;
|
static From& from_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static const bool value = sizeof(probe(_from)) == sizeof(int);
|
static const bool value = sizeof(probe(from_)) == sizeof(int);
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -11,23 +11,23 @@ ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
|||||||
template <typename TWriter>
|
template <typename TWriter>
|
||||||
class CountingDecorator {
|
class CountingDecorator {
|
||||||
public:
|
public:
|
||||||
explicit CountingDecorator(TWriter& writer) : _writer(writer), _count(0) {}
|
explicit CountingDecorator(TWriter& writer) : writer_(writer), count_(0) {}
|
||||||
|
|
||||||
void write(uint8_t c) {
|
void write(uint8_t c) {
|
||||||
_count += _writer.write(c);
|
count_ += writer_.write(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
void write(const uint8_t* s, size_t n) {
|
void write(const uint8_t* s, size_t n) {
|
||||||
_count += _writer.write(s, n);
|
count_ += writer_.write(s, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t count() const {
|
size_t count() const {
|
||||||
return _count;
|
return count_;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
TWriter _writer;
|
TWriter writer_;
|
||||||
size_t _count;
|
size_t count_;
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -12,18 +12,18 @@ ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
|||||||
template <typename TDestination, typename Enable = void>
|
template <typename TDestination, typename Enable = void>
|
||||||
class Writer {
|
class Writer {
|
||||||
public:
|
public:
|
||||||
explicit Writer(TDestination& dest) : _dest(&dest) {}
|
explicit Writer(TDestination& dest) : dest_(&dest) {}
|
||||||
|
|
||||||
size_t write(uint8_t c) {
|
size_t write(uint8_t c) {
|
||||||
return _dest->write(c);
|
return dest_->write(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t write(const uint8_t* s, size_t n) {
|
size_t write(const uint8_t* s, size_t n) {
|
||||||
return _dest->write(s, n);
|
return dest_->write(s, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
TDestination* _dest;
|
TDestination* dest_;
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -13,8 +13,8 @@ class Writer<::String, void> {
|
|||||||
static const size_t bufferCapacity = ARDUINOJSON_STRING_BUFFER_SIZE;
|
static const size_t bufferCapacity = ARDUINOJSON_STRING_BUFFER_SIZE;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit Writer(::String& str) : _destination(&str) {
|
explicit Writer(::String& str) : destination_(&str) {
|
||||||
_size = 0;
|
size_ = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
~Writer() {
|
~Writer() {
|
||||||
@ -22,10 +22,10 @@ class Writer<::String, void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
size_t write(uint8_t c) {
|
size_t write(uint8_t c) {
|
||||||
if (_size + 1 >= bufferCapacity)
|
if (size_ + 1 >= bufferCapacity)
|
||||||
if (flush() != 0)
|
if (flush() != 0)
|
||||||
return 0;
|
return 0;
|
||||||
_buffer[_size++] = static_cast<char>(c);
|
buffer_[size_++] = static_cast<char>(c);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,17 +37,17 @@ class Writer<::String, void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
size_t flush() {
|
size_t flush() {
|
||||||
ARDUINOJSON_ASSERT(_size < bufferCapacity);
|
ARDUINOJSON_ASSERT(size_ < bufferCapacity);
|
||||||
_buffer[_size] = 0;
|
buffer_[size_] = 0;
|
||||||
if (_destination->concat(_buffer))
|
if (destination_->concat(buffer_))
|
||||||
_size = 0;
|
size_ = 0;
|
||||||
return _size;
|
return size_;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
::String* _destination;
|
::String* destination_;
|
||||||
char _buffer[bufferCapacity];
|
char buffer_[bufferCapacity];
|
||||||
size_t _size;
|
size_t size_;
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -13,18 +13,18 @@ class Writer<
|
|||||||
TDestination,
|
TDestination,
|
||||||
typename enable_if<is_base_of<::Print, TDestination>::value>::type> {
|
typename enable_if<is_base_of<::Print, TDestination>::value>::type> {
|
||||||
public:
|
public:
|
||||||
explicit Writer(::Print& print) : _print(&print) {}
|
explicit Writer(::Print& print) : print_(&print) {}
|
||||||
|
|
||||||
size_t write(uint8_t c) {
|
size_t write(uint8_t c) {
|
||||||
return _print->write(c);
|
return print_->write(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t write(const uint8_t* s, size_t n) {
|
size_t write(const uint8_t* s, size_t n) {
|
||||||
return _print->write(s, n);
|
return print_->write(s, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
::Print* _print;
|
::Print* print_;
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -13,21 +13,21 @@ class Writer<
|
|||||||
TDestination,
|
TDestination,
|
||||||
typename enable_if<is_base_of<std::ostream, TDestination>::value>::type> {
|
typename enable_if<is_base_of<std::ostream, TDestination>::value>::type> {
|
||||||
public:
|
public:
|
||||||
explicit Writer(std::ostream& os) : _os(&os) {}
|
explicit Writer(std::ostream& os) : os_(&os) {}
|
||||||
|
|
||||||
size_t write(uint8_t c) {
|
size_t write(uint8_t c) {
|
||||||
_os->put(static_cast<char>(c));
|
os_->put(static_cast<char>(c));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t write(const uint8_t* s, size_t n) {
|
size_t write(const uint8_t* s, size_t n) {
|
||||||
_os->write(reinterpret_cast<const char*>(s),
|
os_->write(reinterpret_cast<const char*>(s),
|
||||||
static_cast<std::streamsize>(n));
|
static_cast<std::streamsize>(n));
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::ostream* _os;
|
std::ostream* os_;
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -24,20 +24,20 @@ template <typename TDestination>
|
|||||||
class Writer<TDestination,
|
class Writer<TDestination,
|
||||||
typename enable_if<is_std_string<TDestination>::value>::type> {
|
typename enable_if<is_std_string<TDestination>::value>::type> {
|
||||||
public:
|
public:
|
||||||
Writer(TDestination& str) : _str(&str) {}
|
Writer(TDestination& str) : str_(&str) {}
|
||||||
|
|
||||||
size_t write(uint8_t c) {
|
size_t write(uint8_t c) {
|
||||||
_str->push_back(static_cast<char>(c));
|
str_->push_back(static_cast<char>(c));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t write(const uint8_t* s, size_t n) {
|
size_t write(const uint8_t* s, size_t n) {
|
||||||
_str->append(reinterpret_cast<const char*>(s), n);
|
str_->append(reinterpret_cast<const char*>(s), n);
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
TDestination* _str;
|
TDestination* str_;
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -12,27 +12,27 @@ class StringCopier {
|
|||||||
public:
|
public:
|
||||||
static const size_t initialCapacity = 31;
|
static const size_t initialCapacity = 31;
|
||||||
|
|
||||||
StringCopier(MemoryPool* pool) : _pool(pool) {}
|
StringCopier(MemoryPool* pool) : pool_(pool) {}
|
||||||
|
|
||||||
~StringCopier() {
|
~StringCopier() {
|
||||||
if (_node)
|
if (node_)
|
||||||
_pool->deallocString(_node);
|
pool_->deallocString(node_);
|
||||||
}
|
}
|
||||||
|
|
||||||
void startString() {
|
void startString() {
|
||||||
_size = 0;
|
size_ = 0;
|
||||||
if (!_node)
|
if (!node_)
|
||||||
_node = _pool->allocString(initialCapacity);
|
node_ = pool_->allocString(initialCapacity);
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonString save() {
|
JsonString save() {
|
||||||
ARDUINOJSON_ASSERT(_node != nullptr);
|
ARDUINOJSON_ASSERT(node_ != nullptr);
|
||||||
_node->data[_size] = 0;
|
node_->data[size_] = 0;
|
||||||
StringNode* node = _pool->findString(adaptString(_node->data, _size));
|
StringNode* node = pool_->findString(adaptString(node_->data, size_));
|
||||||
if (!node) {
|
if (!node) {
|
||||||
node = _pool->reallocString(_node, _size);
|
node = pool_->reallocString(node_, size_);
|
||||||
_pool->addStringToList(node);
|
pool_->addStringToList(node);
|
||||||
_node = nullptr; // next time we need a new string
|
node_ = nullptr; // next time we need a new string
|
||||||
} else {
|
} else {
|
||||||
node->references++;
|
node->references++;
|
||||||
}
|
}
|
||||||
@ -50,30 +50,30 @@ class StringCopier {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void append(char c) {
|
void append(char c) {
|
||||||
if (_node && _size == _node->length)
|
if (node_ && size_ == node_->length)
|
||||||
_node = _pool->reallocString(_node, _size * 2U + 1);
|
node_ = pool_->reallocString(node_, size_ * 2U + 1);
|
||||||
if (_node)
|
if (node_)
|
||||||
_node->data[_size++] = c;
|
node_->data[size_++] = c;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isValid() const {
|
bool isValid() const {
|
||||||
return _node != nullptr;
|
return node_ != nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t size() const {
|
size_t size() const {
|
||||||
return _size;
|
return size_;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonString str() const {
|
JsonString str() const {
|
||||||
ARDUINOJSON_ASSERT(_node != nullptr);
|
ARDUINOJSON_ASSERT(node_ != nullptr);
|
||||||
_node->data[_size] = 0;
|
node_->data[size_] = 0;
|
||||||
return JsonString(_node->data, _size, JsonString::Copied);
|
return JsonString(node_->data, size_, JsonString::Copied);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
MemoryPool* _pool;
|
MemoryPool* pool_;
|
||||||
StringNode* _node = nullptr;
|
StringNode* node_ = nullptr;
|
||||||
size_t _size = 0;
|
size_t size_ = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -11,20 +11,20 @@ ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
|||||||
|
|
||||||
class StringMover {
|
class StringMover {
|
||||||
public:
|
public:
|
||||||
StringMover(char* ptr) : _writePtr(ptr) {}
|
StringMover(char* ptr) : writePtr_(ptr) {}
|
||||||
|
|
||||||
void startString() {
|
void startString() {
|
||||||
_startPtr = _writePtr;
|
startPtr_ = writePtr_;
|
||||||
}
|
}
|
||||||
|
|
||||||
FORCE_INLINE JsonString save() {
|
FORCE_INLINE JsonString save() {
|
||||||
JsonString s = str();
|
JsonString s = str();
|
||||||
_writePtr++;
|
writePtr_++;
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
void append(char c) {
|
void append(char c) {
|
||||||
*_writePtr++ = c;
|
*writePtr_++ = c;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isValid() const {
|
bool isValid() const {
|
||||||
@ -32,17 +32,17 @@ class StringMover {
|
|||||||
}
|
}
|
||||||
|
|
||||||
JsonString str() const {
|
JsonString str() const {
|
||||||
_writePtr[0] = 0; // terminator
|
writePtr_[0] = 0; // terminator
|
||||||
return JsonString(_startPtr, size(), JsonString::Linked);
|
return JsonString(startPtr_, size(), JsonString::Linked);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t size() const {
|
size_t size() const {
|
||||||
return size_t(_writePtr - _startPtr);
|
return size_t(writePtr_ - startPtr_);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
char* _writePtr;
|
char* writePtr_;
|
||||||
char* _startPtr;
|
char* startPtr_;
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -16,20 +16,20 @@ class FlashString {
|
|||||||
static const size_t typeSortKey = 1;
|
static const size_t typeSortKey = 1;
|
||||||
|
|
||||||
FlashString(const __FlashStringHelper* str, size_t sz)
|
FlashString(const __FlashStringHelper* str, size_t sz)
|
||||||
: _str(reinterpret_cast<const char*>(str)), _size(sz) {}
|
: str_(reinterpret_cast<const char*>(str)), size_(sz) {}
|
||||||
|
|
||||||
bool isNull() const {
|
bool isNull() const {
|
||||||
return !_str;
|
return !str_;
|
||||||
}
|
}
|
||||||
|
|
||||||
char operator[](size_t i) const {
|
char operator[](size_t i) const {
|
||||||
ARDUINOJSON_ASSERT(_str != 0);
|
ARDUINOJSON_ASSERT(str_ != 0);
|
||||||
ARDUINOJSON_ASSERT(i <= _size);
|
ARDUINOJSON_ASSERT(i <= size_);
|
||||||
return static_cast<char>(pgm_read_byte(_str + i));
|
return static_cast<char>(pgm_read_byte(str_ + i));
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t size() const {
|
size_t size() const {
|
||||||
return _size;
|
return size_;
|
||||||
}
|
}
|
||||||
|
|
||||||
friend bool stringEquals(FlashString a, SizedRamString b) {
|
friend bool stringEquals(FlashString a, SizedRamString b) {
|
||||||
@ -38,7 +38,7 @@ class FlashString {
|
|||||||
ARDUINOJSON_ASSERT(!b.isNull());
|
ARDUINOJSON_ASSERT(!b.isNull());
|
||||||
if (a.size() != b.size())
|
if (a.size() != b.size())
|
||||||
return false;
|
return false;
|
||||||
return ::memcmp_P(b.data(), a._str, a._size) == 0;
|
return ::memcmp_P(b.data(), a.str_, a.size_) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
friend int stringCompare(FlashString a, SizedRamString b) {
|
friend int stringCompare(FlashString a, SizedRamString b) {
|
||||||
@ -46,7 +46,7 @@ class FlashString {
|
|||||||
ARDUINOJSON_ASSERT(!a.isNull());
|
ARDUINOJSON_ASSERT(!a.isNull());
|
||||||
ARDUINOJSON_ASSERT(!b.isNull());
|
ARDUINOJSON_ASSERT(!b.isNull());
|
||||||
size_t minsize = a.size() < b.size() ? a.size() : b.size();
|
size_t minsize = a.size() < b.size() ? a.size() : b.size();
|
||||||
int res = ::memcmp_P(b.data(), a._str, minsize);
|
int res = ::memcmp_P(b.data(), a.str_, minsize);
|
||||||
if (res)
|
if (res)
|
||||||
return -res;
|
return -res;
|
||||||
if (a.size() < b.size())
|
if (a.size() < b.size())
|
||||||
@ -58,7 +58,7 @@ class FlashString {
|
|||||||
|
|
||||||
friend void stringGetChars(FlashString s, char* p, size_t n) {
|
friend void stringGetChars(FlashString s, char* p, size_t n) {
|
||||||
ARDUINOJSON_ASSERT(s.size() <= n);
|
ARDUINOJSON_ASSERT(s.size() <= n);
|
||||||
::memcpy_P(p, s._str, n);
|
::memcpy_P(p, s.str_, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
StringStoragePolicy::Copy storagePolicy() const {
|
StringStoragePolicy::Copy storagePolicy() const {
|
||||||
@ -66,8 +66,8 @@ class FlashString {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const char* _str;
|
const char* str_;
|
||||||
size_t _size;
|
size_t size_;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
|
@ -13,15 +13,15 @@ ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
|||||||
class JsonStringAdapter : public SizedRamString {
|
class JsonStringAdapter : public SizedRamString {
|
||||||
public:
|
public:
|
||||||
JsonStringAdapter(const JsonString& s)
|
JsonStringAdapter(const JsonString& s)
|
||||||
: SizedRamString(s.c_str(), s.size()), _linked(s.isLinked()) {}
|
: SizedRamString(s.c_str(), s.size()), linked_(s.isLinked()) {}
|
||||||
|
|
||||||
StringStoragePolicy::LinkOrCopy storagePolicy() const {
|
StringStoragePolicy::LinkOrCopy storagePolicy() const {
|
||||||
StringStoragePolicy::LinkOrCopy policy = {_linked};
|
StringStoragePolicy::LinkOrCopy policy = {linked_};
|
||||||
return policy;
|
return policy;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool _linked;
|
bool linked_;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
|
@ -21,31 +21,31 @@ class ZeroTerminatedRamString {
|
|||||||
public:
|
public:
|
||||||
static const size_t typeSortKey = 3;
|
static const size_t typeSortKey = 3;
|
||||||
|
|
||||||
ZeroTerminatedRamString(const char* str) : _str(str) {}
|
ZeroTerminatedRamString(const char* str) : str_(str) {}
|
||||||
|
|
||||||
bool isNull() const {
|
bool isNull() const {
|
||||||
return !_str;
|
return !str_;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t size() const {
|
size_t size() const {
|
||||||
return _str ? ::strlen(_str) : 0;
|
return str_ ? ::strlen(str_) : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
char operator[](size_t i) const {
|
char operator[](size_t i) const {
|
||||||
ARDUINOJSON_ASSERT(_str != 0);
|
ARDUINOJSON_ASSERT(str_ != 0);
|
||||||
ARDUINOJSON_ASSERT(i <= size());
|
ARDUINOJSON_ASSERT(i <= size());
|
||||||
return _str[i];
|
return str_[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* data() const {
|
const char* data() const {
|
||||||
return _str;
|
return str_;
|
||||||
}
|
}
|
||||||
|
|
||||||
friend int stringCompare(ZeroTerminatedRamString a,
|
friend int stringCompare(ZeroTerminatedRamString a,
|
||||||
ZeroTerminatedRamString b) {
|
ZeroTerminatedRamString b) {
|
||||||
ARDUINOJSON_ASSERT(!a.isNull());
|
ARDUINOJSON_ASSERT(!a.isNull());
|
||||||
ARDUINOJSON_ASSERT(!b.isNull());
|
ARDUINOJSON_ASSERT(!b.isNull());
|
||||||
return ::strcmp(a._str, b._str);
|
return ::strcmp(a.str_, b.str_);
|
||||||
}
|
}
|
||||||
|
|
||||||
friend bool stringEquals(ZeroTerminatedRamString a,
|
friend bool stringEquals(ZeroTerminatedRamString a,
|
||||||
@ -58,7 +58,7 @@ class ZeroTerminatedRamString {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
const char* _str;
|
const char* str_;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename TChar>
|
template <typename TChar>
|
||||||
@ -101,24 +101,24 @@ class SizedRamString {
|
|||||||
public:
|
public:
|
||||||
static const size_t typeSortKey = 2;
|
static const size_t typeSortKey = 2;
|
||||||
|
|
||||||
SizedRamString(const char* str, size_t sz) : _str(str), _size(sz) {}
|
SizedRamString(const char* str, size_t sz) : str_(str), size_(sz) {}
|
||||||
|
|
||||||
bool isNull() const {
|
bool isNull() const {
|
||||||
return !_str;
|
return !str_;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t size() const {
|
size_t size() const {
|
||||||
return _size;
|
return size_;
|
||||||
}
|
}
|
||||||
|
|
||||||
char operator[](size_t i) const {
|
char operator[](size_t i) const {
|
||||||
ARDUINOJSON_ASSERT(_str != 0);
|
ARDUINOJSON_ASSERT(str_ != 0);
|
||||||
ARDUINOJSON_ASSERT(i <= size());
|
ARDUINOJSON_ASSERT(i <= size());
|
||||||
return _str[i];
|
return str_[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* data() const {
|
const char* data() const {
|
||||||
return _str;
|
return str_;
|
||||||
}
|
}
|
||||||
|
|
||||||
StringStoragePolicy::Copy storagePolicy() const {
|
StringStoragePolicy::Copy storagePolicy() const {
|
||||||
@ -126,8 +126,8 @@ class SizedRamString {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
const char* _str;
|
const char* str_;
|
||||||
size_t _size;
|
size_t size_;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename TChar>
|
template <typename TChar>
|
||||||
|
@ -16,51 +16,51 @@ class JsonString {
|
|||||||
public:
|
public:
|
||||||
enum Ownership { Copied, Linked };
|
enum Ownership { Copied, Linked };
|
||||||
|
|
||||||
JsonString() : _data(0), _size(0), _ownership(Linked) {}
|
JsonString() : data_(0), size_(0), ownership_(Linked) {}
|
||||||
|
|
||||||
JsonString(const char* data, Ownership ownership = Linked)
|
JsonString(const char* data, Ownership ownership = Linked)
|
||||||
: _data(data), _size(data ? ::strlen(data) : 0), _ownership(ownership) {}
|
: data_(data), size_(data ? ::strlen(data) : 0), ownership_(ownership) {}
|
||||||
|
|
||||||
JsonString(const char* data, size_t sz, Ownership ownership = Linked)
|
JsonString(const char* data, size_t sz, Ownership ownership = Linked)
|
||||||
: _data(data), _size(sz), _ownership(ownership) {}
|
: data_(data), size_(sz), ownership_(ownership) {}
|
||||||
|
|
||||||
// Returns a pointer to the characters.
|
// Returns a pointer to the characters.
|
||||||
const char* c_str() const {
|
const char* c_str() const {
|
||||||
return _data;
|
return data_;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if the string is null.
|
// Returns true if the string is null.
|
||||||
bool isNull() const {
|
bool isNull() const {
|
||||||
return !_data;
|
return !data_;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if the string is stored by address.
|
// Returns true if the string is stored by address.
|
||||||
// Returns false if the string is stored by copy.
|
// Returns false if the string is stored by copy.
|
||||||
bool isLinked() const {
|
bool isLinked() const {
|
||||||
return _ownership == Linked;
|
return ownership_ == Linked;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns length of the string.
|
// Returns length of the string.
|
||||||
size_t size() const {
|
size_t size() const {
|
||||||
return _size;
|
return size_;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if the string is non-null
|
// Returns true if the string is non-null
|
||||||
explicit operator bool() const {
|
explicit operator bool() const {
|
||||||
return _data != 0;
|
return data_ != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if strings are equal.
|
// Returns true if strings are equal.
|
||||||
friend bool operator==(JsonString lhs, JsonString rhs) {
|
friend bool operator==(JsonString lhs, JsonString rhs) {
|
||||||
if (lhs._size != rhs._size)
|
if (lhs.size_ != rhs.size_)
|
||||||
return false;
|
return false;
|
||||||
if (lhs._data == rhs._data)
|
if (lhs.data_ == rhs.data_)
|
||||||
return true;
|
return true;
|
||||||
if (!lhs._data)
|
if (!lhs.data_)
|
||||||
return false;
|
return false;
|
||||||
if (!rhs._data)
|
if (!rhs.data_)
|
||||||
return false;
|
return false;
|
||||||
return memcmp(lhs._data, rhs._data, lhs._size) == 0;
|
return memcmp(lhs.data_, rhs.data_, lhs.size_) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if strings differs.
|
// Returns true if strings differs.
|
||||||
@ -76,9 +76,9 @@ class JsonString {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const char* _data;
|
const char* data_;
|
||||||
size_t _size;
|
size_t size_;
|
||||||
Ownership _ownership;
|
Ownership ownership_;
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
||||||
|
@ -195,35 +195,35 @@ struct Converter<decltype(nullptr)> : private detail::VariantAttorney {
|
|||||||
namespace detail {
|
namespace detail {
|
||||||
class MemoryPoolPrint : public Print {
|
class MemoryPoolPrint : public Print {
|
||||||
public:
|
public:
|
||||||
MemoryPoolPrint(MemoryPool* pool) : _copier(pool) {
|
MemoryPoolPrint(MemoryPool* pool) : copier_(pool) {
|
||||||
_copier.startString();
|
copier_.startString();
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonString str() {
|
JsonString str() {
|
||||||
ARDUINOJSON_ASSERT(!overflowed());
|
ARDUINOJSON_ASSERT(!overflowed());
|
||||||
return _copier.save();
|
return copier_.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t write(uint8_t c) {
|
size_t write(uint8_t c) {
|
||||||
_copier.append(char(c));
|
copier_.append(char(c));
|
||||||
return _copier.isValid() ? 1 : 0;
|
return copier_.isValid() ? 1 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t write(const uint8_t* buffer, size_t size) {
|
size_t write(const uint8_t* buffer, size_t size) {
|
||||||
for (size_t i = 0; i < size; i++) {
|
for (size_t i = 0; i < size; i++) {
|
||||||
_copier.append(char(buffer[i]));
|
copier_.append(char(buffer[i]));
|
||||||
if (!_copier.isValid())
|
if (!copier_.isValid())
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool overflowed() const {
|
bool overflowed() const {
|
||||||
return !_copier.isValid();
|
return !copier_.isValid();
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
StringCopier _copier;
|
StringCopier copier_;
|
||||||
};
|
};
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
|
||||||
|
@ -16,27 +16,27 @@ class JsonVariant : public detail::VariantRefBase<JsonVariant>,
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
// Creates an unbound reference.
|
// Creates an unbound reference.
|
||||||
JsonVariant() : _data(0), _pool(0) {}
|
JsonVariant() : data_(0), pool_(0) {}
|
||||||
|
|
||||||
// INTERNAL USE ONLY
|
// INTERNAL USE ONLY
|
||||||
JsonVariant(detail::MemoryPool* pool, detail::VariantData* data)
|
JsonVariant(detail::MemoryPool* pool, detail::VariantData* data)
|
||||||
: _data(data), _pool(pool) {}
|
: data_(data), pool_(pool) {}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
FORCE_INLINE detail::MemoryPool* getPool() const {
|
FORCE_INLINE detail::MemoryPool* getPool() const {
|
||||||
return _pool;
|
return pool_;
|
||||||
}
|
}
|
||||||
|
|
||||||
FORCE_INLINE detail::VariantData* getData() const {
|
FORCE_INLINE detail::VariantData* getData() const {
|
||||||
return _data;
|
return data_;
|
||||||
}
|
}
|
||||||
|
|
||||||
FORCE_INLINE detail::VariantData* getOrCreateData() const {
|
FORCE_INLINE detail::VariantData* getOrCreateData() const {
|
||||||
return _data;
|
return data_;
|
||||||
}
|
}
|
||||||
|
|
||||||
detail::VariantData* _data;
|
detail::VariantData* data_;
|
||||||
detail::MemoryPool* _pool;
|
detail::MemoryPool* pool_;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
|
@ -30,39 +30,39 @@ class JsonVariantConst : public detail::VariantTag,
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
// Creates an unbound reference.
|
// Creates an unbound reference.
|
||||||
JsonVariantConst() : _data(0) {}
|
JsonVariantConst() : data_(0) {}
|
||||||
|
|
||||||
// INTERNAL USE ONLY
|
// INTERNAL USE ONLY
|
||||||
explicit JsonVariantConst(const detail::VariantData* data) : _data(data) {}
|
explicit JsonVariantConst(const detail::VariantData* data) : data_(data) {}
|
||||||
|
|
||||||
// Returns true if the value is null or the reference is unbound.
|
// Returns true if the value is null or the reference is unbound.
|
||||||
// https://arduinojson.org/v6/api/jsonvariantconst/isnull/
|
// https://arduinojson.org/v6/api/jsonvariantconst/isnull/
|
||||||
FORCE_INLINE bool isNull() const {
|
FORCE_INLINE bool isNull() const {
|
||||||
using namespace detail;
|
using namespace detail;
|
||||||
return variantIsNull(_data);
|
return variantIsNull(data_);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if the reference is unbound.
|
// Returns true if the reference is unbound.
|
||||||
FORCE_INLINE bool isUnbound() const {
|
FORCE_INLINE bool isUnbound() const {
|
||||||
return !_data;
|
return !data_;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the number of bytes occupied by the value.
|
// Returns the number of bytes occupied by the value.
|
||||||
// https://arduinojson.org/v6/api/jsonvariantconst/memoryusage/
|
// https://arduinojson.org/v6/api/jsonvariantconst/memoryusage/
|
||||||
FORCE_INLINE size_t memoryUsage() const {
|
FORCE_INLINE size_t memoryUsage() const {
|
||||||
return _data ? _data->memoryUsage() : 0;
|
return data_ ? data_->memoryUsage() : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the depth (nesting level) of the value.
|
// Returns the depth (nesting level) of the value.
|
||||||
// https://arduinojson.org/v6/api/jsonvariantconst/nesting/
|
// https://arduinojson.org/v6/api/jsonvariantconst/nesting/
|
||||||
FORCE_INLINE size_t nesting() const {
|
FORCE_INLINE size_t nesting() const {
|
||||||
return variantNesting(_data);
|
return variantNesting(data_);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the size of the array or object.
|
// Returns the size of the array or object.
|
||||||
// https://arduinojson.org/v6/api/jsonvariantconst/size/
|
// https://arduinojson.org/v6/api/jsonvariantconst/size/
|
||||||
size_t size() const {
|
size_t size() const {
|
||||||
return variantSize(_data);
|
return variantSize(data_);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Casts the value to the specified type.
|
// Casts the value to the specified type.
|
||||||
@ -93,7 +93,7 @@ class JsonVariantConst : public detail::VariantTag,
|
|||||||
// Gets array's element at specified index.
|
// Gets array's element at specified index.
|
||||||
// https://arduinojson.org/v6/api/jsonvariantconst/subscript/
|
// https://arduinojson.org/v6/api/jsonvariantconst/subscript/
|
||||||
FORCE_INLINE JsonVariantConst operator[](size_t index) const {
|
FORCE_INLINE JsonVariantConst operator[](size_t index) const {
|
||||||
return JsonVariantConst(variantGetElement(_data, index));
|
return JsonVariantConst(variantGetElement(data_, index));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gets object's member with specified key.
|
// Gets object's member with specified key.
|
||||||
@ -102,7 +102,7 @@ class JsonVariantConst : public detail::VariantTag,
|
|||||||
FORCE_INLINE typename detail::enable_if<detail::IsString<TString>::value,
|
FORCE_INLINE typename detail::enable_if<detail::IsString<TString>::value,
|
||||||
JsonVariantConst>::type
|
JsonVariantConst>::type
|
||||||
operator[](const TString& key) const {
|
operator[](const TString& key) const {
|
||||||
return JsonVariantConst(variantGetMember(_data, detail::adaptString(key)));
|
return JsonVariantConst(variantGetMember(data_, detail::adaptString(key)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gets object's member with specified key.
|
// Gets object's member with specified key.
|
||||||
@ -111,7 +111,7 @@ class JsonVariantConst : public detail::VariantTag,
|
|||||||
FORCE_INLINE typename detail::enable_if<detail::IsString<TChar*>::value,
|
FORCE_INLINE typename detail::enable_if<detail::IsString<TChar*>::value,
|
||||||
JsonVariantConst>::type
|
JsonVariantConst>::type
|
||||||
operator[](TChar* key) const {
|
operator[](TChar* key) const {
|
||||||
return JsonVariantConst(variantGetMember(_data, detail::adaptString(key)));
|
return JsonVariantConst(variantGetMember(data_, detail::adaptString(key)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if tge object contains the specified key.
|
// Returns true if tge object contains the specified key.
|
||||||
@ -134,11 +134,11 @@ class JsonVariantConst : public detail::VariantTag,
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
const detail::VariantData* getData() const {
|
const detail::VariantData* getData() const {
|
||||||
return _data;
|
return data_;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const detail::VariantData* _data;
|
const detail::VariantData* data_;
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
||||||
|
@ -81,12 +81,12 @@ struct Comparer<decltype(nullptr), void> : NullComparer {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct ArrayComparer : ComparerBase {
|
struct ArrayComparer : ComparerBase {
|
||||||
const CollectionData* _rhs;
|
const CollectionData* rhs_;
|
||||||
|
|
||||||
explicit ArrayComparer(const CollectionData& rhs) : _rhs(&rhs) {}
|
explicit ArrayComparer(const CollectionData& rhs) : rhs_(&rhs) {}
|
||||||
|
|
||||||
CompareResult visitArray(const CollectionData& lhs) {
|
CompareResult visitArray(const CollectionData& lhs) {
|
||||||
if (JsonArrayConst(&lhs) == JsonArrayConst(_rhs))
|
if (JsonArrayConst(&lhs) == JsonArrayConst(rhs_))
|
||||||
return COMPARE_RESULT_EQUAL;
|
return COMPARE_RESULT_EQUAL;
|
||||||
else
|
else
|
||||||
return COMPARE_RESULT_DIFFER;
|
return COMPARE_RESULT_DIFFER;
|
||||||
@ -94,12 +94,12 @@ struct ArrayComparer : ComparerBase {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct ObjectComparer : ComparerBase {
|
struct ObjectComparer : ComparerBase {
|
||||||
const CollectionData* _rhs;
|
const CollectionData* rhs_;
|
||||||
|
|
||||||
explicit ObjectComparer(const CollectionData& rhs) : _rhs(&rhs) {}
|
explicit ObjectComparer(const CollectionData& rhs) : rhs_(&rhs) {}
|
||||||
|
|
||||||
CompareResult visitObject(const CollectionData& lhs) {
|
CompareResult visitObject(const CollectionData& lhs) {
|
||||||
if (JsonObjectConst(&lhs) == JsonObjectConst(_rhs))
|
if (JsonObjectConst(&lhs) == JsonObjectConst(rhs_))
|
||||||
return COMPARE_RESULT_EQUAL;
|
return COMPARE_RESULT_EQUAL;
|
||||||
else
|
else
|
||||||
return COMPARE_RESULT_DIFFER;
|
return COMPARE_RESULT_DIFFER;
|
||||||
@ -107,15 +107,15 @@ struct ObjectComparer : ComparerBase {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct RawComparer : ComparerBase {
|
struct RawComparer : ComparerBase {
|
||||||
const char* _rhsData;
|
const char* rhsData_;
|
||||||
size_t _rhsSize;
|
size_t rhsSize_;
|
||||||
|
|
||||||
explicit RawComparer(const char* rhsData, size_t rhsSize)
|
explicit RawComparer(const char* rhsData, size_t rhsSize)
|
||||||
: _rhsData(rhsData), _rhsSize(rhsSize) {}
|
: rhsData_(rhsData), rhsSize_(rhsSize) {}
|
||||||
|
|
||||||
CompareResult visitRawJson(const char* lhsData, size_t lhsSize) {
|
CompareResult visitRawJson(const char* lhsData, size_t lhsSize) {
|
||||||
size_t size = _rhsSize < lhsSize ? _rhsSize : lhsSize;
|
size_t size = rhsSize_ < lhsSize ? rhsSize_ : lhsSize;
|
||||||
int n = memcmp(lhsData, _rhsData, size);
|
int n = memcmp(lhsData, rhsData_, size);
|
||||||
if (n < 0)
|
if (n < 0)
|
||||||
return COMPARE_RESULT_LESS;
|
return COMPARE_RESULT_LESS;
|
||||||
else if (n > 0)
|
else if (n > 0)
|
||||||
|
@ -13,51 +13,51 @@
|
|||||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||||
|
|
||||||
class VariantData {
|
class VariantData {
|
||||||
VariantContent _content; // must be first to allow cast from array to variant
|
VariantContent content_; // must be first to allow cast from array to variant
|
||||||
uint8_t _flags;
|
uint8_t flags_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
VariantData() : _flags(VALUE_IS_NULL) {}
|
VariantData() : flags_(VALUE_IS_NULL) {}
|
||||||
|
|
||||||
void reset() {
|
void reset() {
|
||||||
_flags = VALUE_IS_NULL;
|
flags_ = VALUE_IS_NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void operator=(const VariantData& src) {
|
void operator=(const VariantData& src) {
|
||||||
_content = src._content;
|
content_ = src.content_;
|
||||||
_flags = uint8_t((_flags & OWNED_KEY_BIT) | (src._flags & ~OWNED_KEY_BIT));
|
flags_ = uint8_t((flags_ & OWNED_KEY_BIT) | (src.flags_ & ~OWNED_KEY_BIT));
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TVisitor>
|
template <typename TVisitor>
|
||||||
typename TVisitor::result_type accept(TVisitor& visitor) const {
|
typename TVisitor::result_type accept(TVisitor& visitor) const {
|
||||||
switch (type()) {
|
switch (type()) {
|
||||||
case VALUE_IS_FLOAT:
|
case VALUE_IS_FLOAT:
|
||||||
return visitor.visitFloat(_content.asFloat);
|
return visitor.visitFloat(content_.asFloat);
|
||||||
|
|
||||||
case VALUE_IS_ARRAY:
|
case VALUE_IS_ARRAY:
|
||||||
return visitor.visitArray(_content.asCollection);
|
return visitor.visitArray(content_.asCollection);
|
||||||
|
|
||||||
case VALUE_IS_OBJECT:
|
case VALUE_IS_OBJECT:
|
||||||
return visitor.visitObject(_content.asCollection);
|
return visitor.visitObject(content_.asCollection);
|
||||||
|
|
||||||
case VALUE_IS_LINKED_STRING:
|
case VALUE_IS_LINKED_STRING:
|
||||||
case VALUE_IS_OWNED_STRING:
|
case VALUE_IS_OWNED_STRING:
|
||||||
return visitor.visitString(_content.asString.data,
|
return visitor.visitString(content_.asString.data,
|
||||||
_content.asString.size);
|
content_.asString.size);
|
||||||
|
|
||||||
case VALUE_IS_OWNED_RAW:
|
case VALUE_IS_OWNED_RAW:
|
||||||
case VALUE_IS_LINKED_RAW:
|
case VALUE_IS_LINKED_RAW:
|
||||||
return visitor.visitRawJson(_content.asString.data,
|
return visitor.visitRawJson(content_.asString.data,
|
||||||
_content.asString.size);
|
content_.asString.size);
|
||||||
|
|
||||||
case VALUE_IS_SIGNED_INTEGER:
|
case VALUE_IS_SIGNED_INTEGER:
|
||||||
return visitor.visitSignedInteger(_content.asSignedInteger);
|
return visitor.visitSignedInteger(content_.asSignedInteger);
|
||||||
|
|
||||||
case VALUE_IS_UNSIGNED_INTEGER:
|
case VALUE_IS_UNSIGNED_INTEGER:
|
||||||
return visitor.visitUnsignedInteger(_content.asUnsignedInteger);
|
return visitor.visitUnsignedInteger(content_.asUnsignedInteger);
|
||||||
|
|
||||||
case VALUE_IS_BOOLEAN:
|
case VALUE_IS_BOOLEAN:
|
||||||
return visitor.visitBoolean(_content.asBoolean != 0);
|
return visitor.visitBoolean(content_.asBoolean != 0);
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return visitor.visitNull();
|
return visitor.visitNull();
|
||||||
@ -76,14 +76,14 @@ class VariantData {
|
|||||||
bool asBoolean() const;
|
bool asBoolean() const;
|
||||||
|
|
||||||
const char* getOwnedString() const {
|
const char* getOwnedString() const {
|
||||||
if (_flags & OWNED_VALUE_BIT)
|
if (flags_ & OWNED_VALUE_BIT)
|
||||||
return _content.asString.data;
|
return content_.asString.data;
|
||||||
else
|
else
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
CollectionData* asArray() {
|
CollectionData* asArray() {
|
||||||
return isArray() ? &_content.asCollection : 0;
|
return isArray() ? &content_.asCollection : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const CollectionData* asArray() const {
|
const CollectionData* asArray() const {
|
||||||
@ -91,11 +91,11 @@ class VariantData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const CollectionData* asCollection() const {
|
const CollectionData* asCollection() const {
|
||||||
return isCollection() ? &_content.asCollection : 0;
|
return isCollection() ? &content_.asCollection : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
CollectionData* asObject() {
|
CollectionData* asObject() {
|
||||||
return isObject() ? &_content.asCollection : 0;
|
return isObject() ? &content_.asCollection : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const CollectionData* asObject() const {
|
const CollectionData* asObject() const {
|
||||||
@ -103,7 +103,7 @@ class VariantData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool isArray() const {
|
bool isArray() const {
|
||||||
return (_flags & VALUE_IS_ARRAY) != 0;
|
return (flags_ & VALUE_IS_ARRAY) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isBoolean() const {
|
bool isBoolean() const {
|
||||||
@ -111,17 +111,17 @@ class VariantData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool isCollection() const {
|
bool isCollection() const {
|
||||||
return (_flags & COLLECTION_MASK) != 0;
|
return (flags_ & COLLECTION_MASK) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
bool isInteger() const {
|
bool isInteger() const {
|
||||||
switch (type()) {
|
switch (type()) {
|
||||||
case VALUE_IS_UNSIGNED_INTEGER:
|
case VALUE_IS_UNSIGNED_INTEGER:
|
||||||
return canConvertNumber<T>(_content.asUnsignedInteger);
|
return canConvertNumber<T>(content_.asUnsignedInteger);
|
||||||
|
|
||||||
case VALUE_IS_SIGNED_INTEGER:
|
case VALUE_IS_SIGNED_INTEGER:
|
||||||
return canConvertNumber<T>(_content.asSignedInteger);
|
return canConvertNumber<T>(content_.asSignedInteger);
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
@ -129,7 +129,7 @@ class VariantData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool isFloat() const {
|
bool isFloat() const {
|
||||||
return (_flags & NUMBER_BIT) != 0;
|
return (flags_ & NUMBER_BIT) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isString() const {
|
bool isString() const {
|
||||||
@ -137,7 +137,7 @@ class VariantData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool isObject() const {
|
bool isObject() const {
|
||||||
return (_flags & VALUE_IS_OBJECT) != 0;
|
return (flags_ & VALUE_IS_OBJECT) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isNull() const {
|
bool isNull() const {
|
||||||
@ -150,36 +150,36 @@ class VariantData {
|
|||||||
|
|
||||||
void setBoolean(bool value) {
|
void setBoolean(bool value) {
|
||||||
setType(VALUE_IS_BOOLEAN);
|
setType(VALUE_IS_BOOLEAN);
|
||||||
_content.asBoolean = value;
|
content_.asBoolean = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setFloat(JsonFloat value) {
|
void setFloat(JsonFloat value) {
|
||||||
setType(VALUE_IS_FLOAT);
|
setType(VALUE_IS_FLOAT);
|
||||||
_content.asFloat = value;
|
content_.asFloat = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setLinkedRaw(const char* data, size_t n) {
|
void setLinkedRaw(const char* data, size_t n) {
|
||||||
setType(VALUE_IS_LINKED_RAW);
|
setType(VALUE_IS_LINKED_RAW);
|
||||||
_content.asString.data = data;
|
content_.asString.data = data;
|
||||||
_content.asString.size = n;
|
content_.asString.size = n;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setOwnedRaw(const char* data, size_t n) {
|
void setOwnedRaw(const char* data, size_t n) {
|
||||||
setType(VALUE_IS_OWNED_RAW);
|
setType(VALUE_IS_OWNED_RAW);
|
||||||
_content.asString.data = data;
|
content_.asString.data = data;
|
||||||
_content.asString.size = n;
|
content_.asString.size = n;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
typename enable_if<is_unsigned<T>::value>::type setInteger(T value) {
|
typename enable_if<is_unsigned<T>::value>::type setInteger(T value) {
|
||||||
setType(VALUE_IS_UNSIGNED_INTEGER);
|
setType(VALUE_IS_UNSIGNED_INTEGER);
|
||||||
_content.asUnsignedInteger = static_cast<JsonUInt>(value);
|
content_.asUnsignedInteger = static_cast<JsonUInt>(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
typename enable_if<is_signed<T>::value>::type setInteger(T value) {
|
typename enable_if<is_signed<T>::value>::type setInteger(T value) {
|
||||||
setType(VALUE_IS_SIGNED_INTEGER);
|
setType(VALUE_IS_SIGNED_INTEGER);
|
||||||
_content.asSignedInteger = value;
|
content_.asSignedInteger = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setNull() {
|
void setNull() {
|
||||||
@ -192,37 +192,37 @@ class VariantData {
|
|||||||
setType(VALUE_IS_LINKED_STRING);
|
setType(VALUE_IS_LINKED_STRING);
|
||||||
else
|
else
|
||||||
setType(VALUE_IS_OWNED_STRING);
|
setType(VALUE_IS_OWNED_STRING);
|
||||||
_content.asString.data = s.c_str();
|
content_.asString.data = s.c_str();
|
||||||
_content.asString.size = s.size();
|
content_.asString.size = s.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
CollectionData& toArray() {
|
CollectionData& toArray() {
|
||||||
setType(VALUE_IS_ARRAY);
|
setType(VALUE_IS_ARRAY);
|
||||||
_content.asCollection.clear();
|
content_.asCollection.clear();
|
||||||
return _content.asCollection;
|
return content_.asCollection;
|
||||||
}
|
}
|
||||||
|
|
||||||
CollectionData& toObject() {
|
CollectionData& toObject() {
|
||||||
setType(VALUE_IS_OBJECT);
|
setType(VALUE_IS_OBJECT);
|
||||||
_content.asCollection.clear();
|
content_.asCollection.clear();
|
||||||
return _content.asCollection;
|
return content_.asCollection;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t memoryUsage() const {
|
size_t memoryUsage() const {
|
||||||
switch (type()) {
|
switch (type()) {
|
||||||
case VALUE_IS_OWNED_STRING:
|
case VALUE_IS_OWNED_STRING:
|
||||||
case VALUE_IS_OWNED_RAW:
|
case VALUE_IS_OWNED_RAW:
|
||||||
return sizeofString(_content.asString.size);
|
return sizeofString(content_.asString.size);
|
||||||
case VALUE_IS_OBJECT:
|
case VALUE_IS_OBJECT:
|
||||||
case VALUE_IS_ARRAY:
|
case VALUE_IS_ARRAY:
|
||||||
return _content.asCollection.memoryUsage();
|
return content_.asCollection.memoryUsage();
|
||||||
default:
|
default:
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t size() const {
|
size_t size() const {
|
||||||
return isCollection() ? _content.asCollection.size() : 0;
|
return isCollection() ? content_.asCollection.size() : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
VariantData* getElement(size_t index) const {
|
VariantData* getElement(size_t index) const {
|
||||||
@ -241,18 +241,18 @@ class VariantData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void movePointers(ptrdiff_t variantDistance) {
|
void movePointers(ptrdiff_t variantDistance) {
|
||||||
if (_flags & COLLECTION_MASK)
|
if (flags_ & COLLECTION_MASK)
|
||||||
_content.asCollection.movePointers(variantDistance);
|
content_.asCollection.movePointers(variantDistance);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t type() const {
|
uint8_t type() const {
|
||||||
return _flags & VALUE_MASK;
|
return flags_ & VALUE_MASK;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void setType(uint8_t t) {
|
void setType(uint8_t t) {
|
||||||
_flags &= OWNED_KEY_BIT;
|
flags_ &= OWNED_KEY_BIT;
|
||||||
_flags |= t;
|
flags_ |= t;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -19,16 +19,16 @@ template <typename T>
|
|||||||
inline T VariantData::asIntegral() const {
|
inline T VariantData::asIntegral() const {
|
||||||
switch (type()) {
|
switch (type()) {
|
||||||
case VALUE_IS_BOOLEAN:
|
case VALUE_IS_BOOLEAN:
|
||||||
return _content.asBoolean;
|
return content_.asBoolean;
|
||||||
case VALUE_IS_UNSIGNED_INTEGER:
|
case VALUE_IS_UNSIGNED_INTEGER:
|
||||||
return convertNumber<T>(_content.asUnsignedInteger);
|
return convertNumber<T>(content_.asUnsignedInteger);
|
||||||
case VALUE_IS_SIGNED_INTEGER:
|
case VALUE_IS_SIGNED_INTEGER:
|
||||||
return convertNumber<T>(_content.asSignedInteger);
|
return convertNumber<T>(content_.asSignedInteger);
|
||||||
case VALUE_IS_LINKED_STRING:
|
case VALUE_IS_LINKED_STRING:
|
||||||
case VALUE_IS_OWNED_STRING:
|
case VALUE_IS_OWNED_STRING:
|
||||||
return parseNumber<T>(_content.asString.data);
|
return parseNumber<T>(content_.asString.data);
|
||||||
case VALUE_IS_FLOAT:
|
case VALUE_IS_FLOAT:
|
||||||
return convertNumber<T>(_content.asFloat);
|
return convertNumber<T>(content_.asFloat);
|
||||||
default:
|
default:
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -37,12 +37,12 @@ inline T VariantData::asIntegral() const {
|
|||||||
inline bool VariantData::asBoolean() const {
|
inline bool VariantData::asBoolean() const {
|
||||||
switch (type()) {
|
switch (type()) {
|
||||||
case VALUE_IS_BOOLEAN:
|
case VALUE_IS_BOOLEAN:
|
||||||
return _content.asBoolean;
|
return content_.asBoolean;
|
||||||
case VALUE_IS_SIGNED_INTEGER:
|
case VALUE_IS_SIGNED_INTEGER:
|
||||||
case VALUE_IS_UNSIGNED_INTEGER:
|
case VALUE_IS_UNSIGNED_INTEGER:
|
||||||
return _content.asUnsignedInteger != 0;
|
return content_.asUnsignedInteger != 0;
|
||||||
case VALUE_IS_FLOAT:
|
case VALUE_IS_FLOAT:
|
||||||
return _content.asFloat != 0;
|
return content_.asFloat != 0;
|
||||||
case VALUE_IS_NULL:
|
case VALUE_IS_NULL:
|
||||||
return false;
|
return false;
|
||||||
default:
|
default:
|
||||||
@ -55,16 +55,16 @@ template <typename T>
|
|||||||
inline T VariantData::asFloat() const {
|
inline T VariantData::asFloat() const {
|
||||||
switch (type()) {
|
switch (type()) {
|
||||||
case VALUE_IS_BOOLEAN:
|
case VALUE_IS_BOOLEAN:
|
||||||
return static_cast<T>(_content.asBoolean);
|
return static_cast<T>(content_.asBoolean);
|
||||||
case VALUE_IS_UNSIGNED_INTEGER:
|
case VALUE_IS_UNSIGNED_INTEGER:
|
||||||
return static_cast<T>(_content.asUnsignedInteger);
|
return static_cast<T>(content_.asUnsignedInteger);
|
||||||
case VALUE_IS_SIGNED_INTEGER:
|
case VALUE_IS_SIGNED_INTEGER:
|
||||||
return static_cast<T>(_content.asSignedInteger);
|
return static_cast<T>(content_.asSignedInteger);
|
||||||
case VALUE_IS_LINKED_STRING:
|
case VALUE_IS_LINKED_STRING:
|
||||||
case VALUE_IS_OWNED_STRING:
|
case VALUE_IS_OWNED_STRING:
|
||||||
return parseNumber<T>(_content.asString.data);
|
return parseNumber<T>(content_.asString.data);
|
||||||
case VALUE_IS_FLOAT:
|
case VALUE_IS_FLOAT:
|
||||||
return static_cast<T>(_content.asFloat);
|
return static_cast<T>(content_.asFloat);
|
||||||
default:
|
default:
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -73,10 +73,10 @@ inline T VariantData::asFloat() const {
|
|||||||
inline JsonString VariantData::asString() const {
|
inline JsonString VariantData::asString() const {
|
||||||
switch (type()) {
|
switch (type()) {
|
||||||
case VALUE_IS_LINKED_STRING:
|
case VALUE_IS_LINKED_STRING:
|
||||||
return JsonString(_content.asString.data, _content.asString.size,
|
return JsonString(content_.asString.data, content_.asString.size,
|
||||||
JsonString::Linked);
|
JsonString::Linked);
|
||||||
case VALUE_IS_OWNED_STRING:
|
case VALUE_IS_OWNED_STRING:
|
||||||
return JsonString(_content.asString.data, _content.asString.size,
|
return JsonString(content_.asString.data, content_.asString.size,
|
||||||
JsonString::Copied);
|
JsonString::Copied);
|
||||||
default:
|
default:
|
||||||
return JsonString();
|
return JsonString();
|
||||||
@ -86,10 +86,10 @@ inline JsonString VariantData::asString() const {
|
|||||||
inline JsonString VariantData::asRaw() const {
|
inline JsonString VariantData::asRaw() const {
|
||||||
switch (type()) {
|
switch (type()) {
|
||||||
case VALUE_IS_LINKED_RAW:
|
case VALUE_IS_LINKED_RAW:
|
||||||
return JsonString(_content.asString.data, _content.asString.size,
|
return JsonString(content_.asString.data, content_.asString.size,
|
||||||
JsonString::Linked);
|
JsonString::Linked);
|
||||||
case VALUE_IS_OWNED_RAW:
|
case VALUE_IS_OWNED_RAW:
|
||||||
return JsonString(_content.asString.data, _content.asString.size,
|
return JsonString(content_.asString.data, content_.asString.size,
|
||||||
JsonString::Copied);
|
JsonString::Copied);
|
||||||
default:
|
default:
|
||||||
return JsonString();
|
return JsonString();
|
||||||
|
@ -17,10 +17,10 @@ class VariantSlot {
|
|||||||
// CAUTION: same layout as VariantData
|
// CAUTION: same layout as VariantData
|
||||||
// we cannot use composition because it adds padding
|
// we cannot use composition because it adds padding
|
||||||
// (+20% on ESP8266 for example)
|
// (+20% on ESP8266 for example)
|
||||||
VariantContent _content;
|
VariantContent content_;
|
||||||
uint8_t _flags;
|
uint8_t flags_;
|
||||||
VariantSlotDiff _next;
|
VariantSlotDiff next_;
|
||||||
const char* _key;
|
const char* key_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Must be a POD!
|
// Must be a POD!
|
||||||
@ -30,15 +30,15 @@ class VariantSlot {
|
|||||||
// - no inheritance
|
// - no inheritance
|
||||||
|
|
||||||
VariantData* data() {
|
VariantData* data() {
|
||||||
return reinterpret_cast<VariantData*>(&_content);
|
return reinterpret_cast<VariantData*>(&content_);
|
||||||
}
|
}
|
||||||
|
|
||||||
const VariantData* data() const {
|
const VariantData* data() const {
|
||||||
return reinterpret_cast<const VariantData*>(&_content);
|
return reinterpret_cast<const VariantData*>(&content_);
|
||||||
}
|
}
|
||||||
|
|
||||||
VariantSlot* next() {
|
VariantSlot* next() {
|
||||||
return _next ? this + _next : 0;
|
return next_ ? this + next_ : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const VariantSlot* next() const {
|
const VariantSlot* next() const {
|
||||||
@ -48,9 +48,9 @@ class VariantSlot {
|
|||||||
VariantSlot* next(size_t distance) {
|
VariantSlot* next(size_t distance) {
|
||||||
VariantSlot* slot = this;
|
VariantSlot* slot = this;
|
||||||
while (distance--) {
|
while (distance--) {
|
||||||
if (!slot->_next)
|
if (!slot->next_)
|
||||||
return 0;
|
return 0;
|
||||||
slot += slot->_next;
|
slot += slot->next_;
|
||||||
}
|
}
|
||||||
return slot;
|
return slot;
|
||||||
}
|
}
|
||||||
@ -64,7 +64,7 @@ class VariantSlot {
|
|||||||
numeric_limits<VariantSlotDiff>::lowest());
|
numeric_limits<VariantSlotDiff>::lowest());
|
||||||
ARDUINOJSON_ASSERT(!slot || slot - this <=
|
ARDUINOJSON_ASSERT(!slot || slot - this <=
|
||||||
numeric_limits<VariantSlotDiff>::highest());
|
numeric_limits<VariantSlotDiff>::highest());
|
||||||
_next = VariantSlotDiff(slot ? slot - this : 0);
|
next_ = VariantSlotDiff(slot ? slot - this : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setNextNotNull(VariantSlot* slot) {
|
void setNextNotNull(VariantSlot* slot) {
|
||||||
@ -73,35 +73,35 @@ class VariantSlot {
|
|||||||
numeric_limits<VariantSlotDiff>::lowest());
|
numeric_limits<VariantSlotDiff>::lowest());
|
||||||
ARDUINOJSON_ASSERT(slot - this <=
|
ARDUINOJSON_ASSERT(slot - this <=
|
||||||
numeric_limits<VariantSlotDiff>::highest());
|
numeric_limits<VariantSlotDiff>::highest());
|
||||||
_next = VariantSlotDiff(slot - this);
|
next_ = VariantSlotDiff(slot - this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setKey(JsonString k) {
|
void setKey(JsonString k) {
|
||||||
ARDUINOJSON_ASSERT(k);
|
ARDUINOJSON_ASSERT(k);
|
||||||
if (k.isLinked())
|
if (k.isLinked())
|
||||||
_flags &= VALUE_MASK;
|
flags_ &= VALUE_MASK;
|
||||||
else
|
else
|
||||||
_flags |= OWNED_KEY_BIT;
|
flags_ |= OWNED_KEY_BIT;
|
||||||
_key = k.c_str();
|
key_ = k.c_str();
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* key() const {
|
const char* key() const {
|
||||||
return _key;
|
return key_;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ownsKey() const {
|
bool ownsKey() const {
|
||||||
return (_flags & OWNED_KEY_BIT) != 0;
|
return (flags_ & OWNED_KEY_BIT) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void clear() {
|
void clear() {
|
||||||
_next = 0;
|
next_ = 0;
|
||||||
_flags = 0;
|
flags_ = 0;
|
||||||
_key = 0;
|
key_ = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void movePointers(ptrdiff_t variantDistance) {
|
void movePointers(ptrdiff_t variantDistance) {
|
||||||
if (_flags & COLLECTION_MASK)
|
if (flags_ & COLLECTION_MASK)
|
||||||
_content.asCollection.movePointers(variantDistance);
|
content_.asCollection.movePointers(variantDistance);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user