Fixed namespaces

This commit is contained in:
Benoit Blanchon
2014-10-18 23:05:54 +02:00
parent 1abb8ac6ae
commit 074c39ca5b
31 changed files with 638 additions and 554 deletions

View File

@ -2,42 +2,47 @@
#include "ArduinoJson/Internals/JsonWriter.h"
class CompactJsonWriter : public JsonWriter
namespace ArduinoJson
{
public:
explicit CompactJsonWriter(Print* sink)
: JsonWriter(sink)
namespace Internals
{
}
class CompactJsonWriter : public JsonWriter
{
public:
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 writeColon()
{
_length += _sink->write(':');
}
virtual void writeComma()
{
_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('}');
}
};
}
};
}

View File

@ -9,7 +9,7 @@
namespace ArduinoJson
{
namespace Generator
namespace Internals
{
// Decorator on top of Print to allow indented output.
// This class is used by JsonPrintable::prettyPrintTo() but can also be used

View File

@ -1,181 +1,188 @@
#pragma once
class JsonBuffer;
class JsonWriter;
class JsonNodeIterator;
class JsonNode
namespace ArduinoJson
{
friend class JsonNodeIterator;
enum JsonNodeType
{
JSON_UNDEFINED,
JSON_NULL,
JSON_ARRAY,
JSON_OBJECT,
JSON_KEY_VALUE,
JSON_BOOLEAN,
JSON_STRING,
JSON_LONG,
JSON_PROXY,
JSON_DOUBLE_0_DECIMALS,
JSON_DOUBLE_1_DECIMAL,
JSON_DOUBLE_2_DECIMALS,
// etc.
};
union JsonNodeContent
{
bool asBoolean;
double asDouble;
long asInteger;
const char* asString;
struct
{
const char* key;
JsonNode* value;
} asKey;
struct
{
JsonNode* child;
JsonBuffer* buffer;
} asContainer;
struct
{
JsonNode* target;
} asProxy;
};
public:
JsonNode()
: type(JSON_UNDEFINED), next(0)
{
}
class JsonBuffer;
void writeTo(JsonWriter&); // TODO: <- move in JsonNodeSerializer
void setAsArray(JsonBuffer* buffer)
namespace Internals
{
type = JSON_ARRAY;
content.asContainer.child = 0;
content.asContainer.buffer = buffer;
class JsonWriter;
class JsonNodeIterator;
class JsonNode
{
friend class JsonNodeIterator;
enum JsonNodeType
{
JSON_UNDEFINED,
JSON_NULL,
JSON_ARRAY,
JSON_OBJECT,
JSON_KEY_VALUE,
JSON_BOOLEAN,
JSON_STRING,
JSON_LONG,
JSON_PROXY,
JSON_DOUBLE_0_DECIMALS,
JSON_DOUBLE_1_DECIMAL,
JSON_DOUBLE_2_DECIMALS,
// etc.
};
union JsonNodeContent
{
bool asBoolean;
double asDouble;
long asInteger;
const char* asString;
struct
{
const char* key;
JsonNode* value;
} asKey;
struct
{
JsonNode* child;
JsonBuffer* buffer;
} asContainer;
struct
{
JsonNode* target;
} asProxy;
};
public:
JsonNode()
: type(JSON_UNDEFINED), next(0)
{
}
void writeTo(JsonWriter&); // TODO: <- move in JsonNodeSerializer
void setAsArray(JsonBuffer* buffer)
{
type = JSON_ARRAY;
content.asContainer.child = 0;
content.asContainer.buffer = buffer;
}
void setAsBoolean(bool value)
{
type = JSON_BOOLEAN;
content.asBoolean = value;
}
void setAsLong(int value)
{
type = JSON_LONG;
content.asInteger = value;
}
void setAsString(char const* value)
{
type = JSON_STRING;
content.asString = value;
}
void setAsDouble(double value, int decimals)
{
type = static_cast<JsonNodeType>(JSON_DOUBLE_0_DECIMALS + decimals);
content.asDouble = value;
}
void setAsObject(JsonBuffer* buffer)
{
type = JSON_OBJECT;
content.asContainer.child = 0;
content.asContainer.buffer = buffer;
}
void setAsObjectKeyValue(const char* key, JsonNode* value)
{
type = JSON_KEY_VALUE;
content.asKey.key = key;
content.asKey.value = value;
}
bool getAsBoolean()
{
return type == JSON_BOOLEAN ? content.asBoolean : false;
}
double getAsDouble()
{
return type >= JSON_DOUBLE_0_DECIMALS ? content.asDouble : 0;
}
long getAsInteger()
{
return type == JSON_LONG ? content.asInteger : 0;
}
const char* getAsString()
{
return type == JSON_STRING ? content.asString : 0;
}
JsonBuffer* getContainerBuffer()
{
if (type == JSON_PROXY) return content.asProxy.target->getContainerBuffer();
return type == JSON_ARRAY || type == JSON_OBJECT ? content.asContainer.buffer : 0;
}
JsonNode* getContainerChild()
{
if (type == JSON_PROXY) return content.asProxy.target->getContainerChild();
return type == JSON_ARRAY || type == JSON_OBJECT ? content.asContainer.child : 0;
}
const char* getAsObjectKey()
{
return type == JSON_KEY_VALUE ? content.asKey.key : 0;
}
JsonNode* getAsObjectValue()
{
return type == JSON_KEY_VALUE ? content.asKey.value : 0;
}
JsonNode* getProxyTarget()
{
return type == JSON_PROXY ? content.asProxy.target : this;
}
bool isArray()
{
return type == JSON_ARRAY;
}
void addChild(JsonNode* childToAdd);
void removeChild(JsonNode* childToRemove);
void duplicate(JsonNode* other);
private:
JsonNodeType type;
JsonNode* next;
JsonNodeContent content;
inline void writeArrayTo(JsonWriter&);// TODO: <- move in JsonNodeSerializer
inline void writeObjectTo(JsonWriter&);// TODO: <- move in JsonNodeSerializer
void setAsProxyOfSelf();
void setAsProxyOf(JsonNode* target)
{
type = JSON_PROXY;
content.asProxy.target = target;
}
};
}
void setAsBoolean(bool value)
{
type = JSON_BOOLEAN;
content.asBoolean = value;
}
void setAsLong(int value)
{
type = JSON_LONG;
content.asInteger = value;
}
void setAsString(char const* value)
{
type = JSON_STRING;
content.asString = value;
}
void setAsDouble(double value, int decimals)
{
type = static_cast<JsonNodeType>(JSON_DOUBLE_0_DECIMALS + decimals);
content.asDouble = value;
}
void setAsObject(JsonBuffer* buffer)
{
type = JSON_OBJECT;
content.asContainer.child = 0;
content.asContainer.buffer = buffer;
}
void setAsObjectKeyValue(const char* key, JsonNode* value)
{
type = JSON_KEY_VALUE;
content.asKey.key = key;
content.asKey.value = value;
}
bool getAsBoolean()
{
return type == JSON_BOOLEAN ? content.asBoolean : false;
}
double getAsDouble()
{
return type >= JSON_DOUBLE_0_DECIMALS ? content.asDouble : 0;
}
long getAsInteger()
{
return type == JSON_LONG ? content.asInteger : 0;
}
const char* getAsString()
{
return type == JSON_STRING ? content.asString : 0;
}
JsonBuffer* getContainerBuffer()
{
if (type == JSON_PROXY) return content.asProxy.target->getContainerBuffer();
return type == JSON_ARRAY || type == JSON_OBJECT ? content.asContainer.buffer : 0;
}
JsonNode* getContainerChild()
{
if (type == JSON_PROXY) return content.asProxy.target->getContainerChild();
return type == JSON_ARRAY || type == JSON_OBJECT ? content.asContainer.child : 0;
}
const char* getAsObjectKey()
{
return type == JSON_KEY_VALUE ? content.asKey.key : 0;
}
JsonNode* getAsObjectValue()
{
return type == JSON_KEY_VALUE ? content.asKey.value : 0;
}
JsonNode* getProxyTarget()
{
return type == JSON_PROXY ? content.asProxy.target : this;
}
bool isArray()
{
return type == JSON_ARRAY;
}
void addChild(JsonNode* childToAdd);
void removeChild(JsonNode* childToRemove);
void duplicate(JsonNode* other);
private:
JsonNodeType type;
JsonNode* next;
JsonNodeContent content;
inline void writeArrayTo(JsonWriter&);// TODO: <- move in JsonNodeSerializer
inline void writeObjectTo(JsonWriter&);// TODO: <- move in JsonNodeSerializer
void setAsProxyOfSelf();
void setAsProxyOf(JsonNode* target)
{
type = JSON_PROXY;
content.asProxy.target = target;
}
};
}

View File

@ -2,36 +2,41 @@
#include "JsonNode.h"
class JsonNodeIterator
namespace ArduinoJson
{
public:
explicit JsonNodeIterator(JsonNode* node)
: node(node)
namespace Internals
{
class JsonNodeIterator
{
public:
explicit JsonNodeIterator(JsonNode* node)
: node(node)
{
}
bool operator!= (const JsonNodeIterator& other) const
{
return node != other.node;
}
void operator++()
{
node = node->next;
}
JsonNode* operator*() const
{
return node;
}
JsonNode* operator->() const
{
return node;
}
private:
JsonNode* node;
};
}
bool operator!= (const JsonNodeIterator& other) const
{
return node != other.node;
}
void operator++()
{
node = node->next;
}
JsonNode* operator*() const
{
return node;
}
JsonNode* operator->() const
{
return node;
}
private:
JsonNode* node;
};
}

