mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-16 12:02:14 +02:00
Epic refactoring in progress...
This commit is contained in:
@ -20,7 +20,6 @@ class IndentedPrint;
|
|||||||
class JsonArrayConstIterator;
|
class JsonArrayConstIterator;
|
||||||
class JsonArrayImpl;
|
class JsonArrayImpl;
|
||||||
class JsonArrayIterator;
|
class JsonArrayIterator;
|
||||||
class JsonNode;
|
|
||||||
class JsonObjectImpl;
|
class JsonObjectImpl;
|
||||||
class JsonObjectIterator;
|
class JsonObjectIterator;
|
||||||
class JsonParser;
|
class JsonParser;
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "../JsonBuffer.hpp"
|
#include "../ForwardDeclarations.hpp"
|
||||||
#include "JsonArrayIterator.hpp"
|
#include "JsonArrayIterator.hpp"
|
||||||
#include "JsonArrayConstIterator.hpp"
|
#include "JsonArrayConstIterator.hpp"
|
||||||
|
|
||||||
@ -20,13 +20,15 @@ class JsonArrayImpl {
|
|||||||
|
|
||||||
JsonArrayImpl(JsonBuffer *buffer) : _buffer(buffer) {}
|
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();
|
JsonArrayImpl *createNestedArray();
|
||||||
JsonObjectImpl *createNestedObject();
|
JsonObjectImpl *createNestedObject();
|
||||||
|
|
||||||
|
void writeTo(JsonWriter &writer) const;
|
||||||
|
|
||||||
iterator begin() { return iterator(_firstChild); }
|
iterator begin() { return iterator(_firstChild); }
|
||||||
iterator end() { return iterator(0); }
|
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 {
|
class JsonSerializer {
|
||||||
public:
|
public:
|
||||||
static writeTo(JsonValue& value, JsonWriter&);
|
JsonSerializer(JsonWriter &writer) : _writer(writer) {}
|
||||||
|
|
||||||
|
void serialize(JsonValueImpl *value);
|
||||||
|
void serialize(JsonArrayImpl *value);
|
||||||
|
void serialize(JsonObjectImpl *value);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
inline void writeArrayTo(JsonValue& value, JsonWriter&);
|
JsonWriter &_writer;
|
||||||
inline void writeObjectTo(JsonValue& value, JsonWriter&);
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -71,6 +71,8 @@ class JsonValueImpl {
|
|||||||
return _type == JSON_OBJECT ? _content.asObject : NULL;
|
return _type == JSON_OBJECT ? _content.asObject : NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void writeTo(JsonWriter &writer) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Internals::JsonValueType _type;
|
Internals::JsonValueType _type;
|
||||||
Internals::JsonValueContent _content;
|
Internals::JsonValueContent _content;
|
||||||
|
@ -11,6 +11,8 @@
|
|||||||
|
|
||||||
namespace ArduinoJson {
|
namespace ArduinoJson {
|
||||||
class JsonArray : public JsonContainer {
|
class JsonArray : public JsonContainer {
|
||||||
|
friend class JsonValue;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
typedef JsonValue value_type;
|
typedef JsonValue value_type;
|
||||||
typedef Internals::JsonArrayIterator iterator;
|
typedef Internals::JsonArrayIterator iterator;
|
||||||
|
@ -32,13 +32,8 @@ class JsonBuffer {
|
|||||||
JsonObject parseObject(char* json);
|
JsonObject parseObject(char* json);
|
||||||
JsonValue parseValue(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;
|
virtual void* alloc(size_t size) = 0;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void* operator new(size_t size, ArduinoJson::JsonBuffer* buffer);
|
||||||
|
@ -7,16 +7,18 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "Arduino/Printable.hpp"
|
#include "Arduino/Printable.hpp"
|
||||||
|
#include "Internals/IndentedPrint.hpp"
|
||||||
|
|
||||||
namespace ArduinoJson {
|
namespace ArduinoJson {
|
||||||
|
|
||||||
|
// TODO: renale to JsonPrintable
|
||||||
class JsonContainer : public Printable {
|
class JsonContainer : public Printable {
|
||||||
public:
|
public:
|
||||||
size_t printTo(char *buffer, size_t bufferSize) const;
|
size_t printTo(char *buffer, size_t bufferSize) const;
|
||||||
virtual size_t printTo(Print &print) const;
|
virtual size_t printTo(Print &print) const;
|
||||||
|
|
||||||
size_t prettyPrintTo(char *buffer, size_t bufferSize) 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;
|
size_t prettyPrintTo(Print &print) const;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -6,37 +6,40 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "Internals/JsonObjectConstIterator.hpp"
|
|
||||||
#include "Internals/JsonObjectIterator.hpp"
|
|
||||||
#include "JsonContainer.hpp"
|
#include "JsonContainer.hpp"
|
||||||
#include "Internals/JsonObjectNode.hpp"
|
#include "Internals/JsonObjectImpl.hpp"
|
||||||
|
|
||||||
namespace ArduinoJson {
|
namespace ArduinoJson {
|
||||||
class JsonObject : public JsonContainer {
|
class JsonObject : public JsonContainer {
|
||||||
|
friend class JsonValue;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
typedef const char* key_type;
|
||||||
typedef JsonPair value_type;
|
typedef JsonPair value_type;
|
||||||
typedef Internals::JsonObjectIterator iterator;
|
typedef Internals::JsonObjectIterator iterator;
|
||||||
typedef Internals::JsonObjectConstIterator const_iterator;
|
typedef Internals::JsonObjectConstIterator const_iterator;
|
||||||
|
|
||||||
JsonObject(JsonBuffer *buffer) : _buffer(buffer) {}
|
JsonObject(Internals::JsonObjectImpl* impl) : _impl(impl) {}
|
||||||
|
|
||||||
JsonValue operator[](const char *key);
|
JsonValue operator[](key_type key);
|
||||||
void remove(const char *key);
|
void remove(key_type key);
|
||||||
|
|
||||||
JsonArray createNestedArray(const char *key);
|
JsonArray createNestedArray(key_type key);
|
||||||
JsonObject createNestedObject(const char *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); }
|
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); }
|
const_iterator end() const { return const_iterator(0); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Internals::JsonNode *getPairAt(const char *key);
|
Internals::JsonObjectImpl* _impl;
|
||||||
Internals::JsonNode *getOrCreateValueAt(const char *key);
|
|
||||||
|
|
||||||
JsonBuffer *_buffer;
|
|
||||||
Internals::JsonObjectNode *_firstChild;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
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);
|
if (_impl) _impl->set(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void operator=(JsonArray array);
|
||||||
|
void operator=(JsonObject object);
|
||||||
|
|
||||||
void set(double value, int decimals) {
|
void set(double value, int decimals) {
|
||||||
if (_impl) _impl->set(value, decimals);
|
if (_impl) _impl->set(value, decimals);
|
||||||
}
|
}
|
||||||
|
@ -4,49 +4,68 @@
|
|||||||
// Arduino JSON library
|
// Arduino JSON library
|
||||||
// https://github.com/bblanchon/ArduinoJson
|
// https://github.com/bblanchon/ArduinoJson
|
||||||
|
|
||||||
#include "ArduinoJson/JsonArray.hpp"
|
#include "ArduinoJson/Internals/JsonArrayImpl.hpp"
|
||||||
#include "ArduinoJson/JsonObject.hpp"
|
|
||||||
#include "ArduinoJson/JsonValue.hpp"
|
#include "ArduinoJson/JsonBuffer.hpp"
|
||||||
|
#include "ArduinoJson/Internals/JsonObjectImpl.hpp"
|
||||||
|
#include "ArduinoJson/Internals/JsonWriter.hpp"
|
||||||
|
|
||||||
using namespace ArduinoJson;
|
using namespace ArduinoJson;
|
||||||
using namespace ArduinoJson::Internals;
|
using namespace ArduinoJson::Internals;
|
||||||
|
|
||||||
JsonValueImpl *JsonArray::operator[](int index) const {
|
JsonValueImpl *JsonArrayImpl::operator[](int index) const {
|
||||||
for (const_iterator it = begin(); it != end(); ++it) {
|
JsonArrayNode *node = _firstChild;
|
||||||
if (!index) return *it;
|
while (node && index--) node = node->next;
|
||||||
index--;
|
|
||||||
}
|
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonValueImpl *JsonArray::add() {
|
JsonValueImpl *JsonArrayImpl::add() {
|
||||||
if (_buffer) return NULL;
|
if (_buffer) return NULL;
|
||||||
|
|
||||||
JsonArrayNode *node = _buffer->create<JsonArrayNode>();
|
JsonArrayNode *node = new (_buffer) JsonArrayNode();
|
||||||
if (!node) return NULL;
|
if (!node) return NULL;
|
||||||
|
|
||||||
return &node.value;
|
return &node->value;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonArrayImpl *JsonArray::createNestedArray() {
|
JsonArrayImpl *JsonArrayImpl::createNestedArray() {
|
||||||
JsonNode *node = createNode();
|
JsonValueImpl *value = add();
|
||||||
|
if (!value) return NULL;
|
||||||
|
|
||||||
if (node) {
|
JsonArrayImpl *array = new (_buffer) JsonArrayImpl(_buffer);
|
||||||
node->setAsArray(_node->getContainerBuffer());
|
value->set(array);
|
||||||
addChild(node);
|
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonObjectImpl *JsonArrayImpl::createNestedObject() {
|
||||||
|
JsonValueImpl *value = add();
|
||||||
|
if (!value) return NULL;
|
||||||
|
|
||||||
|
JsonObjectImpl *array = new (_buffer) JsonObjectImpl(_buffer);
|
||||||
|
value->set(array);
|
||||||
|
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
|
||||||
|
void JsonArrayImpl::writeTo(JsonWriter &writer) const {
|
||||||
|
JsonArrayNode *child = _firstChild;
|
||||||
|
|
||||||
|
if (child) {
|
||||||
|
writer.beginArray();
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
child->value.writeTo(writer);
|
||||||
|
|
||||||
|
child = child->next;
|
||||||
|
if (!child) break;
|
||||||
|
|
||||||
|
writer.writeComma();
|
||||||
|
}
|
||||||
|
|
||||||
|
writer.endArray();
|
||||||
|
} else {
|
||||||
|
writer.writeEmptyArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
return JsonArray(node);
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonObject JsonArray::createNestedObject() {
|
|
||||||
JsonNode *node = createNode();
|
|
||||||
|
|
||||||
if (node) {
|
|
||||||
node->setAsObject(_node->getContainerBuffer());
|
|
||||||
addChild(node);
|
|
||||||
}
|
|
||||||
|
|
||||||
return JsonObject(node);
|
|
||||||
}
|
}
|
||||||
|
118
src/Internals/JsonObjectImpl.cpp
Normal file
118
src/Internals/JsonObjectImpl.cpp
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
// Copyright Benoit Blanchon 2014
|
||||||
|
// MIT License
|
||||||
|
//
|
||||||
|
// Arduino JSON library
|
||||||
|
// https://github.com/bblanchon/ArduinoJson
|
||||||
|
|
||||||
|
#include "ArduinoJson/JsonObject.hpp"
|
||||||
|
|
||||||
|
#include <string.h> // for strcmp
|
||||||
|
|
||||||
|
#include "ArduinoJson/JsonBuffer.hpp"
|
||||||
|
#include "ArduinoJson/Internals/JsonArrayImpl.hpp"
|
||||||
|
#include "ArduinoJson/Internals/JsonValueImpl.hpp"
|
||||||
|
#include "ArduinoJson/Internals/StringBuilder.hpp"
|
||||||
|
|
||||||
|
using namespace ArduinoJson;
|
||||||
|
using namespace ArduinoJson::Internals;
|
||||||
|
|
||||||
|
void JsonObjectImpl::remove(char const *key) { removeNode(getNodeAt(key)); }
|
||||||
|
|
||||||
|
JsonArrayImpl *JsonObjectImpl::createNestedArray(char const *key) {
|
||||||
|
JsonValueImpl *node = getOrCreateValueAt(key);
|
||||||
|
if (!node) return NULL;
|
||||||
|
|
||||||
|
JsonArrayImpl *array = new (_buffer) JsonArrayImpl(_buffer);
|
||||||
|
node->set(array);
|
||||||
|
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonObject JsonObject::createNestedObject(char const *key) {
|
||||||
|
JsonNode *node = getOrCreateValueAt(key);
|
||||||
|
|
||||||
|
if (node) node->setAsObject(_node->getContainerBuffer());
|
||||||
|
|
||||||
|
return JsonObject(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonNode *JsonObject::getPairAt(const char *key) {
|
||||||
|
for (JsonNode *node = firstChild(); node; node = node->next) {
|
||||||
|
if (!strcmp(node->getAsObjectKey(), key)) return node;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonNode *JsonObject::getOrCreateValueAt(const char *key) {
|
||||||
|
JsonNode *existingNode = getPairAt(key);
|
||||||
|
if (existingNode) return existingNode->getAsObjectValue();
|
||||||
|
|
||||||
|
JsonNode *newValueNode = createNode();
|
||||||
|
if (!newValueNode) return 0;
|
||||||
|
|
||||||
|
JsonNode *newKeyNode = createNode();
|
||||||
|
if (!newKeyNode) return 0;
|
||||||
|
|
||||||
|
newKeyNode->setAsObjectKeyValue(key, newValueNode);
|
||||||
|
|
||||||
|
addChild(newKeyNode);
|
||||||
|
|
||||||
|
return newValueNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
void JsonNode::addChild(JsonNode *childToAdd) {
|
||||||
|
if (type == JSON_PROXY) return content.asProxy.target->addChild(childToAdd);
|
||||||
|
|
||||||
|
if (type != JSON_ARRAY && type != JSON_OBJECT) return;
|
||||||
|
|
||||||
|
JsonNode *lastChild = content.asContainer.child;
|
||||||
|
|
||||||
|
if (!lastChild) {
|
||||||
|
content.asContainer.child = childToAdd;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (lastChild->next) lastChild = lastChild->next;
|
||||||
|
|
||||||
|
lastChild->next = childToAdd;
|
||||||
|
}
|
||||||
|
|
||||||
|
void JsonNode::removeChild(JsonNode *childToRemove) {
|
||||||
|
if (type == JSON_PROXY)
|
||||||
|
return content.asProxy.target->removeChild(childToRemove);
|
||||||
|
|
||||||
|
if (type != JSON_ARRAY && type != JSON_OBJECT) return;
|
||||||
|
|
||||||
|
if (content.asContainer.child == childToRemove) {
|
||||||
|
content.asContainer.child = childToRemove->next;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (JsonNode *child = content.asContainer.child; child;
|
||||||
|
child = child->next) {
|
||||||
|
if (child->next == childToRemove) child->next = childToRemove->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void JsonObjectImpl::writeObjectTo(JsonWriter &writer) {
|
||||||
|
JsonObjectNode *child = _firstChild;
|
||||||
|
|
||||||
|
if (child) {
|
||||||
|
writer.beginObject();
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
writer.writeString(child->content.asKeyValue.key);
|
||||||
|
writer.writeColon();
|
||||||
|
child->value->writeTo(writer);
|
||||||
|
|
||||||
|
child = child->next;
|
||||||
|
if (!child) break;
|
||||||
|
|
||||||
|
writer.writeComma();
|
||||||
|
}
|
||||||
|
|
||||||
|
writer.endObject();
|
||||||
|
} else {
|
||||||
|
writer.writeEmptyObject();
|
||||||
|
}
|
||||||
|
}
|
@ -142,7 +142,8 @@ JsonObject JsonParser::parseObject() {
|
|||||||
const char *key = parseString();
|
const char *key = parseString();
|
||||||
if (!key) return NULL;
|
if (!key) return NULL;
|
||||||
|
|
||||||
skip(':')
|
if (!skip(':'))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
JsonValue value = object[key];
|
JsonValue value = object[key];
|
||||||
|
|
||||||
|
@ -1,147 +0,0 @@
|
|||||||
// Copyright Benoit Blanchon 2014
|
|
||||||
// MIT License
|
|
||||||
//
|
|
||||||
// Arduino JSON library
|
|
||||||
// https://github.com/bblanchon/ArduinoJson
|
|
||||||
|
|
||||||
#include "ArduinoJson/Internals/JsonNode.hpp"
|
|
||||||
|
|
||||||
#include "ArduinoJson/Internals/JsonWriter.hpp"
|
|
||||||
#include "ArduinoJson/JsonArray.hpp"
|
|
||||||
#include "ArduinoJson/JsonObject.hpp"
|
|
||||||
#include "ArduinoJson/JsonBuffer.hpp"
|
|
||||||
|
|
||||||
using namespace ArduinoJson::Internals;
|
|
||||||
|
|
||||||
void JsonNode::writeTo(JsonWriter &writer) {
|
|
||||||
switch (type) {
|
|
||||||
case JSON_PROXY:
|
|
||||||
content.asProxy.target->writeTo(writer);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case JSON_ARRAY:
|
|
||||||
writeArrayTo(writer);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case JSON_OBJECT:
|
|
||||||
writeObjectTo(writer);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case JSON_STRING:
|
|
||||||
writer.writeString(content.asString);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case JSON_LONG:
|
|
||||||
writer.writeInteger(content.asInteger);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case JSON_BOOLEAN:
|
|
||||||
writer.writeBoolean(content.asBoolean);
|
|
||||||
break;
|
|
||||||
|
|
||||||
default: // >= JSON_DOUBLE_0_DECIMALS
|
|
||||||
writer.writeDouble(content.asDouble, type - JSON_DOUBLE_0_DECIMALS);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void JsonNode::addChild(JsonNode *childToAdd) {
|
|
||||||
if (type == JSON_PROXY) return content.asProxy.target->addChild(childToAdd);
|
|
||||||
|
|
||||||
if (type != JSON_ARRAY && type != JSON_OBJECT) return;
|
|
||||||
|
|
||||||
JsonNode *lastChild = content.asContainer.child;
|
|
||||||
|
|
||||||
if (!lastChild) {
|
|
||||||
content.asContainer.child = childToAdd;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (lastChild->next) lastChild = lastChild->next;
|
|
||||||
|
|
||||||
lastChild->next = childToAdd;
|
|
||||||
}
|
|
||||||
|
|
||||||
void JsonNode::removeChild(JsonNode *childToRemove) {
|
|
||||||
if (type == JSON_PROXY)
|
|
||||||
return content.asProxy.target->removeChild(childToRemove);
|
|
||||||
|
|
||||||
if (type != JSON_ARRAY && type != JSON_OBJECT) return;
|
|
||||||
|
|
||||||
if (content.asContainer.child == childToRemove) {
|
|
||||||
content.asContainer.child = childToRemove->next;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (JsonNode *child = content.asContainer.child; child;
|
|
||||||
child = child->next) {
|
|
||||||
if (child->next == childToRemove) child->next = childToRemove->next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void JsonNode::writeArrayTo(JsonWriter &writer) {
|
|
||||||
JsonNode *child = content.asContainer.child;
|
|
||||||
|
|
||||||
if (child) {
|
|
||||||
writer.beginArray();
|
|
||||||
|
|
||||||
for (;;) {
|
|
||||||
child->writeTo(writer);
|
|
||||||
|
|
||||||
child = child->next;
|
|
||||||
if (!child) break;
|
|
||||||
|
|
||||||
writer.writeComma();
|
|
||||||
}
|
|
||||||
|
|
||||||
writer.endArray();
|
|
||||||
} else {
|
|
||||||
writer.writeEmptyArray();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void JsonNode::writeObjectTo(JsonWriter &writer) {
|
|
||||||
JsonNode *child = content.asContainer.child;
|
|
||||||
|
|
||||||
if (child) {
|
|
||||||
writer.beginObject();
|
|
||||||
|
|
||||||
for (;;) {
|
|
||||||
writer.writeString(child->content.asKeyValue.key);
|
|
||||||
writer.writeColon();
|
|
||||||
child->content.asKeyValue.value->writeTo(writer);
|
|
||||||
|
|
||||||
child = child->next;
|
|
||||||
if (!child) break;
|
|
||||||
|
|
||||||
writer.writeComma();
|
|
||||||
}
|
|
||||||
|
|
||||||
writer.endObject();
|
|
||||||
} else {
|
|
||||||
writer.writeEmptyObject();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void JsonNode::setAsProxyOfSelf() {
|
|
||||||
JsonBuffer *buffer = content.asContainer.buffer;
|
|
||||||
if (!buffer) return;
|
|
||||||
|
|
||||||
JsonNode *newNode = buffer->createNode();
|
|
||||||
if (!newNode) return;
|
|
||||||
|
|
||||||
*newNode = *this;
|
|
||||||
|
|
||||||
setAsProxyOf(newNode);
|
|
||||||
}
|
|
||||||
|
|
||||||
void JsonNode::duplicate(JsonNode *other) {
|
|
||||||
if (!other) {
|
|
||||||
type = JSON_UNDEFINED;
|
|
||||||
} else if (other->type == JSON_ARRAY || other->type == JSON_OBJECT) {
|
|
||||||
other->setAsProxyOfSelf();
|
|
||||||
setAsProxyOf(other->content.asProxy.target);
|
|
||||||
} else {
|
|
||||||
*this = *other;
|
|
||||||
}
|
|
||||||
}
|
|
35
src/Internals/JsonValueImpl.cpp
Normal file
35
src/Internals/JsonValueImpl.cpp
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
// Copyright Benoit Blanchon 2014
|
||||||
|
// MIT License
|
||||||
|
//
|
||||||
|
// Arduino JSON library
|
||||||
|
// https://github.com/bblanchon/ArduinoJson
|
||||||
|
|
||||||
|
#include "ArduinoJson/Internals/JsonValueImpl.hpp"
|
||||||
|
|
||||||
|
void JsonValueImpl::writeTo(JsonWriter &writer) const {
|
||||||
|
switch (_type) {
|
||||||
|
case JSON_ARRAY:
|
||||||
|
_content.asArray->writeTo(writer);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case JSON_OBJECT:
|
||||||
|
_content.asObject->writeTo(writer);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case JSON_STRING:
|
||||||
|
writer.writeString(content.asString);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case JSON_LONG:
|
||||||
|
writer.writeInteger(content.asInteger);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case JSON_BOOLEAN:
|
||||||
|
writer.writeBoolean(content.asBoolean);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default: // >= JSON_DOUBLE_0_DECIMALS
|
||||||
|
writer.writeDouble(content.asDouble, type - JSON_DOUBLE_0_DECIMALS);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
@ -6,92 +6,12 @@
|
|||||||
|
|
||||||
#include "ArduinoJson/JsonBuffer.hpp"
|
#include "ArduinoJson/JsonBuffer.hpp"
|
||||||
|
|
||||||
#include <new>
|
|
||||||
|
|
||||||
#include "ArduinoJson/Internals/JsonValueInternal.hpp"
|
|
||||||
#include "ArduinoJson/Internals/JsonParser.hpp"
|
#include "ArduinoJson/Internals/JsonParser.hpp"
|
||||||
#include "ArduinoJson/Internals/JsonNode.hpp"
|
|
||||||
|
|
||||||
using namespace ArduinoJson;
|
using namespace ArduinoJson;
|
||||||
using namespace ArduinoJson::Internals;
|
using namespace ArduinoJson::Internals;
|
||||||
|
|
||||||
JsonValue JsonBuffer::createValue() { return JsonValueInternal(createNode()); }
|
// TODO: what happens if alloc returns NULL
|
||||||
|
void* operator new(size_t size, ArduinoJson::JsonBuffer* buffer) {
|
||||||
JsonNode *JsonBuffer::createNode() {
|
return _buffer->alloc(size);
|
||||||
void *node = allocateNode();
|
|
||||||
if (!node) return 0;
|
|
||||||
|
|
||||||
return new (node) JsonNode();
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonArray JsonBuffer::parseArray(char *json) {
|
|
||||||
JsonParser parser(this, json);
|
|
||||||
return JsonArray(parser.parseAnything());
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonObject JsonBuffer::parseObject(char *json) {
|
|
||||||
JsonParser parser(this, json);
|
|
||||||
return JsonObject(parser.parseAnything());
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonValue JsonBuffer::parseValue(char *json) {
|
|
||||||
JsonParser parser(this, json);
|
|
||||||
return JsonValueInternal(parser.parseAnything());
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonNode *JsonBuffer::createArrayNode() {
|
|
||||||
JsonNode *node = createNode();
|
|
||||||
|
|
||||||
if (node) node->setAsArray(this);
|
|
||||||
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonNode *JsonBuffer::createBoolNode(bool value) {
|
|
||||||
JsonNode *node = createNode();
|
|
||||||
|
|
||||||
if (node) node->setAsBoolean(value);
|
|
||||||
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonNode *JsonBuffer::createDoubleNode(double value, int decimals) {
|
|
||||||
JsonNode *node = createNode();
|
|
||||||
|
|
||||||
if (node) node->setAsDouble(value, decimals);
|
|
||||||
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonNode *JsonBuffer::createLongNode(long value) {
|
|
||||||
JsonNode *node = createNode();
|
|
||||||
|
|
||||||
if (node) node->setAsLong(value);
|
|
||||||
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonNode *JsonBuffer::createObjectNode() {
|
|
||||||
JsonNode *node = createNode();
|
|
||||||
|
|
||||||
if (node) node->setAsObject(this);
|
|
||||||
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
|
|
||||||
Internals::JsonNode *JsonBuffer::createObjectKeyValueNode(const char *key,
|
|
||||||
JsonNode *value) {
|
|
||||||
JsonNode *node = createNode();
|
|
||||||
|
|
||||||
if (node) node->setAsObjectKeyValue(key, value);
|
|
||||||
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonNode *JsonBuffer::createStringNode(const char *value) {
|
|
||||||
JsonNode *node = createNode();
|
|
||||||
|
|
||||||
if (node) node->setAsString(value);
|
|
||||||
|
|
||||||
return node;
|
|
||||||
}
|
}
|
||||||
|
@ -6,62 +6,16 @@
|
|||||||
|
|
||||||
#include "ArduinoJson/JsonObject.hpp"
|
#include "ArduinoJson/JsonObject.hpp"
|
||||||
|
|
||||||
#include <string.h> // for strcmp
|
|
||||||
|
|
||||||
#include "ArduinoJson/JsonBuffer.hpp"
|
|
||||||
#include "ArduinoJson/JsonValue.hpp"
|
|
||||||
#include "ArduinoJson/Internals/JsonNode.hpp"
|
|
||||||
#include "ArduinoJson/Internals/StringBuilder.hpp"
|
|
||||||
|
|
||||||
using namespace ArduinoJson;
|
using namespace ArduinoJson;
|
||||||
using namespace ArduinoJson::Internals;
|
|
||||||
|
|
||||||
JsonValue JsonObject::operator[](char const *key) {
|
JsonValue JsonObject::operator[](char const *key) {
|
||||||
JsonNode *node = getOrCreateValueAt(key);
|
return JsonValue(_impl ? (*_impl)[key] : NULL);
|
||||||
return JsonValueInternal(node);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void JsonObject::remove(char const *key) {
|
JsonArray JsonObject::createNestedArray(key_type key) {
|
||||||
JsonNode *nodeToRemove = getPairAt(key);
|
return JsonArray(_impl ? _impl->createNestedArray(key) : NULL);
|
||||||
if (nodeToRemove) removeChild(nodeToRemove);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonArray JsonObject::createNestedArray(char const *key) {
|
JsonObject JsonObject::createNestedObject(key_type key) {
|
||||||
JsonNode *node = getOrCreateValueAt(key);
|
return JsonObject(_impl ? _impl->createNestedObject(key) : NULL);
|
||||||
|
|
||||||
if (node) node->setAsArray(_node->getContainerBuffer());
|
|
||||||
|
|
||||||
return JsonArray(node);
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonObject JsonObject::createNestedObject(char const *key) {
|
|
||||||
JsonNode *node = getOrCreateValueAt(key);
|
|
||||||
|
|
||||||
if (node) node->setAsObject(_node->getContainerBuffer());
|
|
||||||
|
|
||||||
return JsonObject(node);
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonNode *JsonObject::getPairAt(const char *key) {
|
|
||||||
for (JsonNode *node = firstChild(); node; node = node->next) {
|
|
||||||
if (!strcmp(node->getAsObjectKey(), key)) return node;
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonNode *JsonObject::getOrCreateValueAt(const char *key) {
|
|
||||||
JsonNode *existingNode = getPairAt(key);
|
|
||||||
if (existingNode) return existingNode->getAsObjectValue();
|
|
||||||
|
|
||||||
JsonNode *newValueNode = createNode();
|
|
||||||
if (!newValueNode) return 0;
|
|
||||||
|
|
||||||
JsonNode *newKeyNode = createNode();
|
|
||||||
if (!newKeyNode) return 0;
|
|
||||||
|
|
||||||
newKeyNode->setAsObjectKeyValue(key, newValueNode);
|
|
||||||
|
|
||||||
addChild(newKeyNode);
|
|
||||||
|
|
||||||
return newValueNode;
|
|
||||||
}
|
}
|
||||||
|
@ -7,9 +7,22 @@
|
|||||||
#include "ArduinoJson/JsonValue.hpp"
|
#include "ArduinoJson/JsonValue.hpp"
|
||||||
|
|
||||||
#include "ArduinoJson/JsonArray.hpp"
|
#include "ArduinoJson/JsonArray.hpp"
|
||||||
|
#include "ArduinoJson/JsonObject.hpp"
|
||||||
|
|
||||||
using namespace ArduinoJson;
|
using namespace ArduinoJson;
|
||||||
|
|
||||||
JsonValue::operator JsonArray() const {
|
JsonValue::operator JsonArray() const {
|
||||||
return _impl ? JsonArray(*_impl) : JsonArray();
|
return _impl ? JsonArray(*_impl) : JsonArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JsonValue::operator JsonObject() const {
|
||||||
|
return _impl ? JsonObject(*_impl) : JsonObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
void JsonValue::operator=(JsonArray array) {
|
||||||
|
if (_impl) _impl->set(array._impl);
|
||||||
|
}
|
||||||
|
|
||||||
|
void JsonValue::operator=(JsonObject object) {
|
||||||
|
if (_impl) _impl->set(object._impl);
|
||||||
|
}
|
Reference in New Issue
Block a user