Formated code with clang-format

This commit is contained in:
Benoit Blanchon
2014-10-23 19:54:00 +02:00
parent 888fdc1d54
commit 9175046f35
52 changed files with 2002 additions and 2638 deletions

1
.clang-format Normal file
View File

@ -0,0 +1 @@
BasedOnStyle: LLVM

View File

@ -11,10 +11,8 @@
#include <stdint.h>
// This class reproduces Arduino's Print
class Print
{
class Print {
public:
virtual size_t write(uint8_t) = 0;
size_t print(const char[]);

View File

@ -11,11 +11,9 @@
class Print;
class Printable
{
class Printable {
public:
virtual size_t printTo(Print& p) const = 0;
virtual size_t printTo(Print &p) const = 0;
};
#else
@ -23,4 +21,3 @@ public:
#include <Printable.h>
#endif

View File

@ -2,47 +2,23 @@
#include "ArduinoJson/Internals/JsonWriter.hpp"
namespace ArduinoJson
{
namespace Internals
{
class CompactJsonWriter : public JsonWriter
{
public:
explicit CompactJsonWriter(Print* sink)
: JsonWriter(sink)
{
}
namespace ArduinoJson {
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

@ -7,20 +7,14 @@
#include "../Arduino/Print.hpp"
namespace ArduinoJson
{
namespace Internals
{
// Decorator on top of Print to allow indented output.
// This class is used by JsonPrintable::prettyPrintTo() but can also be used
// for your own purpose, like logging.
class IndentedPrint : public Print
{
public:
IndentedPrint(Print& p)
: sink(&p)
{
namespace ArduinoJson {
namespace Internals {
// Decorator on top of Print to allow indented output.
// This class is used by JsonPrintable::prettyPrintTo() but can also be used
// for your own purpose, like logging.
class IndentedPrint : public Print {
public:
IndentedPrint(Print &p) : sink(&p) {
level = 0;
tabSize = 2;
isNewLine = true;
@ -37,8 +31,8 @@ namespace ArduinoJson
// Set the number of space printed for each level of indentation
void setTabSize(uint8_t n);
private:
Print* sink;
private:
Print *sink;
uint8_t level : 4;
uint8_t tabSize : 3;
bool isNewLine : 1;
@ -47,7 +41,6 @@ namespace ArduinoJson
static const int MAX_LEVEL = 15; // because it's only 4 bits
static const int MAX_TAB_SIZE = 7; // because it's only 3 bits
};
}
};
}
}

View File

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

View File

@ -2,42 +2,25 @@
#include "JsonNode.hpp"
namespace ArduinoJson
{
namespace Internals
{
// TODO: replace by JsonArrayIterator and JsonObjectIterator
class JsonNodeIterator
{
public:
namespace ArduinoJson {
namespace Internals {
// TODO: replace by JsonArrayIterator and JsonObjectIterator
class JsonNodeIterator {
public:
explicit JsonNodeIterator(JsonNode *node) : _node(node) {}
explicit JsonNodeIterator(JsonNode* node)
: _node(node)
{
}
bool operator!= (const JsonNodeIterator& other) const
{
bool operator!=(const JsonNodeIterator &other) const {
return _node != other._node;
}
void operator++()
{
_node = _node->next;
}
void operator++() { _node = _node->next; }
JsonNode* operator*() const
{
return _node;
}
JsonNode *operator*() const { return _node; }
JsonNode* operator->() const
{
return _node;
}
JsonNode *operator->() const { return _node; }
private:
JsonNode* _node;
};
}
private:
JsonNode *_node;
};
}
}

View File

@ -2,42 +2,28 @@
#include "JsonNode.hpp"
namespace ArduinoJson
{
class JsonValue ;
namespace ArduinoJson {
class JsonValue;
namespace Internals
{
class JsonNodeWrapper
{
namespace Internals {
class JsonNodeWrapper {
friend class JsonValue;
public:
JsonNodeWrapper()
: _node(0)
{
}
public:
JsonNodeWrapper() : _node(0) {}
explicit JsonNodeWrapper(JsonNode* node)
: _node(node)
{
}
explicit JsonNodeWrapper(JsonNode *node) : _node(node) {}
protected:
void duplicate(const JsonNodeWrapper& other)
{
if (!_node)
{
protected:
void duplicate(const JsonNodeWrapper &other) {
if (!_node) {
_node = other._node;
}
else
{
} else {
_node->duplicate(other._node);
}
}
JsonNode* _node;
};
}
JsonNode *_node;
};
}
}

View File

@ -2,44 +2,34 @@
#include "JsonNode.hpp"
namespace ArduinoJson
{
class JsonBuffer;
namespace ArduinoJson {
class JsonBuffer;
namespace Internals
{
class JsonNode;
namespace Internals {
class JsonNode;
class JsonParser
{
public:
JsonParser(JsonBuffer* buffer, char* json)
: _buffer(buffer), _ptr(json)
{
class JsonParser {
public:
JsonParser(JsonBuffer *buffer, char *json) : _buffer(buffer), _ptr(json) {}
}
JsonNode *parseAnything();
JsonNode* parseAnything();
private:
JsonBuffer *_buffer;
char *_ptr;
private:
JsonBuffer* _buffer;
char* _ptr;
bool isEnd()
{
return *_ptr == 0;
}
bool isEnd() { return *_ptr == 0; }
bool skip(char charToSkip);
void skipSpaces();
inline JsonNode* parseArray();
inline JsonNode* parseBoolean();
inline JsonNode* parseNull();
inline JsonNode* parseNumber();
inline JsonNode* parseObject();
inline JsonNode* parseObjectKeyValue();
inline JsonNode* parseString();
};
}
inline JsonNode *parseArray();
inline JsonNode *parseBoolean();
inline JsonNode *parseNull();
inline JsonNode *parseNumber();
inline JsonNode *parseObject();
inline JsonNode *parseObjectKeyValue();
inline JsonNode *parseString();
};
}
}

View File

@ -2,22 +2,13 @@
#include "../Arduino/Print.hpp"
namespace ArduinoJson
{
namespace Internals
{
class JsonWriter
{
public:
explicit JsonWriter(Print* sink)
: _sink(sink), _length(0)
{
}
namespace ArduinoJson {
namespace Internals {
class JsonWriter {
public:
explicit JsonWriter(Print *sink) : _sink(sink), _length(0) {}
size_t bytesWritten()
{
return _length;
}
size_t bytesWritten() { return _length; }
virtual void beginArray() = 0;
@ -27,7 +18,7 @@ namespace ArduinoJson
virtual void endObject() = 0;
void writeString(const char* value);
void writeString(const char *value);
void writeInteger(long value);
void writeBoolean(bool value);
void writeDouble(double value, int decimals);
@ -36,19 +27,13 @@ namespace ArduinoJson
virtual void writeComma() = 0;
void writeEmptyArray()
{
_length += _sink->print("[]");
}
void writeEmptyArray() { _length += _sink->print("[]"); }
void writeEmptyObject()
{
_length += _sink->print("{}");
}
void writeEmptyObject() { _length += _sink->print("{}"); }
protected:
Print* _sink;
protected:
Print *_sink;
size_t _length;
};
}
};
}
}

View File

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

View File

@ -7,16 +7,13 @@
#include "../Arduino/Print.hpp"
namespace ArduinoJson
{
namespace Internals
{
class QuotedString
{
public:
static size_t printTo(const char*, Print*);
namespace ArduinoJson {
namespace Internals {
class QuotedString {
public:
static size_t printTo(const char *, Print *);
static char* extractFrom(char* input, char** end);
};
}
static char *extractFrom(char *input, char **end);
};
}
}

View File

@ -7,25 +7,21 @@
#include "../Arduino/Print.hpp"
namespace ArduinoJson
{
namespace Internals
{
class StringBuilder : public Print
{
public:
StringBuilder(char* buf, int size)
: buffer(buf), capacity(size - 1), length(0)
{
namespace ArduinoJson {
namespace Internals {
class StringBuilder : public Print {
public:
StringBuilder(char *buf, int size)
: buffer(buf), capacity(size - 1), length(0) {
buffer[0] = 0;
}
virtual size_t write(uint8_t c);
private:
char* buffer;
private:
char *buffer;
int capacity;
int length;
};
}
};
}
}

View File

@ -3,25 +3,18 @@
#include "JsonContainer.hpp"
#include "JsonArrayIterator.hpp"
namespace ArduinoJson
{
class JsonArray : public JsonContainer
{
public:
JsonArray()
{
}
namespace ArduinoJson {
class JsonArray : public JsonContainer {
public:
JsonArray() {}
explicit JsonArray(Internals::JsonNode* node)
: JsonContainer(node)
{
}
explicit JsonArray(Internals::JsonNode *node) : JsonContainer(node) {}
JsonValue operator[](int index) const;
void add(bool value);
void add(const char* value);
void add(double value, int decimals=2);
void add(const char *value);
void add(double value, int decimals = 2);
void add(int value) { add(static_cast<long>(value)); }
void add(long value);
void add(JsonContainer nestedArray); // TODO: should allow JsonValue too
@ -29,16 +22,10 @@ namespace ArduinoJson
JsonArray createNestedArray();
JsonObject createNestedObject();
bool success()
{
return _node && _node->isArray();
}
bool success() { return _node && _node->isArray(); }
JsonArrayIterator begin();
JsonArrayIterator end()
{
return JsonArrayIterator(0);
}
};
JsonArrayIterator end() { return JsonArrayIterator(0); }
};
}

View File

@ -2,41 +2,28 @@
#include "ArduinoJson/JsonValue.hpp"
namespace ArduinoJson
{
class JsonArray;
namespace ArduinoJson {
class JsonArray;
class JsonArrayIterator
{
class JsonArrayIterator {
friend class JsonArray;
public:
explicit JsonArrayIterator(Internals::JsonNode* node)
: _node(node)
{
}
public:
explicit JsonArrayIterator(Internals::JsonNode *node) : _node(node) {}
void operator++()
{
_node = _node->next;
}
void operator++() { _node = _node->next; }
JsonValue operator*() const
{
return JsonValue(_node);
}
JsonValue operator*() const { return JsonValue(_node); }
bool operator==(const JsonArrayIterator& other) const
{
bool operator==(const JsonArrayIterator &other) const {
return _node == other._node;
}
bool operator!=(const JsonArrayIterator& other) const
{
bool operator!=(const JsonArrayIterator &other) const {
return _node != other._node;
}
private:
Internals::JsonNode* _node;
};
private:
Internals::JsonNode *_node;
};
}

View File

@ -3,50 +3,42 @@
#include "JsonArray.hpp"
#include "JsonObject.hpp"
namespace ArduinoJson
{
namespace Internals
{
class JsonParser;
}
namespace ArduinoJson {
namespace Internals {
class JsonParser;
}
class JsonBuffer
{
class JsonBuffer {
friend class JsonContainer;
friend class Internals::JsonNode;
friend class Internals::JsonParser;
public:
virtual ~JsonBuffer() {};
public:
virtual ~JsonBuffer(){};
JsonArray createArray()
{
return JsonArray(createArrayNode());
}
JsonArray createArray() { return JsonArray(createArrayNode()); }
JsonObject createObject()
{
return JsonObject(createObjectNode());
}
JsonObject createObject() { return JsonObject(createObjectNode()); }
JsonValue createValue();
JsonArray parseArray(char* json);
JsonObject parseObject(char* json);
JsonValue parseValue(char* json); // TODO: remove
JsonArray parseArray(char *json);
JsonObject parseObject(char *json);
JsonValue parseValue(char *json); // TODO: remove
protected:
virtual void* allocateNode() = 0;
protected:
virtual void *allocateNode() = 0;
private:
Internals::JsonNode* createNode();
private:
Internals::JsonNode *createNode();
Internals::JsonNode* createArrayNode();
Internals::JsonNode* createBoolNode(bool value);
Internals::JsonNode* createDoubleNode(double value, int decimals);
Internals::JsonNode* createLongNode(long value);
Internals::JsonNode* createObjectNode();
Internals::JsonNode* createObjectKeyValueNode(const char* key, Internals::JsonNode* value);
Internals::JsonNode* createStringNode(const char* value);
};
Internals::JsonNode *createArrayNode();
Internals::JsonNode *createBoolNode(bool value);
Internals::JsonNode *createDoubleNode(double value, int decimals);
Internals::JsonNode *createLongNode(long value);
Internals::JsonNode *createObjectNode();
Internals::JsonNode *createObjectKeyValueNode(const char *key,
Internals::JsonNode *value);
Internals::JsonNode *createStringNode(const char *value);
};
}

View File

@ -6,49 +6,41 @@
#include "Internals/IndentedPrint.hpp"
#include "Internals/JsonNodeWrapper.hpp"
namespace ArduinoJson
{
class JsonArray;
class JsonObject;
class JsonValue;
namespace ArduinoJson {
class JsonArray;
class JsonObject;
class JsonValue;
class JsonContainer : public Printable, public Internals::JsonNodeWrapper
{
class JsonContainer : public Printable, public Internals::JsonNodeWrapper {
friend class JsonArray;
public:
public:
JsonContainer() {}
explicit JsonContainer(Internals::JsonNode* node)
: JsonNodeWrapper(node)
{
}
explicit JsonContainer(Internals::JsonNode *node) : JsonNodeWrapper(node) {}
size_t size() const;
bool operator==(JsonContainer const& other) const;
bool operator==(JsonContainer const &other) const;
size_t printTo(char* buffer, size_t bufferSize) const;
virtual size_t printTo(Print& print) const;
size_t printTo(char *buffer, size_t bufferSize) const;
virtual size_t printTo(Print &print) const;
size_t prettyPrintTo(char* buffer, size_t bufferSize) const;
size_t prettyPrintTo(ArduinoJson::Internals::IndentedPrint& print) const;
size_t prettyPrintTo(Print& print) const;
size_t prettyPrintTo(char *buffer, size_t bufferSize) const;
size_t prettyPrintTo(ArduinoJson::Internals::IndentedPrint &print) const;
size_t prettyPrintTo(Print &print) const;
protected:
Internals::JsonNodeIterator beginChildren() const
{
protected:
Internals::JsonNodeIterator beginChildren() const {
return Internals::JsonNodeIterator(_node ? _node->getContainerChild() : 0);
}
Internals::JsonNodeIterator endChildren() const
{
Internals::JsonNodeIterator endChildren() const {
return Internals::JsonNodeIterator(0);
}
void addChild(Internals::JsonNode*);
void removeChild(Internals::JsonNode*);
Internals::JsonNode* createNode();
};
void addChild(Internals::JsonNode *);
void removeChild(Internals::JsonNode *);
Internals::JsonNode *createNode();
};
}

View File

@ -3,40 +3,26 @@
#include "ArduinoJson/JsonContainer.hpp"
#include "ArduinoJson/JsonObjectIterator.hpp"
namespace ArduinoJson
{
class JsonObject : public JsonContainer
{
public:
namespace ArduinoJson {
class JsonObject : public JsonContainer {
public:
JsonObject() {}
JsonObject()
{
}
explicit JsonObject(Internals::JsonNode *node) : JsonContainer(node) {}
explicit JsonObject(Internals::JsonNode* node)
: JsonContainer(node)
{
}
JsonValue operator[](const char *key);
void remove(const char *key);
JsonValue operator[](const char* key);
void remove(const char* key);
JsonArray createNestedArray(const char *key);
JsonObject createNestedObject(const char *key);
JsonArray createNestedArray(const char* key);
JsonObject createNestedObject(const char* key);
bool success()
{
return _node && _node->isObject();
}
bool success() { return _node && _node->isObject(); }
JsonObjectIterator begin();
JsonObjectIterator end()
{
return JsonObjectIterator(0);
}
JsonObjectIterator end() { return JsonObjectIterator(0); }
private:
Internals::JsonNode* getOrCreateNodeAt(const char* key);
};
private:
Internals::JsonNode *getOrCreateNodeAt(const char *key);
};
}

View File

@ -2,47 +2,34 @@
#include "ArduinoJson/JsonObjectKeyValue.hpp"
namespace ArduinoJson
{
class JsonObject;
namespace ArduinoJson {
class JsonObject;
class JsonObjectIterator
{
class JsonObjectIterator {
friend class JsonObject;
public:
explicit JsonObjectIterator(Internals::JsonNode* node)
: _objectKeyValue(node)
{
}
public:
explicit JsonObjectIterator(Internals::JsonNode *node)
: _objectKeyValue(node) {}
JsonObjectIterator& operator++()
{
JsonObjectIterator &operator++() {
_objectKeyValue = JsonObjectKeyValue(_objectKeyValue.next());
return *this;
}
JsonObjectKeyValue operator*() const
{
return _objectKeyValue;
}
JsonObjectKeyValue operator*() const { return _objectKeyValue; }
JsonObjectKeyValue* operator->()
{
return &_objectKeyValue;
}
JsonObjectKeyValue *operator->() { return &_objectKeyValue; }
bool operator==(const JsonObjectIterator& other) const
{
bool operator==(const JsonObjectIterator &other) const {
return _objectKeyValue == other._objectKeyValue;
}
bool operator!=(const JsonObjectIterator& other) const
{
bool operator!=(const JsonObjectIterator &other) const {
return _objectKeyValue != other._objectKeyValue;
}
private:
private:
JsonObjectKeyValue _objectKeyValue;
};
};
}

View File

@ -2,42 +2,26 @@
#include "ArduinoJson/JsonValue.hpp"
namespace ArduinoJson
{
class JsonObjectKeyValue
{
public:
explicit JsonObjectKeyValue(Internals::JsonNode* node)
: _node(node)
{
}
namespace ArduinoJson {
class JsonObjectKeyValue {
public:
explicit JsonObjectKeyValue(Internals::JsonNode *node) : _node(node) {}
const char* key() const
{
return _node->getAsObjectKey();
}
const char *key() const { return _node->getAsObjectKey(); }
JsonValue value()
{
return JsonValue(_node->getAsObjectValue());
}
JsonValue value() { return JsonValue(_node->getAsObjectValue()); }
bool operator==(const JsonObjectKeyValue& other) const
{
bool operator==(const JsonObjectKeyValue &other) const {
return _node == other._node;
}
bool operator!=(const JsonObjectKeyValue& other) const
{
bool operator!=(const JsonObjectKeyValue &other) const {
return _node != other._node;
}
Internals::JsonNode* next()
{
return _node->next;
}
Internals::JsonNode *next() { return _node->next; }
private:
Internals::JsonNode* _node;
};
private:
Internals::JsonNode *_node;
};
}

View File

@ -2,32 +2,28 @@
#include "Internals/JsonNodeWrapper.hpp"
namespace ArduinoJson
{
class JsonArray;
class JsonContainer;
class JsonObject;
class JsonValue : public Internals::JsonNodeWrapper
{
public:
namespace ArduinoJson {
class JsonArray;
class JsonContainer;
class JsonObject;
class JsonValue : public Internals::JsonNodeWrapper {
public:
JsonValue() {}
explicit JsonValue(Internals::JsonNode* node)
: JsonNodeWrapper(node)
{
}
explicit JsonValue(Internals::JsonNode *node) : JsonNodeWrapper(node) {}
void operator=(bool);
void operator=(const char*);
void operator=(const char *);
void operator=(double x) { set(x, 2); }
void operator=(int);
void operator=(const JsonValue& value) { duplicate(value); }
void operator=(const Internals::JsonNodeWrapper& object) { duplicate(object); }
void operator=(const JsonValue &value) { duplicate(value); }
void operator=(const Internals::JsonNodeWrapper &object) {
duplicate(object);
}
operator bool() const;
operator const char*() const;
operator const char *() const;
operator double() const;
operator long() const;
operator int() const { return operator long(); }
@ -36,10 +32,6 @@ namespace ArduinoJson
void set(double value, int decimals);
template<typename T>
T as()
{
return static_cast<T>(*this);
}
};
template <typename T> T as() { return static_cast<T>(*this); }
};
}

View File

@ -3,42 +3,29 @@
#include "JsonBuffer.hpp"
#include "JsonObject.hpp"
namespace ArduinoJson
{
template<int CAPACITY>
class StaticJsonBuffer : public JsonBuffer
{
namespace ArduinoJson {
template <int CAPACITY> class StaticJsonBuffer : public JsonBuffer {
friend class JsonObject;
public:
explicit StaticJsonBuffer()
: _size(0)
{
}
public:
explicit StaticJsonBuffer() : _size(0) {}
virtual ~StaticJsonBuffer() {}
int capacity()
{
return CAPACITY;
}
int capacity() { return CAPACITY; }
int size()
{
return _size;
}
int size() { return _size; }
protected:
virtual void* allocateNode()
{
if (_size >= CAPACITY) return 0;
protected:
virtual void *allocateNode() {
if (_size >= CAPACITY)
return 0;
return &_buffer[_size++];
}
private:
private:
Internals::JsonNode _buffer[CAPACITY];
int _size;
};
};
}

1
scripts/format-code.sh Normal file
View File

@ -0,0 +1 @@
find .. -regex ".*\.[ch]pp$" -exec clang-format -i {} \;

View File

@ -8,33 +8,26 @@
#include "ArduinoJson/Arduino/Print.hpp"
#include <cstdio>
size_t Print::print(const char s[])
{
size_t Print::print(const char s[]) {
size_t n = 0;
while (*s)
{
while (*s) {
n += write(*s++);
}
return n;
}
size_t Print::print(double value, int digits)
{
size_t Print::print(double value, int digits) {
char tmp[32];
sprintf(tmp, "%.*g", digits+1, value);
sprintf(tmp, "%.*g", digits + 1, value);
return print(tmp);
}
size_t Print::print(long value)
{
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');
}
size_t Print::println() { return write('\r') + write('\n'); }
#endif

View File

@ -2,26 +2,22 @@
using namespace ArduinoJson::Internals;
void IndentedPrint::indent()
{
void IndentedPrint::indent() {
if (level < MAX_LEVEL)
level++;
}
void IndentedPrint::unindent()
{
void IndentedPrint::unindent() {
if (level > 0)
level--;
}
void IndentedPrint::setTabSize(uint8_t n)
{
void IndentedPrint::setTabSize(uint8_t n) {
if (n < MAX_TAB_SIZE)
tabSize = n;
}
size_t IndentedPrint::write(uint8_t c)
{
size_t IndentedPrint::write(uint8_t c) {
size_t n = 0;
if (isNewLine)
@ -34,11 +30,10 @@ size_t IndentedPrint::write(uint8_t c)
return n;
}
inline size_t IndentedPrint::writeTabs()
{
inline size_t IndentedPrint::writeTabs() {
size_t n = 0;
for (int i = 0; i < level*tabSize; i++)
for (int i = 0; i < level * tabSize; i++)
n += sink->write(' ');
return n;

View File

@ -7,10 +7,8 @@
using namespace ArduinoJson::Internals;
void JsonNode::writeTo(JsonWriter& writer)
{
switch (type)
{
void JsonNode::writeTo(JsonWriter &writer) {
switch (type) {
case JSON_PROXY:
content.asProxy.target->writeTo(writer);
break;
@ -41,18 +39,16 @@ void JsonNode::writeTo(JsonWriter& writer)
}
}
void JsonNode::addChild(JsonNode* childToAdd)
{
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;
JsonNode *lastChild = content.asContainer.child;
if (!lastChild)
{
if (!lastChild) {
content.asContainer.child = childToAdd;
return;
}
@ -63,107 +59,92 @@ void JsonNode::addChild(JsonNode* childToAdd)
lastChild->next = childToAdd;
}
void JsonNode::removeChild(JsonNode* childToRemove)
{
void JsonNode::removeChild(JsonNode *childToRemove) {
if (type == JSON_PROXY)
return content.asProxy.target->removeChild(childToRemove);
if (type != JSON_ARRAY && type != JSON_OBJECT) return;
if (type != JSON_ARRAY && type != JSON_OBJECT)
return;
if (content.asContainer.child == childToRemove)
{
if (content.asContainer.child == childToRemove) {
content.asContainer.child = childToRemove->next;
return;
}
for (JsonNode* child = content.asContainer.child; child; child = child->next)
{
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;
void JsonNode::writeArrayTo(JsonWriter &writer) {
JsonNode *child = content.asContainer.child;
if (child)
{
if (child) {
writer.beginArray();
for (;;)
{
for (;;) {
child->writeTo(writer);
child = child->next;
if (!child) break;
if (!child)
break;
writer.writeComma();
}
writer.endArray();
}
else
{
} else {
writer.writeEmptyArray();
}
}
void JsonNode::writeObjectTo(JsonWriter& writer)
{
JsonNode* child = content.asContainer.child;
void JsonNode::writeObjectTo(JsonWriter &writer) {
JsonNode *child = content.asContainer.child;
if (child)
{
if (child) {
writer.beginObject();
for (;;)
{
for (;;) {
writer.writeString(child->content.asKeyValue.key);
writer.writeColon();
child->content.asKeyValue.value->writeTo(writer);
child = child->next;
if (!child) break;
if (!child)
break;
writer.writeComma();
}
writer.endObject();
}
else
{
} else {
writer.writeEmptyObject();
}
}
void JsonNode::setAsProxyOfSelf()
{
JsonBuffer* buffer = content.asContainer.buffer;
if (!buffer) return;
void JsonNode::setAsProxyOfSelf() {
JsonBuffer *buffer = content.asContainer.buffer;
if (!buffer)
return;
JsonNode* newNode = buffer->createNode();
if (!newNode) return;
JsonNode *newNode = buffer->createNode();
if (!newNode)
return;
*newNode = *this;
setAsProxyOf(newNode);
}
void JsonNode::duplicate(JsonNode* other)
{
if (!other)
{
void JsonNode::duplicate(JsonNode *other) {
if (!other) {
type = JSON_UNDEFINED;
}
else if (other->type == JSON_ARRAY || other->type==JSON_OBJECT)
{
} else if (other->type == JSON_ARRAY || other->type == JSON_OBJECT) {
other->setAsProxyOfSelf();
setAsProxyOf(other->content.asProxy.target);
}
else
{
} else {
*this = *other;
}
}

View File

@ -8,26 +8,24 @@
using namespace ArduinoJson::Internals;
void JsonParser::skipSpaces()
{
while(isspace(*_ptr)) _ptr++;
void JsonParser::skipSpaces() {
while (isspace(*_ptr))
_ptr++;
}
bool JsonParser::skip(char charToSkip)
{
bool JsonParser::skip(char charToSkip) {
skipSpaces();
if (*_ptr != charToSkip) return false;
if (*_ptr != charToSkip)
return false;
_ptr++;
skipSpaces();
return true;
}
JsonNode* JsonParser::parseAnything()
{
JsonNode *JsonParser::parseAnything() {
skipSpaces();
switch(*_ptr)
{
switch (*_ptr) {
case '[':
return parseArray();
@ -64,9 +62,8 @@ JsonNode* JsonParser::parseAnything()
}
}
JsonNode* JsonParser::parseArray()
{
JsonNode* node = _buffer->createArrayNode();
JsonNode *JsonParser::parseArray() {
JsonNode *node = _buffer->createArrayNode();
skip('[');
@ -76,9 +73,8 @@ JsonNode* JsonParser::parseArray()
if (skip(']'))
return node; // empty array
for(;;)
{
JsonNode* child = parseAnything();
for (;;) {
JsonNode *child = parseAnything();
if (!child)
return 0; // child parsing failed
@ -93,8 +89,7 @@ JsonNode* JsonParser::parseArray()
}
}
JsonNode *JsonParser::parseBoolean()
{
JsonNode *JsonParser::parseBoolean() {
bool value = *_ptr == 't';
_ptr += value ? 4 : 5;
@ -104,9 +99,8 @@ JsonNode *JsonParser::parseBoolean()
return _buffer->createBoolNode(value);
}
JsonNode* JsonParser::parseNumber()
{
char* endOfLong;
JsonNode *JsonParser::parseNumber() {
char *endOfLong;
long longValue = strtol(_ptr, &endOfLong, 10);
if (*endOfLong == '.') // stopped on a decimal separator
@ -114,24 +108,20 @@ JsonNode* JsonParser::parseNumber()
double value = strtod(_ptr, &_ptr);
int decimals = _ptr - endOfLong - 1;
return _buffer->createDoubleNode(value, decimals);
}
else
{
} else {
_ptr = endOfLong;
return _buffer->createLongNode(longValue);
}
}
JsonNode* JsonParser::parseNull()
{
JsonNode *JsonParser::parseNull() {
_ptr += 4; // strlen("null")
return _buffer->createStringNode(0);
}
JsonNode* JsonParser::parseObject()
{
JsonNode* node = _buffer->createObjectNode();
JsonNode *JsonParser::parseObject() {
JsonNode *node = _buffer->createObjectNode();
skip('{');
@ -141,9 +131,8 @@ JsonNode* JsonParser::parseObject()
if (skip('}'))
return node; // empty object
for(;;)
{
JsonNode* child = parseObjectKeyValue();
for (;;) {
JsonNode *child = parseObjectKeyValue();
if (!child)
return 0; // child parsing failed
@ -158,9 +147,8 @@ JsonNode* JsonParser::parseObject()
}
}
JsonNode* JsonParser::parseObjectKeyValue()
{
const char* key = QuotedString::extractFrom(_ptr, &_ptr);
JsonNode *JsonParser::parseObjectKeyValue() {
const char *key = QuotedString::extractFrom(_ptr, &_ptr);
if (!key)
return 0; // failed to extract key
@ -168,7 +156,7 @@ JsonNode* JsonParser::parseObjectKeyValue()
if (!skip(':'))
return 0; // colon is missing
JsonNode* value = parseAnything();
JsonNode *value = parseAnything();
if (!value)
return 0; // value parsing failed
@ -176,8 +164,7 @@ JsonNode* JsonParser::parseObjectKeyValue()
return _buffer->createObjectKeyValueNode(key, value);
}
JsonNode* JsonParser::parseString()
{
const char* s = QuotedString::extractFrom(_ptr, &_ptr);
JsonNode *JsonParser::parseString() {
const char *s = QuotedString::extractFrom(_ptr, &_ptr);
return _buffer->createStringNode(s);
}

View File

@ -3,22 +3,16 @@
using namespace ArduinoJson::Internals;
void JsonWriter::writeString(char const* value)
{
void JsonWriter::writeString(char const *value) {
_length += QuotedString::printTo(value, _sink);
}
void JsonWriter::writeInteger(long value)
{
_length += _sink->print(value);
}
void JsonWriter::writeInteger(long value) { _length += _sink->print(value); }
void JsonWriter::writeBoolean(bool value)
{
void JsonWriter::writeBoolean(bool value) {
_length += _sink->print(value ? "true" : "false");
}
void JsonWriter::writeDouble(double value, int decimals)
{
void JsonWriter::writeDouble(double value, int decimals) {
_length += _sink->print(value, decimals);
}

View File

@ -7,97 +7,83 @@
using namespace ArduinoJson::Internals;
static inline char getSpecialChar(char c)
{
static inline char getSpecialChar(char c) {
// Optimized for code size on a 8-bit AVR
const char* p = "\"\"\\\\\bb\ff\nn\rr\tt\0";
const char *p = "\"\"\\\\\bb\ff\nn\rr\tt\0";
while (p[0] && p[0] != c)
{
while (p[0] && p[0] != c) {
p += 2;
}
return p[1];
}
static inline size_t printCharTo(char c, Print* p)
{
static inline size_t printCharTo(char c, Print *p) {
char specialChar = getSpecialChar(c);
return specialChar != 0
? p->write('\\') + p->write(specialChar)
return specialChar != 0 ? p->write('\\') + p->write(specialChar)
: p->write(c);
}
size_t QuotedString::printTo(const char* s, Print* p)
{
if (!s) return p->print("null");
size_t QuotedString::printTo(const char *s, Print *p) {
if (!s)
return p->print("null");
size_t n = p->write('\"');
while (*s)
{
while (*s) {
n += printCharTo(*s++, p);
}
return n + p->write('\"');
}
static char unescapeChar(char c)
{
static char unescapeChar(char c) {
// Optimized for code size on a 8-bit AVR
const char* p = "b\bf\fn\nr\rt\t";
const char *p = "b\bf\fn\nr\rt\t";
for (;;)
{
if (p[0] == 0) return c;
if (p[0] == c) return p[1];
for (;;) {
if (p[0] == 0)
return c;
if (p[0] == c)
return p[1];
p += 2;
}
}
static inline bool isQuote(char c)
{
return c == '\"' || c == '\'';
}
static inline bool isQuote(char c) { return c == '\"' || c == '\''; }
char* QuotedString::extractFrom(char* input, char** endPtr)
{
char *QuotedString::extractFrom(char *input, char **endPtr) {
char firstChar = *input;
if (!isQuote(firstChar))
{
if (!isQuote(firstChar)) {
// must start with a quote
return 0;
}
char stopChar = firstChar; // closing quote is the same as opening quote
char* startPtr = input + 1; // skip the quote
char* readPtr = startPtr;
char* writePtr = startPtr;
char *startPtr = input + 1; // skip the quote
char *readPtr = startPtr;
char *writePtr = startPtr;
char c;
for (;;)
{
for (;;) {
c = *readPtr++;
if (c == 0)
{
if (c == 0) {
// premature ending
return 0;
}
if (c == stopChar)
{
if (c == stopChar) {
// closing quote
break;
}
if (c == '\\')
{
if (c == '\\') {
// replace char
c = unescapeChar(*readPtr++);
}

View File

@ -7,9 +7,9 @@
using namespace ArduinoJson::Internals;
size_t StringBuilder::write(uint8_t c)
{
if (length >= capacity) return 0;
size_t StringBuilder::write(uint8_t c) {
if (length >= capacity)
return 0;
buffer[length++] = c;
buffer[length] = 0;

View File

@ -5,69 +5,66 @@
using namespace ArduinoJson;
using namespace ArduinoJson::Internals;
JsonValue JsonArray::operator[](int index) const
{
for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it)
{
if (!index) return JsonValue(*it);
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;
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;
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;
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;
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;
void JsonArray::add(JsonContainer nestedContainer) {
JsonNode *node = createNode();
if (!node)
return;
node->duplicate(nestedContainer._node);
addChild(node);
}
JsonArray JsonArray::createNestedArray()
{
JsonNode* node = createNode();
JsonArray JsonArray::createNestedArray() {
JsonNode *node = createNode();
if (node)
{
if (node) {
node->setAsArray(_node->getContainerBuffer());
addChild(node);
}
@ -75,12 +72,10 @@ JsonArray JsonArray::createNestedArray()
return JsonArray(node);
}
JsonObject JsonArray::createNestedObject()
{
JsonNode* node = createNode();
JsonObject JsonArray::createNestedObject() {
JsonNode *node = createNode();
if (node)
{
if (node) {
node->setAsObject(_node->getContainerBuffer());
addChild(node);
}
@ -88,8 +83,7 @@ JsonObject JsonArray::createNestedObject()
return JsonObject(node);
}
JsonArrayIterator JsonArray::begin()
{
JsonArrayIterator JsonArray::begin() {
if (!_node)
return end();

View File

@ -9,40 +9,33 @@
using namespace ArduinoJson;
using namespace ArduinoJson::Internals;
JsonValue JsonBuffer::createValue()
{
return JsonValue(createNode());
}
JsonValue JsonBuffer::createValue() { return JsonValue(createNode()); }
JsonNode* JsonBuffer::createNode()
{
void* node = allocateNode();
if (!node) return 0;
JsonNode *JsonBuffer::createNode() {
void *node = allocateNode();
if (!node)
return 0;
return new (node) JsonNode();
}
JsonArray JsonBuffer::parseArray(char* json)
{
JsonArray JsonBuffer::parseArray(char *json) {
JsonParser parser(this, json);
return JsonArray(parser.parseAnything());
}
JsonObject JsonBuffer::parseObject(char* json)
{
JsonObject JsonBuffer::parseObject(char *json) {
JsonParser parser(this, json);
return JsonObject(parser.parseAnything());
}
JsonValue JsonBuffer::parseValue(char* json)
{
JsonValue JsonBuffer::parseValue(char *json) {
JsonParser parser(this, json);
return JsonValue(parser.parseAnything());
}
JsonNode* JsonBuffer::createArrayNode()
{
JsonNode* node = createNode();
JsonNode *JsonBuffer::createArrayNode() {
JsonNode *node = createNode();
if (node)
node->setAsArray(this);
@ -50,9 +43,8 @@ JsonNode* JsonBuffer::createArrayNode()
return node;
}
JsonNode* JsonBuffer::createBoolNode(bool value)
{
JsonNode* node = createNode();
JsonNode *JsonBuffer::createBoolNode(bool value) {
JsonNode *node = createNode();
if (node)
node->setAsBoolean(value);
@ -60,9 +52,8 @@ JsonNode* JsonBuffer::createBoolNode(bool value)
return node;
}
JsonNode* JsonBuffer::createDoubleNode(double value, int decimals)
{
JsonNode* node = createNode();
JsonNode *JsonBuffer::createDoubleNode(double value, int decimals) {
JsonNode *node = createNode();
if (node)
node->setAsDouble(value, decimals);
@ -70,9 +61,8 @@ JsonNode* JsonBuffer::createDoubleNode(double value, int decimals)
return node;
}
JsonNode* JsonBuffer::createLongNode(long value)
{
JsonNode* node = createNode();
JsonNode *JsonBuffer::createLongNode(long value) {
JsonNode *node = createNode();
if (node)
node->setAsLong(value);
@ -80,9 +70,8 @@ JsonNode* JsonBuffer::createLongNode(long value)
return node;
}
JsonNode* JsonBuffer::createObjectNode()
{
JsonNode* node = createNode();
JsonNode *JsonBuffer::createObjectNode() {
JsonNode *node = createNode();
if (node)
node->setAsObject(this);
@ -90,9 +79,9 @@ JsonNode* JsonBuffer::createObjectNode()
return node;
}
Internals::JsonNode* JsonBuffer::createObjectKeyValueNode(const char* key, JsonNode* value)
{
JsonNode* node = createNode();
Internals::JsonNode *JsonBuffer::createObjectKeyValueNode(const char *key,
JsonNode *value) {
JsonNode *node = createNode();
if (node)
node->setAsObjectKeyValue(key, value);
@ -100,9 +89,8 @@ Internals::JsonNode* JsonBuffer::createObjectKeyValueNode(const char* key, JsonN
return node;
}
JsonNode* JsonBuffer::createStringNode(const char* value)
{
JsonNode* node = createNode();
JsonNode *JsonBuffer::createStringNode(const char *value) {
JsonNode *node = createNode();
if (node)
node->setAsString(value);

View File

@ -8,76 +8,68 @@
using namespace ArduinoJson;
using namespace ArduinoJson::Internals;
size_t JsonContainer::printTo(char* buffer, size_t bufferSize) const
{
size_t JsonContainer::printTo(char *buffer, size_t bufferSize) const {
StringBuilder sb(buffer, bufferSize);
return printTo(sb);
}
size_t JsonContainer::printTo(Print& p) const
{
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
{
size_t JsonContainer::prettyPrintTo(char *buffer, size_t bufferSize) const {
StringBuilder sb(buffer, bufferSize);
return prettyPrintTo(sb);
}
size_t JsonContainer::prettyPrintTo(IndentedPrint& p) const
{
size_t JsonContainer::prettyPrintTo(IndentedPrint &p) const {
PrettyJsonWriter writer(&p);
_node->writeTo(writer);
return writer.bytesWritten();
}
size_t JsonContainer::prettyPrintTo(Print& print) const
{
size_t JsonContainer::prettyPrintTo(Print &print) const {
IndentedPrint indentedPrint = IndentedPrint(print);
return prettyPrintTo(indentedPrint);
}
JsonNode* JsonContainer::createNode()
{
if (!_node) return 0;
JsonNode *JsonContainer::createNode() {
if (!_node)
return 0;
JsonBuffer* buffer = _node->getContainerBuffer();
if (!buffer) 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;
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)
{
void JsonContainer::addChild(JsonNode *childToAdd) {
if (_node)
_node->addChild(childToAdd);
}
void JsonContainer::removeChild(JsonNode* childToRemove)
{
void JsonContainer::removeChild(JsonNode *childToRemove) {
if (_node)
_node->removeChild(childToRemove);
}
size_t JsonContainer::size() const
{
size_t JsonContainer::size() const {
int n = 0;
for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it)
{
for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it) {
n++;
}
return n;
}

View File

@ -10,28 +10,23 @@
using namespace ArduinoJson;
using namespace ArduinoJson::Internals;
JsonValue JsonObject::operator[](char const* key)
{
JsonNode* node = getOrCreateNodeAt(key);
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();
void JsonObject::remove(char const *key) {
for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it) {
const char *childKey = it->getAsObjectKey();
if (!strcmp(childKey, key))
{
if (!strcmp(childKey, key)) {
removeChild(*it);
}
}
}
JsonArray JsonObject::createNestedArray(char const* key)
{
JsonNode* node = getOrCreateNodeAt(key);
JsonArray JsonObject::createNestedArray(char const *key) {
JsonNode *node = getOrCreateNodeAt(key);
if (node)
node->setAsArray(_node->getContainerBuffer());
@ -39,9 +34,8 @@ JsonArray JsonObject::createNestedArray(char const* key)
return JsonArray(node);
}
JsonObject JsonObject::createNestedObject(char const* key)
{
JsonNode* node = getOrCreateNodeAt(key);
JsonObject JsonObject::createNestedObject(char const *key) {
JsonNode *node = getOrCreateNodeAt(key);
if (node)
node->setAsObject(_node->getContainerBuffer());
@ -49,21 +43,21 @@ JsonObject JsonObject::createNestedObject(char const* key)
return JsonObject(node);
}
JsonNode* JsonObject::getOrCreateNodeAt(const char* key)
{
for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it)
{
const char* childKey = it->getAsObjectKey();
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 *newValueNode = createNode();
if (!newValueNode)
return 0;
JsonNode* newKeyNode = createNode();
if (!newKeyNode) return 0;
JsonNode *newKeyNode = createNode();
if (!newKeyNode)
return 0;
newKeyNode->setAsObjectKeyValue(key, newValueNode);
@ -72,7 +66,6 @@ JsonNode* JsonObject::getOrCreateNodeAt(const char* key)
return newValueNode;
}
JsonObjectIterator JsonObject::begin()
{
JsonObjectIterator JsonObject::begin() {
return JsonObjectIterator(_node->getContainerChild());
}

View File

@ -6,56 +6,38 @@
using namespace ArduinoJson;
void JsonValue::operator=(bool value)
{
void JsonValue::operator=(bool value) {
if (_node)
_node->setAsBoolean(value);
}
void JsonValue::operator=(char const* value)
{
void JsonValue::operator=(char const *value) {
if (_node)
_node->setAsString(value);
}
void JsonValue::set(double value, int decimals)
{
void JsonValue::set(double value, int decimals) {
if (_node)
_node->setAsDouble(value, decimals);
}
void JsonValue::operator=(int value)
{
void JsonValue::operator=(int value) {
if (_node)
_node->setAsLong(value);
}
JsonValue::operator bool() const
{
JsonValue::operator bool() const {
return _node ? _node->getAsBoolean() : false;
}
JsonValue::operator char const*() const
{
JsonValue::operator char const *() const {
return _node ? _node->getAsString() : 0;
}
JsonValue::operator double() const
{
return _node ? _node->getAsDouble() : 0;
}
JsonValue::operator double() const { return _node ? _node->getAsDouble() : 0; }
JsonValue::operator long() const
{
return _node ? _node->getAsInteger() : 0;
}
JsonValue::operator long() const { return _node ? _node->getAsInteger() : 0; }
JsonValue::operator JsonArray() const
{
return JsonArray(_node);
}
JsonValue::operator JsonArray() const { return JsonArray(_node); }
JsonValue::operator JsonObject() const
{
return JsonObject(_node);
}
JsonValue::operator JsonObject() const { return JsonObject(_node); }

View File

@ -6,18 +6,14 @@
using namespace ArduinoJson;
struct Person
{
struct Person {
int id;
char name[32];
};
class Issue10 : public testing::Test
{
class Issue10 : public testing::Test {
protected:
virtual void SetUp()
{
virtual void SetUp() {
Person boss;
boss.id = 1;
strcpy(boss.name, "Jeff");
@ -28,47 +24,41 @@ protected:
persons[1] = employee;
}
void checkJsonString(JsonContainer& p)
{
void checkJsonString(JsonContainer &p) {
char buffer[256];
p.printTo(buffer, sizeof(buffer));
EXPECT_STREQ("[{\"id\":1,\"name\":\"Jeff\"},{\"id\":2,\"name\":\"John\"}]", buffer);
EXPECT_STREQ("[{\"id\":1,\"name\":\"Jeff\"},{\"id\":2,\"name\":\"John\"}]",
buffer);
}
void nodeCountMustBe(int expected)
{
EXPECT_EQ(expected, json.size());
}
void nodeCountMustBe(int expected) { EXPECT_EQ(expected, json.size()); }
Person persons[2];
StaticJsonBuffer<20> json;
};
TEST_F(Issue10, PopulateArrayByAddingAnObject)
{
JsonArray array= json.createArray();
TEST_F(Issue10, PopulateArrayByAddingAnObject) {
JsonArray array = json.createArray();
for (int i = 0; i < 2; i++)
{
for (int i = 0; i < 2; i++) {
JsonObject object = json.createObject();
object["id"] = persons[i].id;
object["name"] = persons[i].name;
array.add(object); // <- adds a reference to an existing objet (creates 2 extra proxy nodes)
array.add(object); // <- adds a reference to an existing objet (creates 2
// extra proxy nodes)
}
checkJsonString(array);
nodeCountMustBe(15);
}
TEST_F(Issue10, PopulateArrayByCreatingNestedObjects)
{
TEST_F(Issue10, PopulateArrayByCreatingNestedObjects) {
JsonArray array = json.createArray();
for (int i = 0; i < 2; i++)
{
for (int i = 0; i < 2; i++) {
JsonObject object = array.createNestedObject();
object["id"] = persons[i].id;

View File

@ -4,55 +4,37 @@
using namespace ArduinoJson;
class JsonArray_Container_Tests : public ::testing::Test
{
class JsonArray_Container_Tests : public ::testing::Test {
protected:
virtual void SetUp()
{
array = json.createArray();
}
virtual void SetUp() { array = json.createArray(); }
void nodeCountMustBe(int expected)
{
EXPECT_EQ(expected, json.size());
}
void nodeCountMustBe(int expected) { EXPECT_EQ(expected, json.size()); }
template<typename T>
void firstElementMustBe(T expected)
{
template <typename T> void firstElementMustBe(T expected) {
elementAtIndexMustBe(0, expected);
}
template<typename T>
void secondElementMustBe(T expected)
{
template <typename T> void secondElementMustBe(T expected) {
elementAtIndexMustBe(1, expected);
}
void sizeMustBe(int expected)
{
EXPECT_EQ(expected, array.size());
}
void sizeMustBe(int expected) { EXPECT_EQ(expected, array.size()); }
StaticJsonBuffer<42> json;
JsonArray array;
private:
template<typename T>
void elementAtIndexMustBe(int index, T expected)
{
template <typename T> void elementAtIndexMustBe(int index, T expected) {
EXPECT_EQ(expected, array[index].as<T>());
}
};
TEST_F(JsonArray_Container_Tests, InitialSizeIsZero)
{
TEST_F(JsonArray_Container_Tests, InitialSizeIsZero) {
sizeMustBe(0);
nodeCountMustBe(1);
}
TEST_F(JsonArray_Container_Tests, Grow_WhenValuesAreAdded)
{
TEST_F(JsonArray_Container_Tests, Grow_WhenValuesAreAdded) {
array.add("hello");
sizeMustBe(1);
nodeCountMustBe(2);
@ -62,8 +44,7 @@ TEST_F(JsonArray_Container_Tests, Grow_WhenValuesAreAdded)
nodeCountMustBe(3);
}
TEST_F(JsonArray_Container_Tests, CanStoreIntegers)
{
TEST_F(JsonArray_Container_Tests, CanStoreIntegers) {
array.add(123);
array.add(456);
@ -72,8 +53,7 @@ TEST_F(JsonArray_Container_Tests, CanStoreIntegers)
nodeCountMustBe(3);
}
TEST_F(JsonArray_Container_Tests, CanStoreDoubles)
{
TEST_F(JsonArray_Container_Tests, CanStoreDoubles) {
array.add(123.45);
array.add(456.78);
@ -82,8 +62,7 @@ TEST_F(JsonArray_Container_Tests, CanStoreDoubles)
nodeCountMustBe(3);
}
TEST_F(JsonArray_Container_Tests, CanStoreBooleans)
{
TEST_F(JsonArray_Container_Tests, CanStoreBooleans) {
array.add(true);
array.add(false);
@ -92,10 +71,9 @@ TEST_F(JsonArray_Container_Tests, CanStoreBooleans)
nodeCountMustBe(3);
}
TEST_F(JsonArray_Container_Tests, CanStoreStrings)
{
const char* firstString = "h3110";
const char* secondString = "w0r1d";
TEST_F(JsonArray_Container_Tests, CanStoreStrings) {
const char *firstString = "h3110";
const char *secondString = "w0r1d";
array.add(firstString);
array.add(secondString);
@ -105,8 +83,7 @@ TEST_F(JsonArray_Container_Tests, CanStoreStrings)
nodeCountMustBe(3);
}
TEST_F(JsonArray_Container_Tests, CanStoreNestedArrays)
{
TEST_F(JsonArray_Container_Tests, CanStoreNestedArrays) {
JsonArray innerarray1 = json.createArray();
JsonArray innerarray2 = json.createArray();
@ -118,8 +95,7 @@ TEST_F(JsonArray_Container_Tests, CanStoreNestedArrays)
nodeCountMustBe(1 + 3 + 3);
}
TEST_F(JsonArray_Container_Tests, CanStoreNestedObjects)
{
TEST_F(JsonArray_Container_Tests, CanStoreNestedObjects) {
JsonObject innerObject1 = json.createObject();
JsonObject innerObject2 = json.createObject();
@ -131,8 +107,7 @@ TEST_F(JsonArray_Container_Tests, CanStoreNestedObjects)
nodeCountMustBe(1 + 3 + 3);
}
TEST_F(JsonArray_Container_Tests, CanCreateNestedArrays)
{
TEST_F(JsonArray_Container_Tests, CanCreateNestedArrays) {
JsonArray innerarray1 = array.createNestedArray();
JsonArray innerarray2 = array.createNestedArray();
@ -141,8 +116,7 @@ TEST_F(JsonArray_Container_Tests, CanCreateNestedArrays)
nodeCountMustBe(1 + 1 + 1);
}
TEST_F(JsonArray_Container_Tests, CanCreateNestedObjects)
{
TEST_F(JsonArray_Container_Tests, CanCreateNestedObjects) {
JsonObject innerObject1 = array.createNestedObject();
JsonObject innerObject2 = array.createNestedObject();

View File

@ -4,8 +4,7 @@
using namespace ArduinoJson;
TEST(JsonArray_Iterator_Test, SimpleTest)
{
TEST(JsonArray_Iterator_Test, SimpleTest) {
StaticJsonBuffer<42> jsonBuffer;
JsonArray array = jsonBuffer.createArray();

View File

@ -11,19 +11,14 @@
using namespace ArduinoJson;
class JsonArray_PrettyPrintTo_Tests : public testing::Test
{
class JsonArray_PrettyPrintTo_Tests : public testing::Test {
protected:
JsonArray array;
StaticJsonBuffer<30> json;
virtual void SetUp()
{
array = json.createArray();
}
virtual void SetUp() { array = json.createArray(); }
void outputMustBe(const char* expected)
{
void outputMustBe(const char *expected) {
size_t n = array.prettyPrintTo(buffer, sizeof(buffer));
EXPECT_STREQ(expected, buffer);
EXPECT_EQ(strlen(expected), n);
@ -33,47 +28,37 @@ private:
char buffer[256];
};
TEST_F(JsonArray_PrettyPrintTo_Tests, Empty)
{
outputMustBe("[]");
}
TEST_F(JsonArray_PrettyPrintTo_Tests, Empty) { outputMustBe("[]"); }
TEST_F(JsonArray_PrettyPrintTo_Tests, OneElement)
{
TEST_F(JsonArray_PrettyPrintTo_Tests, OneElement) {
array.add(1);
outputMustBe(
"[\r\n"
outputMustBe("[\r\n"
" 1\r\n"
"]");
}
TEST_F(JsonArray_PrettyPrintTo_Tests, TwoElements)
{
TEST_F(JsonArray_PrettyPrintTo_Tests, TwoElements) {
array.add(1);
array.add(2);
outputMustBe(
"[\r\n"
outputMustBe("[\r\n"
" 1,\r\n"
" 2\r\n"
"]");
}
TEST_F(JsonArray_PrettyPrintTo_Tests, EmptyNestedArrays)
{
TEST_F(JsonArray_PrettyPrintTo_Tests, EmptyNestedArrays) {
array.createNestedArray();
array.createNestedArray();
outputMustBe(
"[\r\n"
outputMustBe("[\r\n"
" [],\r\n"
" []\r\n"
"]");
}
TEST_F(JsonArray_PrettyPrintTo_Tests, NestedArrays)
{
TEST_F(JsonArray_PrettyPrintTo_Tests, NestedArrays) {
JsonArray nested1 = array.createNestedArray();
nested1.add(1);
nested1.add(2);
@ -81,8 +66,7 @@ TEST_F(JsonArray_PrettyPrintTo_Tests, NestedArrays)
JsonObject nested2 = array.createNestedObject();
nested2["key"] = 3;
outputMustBe(
"[\r\n"
outputMustBe("[\r\n"
" [\r\n"
" 1,\r\n"
" 2\r\n"

View File

@ -10,19 +10,14 @@
using namespace ArduinoJson;
class JsonArray_PrintTo_Tests : public testing::Test
{
class JsonArray_PrintTo_Tests : public testing::Test {
protected:
JsonArray array;
StaticJsonBuffer<3> json;
virtual void SetUp()
{
array = json.createArray();
}
virtual void SetUp() { array = json.createArray(); }
void outputMustBe(const char* expected)
{
void outputMustBe(const char *expected) {
size_t n = array.printTo(buffer, sizeof(buffer));
EXPECT_STREQ(expected, buffer);
EXPECT_EQ(strlen(expected), n);
@ -32,35 +27,28 @@ private:
char buffer[256];
};
TEST_F(JsonArray_PrintTo_Tests, Empty)
{
outputMustBe("[]");
}
TEST_F(JsonArray_PrintTo_Tests, Empty) { outputMustBe("[]"); }
TEST_F(JsonArray_PrintTo_Tests, Null)
{
array.add((char*) 0);
TEST_F(JsonArray_PrintTo_Tests, Null) {
array.add((char *)0);
outputMustBe("[null]");
}
TEST_F(JsonArray_PrintTo_Tests, OneString)
{
TEST_F(JsonArray_PrintTo_Tests, OneString) {
array.add("hello");
outputMustBe("[\"hello\"]");
}
TEST_F(JsonArray_PrintTo_Tests, TwoStrings)
{
TEST_F(JsonArray_PrintTo_Tests, TwoStrings) {
array.add("hello");
array.add("world");
outputMustBe("[\"hello\",\"world\"]");
}
TEST_F(JsonArray_PrintTo_Tests, OneStringOverCapacity)
{
TEST_F(JsonArray_PrintTo_Tests, OneStringOverCapacity) {
array.add("hello");
array.add("world");
array.add("lost");
@ -68,35 +56,30 @@ TEST_F(JsonArray_PrintTo_Tests, OneStringOverCapacity)
outputMustBe("[\"hello\",\"world\"]");
}
TEST_F(JsonArray_PrintTo_Tests, OneDoubleDefaultDigits)
{
TEST_F(JsonArray_PrintTo_Tests, OneDoubleDefaultDigits) {
array.add(3.14159265358979323846);
outputMustBe("[3.14]");
}
TEST_F(JsonArray_PrintTo_Tests, OneDoubleFourDigits)
{
TEST_F(JsonArray_PrintTo_Tests, OneDoubleFourDigits) {
array.add(3.14159265358979323846, 4);
outputMustBe("[3.1416]");
}
TEST_F(JsonArray_PrintTo_Tests, OneInteger)
{
TEST_F(JsonArray_PrintTo_Tests, OneInteger) {
array.add(1);
outputMustBe("[1]");
}
TEST_F(JsonArray_PrintTo_Tests, TwoIntegers)
{
TEST_F(JsonArray_PrintTo_Tests, TwoIntegers) {
array.add(1);
array.add(2);
outputMustBe("[1,2]");
}
TEST_F(JsonArray_PrintTo_Tests, OneIntegerOverCapacity)
{
TEST_F(JsonArray_PrintTo_Tests, OneIntegerOverCapacity) {
array.add(1);
array.add(2);
array.add(3);
@ -104,30 +87,26 @@ TEST_F(JsonArray_PrintTo_Tests, OneIntegerOverCapacity)
outputMustBe("[1,2]");
}
TEST_F(JsonArray_PrintTo_Tests, OneTrue)
{
TEST_F(JsonArray_PrintTo_Tests, OneTrue) {
array.add(true);
outputMustBe("[true]");
}
TEST_F(JsonArray_PrintTo_Tests, OneFalse)
{
TEST_F(JsonArray_PrintTo_Tests, OneFalse) {
array.add(false);
outputMustBe("[false]");
}
TEST_F(JsonArray_PrintTo_Tests, TwoBooleans)
{
TEST_F(JsonArray_PrintTo_Tests, TwoBooleans) {
array.add(false);
array.add(true);
outputMustBe("[false,true]");
}
TEST_F(JsonArray_PrintTo_Tests, OneBooleanOverCapacity)
{
TEST_F(JsonArray_PrintTo_Tests, OneBooleanOverCapacity) {
array.add(false);
array.add(true);
array.add(false);
@ -135,15 +114,13 @@ TEST_F(JsonArray_PrintTo_Tests, OneBooleanOverCapacity)
outputMustBe("[false,true]");
}
TEST_F(JsonArray_PrintTo_Tests, OneEmptyNestedArray)
{
TEST_F(JsonArray_PrintTo_Tests, OneEmptyNestedArray) {
array.createNestedArray();
outputMustBe("[[]]");
}
TEST_F(JsonArray_PrintTo_Tests, OneEmptyNestedHash)
{
TEST_F(JsonArray_PrintTo_Tests, OneEmptyNestedHash) {
array.createNestedObject();
outputMustBe("[{}]");

View File

@ -4,25 +4,19 @@
using namespace ArduinoJson;
class JsonObject_Container_Tests : public ::testing::Test
{
class JsonObject_Container_Tests : public ::testing::Test {
protected:
virtual void SetUp()
{
object = json.createObject();
}
virtual void SetUp() { object = json.createObject(); }
StaticJsonBuffer<42> json;
JsonObject object;
};
TEST_F(JsonObject_Container_Tests, InitialSizeIsZero)
{
TEST_F(JsonObject_Container_Tests, InitialSizeIsZero) {
EXPECT_EQ(0, object.size());
}
TEST_F(JsonObject_Container_Tests, Grow_WhenValuesAreAdded)
{
TEST_F(JsonObject_Container_Tests, Grow_WhenValuesAreAdded) {
object["hello"];
EXPECT_EQ(1, object.size());
@ -30,8 +24,7 @@ TEST_F(JsonObject_Container_Tests, Grow_WhenValuesAreAdded)
EXPECT_EQ(2, object.size());
}
TEST_F(JsonObject_Container_Tests, DoNotGrow_WhenSameValueIsAdded)
{
TEST_F(JsonObject_Container_Tests, DoNotGrow_WhenSameValueIsAdded) {
object["hello"];
EXPECT_EQ(1, object.size());
@ -39,8 +32,7 @@ TEST_F(JsonObject_Container_Tests, DoNotGrow_WhenSameValueIsAdded)
EXPECT_EQ(1, object.size());
}
TEST_F(JsonObject_Container_Tests, Shrink_WhenValuesAreRemoved)
{
TEST_F(JsonObject_Container_Tests, Shrink_WhenValuesAreRemoved) {
object["hello"];
object["world"];
@ -51,8 +43,8 @@ TEST_F(JsonObject_Container_Tests, Shrink_WhenValuesAreRemoved)
EXPECT_EQ(0, object.size());
}
TEST_F(JsonObject_Container_Tests, DoNotShrink_WhenRemoveIsCalledWithAWrongKey)
{
TEST_F(JsonObject_Container_Tests,
DoNotShrink_WhenRemoveIsCalledWithAWrongKey) {
object["hello"];
object["world"];
@ -61,62 +53,56 @@ TEST_F(JsonObject_Container_Tests, DoNotShrink_WhenRemoveIsCalledWithAWrongKey)
EXPECT_EQ(2, object.size());
}
TEST_F(JsonObject_Container_Tests, CanStoreIntegers)
{
TEST_F(JsonObject_Container_Tests, CanStoreIntegers) {
object["hello"] = 123;
object["world"] = 456;
EXPECT_EQ(123, (int) object["hello"]);
EXPECT_EQ(456, (int) object["world"]);
EXPECT_EQ(123, (int)object["hello"]);
EXPECT_EQ(456, (int)object["world"]);
}
TEST_F(JsonObject_Container_Tests, CanStoreDoubles)
{
TEST_F(JsonObject_Container_Tests, CanStoreDoubles) {
object["hello"] = 123.45;
object["world"] = 456.78;
EXPECT_EQ(123.45, (double) object["hello"]);
EXPECT_EQ(456.78, (double) object["world"]);
EXPECT_EQ(123.45, (double)object["hello"]);
EXPECT_EQ(456.78, (double)object["world"]);
}
TEST_F(JsonObject_Container_Tests, CanStoreBooleans)
{
TEST_F(JsonObject_Container_Tests, CanStoreBooleans) {
object["hello"] = true;
object["world"] = false;
EXPECT_TRUE((bool) object["hello"]);
EXPECT_FALSE((bool) object["world"]);
EXPECT_TRUE((bool)object["hello"]);
EXPECT_FALSE((bool)object["world"]);
}
TEST_F(JsonObject_Container_Tests, CanStoreStrings)
{
TEST_F(JsonObject_Container_Tests, CanStoreStrings) {
object["hello"] = "h3110";
object["world"] = "w0r1d";
EXPECT_STREQ("h3110", (const char*) object["hello"]);
EXPECT_STREQ("w0r1d", (const char*) object["world"]);
EXPECT_STREQ("h3110", (const char *)object["hello"]);
EXPECT_STREQ("w0r1d", (const char *)object["world"]);
}
TEST_F(JsonObject_Container_Tests, CanStoreInnerArrays)
{
TEST_F(JsonObject_Container_Tests, CanStoreInnerArrays) {
JsonArray innerarray1 = json.createArray();
JsonArray innerarray2 = json.createArray();
object["hello"] = innerarray1;
object["world"] = innerarray2;
EXPECT_EQ(innerarray1, (JsonArray) object["hello"]);
EXPECT_EQ(innerarray2, (JsonArray) object["world"]);
EXPECT_EQ(innerarray1, (JsonArray)object["hello"]);
EXPECT_EQ(innerarray2, (JsonArray)object["world"]);
}
TEST_F(JsonObject_Container_Tests, CanStoreInnerObjects)
{
TEST_F(JsonObject_Container_Tests, CanStoreInnerObjects) {
JsonObject innerObject1 = json.createObject();
JsonObject innerObject2 = json.createObject();
object["hello"] = innerObject1;
object["world"] = innerObject2;
EXPECT_EQ(innerObject1, (JsonObject) object["hello"]);
EXPECT_EQ(innerObject2, (JsonObject) object["world"]);
EXPECT_EQ(innerObject1, (JsonObject)object["hello"]);
EXPECT_EQ(innerObject2, (JsonObject)object["world"]);
}

View File

@ -4,8 +4,7 @@
using namespace ArduinoJson;
TEST(JsonObject_Iterator_Test, SimpleTest)
{
TEST(JsonObject_Iterator_Test, SimpleTest) {
StaticJsonBuffer<42> jsonBuffer;
JsonObject object = jsonBuffer.createObject();
@ -17,7 +16,8 @@ TEST(JsonObject_Iterator_Test, SimpleTest)
EXPECT_NE(end, it);
EXPECT_STREQ("ab", it->key());
EXPECT_EQ(12, it->value().as<int>()); ++it;
EXPECT_EQ(12, it->value().as<int>());
++it;
EXPECT_NE(end, it);
EXPECT_STREQ("cd", it->key());
EXPECT_EQ(34, it->value().as<int>());

View File

@ -10,19 +10,14 @@
using namespace ArduinoJson;
class JsonObject_PrettyPrintTo_Tests : public testing::Test
{
class JsonObject_PrettyPrintTo_Tests : public testing::Test {
protected:
JsonObject object;
StaticJsonBuffer<30> json;
virtual void SetUp()
{
object = json.createObject();
}
virtual void SetUp() { object = json.createObject(); }
void outputMustBe(const char* expected)
{
void outputMustBe(const char *expected) {
size_t n = object.prettyPrintTo(buffer, sizeof(buffer));
EXPECT_STREQ(expected, buffer);
EXPECT_EQ(strlen(expected), n);
@ -32,55 +27,44 @@ private:
char buffer[256];
};
TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyObject)
{
outputMustBe("{}");
}
TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyObject) { outputMustBe("{}"); }
TEST_F(JsonObject_PrettyPrintTo_Tests, OneMember)
{
TEST_F(JsonObject_PrettyPrintTo_Tests, OneMember) {
object["key"] = "value";
outputMustBe(
"{\r\n"
outputMustBe("{\r\n"
" \"key\": \"value\"\r\n"
"}");
}
TEST_F(JsonObject_PrettyPrintTo_Tests, TwoMembers)
{
TEST_F(JsonObject_PrettyPrintTo_Tests, TwoMembers) {
object["key1"] = "value1";
object["key2"] = "value2";
outputMustBe(
"{\r\n"
outputMustBe("{\r\n"
" \"key1\": \"value1\",\r\n"
" \"key2\": \"value2\"\r\n"
"}");
}
TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyNestedContainers)
{
TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyNestedContainers) {
object.createNestedObject("key1");
object.createNestedArray("key2");
outputMustBe(
"{\r\n"
outputMustBe("{\r\n"
" \"key1\": {},\r\n"
" \"key2\": []\r\n"
"}");
}
TEST_F(JsonObject_PrettyPrintTo_Tests, NestedContainers)
{
TEST_F(JsonObject_PrettyPrintTo_Tests, NestedContainers) {
JsonObject nested1 = object.createNestedObject("key1");
nested1["a"] = 1;
JsonArray nested2 = object.createNestedArray("key2");
nested2.add(2);
outputMustBe(
"{\r\n"
outputMustBe("{\r\n"
" \"key1\": {\r\n"
" \"a\": 1\r\n"
" },\r\n"

View File

@ -6,16 +6,11 @@
using namespace ArduinoJson;
class JsonObject_Serialization_Tests : public testing::Test
{
class JsonObject_Serialization_Tests : public testing::Test {
protected:
virtual void SetUp()
{
object = json.createObject();
}
virtual void SetUp() { object = json.createObject(); }
void outputMustBe(const char* expected)
{
void outputMustBe(const char *expected) {
char actual[256];
int result = object.printTo(actual, sizeof(actual));
@ -27,28 +22,22 @@ protected:
StaticJsonBuffer<5> json;
};
TEST_F(JsonObject_Serialization_Tests, EmptyObject)
{
outputMustBe("{}");
}
TEST_F(JsonObject_Serialization_Tests, EmptyObject) { outputMustBe("{}"); }
TEST_F(JsonObject_Serialization_Tests, OneString)
{
TEST_F(JsonObject_Serialization_Tests, OneString) {
object["key"] = "value";
outputMustBe("{\"key\":\"value\"}");
}
TEST_F(JsonObject_Serialization_Tests, TwoStrings)
{
TEST_F(JsonObject_Serialization_Tests, TwoStrings) {
object["key1"] = "value1";
object["key2"] = "value2";
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
}
TEST_F(JsonObject_Serialization_Tests, RemoveFirst)
{
TEST_F(JsonObject_Serialization_Tests, RemoveFirst) {
object["key1"] = "value1";
object["key2"] = "value2";
object.remove("key1");
@ -56,8 +45,7 @@ TEST_F(JsonObject_Serialization_Tests, RemoveFirst)
outputMustBe("{\"key2\":\"value2\"}");
}
TEST_F(JsonObject_Serialization_Tests, RemoveLast)
{
TEST_F(JsonObject_Serialization_Tests, RemoveLast) {
object["key1"] = "value1";
object["key2"] = "value2";
object.remove("key2");
@ -65,8 +53,7 @@ TEST_F(JsonObject_Serialization_Tests, RemoveLast)
outputMustBe("{\"key1\":\"value1\"}");
}
TEST_F(JsonObject_Serialization_Tests, RemoveUnexistingKey)
{
TEST_F(JsonObject_Serialization_Tests, RemoveUnexistingKey) {
object["key1"] = "value1";
object["key2"] = "value2";
object.remove("key3");
@ -74,16 +61,14 @@ TEST_F(JsonObject_Serialization_Tests, RemoveUnexistingKey)
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
}
TEST_F(JsonObject_Serialization_Tests, ReplaceExistingKey)
{
TEST_F(JsonObject_Serialization_Tests, ReplaceExistingKey) {
object["key"] = "value1";
object["key"] = "value2";
outputMustBe("{\"key\":\"value2\"}");
}
TEST_F(JsonObject_Serialization_Tests, OneStringOverCapacity)
{
TEST_F(JsonObject_Serialization_Tests, OneStringOverCapacity) {
object["key1"] = "value1";
object["key2"] = "value2";
object["key3"] = "value3";
@ -91,44 +76,37 @@ TEST_F(JsonObject_Serialization_Tests, OneStringOverCapacity)
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
}
TEST_F(JsonObject_Serialization_Tests, OneInteger)
{
TEST_F(JsonObject_Serialization_Tests, OneInteger) {
object["key"] = 1;
outputMustBe("{\"key\":1}");
}
TEST_F(JsonObject_Serialization_Tests, OneDoubleFourDigits)
{
TEST_F(JsonObject_Serialization_Tests, OneDoubleFourDigits) {
object["key"].set(3.14159265358979323846, 4);
outputMustBe("{\"key\":3.1416}");
}
TEST_F(JsonObject_Serialization_Tests, OneDoubleDefaultDigits)
{
TEST_F(JsonObject_Serialization_Tests, OneDoubleDefaultDigits) {
object["key"] = 3.14159265358979323846;
outputMustBe("{\"key\":3.14}");
}
TEST_F(JsonObject_Serialization_Tests, OneNull)
{
object["key"] = (char*) 0;
TEST_F(JsonObject_Serialization_Tests, OneNull) {
object["key"] = (char *)0;
outputMustBe("{\"key\":null}");
}
TEST_F(JsonObject_Serialization_Tests, OneTrue)
{
TEST_F(JsonObject_Serialization_Tests, OneTrue) {
object["key"] = true;
outputMustBe("{\"key\":true}");
}
TEST_F(JsonObject_Serialization_Tests, OneFalse)
{
TEST_F(JsonObject_Serialization_Tests, OneFalse) {
object["key"] = false;
outputMustBe("{\"key\":false}");
}
TEST_F(JsonObject_Serialization_Tests, OneEmptyNestedArrayViaProxy)
{
TEST_F(JsonObject_Serialization_Tests, OneEmptyNestedArrayViaProxy) {
JsonArray nestedArray = json.createArray();
object["key"] = nestedArray;
@ -136,8 +114,7 @@ TEST_F(JsonObject_Serialization_Tests, OneEmptyNestedArrayViaProxy)
outputMustBe("{\"key\":[]}");
}
TEST_F(JsonObject_Serialization_Tests, OneEmptyNestedObjectViaProxy)
{
TEST_F(JsonObject_Serialization_Tests, OneEmptyNestedObjectViaProxy) {
JsonObject nestedArray = json.createObject();
object["key"] = nestedArray;
@ -145,15 +122,13 @@ TEST_F(JsonObject_Serialization_Tests, OneEmptyNestedObjectViaProxy)
outputMustBe("{\"key\":{}}");
}
TEST_F(JsonObject_Serialization_Tests, OneEmptyNestedObject)
{
TEST_F(JsonObject_Serialization_Tests, OneEmptyNestedObject) {
object.createNestedObject("key");
outputMustBe("{\"key\":{}}");
}
TEST_F(JsonObject_Serialization_Tests, OneEmptyNestedArray)
{
TEST_F(JsonObject_Serialization_Tests, OneEmptyNestedArray) {
object.createNestedArray("key");
outputMustBe("{\"key\":[]}");

View File

@ -4,52 +4,36 @@
using namespace ArduinoJson;
class JsonParser_Array_Tests : public testing::Test
{
class JsonParser_Array_Tests : public testing::Test {
protected:
void whenInputIs(const char *json)
{
void whenInputIs(const char *json) {
strcpy(_jsonString, json);
_array = _jsonBuffer.parseArray(_jsonString);
}
void parseMustSucceed()
{
EXPECT_TRUE(_array.success());
}
void parseMustSucceed() { EXPECT_TRUE(_array.success()); }
void parseMustFail()
{
void parseMustFail() {
EXPECT_FALSE(_array.success());
EXPECT_EQ(0, _array.size());
}
void sizeMustBe(int expected)
{
EXPECT_EQ(expected, _array.size());
}
void sizeMustBe(int expected) { EXPECT_EQ(expected, _array.size()); }
template<typename T>
void firstElementMustBe(T expected)
{
template <typename T> void firstElementMustBe(T expected) {
elementAtIndexMustBe(0, expected);
}
template<typename T>
void secondElementMustBe(T expected)
{
template <typename T> void secondElementMustBe(T expected) {
elementAtIndexMustBe(1, expected);
}
template<typename T>
void elementAtIndexMustBe(int index, T expected)
{
template <typename T> void elementAtIndexMustBe(int index, T expected) {
EXPECT_EQ(expected, _array[index].as<T>());
}
void elementAtIndexMustBe(int index, const char* expected)
{
EXPECT_STREQ(expected, _array[index].as<const char*>());
void elementAtIndexMustBe(int index, const char *expected) {
EXPECT_STREQ(expected, _array[index].as<const char *>());
}
StaticJsonBuffer<42> _jsonBuffer;
@ -57,38 +41,33 @@ protected:
char _jsonString[256];
};
TEST_F(JsonParser_Array_Tests, EmptyArray)
{
TEST_F(JsonParser_Array_Tests, EmptyArray) {
whenInputIs("[]");
parseMustSucceed();
sizeMustBe(0);
}
TEST_F(JsonParser_Array_Tests, ArrayWithNoEnd)
{
TEST_F(JsonParser_Array_Tests, ArrayWithNoEnd) {
whenInputIs("[");
parseMustFail();
}
TEST_F(JsonParser_Array_Tests, EmptyArrayWithLeadingSpaces)
{
TEST_F(JsonParser_Array_Tests, EmptyArrayWithLeadingSpaces) {
whenInputIs(" []");
parseMustSucceed();
sizeMustBe(0);
}
TEST_F(JsonParser_Array_Tests, Garbage)
{
TEST_F(JsonParser_Array_Tests, Garbage) {
whenInputIs("%*$£¤");
parseMustFail();
}
TEST_F(JsonParser_Array_Tests, OneInteger)
{
TEST_F(JsonParser_Array_Tests, OneInteger) {
whenInputIs("[42]");
parseMustSucceed();
@ -96,8 +75,7 @@ TEST_F(JsonParser_Array_Tests, OneInteger)
firstElementMustBe(42);
}
TEST_F(JsonParser_Array_Tests, OneIntegerWithSpacesBefore)
{
TEST_F(JsonParser_Array_Tests, OneIntegerWithSpacesBefore) {
whenInputIs("[ \t\r\n42]");
parseMustSucceed();
@ -105,8 +83,7 @@ TEST_F(JsonParser_Array_Tests, OneIntegerWithSpacesBefore)
firstElementMustBe(42);
}
TEST_F(JsonParser_Array_Tests, OneIntegerWithSpaceAfter)
{
TEST_F(JsonParser_Array_Tests, OneIntegerWithSpaceAfter) {
whenInputIs("[42 \t\r\n]");
parseMustSucceed();
@ -114,8 +91,7 @@ TEST_F(JsonParser_Array_Tests, OneIntegerWithSpaceAfter)
firstElementMustBe(42);
}
TEST_F(JsonParser_Array_Tests, TwoIntegers)
{
TEST_F(JsonParser_Array_Tests, TwoIntegers) {
whenInputIs("[42,84]");
parseMustSucceed();
@ -124,8 +100,7 @@ TEST_F(JsonParser_Array_Tests, TwoIntegers)
secondElementMustBe(84);
}
TEST_F(JsonParser_Array_Tests, TwoDoubles)
{
TEST_F(JsonParser_Array_Tests, TwoDoubles) {
whenInputIs("[4.2,8.4]");
parseMustSucceed();
@ -134,8 +109,7 @@ TEST_F(JsonParser_Array_Tests, TwoDoubles)
secondElementMustBe(8.4);
}
TEST_F(JsonParser_Array_Tests, TwoBooleans)
{
TEST_F(JsonParser_Array_Tests, TwoBooleans) {
whenInputIs("[true,false]");
parseMustSucceed();
@ -144,9 +118,8 @@ TEST_F(JsonParser_Array_Tests, TwoBooleans)
secondElementMustBe(false);
}
TEST_F(JsonParser_Array_Tests, TwoNulls)
{
const char* const nullCharPtr = 0;
TEST_F(JsonParser_Array_Tests, TwoNulls) {
const char *const nullCharPtr = 0;
whenInputIs("[null,null]");
@ -156,8 +129,7 @@ TEST_F(JsonParser_Array_Tests, TwoNulls)
secondElementMustBe(nullCharPtr);
}
TEST_F(JsonParser_Array_Tests, TwoStrings)
{
TEST_F(JsonParser_Array_Tests, TwoStrings) {
whenInputIs("[\"hello\",\"world\"]");
parseMustSucceed();

View File

@ -3,8 +3,7 @@
using namespace ArduinoJson;
TEST(JsonParser_Nested_Tests, ArrayNestedInObject)
{
TEST(JsonParser_Nested_Tests, ArrayNestedInObject) {
StaticJsonBuffer<42> jsonBuffer;
char jsonString[] = " { \"ab\" : [ 1 , 2 ] , \"cd\" : [ 3 , 4 ] } ";
@ -27,10 +26,10 @@ TEST(JsonParser_Nested_Tests, ArrayNestedInObject)
EXPECT_EQ(4, array2[1].as<int>());
}
TEST(JsonParser_Nested_Tests, ObjectNestedInArray)
{
TEST(JsonParser_Nested_Tests, ObjectNestedInArray) {
StaticJsonBuffer<42> jsonBuffer;
char jsonString[] = " [ { \"a\" : 1 , \"b\" : 2 } , { \"c\" : 3 , \"d\" : 4 } ] ";
char jsonString[] =
" [ { \"a\" : 1 , \"b\" : 2 } , { \"c\" : 3 , \"d\" : 4 } ] ";
JsonArray array = jsonBuffer.parseArray(jsonString);
JsonObject object1 = array[0];

View File

@ -4,39 +4,24 @@
using namespace ArduinoJson;
class JsonParser_Object_Test : public testing::Test
{
class JsonParser_Object_Test : public testing::Test {
protected:
void whenInputIs(const char* jsonString)
{
void whenInputIs(const char *jsonString) {
strcpy(_jsonString, jsonString);
_object = _jsonBuffer.parseObject(_jsonString);
}
void parseMustSucceed()
{
EXPECT_TRUE(_object.success());
void parseMustSucceed() { EXPECT_TRUE(_object.success()); }
void parseMustFail() { EXPECT_FALSE(_object.success()); }
void sizeMustBe(int expected) { EXPECT_EQ(expected, _object.size()); }
void keyMustHaveValue(const char *key, const char *expected) {
EXPECT_STREQ(expected, _object[key].as<const char *>());
}
void parseMustFail()
{
EXPECT_FALSE(_object.success());
}
void sizeMustBe(int expected)
{
EXPECT_EQ(expected, _object.size());
}
void keyMustHaveValue(const char* key, const char* expected)
{
EXPECT_STREQ(expected, _object[key].as<const char*>());
}
template<typename T>
void keyMustHaveValue(const char* key, T expected)
{
template <typename T> void keyMustHaveValue(const char *key, T expected) {
EXPECT_EQ(expected, _object[key].as<T>());
}
@ -46,84 +31,73 @@ private:
char _jsonString[256];
};
TEST_F(JsonParser_Object_Test, EmptyObject)
{
TEST_F(JsonParser_Object_Test, EmptyObject) {
whenInputIs("{}");
parseMustSucceed();
sizeMustBe(0);
}
TEST_F(JsonParser_Object_Test, MissingClosingBrace)
{
TEST_F(JsonParser_Object_Test, MissingClosingBrace) {
whenInputIs("{");
parseMustFail();
sizeMustBe(0);
}
TEST_F(JsonParser_Object_Test, MissingColonAndValue)
{
TEST_F(JsonParser_Object_Test, MissingColonAndValue) {
whenInputIs("{\"key\"}");
parseMustFail();
sizeMustBe(0);
}
TEST_F(JsonParser_Object_Test, MissingQuotesAndColonAndValue)
{
TEST_F(JsonParser_Object_Test, MissingQuotesAndColonAndValue) {
whenInputIs("{key}");
parseMustFail();
sizeMustBe(0);
}
TEST_F(JsonParser_Object_Test, OneString)
{
TEST_F(JsonParser_Object_Test, OneString) {
whenInputIs("{\"key\":\"value\"}");
parseMustSucceed();
sizeMustBe(1);
keyMustHaveValue("key", "value");
}
TEST_F(JsonParser_Object_Test, OneStringSingleQuotes)
{
TEST_F(JsonParser_Object_Test, OneStringSingleQuotes) {
whenInputIs("{'key':'value'}");
parseMustSucceed();
sizeMustBe(1);
keyMustHaveValue("key", "value");
}
TEST_F(JsonParser_Object_Test, OneStringSpaceBeforeKey)
{
TEST_F(JsonParser_Object_Test, OneStringSpaceBeforeKey) {
whenInputIs("{ \"key\":\"value\"}");
parseMustSucceed();
sizeMustBe(1);
keyMustHaveValue("key", "value");
}
TEST_F(JsonParser_Object_Test, OneStringSpaceAfterKey)
{
TEST_F(JsonParser_Object_Test, OneStringSpaceAfterKey) {
whenInputIs("{\"key\" :\"value\"}");
parseMustSucceed();
sizeMustBe(1);
keyMustHaveValue("key", "value");
}
TEST_F(JsonParser_Object_Test, OneStringSpaceBeforeValue)
{
TEST_F(JsonParser_Object_Test, OneStringSpaceBeforeValue) {
whenInputIs("{\"key\": \"value\"}");
parseMustSucceed();
sizeMustBe(1);
keyMustHaveValue("key", "value");
}
TEST_F(JsonParser_Object_Test, OneStringSpaceAfterValue)
{
TEST_F(JsonParser_Object_Test, OneStringSpaceAfterValue) {
whenInputIs("{\"key\":\"value\" }");
parseMustSucceed();
sizeMustBe(1);
keyMustHaveValue("key", "value");
}
TEST_F(JsonParser_Object_Test, TwoStrings)
{
TEST_F(JsonParser_Object_Test, TwoStrings) {
whenInputIs("{\"key1\":\"value1\",\"key2\":\"value2\"}");
parseMustSucceed();
sizeMustBe(2);
@ -131,8 +105,7 @@ TEST_F(JsonParser_Object_Test, TwoStrings)
keyMustHaveValue("key2", "value2");
}
TEST_F(JsonParser_Object_Test, TwoStringsSpaceBeforeComma)
{
TEST_F(JsonParser_Object_Test, TwoStringsSpaceBeforeComma) {
whenInputIs("{\"key1\":\"value1\" ,\"key2\":\"value2\"}");
parseMustSucceed();
sizeMustBe(2);
@ -140,8 +113,7 @@ TEST_F(JsonParser_Object_Test, TwoStringsSpaceBeforeComma)
keyMustHaveValue("key2", "value2");
}
TEST_F(JsonParser_Object_Test, TwoStringsSpaceAfterComma)
{
TEST_F(JsonParser_Object_Test, TwoStringsSpaceAfterComma) {
whenInputIs("{\"key1\":\"value1\" ,\"key2\":\"value2\"}");
parseMustSucceed();
sizeMustBe(2);
@ -149,15 +121,13 @@ TEST_F(JsonParser_Object_Test, TwoStringsSpaceAfterComma)
keyMustHaveValue("key2", "value2");
}
TEST_F(JsonParser_Object_Test, EndingWithAComma)
{
TEST_F(JsonParser_Object_Test, EndingWithAComma) {
whenInputIs("{\"key1\":\"value1\",}");
parseMustFail();
sizeMustBe(0);
}
TEST_F(JsonParser_Object_Test, TwoIntergers)
{
TEST_F(JsonParser_Object_Test, TwoIntergers) {
whenInputIs("{\"key1\":42,\"key2\":-42}");
parseMustSucceed();
sizeMustBe(2);
@ -165,8 +135,7 @@ TEST_F(JsonParser_Object_Test, TwoIntergers)
keyMustHaveValue("key2", -42);
}
TEST_F(JsonParser_Object_Test, TwoDoubles)
{
TEST_F(JsonParser_Object_Test, TwoDoubles) {
whenInputIs("{\"key1\":12.345,\"key2\":-7.89}");
parseMustSucceed();
sizeMustBe(2);
@ -174,8 +143,7 @@ TEST_F(JsonParser_Object_Test, TwoDoubles)
keyMustHaveValue("key2", -7.89);
}
TEST_F(JsonParser_Object_Test, TwoBooleans)
{
TEST_F(JsonParser_Object_Test, TwoBooleans) {
whenInputIs("{\"key1\":true,\"key2\":false}");
parseMustSucceed();
sizeMustBe(2);
@ -183,9 +151,8 @@ TEST_F(JsonParser_Object_Test, TwoBooleans)
keyMustHaveValue("key2", false);
}
TEST_F(JsonParser_Object_Test, TwoNulls)
{
const char* const nullstr = 0;
TEST_F(JsonParser_Object_Test, TwoNulls) {
const char *const nullstr = 0;
whenInputIs("{\"key1\":null,\"key2\":null}");
parseMustSucceed();
@ -194,8 +161,7 @@ TEST_F(JsonParser_Object_Test, TwoNulls)
keyMustHaveValue("key2", nullstr);
}
TEST_F(JsonParser_Object_Test, NullForKey)
{
TEST_F(JsonParser_Object_Test, NullForKey) {
whenInputIs("null:\"value\"}");
parseMustFail();
}

View File

@ -4,11 +4,9 @@
using namespace ArduinoJson;
class JsonValueTests : public ::testing::Test
{
class JsonValueTests : public ::testing::Test {
protected:
virtual void SetUp()
{
virtual void SetUp() {
jsonValue1 = json.createValue();
jsonValue2 = json.createValue();
}
@ -18,104 +16,90 @@ protected:
JsonValue jsonValue2;
};
TEST_F(JsonValueTests, CanStoreInteger)
{
TEST_F(JsonValueTests, CanStoreInteger) {
jsonValue1 = 123;
EXPECT_EQ(123, (int) jsonValue1);
EXPECT_EQ(123, (int)jsonValue1);
}
TEST_F(JsonValueTests, CanStoreDouble)
{
TEST_F(JsonValueTests, CanStoreDouble) {
jsonValue1 = 123.45;
EXPECT_EQ(123.45, (double) jsonValue1);
EXPECT_EQ(123.45, (double)jsonValue1);
}
TEST_F(JsonValueTests, CanStoreTrue)
{
TEST_F(JsonValueTests, CanStoreTrue) {
jsonValue1 = true;
EXPECT_TRUE((bool) jsonValue1);
EXPECT_TRUE((bool)jsonValue1);
}
TEST_F(JsonValueTests, CanStoreFalse)
{
TEST_F(JsonValueTests, CanStoreFalse) {
jsonValue1 = false;
EXPECT_FALSE((bool) jsonValue1);
EXPECT_FALSE((bool)jsonValue1);
}
TEST_F(JsonValueTests, CanStoreString)
{
TEST_F(JsonValueTests, CanStoreString) {
jsonValue1 = "hello";
EXPECT_STREQ("hello", (const char*) jsonValue1);
EXPECT_STREQ("hello", (const char *)jsonValue1);
}
TEST_F(JsonValueTests, CanStoreObject)
{
TEST_F(JsonValueTests, CanStoreObject) {
JsonObject innerObject1 = json.createObject();
jsonValue1 = innerObject1;
EXPECT_EQ(innerObject1, (JsonObject) jsonValue1);
EXPECT_EQ(innerObject1, (JsonObject)jsonValue1);
}
TEST_F(JsonValueTests, IntegersAreCopiedByValue)
{
TEST_F(JsonValueTests, IntegersAreCopiedByValue) {
jsonValue1 = 123;
jsonValue2 = jsonValue1;
jsonValue1 = 456;
EXPECT_EQ(123, (int) jsonValue2);
EXPECT_EQ(123, (int)jsonValue2);
}
TEST_F(JsonValueTests, DoublesAreCopiedByValue)
{
TEST_F(JsonValueTests, DoublesAreCopiedByValue) {
jsonValue1 = 123.45;
jsonValue2 = jsonValue1;
jsonValue1 = 456.78;
EXPECT_EQ(123.45, (double) jsonValue2);
EXPECT_EQ(123.45, (double)jsonValue2);
}
TEST_F(JsonValueTests, BooleansAreCopiedByValue)
{
TEST_F(JsonValueTests, BooleansAreCopiedByValue) {
jsonValue1 = true;
jsonValue2 = jsonValue1;
jsonValue1 = false;
EXPECT_TRUE((bool) jsonValue2);
EXPECT_TRUE((bool)jsonValue2);
}
TEST_F(JsonValueTests, StringsAreCopiedByValue)
{
TEST_F(JsonValueTests, StringsAreCopiedByValue) {
jsonValue1 = "hello";
jsonValue2 = jsonValue1;
jsonValue1 = "world";
EXPECT_STREQ("hello", (const char*) jsonValue2);
EXPECT_STREQ("hello", (const char *)jsonValue2);
}
TEST_F(JsonValueTests, ObjectsAreCopiedByReference)
{
TEST_F(JsonValueTests, ObjectsAreCopiedByReference) {
JsonObject object = json.createObject();
jsonValue1 = object;
object["hello"] = "world";
EXPECT_EQ(1, ((JsonObject) jsonValue1).size());
EXPECT_EQ(1, ((JsonObject)jsonValue1).size());
}
TEST_F(JsonValueTests, ArraysAreCopiedByReference)
{
TEST_F(JsonValueTests, ArraysAreCopiedByReference) {
JsonArray array = json.createArray();
jsonValue1 = array;
array.add("world");
EXPECT_EQ(1, ((JsonObject) jsonValue1).size());
EXPECT_EQ(1, ((JsonObject)jsonValue1).size());
}

View File

@ -3,147 +3,122 @@
using namespace ArduinoJson::Internals;
class QuotedString_ExtractFrom_Tests : public testing::Test
{
class QuotedString_ExtractFrom_Tests : public testing::Test {
protected:
void whenInputIs(const char* json)
{
void whenInputIs(const char *json) {
strcpy(_jsonString, json);
_result = QuotedString::extractFrom(_jsonString, &_trailing);
}
void resultMustBe(const char* expected)
{
EXPECT_STREQ(expected, _result);
}
void resultMustBe(const char *expected) { EXPECT_STREQ(expected, _result); }
void trailingMustBe(const char* expected)
{
void trailingMustBe(const char *expected) {
EXPECT_STREQ(expected, _trailing);
}
private:
char _jsonString[256];
char* _result;
char* _trailing;
char *_result;
char *_trailing;
};
TEST_F(QuotedString_ExtractFrom_Tests, EmptyDoubleQuotedString)
{
TEST_F(QuotedString_ExtractFrom_Tests, EmptyDoubleQuotedString) {
whenInputIs("\"\"");
resultMustBe("");
trailingMustBe("");
}
TEST_F(QuotedString_ExtractFrom_Tests, NoQuotes)
{
TEST_F(QuotedString_ExtractFrom_Tests, NoQuotes) {
whenInputIs("hello world");
resultMustBe(0);
}
TEST_F(QuotedString_ExtractFrom_Tests, EmptySingleQuotedString)
{
TEST_F(QuotedString_ExtractFrom_Tests, EmptySingleQuotedString) {
whenInputIs("''");
resultMustBe("");
trailingMustBe("");
}
TEST_F(QuotedString_ExtractFrom_Tests, SimpleDoubleQuotedString)
{
TEST_F(QuotedString_ExtractFrom_Tests, SimpleDoubleQuotedString) {
whenInputIs("\"hello world\"");
resultMustBe("hello world");
trailingMustBe("");
}
TEST_F(QuotedString_ExtractFrom_Tests, DoubleQuotedStringWithTrailing)
{
TEST_F(QuotedString_ExtractFrom_Tests, DoubleQuotedStringWithTrailing) {
whenInputIs("\"hello\" world");
resultMustBe("hello");
trailingMustBe(" world");
}
TEST_F(QuotedString_ExtractFrom_Tests, SingleQuotedStringWithTrailing)
{
TEST_F(QuotedString_ExtractFrom_Tests, SingleQuotedStringWithTrailing) {
whenInputIs("'hello' world");
resultMustBe("hello");
trailingMustBe(" world");
}
TEST_F(QuotedString_ExtractFrom_Tests, CurlyBraces)
{
TEST_F(QuotedString_ExtractFrom_Tests, CurlyBraces) {
whenInputIs("\"{hello:world}\"");
resultMustBe("{hello:world}");
}
TEST_F(QuotedString_ExtractFrom_Tests, SquareBraquets)
{
TEST_F(QuotedString_ExtractFrom_Tests, SquareBraquets) {
whenInputIs("\"[hello,world]\"");
resultMustBe("[hello,world]");
}
TEST_F(QuotedString_ExtractFrom_Tests, EscapedDoubleQuote)
{
TEST_F(QuotedString_ExtractFrom_Tests, EscapedDoubleQuote) {
whenInputIs("\"hello \\\"world\\\"\"");
resultMustBe("hello \"world\"");
}
TEST_F(QuotedString_ExtractFrom_Tests, EscapedSingleQuote)
{
TEST_F(QuotedString_ExtractFrom_Tests, EscapedSingleQuote) {
whenInputIs("\"hello \\\'world\\\'\"");
resultMustBe("hello 'world'");
}
TEST_F(QuotedString_ExtractFrom_Tests, EscapedSolidus)
{
TEST_F(QuotedString_ExtractFrom_Tests, EscapedSolidus) {
whenInputIs("\"hello \\/world\\/\"");
resultMustBe("hello /world/");
}
TEST_F(QuotedString_ExtractFrom_Tests, EscapedReverseSolidus)
{
TEST_F(QuotedString_ExtractFrom_Tests, EscapedReverseSolidus) {
whenInputIs("\"hello \\\\world\\\\\"");
resultMustBe("hello \\world\\");
}
TEST_F(QuotedString_ExtractFrom_Tests, EscapedBackspace)
{
TEST_F(QuotedString_ExtractFrom_Tests, EscapedBackspace) {
whenInputIs("\"hello \\bworld\\b\"");
resultMustBe("hello \bworld\b");
}
TEST_F(QuotedString_ExtractFrom_Tests, EscapedFormfeed)
{
TEST_F(QuotedString_ExtractFrom_Tests, EscapedFormfeed) {
whenInputIs("\"hello \\fworld\\f\"");
resultMustBe("hello \fworld\f");
}
TEST_F(QuotedString_ExtractFrom_Tests, EscapedNewline)
{
TEST_F(QuotedString_ExtractFrom_Tests, EscapedNewline) {
whenInputIs("\"hello \\nworld\\n\"");
resultMustBe("hello \nworld\n");
}
TEST_F(QuotedString_ExtractFrom_Tests, EscapedCarriageReturn)
{
TEST_F(QuotedString_ExtractFrom_Tests, EscapedCarriageReturn) {
whenInputIs("\"hello \\rworld\\r\"");
resultMustBe("hello \rworld\r");
}
TEST_F(QuotedString_ExtractFrom_Tests, EscapedTab)
{
TEST_F(QuotedString_ExtractFrom_Tests, EscapedTab) {
whenInputIs("\"hello \\tworld\\t\"");
resultMustBe("hello \tworld\t");
}
TEST_F(QuotedString_ExtractFrom_Tests, AllEscapedCharsTogether)
{
TEST_F(QuotedString_ExtractFrom_Tests, AllEscapedCharsTogether) {
whenInputIs("\"1\\\"2\\\\3\\/4\\b5\\f6\\n7\\r8\\t9\"");
resultMustBe("1\"2\\3/4\b5\f6\n7\r8\t9");
}

View File

@ -5,17 +5,14 @@
using namespace ArduinoJson::Internals;
class QuotedString_PrintTo_Tests : public testing::Test
{
class QuotedString_PrintTo_Tests : public testing::Test {
protected:
void whenInputIs(const char* input)
{
void whenInputIs(const char *input) {
StringBuilder sb(buffer, sizeof(buffer));
returnValue = QuotedString::printTo(input, &sb);
}
void outputMustBe(const char* expected)
{
void outputMustBe(const char *expected) {
EXPECT_STREQ(expected, buffer);
EXPECT_EQ(strlen(expected), returnValue);
}
@ -25,62 +22,52 @@ private:
size_t returnValue;
};
TEST_F(QuotedString_PrintTo_Tests, Null)
{
TEST_F(QuotedString_PrintTo_Tests, Null) {
whenInputIs(0);
outputMustBe("null");
}
TEST_F(QuotedString_PrintTo_Tests, EmptyString)
{
TEST_F(QuotedString_PrintTo_Tests, EmptyString) {
whenInputIs("");
outputMustBe("\"\"");
}
TEST_F(QuotedString_PrintTo_Tests, QuotationMark)
{
TEST_F(QuotedString_PrintTo_Tests, QuotationMark) {
whenInputIs("\"");
outputMustBe("\"\\\"\"");
}
TEST_F(QuotedString_PrintTo_Tests, ReverseSolidus)
{
TEST_F(QuotedString_PrintTo_Tests, ReverseSolidus) {
whenInputIs("\\");
outputMustBe("\"\\\\\"");
}
TEST_F(QuotedString_PrintTo_Tests, Solidus)
{
TEST_F(QuotedString_PrintTo_Tests, Solidus) {
whenInputIs("/");
outputMustBe("\"/\""); // but the JSON format allows \/
}
TEST_F(QuotedString_PrintTo_Tests, Backspace)
{
TEST_F(QuotedString_PrintTo_Tests, Backspace) {
whenInputIs("\b");
outputMustBe("\"\\b\"");
}
TEST_F(QuotedString_PrintTo_Tests, Formfeed)
{
TEST_F(QuotedString_PrintTo_Tests, Formfeed) {
whenInputIs("\f");
outputMustBe("\"\\f\"");
}
TEST_F(QuotedString_PrintTo_Tests, Newline)
{
TEST_F(QuotedString_PrintTo_Tests, Newline) {
whenInputIs("\n");
outputMustBe("\"\\n\"");
}
TEST_F(QuotedString_PrintTo_Tests, CarriageReturn)
{
TEST_F(QuotedString_PrintTo_Tests, CarriageReturn) {
whenInputIs("\r");
outputMustBe("\"\\r\"");
}
TEST_F(QuotedString_PrintTo_Tests, HorizontalTab)
{
TEST_F(QuotedString_PrintTo_Tests, HorizontalTab) {
whenInputIs("\t");
outputMustBe("\"\\t\"");
}

View File

@ -4,20 +4,17 @@
using namespace ArduinoJson;
TEST(StaticJsonBuffer, CapacityMatchTemplateParameter)
{
TEST(StaticJsonBuffer, CapacityMatchTemplateParameter) {
StaticJsonBuffer<42> json;
EXPECT_EQ(42, json.capacity());
}
TEST(StaticJsonBuffer, InitialSizeIsZero)
{
TEST(StaticJsonBuffer, InitialSizeIsZero) {
StaticJsonBuffer<42> json;
EXPECT_EQ(0, json.size());
}
TEST(StaticJsonBuffer, WhenCreateObjectIsCalled_ThenSizeIsIncreasedByOne)
{
TEST(StaticJsonBuffer, WhenCreateObjectIsCalled_ThenSizeIsIncreasedByOne) {
StaticJsonBuffer<42> json;
json.createObject();
@ -27,8 +24,8 @@ TEST(StaticJsonBuffer, WhenCreateObjectIsCalled_ThenSizeIsIncreasedByOne)
EXPECT_EQ(2, json.size());
}
TEST(StaticJsonBuffer, GivenBufferIsFull_WhenCreateObjectIsCalled_ThenSizeDoesNotChange)
{
TEST(StaticJsonBuffer,
GivenBufferIsFull_WhenCreateObjectIsCalled_ThenSizeDoesNotChange) {
StaticJsonBuffer<1> json;
json.createObject();
@ -38,16 +35,16 @@ TEST(StaticJsonBuffer, GivenBufferIsFull_WhenCreateObjectIsCalled_ThenSizeDoesNo
EXPECT_EQ(1, json.size());
}
TEST(StaticJsonBuffer, WhenCreateObjectIsCalled_ThenAnEmptyJsonObjectIsReturned)
{
TEST(StaticJsonBuffer,
WhenCreateObjectIsCalled_ThenAnEmptyJsonObjectIsReturned) {
StaticJsonBuffer<42> json;
JsonObject obj = json.createObject();
EXPECT_EQ(0, obj.size());
}
TEST(StaticJsonBuffer, GivenAJsonObject_WhenValuesAreAdded_ThenSizeIsIncreasedByTwo)
{
TEST(StaticJsonBuffer,
GivenAJsonObject_WhenValuesAreAdded_ThenSizeIsIncreasedByTwo) {
StaticJsonBuffer<42> json;
JsonObject obj = json.createObject();
@ -58,8 +55,9 @@ TEST(StaticJsonBuffer, GivenAJsonObject_WhenValuesAreAdded_ThenSizeIsIncreasedBy
EXPECT_EQ(5, json.size());
}
TEST(StaticJsonBuffer, GivenAJsonObject_WhenSameValuesAreAddedTwice_ThenSizeIsOnlyIncreasedByTwo)
{
TEST(
StaticJsonBuffer,
GivenAJsonObject_WhenSameValuesAreAddedTwice_ThenSizeIsOnlyIncreasedByTwo) {
StaticJsonBuffer<42> json;
JsonObject obj = json.createObject();

View File

@ -3,43 +3,25 @@
using namespace ArduinoJson::Internals;
class StringBuilderTests : public testing::Test
{
class StringBuilderTests : public testing::Test {
protected:
virtual void SetUp() { sb = new StringBuilder(buffer, sizeof(buffer)); }
virtual void SetUp()
{
sb = new StringBuilder(buffer, sizeof(buffer));
}
void print(const char *value) { returnValue = sb->print(value); }
void print(const char* value)
{
returnValue = sb->print(value);
}
void outputMustBe(const char *expected) { EXPECT_STREQ(expected, buffer); }
void outputMustBe(const char* expected)
{
EXPECT_STREQ(expected, buffer);
}
void resultMustBe(size_t expected)
{
EXPECT_EQ(expected, returnValue);
}
void resultMustBe(size_t expected) { EXPECT_EQ(expected, returnValue); }
private:
char buffer[20];
Print* sb;
Print *sb;
size_t returnValue;
};
TEST_F(StringBuilderTests, InitialState)
{
outputMustBe("");
}
TEST_F(StringBuilderTests, InitialState) { outputMustBe(""); }
TEST_F(StringBuilderTests, OverCapacity)
{
TEST_F(StringBuilderTests, OverCapacity) {
print("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
resultMustBe(19);
@ -49,22 +31,19 @@ TEST_F(StringBuilderTests, OverCapacity)
outputMustBe("ABCDEFGHIJKLMNOPQRS");
}
TEST_F(StringBuilderTests, EmptyString)
{
TEST_F(StringBuilderTests, EmptyString) {
print("");
resultMustBe(0);
outputMustBe("");
}
TEST_F(StringBuilderTests, OneString)
{
TEST_F(StringBuilderTests, OneString) {
print("ABCD");
resultMustBe(4);
outputMustBe("ABCD");
}
TEST_F(StringBuilderTests, TwoStrings)
{
TEST_F(StringBuilderTests, TwoStrings) {
print("ABCD");
resultMustBe(4);