View File

@ -1,37 +1,43 @@
#pragma once
#include "JsonNode.h"
class JsonValue;
class JsonNodeWrapper
namespace ArduinoJson
{
friend class JsonValue;
public:
JsonNodeWrapper()
: _node(0)
class JsonValue ;
namespace Internals
{
}
explicit JsonNodeWrapper(JsonNode* node)
: _node(node)
{
}
protected:
void duplicate(const JsonNodeWrapper& other)
{
if (!_node)
class JsonNodeWrapper
{
_node = other._node;
}
else
{
_node->duplicate(other._node);
}
friend class JsonValue;
public:
JsonNodeWrapper()
: _node(0)
{
}
explicit JsonNodeWrapper(JsonNode* node)
: _node(node)
{
}
protected:
void duplicate(const JsonNodeWrapper& other)
{
if (!_node)
{
_node = other._node;
}
else
{
_node->duplicate(other._node);
}
}
JsonNode* _node;
};
}
JsonNode* _node;
};
}

View File

@ -1,43 +1,50 @@
#include "JsonNode.h"
#pragma once
class JsonNode;
class JsonBuffer;
#include "JsonNode.h"
class JsonParser
namespace ArduinoJson
{
public:
JsonParser(JsonBuffer* buffer, char* json)
: _buffer(buffer), _ptr(json)
class JsonBuffer;
namespace Internals
{
class JsonNode;
class JsonParser
{
public:
JsonParser(JsonBuffer* buffer, char* json)
: _buffer(buffer), _ptr(json)
{
}
JsonNode* parseAnything();
private:
JsonBuffer* _buffer;
char* _ptr;
inline bool isArrayStart();
inline bool isArrayStop();
inline bool isBoolean();
inline bool isComma();
inline bool isDouble();
inline bool isEnd();
inline bool isLong();
inline bool isNull();
inline bool isSpace();
inline void skipOneChar();
inline void skipSpaces();
inline JsonNode* parseArray();
inline JsonNode* parseBoolean();
inline JsonNode* parseLong();
inline JsonNode* parseNull();
inline JsonNode* parseString();
JsonNode *parseDouble();
};
}
JsonNode* parseAnything();
private:
JsonBuffer* _buffer;
char* _ptr;
inline bool isArrayStart();
inline bool isArrayStop();
inline bool isBoolean();
inline bool isComma();
inline bool isDouble();
inline bool isEnd();
inline bool isLong();
inline bool isNull();
inline bool isSpace();
inline void skipOneChar();
inline void skipSpaces();
inline JsonNode* parseArray();
inline JsonNode* parseBoolean();
inline JsonNode* parseLong();
inline JsonNode* parseNull();
inline JsonNode* parseString();
JsonNode *parseDouble();
};
}

