mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-17 20:42:24 +02:00
Huge refactoring in progress...
This commit is contained in:
@ -10,23 +10,20 @@
|
|||||||
|
|
||||||
namespace ArduinoJson {
|
namespace ArduinoJson {
|
||||||
class JsonArray;
|
class JsonArray;
|
||||||
|
class JsonArrayConstIterator;
|
||||||
|
class JsonArrayIterator;
|
||||||
class JsonBuffer;
|
class JsonBuffer;
|
||||||
class JsonPair;
|
|
||||||
class JsonValue;
|
|
||||||
class JsonObject;
|
class JsonObject;
|
||||||
|
class JsonObjectConstIterator;
|
||||||
|
class JsonObjectIterator;
|
||||||
|
struct JsonPair;
|
||||||
|
class JsonValue;
|
||||||
|
|
||||||
namespace Internals {
|
namespace Internals {
|
||||||
class IndentedPrint;
|
class IndentedPrint;
|
||||||
class JsonArrayConstIterator;
|
|
||||||
class JsonArrayImpl;
|
|
||||||
class JsonArrayIterator;
|
|
||||||
class JsonArrayNode;
|
class JsonArrayNode;
|
||||||
class JsonObjectConstIterator;
|
|
||||||
class JsonObjectImpl;
|
|
||||||
class JsonObjectIterator;
|
|
||||||
class JsonObjectNode;
|
class JsonObjectNode;
|
||||||
class JsonParser;
|
class JsonParser;
|
||||||
class JsonValueImpl;
|
|
||||||
class JsonWriter;
|
class JsonWriter;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,48 +0,0 @@
|
|||||||
// Copyright Benoit Blanchon 2014
|
|
||||||
// MIT License
|
|
||||||
//
|
|
||||||
// Arduino JSON library
|
|
||||||
// https://github.com/bblanchon/ArduinoJson
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "../ForwardDeclarations.hpp"
|
|
||||||
#include "JsonArrayIterator.hpp"
|
|
||||||
#include "JsonArrayConstIterator.hpp"
|
|
||||||
|
|
||||||
namespace ArduinoJson {
|
|
||||||
namespace Internals {
|
|
||||||
class JsonArrayImpl {
|
|
||||||
public:
|
|
||||||
typedef JsonValueImpl *value_type;
|
|
||||||
typedef JsonArrayIterator iterator;
|
|
||||||
typedef JsonArrayConstIterator const_iterator;
|
|
||||||
|
|
||||||
static JsonArrayImpl *createFrom(JsonBuffer *buffer);
|
|
||||||
|
|
||||||
int size() const;
|
|
||||||
|
|
||||||
value_type operator[](int index) const;
|
|
||||||
value_type add();
|
|
||||||
|
|
||||||
JsonArrayImpl *createNestedArray();
|
|
||||||
JsonObjectImpl *createNestedObject();
|
|
||||||
|
|
||||||
void writeTo(JsonWriter &writer) const;
|
|
||||||
|
|
||||||
iterator begin() { return iterator(_firstNode); }
|
|
||||||
iterator end() { return iterator(0); }
|
|
||||||
|
|
||||||
const_iterator begin() const { return const_iterator(_firstNode); }
|
|
||||||
const_iterator end() const { return const_iterator(0); }
|
|
||||||
|
|
||||||
private:
|
|
||||||
JsonArrayImpl(JsonBuffer *buffer) : _buffer(buffer), _firstNode(NULL) {}
|
|
||||||
|
|
||||||
inline void addNode(JsonArrayNode *node);
|
|
||||||
|
|
||||||
JsonBuffer *_buffer;
|
|
||||||
Internals::JsonArrayNode *_firstNode;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "JsonValueImpl.hpp"
|
#include "../JsonValue.hpp"
|
||||||
#include "../JsonBuffer.hpp"
|
#include "../JsonBuffer.hpp"
|
||||||
|
|
||||||
namespace ArduinoJson {
|
namespace ArduinoJson {
|
||||||
@ -20,7 +20,7 @@ class JsonArrayNode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
JsonArrayNode* next;
|
JsonArrayNode* next;
|
||||||
JsonValueImpl value;
|
JsonValue value;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
JsonArrayNode() : next(0) {}
|
JsonArrayNode() : next(0) {}
|
||||||
|
@ -1,53 +0,0 @@
|
|||||||
// 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;
|
|
||||||
|
|
||||||
static JsonObjectImpl *createFrom(JsonBuffer *buffer);
|
|
||||||
|
|
||||||
int size() const;
|
|
||||||
|
|
||||||
JsonValueImpl *operator[](const char *key);
|
|
||||||
void remove(key_type key);
|
|
||||||
|
|
||||||
JsonArrayImpl *createNestedArray(key_type key);
|
|
||||||
JsonObjectImpl *createNestedObject(key_type key);
|
|
||||||
|
|
||||||
iterator begin() { return iterator(_firstNode); }
|
|
||||||
iterator end() { return iterator(0); }
|
|
||||||
|
|
||||||
const_iterator begin() const { return const_iterator(_firstNode); }
|
|
||||||
const_iterator end() const { return const_iterator(0); }
|
|
||||||
|
|
||||||
void writeTo(JsonWriter &writer) const;
|
|
||||||
|
|
||||||
private:
|
|
||||||
JsonObjectImpl(JsonBuffer *buffer) : _buffer(buffer), _firstNode(NULL) {}
|
|
||||||
|
|
||||||
void addNode(JsonObjectNode *nodeToAdd);
|
|
||||||
void removeNode(JsonObjectNode *nodeToRemove);
|
|
||||||
|
|
||||||
JsonObjectNode *getNodeAt(key_type key);
|
|
||||||
JsonObjectNode *getOrCreateNodeAt(key_type key);
|
|
||||||
|
|
||||||
JsonBuffer *_buffer;
|
|
||||||
JsonObjectNode *_firstNode;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,36 +0,0 @@
|
|||||||
// Copyright Benoit Blanchon 2014
|
|
||||||
// MIT License
|
|
||||||
//
|
|
||||||
// Arduino JSON library
|
|
||||||
// https://github.com/bblanchon/ArduinoJson
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
namespace ArduinoJson {
|
|
||||||
namespace Internals {
|
|
||||||
|
|
||||||
class JsonObjectIterator {
|
|
||||||
public:
|
|
||||||
explicit JsonObjectIterator(Internals::JsonObjectNode *node) : _pair(node) {}
|
|
||||||
|
|
||||||
JsonPair &operator*() { return _pair; }
|
|
||||||
JsonPair *operator->() { return &_pair; }
|
|
||||||
|
|
||||||
bool operator==(const JsonObjectIterator &other) const {
|
|
||||||
return _pair._node == other._pair._node;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator!=(const JsonObjectIterator &other) const {
|
|
||||||
return _pair._node != other._pair._node;
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonObjectIterator &operator++() {
|
|
||||||
if (_pair._node) _pair._node = _pair._node->next;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
JsonPair _pair;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "JsonValueImpl.hpp"
|
#include "../JsonPair.hpp"
|
||||||
#include "../JsonBuffer.hpp"
|
#include "../JsonBuffer.hpp"
|
||||||
|
|
||||||
namespace ArduinoJson {
|
namespace ArduinoJson {
|
||||||
@ -19,12 +19,11 @@ class JsonObjectNode {
|
|||||||
return ptr ? new (ptr) JsonObjectNode(key) : NULL;
|
return ptr ? new (ptr) JsonObjectNode(key) : NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* const key;
|
JsonPair pair;
|
||||||
JsonValueImpl value;
|
|
||||||
JsonObjectNode* next;
|
JsonObjectNode* next;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
JsonObjectNode(const char* k) : key(k), next(NULL) {}
|
JsonObjectNode(const char* k) : pair(k), next(NULL) {}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,9 +16,8 @@ class JsonParser {
|
|||||||
public:
|
public:
|
||||||
JsonParser(JsonBuffer *buffer, char *json) : _buffer(buffer), _ptr(json) {}
|
JsonParser(JsonBuffer *buffer, char *json) : _buffer(buffer), _ptr(json) {}
|
||||||
|
|
||||||
JsonArray parseArray();
|
JsonArray &parseArray();
|
||||||
JsonObject parseObject();
|
JsonObject &parseObject();
|
||||||
JsonValue parseValue();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool isEnd() { return *_ptr == 0; }
|
bool isEnd() { return *_ptr == 0; }
|
||||||
@ -26,7 +25,7 @@ class JsonParser {
|
|||||||
bool skip(char charToSkip);
|
bool skip(char charToSkip);
|
||||||
void skipSpaces();
|
void skipSpaces();
|
||||||
|
|
||||||
void parseValueTo(JsonValue);
|
void parseAnythingTo(JsonValue &destination);
|
||||||
inline void parseBooleanTo(JsonValue &destination);
|
inline void parseBooleanTo(JsonValue &destination);
|
||||||
inline void parseNullTo(JsonValue &destination);
|
inline void parseNullTo(JsonValue &destination);
|
||||||
inline void parseNumberTo(JsonValue &destination);
|
inline void parseNumberTo(JsonValue &destination);
|
||||||
|
@ -16,8 +16,8 @@ union JsonValueContent {
|
|||||||
double asDouble;
|
double asDouble;
|
||||||
long asInteger;
|
long asInteger;
|
||||||
const char* asString;
|
const char* asString;
|
||||||
JsonArrayImpl* asArray;
|
JsonArray* asArray;
|
||||||
JsonObjectImpl* asObject;
|
JsonObject* asObject;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,83 +0,0 @@
|
|||||||
// Copyright Benoit Blanchon 2014
|
|
||||||
// MIT License
|
|
||||||
//
|
|
||||||
// Arduino JSON library
|
|
||||||
// https://github.com/bblanchon/ArduinoJson
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
|
|
||||||
#include "../ForwardDeclarations.hpp"
|
|
||||||
#include "JsonValueContent.hpp"
|
|
||||||
#include "JsonValueType.hpp"
|
|
||||||
|
|
||||||
namespace ArduinoJson {
|
|
||||||
namespace Internals {
|
|
||||||
|
|
||||||
class JsonValueImpl {
|
|
||||||
public:
|
|
||||||
JsonValueImpl() : _type(JSON_UNDEFINED) {}
|
|
||||||
|
|
||||||
static JsonValueImpl *createFrom(JsonBuffer *buffer);
|
|
||||||
|
|
||||||
void set(bool value) {
|
|
||||||
_type = JSON_BOOLEAN;
|
|
||||||
_content.asBoolean = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
void set(const char *value) {
|
|
||||||
_type = JSON_STRING;
|
|
||||||
_content.asString = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
void set(double value, int decimals = 2) {
|
|
||||||
_type = static_cast<JsonValueType>(JSON_DOUBLE_0_DECIMALS + decimals);
|
|
||||||
_content.asDouble = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
void set(long value) {
|
|
||||||
_type = JSON_LONG;
|
|
||||||
_content.asInteger = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
void set(JsonArrayImpl *array) {
|
|
||||||
_type = JSON_ARRAY;
|
|
||||||
_content.asArray = array;
|
|
||||||
}
|
|
||||||
|
|
||||||
void set(JsonObjectImpl *object) {
|
|
||||||
_type = JSON_OBJECT;
|
|
||||||
_content.asObject = object;
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonArrayImpl *asArray() {
|
|
||||||
return _type == JSON_ARRAY ? _content.asArray : NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonObjectImpl *asObject() {
|
|
||||||
return _type == JSON_OBJECT ? _content.asObject : NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool asBool() const {
|
|
||||||
return _type == JSON_BOOLEAN ? _content.asBoolean : false;
|
|
||||||
}
|
|
||||||
|
|
||||||
const char *asString() const {
|
|
||||||
return _type == JSON_STRING ? _content.asString : NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
double asDouble() const {
|
|
||||||
return _type >= JSON_DOUBLE_0_DECIMALS ? _content.asDouble : 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
long asLong() const { return _type == JSON_LONG ? _content.asInteger : 0; }
|
|
||||||
|
|
||||||
void writeTo(JsonWriter &writer) const;
|
|
||||||
|
|
||||||
private:
|
|
||||||
Internals::JsonValueType _type;
|
|
||||||
Internals::JsonValueContent _content;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
@ -11,6 +11,7 @@ namespace Internals {
|
|||||||
|
|
||||||
enum JsonValueType {
|
enum JsonValueType {
|
||||||
JSON_UNDEFINED,
|
JSON_UNDEFINED,
|
||||||
|
JSON_INVALID,
|
||||||
JSON_ARRAY,
|
JSON_ARRAY,
|
||||||
JSON_OBJECT,
|
JSON_OBJECT,
|
||||||
JSON_BOOLEAN,
|
JSON_BOOLEAN,
|
||||||
|
@ -6,58 +6,48 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "ForwardDeclarations.hpp"
|
||||||
|
#include "JsonArrayIterator.hpp"
|
||||||
|
#include "JsonArrayConstIterator.hpp"
|
||||||
#include "JsonPrintable.hpp"
|
#include "JsonPrintable.hpp"
|
||||||
#include "Internals/JsonArrayImpl.hpp"
|
|
||||||
|
|
||||||
namespace ArduinoJson {
|
namespace ArduinoJson {
|
||||||
class JsonArray : public JsonPrintable {
|
class JsonArray : public JsonPrintable {
|
||||||
friend class JsonValue;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
typedef JsonValue value_type;
|
typedef JsonValue value_type;
|
||||||
typedef Internals::JsonArrayIterator iterator;
|
typedef JsonArrayIterator iterator;
|
||||||
typedef Internals::JsonArrayConstIterator const_iterator;
|
typedef JsonArrayConstIterator const_iterator;
|
||||||
|
|
||||||
JsonArray() : _impl(NULL) {}
|
JsonArray(JsonBuffer *buffer = NULL) : _buffer(buffer), _firstNode(NULL) {}
|
||||||
JsonArray(Internals::JsonArrayImpl* impl) : _impl(impl) {}
|
|
||||||
|
|
||||||
bool success() const { return _impl; }
|
int size() const;
|
||||||
|
|
||||||
int size() const { return _impl ? _impl->size() : 0; }
|
value_type &operator[](int index) const;
|
||||||
|
value_type &add();
|
||||||
value_type operator[](int index) const;
|
|
||||||
value_type add();
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void add(T value) {
|
void add(T value) {
|
||||||
add().set(value);
|
add().set(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void add(double value, int decimals = 2) { add().set(value, decimals); }
|
JsonArray &createNestedArray();
|
||||||
|
JsonObject &createNestedObject();
|
||||||
|
|
||||||
JsonArray createNestedArray();
|
iterator begin() { return iterator(_firstNode); }
|
||||||
JsonObject createNestedObject();
|
|
||||||
|
|
||||||
iterator begin() {
|
|
||||||
if (!_impl) return end();
|
|
||||||
return _impl->begin();
|
|
||||||
}
|
|
||||||
iterator end() { return iterator(0); }
|
iterator end() { return iterator(0); }
|
||||||
|
|
||||||
const_iterator begin() const {
|
const_iterator begin() const { return const_iterator(_firstNode); }
|
||||||
if (!_impl) return end();
|
|
||||||
return const_cast<const Internals::JsonArrayImpl*>(_impl)->begin();
|
|
||||||
}
|
|
||||||
const_iterator end() const { return const_iterator(0); }
|
const_iterator end() const { return const_iterator(0); }
|
||||||
|
|
||||||
bool operator==(const JsonArray& other) const { return _impl == other._impl; }
|
static JsonArray &invalid() { return _invalid; }
|
||||||
|
|
||||||
protected:
|
virtual void writeTo(Internals::JsonWriter &writer) const;
|
||||||
virtual void writeTo(Internals::JsonWriter& writer) const {
|
|
||||||
if (_impl) _impl->writeTo(writer);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Internals::JsonArrayImpl* _impl;
|
inline void addNode(Internals::JsonArrayNode *node);
|
||||||
|
|
||||||
|
JsonBuffer *_buffer;
|
||||||
|
Internals::JsonArrayNode *_firstNode;
|
||||||
|
static JsonArray _invalid;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -6,18 +6,17 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "JsonArrayNode.hpp"
|
#include "Internals/JsonArrayNode.hpp"
|
||||||
|
|
||||||
namespace ArduinoJson {
|
namespace ArduinoJson {
|
||||||
namespace Internals {
|
|
||||||
|
|
||||||
// TODO: copy from JsonArrayIterator
|
|
||||||
class JsonArrayConstIterator {
|
class JsonArrayConstIterator {
|
||||||
public:
|
public:
|
||||||
explicit JsonArrayConstIterator(JsonArrayNode *node) : _node(node) {}
|
explicit JsonArrayConstIterator(Internals::JsonArrayNode *node)
|
||||||
|
: _node(node) {}
|
||||||
|
|
||||||
const JsonValueImpl &operator*() const { return _node->value; }
|
const JsonValue &operator*() const { return _node->value; }
|
||||||
const JsonValueImpl *operator->() { return &_node->value; }
|
const JsonValue *operator->() { return &_node->value; }
|
||||||
|
|
||||||
bool operator==(const JsonArrayConstIterator &other) const {
|
bool operator==(const JsonArrayConstIterator &other) const {
|
||||||
return _node == other._node;
|
return _node == other._node;
|
||||||
@ -28,12 +27,11 @@ class JsonArrayConstIterator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
JsonArrayConstIterator &operator++() {
|
JsonArrayConstIterator &operator++() {
|
||||||
_node = _node->next;
|
if (_node) _node = _node->next;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
JsonArrayNode *_node;
|
Internals::JsonArrayNode *_node;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
|
@ -6,19 +6,16 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "JsonArrayNode.hpp"
|
#include "Internals/JsonArrayNode.hpp"
|
||||||
|
|
||||||
namespace ArduinoJson {
|
namespace ArduinoJson {
|
||||||
namespace Internals {
|
|
||||||
|
|
||||||
class JsonArrayIterator {
|
class JsonArrayIterator {
|
||||||
public:
|
public:
|
||||||
explicit JsonArrayIterator(Internals::JsonArrayNode *node) : _node(node) {
|
explicit JsonArrayIterator(Internals::JsonArrayNode *node) : _node(node) {}
|
||||||
updateValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonValue operator*() const { return _value; }
|
JsonValue &operator*() const { return _node->value; }
|
||||||
JsonValue *operator->() { return &_value; }
|
JsonValue *operator->() { return &_node->value; }
|
||||||
|
|
||||||
bool operator==(const JsonArrayIterator &other) const {
|
bool operator==(const JsonArrayIterator &other) const {
|
||||||
return _node == other._node;
|
return _node == other._node;
|
||||||
@ -29,18 +26,11 @@ class JsonArrayIterator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
JsonArrayIterator &operator++() {
|
JsonArrayIterator &operator++() {
|
||||||
if (_node) {
|
if (_node) _node = _node->next;
|
||||||
_node = _node->next;
|
|
||||||
updateValue();
|
|
||||||
}
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void updateValue() { _value = JsonValue(_node ? &_node->value : NULL); }
|
Internals::JsonArrayNode *_node;
|
||||||
|
|
||||||
JsonArrayNode *_node;
|
|
||||||
JsonValue _value;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
|
@ -17,21 +17,12 @@ class JsonBuffer {
|
|||||||
public:
|
public:
|
||||||
virtual ~JsonBuffer() {}
|
virtual ~JsonBuffer() {}
|
||||||
|
|
||||||
JsonArray createArray();
|
JsonArray &createArray();
|
||||||
JsonObject createObject();
|
JsonObject &createObject();
|
||||||
JsonValue createValue();
|
|
||||||
|
|
||||||
template <typename T>
|
JsonArray &parseArray(char *json);
|
||||||
JsonValue createValue(T x) {
|
JsonObject &parseObject(char *json);
|
||||||
JsonValue value;
|
|
||||||
value = x;
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonArray parseArray(char* json);
|
virtual void *alloc(size_t size) = 0;
|
||||||
JsonObject parseObject(char* json);
|
|
||||||
JsonValue parseValue(char* json);
|
|
||||||
|
|
||||||
virtual void* alloc(size_t size) = 0;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -6,56 +6,53 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "JsonObjectConstIterator.hpp"
|
||||||
|
#include "JsonObjectIterator.hpp"
|
||||||
#include "JsonPrintable.hpp"
|
#include "JsonPrintable.hpp"
|
||||||
#include "Internals/JsonObjectImpl.hpp"
|
#include "Internals/JsonObjectNode.hpp"
|
||||||
|
|
||||||
namespace ArduinoJson {
|
namespace ArduinoJson {
|
||||||
class JsonObject : public JsonPrintable {
|
class JsonObject : public JsonPrintable {
|
||||||
friend class JsonValue;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
typedef const char* key_type;
|
typedef const char *key_type;
|
||||||
typedef JsonPair value_type;
|
typedef JsonPair value_type;
|
||||||
typedef Internals::JsonObjectIterator iterator;
|
typedef JsonObjectIterator iterator;
|
||||||
typedef Internals::JsonObjectConstIterator const_iterator;
|
typedef JsonObjectConstIterator const_iterator;
|
||||||
|
|
||||||
JsonObject() : _impl(NULL) {}
|
JsonObject(JsonBuffer *buffer) : _buffer(buffer), _firstNode(NULL) {}
|
||||||
JsonObject(Internals::JsonObjectImpl* impl) : _impl(impl) {}
|
|
||||||
|
|
||||||
bool success() const { return _impl; }
|
int size() const;
|
||||||
|
|
||||||
int size() const { return _impl ? _impl->size() : 0; }
|
JsonValue &operator[](key_type key);
|
||||||
|
void remove(key_type key);
|
||||||
|
|
||||||
JsonValue operator[](key_type key);
|
template <typename T>
|
||||||
void remove(key_type key) {
|
void add(key_type key, T value) {
|
||||||
if (_impl) _impl->remove(key);
|
(*this)[key] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonArray createNestedArray(key_type key);
|
JsonArray &createNestedArray(key_type key);
|
||||||
JsonObject createNestedObject(key_type key);
|
JsonObject &createNestedObject(key_type key);
|
||||||
|
|
||||||
iterator begin() {
|
iterator begin() { return iterator(_firstNode); }
|
||||||
if (!_impl) return end();
|
|
||||||
return _impl->begin();
|
|
||||||
}
|
|
||||||
iterator end() { return iterator(0); }
|
iterator end() { return iterator(0); }
|
||||||
|
|
||||||
const_iterator begin() const {
|
const_iterator begin() const { return const_iterator(_firstNode); }
|
||||||
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); }
|
||||||
|
|
||||||
bool operator==(const JsonObject& other) const {
|
static JsonObject &invalid() { return _invalid; }
|
||||||
return _impl == other._impl;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
virtual void writeTo(Internals::JsonWriter &writer) const;
|
||||||
virtual void writeTo(Internals::JsonWriter& writer) const {
|
|
||||||
if (_impl) _impl->writeTo(writer);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Internals::JsonObjectImpl* _impl;
|
void addNode(Internals::JsonObjectNode *nodeToAdd);
|
||||||
|
void removeNode(Internals::JsonObjectNode *nodeToRemove);
|
||||||
|
|
||||||
|
Internals::JsonObjectNode *getNodeAt(key_type key);
|
||||||
|
Internals::JsonObjectNode *getOrCreateNodeAt(key_type key);
|
||||||
|
|
||||||
|
JsonBuffer *_buffer;
|
||||||
|
Internals::JsonObjectNode *_firstNode;
|
||||||
|
static JsonObject _invalid;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -6,35 +6,33 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "../JsonPair.hpp"
|
#include "ForwardDeclarations.hpp"
|
||||||
#include "JsonObjectNode.hpp"
|
#include "Internals/JsonObjectNode.hpp"
|
||||||
|
|
||||||
namespace ArduinoJson {
|
namespace ArduinoJson {
|
||||||
namespace Internals {
|
|
||||||
|
|
||||||
class JsonObjectConstIterator {
|
class JsonObjectConstIterator {
|
||||||
public:
|
public:
|
||||||
explicit JsonObjectConstIterator(Internals::JsonObjectNode *node)
|
explicit JsonObjectConstIterator(Internals::JsonObjectNode *node)
|
||||||
: _pair(node) {}
|
: _node(node) {}
|
||||||
|
|
||||||
const JsonPair operator*() const { return _pair; }
|
const JsonPair &operator*() { return _node->pair; }
|
||||||
const JsonPair *operator->() { return &_pair; }
|
const JsonPair *operator->() { return &_node->pair; }
|
||||||
|
|
||||||
bool operator==(const JsonObjectConstIterator &other) const {
|
bool operator==(const JsonObjectConstIterator &other) const {
|
||||||
return _pair._node == other._pair._node;
|
return _node == other._node;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator!=(const JsonObjectConstIterator &other) const {
|
bool operator!=(const JsonObjectConstIterator &other) const {
|
||||||
return _pair._node != other._pair._node;
|
return _node != other._node;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonObjectConstIterator &operator++() {
|
JsonObjectConstIterator &operator++() {
|
||||||
_pair._node = _pair._node->next;
|
if (_node) _node = _node->next;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
JsonPair _pair;
|
Internals::JsonObjectNode *_node;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
|
@ -1,33 +1,37 @@
|
|||||||
|
// Copyright Benoit Blanchon 2014
|
||||||
|
// MIT License
|
||||||
|
//
|
||||||
|
// Arduino JSON library
|
||||||
|
// https://github.com/bblanchon/ArduinoJson
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "ArduinoJson/JsonObjectKeyValue.hpp"
|
#include "ForwardDeclarations.hpp"
|
||||||
|
#include "Internals/JsonObjectNode.hpp"
|
||||||
|
|
||||||
namespace ArduinoJson {
|
namespace ArduinoJson {
|
||||||
class JsonObject;
|
|
||||||
|
|
||||||
class JsonObjectIterator {
|
class JsonObjectIterator {
|
||||||
friend class JsonObject;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit JsonObjectIterator(Internals::JsonNode* node) : _node(node) {}
|
explicit JsonObjectIterator(Internals::JsonObjectNode *node) : _node(node) {}
|
||||||
|
|
||||||
const char* key() const { return operator*().key(); }
|
JsonPair &operator*() { return _node->pair; }
|
||||||
|
JsonPair *operator->() { return &_node->pair; }
|
||||||
|
|
||||||
JsonValue value() const { return operator*().value(); }
|
bool operator==(const JsonObjectIterator &other) const {
|
||||||
|
|
||||||
void operator++() { _node = _node->next; }
|
|
||||||
|
|
||||||
JsonObjectKeyValue operator*() const { return JsonObjectKeyValue(_node); }
|
|
||||||
|
|
||||||
bool operator==(const JsonObjectIterator& other) const {
|
|
||||||
return _node == other._node;
|
return _node == other._node;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator!=(const JsonObjectIterator& other) const {
|
bool operator!=(const JsonObjectIterator &other) const {
|
||||||
return _node != other._node;
|
return _node != other._node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JsonObjectIterator &operator++() {
|
||||||
|
if (_node) _node = _node->next;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Internals::JsonNode* _node;
|
Internals::JsonObjectNode *_node;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -10,17 +10,10 @@
|
|||||||
#include "Internals/JsonObjectNode.hpp"
|
#include "Internals/JsonObjectNode.hpp"
|
||||||
|
|
||||||
namespace ArduinoJson {
|
namespace ArduinoJson {
|
||||||
class JsonPair {
|
struct JsonPair {
|
||||||
friend class Internals::JsonObjectIterator;
|
JsonPair(const char* k) : key(k) {}
|
||||||
friend class Internals::JsonObjectConstIterator;
|
|
||||||
|
|
||||||
public:
|
const char* const key;
|
||||||
JsonPair(Internals::JsonObjectNode *node) : _node(node) {}
|
JsonValue value;
|
||||||
|
|
||||||
const char *key() const { return _node->key; }
|
|
||||||
JsonValue value() { return JsonValue(&_node->value); }
|
|
||||||
|
|
||||||
private:
|
|
||||||
Internals::JsonObjectNode *_node;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -9,62 +9,45 @@
|
|||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
#include "ForwardDeclarations.hpp"
|
#include "ForwardDeclarations.hpp"
|
||||||
#include "Internals/JsonValueImpl.hpp"
|
#include "Internals/JsonValueContent.hpp"
|
||||||
|
#include "Internals/JsonValueType.hpp"
|
||||||
|
|
||||||
namespace ArduinoJson {
|
namespace ArduinoJson {
|
||||||
|
|
||||||
class JsonValue {
|
class JsonValue {
|
||||||
public:
|
public:
|
||||||
JsonValue() : _impl(NULL) {}
|
JsonValue() : _type(Internals::JSON_UNDEFINED) {}
|
||||||
JsonValue(Internals::JsonValueImpl *impl) : _impl(impl) {}
|
|
||||||
|
|
||||||
void set(const char *value) {
|
void set(bool value);
|
||||||
if (_impl) _impl->set(value);
|
void set(const char *value);
|
||||||
}
|
void set(double value, int decimals = 2);
|
||||||
void set(bool value) {
|
void set(int value) { set(static_cast<long>(value)); }
|
||||||
if (_impl) _impl->set(value);
|
void set(long value);
|
||||||
}
|
void set(JsonArray &array);
|
||||||
void set(double value, int decimals = 2) {
|
void set(JsonObject &object);
|
||||||
if (_impl) _impl->set(value, decimals);
|
|
||||||
}
|
|
||||||
void set(int value) {
|
|
||||||
if (_impl) _impl->set(static_cast<long>(value));
|
|
||||||
}
|
|
||||||
void set(long value) {
|
|
||||||
if (_impl) _impl->set(value);
|
|
||||||
}
|
|
||||||
void set(JsonObject object);
|
|
||||||
void set(JsonArray array);
|
|
||||||
|
|
||||||
operator bool() const { return _impl ? _impl->asBool() : false; }
|
JsonArray &asArray();
|
||||||
operator int() const { return _impl ? _impl->asLong() : 0; }
|
JsonObject &asObject();
|
||||||
operator long() const { return _impl ? _impl->asLong() : 0; }
|
bool asBool() const;
|
||||||
operator double() const { return _impl ? _impl->asDouble() : 0.0; }
|
const char *asString() const;
|
||||||
operator const char *() const {
|
double asDouble() const;
|
||||||
return _impl ? _impl->asString() : static_cast<char *>(NULL);
|
long asLong() const;
|
||||||
}
|
|
||||||
operator JsonArray();
|
|
||||||
operator JsonObject();
|
|
||||||
|
|
||||||
bool success() { return _impl; }
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
JsonValue operator=(T value) {
|
JsonValue &operator=(T value) {
|
||||||
set(value);
|
set(value);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
static JsonValue &invalid() { return _invalid; }
|
||||||
T as() {
|
|
||||||
return static_cast<T>(*this);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
bool success() { return _type != Internals::JSON_INVALID; }
|
||||||
virtual void writeTo(Internals::JsonWriter &writer) const {
|
|
||||||
if (_impl) _impl->writeTo(writer);
|
void writeTo(Internals::JsonWriter &writer) const;
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Internals::JsonValueImpl *_impl;
|
Internals::JsonValueType _type;
|
||||||
|
Internals::JsonValueContent _content;
|
||||||
|
static JsonValue _invalid;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -1,91 +0,0 @@
|
|||||||
// Copyright Benoit Blanchon 2014
|
|
||||||
// MIT License
|
|
||||||
//
|
|
||||||
// Arduino JSON library
|
|
||||||
// https://github.com/bblanchon/ArduinoJson
|
|
||||||
|
|
||||||
#include "ArduinoJson/Internals/JsonArrayImpl.hpp"
|
|
||||||
|
|
||||||
#include "ArduinoJson/JsonBuffer.hpp"
|
|
||||||
#include "ArduinoJson/Internals/JsonObjectImpl.hpp"
|
|
||||||
#include "ArduinoJson/Internals/JsonWriter.hpp"
|
|
||||||
|
|
||||||
using namespace ArduinoJson;
|
|
||||||
using namespace ArduinoJson::Internals;
|
|
||||||
|
|
||||||
JsonArrayImpl *JsonArrayImpl::createFrom(JsonBuffer *buffer) {
|
|
||||||
void *ptr = buffer->alloc(sizeof(JsonArrayImpl));
|
|
||||||
return ptr ? new (ptr) JsonArrayImpl(buffer) : NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
int JsonArrayImpl::size() const {
|
|
||||||
int nodeCount = 0;
|
|
||||||
for (JsonArrayNode *node = _firstNode; node; node = node->next) nodeCount++;
|
|
||||||
return nodeCount;
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonValueImpl *JsonArrayImpl::operator[](int index) const {
|
|
||||||
JsonArrayNode *node = _firstNode;
|
|
||||||
while (node && index--) node = node->next;
|
|
||||||
return node ? &node->value : NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonValueImpl *JsonArrayImpl::add() {
|
|
||||||
JsonArrayNode *node = JsonArrayNode::createFrom(_buffer);
|
|
||||||
if (!node) return NULL;
|
|
||||||
|
|
||||||
addNode(node);
|
|
||||||
|
|
||||||
return &node->value;
|
|
||||||
}
|
|
||||||
|
|
||||||
void JsonArrayImpl::addNode(JsonArrayNode *newNode) {
|
|
||||||
if (_firstNode) {
|
|
||||||
JsonArrayNode *lastNode = _firstNode;
|
|
||||||
while (lastNode->next) lastNode = lastNode->next;
|
|
||||||
lastNode->next = newNode;
|
|
||||||
} else {
|
|
||||||
_firstNode = newNode;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonArrayImpl *JsonArrayImpl::createNestedArray() {
|
|
||||||
JsonValueImpl *value = add();
|
|
||||||
if (!value) return NULL;
|
|
||||||
|
|
||||||
JsonArrayImpl *array = JsonArrayImpl::createFrom(_buffer);
|
|
||||||
value->set(array);
|
|
||||||
|
|
||||||
return array;
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonObjectImpl *JsonArrayImpl::createNestedObject() {
|
|
||||||
JsonValueImpl *value = add();
|
|
||||||
if (!value) return NULL;
|
|
||||||
|
|
||||||
JsonObjectImpl *array = JsonObjectImpl::createFrom(_buffer);
|
|
||||||
value->set(array);
|
|
||||||
|
|
||||||
return array;
|
|
||||||
}
|
|
||||||
|
|
||||||
void JsonArrayImpl::writeTo(JsonWriter &writer) const {
|
|
||||||
JsonArrayNode *child = _firstNode;
|
|
||||||
|
|
||||||
if (child) {
|
|
||||||
writer.beginArray();
|
|
||||||
|
|
||||||
for (;;) {
|
|
||||||
child->value.writeTo(writer);
|
|
||||||
|
|
||||||
child = child->next;
|
|
||||||
if (!child) break;
|
|
||||||
|
|
||||||
writer.writeComma();
|
|
||||||
}
|
|
||||||
|
|
||||||
writer.endArray();
|
|
||||||
} else {
|
|
||||||
writer.writeEmptyArray();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,116 +0,0 @@
|
|||||||
// 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/JsonWriter.hpp"
|
|
||||||
#include "ArduinoJson/Internals/StringBuilder.hpp"
|
|
||||||
|
|
||||||
using namespace ArduinoJson;
|
|
||||||
using namespace ArduinoJson::Internals;
|
|
||||||
|
|
||||||
JsonObjectImpl *JsonObjectImpl::createFrom(JsonBuffer *buffer) {
|
|
||||||
void *ptr = buffer->alloc(sizeof(JsonObjectImpl));
|
|
||||||
return ptr ? new (ptr) JsonObjectImpl(buffer) : NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
int JsonObjectImpl::size() const {
|
|
||||||
int nodeCount = 0;
|
|
||||||
for (JsonObjectNode *node = _firstNode; node; node = node->next) nodeCount++;
|
|
||||||
return nodeCount;
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonValueImpl *JsonObjectImpl::operator[](const char *key) {
|
|
||||||
JsonObjectNode *node = getOrCreateNodeAt(key);
|
|
||||||
return node ? &node->value : NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
void JsonObjectImpl::remove(char const *key) { removeNode(getNodeAt(key)); }
|
|
||||||
|
|
||||||
JsonArrayImpl *JsonObjectImpl::createNestedArray(char const *key) {
|
|
||||||
JsonObjectNode *node = getOrCreateNodeAt(key);
|
|
||||||
if (!node) return NULL;
|
|
||||||
|
|
||||||
JsonArrayImpl *array = JsonArrayImpl::createFrom(_buffer);
|
|
||||||
node->value.set(array);
|
|
||||||
|
|
||||||
return array;
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonObjectImpl *JsonObjectImpl::createNestedObject(const char *key) {
|
|
||||||
JsonObjectNode *node = getOrCreateNodeAt(key);
|
|
||||||
if (!node) return NULL;
|
|
||||||
|
|
||||||
JsonObjectImpl *object = JsonObjectImpl::createFrom(_buffer);
|
|
||||||
node->value.set(object);
|
|
||||||
|
|
||||||
return object;
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonObjectNode *JsonObjectImpl::getNodeAt(const char *key) {
|
|
||||||
for (JsonObjectNode *node = _firstNode; node; node = node->next) {
|
|
||||||
if (!strcmp(node->key, key)) return node;
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonObjectNode *JsonObjectImpl::getOrCreateNodeAt(const char *key) {
|
|
||||||
JsonObjectNode *existingNode = getNodeAt(key);
|
|
||||||
if (existingNode) return existingNode;
|
|
||||||
|
|
||||||
JsonObjectNode *newNode = JsonObjectNode::createFrom(_buffer, key);
|
|
||||||
|
|
||||||
if (newNode) addNode(newNode);
|
|
||||||
|
|
||||||
return newNode;
|
|
||||||
}
|
|
||||||
|
|
||||||
void JsonObjectImpl::addNode(JsonObjectNode *nodeToAdd) {
|
|
||||||
if (!_firstNode) {
|
|
||||||
_firstNode = nodeToAdd;
|
|
||||||
} else {
|
|
||||||
JsonObjectNode *lastNode = _firstNode;
|
|
||||||
while (lastNode->next) lastNode = lastNode->next;
|
|
||||||
lastNode->next = nodeToAdd;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void JsonObjectImpl::removeNode(JsonObjectNode *nodeToRemove) {
|
|
||||||
if (nodeToRemove == _firstNode) {
|
|
||||||
_firstNode = nodeToRemove->next;
|
|
||||||
} else {
|
|
||||||
for (JsonObjectNode *node = _firstNode; node; node = node->next)
|
|
||||||
if (node->next == nodeToRemove) node->next = nodeToRemove->next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void JsonObjectImpl::writeTo(JsonWriter &writer) const {
|
|
||||||
JsonObjectNode *node = _firstNode;
|
|
||||||
|
|
||||||
if (node) {
|
|
||||||
writer.beginObject();
|
|
||||||
|
|
||||||
for (;;) {
|
|
||||||
writer.writeString(node->key);
|
|
||||||
writer.writeColon();
|
|
||||||
node->value.writeTo(writer);
|
|
||||||
|
|
||||||
node = node->next;
|
|
||||||
if (!node) break;
|
|
||||||
|
|
||||||
writer.writeComma();
|
|
||||||
}
|
|
||||||
|
|
||||||
writer.endObject();
|
|
||||||
} else {
|
|
||||||
writer.writeEmptyObject();
|
|
||||||
}
|
|
||||||
}
|
|
@ -30,7 +30,7 @@ bool JsonParser::skip(char charToSkip) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void JsonParser::parseValueTo(JsonValue destination) {
|
void JsonParser::parseAnythingTo(JsonValue &destination) {
|
||||||
skipSpaces();
|
skipSpaces();
|
||||||
|
|
||||||
switch (*_ptr) {
|
switch (*_ptr) {
|
||||||
@ -73,23 +73,23 @@ void JsonParser::parseValueTo(JsonValue destination) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonArray JsonParser::parseArray() {
|
JsonArray &JsonParser::parseArray() {
|
||||||
skip('[');
|
skip('[');
|
||||||
|
|
||||||
if (isEnd()) return NULL;
|
if (isEnd()) return JsonArray::invalid();
|
||||||
|
|
||||||
JsonArray array = _buffer->createArray();
|
JsonArray &array = _buffer->createArray();
|
||||||
if (skip(']')) return array; // empty array
|
if (skip(']')) return array; // empty array
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
JsonValue child = array.add();
|
JsonValue &child = array.add();
|
||||||
|
|
||||||
parseValueTo(child);
|
parseAnythingTo(child);
|
||||||
if (!child.success()) return NULL;
|
if (!child.success()) return JsonArray::invalid(); // child parsing failed
|
||||||
|
|
||||||
if (skip(']')) return array; // end of the array
|
if (skip(']')) return array; // end of the array
|
||||||
|
|
||||||
if (!skip(',')) return NULL; // comma is missing
|
if (!skip(',')) return JsonArray::invalid(); // comma is missing
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,38 +126,34 @@ void JsonParser::parseNullTo(JsonValue &destination) {
|
|||||||
destination = static_cast<const char *>(NULL);
|
destination = static_cast<const char *>(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonObject JsonParser::parseObject() {
|
JsonObject &JsonParser::parseObject() {
|
||||||
skip('{');
|
skip('{');
|
||||||
|
|
||||||
if (isEnd()) return NULL; // premature ending
|
if (isEnd()) return JsonObject::invalid(); // premature ending
|
||||||
|
|
||||||
JsonObject object = _buffer->createObject();
|
JsonObject &object = _buffer->createObject();
|
||||||
|
|
||||||
if (skip('}')) return object; // empty object
|
if (skip('}')) return object; // empty object
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
const char *key = parseString();
|
const char *key = parseString();
|
||||||
if (!key) return NULL;
|
if (!key) break; // key parsing failed
|
||||||
|
|
||||||
if (!skip(':')) return NULL;
|
if (!skip(':')) break; // colon is missing
|
||||||
|
|
||||||
JsonValue value = object[key];
|
JsonValue &value = object[key];
|
||||||
|
|
||||||
parseValueTo(value);
|
parseAnythingTo(value);
|
||||||
if (!value.success()) return NULL;
|
if (!value.success()) break; // value parsing failed
|
||||||
|
|
||||||
if (skip('}')) return object; // end of the object
|
if (skip('}')) return object; // end of the object
|
||||||
|
|
||||||
if (!skip(',')) return 0; // comma is missing
|
if (!skip(',')) break; // comma is missing
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return JsonObject::invalid();
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *JsonParser::parseString() {
|
const char *JsonParser::parseString() {
|
||||||
return QuotedString::extractFrom(_ptr, &_ptr);
|
return QuotedString::extractFrom(_ptr, &_ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonValue JsonParser::parseValue() {
|
|
||||||
JsonValue value = _buffer->createValue();
|
|
||||||
parseValueTo(value);
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
@ -1,46 +0,0 @@
|
|||||||
// Copyright Benoit Blanchon 2014
|
|
||||||
// MIT License
|
|
||||||
//
|
|
||||||
// Arduino JSON library
|
|
||||||
// https://github.com/bblanchon/ArduinoJson
|
|
||||||
|
|
||||||
#include "ArduinoJson/Internals/JsonValueImpl.hpp"
|
|
||||||
#include "ArduinoJson/Internals/JsonArrayImpl.hpp"
|
|
||||||
#include "ArduinoJson/Internals/JsonObjectImpl.hpp"
|
|
||||||
#include "ArduinoJson/Internals/JsonWriter.hpp"
|
|
||||||
|
|
||||||
using namespace ArduinoJson;
|
|
||||||
using namespace ArduinoJson::Internals;
|
|
||||||
|
|
||||||
JsonValueImpl* JsonValueImpl::createFrom(JsonBuffer* buffer) {
|
|
||||||
void* ptr = buffer->alloc(sizeof(JsonValueImpl));
|
|
||||||
return ptr ? new (ptr) JsonValueImpl() : NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
@ -5,22 +5,76 @@
|
|||||||
// https://github.com/bblanchon/ArduinoJson
|
// https://github.com/bblanchon/ArduinoJson
|
||||||
|
|
||||||
#include "ArduinoJson/JsonArray.hpp"
|
#include "ArduinoJson/JsonArray.hpp"
|
||||||
|
|
||||||
|
#include "ArduinoJson/JsonBuffer.hpp"
|
||||||
#include "ArduinoJson/JsonObject.hpp"
|
#include "ArduinoJson/JsonObject.hpp"
|
||||||
#include "ArduinoJson/JsonValue.hpp"
|
#include "ArduinoJson/Internals/JsonWriter.hpp"
|
||||||
|
|
||||||
using namespace ArduinoJson;
|
using namespace ArduinoJson;
|
||||||
using namespace ArduinoJson::Internals;
|
using namespace ArduinoJson::Internals;
|
||||||
|
|
||||||
JsonValue JsonArray::add() { return JsonValue(_impl ? _impl->add() : NULL); }
|
int JsonArray::size() const {
|
||||||
|
int nodeCount = 0;
|
||||||
JsonValue JsonArray::operator[](int index) const {
|
for (JsonArrayNode *node = _firstNode; node; node = node->next) nodeCount++;
|
||||||
return JsonValue(_impl ? (*_impl)[index] : NULL);
|
return nodeCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonArray JsonArray::createNestedArray() {
|
JsonValue &JsonArray::operator[](int index) const {
|
||||||
return JsonArray(_impl ? _impl->createNestedArray() : NULL);
|
JsonArrayNode *node = _firstNode;
|
||||||
|
while (node && index--) node = node->next;
|
||||||
|
return node ? node->value : JsonValue::invalid();
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonObject JsonArray::createNestedObject() {
|
JsonValue &JsonArray::add() {
|
||||||
return JsonObject(_impl ? _impl->createNestedObject() : NULL);
|
JsonArrayNode *node = JsonArrayNode::createFrom(_buffer);
|
||||||
}
|
if (!node) return JsonValue::invalid();
|
||||||
|
|
||||||
|
addNode(node);
|
||||||
|
|
||||||
|
return node->value;
|
||||||
|
}
|
||||||
|
|
||||||
|
void JsonArray::addNode(JsonArrayNode *newNode) {
|
||||||
|
if (_firstNode) {
|
||||||
|
JsonArrayNode *lastNode = _firstNode;
|
||||||
|
while (lastNode->next) lastNode = lastNode->next;
|
||||||
|
lastNode->next = newNode;
|
||||||
|
} else {
|
||||||
|
_firstNode = newNode;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonArray &JsonArray::createNestedArray() {
|
||||||
|
if (!_buffer) return JsonArray::invalid();
|
||||||
|
JsonArray &array = _buffer->createArray();
|
||||||
|
add(array);
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonObject &JsonArray::createNestedObject() {
|
||||||
|
if (!_buffer) return JsonObject::invalid();
|
||||||
|
JsonObject &object = _buffer->createObject();
|
||||||
|
add(object);
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
|
||||||
|
void JsonArray::writeTo(JsonWriter &writer) const {
|
||||||
|
JsonArrayNode *child = _firstNode;
|
||||||
|
|
||||||
|
if (child) {
|
||||||
|
writer.beginArray();
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
child->value.writeTo(writer);
|
||||||
|
|
||||||
|
child = child->next;
|
||||||
|
if (!child) break;
|
||||||
|
|
||||||
|
writer.writeComma();
|
||||||
|
}
|
||||||
|
|
||||||
|
writer.endArray();
|
||||||
|
} else {
|
||||||
|
writer.writeEmptyArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -7,39 +7,31 @@
|
|||||||
#include "ArduinoJson/JsonBuffer.hpp"
|
#include "ArduinoJson/JsonBuffer.hpp"
|
||||||
|
|
||||||
#include "ArduinoJson/JsonArray.hpp"
|
#include "ArduinoJson/JsonArray.hpp"
|
||||||
#include "ArduinoJson/Internals/JsonArrayImpl.hpp"
|
|
||||||
#include "ArduinoJson/JsonObject.hpp"
|
#include "ArduinoJson/JsonObject.hpp"
|
||||||
#include "ArduinoJson/Internals/JsonObjectImpl.hpp"
|
|
||||||
#include "ArduinoJson/JsonValue.hpp"
|
#include "ArduinoJson/JsonValue.hpp"
|
||||||
#include "ArduinoJson/Internals/JsonValueImpl.hpp"
|
|
||||||
#include "ArduinoJson/Internals/JsonParser.hpp"
|
#include "ArduinoJson/Internals/JsonParser.hpp"
|
||||||
|
|
||||||
using namespace ArduinoJson;
|
using namespace ArduinoJson;
|
||||||
using namespace ArduinoJson::Internals;
|
using namespace ArduinoJson::Internals;
|
||||||
|
|
||||||
JsonArray JsonBuffer::createArray() {
|
JsonArray &JsonBuffer::createArray() {
|
||||||
return JsonArray(JsonArrayImpl::createFrom(this));
|
void *ptr = alloc(sizeof(JsonArray));
|
||||||
|
if (ptr) return *new (ptr) JsonArray(this);
|
||||||
|
return JsonArray::invalid();
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonObject JsonBuffer::createObject() {
|
JsonObject &JsonBuffer::createObject() {
|
||||||
return JsonObject(JsonObjectImpl::createFrom(this));
|
void *ptr = alloc(sizeof(JsonObject));
|
||||||
|
if (ptr) return *new (ptr) JsonObject(this);
|
||||||
|
return JsonObject::invalid();
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonValue JsonBuffer::createValue() {
|
JsonArray &JsonBuffer::parseArray(char *json) {
|
||||||
return JsonValue(JsonValueImpl::createFrom(this));
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonArray JsonBuffer::parseArray(char* json) {
|
|
||||||
JsonParser parser(this, json);
|
JsonParser parser(this, json);
|
||||||
return JsonArray(parser.parseArray());
|
return parser.parseArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonObject JsonBuffer::parseObject(char* json) {
|
JsonObject &JsonBuffer::parseObject(char *json) {
|
||||||
JsonParser parser(this, json);
|
JsonParser parser(this, json);
|
||||||
return JsonObject(parser.parseObject());
|
return parser.parseObject();
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonValue JsonBuffer::parseValue(char* json) {
|
|
||||||
JsonParser parser(this, json);
|
|
||||||
return JsonValue(parser.parseValue());
|
|
||||||
}
|
|
@ -4,20 +4,102 @@
|
|||||||
// Arduino JSON library
|
// Arduino JSON library
|
||||||
// https://github.com/bblanchon/ArduinoJson
|
// https://github.com/bblanchon/ArduinoJson
|
||||||
|
|
||||||
#include "ArduinoJson/JsonArray.hpp"
|
|
||||||
#include "ArduinoJson/JsonObject.hpp"
|
#include "ArduinoJson/JsonObject.hpp"
|
||||||
|
|
||||||
|
#include <string.h> // for strcmp
|
||||||
|
|
||||||
|
#include "ArduinoJson/JsonBuffer.hpp"
|
||||||
|
#include "ArduinoJson/JsonArray.hpp"
|
||||||
#include "ArduinoJson/JsonValue.hpp"
|
#include "ArduinoJson/JsonValue.hpp"
|
||||||
|
#include "ArduinoJson/Internals/JsonWriter.hpp"
|
||||||
|
#include "ArduinoJson/Internals/StringBuilder.hpp"
|
||||||
|
|
||||||
using namespace ArduinoJson;
|
using namespace ArduinoJson;
|
||||||
|
using namespace ArduinoJson::Internals;
|
||||||
|
|
||||||
JsonValue JsonObject::operator[](char const *key) {
|
int JsonObject::size() const {
|
||||||
return JsonValue(_impl ? (*_impl)[key] : NULL);
|
int nodeCount = 0;
|
||||||
|
for (JsonObjectNode *node = _firstNode; node; node = node->next) nodeCount++;
|
||||||
|
return nodeCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonArray JsonObject::createNestedArray(key_type key) {
|
JsonValue &JsonObject::operator[](const char *key) {
|
||||||
return JsonArray(_impl ? _impl->createNestedArray(key) : NULL);
|
JsonObjectNode *node = getOrCreateNodeAt(key);
|
||||||
|
return node ? node->pair.value : JsonValue::invalid();
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonObject JsonObject::createNestedObject(key_type key) {
|
void JsonObject::remove(char const *key) { removeNode(getNodeAt(key)); }
|
||||||
return JsonObject(_impl ? _impl->createNestedObject(key) : NULL);
|
|
||||||
|
JsonArray &JsonObject::createNestedArray(char const *key) {
|
||||||
|
if (!_buffer) return JsonArray::invalid();
|
||||||
|
JsonArray &array = _buffer->createArray();
|
||||||
|
add(key, array);
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonObject &JsonObject::createNestedObject(const char *key) {
|
||||||
|
if (!_buffer) return JsonObject::invalid();
|
||||||
|
JsonObject &object = _buffer->createObject();
|
||||||
|
add(key, object);
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonObjectNode *JsonObject::getNodeAt(const char *key) {
|
||||||
|
for (JsonObjectNode *node = _firstNode; node; node = node->next) {
|
||||||
|
if (!strcmp(node->pair.key, key)) return node;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonObjectNode *JsonObject::getOrCreateNodeAt(const char *key) {
|
||||||
|
JsonObjectNode *existingNode = getNodeAt(key);
|
||||||
|
if (existingNode) return existingNode;
|
||||||
|
|
||||||
|
JsonObjectNode *newNode = JsonObjectNode::createFrom(_buffer, key);
|
||||||
|
|
||||||
|
if (newNode) addNode(newNode);
|
||||||
|
|
||||||
|
return newNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
void JsonObject::addNode(JsonObjectNode *nodeToAdd) {
|
||||||
|
if (!_firstNode) {
|
||||||
|
_firstNode = nodeToAdd;
|
||||||
|
} else {
|
||||||
|
JsonObjectNode *lastNode = _firstNode;
|
||||||
|
while (lastNode->next) lastNode = lastNode->next;
|
||||||
|
lastNode->next = nodeToAdd;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void JsonObject::removeNode(JsonObjectNode *nodeToRemove) {
|
||||||
|
if (nodeToRemove == _firstNode) {
|
||||||
|
_firstNode = nodeToRemove->next;
|
||||||
|
} else {
|
||||||
|
for (JsonObjectNode *node = _firstNode; node; node = node->next)
|
||||||
|
if (node->next == nodeToRemove) node->next = nodeToRemove->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void JsonObject::writeTo(JsonWriter &writer) const {
|
||||||
|
JsonObjectNode *node = _firstNode;
|
||||||
|
|
||||||
|
if (node) {
|
||||||
|
writer.beginObject();
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
writer.writeString(node->pair.key);
|
||||||
|
writer.writeColon();
|
||||||
|
node->pair.value.writeTo(writer);
|
||||||
|
|
||||||
|
node = node->next;
|
||||||
|
if (!node) break;
|
||||||
|
|
||||||
|
writer.writeComma();
|
||||||
|
}
|
||||||
|
|
||||||
|
writer.endObject();
|
||||||
|
} else {
|
||||||
|
writer.writeEmptyObject();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,24 +4,98 @@
|
|||||||
// Arduino JSON library
|
// Arduino JSON library
|
||||||
// https://github.com/bblanchon/ArduinoJson
|
// https://github.com/bblanchon/ArduinoJson
|
||||||
|
|
||||||
|
#include "ArduinoJson/JsonValue.hpp"
|
||||||
#include "ArduinoJson/JsonArray.hpp"
|
#include "ArduinoJson/JsonArray.hpp"
|
||||||
#include "ArduinoJson/JsonObject.hpp"
|
#include "ArduinoJson/JsonObject.hpp"
|
||||||
#include "ArduinoJson/JsonValue.hpp"
|
#include "ArduinoJson/Internals/JsonWriter.hpp"
|
||||||
|
|
||||||
using namespace ArduinoJson;
|
using namespace ArduinoJson;
|
||||||
|
using namespace ArduinoJson::Internals;
|
||||||
|
|
||||||
JsonValue::operator JsonArray() {
|
JsonArray &JsonValue::asArray() {
|
||||||
return JsonArray(_impl ? _impl->asArray() : NULL);
|
return _type == JSON_ARRAY ? *_content.asArray : JsonArray::invalid();
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonValue::operator JsonObject() {
|
JsonObject &JsonValue::asObject() {
|
||||||
return JsonObject(_impl ? _impl->asObject() : NULL);
|
return _type == JSON_OBJECT ? *_content.asObject : JsonObject::invalid();
|
||||||
}
|
}
|
||||||
|
|
||||||
void JsonValue::set(JsonArray array) {
|
bool JsonValue::asBool() const {
|
||||||
if (_impl) _impl->set(array._impl);
|
return _type == JSON_BOOLEAN ? _content.asBoolean : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void JsonValue::set(JsonObject object) {
|
const char *JsonValue::asString() const {
|
||||||
if (_impl) _impl->set(object._impl);
|
return _type == JSON_STRING ? _content.asString : NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
double JsonValue::asDouble() const {
|
||||||
|
return _type >= JSON_DOUBLE_0_DECIMALS ? _content.asDouble : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
long JsonValue::asLong() const {
|
||||||
|
return _type == JSON_LONG ? _content.asInteger : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void JsonValue::set(bool value) {
|
||||||
|
if (_type == JSON_INVALID) return;
|
||||||
|
_type = Internals::JSON_BOOLEAN;
|
||||||
|
_content.asBoolean = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
void JsonValue::set(const char *value) {
|
||||||
|
if (_type == JSON_INVALID) return;
|
||||||
|
_type = JSON_STRING;
|
||||||
|
_content.asString = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
void JsonValue::set(double value, int decimals) {
|
||||||
|
if (_type == JSON_INVALID) return;
|
||||||
|
_type = static_cast<JsonValueType>(JSON_DOUBLE_0_DECIMALS + decimals);
|
||||||
|
_content.asDouble = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
void JsonValue::set(long value) {
|
||||||
|
if (_type == JSON_INVALID) return;
|
||||||
|
_type = JSON_LONG;
|
||||||
|
_content.asInteger = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
void JsonValue::set(JsonArray &array) {
|
||||||
|
if (_type == JSON_INVALID) return;
|
||||||
|
_type = JSON_ARRAY;
|
||||||
|
_content.asArray = &array;
|
||||||
|
}
|
||||||
|
|
||||||
|
void JsonValue::set(JsonObject &object) {
|
||||||
|
if (_type == JSON_INVALID) return;
|
||||||
|
_type = JSON_OBJECT;
|
||||||
|
_content.asObject = &object;
|
||||||
|
}
|
||||||
|
|
||||||
|
void JsonValue::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;
|
||||||
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user