Huge refactoring in progress...

This commit is contained in:
Benoit Blanchon
2014-10-29 14:24:34 +01:00
parent 10ab95522d
commit ba2b142c8a
27 changed files with 408 additions and 743 deletions

View File

@ -10,23 +10,20 @@
namespace ArduinoJson {
class JsonArray;
class JsonArrayConstIterator;
class JsonArrayIterator;
class JsonBuffer;
class JsonPair;
class JsonValue;
class JsonObject;
class JsonObjectConstIterator;
class JsonObjectIterator;
struct JsonPair;
class JsonValue;
namespace Internals {
class IndentedPrint;
class JsonArrayConstIterator;
class JsonArrayImpl;
class JsonArrayIterator;
class JsonArrayNode;
class JsonObjectConstIterator;
class JsonObjectImpl;
class JsonObjectIterator;
class JsonObjectNode;
class JsonParser;
class JsonValueImpl;
class JsonWriter;
}
}

View File

@ -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;
};
}
}

View File

@ -6,7 +6,7 @@
#pragma once
#include "JsonValueImpl.hpp"
#include "../JsonValue.hpp"
#include "../JsonBuffer.hpp"
namespace ArduinoJson {
@ -20,7 +20,7 @@ class JsonArrayNode {
}
JsonArrayNode* next;
JsonValueImpl value;
JsonValue value;
private:
JsonArrayNode() : next(0) {}

View File

@ -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;
};
}
}

View File

@ -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;
};
}
}

View File

@ -6,7 +6,7 @@
#pragma once
#include "JsonValueImpl.hpp"
#include "../JsonPair.hpp"
#include "../JsonBuffer.hpp"
namespace ArduinoJson {
@ -19,12 +19,11 @@ class JsonObjectNode {
return ptr ? new (ptr) JsonObjectNode(key) : NULL;
}
const char* const key;
JsonValueImpl value;
JsonPair pair;
JsonObjectNode* next;
private:
JsonObjectNode(const char* k) : key(k), next(NULL) {}
JsonObjectNode(const char* k) : pair(k), next(NULL) {}
};
}
}

View File

@ -16,9 +16,8 @@ class JsonParser {
public:
JsonParser(JsonBuffer *buffer, char *json) : _buffer(buffer), _ptr(json) {}
JsonArray parseArray();
JsonObject parseObject();
JsonValue parseValue();
JsonArray &parseArray();
JsonObject &parseObject();
private:
bool isEnd() { return *_ptr == 0; }
@ -26,7 +25,7 @@ class JsonParser {
bool skip(char charToSkip);
void skipSpaces();
void parseValueTo(JsonValue);
void parseAnythingTo(JsonValue &destination);
inline void parseBooleanTo(JsonValue &destination);
inline void parseNullTo(JsonValue &destination);
inline void parseNumberTo(JsonValue &destination);

View File

@ -16,8 +16,8 @@ union JsonValueContent {
double asDouble;
long asInteger;
const char* asString;
JsonArrayImpl* asArray;
JsonObjectImpl* asObject;
JsonArray* asArray;
JsonObject* asObject;
};
}
}

View File

@ -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;
};
}
}

View File

@ -11,6 +11,7 @@ namespace Internals {
enum JsonValueType {
JSON_UNDEFINED,
JSON_INVALID,
JSON_ARRAY,
JSON_OBJECT,
JSON_BOOLEAN,

View File

@ -6,58 +6,48 @@
#pragma once
#include "ForwardDeclarations.hpp"
#include "JsonArrayIterator.hpp"
#include "JsonArrayConstIterator.hpp"
#include "JsonPrintable.hpp"
#include "Internals/JsonArrayImpl.hpp"
namespace ArduinoJson {
class JsonArray : public JsonPrintable {
friend class JsonValue;
public:
typedef JsonValue value_type;
typedef Internals::JsonArrayIterator iterator;
typedef Internals::JsonArrayConstIterator const_iterator;
typedef JsonArrayIterator iterator;
typedef JsonArrayConstIterator const_iterator;
JsonArray() : _impl(NULL) {}
JsonArray(Internals::JsonArrayImpl* impl) : _impl(impl) {}
JsonArray(JsonBuffer *buffer = NULL) : _buffer(buffer), _firstNode(NULL) {}
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>
void add(T value) {
add().set(value);
}
void add(double value, int decimals = 2) { add().set(value, decimals); }
JsonArray &createNestedArray();
JsonObject &createNestedObject();
JsonArray createNestedArray();
JsonObject createNestedObject();
iterator begin() {
if (!_impl) return end();
return _impl->begin();
}
iterator begin() { return iterator(_firstNode); }
iterator end() { return iterator(0); }
const_iterator begin() const {
if (!_impl) return end();
return const_cast<const Internals::JsonArrayImpl*>(_impl)->begin();
}
const_iterator begin() const { return const_iterator(_firstNode); }
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 {
if (_impl) _impl->writeTo(writer);
}
virtual void writeTo(Internals::JsonWriter &writer) const;
private:
Internals::JsonArrayImpl* _impl;
inline void addNode(Internals::JsonArrayNode *node);
JsonBuffer *_buffer;
Internals::JsonArrayNode *_firstNode;
static JsonArray _invalid;
};
}

View File

@ -6,18 +6,17 @@
#pragma once
#include "JsonArrayNode.hpp"
#include "Internals/JsonArrayNode.hpp"
namespace ArduinoJson {
namespace Internals {
// TODO: copy from JsonArrayIterator
class JsonArrayConstIterator {
public:
explicit JsonArrayConstIterator(JsonArrayNode *node) : _node(node) {}
explicit JsonArrayConstIterator(Internals::JsonArrayNode *node)
: _node(node) {}
const JsonValueImpl &operator*() const { return _node->value; }
const JsonValueImpl *operator->() { return &_node->value; }
const JsonValue &operator*() const { return _node->value; }
const JsonValue *operator->() { return &_node->value; }
bool operator==(const JsonArrayConstIterator &other) const {
return _node == other._node;
@ -28,12 +27,11 @@ class JsonArrayConstIterator {
}
JsonArrayConstIterator &operator++() {
_node = _node->next;
if (_node) _node = _node->next;
return *this;
}
private:
JsonArrayNode *_node;
Internals::JsonArrayNode *_node;
};
}
}
}

View File

@ -6,19 +6,16 @@
#pragma once
#include "JsonArrayNode.hpp"
#include "Internals/JsonArrayNode.hpp"
namespace ArduinoJson {
namespace Internals {
class JsonArrayIterator {
public:
explicit JsonArrayIterator(Internals::JsonArrayNode *node) : _node(node) {
updateValue();
}
explicit JsonArrayIterator(Internals::JsonArrayNode *node) : _node(node) {}
JsonValue operator*() const { return _value; }
JsonValue *operator->() { return &_value; }
JsonValue &operator*() const { return _node->value; }
JsonValue *operator->() { return &_node->value; }
bool operator==(const JsonArrayIterator &other) const {
return _node == other._node;
@ -29,18 +26,11 @@ class JsonArrayIterator {
}
JsonArrayIterator &operator++() {
if (_node) {
_node = _node->next;
updateValue();
}
if (_node) _node = _node->next;
return *this;
}
private:
void updateValue() { _value = JsonValue(_node ? &_node->value : NULL); }
JsonArrayNode *_node;
JsonValue _value;
Internals::JsonArrayNode *_node;
};
}
}

View File

@ -17,21 +17,12 @@ class JsonBuffer {
public:
virtual ~JsonBuffer() {}
JsonArray createArray();
JsonObject createObject();
JsonValue createValue();
JsonArray &createArray();
JsonObject &createObject();
template <typename T>
JsonValue createValue(T x) {
JsonValue value;
value = x;
return value;
}
JsonArray &parseArray(char *json);
JsonObject &parseObject(char *json);
JsonArray parseArray(char* json);
JsonObject parseObject(char* json);
JsonValue parseValue(char* json);
virtual void* alloc(size_t size) = 0;
virtual void *alloc(size_t size) = 0;
};
}

View File

@ -6,56 +6,53 @@
#pragma once
#include "JsonObjectConstIterator.hpp"
#include "JsonObjectIterator.hpp"
#include "JsonPrintable.hpp"
#include "Internals/JsonObjectImpl.hpp"
#include "Internals/JsonObjectNode.hpp"
namespace ArduinoJson {
class JsonObject : public JsonPrintable {
friend class JsonValue;
public:
typedef const char* key_type;
typedef const char *key_type;
typedef JsonPair value_type;
typedef Internals::JsonObjectIterator iterator;
typedef Internals::JsonObjectConstIterator const_iterator;
typedef JsonObjectIterator iterator;
typedef JsonObjectConstIterator const_iterator;
JsonObject() : _impl(NULL) {}
JsonObject(Internals::JsonObjectImpl* impl) : _impl(impl) {}
JsonObject(JsonBuffer *buffer) : _buffer(buffer), _firstNode(NULL) {}
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);
void remove(key_type key) {
if (_impl) _impl->remove(key);
template <typename T>
void add(key_type key, T value) {
(*this)[key] = value;
}
JsonArray createNestedArray(key_type key);
JsonObject createNestedObject(key_type key);
JsonArray &createNestedArray(key_type key);
JsonObject &createNestedObject(key_type key);
iterator begin() {
if (!_impl) return end();
return _impl->begin();
}
iterator begin() { return iterator(_firstNode); }
iterator end() { return iterator(0); }
const_iterator begin() const {
if (!_impl) return end();
return const_cast<const Internals::JsonObjectImpl*>(_impl)->begin();
}
const_iterator begin() const { return const_iterator(_firstNode); }
const_iterator end() const { return const_iterator(0); }
bool operator==(const JsonObject& other) const {
return _impl == other._impl;
}
static JsonObject &invalid() { return _invalid; }
protected:
virtual void writeTo(Internals::JsonWriter& writer) const {
if (_impl) _impl->writeTo(writer);
}
virtual void writeTo(Internals::JsonWriter &writer) const;
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;
};
}

View File

@ -6,35 +6,33 @@
#pragma once
#include "../JsonPair.hpp"
#include "JsonObjectNode.hpp"
#include "ForwardDeclarations.hpp"
#include "Internals/JsonObjectNode.hpp"
namespace ArduinoJson {
namespace Internals {
class JsonObjectConstIterator {
public:
explicit JsonObjectConstIterator(Internals::JsonObjectNode *node)
: _pair(node) {}
: _node(node) {}
const JsonPair operator*() const { return _pair; }
const JsonPair *operator->() { return &_pair; }
const JsonPair &operator*() { return _node->pair; }
const JsonPair *operator->() { return &_node->pair; }
bool operator==(const JsonObjectConstIterator &other) const {
return _pair._node == other._pair._node;
return _node == other._node;
}
bool operator!=(const JsonObjectConstIterator &other) const {
return _pair._node != other._pair._node;
return _node != other._node;
}
JsonObjectConstIterator &operator++() {
_pair._node = _pair._node->next;
if (_node) _node = _node->next;
return *this;
}
private:
JsonPair _pair;
Internals::JsonObjectNode *_node;
};
}
}

View File

@ -1,33 +1,37 @@
// Copyright Benoit Blanchon 2014
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#pragma once
#include "ArduinoJson/JsonObjectKeyValue.hpp"
#include "ForwardDeclarations.hpp"
#include "Internals/JsonObjectNode.hpp"
namespace ArduinoJson {
class JsonObject;
class JsonObjectIterator {
friend class JsonObject;
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(); }
void operator++() { _node = _node->next; }
JsonObjectKeyValue operator*() const { return JsonObjectKeyValue(_node); }
bool operator==(const JsonObjectIterator& other) const {
bool operator==(const JsonObjectIterator &other) const {
return _node == other._node;
}
bool operator!=(const JsonObjectIterator& other) const {
bool operator!=(const JsonObjectIterator &other) const {
return _node != other._node;
}
JsonObjectIterator &operator++() {
if (_node) _node = _node->next;
return *this;
}
private:
Internals::JsonNode* _node;
Internals::JsonObjectNode *_node;
};
}
}

View File

@ -10,17 +10,10 @@
#include "Internals/JsonObjectNode.hpp"
namespace ArduinoJson {
class JsonPair {
friend class Internals::JsonObjectIterator;
friend class Internals::JsonObjectConstIterator;
struct JsonPair {
JsonPair(const char* k) : key(k) {}
public:
JsonPair(Internals::JsonObjectNode *node) : _node(node) {}
const char *key() const { return _node->key; }
JsonValue value() { return JsonValue(&_node->value); }
private:
Internals::JsonObjectNode *_node;
const char* const key;
JsonValue value;
};
}

View File

@ -9,62 +9,45 @@
#include <stddef.h>
#include "ForwardDeclarations.hpp"
#include "Internals/JsonValueImpl.hpp"
#include "Internals/JsonValueContent.hpp"
#include "Internals/JsonValueType.hpp"
namespace ArduinoJson {
class JsonValue {
public:
JsonValue() : _impl(NULL) {}
JsonValue(Internals::JsonValueImpl *impl) : _impl(impl) {}
JsonValue() : _type(Internals::JSON_UNDEFINED) {}
void set(const char *value) {
if (_impl) _impl->set(value);
}
void set(bool value) {
if (_impl) _impl->set(value);
}
void set(double value, int decimals = 2) {
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);
void set(bool value);
void set(const char *value);
void set(double value, int decimals = 2);
void set(int value) { set(static_cast<long>(value)); }
void set(long value);
void set(JsonArray &array);
void set(JsonObject &object);
operator bool() const { return _impl ? _impl->asBool() : false; }
operator int() const { return _impl ? _impl->asLong() : 0; }
operator long() const { return _impl ? _impl->asLong() : 0; }
operator double() const { return _impl ? _impl->asDouble() : 0.0; }
operator const char *() const {
return _impl ? _impl->asString() : static_cast<char *>(NULL);
}
operator JsonArray();
operator JsonObject();
bool success() { return _impl; }
JsonArray &asArray();
JsonObject &asObject();
bool asBool() const;
const char *asString() const;
double asDouble() const;
long asLong() const;
template <typename T>
JsonValue operator=(T value) {
JsonValue &operator=(T value) {
set(value);
return *this;
}
template <typename T>
T as() {
return static_cast<T>(*this);
}
static JsonValue &invalid() { return _invalid; }
protected:
virtual void writeTo(Internals::JsonWriter &writer) const {
if (_impl) _impl->writeTo(writer);
}
bool success() { return _type != Internals::JSON_INVALID; }
void writeTo(Internals::JsonWriter &writer) const;
private:
Internals::JsonValueImpl *_impl;
Internals::JsonValueType _type;
Internals::JsonValueContent _content;
static JsonValue _invalid;
};
}