From 5443e90baf2861c0855aa3b13b17b20c932a1560 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Fri, 31 Oct 2014 12:02:15 +0100 Subject: [PATCH] Cleaning up... --- .../Internals/CompactJsonWriter.hpp | 9 ++---- .../ArduinoJson/Internals/JsonArrayNode.hpp | 2 +- include/ArduinoJson/Internals/JsonParser.hpp | 2 +- .../ArduinoJson/Internals/JsonSerializer.hpp | 26 --------------- .../Internals/JsonValueContent.hpp | 2 +- include/ArduinoJson/Internals/JsonWriter.hpp | 9 ++---- include/ArduinoJson/Internals/NonCopyable.hpp | 25 --------------- .../ArduinoJson/Internals/ReferenceType.hpp | 32 +++++++++++++++++++ include/ArduinoJson/JsonArray.hpp | 10 ++---- include/ArduinoJson/JsonObject.hpp | 10 ++---- src/JsonValue.cpp | 6 ++-- 11 files changed, 47 insertions(+), 86 deletions(-) delete mode 100644 include/ArduinoJson/Internals/JsonSerializer.hpp delete mode 100644 include/ArduinoJson/Internals/NonCopyable.hpp create mode 100644 include/ArduinoJson/Internals/ReferenceType.hpp diff --git a/include/ArduinoJson/Internals/CompactJsonWriter.hpp b/include/ArduinoJson/Internals/CompactJsonWriter.hpp index abc39027..dcc1e7e7 100644 --- a/include/ArduinoJson/Internals/CompactJsonWriter.hpp +++ b/include/ArduinoJson/Internals/CompactJsonWriter.hpp @@ -16,16 +16,13 @@ class CompactJsonWriter : public JsonWriter { explicit CompactJsonWriter(Print *sink) : JsonWriter(sink) {} virtual void beginArray() { _length += _sink->write('['); } - virtual void endArray() { _length += _sink->write(']'); } - virtual void writeColon() { _length += _sink->write(':'); } - - virtual void writeComma() { _length += _sink->write(','); } - virtual void beginObject() { _length += _sink->write('{'); } - virtual void endObject() { _length += _sink->write('}'); } + + virtual void writeColon() { _length += _sink->write(':'); } + virtual void writeComma() { _length += _sink->write(','); } }; } } diff --git a/include/ArduinoJson/Internals/JsonArrayNode.hpp b/include/ArduinoJson/Internals/JsonArrayNode.hpp index 13c3a62e..0eee7047 100644 --- a/include/ArduinoJson/Internals/JsonArrayNode.hpp +++ b/include/ArduinoJson/Internals/JsonArrayNode.hpp @@ -14,7 +14,7 @@ namespace Internals { class JsonArrayNode { public: - JsonArrayNode() : next(0) {} + JsonArrayNode() : next(NULL) {} JsonArrayNode* next; JsonValue value; diff --git a/include/ArduinoJson/Internals/JsonParser.hpp b/include/ArduinoJson/Internals/JsonParser.hpp index 0741cbb8..084fe193 100644 --- a/include/ArduinoJson/Internals/JsonParser.hpp +++ b/include/ArduinoJson/Internals/JsonParser.hpp @@ -20,7 +20,7 @@ class JsonParser { JsonObject &parseObject(); private: - bool isEnd() { return *_ptr == 0; } + bool isEnd() { return *_ptr == '\0'; } bool skip(char charToSkip); void skipSpaces(); diff --git a/include/ArduinoJson/Internals/JsonSerializer.hpp b/include/ArduinoJson/Internals/JsonSerializer.hpp deleted file mode 100644 index bba785cd..00000000 --- a/include/ArduinoJson/Internals/JsonSerializer.hpp +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright Benoit Blanchon 2014 -// MIT License -// -// Arduino JSON library -// https://github.com/bblanchon/ArduinoJson - -#pragma once - -#include "../ForwardDeclarations.hpp" - -namespace ArduinoJson { -namespace Internals { - -class JsonSerializer { - public: - JsonSerializer(JsonWriter &writer) : _writer(writer) {} - - void serialize(JsonValueImpl *value); - void serialize(JsonArrayImpl *value); - void serialize(JsonObjectImpl *value); - - private: - JsonWriter &_writer; -}; -} -} diff --git a/include/ArduinoJson/Internals/JsonValueContent.hpp b/include/ArduinoJson/Internals/JsonValueContent.hpp index bf94b44a..59ebcec1 100644 --- a/include/ArduinoJson/Internals/JsonValueContent.hpp +++ b/include/ArduinoJson/Internals/JsonValueContent.hpp @@ -14,7 +14,7 @@ namespace Internals { union JsonValueContent { bool asBoolean; double asDouble; - long asInteger; + long asLong; const char* asString; JsonArray* asArray; JsonObject* asObject; diff --git a/include/ArduinoJson/Internals/JsonWriter.hpp b/include/ArduinoJson/Internals/JsonWriter.hpp index aa25fab9..56002d2a 100644 --- a/include/ArduinoJson/Internals/JsonWriter.hpp +++ b/include/ArduinoJson/Internals/JsonWriter.hpp @@ -18,12 +18,12 @@ class JsonWriter { size_t bytesWritten() { return _length; } virtual void beginArray() = 0; - virtual void endArray() = 0; + void writeEmptyArray() { _length += _sink->print("[]"); } virtual void beginObject() = 0; - virtual void endObject() = 0; + void writeEmptyObject() { _length += _sink->print("{}"); } void writeString(const char *value); void writeInteger(long value); @@ -31,13 +31,8 @@ class JsonWriter { void writeDouble(double value, int decimals); virtual void writeColon() = 0; - virtual void writeComma() = 0; - void writeEmptyArray() { _length += _sink->print("[]"); } - - void writeEmptyObject() { _length += _sink->print("{}"); } - protected: Print *_sink; size_t _length; diff --git a/include/ArduinoJson/Internals/NonCopyable.hpp b/include/ArduinoJson/Internals/NonCopyable.hpp deleted file mode 100644 index bdaa9721..00000000 --- a/include/ArduinoJson/Internals/NonCopyable.hpp +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright Benoit Blanchon 2014 -// MIT License -// -// Arduino JSON library -// https://github.com/bblanchon/ArduinoJson - -#pragma once - -namespace ArduinoJson { -namespace Internals { - -// A class that is not meant to be copied -class NonCopyable { - protected: - NonCopyable() {} - - private: - // copy constructor is private - NonCopyable(const NonCopyable&); - - // copy operator is private - NonCopyable& operator=(const NonCopyable&); -}; -} -} diff --git a/include/ArduinoJson/Internals/ReferenceType.hpp b/include/ArduinoJson/Internals/ReferenceType.hpp new file mode 100644 index 00000000..8c2e1a9b --- /dev/null +++ b/include/ArduinoJson/Internals/ReferenceType.hpp @@ -0,0 +1,32 @@ +// Copyright Benoit Blanchon 2014 +// MIT License +// +// Arduino JSON library +// https://github.com/bblanchon/ArduinoJson + +#pragma once + +namespace ArduinoJson { +namespace Internals { + +// A type that is meant to be used by reference only (JsonArray and JsonObject) +class ReferenceType { + public: + bool operator==(const ReferenceType& other) const { + // two JsonArray are equal if they are the same instance + // (we don't compare the content) + return this == &other; + } + + protected: + ReferenceType() {} + + private: + // copy constructor is private + ReferenceType(const ReferenceType&); + + // copy operator is private + ReferenceType& operator=(const ReferenceType&); +}; +} +} diff --git a/include/ArduinoJson/JsonArray.hpp b/include/ArduinoJson/JsonArray.hpp index c29efec5..30e4bea7 100644 --- a/include/ArduinoJson/JsonArray.hpp +++ b/include/ArduinoJson/JsonArray.hpp @@ -11,14 +11,14 @@ #include "JsonArrayConstIterator.hpp" #include "JsonPrintable.hpp" #include "JsonObject.hpp" -#include "Internals/NonCopyable.hpp" +#include "Internals/ReferenceType.hpp" #define JSON_ARRAY_SIZE(NUMBER_OF_ELEMENTS) \ (sizeof(JsonArray) + (NUMBER_OF_ELEMENTS) * sizeof(Internals::JsonArrayNode)) namespace ArduinoJson { -class JsonArray : public JsonPrintable, Internals::NonCopyable { +class JsonArray : public JsonPrintable, public Internals::ReferenceType { friend class JsonBuffer; public: @@ -67,10 +67,4 @@ class JsonArray : public JsonPrintable, Internals::NonCopyable { Internals::JsonArrayNode *_firstNode; static JsonArray _invalid; }; - -inline bool operator==(const JsonArray &left, const JsonArray &right) { - // two JsonArray are equal if they are the same instance - // (we don't compare the content) - return &left == &right; -} } diff --git a/include/ArduinoJson/JsonObject.hpp b/include/ArduinoJson/JsonObject.hpp index d7ff4018..6243e591 100644 --- a/include/ArduinoJson/JsonObject.hpp +++ b/include/ArduinoJson/JsonObject.hpp @@ -7,7 +7,7 @@ #pragma once #include "Internals/JsonObjectNode.hpp" -#include "Internals/NonCopyable.hpp" +#include "Internals/ReferenceType.hpp" #include "JsonArray.hpp" #include "JsonObjectConstIterator.hpp" #include "JsonObjectIterator.hpp" @@ -19,7 +19,7 @@ namespace ArduinoJson { -class JsonObject : public JsonPrintable, Internals::NonCopyable { +class JsonObject : public JsonPrintable, public Internals::ReferenceType { friend class JsonBuffer; public: @@ -72,10 +72,4 @@ class JsonObject : public JsonPrintable, Internals::NonCopyable { Internals::JsonObjectNode *_firstNode; static JsonObject _invalid; }; - -inline bool operator==(const JsonObject &left, const JsonObject &right) { - // two JsonObject are equal if they are the same instance - // (we don't compare the content) - return &left == &right; -} } diff --git a/src/JsonValue.cpp b/src/JsonValue.cpp index c0bdfc1a..c0e8a890 100644 --- a/src/JsonValue.cpp +++ b/src/JsonValue.cpp @@ -35,7 +35,7 @@ JsonValue::operator double() const { } JsonValue::operator long() const { - return _type == JSON_LONG ? _content.asInteger : 0; + return _type == JSON_LONG ? _content.asLong : 0; } void JsonValue::set(bool value) { @@ -59,7 +59,7 @@ void JsonValue::set(double value, int decimals) { void JsonValue::set(long value) { if (_type == JSON_INVALID) return; _type = JSON_LONG; - _content.asInteger = value; + _content.asLong = value; } void JsonValue::set(JsonArray &array) { @@ -89,7 +89,7 @@ void JsonValue::writeTo(JsonWriter &writer) const { break; case JSON_LONG: - writer.writeInteger(_content.asInteger); + writer.writeInteger(_content.asLong); break; case JSON_BOOLEAN: