Cleaning up...

This commit is contained in:
Benoit Blanchon
2014-10-31 12:02:15 +01:00
parent a5dbb397ca
commit 5443e90baf
11 changed files with 47 additions and 86 deletions

View File

@ -16,16 +16,13 @@ class CompactJsonWriter : public JsonWriter {
explicit CompactJsonWriter(Print *sink) : JsonWriter(sink) {} explicit CompactJsonWriter(Print *sink) : JsonWriter(sink) {}
virtual void beginArray() { _length += _sink->write('['); } virtual void beginArray() { _length += _sink->write('['); }
virtual void endArray() { _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 beginObject() { _length += _sink->write('{'); }
virtual void endObject() { _length += _sink->write('}'); } virtual void endObject() { _length += _sink->write('}'); }
virtual void writeColon() { _length += _sink->write(':'); }
virtual void writeComma() { _length += _sink->write(','); }
}; };
} }
} }

View File

@ -14,7 +14,7 @@ namespace Internals {
class JsonArrayNode { class JsonArrayNode {
public: public:
JsonArrayNode() : next(0) {} JsonArrayNode() : next(NULL) {}
JsonArrayNode* next; JsonArrayNode* next;
JsonValue value; JsonValue value;

View File

@ -20,7 +20,7 @@ class JsonParser {
JsonObject &parseObject(); JsonObject &parseObject();
private: private:
bool isEnd() { return *_ptr == 0; } bool isEnd() { return *_ptr == '\0'; }
bool skip(char charToSkip); bool skip(char charToSkip);
void skipSpaces(); void skipSpaces();

View File

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

View File

@ -14,7 +14,7 @@ namespace Internals {
union JsonValueContent { union JsonValueContent {
bool asBoolean; bool asBoolean;
double asDouble; double asDouble;
long asInteger; long asLong;
const char* asString; const char* asString;
JsonArray* asArray; JsonArray* asArray;
JsonObject* asObject; JsonObject* asObject;

View File

@ -18,12 +18,12 @@ class JsonWriter {
size_t bytesWritten() { return _length; } size_t bytesWritten() { return _length; }
virtual void beginArray() = 0; virtual void beginArray() = 0;
virtual void endArray() = 0; virtual void endArray() = 0;
void writeEmptyArray() { _length += _sink->print("[]"); }
virtual void beginObject() = 0; virtual void beginObject() = 0;
virtual void endObject() = 0; virtual void endObject() = 0;
void writeEmptyObject() { _length += _sink->print("{}"); }
void writeString(const char *value); void writeString(const char *value);
void writeInteger(long value); void writeInteger(long value);
@ -31,13 +31,8 @@ class JsonWriter {
void writeDouble(double value, int decimals); void writeDouble(double value, int decimals);
virtual void writeColon() = 0; virtual void writeColon() = 0;
virtual void writeComma() = 0; virtual void writeComma() = 0;
void writeEmptyArray() { _length += _sink->print("[]"); }
void writeEmptyObject() { _length += _sink->print("{}"); }
protected: protected:
Print *_sink; Print *_sink;
size_t _length; size_t _length;

View File

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

View File

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

View File

@ -11,14 +11,14 @@
#include "JsonArrayConstIterator.hpp" #include "JsonArrayConstIterator.hpp"
#include "JsonPrintable.hpp" #include "JsonPrintable.hpp"
#include "JsonObject.hpp" #include "JsonObject.hpp"
#include "Internals/NonCopyable.hpp" #include "Internals/ReferenceType.hpp"
#define JSON_ARRAY_SIZE(NUMBER_OF_ELEMENTS) \ #define JSON_ARRAY_SIZE(NUMBER_OF_ELEMENTS) \
(sizeof(JsonArray) + (NUMBER_OF_ELEMENTS) * sizeof(Internals::JsonArrayNode)) (sizeof(JsonArray) + (NUMBER_OF_ELEMENTS) * sizeof(Internals::JsonArrayNode))
namespace ArduinoJson { namespace ArduinoJson {
class JsonArray : public JsonPrintable, Internals::NonCopyable { class JsonArray : public JsonPrintable, public Internals::ReferenceType {
friend class JsonBuffer; friend class JsonBuffer;
public: public:
@ -67,10 +67,4 @@ class JsonArray : public JsonPrintable, Internals::NonCopyable {
Internals::JsonArrayNode *_firstNode; Internals::JsonArrayNode *_firstNode;
static JsonArray _invalid; 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;
}
} }

View File

@ -7,7 +7,7 @@
#pragma once #pragma once
#include "Internals/JsonObjectNode.hpp" #include "Internals/JsonObjectNode.hpp"
#include "Internals/NonCopyable.hpp" #include "Internals/ReferenceType.hpp"
#include "JsonArray.hpp" #include "JsonArray.hpp"
#include "JsonObjectConstIterator.hpp" #include "JsonObjectConstIterator.hpp"
#include "JsonObjectIterator.hpp" #include "JsonObjectIterator.hpp"
@ -19,7 +19,7 @@
namespace ArduinoJson { namespace ArduinoJson {
class JsonObject : public JsonPrintable, Internals::NonCopyable { class JsonObject : public JsonPrintable, public Internals::ReferenceType {
friend class JsonBuffer; friend class JsonBuffer;
public: public:
@ -72,10 +72,4 @@ class JsonObject : public JsonPrintable, Internals::NonCopyable {
Internals::JsonObjectNode *_firstNode; Internals::JsonObjectNode *_firstNode;
static JsonObject _invalid; 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;
}
} }

View File

@ -35,7 +35,7 @@ JsonValue::operator double() const {
} }
JsonValue::operator long() const { JsonValue::operator long() const {
return _type == JSON_LONG ? _content.asInteger : 0; return _type == JSON_LONG ? _content.asLong : 0;
} }
void JsonValue::set(bool value) { void JsonValue::set(bool value) {
@ -59,7 +59,7 @@ void JsonValue::set(double value, int decimals) {
void JsonValue::set(long value) { void JsonValue::set(long value) {
if (_type == JSON_INVALID) return; if (_type == JSON_INVALID) return;
_type = JSON_LONG; _type = JSON_LONG;
_content.asInteger = value; _content.asLong = value;
} }
void JsonValue::set(JsonArray &array) { void JsonValue::set(JsonArray &array) {
@ -89,7 +89,7 @@ void JsonValue::writeTo(JsonWriter &writer) const {
break; break;
case JSON_LONG: case JSON_LONG:
writer.writeInteger(_content.asInteger); writer.writeInteger(_content.asLong);
break; break;
case JSON_BOOLEAN: case JSON_BOOLEAN: