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,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();
}
}

View File

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

View File

@ -30,7 +30,7 @@ bool JsonParser::skip(char charToSkip) {
return true;
}
void JsonParser::parseValueTo(JsonValue destination) {
void JsonParser::parseAnythingTo(JsonValue &destination) {
skipSpaces();
switch (*_ptr) {
@ -73,23 +73,23 @@ void JsonParser::parseValueTo(JsonValue destination) {
}
}
JsonArray JsonParser::parseArray() {
JsonArray &JsonParser::parseArray() {
skip('[');
if (isEnd()) return NULL;
if (isEnd()) return JsonArray::invalid();
JsonArray array = _buffer->createArray();
JsonArray &array = _buffer->createArray();
if (skip(']')) return array; // empty array
for (;;) {
JsonValue child = array.add();
JsonValue &child = array.add();
parseValueTo(child);
if (!child.success()) return NULL;
parseAnythingTo(child);
if (!child.success()) return JsonArray::invalid(); // child parsing failed
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);
}
JsonObject JsonParser::parseObject() {
JsonObject &JsonParser::parseObject() {
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
for (;;) {
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);
if (!value.success()) return NULL;
parseAnythingTo(value);
if (!value.success()) break; // value parsing failed
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() {
return QuotedString::extractFrom(_ptr, &_ptr);
}
JsonValue JsonParser::parseValue() {
JsonValue value = _buffer->createValue();
parseValueTo(value);
return value;
}

View File

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