View File

@ -2,48 +2,53 @@
#include "../Arduino/Print.h"
class JsonWriter
namespace ArduinoJson
{
public:
explicit JsonWriter(Print* sink)
: _sink(sink), _length(0)
namespace Internals
{
class JsonWriter
{
public:
explicit JsonWriter(Print* sink)
: _sink(sink), _length(0)
{
}
size_t bytesWritten()
{
return _length;
}
virtual void beginArray() = 0;
virtual void endArray() = 0;
virtual void beginObject() = 0;
virtual void endObject() = 0;
void writeString(const char* value);
void writeInteger(long value);
void writeBoolean(bool value);
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;
};
}
size_t bytesWritten()
{
return _length;
}
virtual void beginArray() = 0;
virtual void endArray() = 0;
virtual void beginObject() = 0;
virtual void endObject() = 0;
void writeString(const char* value);
void writeInteger(long value);
void writeBoolean(bool value);
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;
};
}

View File

@ -3,63 +3,67 @@
#include "JsonWriter.h"
#include "IndentedPrint.h"
using namespace ArduinoJson::Generator;
class PrettyJsonWriter : public JsonWriter
namespace ArduinoJson
{
public:
explicit PrettyJsonWriter(IndentedPrint* sink)
: JsonWriter(sink), _indenter(sink)
namespace Internals
{
}
class PrettyJsonWriter : public JsonWriter
{
public:
explicit PrettyJsonWriter(IndentedPrint* sink)
: JsonWriter(sink), _indenter(sink)
{
}
virtual void beginArray()
{
_length += _sink->write('[');
indent();
}
virtual void beginArray()
{
_length += _sink->write('[');
indent();
}
virtual void endArray()
{
unindent();
_length += _sink->write(']');
}
virtual void endArray()
{
unindent();
_length += _sink->write(']');
}
virtual void writeColon()
{
_length += _sink->print(": ");
}
virtual void writeColon()
{
_length += _sink->print(": ");
}
virtual void writeComma()
{
_length += _sink->write(',');
_length += _indenter->println();
}
virtual void writeComma()
{
_length += _sink->write(',');
_length += _indenter->println();
}
virtual void beginObject()
{
_length += _sink->write('{');
indent();
}
virtual void beginObject()
{
_length += _sink->write('{');
indent();
}
virtual void endObject()
{
unindent();
_length += _sink->write('}');
}
virtual void endObject()
{
unindent();
_length += _sink->write('}');
}
private:
IndentedPrint* _indenter;
private:
IndentedPrint* _indenter;
void indent()
{
_indenter->indent();
_length += _indenter->println();
}
void indent()
{
_indenter->indent();
_length += _indenter->println();
}
void unindent()
{
_length += _indenter->println();
_indenter->unindent();
void unindent()
{
_length += _indenter->println();
_indenter->unindent();
}
};
}
};
}