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

@@ -1,39 +0,0 @@
// Copyright Benoit Blanchon 2014
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#pragma once
#include "JsonArrayNode.hpp"
namespace ArduinoJson {
namespace Internals {
// TODO: copy from JsonArrayIterator
class JsonArrayConstIterator {
public:
explicit JsonArrayConstIterator(JsonArrayNode *node) : _node(node) {}
const JsonValueImpl &operator*() const { return _node->value; }
const JsonValueImpl *operator->() { return &_node->value; }
bool operator==(const JsonArrayConstIterator &other) const {
return _node == other._node;
}
bool operator!=(const JsonArrayConstIterator &other) const {
return _node != other._node;
}
JsonArrayConstIterator &operator++() {
_node = _node->next;
return *this;
}
private:
JsonArrayNode *_node;
};
}
}

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

@@ -1,46 +0,0 @@
// Copyright Benoit Blanchon 2014
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#pragma once
#include "JsonArrayNode.hpp"
namespace ArduinoJson {
namespace Internals {
class JsonArrayIterator {
public:
explicit JsonArrayIterator(Internals::JsonArrayNode *node) : _node(node) {
updateValue();
}
JsonValue operator*() const { return _value; }
JsonValue *operator->() { return &_value; }
bool operator==(const JsonArrayIterator &other) const {
return _node == other._node;
}
bool operator!=(const JsonArrayIterator &other) const {
return _node != other._node;
}
JsonArrayIterator &operator++() {
if (_node) {
_node = _node->next;
updateValue();
}
return *this;
}
private:
void updateValue() { _value = JsonValue(_node ? &_node->value : NULL); }
JsonArrayNode *_node;
JsonValue _value;
};
}
}

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,40 +0,0 @@
// Copyright Benoit Blanchon 2014
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#pragma once
#include "../JsonPair.hpp"
#include "JsonObjectNode.hpp"
namespace ArduinoJson {
namespace Internals {
class JsonObjectConstIterator {
public:
explicit JsonObjectConstIterator(Internals::JsonObjectNode *node)
: _pair(node) {}
const JsonPair operator*() const { return _pair; }
const JsonPair *operator->() { return &_pair; }
bool operator==(const JsonObjectConstIterator &other) const {
return _pair._node == other._pair._node;
}
bool operator!=(const JsonObjectConstIterator &other) const {
return _pair._node != other._pair._node;
}
JsonObjectConstIterator &operator++() {
_pair._node = _pair._node->next;
return *this;
}
private:
JsonPair _pair;
};
}
}

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,