From 58d2c4a62f5c0fb47c3dd7383bdb88bbe3221946 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Thu, 16 Oct 2014 16:25:42 +0200 Subject: [PATCH] Renamed srcs/ into src/ --- CMakeLists.txt | 2 +- {srcs => src}/Arduino/Print.cpp | 78 ++--- {srcs => src}/CMakeLists.txt | 10 +- {srcs => src}/Internals/CompactJsonWriter.cpp | 2 +- {srcs => src}/Internals/EscapedString.cpp | 88 ++--- {srcs => src}/Internals/IndentedPrint.cpp | 88 ++--- {srcs => src}/Internals/JsonNode.cpp | 330 +++++++++--------- {srcs => src}/Internals/JsonParser.cpp | 0 {srcs => src}/Internals/JsonWriter.cpp | 48 +-- {srcs => src}/Internals/PrettyJsonWriter.cpp | 2 +- {srcs => src}/Internals/StringBuilder.cpp | 32 +- {srcs => src}/JsonArray.cpp | 170 ++++----- {srcs => src}/JsonBuffer.cpp | 170 ++++----- {srcs => src}/JsonContainer.cpp | 164 ++++----- {srcs => src}/JsonObject.cpp | 144 ++++---- {srcs => src}/JsonValue.cpp | 116 +++--- 16 files changed, 722 insertions(+), 722 deletions(-) rename {srcs => src}/Arduino/Print.cpp (93%) rename {srcs => src}/CMakeLists.txt (97%) rename {srcs => src}/Internals/CompactJsonWriter.cpp (98%) rename {srcs => src}/Internals/EscapedString.cpp (94%) rename {srcs => src}/Internals/IndentedPrint.cpp (93%) rename {srcs => src}/Internals/JsonNode.cpp (95%) rename {srcs => src}/Internals/JsonParser.cpp (100%) rename {srcs => src}/Internals/JsonWriter.cpp (95%) rename {srcs => src}/Internals/PrettyJsonWriter.cpp (98%) rename {srcs => src}/Internals/StringBuilder.cpp (95%) rename {srcs => src}/JsonArray.cpp (95%) rename {srcs => src}/JsonBuffer.cpp (94%) rename {srcs => src}/JsonContainer.cpp (95%) rename {srcs => src}/JsonObject.cpp (95%) rename {srcs => src}/JsonValue.cpp (94%) diff --git a/CMakeLists.txt b/CMakeLists.txt index d4e85e90..103d3a6f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,5 +3,5 @@ project(ArduinoJson) enable_testing() -add_subdirectory(srcs) +add_subdirectory(src) add_subdirectory(test) \ No newline at end of file diff --git a/srcs/Arduino/Print.cpp b/src/Arduino/Print.cpp similarity index 93% rename from srcs/Arduino/Print.cpp rename to src/Arduino/Print.cpp index 664178cc..87100cad 100644 --- a/srcs/Arduino/Print.cpp +++ b/src/Arduino/Print.cpp @@ -1,40 +1,40 @@ -/* - * Arduino JSON library - * Benoit Blanchon 2014 - MIT License - */ - -#ifndef ARDUINO - -#include "ArduinoJson/Arduino/Print.h" -#include - -size_t Print::print(const char s[]) -{ - size_t n = 0; - while (*s) - { - n += write(*s++); - } - return n; -} - -size_t Print::print(double value, int digits) -{ - char tmp[32]; - sprintf(tmp, "%.*lg", digits+1, value); - return print(tmp); -} - -size_t Print::print(long value) -{ - char tmp[32]; - sprintf(tmp, "%ld", value); - return print(tmp); -} - -size_t Print::println() -{ - return write('\r') + write('\n'); -} - +/* + * Arduino JSON library + * Benoit Blanchon 2014 - MIT License + */ + +#ifndef ARDUINO + +#include "ArduinoJson/Arduino/Print.h" +#include + +size_t Print::print(const char s[]) +{ + size_t n = 0; + while (*s) + { + n += write(*s++); + } + return n; +} + +size_t Print::print(double value, int digits) +{ + char tmp[32]; + sprintf(tmp, "%.*lg", digits+1, value); + return print(tmp); +} + +size_t Print::print(long value) +{ + char tmp[32]; + sprintf(tmp, "%ld", value); + return print(tmp); +} + +size_t Print::println() +{ + return write('\r') + write('\n'); +} + #endif \ No newline at end of file diff --git a/srcs/CMakeLists.txt b/src/CMakeLists.txt similarity index 97% rename from srcs/CMakeLists.txt rename to src/CMakeLists.txt index 36b669f1..3a140591 100644 --- a/srcs/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,6 +1,6 @@ -file(GLOB_RECURSE INC_FILES ../include/*.h) -file(GLOB_RECURSE SRC_FILES *.cpp) - -include_directories(../include) - +file(GLOB_RECURSE INC_FILES ../include/*.h) +file(GLOB_RECURSE SRC_FILES *.cpp) + +include_directories(../include) + add_library(ArduinoJson ${SRC_FILES} ${INC_FILES}) \ No newline at end of file diff --git a/srcs/Internals/CompactJsonWriter.cpp b/src/Internals/CompactJsonWriter.cpp similarity index 98% rename from srcs/Internals/CompactJsonWriter.cpp rename to src/Internals/CompactJsonWriter.cpp index a0f68a4d..40fc3969 100644 --- a/srcs/Internals/CompactJsonWriter.cpp +++ b/src/Internals/CompactJsonWriter.cpp @@ -1 +1 @@ -#include "ArduinoJson/Internals/CompactJsonWriter.h" +#include "ArduinoJson/Internals/CompactJsonWriter.h" diff --git a/srcs/Internals/EscapedString.cpp b/src/Internals/EscapedString.cpp similarity index 94% rename from srcs/Internals/EscapedString.cpp rename to src/Internals/EscapedString.cpp index e4bc4231..ad150385 100644 --- a/srcs/Internals/EscapedString.cpp +++ b/src/Internals/EscapedString.cpp @@ -1,45 +1,45 @@ -/* - * Arduino JSON library - * Benoit Blanchon 2014 - MIT License - */ - -#include "ArduinoJson/Internals/EscapedString.h" - -using namespace ArduinoJson::Internals; - -static inline char getSpecialChar(char c) -{ - // Optimized for code size on a 8-bit AVR - - const char* p = "\"\"\\\\\bb\ff\nn\rr\tt\0"; - - while (p[0] && p[0] != c) - { - p += 2; - } - - return p[1]; -} - -static inline size_t printCharTo(char c, Print* p) -{ - char specialChar = getSpecialChar(c); - - return specialChar != 0 - ? p->write('\\') + p->write(specialChar) - : p->write(c); -} - -size_t EscapedString::printTo(const char* s, Print* p) -{ - if (!s) return p->print("null"); - - size_t n = p->write('\"'); - - while (*s) - { - n += printCharTo(*s++, p); - } - - return n + p->write('\"'); +/* + * Arduino JSON library + * Benoit Blanchon 2014 - MIT License + */ + +#include "ArduinoJson/Internals/EscapedString.h" + +using namespace ArduinoJson::Internals; + +static inline char getSpecialChar(char c) +{ + // Optimized for code size on a 8-bit AVR + + const char* p = "\"\"\\\\\bb\ff\nn\rr\tt\0"; + + while (p[0] && p[0] != c) + { + p += 2; + } + + return p[1]; +} + +static inline size_t printCharTo(char c, Print* p) +{ + char specialChar = getSpecialChar(c); + + return specialChar != 0 + ? p->write('\\') + p->write(specialChar) + : p->write(c); +} + +size_t EscapedString::printTo(const char* s, Print* p) +{ + if (!s) return p->print("null"); + + size_t n = p->write('\"'); + + while (*s) + { + n += printCharTo(*s++, p); + } + + return n + p->write('\"'); } \ No newline at end of file diff --git a/srcs/Internals/IndentedPrint.cpp b/src/Internals/IndentedPrint.cpp similarity index 93% rename from srcs/Internals/IndentedPrint.cpp rename to src/Internals/IndentedPrint.cpp index 86c36529..74c4dc21 100644 --- a/srcs/Internals/IndentedPrint.cpp +++ b/src/Internals/IndentedPrint.cpp @@ -1,45 +1,45 @@ -#include "ArduinoJson/Internals/IndentedPrint.h" - -using namespace ArduinoJson::Generator; - -void IndentedPrint::indent() -{ - if (level < MAX_LEVEL) - level++; -} - -void IndentedPrint::unindent() -{ - if (level > 0) - level--; -} - -void IndentedPrint::setTabSize(uint8_t n) -{ - if (n < MAX_TAB_SIZE) - tabSize = n; -} - -size_t IndentedPrint::write(uint8_t c) -{ - size_t n = 0; - - if (isNewLine) - n += writeTabs(); - - n += sink->write(c); - - isNewLine = c == '\n'; - - return n; -} - -inline size_t IndentedPrint::writeTabs() -{ - size_t n = 0; - - for (int i = 0; i < level*tabSize; i++) - n += sink->write(' '); - - return n; +#include "ArduinoJson/Internals/IndentedPrint.h" + +using namespace ArduinoJson::Generator; + +void IndentedPrint::indent() +{ + if (level < MAX_LEVEL) + level++; +} + +void IndentedPrint::unindent() +{ + if (level > 0) + level--; +} + +void IndentedPrint::setTabSize(uint8_t n) +{ + if (n < MAX_TAB_SIZE) + tabSize = n; +} + +size_t IndentedPrint::write(uint8_t c) +{ + size_t n = 0; + + if (isNewLine) + n += writeTabs(); + + n += sink->write(c); + + isNewLine = c == '\n'; + + return n; +} + +inline size_t IndentedPrint::writeTabs() +{ + size_t n = 0; + + for (int i = 0; i < level*tabSize; i++) + n += sink->write(' '); + + return n; } \ No newline at end of file diff --git a/srcs/Internals/JsonNode.cpp b/src/Internals/JsonNode.cpp similarity index 95% rename from srcs/Internals/JsonNode.cpp rename to src/Internals/JsonNode.cpp index 2dab693c..91ef2408 100644 --- a/srcs/Internals/JsonNode.cpp +++ b/src/Internals/JsonNode.cpp @@ -1,166 +1,166 @@ -#include "ArduinoJson/Internals/JsonNode.h" - -#include "ArduinoJson/Internals/JsonWriter.h" -#include "ArduinoJson/JsonArray.h" -#include "ArduinoJson/JsonObject.h" -#include "ArduinoJson/JsonBuffer.h" - -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.asKey.key); - writer.writeColon(); - child->content.asKey.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; - } +#include "ArduinoJson/Internals/JsonNode.h" + +#include "ArduinoJson/Internals/JsonWriter.h" +#include "ArduinoJson/JsonArray.h" +#include "ArduinoJson/JsonObject.h" +#include "ArduinoJson/JsonBuffer.h" + +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.asKey.key); + writer.writeColon(); + child->content.asKey.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; + } } \ No newline at end of file diff --git a/srcs/Internals/JsonParser.cpp b/src/Internals/JsonParser.cpp similarity index 100% rename from srcs/Internals/JsonParser.cpp rename to src/Internals/JsonParser.cpp diff --git a/srcs/Internals/JsonWriter.cpp b/src/Internals/JsonWriter.cpp similarity index 95% rename from srcs/Internals/JsonWriter.cpp rename to src/Internals/JsonWriter.cpp index 185c1a74..c005c73b 100644 --- a/srcs/Internals/JsonWriter.cpp +++ b/src/Internals/JsonWriter.cpp @@ -1,25 +1,25 @@ -#include "ArduinoJson/Internals/JsonWriter.h" -#include "ArduinoJson/Internals/EscapedString.h" - -using namespace ArduinoJson::Internals; - -void JsonWriter::writeString(char const* value) -{ - _length += EscapedString::printTo(value, _sink); -} - -void JsonWriter::writeInteger(long value) -{ - - _length += _sink->print(value); -} - -void JsonWriter::writeBoolean(bool value) -{ - _length += _sink->print(value ? "true" : "false"); -} - -void JsonWriter::writeDouble(double value, int decimals) -{ - _length += _sink->print(value, decimals); +#include "ArduinoJson/Internals/JsonWriter.h" +#include "ArduinoJson/Internals/EscapedString.h" + +using namespace ArduinoJson::Internals; + +void JsonWriter::writeString(char const* value) +{ + _length += EscapedString::printTo(value, _sink); +} + +void JsonWriter::writeInteger(long value) +{ + + _length += _sink->print(value); +} + +void JsonWriter::writeBoolean(bool value) +{ + _length += _sink->print(value ? "true" : "false"); +} + +void JsonWriter::writeDouble(double value, int decimals) +{ + _length += _sink->print(value, decimals); } \ No newline at end of file diff --git a/srcs/Internals/PrettyJsonWriter.cpp b/src/Internals/PrettyJsonWriter.cpp similarity index 98% rename from srcs/Internals/PrettyJsonWriter.cpp rename to src/Internals/PrettyJsonWriter.cpp index bee97566..1f9da156 100644 --- a/srcs/Internals/PrettyJsonWriter.cpp +++ b/src/Internals/PrettyJsonWriter.cpp @@ -1 +1 @@ -#include "ArduinoJson/Internals/PrettyJsonWriter.h" +#include "ArduinoJson/Internals/PrettyJsonWriter.h" diff --git a/srcs/Internals/StringBuilder.cpp b/src/Internals/StringBuilder.cpp similarity index 95% rename from srcs/Internals/StringBuilder.cpp rename to src/Internals/StringBuilder.cpp index 23f9eb57..9f5c867a 100644 --- a/srcs/Internals/StringBuilder.cpp +++ b/src/Internals/StringBuilder.cpp @@ -1,17 +1,17 @@ -/* - * Arduino JSON library - * Benoit Blanchon 2014 - MIT License - */ - -#include "ArduinoJson/Internals/StringBuilder.h" - -using namespace ArduinoJson::Internals; - -size_t StringBuilder::write(uint8_t c) -{ - if (length >= capacity) return 0; - - buffer[length++] = c; - buffer[length] = 0; - return 1; +/* + * Arduino JSON library + * Benoit Blanchon 2014 - MIT License + */ + +#include "ArduinoJson/Internals/StringBuilder.h" + +using namespace ArduinoJson::Internals; + +size_t StringBuilder::write(uint8_t c) +{ + if (length >= capacity) return 0; + + buffer[length++] = c; + buffer[length] = 0; + return 1; } \ No newline at end of file diff --git a/srcs/JsonArray.cpp b/src/JsonArray.cpp similarity index 95% rename from srcs/JsonArray.cpp rename to src/JsonArray.cpp index 7e7a9534..a388c2d2 100644 --- a/srcs/JsonArray.cpp +++ b/src/JsonArray.cpp @@ -1,86 +1,86 @@ -#include "ArduinoJson/JsonArray.h" -#include "ArduinoJson/JsonObject.h" -#include "ArduinoJson/JsonValue.h" - -JsonValue JsonArray::operator[](int index) const -{ - for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it) - { - if (!index) return JsonValue(*it); - index--; - } - - return JsonValue(); -} - -void JsonArray::add(bool value) -{ - JsonNode* node = createNode(); - if (!node) return; - - node->setAsBoolean(value); - addChild(node); -} - -void JsonArray::add(char const* value) -{ - JsonNode* node = createNode(); - if (!node) return; - - node->setAsString(value); - addChild(node); -} - -void JsonArray::add(double value, int decimals) -{ - JsonNode* node = createNode(); - if (!node) return; - - node->setAsDouble(value, decimals); - addChild(node); -} - -void JsonArray::add(long value) -{ - JsonNode* node = createNode(); - if (!node) return; - - node->setAsLong(value); - addChild(node); -} - -// TODO: we should have the same issue as in JsonValue -void JsonArray::add(JsonContainer nestedContainer) -{ - JsonNode* node = createNode(); - if (!node) return; - - node->duplicate(nestedContainer._node); - addChild(node); -} - -JsonArray JsonArray::createNestedArray() -{ - JsonNode* node = createNode(); - - if (node) - { - node->setAsArray(_node->getContainerBuffer()); - addChild(node); - } - - return JsonArray(node); -} - -JsonObject JsonArray::createNestedObject() -{ - JsonNode* node = createNode(); - - if (node) - { - node->setAsObject(_node->getContainerBuffer()); - addChild(node); - } - - return JsonObject(node); +#include "ArduinoJson/JsonArray.h" +#include "ArduinoJson/JsonObject.h" +#include "ArduinoJson/JsonValue.h" + +JsonValue JsonArray::operator[](int index) const +{ + for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it) + { + if (!index) return JsonValue(*it); + index--; + } + + return JsonValue(); +} + +void JsonArray::add(bool value) +{ + JsonNode* node = createNode(); + if (!node) return; + + node->setAsBoolean(value); + addChild(node); +} + +void JsonArray::add(char const* value) +{ + JsonNode* node = createNode(); + if (!node) return; + + node->setAsString(value); + addChild(node); +} + +void JsonArray::add(double value, int decimals) +{ + JsonNode* node = createNode(); + if (!node) return; + + node->setAsDouble(value, decimals); + addChild(node); +} + +void JsonArray::add(long value) +{ + JsonNode* node = createNode(); + if (!node) return; + + node->setAsLong(value); + addChild(node); +} + +// TODO: we should have the same issue as in JsonValue +void JsonArray::add(JsonContainer nestedContainer) +{ + JsonNode* node = createNode(); + if (!node) return; + + node->duplicate(nestedContainer._node); + addChild(node); +} + +JsonArray JsonArray::createNestedArray() +{ + JsonNode* node = createNode(); + + if (node) + { + node->setAsArray(_node->getContainerBuffer()); + addChild(node); + } + + return JsonArray(node); +} + +JsonObject JsonArray::createNestedObject() +{ + JsonNode* node = createNode(); + + if (node) + { + node->setAsObject(_node->getContainerBuffer()); + addChild(node); + } + + return JsonObject(node); } \ No newline at end of file diff --git a/srcs/JsonBuffer.cpp b/src/JsonBuffer.cpp similarity index 94% rename from srcs/JsonBuffer.cpp rename to src/JsonBuffer.cpp index 217c6321..068066b8 100644 --- a/srcs/JsonBuffer.cpp +++ b/src/JsonBuffer.cpp @@ -1,86 +1,86 @@ -#include "ArduinoJson/JsonBuffer.h" - -#include - -#include "ArduinoJson/JsonValue.h" -#include "ArduinoJson/Internals/JsonParser.h" -#include "ArduinoJson/Internals/JsonNode.h" - -JsonValue JsonBuffer::createValue() -{ - return JsonValue(createNode()); -} - -JsonNode* JsonBuffer::createNode() -{ - void* node = allocateNode(); - if (!node) return 0; - - return new (node) JsonNode(); -} - -JsonArray JsonBuffer::parseArray(char* json) -{ - JsonParser parser(this, json); - return JsonArray(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; -} - -JsonNode* JsonBuffer::createStringNode(const char* value) -{ - JsonNode* node = createNode(); - - if (node) - node->setAsString(value); - - return node; +#include "ArduinoJson/JsonBuffer.h" + +#include + +#include "ArduinoJson/JsonValue.h" +#include "ArduinoJson/Internals/JsonParser.h" +#include "ArduinoJson/Internals/JsonNode.h" + +JsonValue JsonBuffer::createValue() +{ + return JsonValue(createNode()); +} + +JsonNode* JsonBuffer::createNode() +{ + void* node = allocateNode(); + if (!node) return 0; + + return new (node) JsonNode(); +} + +JsonArray JsonBuffer::parseArray(char* json) +{ + JsonParser parser(this, json); + return JsonArray(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; +} + +JsonNode* JsonBuffer::createStringNode(const char* value) +{ + JsonNode* node = createNode(); + + if (node) + node->setAsString(value); + + return node; } \ No newline at end of file diff --git a/srcs/JsonContainer.cpp b/src/JsonContainer.cpp similarity index 95% rename from srcs/JsonContainer.cpp rename to src/JsonContainer.cpp index 323ca050..8ff51244 100644 --- a/srcs/JsonContainer.cpp +++ b/src/JsonContainer.cpp @@ -1,82 +1,82 @@ -#include "ArduinoJson/JsonContainer.h" - -#include "ArduinoJson/JsonBuffer.h" -#include "ArduinoJson/Internals/StringBuilder.h" -#include "ArduinoJson/Internals/CompactJsonWriter.h" -#include "ArduinoJson/Internals/PrettyJsonWriter.h" - -using namespace ArduinoJson::Internals; - -size_t JsonContainer::printTo(char* buffer, size_t bufferSize) const -{ - StringBuilder sb(buffer, bufferSize); - return printTo(sb); -} - -size_t JsonContainer::printTo(Print& p) const -{ - CompactJsonWriter writer(&p); - _node->writeTo(writer); - return writer.bytesWritten(); -} - -size_t JsonContainer::prettyPrintTo(char* buffer, size_t bufferSize) const -{ - StringBuilder sb(buffer, bufferSize); - return prettyPrintTo(sb); -} - -size_t JsonContainer::prettyPrintTo(IndentedPrint& p) const -{ - PrettyJsonWriter writer(&p); - _node->writeTo(writer); - return writer.bytesWritten(); -} - -size_t JsonContainer::prettyPrintTo(Print& print) const -{ - IndentedPrint indentedPrint = IndentedPrint(print); - return prettyPrintTo(indentedPrint); -} - -JsonNode* JsonContainer::createNode() -{ - if (!_node) return 0; - - JsonBuffer* buffer = _node->getContainerBuffer(); - if (!buffer) return 0; - - return buffer->createNode(); -} - -bool JsonContainer::operator==(const JsonContainer & other) const -{ - if (_node == other._node) return true; - if (!_node || !other._node) return false; - return _node->getProxyTarget() == other._node->getProxyTarget(); -} - -void JsonContainer::addChild(JsonNode* childToAdd) -{ - if (_node) - _node->addChild(childToAdd); -} - -void JsonContainer::removeChild(JsonNode* childToRemove) -{ - if (_node) - _node->removeChild(childToRemove); -} - -size_t JsonContainer::size() const -{ - int size = 0; - - for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it) - { - size++; - } - - return size; -} - +#include "ArduinoJson/JsonContainer.h" + +#include "ArduinoJson/JsonBuffer.h" +#include "ArduinoJson/Internals/StringBuilder.h" +#include "ArduinoJson/Internals/CompactJsonWriter.h" +#include "ArduinoJson/Internals/PrettyJsonWriter.h" + +using namespace ArduinoJson::Internals; + +size_t JsonContainer::printTo(char* buffer, size_t bufferSize) const +{ + StringBuilder sb(buffer, bufferSize); + return printTo(sb); +} + +size_t JsonContainer::printTo(Print& p) const +{ + CompactJsonWriter writer(&p); + _node->writeTo(writer); + return writer.bytesWritten(); +} + +size_t JsonContainer::prettyPrintTo(char* buffer, size_t bufferSize) const +{ + StringBuilder sb(buffer, bufferSize); + return prettyPrintTo(sb); +} + +size_t JsonContainer::prettyPrintTo(IndentedPrint& p) const +{ + PrettyJsonWriter writer(&p); + _node->writeTo(writer); + return writer.bytesWritten(); +} + +size_t JsonContainer::prettyPrintTo(Print& print) const +{ + IndentedPrint indentedPrint = IndentedPrint(print); + return prettyPrintTo(indentedPrint); +} + +JsonNode* JsonContainer::createNode() +{ + if (!_node) return 0; + + JsonBuffer* buffer = _node->getContainerBuffer(); + if (!buffer) return 0; + + return buffer->createNode(); +} + +bool JsonContainer::operator==(const JsonContainer & other) const +{ + if (_node == other._node) return true; + if (!_node || !other._node) return false; + return _node->getProxyTarget() == other._node->getProxyTarget(); +} + +void JsonContainer::addChild(JsonNode* childToAdd) +{ + if (_node) + _node->addChild(childToAdd); +} + +void JsonContainer::removeChild(JsonNode* childToRemove) +{ + if (_node) + _node->removeChild(childToRemove); +} + +size_t JsonContainer::size() const +{ + int size = 0; + + for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it) + { + size++; + } + + return size; +} + diff --git a/srcs/JsonObject.cpp b/src/JsonObject.cpp similarity index 95% rename from srcs/JsonObject.cpp rename to src/JsonObject.cpp index a7173972..cb9271fc 100644 --- a/srcs/JsonObject.cpp +++ b/src/JsonObject.cpp @@ -1,73 +1,73 @@ -#include "ArduinoJson/JsonObject.h" - -#include // for strcmp - -#include "ArduinoJson/JsonBuffer.h" -#include "ArduinoJson/JsonValue.h" -#include "ArduinoJson/Internals/EscapedString.h" -#include "ArduinoJson/Internals/JsonNode.h" -#include "ArduinoJson/Internals/StringBuilder.h" - -using namespace ArduinoJson::Internals; - -JsonValue JsonObject::operator[](char const* key) -{ - JsonNode* node = getOrCreateNodeAt(key); - return JsonValue(node); -} - -void JsonObject::remove(char const* key) -{ - for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it) - { - const char* childKey = it->getAsObjectKey(); - - if (!strcmp(childKey, key)) - { - removeChild(*it); - } - } -} - -JsonArray JsonObject::createNestedArray(char const* key) -{ - JsonNode* node = getOrCreateNodeAt(key); - - if (node) - node->setAsArray(_node->getContainerBuffer()); - - return JsonArray(node); -} - -JsonObject JsonObject::createNestedObject(char const* key) -{ - JsonNode* node = getOrCreateNodeAt(key); - - if (node) - node->setAsObject(_node->getContainerBuffer()); - - return JsonObject(node); -} - -JsonNode* JsonObject::getOrCreateNodeAt(const char* key) -{ - for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it) - { - const char* childKey = it->getAsObjectKey(); - - if (!strcmp(childKey, key)) - return it->getAsObjectValue(); - } - - JsonNode* newValueNode = createNode(); - if (!newValueNode) return 0; - - JsonNode* newKeyNode = createNode(); - if (!newKeyNode) return 0; - - newKeyNode->setAsObjectKeyValue(key, newValueNode); - - addChild(newKeyNode); - - return newValueNode; +#include "ArduinoJson/JsonObject.h" + +#include // for strcmp + +#include "ArduinoJson/JsonBuffer.h" +#include "ArduinoJson/JsonValue.h" +#include "ArduinoJson/Internals/EscapedString.h" +#include "ArduinoJson/Internals/JsonNode.h" +#include "ArduinoJson/Internals/StringBuilder.h" + +using namespace ArduinoJson::Internals; + +JsonValue JsonObject::operator[](char const* key) +{ + JsonNode* node = getOrCreateNodeAt(key); + return JsonValue(node); +} + +void JsonObject::remove(char const* key) +{ + for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it) + { + const char* childKey = it->getAsObjectKey(); + + if (!strcmp(childKey, key)) + { + removeChild(*it); + } + } +} + +JsonArray JsonObject::createNestedArray(char const* key) +{ + JsonNode* node = getOrCreateNodeAt(key); + + if (node) + node->setAsArray(_node->getContainerBuffer()); + + return JsonArray(node); +} + +JsonObject JsonObject::createNestedObject(char const* key) +{ + JsonNode* node = getOrCreateNodeAt(key); + + if (node) + node->setAsObject(_node->getContainerBuffer()); + + return JsonObject(node); +} + +JsonNode* JsonObject::getOrCreateNodeAt(const char* key) +{ + for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it) + { + const char* childKey = it->getAsObjectKey(); + + if (!strcmp(childKey, key)) + return it->getAsObjectValue(); + } + + JsonNode* newValueNode = createNode(); + if (!newValueNode) return 0; + + JsonNode* newKeyNode = createNode(); + if (!newKeyNode) return 0; + + newKeyNode->setAsObjectKeyValue(key, newValueNode); + + addChild(newKeyNode); + + return newValueNode; } \ No newline at end of file diff --git a/srcs/JsonValue.cpp b/src/JsonValue.cpp similarity index 94% rename from srcs/JsonValue.cpp rename to src/JsonValue.cpp index 7cb6e105..e197e07e 100644 --- a/srcs/JsonValue.cpp +++ b/src/JsonValue.cpp @@ -1,59 +1,59 @@ -#include "ArduinoJson/JsonValue.h" - -#include "ArduinoJson/JsonArray.h" -#include "ArduinoJson/JsonObject.h" -#include "ArduinoJson/Internals/JsonNode.h" - -void JsonValue::operator=(bool value) -{ - if (_node) - _node->setAsBoolean(value); -} - -void JsonValue::operator=(char const* value) -{ - if (_node) - _node->setAsString(value); -} - -void JsonValue::set(double value, int decimals) -{ - if (_node) - _node->setAsDouble(value, decimals); -} - -void JsonValue::operator=(int value) -{ - if (_node) - _node->setAsLong(value); -} - -JsonValue::operator bool() const -{ - return _node ? _node->getAsBoolean() : false; -} - -JsonValue::operator char const*() const -{ - return _node ? _node->getAsString() : 0; -} - -JsonValue::operator double() const -{ - return _node ? _node->getAsDouble() : 0; -} - -JsonValue::operator long() const -{ - return _node ? _node->getAsInteger() : 0; -} - -JsonValue::operator JsonArray() const -{ - return JsonArray(_node); -} - -JsonValue::operator JsonObject() const -{ - return JsonObject(_node); +#include "ArduinoJson/JsonValue.h" + +#include "ArduinoJson/JsonArray.h" +#include "ArduinoJson/JsonObject.h" +#include "ArduinoJson/Internals/JsonNode.h" + +void JsonValue::operator=(bool value) +{ + if (_node) + _node->setAsBoolean(value); +} + +void JsonValue::operator=(char const* value) +{ + if (_node) + _node->setAsString(value); +} + +void JsonValue::set(double value, int decimals) +{ + if (_node) + _node->setAsDouble(value, decimals); +} + +void JsonValue::operator=(int value) +{ + if (_node) + _node->setAsLong(value); +} + +JsonValue::operator bool() const +{ + return _node ? _node->getAsBoolean() : false; +} + +JsonValue::operator char const*() const +{ + return _node ? _node->getAsString() : 0; +} + +JsonValue::operator double() const +{ + return _node ? _node->getAsDouble() : 0; +} + +JsonValue::operator long() const +{ + return _node ? _node->getAsInteger() : 0; +} + +JsonValue::operator JsonArray() const +{ + return JsonArray(_node); +} + +JsonValue::operator JsonObject() const +{ + return JsonObject(_node); } \ No newline at end of file