mirror of
https://github.com/eledio-devices/thirdparty-ArduinoJson.git
synced 2025-11-01 08:48:30 +01:00
Epic refactoring in progress...
This commit is contained in:
@@ -20,7 +20,6 @@ class IndentedPrint;
|
||||
class JsonArrayConstIterator;
|
||||
class JsonArrayImpl;
|
||||
class JsonArrayIterator;
|
||||
class JsonNode;
|
||||
class JsonObjectImpl;
|
||||
class JsonObjectIterator;
|
||||
class JsonParser;
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../JsonBuffer.hpp"
|
||||
#include "../ForwardDeclarations.hpp"
|
||||
#include "JsonArrayIterator.hpp"
|
||||
#include "JsonArrayConstIterator.hpp"
|
||||
|
||||
@@ -20,13 +20,15 @@ class JsonArrayImpl {
|
||||
|
||||
JsonArrayImpl(JsonBuffer *buffer) : _buffer(buffer) {}
|
||||
|
||||
value_type *operator[](int index) const;
|
||||
value_type operator[](int index) const;
|
||||
|
||||
value_type *add();
|
||||
value_type add();
|
||||
|
||||
JsonArrayImpl *createNestedArray();
|
||||
JsonObjectImpl *createNestedObject();
|
||||
|
||||
void writeTo(JsonWriter &writer) const;
|
||||
|
||||
iterator begin() { return iterator(_firstChild); }
|
||||
iterator end() { return iterator(0); }
|
||||
|
||||
|
||||
45
include/ArduinoJson/Internals/JsonObjectImpl.hpp
Normal file
45
include/ArduinoJson/Internals/JsonObjectImpl.hpp
Normal file
@@ -0,0 +1,45 @@
|
||||
// Copyright Benoit Blanchon 2014
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "JsonObjectConstIterator.hpp"
|
||||
#include "JsonObjectIterator.hpp"
|
||||
#include "JsonObjectNode.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
namespace Internals {
|
||||
class JsonObjectImpl {
|
||||
public:
|
||||
typedef const char *key_type;
|
||||
typedef JsonPair value_type;
|
||||
typedef JsonObjectIterator iterator;
|
||||
typedef JsonObjectConstIterator const_iterator;
|
||||
|
||||
JsonObjectImpl(JsonBuffer *buffer) : _buffer(buffer) {}
|
||||
|
||||
JsonValueImpl *operator[](const char *key) { return getOrCreateValueAt(key); }
|
||||
void remove(key_type key);
|
||||
|
||||
JsonArrayImpl *createNestedArray(key_type key);
|
||||
JsonObjectImpl *createNestedObject(key_type key);
|
||||
|
||||
iterator begin() { return iterator(_firstChild); }
|
||||
iterator end() { return iterator(0); }
|
||||
|
||||
const_iterator begin() const { return const_iterator(_firstChild); }
|
||||
const_iterator end() const { return const_iterator(0); }
|
||||
|
||||
private:
|
||||
JsonObjectNode *getNodeAt(key_type key);
|
||||
void removeNode(JsonObjectNode *nodeToRemove);
|
||||
JsonValueImpl *getOrCreateValueAt(key_type key);
|
||||
|
||||
JsonBuffer *_buffer;
|
||||
JsonObjectNode *_firstChild;
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -13,11 +13,14 @@ namespace Internals {
|
||||
|
||||
class JsonSerializer {
|
||||
public:
|
||||
static writeTo(JsonValue& value, JsonWriter&);
|
||||
JsonSerializer(JsonWriter &writer) : _writer(writer) {}
|
||||
|
||||
void serialize(JsonValueImpl *value);
|
||||
void serialize(JsonArrayImpl *value);
|
||||
void serialize(JsonObjectImpl *value);
|
||||
|
||||
private:
|
||||
inline void writeArrayTo(JsonValue& value, JsonWriter&);
|
||||
inline void writeObjectTo(JsonValue& value, JsonWriter&);
|
||||
JsonWriter &_writer;
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -71,6 +71,8 @@ class JsonValueImpl {
|
||||
return _type == JSON_OBJECT ? _content.asObject : NULL;
|
||||
}
|
||||
|
||||
void writeTo(JsonWriter &writer) const;
|
||||
|
||||
private:
|
||||
Internals::JsonValueType _type;
|
||||
Internals::JsonValueContent _content;
|
||||
|
||||
@@ -11,6 +11,8 @@
|
||||
|
||||
namespace ArduinoJson {
|
||||
class JsonArray : public JsonContainer {
|
||||
friend class JsonValue;
|
||||
|
||||
public:
|
||||
typedef JsonValue value_type;
|
||||
typedef Internals::JsonArrayIterator iterator;
|
||||
|
||||
@@ -32,13 +32,8 @@ class JsonBuffer {
|
||||
JsonObject parseObject(char* json);
|
||||
JsonValue parseValue(char* json);
|
||||
|
||||
template <typename T>
|
||||
T* create() {
|
||||
void* p = alloc(sizeof(T));
|
||||
if (!p) return NULL;
|
||||
return new (p) T();
|
||||
}
|
||||
|
||||
virtual void* alloc(size_t size) = 0;
|
||||
};
|
||||
}
|
||||
|
||||
void* operator new(size_t size, ArduinoJson::JsonBuffer* buffer);
|
||||
|
||||
@@ -7,16 +7,18 @@
|
||||
#pragma once
|
||||
|
||||
#include "Arduino/Printable.hpp"
|
||||
#include "Internals/IndentedPrint.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
|
||||
// TODO: renale to JsonPrintable
|
||||
class JsonContainer : public Printable {
|
||||
public:
|
||||
size_t printTo(char *buffer, size_t bufferSize) const;
|
||||
virtual size_t printTo(Print &print) const;
|
||||
|
||||
size_t prettyPrintTo(char *buffer, size_t bufferSize) const;
|
||||
size_t prettyPrintTo(ArduinoJson::Internals::IndentedPrint &print) const;
|
||||
size_t prettyPrintTo(Internals::IndentedPrint &print) const;
|
||||
size_t prettyPrintTo(Print &print) const;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -6,37 +6,40 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Internals/JsonObjectConstIterator.hpp"
|
||||
#include "Internals/JsonObjectIterator.hpp"
|
||||
#include "JsonContainer.hpp"
|
||||
#include "Internals/JsonObjectNode.hpp"
|
||||
#include "Internals/JsonObjectImpl.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
class JsonObject : public JsonContainer {
|
||||
friend class JsonValue;
|
||||
|
||||
public:
|
||||
typedef const char* key_type;
|
||||
typedef JsonPair value_type;
|
||||
typedef Internals::JsonObjectIterator iterator;
|
||||
typedef Internals::JsonObjectConstIterator const_iterator;
|
||||
|
||||
JsonObject(JsonBuffer *buffer) : _buffer(buffer) {}
|
||||
JsonObject(Internals::JsonObjectImpl* impl) : _impl(impl) {}
|
||||
|
||||
JsonValue operator[](const char *key);
|
||||
void remove(const char *key);
|
||||
JsonValue operator[](key_type key);
|
||||
void remove(key_type key);
|
||||
|
||||
JsonArray createNestedArray(const char *key);
|
||||
JsonObject createNestedObject(const char *key);
|
||||
JsonArray createNestedArray(key_type key);
|
||||
JsonObject createNestedObject(key_type key);
|
||||
|
||||
iterator begin() { return iterator(_firstChild); }
|
||||
iterator begin() {
|
||||
if (!_impl) return end();
|
||||
return _impl->begin();
|
||||
}
|
||||
iterator end() { return iterator(0); }
|
||||
|
||||
const_iterator begin() const { return const_iterator(_firstChild); }
|
||||
const_iterator begin() const {
|
||||
if (!_impl) return end();
|
||||
return const_cast<const Internals::JsonObjectImpl*>(_impl)->begin();
|
||||
}
|
||||
const_iterator end() const { return const_iterator(0); }
|
||||
|
||||
private:
|
||||
Internals::JsonNode *getPairAt(const char *key);
|
||||
Internals::JsonNode *getOrCreateValueAt(const char *key);
|
||||
|
||||
JsonBuffer *_buffer;
|
||||
Internals::JsonObjectNode *_firstChild;
|
||||
Internals::JsonObjectImpl* _impl;
|
||||
};
|
||||
}
|
||||
|
||||
33
include/ArduinoJson/JsonObjectIterator.hpp
Normal file
33
include/ArduinoJson/JsonObjectIterator.hpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
|
||||
#include "ArduinoJson/JsonObjectKeyValue.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
class JsonObject;
|
||||
|
||||
class JsonObjectIterator {
|
||||
friend class JsonObject;
|
||||
|
||||
public:
|
||||
explicit JsonObjectIterator(Internals::JsonNode* node) : _node(node) {}
|
||||
|
||||
const char* key() const { return operator*().key(); }
|
||||
|
||||
JsonValue value() const { return operator*().value(); }
|
||||
|
||||
void operator++() { _node = _node->next; }
|
||||
|
||||
JsonObjectKeyValue operator*() const { return JsonObjectKeyValue(_node); }
|
||||
|
||||
bool operator==(const JsonObjectIterator& other) const {
|
||||
return _node == other._node;
|
||||
}
|
||||
|
||||
bool operator!=(const JsonObjectIterator& other) const {
|
||||
return _node != other._node;
|
||||
}
|
||||
|
||||
private:
|
||||
Internals::JsonNode* _node;
|
||||
};
|
||||
}
|
||||
@@ -23,6 +23,9 @@ class JsonValue {
|
||||
if (_impl) _impl->set(value);
|
||||
}
|
||||
|
||||
void operator=(JsonArray array);
|
||||
void operator=(JsonObject object);
|
||||
|
||||
void set(double value, int decimals) {
|
||||
if (_impl) _impl->set(value, decimals);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user