mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-13 10:36:34 +02:00
Formated code with clang-format
This commit is contained in:
1
.clang-format
Normal file
1
.clang-format
Normal file
@ -0,0 +1 @@
|
||||
BasedOnStyle: LLVM
|
@ -11,16 +11,14 @@
|
||||
#include <stdint.h>
|
||||
|
||||
// This class reproduces Arduino's Print
|
||||
class Print
|
||||
{
|
||||
class Print {
|
||||
public:
|
||||
virtual size_t write(uint8_t) = 0;
|
||||
|
||||
virtual size_t write(uint8_t) = 0;
|
||||
|
||||
size_t print(const char[]);
|
||||
size_t print(double, int = 2);
|
||||
size_t print(long);
|
||||
size_t println();
|
||||
size_t print(const char[]);
|
||||
size_t print(double, int = 2);
|
||||
size_t print(long);
|
||||
size_t println();
|
||||
};
|
||||
|
||||
#else
|
||||
|
@ -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
|
||||
|
||||
|
@ -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('}'); }
|
||||
};
|
||||
}
|
||||
}
|
@ -7,47 +7,40 @@
|
||||
|
||||
#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:
|
||||
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;
|
||||
}
|
||||
|
||||
IndentedPrint(Print& p)
|
||||
: sink(&p)
|
||||
{
|
||||
level = 0;
|
||||
tabSize = 2;
|
||||
isNewLine = true;
|
||||
}
|
||||
virtual size_t write(uint8_t);
|
||||
|
||||
virtual size_t write(uint8_t);
|
||||
// Adds one level of indentation
|
||||
void indent();
|
||||
|
||||
// Adds one level of indentation
|
||||
void indent();
|
||||
// Removes one level of indentation
|
||||
void unindent();
|
||||
|
||||
// Removes one level of indentation
|
||||
void unindent();
|
||||
// Set the number of space printed for each level of indentation
|
||||
void setTabSize(uint8_t n);
|
||||
|
||||
// Set the number of space printed for each level of indentation
|
||||
void setTabSize(uint8_t n);
|
||||
private:
|
||||
Print *sink;
|
||||
uint8_t level : 4;
|
||||
uint8_t tabSize : 3;
|
||||
bool isNewLine : 1;
|
||||
|
||||
private:
|
||||
Print* sink;
|
||||
uint8_t level : 4;
|
||||
uint8_t tabSize : 3;
|
||||
bool isNewLine : 1;
|
||||
size_t writeTabs();
|
||||
|
||||
size_t writeTabs();
|
||||
|
||||
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
|
||||
};
|
||||
}
|
||||
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
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,191 +1,159 @@
|
||||
#pragma once
|
||||
|
||||
namespace ArduinoJson
|
||||
{
|
||||
class JsonBuffer;
|
||||
namespace ArduinoJson {
|
||||
class JsonBuffer;
|
||||
|
||||
namespace Internals
|
||||
{
|
||||
class JsonWriter;
|
||||
namespace Internals {
|
||||
class JsonWriter;
|
||||
|
||||
class JsonNode
|
||||
{
|
||||
enum JsonNodeType
|
||||
{
|
||||
JSON_UNDEFINED,
|
||||
JSON_NULL,
|
||||
JSON_ARRAY,
|
||||
JSON_OBJECT,
|
||||
JSON_KEY_VALUE,
|
||||
JSON_BOOLEAN,
|
||||
JSON_STRING,
|
||||
JSON_LONG,
|
||||
JSON_PROXY,
|
||||
JSON_DOUBLE_0_DECIMALS,
|
||||
JSON_DOUBLE_1_DECIMAL,
|
||||
JSON_DOUBLE_2_DECIMALS
|
||||
// etc.
|
||||
};
|
||||
class JsonNode {
|
||||
enum JsonNodeType {
|
||||
JSON_UNDEFINED,
|
||||
JSON_NULL,
|
||||
JSON_ARRAY,
|
||||
JSON_OBJECT,
|
||||
JSON_KEY_VALUE,
|
||||
JSON_BOOLEAN,
|
||||
JSON_STRING,
|
||||
JSON_LONG,
|
||||
JSON_PROXY,
|
||||
JSON_DOUBLE_0_DECIMALS,
|
||||
JSON_DOUBLE_1_DECIMAL,
|
||||
JSON_DOUBLE_2_DECIMALS
|
||||
// etc.
|
||||
};
|
||||
|
||||
union JsonNodeContent
|
||||
{
|
||||
bool asBoolean;
|
||||
double asDouble;
|
||||
long asInteger;
|
||||
const char* asString;
|
||||
union JsonNodeContent {
|
||||
bool asBoolean;
|
||||
double asDouble;
|
||||
long asInteger;
|
||||
const char *asString;
|
||||
|
||||
struct
|
||||
{
|
||||
const char* key;
|
||||
JsonNode* value;
|
||||
} asKeyValue;
|
||||
struct {
|
||||
const char *key;
|
||||
JsonNode *value;
|
||||
} asKeyValue;
|
||||
|
||||
struct
|
||||
{
|
||||
JsonNode* child;
|
||||
JsonBuffer* buffer;
|
||||
} asContainer;
|
||||
struct {
|
||||
JsonNode *child;
|
||||
JsonBuffer *buffer;
|
||||
} asContainer;
|
||||
|
||||
struct
|
||||
{
|
||||
JsonNode* target;
|
||||
} asProxy;
|
||||
struct {
|
||||
JsonNode *target;
|
||||
} asProxy;
|
||||
};
|
||||
|
||||
};
|
||||
public:
|
||||
JsonNode() : next(0), type(JSON_UNDEFINED) {}
|
||||
|
||||
public:
|
||||
JsonNode()
|
||||
: next(0), type(JSON_UNDEFINED)
|
||||
{
|
||||
JsonNode *next;
|
||||
|
||||
}
|
||||
void writeTo(JsonWriter &); // TODO: <- move in JsonNodeSerializer
|
||||
|
||||
JsonNode* next;
|
||||
void setAsArray(JsonBuffer *buffer) {
|
||||
type = JSON_ARRAY;
|
||||
content.asContainer.child = 0;
|
||||
content.asContainer.buffer = buffer;
|
||||
}
|
||||
|
||||
void writeTo(JsonWriter&); // TODO: <- move in JsonNodeSerializer
|
||||
void setAsBoolean(bool value) {
|
||||
type = JSON_BOOLEAN;
|
||||
content.asBoolean = value;
|
||||
}
|
||||
|
||||
void setAsArray(JsonBuffer* buffer)
|
||||
{
|
||||
type = JSON_ARRAY;
|
||||
content.asContainer.child = 0;
|
||||
content.asContainer.buffer = buffer;
|
||||
}
|
||||
void setAsLong(int value) {
|
||||
type = JSON_LONG;
|
||||
content.asInteger = value;
|
||||
}
|
||||
|
||||
void setAsBoolean(bool value)
|
||||
{
|
||||
type = JSON_BOOLEAN;
|
||||
content.asBoolean = value;
|
||||
}
|
||||
void setAsString(char const *value) {
|
||||
type = JSON_STRING;
|
||||
content.asString = value;
|
||||
}
|
||||
|
||||
void setAsLong(int value)
|
||||
{
|
||||
type = JSON_LONG;
|
||||
content.asInteger = value;
|
||||
}
|
||||
void setAsDouble(double value, int decimals) {
|
||||
type = static_cast<JsonNodeType>(JSON_DOUBLE_0_DECIMALS + decimals);
|
||||
content.asDouble = value;
|
||||
}
|
||||
|
||||
void setAsString(char const* value)
|
||||
{
|
||||
type = JSON_STRING;
|
||||
content.asString = value;
|
||||
}
|
||||
void setAsObject(JsonBuffer *buffer) {
|
||||
type = JSON_OBJECT;
|
||||
content.asContainer.child = 0;
|
||||
content.asContainer.buffer = buffer;
|
||||
}
|
||||
|
||||
void setAsDouble(double value, int decimals)
|
||||
{
|
||||
type = static_cast<JsonNodeType>(JSON_DOUBLE_0_DECIMALS + decimals);
|
||||
content.asDouble = value;
|
||||
}
|
||||
void setAsObjectKeyValue(const char *key, JsonNode *value) {
|
||||
type = JSON_KEY_VALUE;
|
||||
content.asKeyValue.key = key;
|
||||
content.asKeyValue.value = value;
|
||||
}
|
||||
|
||||
void setAsObject(JsonBuffer* buffer)
|
||||
{
|
||||
type = JSON_OBJECT;
|
||||
content.asContainer.child = 0;
|
||||
content.asContainer.buffer = buffer;
|
||||
}
|
||||
bool getAsBoolean() {
|
||||
return type == JSON_BOOLEAN ? content.asBoolean : false;
|
||||
}
|
||||
|
||||
void setAsObjectKeyValue(const char* key, JsonNode* value)
|
||||
{
|
||||
type = JSON_KEY_VALUE;
|
||||
content.asKeyValue.key = key;
|
||||
content.asKeyValue.value = value;
|
||||
}
|
||||
double getAsDouble() {
|
||||
return type >= JSON_DOUBLE_0_DECIMALS ? content.asDouble : 0;
|
||||
}
|
||||
|
||||
bool getAsBoolean()
|
||||
{
|
||||
return type == JSON_BOOLEAN ? content.asBoolean : false;
|
||||
}
|
||||
long getAsInteger() { return type == JSON_LONG ? content.asInteger : 0; }
|
||||
|
||||
double getAsDouble()
|
||||
{
|
||||
return type >= JSON_DOUBLE_0_DECIMALS ? content.asDouble : 0;
|
||||
}
|
||||
const char *getAsString() {
|
||||
return type == JSON_STRING ? content.asString : 0;
|
||||
}
|
||||
|
||||
long getAsInteger()
|
||||
{
|
||||
return type == JSON_LONG ? content.asInteger : 0;
|
||||
}
|
||||
JsonBuffer *getContainerBuffer() {
|
||||
if (type == JSON_PROXY)
|
||||
return content.asProxy.target->getContainerBuffer();
|
||||
return type == JSON_ARRAY || type == JSON_OBJECT
|
||||
? content.asContainer.buffer
|
||||
: 0;
|
||||
}
|
||||
|
||||
const char* getAsString()
|
||||
{
|
||||
return type == JSON_STRING ? content.asString : 0;
|
||||
}
|
||||
JsonNode *getContainerChild() {
|
||||
if (type == JSON_PROXY)
|
||||
return content.asProxy.target->getContainerChild();
|
||||
return type == JSON_ARRAY || type == JSON_OBJECT ? content.asContainer.child
|
||||
: 0;
|
||||
}
|
||||
|
||||
JsonBuffer* getContainerBuffer()
|
||||
{
|
||||
if (type == JSON_PROXY) return content.asProxy.target->getContainerBuffer();
|
||||
return type == JSON_ARRAY || type == JSON_OBJECT ? content.asContainer.buffer : 0;
|
||||
}
|
||||
const char *getAsObjectKey() {
|
||||
return type == JSON_KEY_VALUE ? content.asKeyValue.key : 0;
|
||||
}
|
||||
|
||||
JsonNode* getContainerChild()
|
||||
{
|
||||
if (type == JSON_PROXY) return content.asProxy.target->getContainerChild();
|
||||
return type == JSON_ARRAY || type == JSON_OBJECT ? content.asContainer.child : 0;
|
||||
}
|
||||
JsonNode *getAsObjectValue() {
|
||||
return type == JSON_KEY_VALUE ? content.asKeyValue.value : 0;
|
||||
}
|
||||
|
||||
const char* getAsObjectKey()
|
||||
{
|
||||
return type == JSON_KEY_VALUE ? content.asKeyValue.key : 0;
|
||||
}
|
||||
JsonNode *getProxyTarget() {
|
||||
return type == JSON_PROXY ? content.asProxy.target : this;
|
||||
}
|
||||
|
||||
JsonNode* getAsObjectValue()
|
||||
{
|
||||
return type == JSON_KEY_VALUE ? content.asKeyValue.value : 0;
|
||||
}
|
||||
bool isArray() { return type == JSON_ARRAY; }
|
||||
|
||||
JsonNode* getProxyTarget()
|
||||
{
|
||||
return type == JSON_PROXY ? content.asProxy.target : this;
|
||||
}
|
||||
bool isObject() { return type == JSON_OBJECT; }
|
||||
|
||||
bool isArray()
|
||||
{
|
||||
return type == JSON_ARRAY;
|
||||
}
|
||||
void addChild(JsonNode *childToAdd);
|
||||
|
||||
bool isObject()
|
||||
{
|
||||
return type == JSON_OBJECT;
|
||||
}
|
||||
void removeChild(JsonNode *childToRemove);
|
||||
|
||||
void addChild(JsonNode* childToAdd);
|
||||
void duplicate(JsonNode *other);
|
||||
|
||||
void removeChild(JsonNode* childToRemove);
|
||||
private:
|
||||
JsonNodeType type;
|
||||
JsonNodeContent content;
|
||||
|
||||
void duplicate(JsonNode* other);
|
||||
inline void writeArrayTo(JsonWriter &); // TODO: <- move in JsonNodeSerializer
|
||||
inline void
|
||||
writeObjectTo(JsonWriter &); // TODO: <- move in JsonNodeSerializer
|
||||
|
||||
private:
|
||||
JsonNodeType type;
|
||||
JsonNodeContent content;
|
||||
void setAsProxyOfSelf();
|
||||
|
||||
inline void writeArrayTo(JsonWriter&);// TODO: <- move in JsonNodeSerializer
|
||||
inline void writeObjectTo(JsonWriter&);// TODO: <- move in JsonNodeSerializer
|
||||
|
||||
void setAsProxyOfSelf();
|
||||
|
||||
void setAsProxyOf(JsonNode* target)
|
||||
{
|
||||
type = JSON_PROXY;
|
||||
content.asProxy.target = target;
|
||||
}
|
||||
};
|
||||
}
|
||||
void setAsProxyOf(JsonNode *target) {
|
||||
type = JSON_PROXY;
|
||||
content.asProxy.target = target;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
@ -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 {
|
||||
return _node != other._node;
|
||||
}
|
||||
|
||||
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;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -2,42 +2,28 @@
|
||||
|
||||
#include "JsonNode.hpp"
|
||||
|
||||
namespace ArduinoJson
|
||||
{
|
||||
class JsonValue ;
|
||||
namespace ArduinoJson {
|
||||
class JsonValue;
|
||||
|
||||
namespace Internals
|
||||
{
|
||||
class JsonNodeWrapper
|
||||
{
|
||||
friend class JsonValue;
|
||||
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)
|
||||
{
|
||||
_node = other._node;
|
||||
}
|
||||
else
|
||||
{
|
||||
_node->duplicate(other._node);
|
||||
}
|
||||
}
|
||||
|
||||
JsonNode* _node;
|
||||
};
|
||||
protected:
|
||||
void duplicate(const JsonNodeWrapper &other) {
|
||||
if (!_node) {
|
||||
_node = other._node;
|
||||
} else {
|
||||
_node->duplicate(other._node);
|
||||
}
|
||||
}
|
||||
|
||||
JsonNode *_node;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
|
||||
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();
|
||||
};
|
||||
}
|
||||
}
|
@ -2,53 +2,38 @@
|
||||
|
||||
#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;
|
||||
virtual void beginArray() = 0;
|
||||
|
||||
virtual void endArray() = 0;
|
||||
virtual void endArray() = 0;
|
||||
|
||||
virtual void beginObject() = 0;
|
||||
virtual void beginObject() = 0;
|
||||
|
||||
virtual void endObject() = 0;
|
||||
virtual void endObject() = 0;
|
||||
|
||||
void writeString(const char* value);
|
||||
void writeInteger(long value);
|
||||
void writeBoolean(bool value);
|
||||
void writeDouble(double value, int decimals);
|
||||
void writeString(const char *value);
|
||||
void writeInteger(long value);
|
||||
void writeBoolean(bool value);
|
||||
void writeDouble(double value, int decimals);
|
||||
|
||||
virtual void writeColon() = 0;
|
||||
virtual void writeColon() = 0;
|
||||
|
||||
virtual void writeComma() = 0;
|
||||
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;
|
||||
size_t _length;
|
||||
};
|
||||
}
|
||||
protected:
|
||||
Print *_sink;
|
||||
size_t _length;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -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()
|
||||
{
|
||||
_length += _sink->write('[');
|
||||
indent();
|
||||
}
|
||||
virtual void beginArray() {
|
||||
_length += _sink->write('[');
|
||||
indent();
|
||||
}
|
||||
|
||||
virtual void endArray()
|
||||
{
|
||||
unindent();
|
||||
_length += _sink->write(']');
|
||||
}
|
||||
virtual void endArray() {
|
||||
unindent();
|
||||
_length += _sink->write(']');
|
||||
}
|
||||
|
||||
virtual void writeColon()
|
||||
{
|
||||
_length += _sink->print(": ");
|
||||
}
|
||||
virtual void writeColon() { _length += _sink->print(": "); }
|
||||
|
||||
virtual void writeComma()
|
||||
{
|
||||
_length += _sink->write(',');
|
||||
_length += _indenter->println();
|
||||
}
|
||||
virtual void writeComma() {
|
||||
_length += _sink->write(',');
|
||||
_length += _indenter->println();
|
||||
}
|
||||
|
||||
virtual void beginObject()
|
||||
{
|
||||
_length += _sink->write('{');
|
||||
indent();
|
||||
}
|
||||
virtual void beginObject() {
|
||||
_length += _sink->write('{');
|
||||
indent();
|
||||
}
|
||||
|
||||
virtual void endObject()
|
||||
{
|
||||
unindent();
|
||||
_length += _sink->write('}');
|
||||
}
|
||||
virtual void endObject() {
|
||||
unindent();
|
||||
_length += _sink->write('}');
|
||||
}
|
||||
|
||||
private:
|
||||
IndentedPrint* _indenter;
|
||||
private:
|
||||
IndentedPrint *_indenter;
|
||||
|
||||
void indent()
|
||||
{
|
||||
_indenter->indent();
|
||||
_length += _indenter->println();
|
||||
}
|
||||
void indent() {
|
||||
_indenter->indent();
|
||||
_length += _indenter->println();
|
||||
}
|
||||
|
||||
void unindent()
|
||||
{
|
||||
_length += _indenter->println();
|
||||
_indenter->unindent();
|
||||
}
|
||||
};
|
||||
}
|
||||
void unindent() {
|
||||
_length += _indenter->println();
|
||||
_indenter->unindent();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
@ -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);
|
||||
};
|
||||
}
|
||||
}
|
@ -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)
|
||||
{
|
||||
buffer[0] = 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);
|
||||
virtual size_t write(uint8_t c);
|
||||
|
||||
private:
|
||||
char* buffer;
|
||||
int capacity;
|
||||
int length;
|
||||
};
|
||||
}
|
||||
private:
|
||||
char *buffer;
|
||||
int capacity;
|
||||
int length;
|
||||
};
|
||||
}
|
||||
}
|
@ -3,42 +3,29 @@
|
||||
#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;
|
||||
JsonValue operator[](int index) const;
|
||||
|
||||
void add(bool value);
|
||||
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
|
||||
void add(bool value);
|
||||
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
|
||||
|
||||
JsonArray createNestedArray();
|
||||
JsonObject createNestedObject();
|
||||
JsonArray createNestedArray();
|
||||
JsonObject createNestedObject();
|
||||
|
||||
bool success()
|
||||
{
|
||||
return _node && _node->isArray();
|
||||
}
|
||||
bool success() { return _node && _node->isArray(); }
|
||||
|
||||
JsonArrayIterator begin();
|
||||
JsonArrayIterator begin();
|
||||
|
||||
JsonArrayIterator end()
|
||||
{
|
||||
return JsonArrayIterator(0);
|
||||
}
|
||||
};
|
||||
JsonArrayIterator end() { return JsonArrayIterator(0); }
|
||||
};
|
||||
}
|
||||
|
@ -2,41 +2,28 @@
|
||||
|
||||
#include "ArduinoJson/JsonValue.hpp"
|
||||
|
||||
namespace ArduinoJson
|
||||
{
|
||||
class JsonArray;
|
||||
namespace ArduinoJson {
|
||||
class JsonArray;
|
||||
|
||||
class JsonArrayIterator
|
||||
{
|
||||
friend class JsonArray;
|
||||
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
|
||||
{
|
||||
return _node == other._node;
|
||||
}
|
||||
bool operator==(const JsonArrayIterator &other) const {
|
||||
return _node == other._node;
|
||||
}
|
||||
|
||||
bool operator!=(const JsonArrayIterator& other) const
|
||||
{
|
||||
return _node != other._node;
|
||||
}
|
||||
bool operator!=(const JsonArrayIterator &other) const {
|
||||
return _node != other._node;
|
||||
}
|
||||
|
||||
private:
|
||||
Internals::JsonNode* _node;
|
||||
};
|
||||
private:
|
||||
Internals::JsonNode *_node;
|
||||
};
|
||||
}
|
@ -3,50 +3,42 @@
|
||||
#include "JsonArray.hpp"
|
||||
#include "JsonObject.hpp"
|
||||
|
||||
namespace ArduinoJson
|
||||
{
|
||||
namespace Internals
|
||||
{
|
||||
class JsonParser;
|
||||
}
|
||||
|
||||
class JsonBuffer
|
||||
{
|
||||
friend class JsonContainer;
|
||||
friend class Internals::JsonNode;
|
||||
friend class Internals::JsonParser;
|
||||
|
||||
public:
|
||||
virtual ~JsonBuffer() {};
|
||||
|
||||
JsonArray createArray()
|
||||
{
|
||||
return JsonArray(createArrayNode());
|
||||
}
|
||||
|
||||
JsonObject createObject()
|
||||
{
|
||||
return JsonObject(createObjectNode());
|
||||
}
|
||||
|
||||
JsonValue createValue();
|
||||
|
||||
JsonArray parseArray(char* json);
|
||||
JsonObject parseObject(char* json);
|
||||
JsonValue parseValue(char* json); // TODO: remove
|
||||
|
||||
protected:
|
||||
virtual void* allocateNode() = 0;
|
||||
|
||||
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);
|
||||
};
|
||||
namespace ArduinoJson {
|
||||
namespace Internals {
|
||||
class JsonParser;
|
||||
}
|
||||
|
||||
class JsonBuffer {
|
||||
friend class JsonContainer;
|
||||
friend class Internals::JsonNode;
|
||||
friend class Internals::JsonParser;
|
||||
|
||||
public:
|
||||
virtual ~JsonBuffer(){};
|
||||
|
||||
JsonArray createArray() { return JsonArray(createArrayNode()); }
|
||||
|
||||
JsonObject createObject() { return JsonObject(createObjectNode()); }
|
||||
|
||||
JsonValue createValue();
|
||||
|
||||
JsonArray parseArray(char *json);
|
||||
JsonObject parseObject(char *json);
|
||||
JsonValue parseValue(char *json); // TODO: remove
|
||||
|
||||
protected:
|
||||
virtual void *allocateNode() = 0;
|
||||
|
||||
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);
|
||||
};
|
||||
}
|
@ -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
|
||||
{
|
||||
friend class JsonArray;
|
||||
class JsonContainer : public Printable, public Internals::JsonNodeWrapper {
|
||||
friend class JsonArray;
|
||||
|
||||
public:
|
||||
JsonContainer() {}
|
||||
public:
|
||||
JsonContainer() {}
|
||||
|
||||
explicit JsonContainer(Internals::JsonNode* node)
|
||||
: JsonNodeWrapper(node)
|
||||
{
|
||||
}
|
||||
explicit JsonContainer(Internals::JsonNode *node) : JsonNodeWrapper(node) {}
|
||||
|
||||
size_t size() const;
|
||||
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:
|
||||
protected:
|
||||
Internals::JsonNodeIterator beginChildren() const {
|
||||
return Internals::JsonNodeIterator(_node ? _node->getContainerChild() : 0);
|
||||
}
|
||||
|
||||
Internals::JsonNodeIterator beginChildren() const
|
||||
{
|
||||
return Internals::JsonNodeIterator(_node ? _node->getContainerChild() : 0);
|
||||
}
|
||||
Internals::JsonNodeIterator endChildren() const {
|
||||
return Internals::JsonNodeIterator(0);
|
||||
}
|
||||
|
||||
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();
|
||||
};
|
||||
}
|
@ -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 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);
|
||||
};
|
||||
}
|
@ -2,47 +2,34 @@
|
||||
|
||||
#include "ArduinoJson/JsonObjectKeyValue.hpp"
|
||||
|
||||
namespace ArduinoJson
|
||||
{
|
||||
class JsonObject;
|
||||
namespace ArduinoJson {
|
||||
class JsonObject;
|
||||
|
||||
class JsonObjectIterator
|
||||
{
|
||||
friend class JsonObject;
|
||||
class JsonObjectIterator {
|
||||
friend class JsonObject;
|
||||
|
||||
public:
|
||||
explicit JsonObjectIterator(Internals::JsonNode* node)
|
||||
: _objectKeyValue(node)
|
||||
{
|
||||
}
|
||||
public:
|
||||
explicit JsonObjectIterator(Internals::JsonNode *node)
|
||||
: _objectKeyValue(node) {}
|
||||
|
||||
JsonObjectIterator& operator++()
|
||||
{
|
||||
_objectKeyValue = JsonObjectKeyValue(_objectKeyValue.next());
|
||||
return *this;
|
||||
}
|
||||
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
|
||||
{
|
||||
return _objectKeyValue == other._objectKeyValue;
|
||||
}
|
||||
bool operator==(const JsonObjectIterator &other) const {
|
||||
return _objectKeyValue == other._objectKeyValue;
|
||||
}
|
||||
|
||||
bool operator!=(const JsonObjectIterator& other) const
|
||||
{
|
||||
return _objectKeyValue != other._objectKeyValue;
|
||||
}
|
||||
bool operator!=(const JsonObjectIterator &other) const {
|
||||
return _objectKeyValue != other._objectKeyValue;
|
||||
}
|
||||
|
||||
private:
|
||||
JsonObjectKeyValue _objectKeyValue;
|
||||
};
|
||||
private:
|
||||
JsonObjectKeyValue _objectKeyValue;
|
||||
};
|
||||
}
|
@ -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
|
||||
{
|
||||
return _node == other._node;
|
||||
}
|
||||
bool operator==(const JsonObjectKeyValue &other) const {
|
||||
return _node == other._node;
|
||||
}
|
||||
|
||||
bool operator!=(const JsonObjectKeyValue& other) const
|
||||
{
|
||||
return _node != other._node;
|
||||
}
|
||||
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;
|
||||
};
|
||||
}
|
@ -2,44 +2,36 @@
|
||||
|
||||
#include "Internals/JsonNodeWrapper.hpp"
|
||||
|
||||
namespace ArduinoJson
|
||||
{
|
||||
class JsonArray;
|
||||
class JsonContainer;
|
||||
class JsonObject;
|
||||
namespace ArduinoJson {
|
||||
class JsonArray;
|
||||
class JsonContainer;
|
||||
class JsonObject;
|
||||
|
||||
class JsonValue : public Internals::JsonNodeWrapper
|
||||
{
|
||||
public:
|
||||
class JsonValue : public Internals::JsonNodeWrapper {
|
||||
public:
|
||||
JsonValue() {}
|
||||
|
||||
JsonValue() {}
|
||||
explicit JsonValue(Internals::JsonNode *node) : JsonNodeWrapper(node) {}
|
||||
|
||||
explicit JsonValue(Internals::JsonNode* node)
|
||||
: JsonNodeWrapper(node)
|
||||
{
|
||||
}
|
||||
void operator=(bool);
|
||||
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=(bool);
|
||||
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); }
|
||||
operator bool() const;
|
||||
operator const char *() const;
|
||||
operator double() const;
|
||||
operator long() const;
|
||||
operator int() const { return operator long(); }
|
||||
operator JsonArray() const;
|
||||
operator JsonObject() const;
|
||||
|
||||
operator bool() const;
|
||||
operator const char*() const;
|
||||
operator double() const;
|
||||
operator long() const;
|
||||
operator int() const { return operator long(); }
|
||||
operator JsonArray() const;
|
||||
operator JsonObject() const;
|
||||
void set(double value, int decimals);
|
||||
|
||||
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); }
|
||||
};
|
||||
}
|
@ -3,42 +3,29 @@
|
||||
#include "JsonBuffer.hpp"
|
||||
#include "JsonObject.hpp"
|
||||
|
||||
namespace ArduinoJson
|
||||
{
|
||||
template<int CAPACITY>
|
||||
class StaticJsonBuffer : public JsonBuffer
|
||||
{
|
||||
friend class JsonObject;
|
||||
namespace ArduinoJson {
|
||||
template <int CAPACITY> class StaticJsonBuffer : public JsonBuffer {
|
||||
friend class JsonObject;
|
||||
|
||||
public:
|
||||
public:
|
||||
explicit StaticJsonBuffer() : _size(0) {}
|
||||
|
||||
explicit StaticJsonBuffer()
|
||||
: _size(0)
|
||||
{
|
||||
}
|
||||
virtual ~StaticJsonBuffer() {}
|
||||
|
||||
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++];
|
||||
}
|
||||
|
||||
return &_buffer[_size++];
|
||||
}
|
||||
|
||||
private:
|
||||
Internals::JsonNode _buffer[CAPACITY];
|
||||
int _size;
|
||||
};
|
||||
private:
|
||||
Internals::JsonNode _buffer[CAPACITY];
|
||||
int _size;
|
||||
};
|
||||
}
|
||||
|
1
scripts/format-code.sh
Normal file
1
scripts/format-code.sh
Normal file
@ -0,0 +1 @@
|
||||
find .. -regex ".*\.[ch]pp$" -exec clang-format -i {} \;
|
@ -8,33 +8,26 @@
|
||||
#include "ArduinoJson/Arduino/Print.hpp"
|
||||
#include <cstdio>
|
||||
|
||||
size_t Print::print(const char s[])
|
||||
{
|
||||
size_t n = 0;
|
||||
while (*s)
|
||||
{
|
||||
n += write(*s++);
|
||||
}
|
||||
return n;
|
||||
size_t Print::print(const char s[]) {
|
||||
size_t n = 0;
|
||||
while (*s) {
|
||||
n += write(*s++);
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t Print::print(double value, int digits)
|
||||
{
|
||||
char tmp[32];
|
||||
sprintf(tmp, "%.*g", digits+1, value);
|
||||
return print(tmp);
|
||||
size_t Print::print(double value, int digits) {
|
||||
char tmp[32];
|
||||
sprintf(tmp, "%.*g", digits + 1, value);
|
||||
return print(tmp);
|
||||
}
|
||||
|
||||
size_t Print::print(long value)
|
||||
{
|
||||
char tmp[32];
|
||||
sprintf(tmp, "%ld", value);
|
||||
return print(tmp);
|
||||
size_t Print::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
|
@ -2,44 +2,39 @@
|
||||
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
||||
void IndentedPrint::indent()
|
||||
{
|
||||
if (level < MAX_LEVEL)
|
||||
level++;
|
||||
void IndentedPrint::indent() {
|
||||
if (level < MAX_LEVEL)
|
||||
level++;
|
||||
}
|
||||
|
||||
void IndentedPrint::unindent()
|
||||
{
|
||||
if (level > 0)
|
||||
level--;
|
||||
void IndentedPrint::unindent() {
|
||||
if (level > 0)
|
||||
level--;
|
||||
}
|
||||
|
||||
void IndentedPrint::setTabSize(uint8_t n)
|
||||
{
|
||||
if (n < MAX_TAB_SIZE)
|
||||
tabSize = n;
|
||||
void IndentedPrint::setTabSize(uint8_t n) {
|
||||
if (n < MAX_TAB_SIZE)
|
||||
tabSize = n;
|
||||
}
|
||||
|
||||
size_t IndentedPrint::write(uint8_t c)
|
||||
{
|
||||
size_t n = 0;
|
||||
size_t IndentedPrint::write(uint8_t c) {
|
||||
size_t n = 0;
|
||||
|
||||
if (isNewLine)
|
||||
n += writeTabs();
|
||||
if (isNewLine)
|
||||
n += writeTabs();
|
||||
|
||||
n += sink->write(c);
|
||||
n += sink->write(c);
|
||||
|
||||
isNewLine = c == '\n';
|
||||
isNewLine = c == '\n';
|
||||
|
||||
return n;
|
||||
return n;
|
||||
}
|
||||
|
||||
inline size_t IndentedPrint::writeTabs()
|
||||
{
|
||||
size_t n = 0;
|
||||
inline size_t IndentedPrint::writeTabs() {
|
||||
size_t n = 0;
|
||||
|
||||
for (int i = 0; i < level*tabSize; i++)
|
||||
n += sink->write(' ');
|
||||
for (int i = 0; i < level * tabSize; i++)
|
||||
n += sink->write(' ');
|
||||
|
||||
return n;
|
||||
return n;
|
||||
}
|
@ -7,163 +7,144 @@
|
||||
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
||||
void JsonNode::writeTo(JsonWriter& writer)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case JSON_PROXY:
|
||||
content.asProxy.target->writeTo(writer);
|
||||
break;
|
||||
void JsonNode::writeTo(JsonWriter &writer) {
|
||||
switch (type) {
|
||||
case JSON_PROXY:
|
||||
content.asProxy.target->writeTo(writer);
|
||||
break;
|
||||
|
||||
case JSON_ARRAY:
|
||||
writeArrayTo(writer);
|
||||
break;
|
||||
case JSON_ARRAY:
|
||||
writeArrayTo(writer);
|
||||
break;
|
||||
|
||||
case JSON_OBJECT:
|
||||
writeObjectTo(writer);
|
||||
break;
|
||||
case JSON_OBJECT:
|
||||
writeObjectTo(writer);
|
||||
break;
|
||||
|
||||
case JSON_STRING:
|
||||
writer.writeString(content.asString);
|
||||
break;
|
||||
case JSON_STRING:
|
||||
writer.writeString(content.asString);
|
||||
break;
|
||||
|
||||
case JSON_LONG:
|
||||
writer.writeInteger(content.asInteger);
|
||||
break;
|
||||
case JSON_LONG:
|
||||
writer.writeInteger(content.asInteger);
|
||||
break;
|
||||
|
||||
case JSON_BOOLEAN:
|
||||
writer.writeBoolean(content.asBoolean);
|
||||
break;
|
||||
case JSON_BOOLEAN:
|
||||
writer.writeBoolean(content.asBoolean);
|
||||
break;
|
||||
|
||||
default: // >= JSON_DOUBLE_0_DECIMALS
|
||||
writer.writeDouble(content.asDouble, type - JSON_DOUBLE_0_DECIMALS);
|
||||
break;
|
||||
}
|
||||
default: // >= JSON_DOUBLE_0_DECIMALS
|
||||
writer.writeDouble(content.asDouble, type - JSON_DOUBLE_0_DECIMALS);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void JsonNode::addChild(JsonNode* childToAdd)
|
||||
{
|
||||
if (type == JSON_PROXY)
|
||||
return content.asProxy.target->addChild(childToAdd);
|
||||
void JsonNode::addChild(JsonNode *childToAdd) {
|
||||
if (type == JSON_PROXY)
|
||||
return content.asProxy.target->addChild(childToAdd);
|
||||
|
||||
if (type != JSON_ARRAY && type != JSON_OBJECT)
|
||||
return;
|
||||
if (type != JSON_ARRAY && type != JSON_OBJECT)
|
||||
return;
|
||||
|
||||
JsonNode* lastChild = content.asContainer.child;
|
||||
JsonNode *lastChild = content.asContainer.child;
|
||||
|
||||
if (!lastChild)
|
||||
{
|
||||
content.asContainer.child = childToAdd;
|
||||
return;
|
||||
}
|
||||
if (!lastChild) {
|
||||
content.asContainer.child = childToAdd;
|
||||
return;
|
||||
}
|
||||
|
||||
while (lastChild->next)
|
||||
lastChild = lastChild->next;
|
||||
while (lastChild->next)
|
||||
lastChild = lastChild->next;
|
||||
|
||||
lastChild->next = childToAdd;
|
||||
lastChild->next = childToAdd;
|
||||
}
|
||||
|
||||
void JsonNode::removeChild(JsonNode* childToRemove)
|
||||
{
|
||||
if (type == JSON_PROXY)
|
||||
return content.asProxy.target->removeChild(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)
|
||||
{
|
||||
content.asContainer.child = childToRemove->next;
|
||||
return;
|
||||
}
|
||||
if (content.asContainer.child == childToRemove) {
|
||||
content.asContainer.child = childToRemove->next;
|
||||
return;
|
||||
}
|
||||
|
||||
for (JsonNode* child = content.asContainer.child; child; child = child->next)
|
||||
{
|
||||
if (child->next == childToRemove)
|
||||
child->next = childToRemove->next;
|
||||
}
|
||||
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)
|
||||
{
|
||||
writer.beginArray();
|
||||
if (child) {
|
||||
writer.beginArray();
|
||||
|
||||
for (;;)
|
||||
{
|
||||
child->writeTo(writer);
|
||||
for (;;) {
|
||||
child->writeTo(writer);
|
||||
|
||||
child = child->next;
|
||||
if (!child) break;
|
||||
child = child->next;
|
||||
if (!child)
|
||||
break;
|
||||
|
||||
writer.writeComma();
|
||||
}
|
||||
|
||||
writer.endArray();
|
||||
}
|
||||
else
|
||||
{
|
||||
writer.writeEmptyArray();
|
||||
writer.writeComma();
|
||||
}
|
||||
|
||||
writer.endArray();
|
||||
} 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)
|
||||
{
|
||||
writer.beginObject();
|
||||
if (child) {
|
||||
writer.beginObject();
|
||||
|
||||
for (;;)
|
||||
{
|
||||
writer.writeString(child->content.asKeyValue.key);
|
||||
writer.writeColon();
|
||||
child->content.asKeyValue.value->writeTo(writer);
|
||||
for (;;) {
|
||||
writer.writeString(child->content.asKeyValue.key);
|
||||
writer.writeColon();
|
||||
child->content.asKeyValue.value->writeTo(writer);
|
||||
|
||||
child = child->next;
|
||||
if (!child) break;
|
||||
child = child->next;
|
||||
if (!child)
|
||||
break;
|
||||
|
||||
writer.writeComma();
|
||||
}
|
||||
|
||||
writer.endObject();
|
||||
}
|
||||
else
|
||||
{
|
||||
writer.writeEmptyObject();
|
||||
writer.writeComma();
|
||||
}
|
||||
|
||||
writer.endObject();
|
||||
} 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;
|
||||
*newNode = *this;
|
||||
|
||||
setAsProxyOf(newNode);
|
||||
setAsProxyOf(newNode);
|
||||
}
|
||||
|
||||
void JsonNode::duplicate(JsonNode* other)
|
||||
{
|
||||
if (!other)
|
||||
{
|
||||
type = JSON_UNDEFINED;
|
||||
}
|
||||
else if (other->type == JSON_ARRAY || other->type==JSON_OBJECT)
|
||||
{
|
||||
other->setAsProxyOfSelf();
|
||||
setAsProxyOf(other->content.asProxy.target);
|
||||
}
|
||||
else
|
||||
{
|
||||
*this = *other;
|
||||
}
|
||||
void JsonNode::duplicate(JsonNode *other) {
|
||||
if (!other) {
|
||||
type = JSON_UNDEFINED;
|
||||
} else if (other->type == JSON_ARRAY || other->type == JSON_OBJECT) {
|
||||
other->setAsProxyOfSelf();
|
||||
setAsProxyOf(other->content.asProxy.target);
|
||||
} else {
|
||||
*this = *other;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -8,176 +8,163 @@
|
||||
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
||||
void JsonParser::skipSpaces()
|
||||
{
|
||||
while(isspace(*_ptr)) _ptr++;
|
||||
}
|
||||
|
||||
bool JsonParser::skip(char charToSkip)
|
||||
{
|
||||
skipSpaces();
|
||||
if (*_ptr != charToSkip) return false;
|
||||
void JsonParser::skipSpaces() {
|
||||
while (isspace(*_ptr))
|
||||
_ptr++;
|
||||
skipSpaces();
|
||||
return true;
|
||||
}
|
||||
|
||||
JsonNode* JsonParser::parseAnything()
|
||||
{
|
||||
skipSpaces();
|
||||
|
||||
switch(*_ptr)
|
||||
{
|
||||
case '[':
|
||||
return parseArray();
|
||||
|
||||
case 't':
|
||||
case 'f':
|
||||
return parseBoolean();
|
||||
|
||||
case '-':
|
||||
case '.':
|
||||
case '0':
|
||||
case '1':
|
||||
case '2':
|
||||
case '3':
|
||||
case '4':
|
||||
case '5':
|
||||
case '6':
|
||||
case '7':
|
||||
case '8':
|
||||
case '9':
|
||||
return parseNumber();
|
||||
|
||||
case 'n':
|
||||
return parseNull();
|
||||
|
||||
case '{':
|
||||
return parseObject();
|
||||
|
||||
case '\'':
|
||||
case '\"':
|
||||
return parseString();
|
||||
|
||||
default:
|
||||
return NULL; // invalid JSON
|
||||
}
|
||||
bool JsonParser::skip(char charToSkip) {
|
||||
skipSpaces();
|
||||
if (*_ptr != charToSkip)
|
||||
return false;
|
||||
_ptr++;
|
||||
skipSpaces();
|
||||
return true;
|
||||
}
|
||||
|
||||
JsonNode* JsonParser::parseArray()
|
||||
{
|
||||
JsonNode* node = _buffer->createArrayNode();
|
||||
JsonNode *JsonParser::parseAnything() {
|
||||
skipSpaces();
|
||||
|
||||
skip('[');
|
||||
switch (*_ptr) {
|
||||
case '[':
|
||||
return parseArray();
|
||||
|
||||
if (isEnd())
|
||||
return 0;
|
||||
case 't':
|
||||
case 'f':
|
||||
return parseBoolean();
|
||||
|
||||
case '-':
|
||||
case '.':
|
||||
case '0':
|
||||
case '1':
|
||||
case '2':
|
||||
case '3':
|
||||
case '4':
|
||||
case '5':
|
||||
case '6':
|
||||
case '7':
|
||||
case '8':
|
||||
case '9':
|
||||
return parseNumber();
|
||||
|
||||
case 'n':
|
||||
return parseNull();
|
||||
|
||||
case '{':
|
||||
return parseObject();
|
||||
|
||||
case '\'':
|
||||
case '\"':
|
||||
return parseString();
|
||||
|
||||
default:
|
||||
return NULL; // invalid JSON
|
||||
}
|
||||
}
|
||||
|
||||
JsonNode *JsonParser::parseArray() {
|
||||
JsonNode *node = _buffer->createArrayNode();
|
||||
|
||||
skip('[');
|
||||
|
||||
if (isEnd())
|
||||
return 0;
|
||||
|
||||
if (skip(']'))
|
||||
return node; // empty array
|
||||
|
||||
for (;;) {
|
||||
JsonNode *child = parseAnything();
|
||||
|
||||
if (!child)
|
||||
return 0; // child parsing failed
|
||||
|
||||
node->addChild(child);
|
||||
|
||||
if (skip(']'))
|
||||
return node; // empty array
|
||||
return node; // end of the array
|
||||
|
||||
for(;;)
|
||||
{
|
||||
JsonNode* child = parseAnything();
|
||||
|
||||
if (!child)
|
||||
return 0; // child parsing failed
|
||||
|
||||
node->addChild(child);
|
||||
|
||||
if (skip(']'))
|
||||
return node; // end of the array
|
||||
|
||||
if (!skip(','))
|
||||
return 0; // comma is missing
|
||||
}
|
||||
if (!skip(','))
|
||||
return 0; // comma is missing
|
||||
}
|
||||
}
|
||||
|
||||
JsonNode *JsonParser::parseBoolean()
|
||||
{
|
||||
bool value = *_ptr == 't';
|
||||
JsonNode *JsonParser::parseBoolean() {
|
||||
bool value = *_ptr == 't';
|
||||
|
||||
_ptr += value ? 4 : 5;
|
||||
// 4 = strlen("true")
|
||||
// 5 = strlen("false");
|
||||
_ptr += value ? 4 : 5;
|
||||
// 4 = strlen("true")
|
||||
// 5 = strlen("false");
|
||||
|
||||
return _buffer->createBoolNode(value);
|
||||
return _buffer->createBoolNode(value);
|
||||
}
|
||||
|
||||
JsonNode* JsonParser::parseNumber()
|
||||
{
|
||||
char* endOfLong;
|
||||
long longValue = strtol(_ptr, &endOfLong, 10);
|
||||
JsonNode *JsonParser::parseNumber() {
|
||||
char *endOfLong;
|
||||
long longValue = strtol(_ptr, &endOfLong, 10);
|
||||
|
||||
if (*endOfLong == '.') // stopped on a decimal separator
|
||||
{
|
||||
double value = strtod(_ptr, &_ptr);
|
||||
int decimals = _ptr - endOfLong - 1;
|
||||
return _buffer->createDoubleNode(value, decimals);
|
||||
}
|
||||
else
|
||||
{
|
||||
_ptr = endOfLong;
|
||||
return _buffer->createLongNode(longValue);
|
||||
}
|
||||
if (*endOfLong == '.') // stopped on a decimal separator
|
||||
{
|
||||
double value = strtod(_ptr, &_ptr);
|
||||
int decimals = _ptr - endOfLong - 1;
|
||||
return _buffer->createDoubleNode(value, decimals);
|
||||
} else {
|
||||
_ptr = endOfLong;
|
||||
return _buffer->createLongNode(longValue);
|
||||
}
|
||||
}
|
||||
|
||||
JsonNode* JsonParser::parseNull()
|
||||
{
|
||||
_ptr += 4; // strlen("null")
|
||||
JsonNode *JsonParser::parseNull() {
|
||||
_ptr += 4; // strlen("null")
|
||||
|
||||
return _buffer->createStringNode(0);
|
||||
return _buffer->createStringNode(0);
|
||||
}
|
||||
|
||||
JsonNode* JsonParser::parseObject()
|
||||
{
|
||||
JsonNode* node = _buffer->createObjectNode();
|
||||
JsonNode *JsonParser::parseObject() {
|
||||
JsonNode *node = _buffer->createObjectNode();
|
||||
|
||||
skip('{');
|
||||
skip('{');
|
||||
|
||||
if (isEnd())
|
||||
return 0; // premature ending
|
||||
if (isEnd())
|
||||
return 0; // premature ending
|
||||
|
||||
if (skip('}'))
|
||||
return node; // empty object
|
||||
|
||||
for (;;) {
|
||||
JsonNode *child = parseObjectKeyValue();
|
||||
|
||||
if (!child)
|
||||
return 0; // child parsing failed
|
||||
|
||||
node->addChild(child);
|
||||
|
||||
if (skip('}'))
|
||||
return node; // empty object
|
||||
return node; // end of the object
|
||||
|
||||
for(;;)
|
||||
{
|
||||
JsonNode* child = parseObjectKeyValue();
|
||||
|
||||
if (!child)
|
||||
return 0; // child parsing failed
|
||||
|
||||
node->addChild(child);
|
||||
|
||||
if (skip('}'))
|
||||
return node; // end of the object
|
||||
|
||||
if (!skip(','))
|
||||
return 0; // comma is missing
|
||||
}
|
||||
if (!skip(','))
|
||||
return 0; // comma is missing
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
if (!key)
|
||||
return 0; // failed to extract key
|
||||
|
||||
if (!skip(':'))
|
||||
return 0; // colon is missing
|
||||
if (!skip(':'))
|
||||
return 0; // colon is missing
|
||||
|
||||
JsonNode* value = parseAnything();
|
||||
JsonNode *value = parseAnything();
|
||||
|
||||
if (!value)
|
||||
return 0; // value parsing failed
|
||||
if (!value)
|
||||
return 0; // value parsing failed
|
||||
|
||||
return _buffer->createObjectKeyValueNode(key, value);
|
||||
return _buffer->createObjectKeyValueNode(key, value);
|
||||
}
|
||||
|
||||
JsonNode* JsonParser::parseString()
|
||||
{
|
||||
const char* s = QuotedString::extractFrom(_ptr, &_ptr);
|
||||
return _buffer->createStringNode(s);
|
||||
JsonNode *JsonParser::parseString() {
|
||||
const char *s = QuotedString::extractFrom(_ptr, &_ptr);
|
||||
return _buffer->createStringNode(s);
|
||||
}
|
||||
|
@ -3,22 +3,16 @@
|
||||
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
||||
void JsonWriter::writeString(char const* value)
|
||||
{
|
||||
_length += QuotedString::printTo(value, _sink);
|
||||
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) {
|
||||
_length += _sink->print(value ? "true" : "false");
|
||||
}
|
||||
|
||||
void JsonWriter::writeBoolean(bool value)
|
||||
{
|
||||
_length += _sink->print(value ? "true" : "false");
|
||||
}
|
||||
|
||||
void JsonWriter::writeDouble(double value, int decimals)
|
||||
{
|
||||
_length += _sink->print(value, decimals);
|
||||
void JsonWriter::writeDouble(double value, int decimals) {
|
||||
_length += _sink->print(value, decimals);
|
||||
}
|
@ -7,109 +7,95 @@
|
||||
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
||||
static inline char getSpecialChar(char c)
|
||||
{
|
||||
// Optimized for code size on a 8-bit AVR
|
||||
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)
|
||||
{
|
||||
p += 2;
|
||||
while (p[0] && p[0] != c) {
|
||||
p += 2;
|
||||
}
|
||||
|
||||
return p[1];
|
||||
}
|
||||
|
||||
static inline size_t printCharTo(char c, Print *p) {
|
||||
char specialChar = getSpecialChar(c);
|
||||
|
||||
return specialChar != 0 ? p->write('\\') + p->write(specialChar)
|
||||
: p->write(c);
|
||||
}
|
||||
|
||||
size_t QuotedString::printTo(const char *s, Print *p) {
|
||||
if (!s)
|
||||
return p->print("null");
|
||||
|
||||
size_t n = p->write('\"');
|
||||
|
||||
while (*s) {
|
||||
n += printCharTo(*s++, p);
|
||||
}
|
||||
|
||||
return n + p->write('\"');
|
||||
}
|
||||
|
||||
static char unescapeChar(char c) {
|
||||
// Optimized for code size on a 8-bit AVR
|
||||
|
||||
const char *p = "b\bf\fn\nr\rt\t";
|
||||
|
||||
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 == '\''; }
|
||||
|
||||
char *QuotedString::extractFrom(char *input, char **endPtr) {
|
||||
char firstChar = *input;
|
||||
|
||||
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 c;
|
||||
|
||||
for (;;) {
|
||||
c = *readPtr++;
|
||||
|
||||
if (c == 0) {
|
||||
// premature ending
|
||||
return 0;
|
||||
}
|
||||
|
||||
return p[1];
|
||||
}
|
||||
|
||||
static inline size_t printCharTo(char c, Print* p)
|
||||
{
|
||||
char specialChar = getSpecialChar(c);
|
||||
|
||||
return specialChar != 0
|
||||
? p->write('\\') + p->write(specialChar)
|
||||
: p->write(c);
|
||||
}
|
||||
|
||||
size_t QuotedString::printTo(const char* s, Print* p)
|
||||
{
|
||||
if (!s) return p->print("null");
|
||||
|
||||
size_t n = p->write('\"');
|
||||
|
||||
while (*s)
|
||||
{
|
||||
n += printCharTo(*s++, p);
|
||||
if (c == stopChar) {
|
||||
// closing quote
|
||||
break;
|
||||
}
|
||||
|
||||
return n + p->write('\"');
|
||||
}
|
||||
|
||||
static char unescapeChar(char c)
|
||||
{
|
||||
// Optimized for code size on a 8-bit AVR
|
||||
|
||||
const char* p = "b\bf\fn\nr\rt\t";
|
||||
|
||||
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 == '\'';
|
||||
}
|
||||
|
||||
char* QuotedString::extractFrom(char* input, char** endPtr)
|
||||
{
|
||||
char firstChar = *input;
|
||||
|
||||
if (!isQuote(firstChar))
|
||||
{
|
||||
// must start with a quote
|
||||
return 0;
|
||||
if (c == '\\') {
|
||||
// replace char
|
||||
c = unescapeChar(*readPtr++);
|
||||
}
|
||||
|
||||
char stopChar = firstChar; // closing quote is the same as opening quote
|
||||
*writePtr++ = c;
|
||||
}
|
||||
|
||||
char* startPtr = input + 1; // skip the quote
|
||||
char* readPtr = startPtr;
|
||||
char* writePtr = startPtr;
|
||||
char c;
|
||||
// end the string here
|
||||
*writePtr = 0;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
c = *readPtr++;
|
||||
// update end ptr
|
||||
*endPtr = readPtr;
|
||||
|
||||
if (c == 0)
|
||||
{
|
||||
// premature ending
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (c == stopChar)
|
||||
{
|
||||
// closing quote
|
||||
break;
|
||||
}
|
||||
|
||||
if (c == '\\')
|
||||
{
|
||||
// replace char
|
||||
c = unescapeChar(*readPtr++);
|
||||
}
|
||||
|
||||
*writePtr++ = c;
|
||||
}
|
||||
|
||||
// end the string here
|
||||
*writePtr = 0;
|
||||
|
||||
// update end ptr
|
||||
*endPtr = readPtr;
|
||||
|
||||
return startPtr;
|
||||
return startPtr;
|
||||
}
|
@ -7,11 +7,11 @@
|
||||
|
||||
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;
|
||||
return 1;
|
||||
buffer[length++] = c;
|
||||
buffer[length] = 0;
|
||||
return 1;
|
||||
}
|
@ -5,93 +5,87 @@
|
||||
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);
|
||||
index--;
|
||||
}
|
||||
JsonValue JsonArray::operator[](int index) const {
|
||||
for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it) {
|
||||
if (!index)
|
||||
return JsonValue(*it);
|
||||
index--;
|
||||
}
|
||||
|
||||
return JsonValue();
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
node->duplicate(nestedContainer._node);
|
||||
addChild(node);
|
||||
}
|
||||
|
||||
JsonArray JsonArray::createNestedArray() {
|
||||
JsonNode *node = createNode();
|
||||
|
||||
if (node) {
|
||||
node->setAsArray(_node->getContainerBuffer());
|
||||
addChild(node);
|
||||
}
|
||||
|
||||
return JsonArray(node);
|
||||
}
|
||||
|
||||
JsonArray JsonArray::createNestedArray()
|
||||
{
|
||||
JsonNode* node = createNode();
|
||||
JsonObject JsonArray::createNestedObject() {
|
||||
JsonNode *node = createNode();
|
||||
|
||||
if (node)
|
||||
{
|
||||
node->setAsArray(_node->getContainerBuffer());
|
||||
addChild(node);
|
||||
}
|
||||
if (node) {
|
||||
node->setAsObject(_node->getContainerBuffer());
|
||||
addChild(node);
|
||||
}
|
||||
|
||||
return JsonArray(node);
|
||||
return JsonObject(node);
|
||||
}
|
||||
|
||||
JsonObject JsonArray::createNestedObject()
|
||||
{
|
||||
JsonNode* node = createNode();
|
||||
JsonArrayIterator JsonArray::begin() {
|
||||
if (!_node)
|
||||
return end();
|
||||
|
||||
if (node)
|
||||
{
|
||||
node->setAsObject(_node->getContainerBuffer());
|
||||
addChild(node);
|
||||
}
|
||||
|
||||
return JsonObject(node);
|
||||
}
|
||||
|
||||
JsonArrayIterator JsonArray::begin()
|
||||
{
|
||||
if (!_node)
|
||||
return end();
|
||||
|
||||
return JsonArrayIterator(_node->getContainerChild());
|
||||
return JsonArrayIterator(_node->getContainerChild());
|
||||
}
|
@ -9,103 +9,91 @@
|
||||
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;
|
||||
|
||||
return new (node) JsonNode();
|
||||
}
|
||||
|
||||
JsonNode* JsonBuffer::createNode()
|
||||
{
|
||||
void* node = allocateNode();
|
||||
if (!node) return 0;
|
||||
|
||||
return new (node) JsonNode();
|
||||
JsonArray JsonBuffer::parseArray(char *json) {
|
||||
JsonParser parser(this, json);
|
||||
return JsonArray(parser.parseAnything());
|
||||
}
|
||||
|
||||
JsonArray JsonBuffer::parseArray(char* json)
|
||||
{
|
||||
JsonParser parser(this, json);
|
||||
return JsonArray(parser.parseAnything());
|
||||
JsonObject JsonBuffer::parseObject(char *json) {
|
||||
JsonParser parser(this, json);
|
||||
return JsonObject(parser.parseAnything());
|
||||
}
|
||||
|
||||
JsonObject JsonBuffer::parseObject(char* json)
|
||||
{
|
||||
JsonParser parser(this, json);
|
||||
return JsonObject(parser.parseAnything());
|
||||
JsonValue JsonBuffer::parseValue(char *json) {
|
||||
JsonParser parser(this, json);
|
||||
return JsonValue(parser.parseAnything());
|
||||
}
|
||||
|
||||
JsonValue JsonBuffer::parseValue(char* json)
|
||||
{
|
||||
JsonParser parser(this, json);
|
||||
return JsonValue(parser.parseAnything());
|
||||
JsonNode *JsonBuffer::createArrayNode() {
|
||||
JsonNode *node = createNode();
|
||||
|
||||
if (node)
|
||||
node->setAsArray(this);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
JsonNode* JsonBuffer::createArrayNode()
|
||||
{
|
||||
JsonNode* node = createNode();
|
||||
JsonNode *JsonBuffer::createBoolNode(bool value) {
|
||||
JsonNode *node = createNode();
|
||||
|
||||
if (node)
|
||||
node->setAsArray(this);
|
||||
if (node)
|
||||
node->setAsBoolean(value);
|
||||
|
||||
return node;
|
||||
return node;
|
||||
}
|
||||
|
||||
JsonNode* JsonBuffer::createBoolNode(bool value)
|
||||
{
|
||||
JsonNode* node = createNode();
|
||||
JsonNode *JsonBuffer::createDoubleNode(double value, int decimals) {
|
||||
JsonNode *node = createNode();
|
||||
|
||||
if (node)
|
||||
node->setAsBoolean(value);
|
||||
if (node)
|
||||
node->setAsDouble(value, decimals);
|
||||
|
||||
return node;
|
||||
return node;
|
||||
}
|
||||
|
||||
JsonNode* JsonBuffer::createDoubleNode(double value, int decimals)
|
||||
{
|
||||
JsonNode* node = createNode();
|
||||
JsonNode *JsonBuffer::createLongNode(long value) {
|
||||
JsonNode *node = createNode();
|
||||
|
||||
if (node)
|
||||
node->setAsDouble(value, decimals);
|
||||
if (node)
|
||||
node->setAsLong(value);
|
||||
|
||||
return node;
|
||||
return node;
|
||||
}
|
||||
|
||||
JsonNode* JsonBuffer::createLongNode(long value)
|
||||
{
|
||||
JsonNode* node = createNode();
|
||||
JsonNode *JsonBuffer::createObjectNode() {
|
||||
JsonNode *node = createNode();
|
||||
|
||||
if (node)
|
||||
node->setAsLong(value);
|
||||
if (node)
|
||||
node->setAsObject(this);
|
||||
|
||||
return node;
|
||||
return node;
|
||||
}
|
||||
|
||||
JsonNode* JsonBuffer::createObjectNode()
|
||||
{
|
||||
JsonNode* node = createNode();
|
||||
Internals::JsonNode *JsonBuffer::createObjectKeyValueNode(const char *key,
|
||||
JsonNode *value) {
|
||||
JsonNode *node = createNode();
|
||||
|
||||
if (node)
|
||||
node->setAsObject(this);
|
||||
if (node)
|
||||
node->setAsObjectKeyValue(key, value);
|
||||
|
||||
return node;
|
||||
return node;
|
||||
}
|
||||
|
||||
Internals::JsonNode* JsonBuffer::createObjectKeyValueNode(const char* key, JsonNode* value)
|
||||
{
|
||||
JsonNode* node = createNode();
|
||||
JsonNode *JsonBuffer::createStringNode(const char *value) {
|
||||
JsonNode *node = createNode();
|
||||
|
||||
if (node)
|
||||
node->setAsObjectKeyValue(key, value);
|
||||
if (node)
|
||||
node->setAsString(value);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
JsonNode* JsonBuffer::createStringNode(const char* value)
|
||||
{
|
||||
JsonNode* node = createNode();
|
||||
|
||||
if (node)
|
||||
node->setAsString(value);
|
||||
|
||||
return node;
|
||||
return node;
|
||||
}
|
@ -8,76 +8,68 @@
|
||||
using namespace ArduinoJson;
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
||||
size_t JsonContainer::printTo(char* buffer, size_t bufferSize) const
|
||||
{
|
||||
StringBuilder sb(buffer, bufferSize);
|
||||
return printTo(sb);
|
||||
size_t JsonContainer::printTo(char *buffer, size_t bufferSize) const {
|
||||
StringBuilder sb(buffer, bufferSize);
|
||||
return printTo(sb);
|
||||
}
|
||||
|
||||
size_t JsonContainer::printTo(Print& p) const
|
||||
{
|
||||
CompactJsonWriter writer(&p);
|
||||
_node->writeTo(writer);
|
||||
return writer.bytesWritten();
|
||||
size_t JsonContainer::printTo(Print &p) const {
|
||||
CompactJsonWriter writer(&p);
|
||||
_node->writeTo(writer);
|
||||
return writer.bytesWritten();
|
||||
}
|
||||
|
||||
size_t JsonContainer::prettyPrintTo(char* buffer, size_t bufferSize) const
|
||||
{
|
||||
StringBuilder sb(buffer, bufferSize);
|
||||
return prettyPrintTo(sb);
|
||||
size_t JsonContainer::prettyPrintTo(char *buffer, size_t bufferSize) const {
|
||||
StringBuilder sb(buffer, bufferSize);
|
||||
return prettyPrintTo(sb);
|
||||
}
|
||||
|
||||
size_t JsonContainer::prettyPrintTo(IndentedPrint& p) const
|
||||
{
|
||||
PrettyJsonWriter writer(&p);
|
||||
_node->writeTo(writer);
|
||||
return writer.bytesWritten();
|
||||
size_t JsonContainer::prettyPrintTo(IndentedPrint &p) const {
|
||||
PrettyJsonWriter writer(&p);
|
||||
_node->writeTo(writer);
|
||||
return writer.bytesWritten();
|
||||
}
|
||||
|
||||
size_t JsonContainer::prettyPrintTo(Print& print) const
|
||||
{
|
||||
IndentedPrint indentedPrint = IndentedPrint(print);
|
||||
return prettyPrintTo(indentedPrint);
|
||||
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();
|
||||
return buffer->createNode();
|
||||
}
|
||||
|
||||
bool JsonContainer::operator==(const JsonContainer & other) const
|
||||
{
|
||||
if (_node == other._node) return true;
|
||||
if (!_node || !other._node) return false;
|
||||
return _node->getProxyTarget() == other._node->getProxyTarget();
|
||||
bool JsonContainer::operator==(const JsonContainer &other) const {
|
||||
if (_node == other._node)
|
||||
return true;
|
||||
if (!_node || !other._node)
|
||||
return false;
|
||||
return _node->getProxyTarget() == other._node->getProxyTarget();
|
||||
}
|
||||
|
||||
void JsonContainer::addChild(JsonNode* childToAdd)
|
||||
{
|
||||
if (_node)
|
||||
_node->addChild(childToAdd);
|
||||
void JsonContainer::addChild(JsonNode *childToAdd) {
|
||||
if (_node)
|
||||
_node->addChild(childToAdd);
|
||||
}
|
||||
|
||||
void JsonContainer::removeChild(JsonNode* childToRemove)
|
||||
{
|
||||
if (_node)
|
||||
_node->removeChild(childToRemove);
|
||||
void JsonContainer::removeChild(JsonNode *childToRemove) {
|
||||
if (_node)
|
||||
_node->removeChild(childToRemove);
|
||||
}
|
||||
|
||||
size_t JsonContainer::size() const
|
||||
{
|
||||
int n = 0;
|
||||
size_t JsonContainer::size() const {
|
||||
int n = 0;
|
||||
|
||||
for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it)
|
||||
{
|
||||
n++;
|
||||
}
|
||||
for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it) {
|
||||
n++;
|
||||
}
|
||||
|
||||
return n;
|
||||
return n;
|
||||
}
|
||||
|
||||
|
@ -10,69 +10,62 @@
|
||||
using namespace ArduinoJson;
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
||||
JsonValue JsonObject::operator[](char const* key)
|
||||
{
|
||||
JsonNode* node = getOrCreateNodeAt(key);
|
||||
return JsonValue(node);
|
||||
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))
|
||||
{
|
||||
removeChild(*it);
|
||||
}
|
||||
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());
|
||||
if (node)
|
||||
node->setAsArray(_node->getContainerBuffer());
|
||||
|
||||
return JsonArray(node);
|
||||
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());
|
||||
if (node)
|
||||
node->setAsObject(_node->getContainerBuffer());
|
||||
|
||||
return JsonObject(node);
|
||||
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();
|
||||
}
|
||||
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);
|
||||
newKeyNode->setAsObjectKeyValue(key, newValueNode);
|
||||
|
||||
addChild(newKeyNode);
|
||||
addChild(newKeyNode);
|
||||
|
||||
return newValueNode;
|
||||
return newValueNode;
|
||||
}
|
||||
|
||||
JsonObjectIterator JsonObject::begin()
|
||||
{
|
||||
return JsonObjectIterator(_node->getContainerChild());
|
||||
JsonObjectIterator JsonObject::begin() {
|
||||
return JsonObjectIterator(_node->getContainerChild());
|
||||
}
|
@ -6,56 +6,38 @@
|
||||
|
||||
using namespace ArduinoJson;
|
||||
|
||||
void JsonValue::operator=(bool value)
|
||||
{
|
||||
if (_node)
|
||||
_node->setAsBoolean(value);
|
||||
void JsonValue::operator=(bool value) {
|
||||
if (_node)
|
||||
_node->setAsBoolean(value);
|
||||
}
|
||||
|
||||
void JsonValue::operator=(char const* value)
|
||||
{
|
||||
if (_node)
|
||||
_node->setAsString(value);
|
||||
void JsonValue::operator=(char const *value) {
|
||||
if (_node)
|
||||
_node->setAsString(value);
|
||||
}
|
||||
|
||||
void JsonValue::set(double value, int decimals)
|
||||
{
|
||||
if (_node)
|
||||
_node->setAsDouble(value, decimals);
|
||||
void JsonValue::set(double value, int decimals) {
|
||||
if (_node)
|
||||
_node->setAsDouble(value, decimals);
|
||||
}
|
||||
|
||||
void JsonValue::operator=(int value)
|
||||
{
|
||||
if (_node)
|
||||
_node->setAsLong(value);
|
||||
void JsonValue::operator=(int value) {
|
||||
if (_node)
|
||||
_node->setAsLong(value);
|
||||
}
|
||||
|
||||
JsonValue::operator bool() const
|
||||
{
|
||||
return _node ? _node->getAsBoolean() : false;
|
||||
JsonValue::operator bool() const {
|
||||
return _node ? _node->getAsBoolean() : false;
|
||||
}
|
||||
|
||||
JsonValue::operator char const*() const
|
||||
{
|
||||
return _node ? _node->getAsString() : 0;
|
||||
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); }
|
@ -6,75 +6,65 @@
|
||||
|
||||
using namespace ArduinoJson;
|
||||
|
||||
struct Person
|
||||
{
|
||||
int id;
|
||||
char name[32];
|
||||
struct Person {
|
||||
int id;
|
||||
char name[32];
|
||||
};
|
||||
|
||||
class Issue10 : public testing::Test
|
||||
{
|
||||
class Issue10 : public testing::Test {
|
||||
protected:
|
||||
virtual void SetUp() {
|
||||
Person boss;
|
||||
boss.id = 1;
|
||||
strcpy(boss.name, "Jeff");
|
||||
Person employee;
|
||||
employee.id = 2;
|
||||
strcpy(employee.name, "John");
|
||||
persons[0] = boss;
|
||||
persons[1] = employee;
|
||||
}
|
||||
|
||||
virtual void SetUp()
|
||||
{
|
||||
Person boss;
|
||||
boss.id = 1;
|
||||
strcpy(boss.name, "Jeff");
|
||||
Person employee;
|
||||
employee.id = 2;
|
||||
strcpy(employee.name, "John");
|
||||
persons[0] = boss;
|
||||
persons[1] = employee;
|
||||
}
|
||||
void checkJsonString(JsonContainer &p) {
|
||||
char buffer[256];
|
||||
p.printTo(buffer, sizeof(buffer));
|
||||
|
||||
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;
|
||||
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++)
|
||||
{
|
||||
JsonObject object = json.createObject();
|
||||
for (int i = 0; i < 2; i++) {
|
||||
JsonObject object = json.createObject();
|
||||
|
||||
object["id"] = persons[i].id;
|
||||
object["name"] = persons[i].name;
|
||||
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);
|
||||
checkJsonString(array);
|
||||
nodeCountMustBe(15);
|
||||
}
|
||||
|
||||
TEST_F(Issue10, PopulateArrayByCreatingNestedObjects)
|
||||
{
|
||||
JsonArray array = json.createArray();
|
||||
TEST_F(Issue10, PopulateArrayByCreatingNestedObjects) {
|
||||
JsonArray array = json.createArray();
|
||||
|
||||
for (int i = 0; i < 2; i++)
|
||||
{
|
||||
JsonObject object = array.createNestedObject();
|
||||
for (int i = 0; i < 2; i++) {
|
||||
JsonObject object = array.createNestedObject();
|
||||
|
||||
object["id"] = persons[i].id;
|
||||
object["name"] = persons[i].name;
|
||||
}
|
||||
object["id"] = persons[i].id;
|
||||
object["name"] = persons[i].name;
|
||||
}
|
||||
|
||||
checkJsonString(array);
|
||||
nodeCountMustBe(11);
|
||||
checkJsonString(array);
|
||||
nodeCountMustBe(11);
|
||||
}
|
||||
|
@ -4,149 +4,123 @@
|
||||
|
||||
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)
|
||||
{
|
||||
elementAtIndexMustBe(0, expected);
|
||||
}
|
||||
template <typename T> void firstElementMustBe(T expected) {
|
||||
elementAtIndexMustBe(0, expected);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void secondElementMustBe(T expected)
|
||||
{
|
||||
elementAtIndexMustBe(1, 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;
|
||||
StaticJsonBuffer<42> json;
|
||||
JsonArray array;
|
||||
|
||||
private:
|
||||
template<typename T>
|
||||
void elementAtIndexMustBe(int index, T expected)
|
||||
{
|
||||
EXPECT_EQ(expected, array[index].as<T>());
|
||||
}
|
||||
template <typename T> void elementAtIndexMustBe(int index, T expected) {
|
||||
EXPECT_EQ(expected, array[index].as<T>());
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, InitialSizeIsZero)
|
||||
{
|
||||
sizeMustBe(0);
|
||||
nodeCountMustBe(1);
|
||||
TEST_F(JsonArray_Container_Tests, InitialSizeIsZero) {
|
||||
sizeMustBe(0);
|
||||
nodeCountMustBe(1);
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, Grow_WhenValuesAreAdded)
|
||||
{
|
||||
array.add("hello");
|
||||
sizeMustBe(1);
|
||||
nodeCountMustBe(2);
|
||||
TEST_F(JsonArray_Container_Tests, Grow_WhenValuesAreAdded) {
|
||||
array.add("hello");
|
||||
sizeMustBe(1);
|
||||
nodeCountMustBe(2);
|
||||
|
||||
array.add("world");
|
||||
sizeMustBe(2);
|
||||
nodeCountMustBe(3);
|
||||
array.add("world");
|
||||
sizeMustBe(2);
|
||||
nodeCountMustBe(3);
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, CanStoreIntegers)
|
||||
{
|
||||
array.add(123);
|
||||
array.add(456);
|
||||
TEST_F(JsonArray_Container_Tests, CanStoreIntegers) {
|
||||
array.add(123);
|
||||
array.add(456);
|
||||
|
||||
firstElementMustBe(123);
|
||||
secondElementMustBe(456);
|
||||
nodeCountMustBe(3);
|
||||
firstElementMustBe(123);
|
||||
secondElementMustBe(456);
|
||||
nodeCountMustBe(3);
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, CanStoreDoubles)
|
||||
{
|
||||
array.add(123.45);
|
||||
array.add(456.78);
|
||||
TEST_F(JsonArray_Container_Tests, CanStoreDoubles) {
|
||||
array.add(123.45);
|
||||
array.add(456.78);
|
||||
|
||||
firstElementMustBe(123.45);
|
||||
secondElementMustBe(456.78);
|
||||
nodeCountMustBe(3);
|
||||
firstElementMustBe(123.45);
|
||||
secondElementMustBe(456.78);
|
||||
nodeCountMustBe(3);
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, CanStoreBooleans)
|
||||
{
|
||||
array.add(true);
|
||||
array.add(false);
|
||||
TEST_F(JsonArray_Container_Tests, CanStoreBooleans) {
|
||||
array.add(true);
|
||||
array.add(false);
|
||||
|
||||
firstElementMustBe(true);
|
||||
secondElementMustBe(false);
|
||||
nodeCountMustBe(3);
|
||||
firstElementMustBe(true);
|
||||
secondElementMustBe(false);
|
||||
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);
|
||||
array.add(firstString);
|
||||
array.add(secondString);
|
||||
|
||||
firstElementMustBe(firstString);
|
||||
secondElementMustBe(secondString);
|
||||
nodeCountMustBe(3);
|
||||
firstElementMustBe(firstString);
|
||||
secondElementMustBe(secondString);
|
||||
nodeCountMustBe(3);
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, CanStoreNestedArrays)
|
||||
{
|
||||
JsonArray innerarray1 = json.createArray();
|
||||
JsonArray innerarray2 = json.createArray();
|
||||
TEST_F(JsonArray_Container_Tests, CanStoreNestedArrays) {
|
||||
JsonArray innerarray1 = json.createArray();
|
||||
JsonArray innerarray2 = json.createArray();
|
||||
|
||||
array.add(innerarray1);
|
||||
array.add(innerarray2);
|
||||
array.add(innerarray1);
|
||||
array.add(innerarray2);
|
||||
|
||||
firstElementMustBe(innerarray1);
|
||||
secondElementMustBe(innerarray2);
|
||||
nodeCountMustBe(1 + 3 + 3);
|
||||
firstElementMustBe(innerarray1);
|
||||
secondElementMustBe(innerarray2);
|
||||
nodeCountMustBe(1 + 3 + 3);
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, CanStoreNestedObjects)
|
||||
{
|
||||
JsonObject innerObject1 = json.createObject();
|
||||
JsonObject innerObject2 = json.createObject();
|
||||
TEST_F(JsonArray_Container_Tests, CanStoreNestedObjects) {
|
||||
JsonObject innerObject1 = json.createObject();
|
||||
JsonObject innerObject2 = json.createObject();
|
||||
|
||||
array.add(innerObject1);
|
||||
array.add(innerObject2);
|
||||
array.add(innerObject1);
|
||||
array.add(innerObject2);
|
||||
|
||||
firstElementMustBe(innerObject1);
|
||||
secondElementMustBe(innerObject2);
|
||||
nodeCountMustBe(1 + 3 + 3);
|
||||
firstElementMustBe(innerObject1);
|
||||
secondElementMustBe(innerObject2);
|
||||
nodeCountMustBe(1 + 3 + 3);
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, CanCreateNestedArrays)
|
||||
{
|
||||
JsonArray innerarray1 = array.createNestedArray();
|
||||
JsonArray innerarray2 = array.createNestedArray();
|
||||
TEST_F(JsonArray_Container_Tests, CanCreateNestedArrays) {
|
||||
JsonArray innerarray1 = array.createNestedArray();
|
||||
JsonArray innerarray2 = array.createNestedArray();
|
||||
|
||||
firstElementMustBe(innerarray1);
|
||||
secondElementMustBe(innerarray2);
|
||||
nodeCountMustBe(1 + 1 + 1);
|
||||
firstElementMustBe(innerarray1);
|
||||
secondElementMustBe(innerarray2);
|
||||
nodeCountMustBe(1 + 1 + 1);
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, CanCreateNestedObjects)
|
||||
{
|
||||
JsonObject innerObject1 = array.createNestedObject();
|
||||
JsonObject innerObject2 = array.createNestedObject();
|
||||
TEST_F(JsonArray_Container_Tests, CanCreateNestedObjects) {
|
||||
JsonObject innerObject1 = array.createNestedObject();
|
||||
JsonObject innerObject2 = array.createNestedObject();
|
||||
|
||||
firstElementMustBe(innerObject1);
|
||||
secondElementMustBe(innerObject2);
|
||||
nodeCountMustBe(3);
|
||||
firstElementMustBe(innerObject1);
|
||||
secondElementMustBe(innerObject2);
|
||||
nodeCountMustBe(3);
|
||||
}
|
@ -4,22 +4,21 @@
|
||||
|
||||
using namespace ArduinoJson;
|
||||
|
||||
TEST(JsonArray_Iterator_Test, SimpleTest)
|
||||
{
|
||||
StaticJsonBuffer<42> jsonBuffer;
|
||||
TEST(JsonArray_Iterator_Test, SimpleTest) {
|
||||
StaticJsonBuffer<42> jsonBuffer;
|
||||
|
||||
JsonArray array = jsonBuffer.createArray();
|
||||
array.add(12);
|
||||
array.add(34);
|
||||
JsonArray array = jsonBuffer.createArray();
|
||||
array.add(12);
|
||||
array.add(34);
|
||||
|
||||
JsonArrayIterator it = array.begin();
|
||||
JsonArrayIterator end = array.end();
|
||||
JsonArrayIterator it = array.begin();
|
||||
JsonArrayIterator end = array.end();
|
||||
|
||||
EXPECT_NE(end, it);
|
||||
EXPECT_EQ(12, (*it).as<int>()); // TODO: use ->
|
||||
++it;
|
||||
EXPECT_NE(end, it);
|
||||
EXPECT_EQ(34, (*it).as<int>()); // TODO: use ->
|
||||
++it;
|
||||
EXPECT_EQ(array.end(), it);
|
||||
EXPECT_NE(end, it);
|
||||
EXPECT_EQ(12, (*it).as<int>()); // TODO: use ->
|
||||
++it;
|
||||
EXPECT_NE(end, it);
|
||||
EXPECT_EQ(34, (*it).as<int>()); // TODO: use ->
|
||||
++it;
|
||||
EXPECT_EQ(array.end(), it);
|
||||
}
|
@ -11,84 +11,68 @@
|
||||
|
||||
using namespace ArduinoJson;
|
||||
|
||||
class JsonArray_PrettyPrintTo_Tests : public testing::Test
|
||||
{
|
||||
class JsonArray_PrettyPrintTo_Tests : public testing::Test {
|
||||
protected:
|
||||
JsonArray array;
|
||||
StaticJsonBuffer<30> json;
|
||||
JsonArray array;
|
||||
StaticJsonBuffer<30> json;
|
||||
|
||||
virtual void SetUp()
|
||||
{
|
||||
array = json.createArray();
|
||||
}
|
||||
virtual void SetUp() { array = json.createArray(); }
|
||||
|
||||
void outputMustBe(const char* expected)
|
||||
{
|
||||
size_t n = array.prettyPrintTo(buffer, sizeof(buffer));
|
||||
EXPECT_STREQ(expected, buffer);
|
||||
EXPECT_EQ(strlen(expected), n);
|
||||
}
|
||||
void outputMustBe(const char *expected) {
|
||||
size_t n = array.prettyPrintTo(buffer, sizeof(buffer));
|
||||
EXPECT_STREQ(expected, buffer);
|
||||
EXPECT_EQ(strlen(expected), n);
|
||||
}
|
||||
|
||||
private:
|
||||
char buffer[256];
|
||||
char buffer[256];
|
||||
};
|
||||
|
||||
TEST_F(JsonArray_PrettyPrintTo_Tests, Empty)
|
||||
{
|
||||
outputMustBe("[]");
|
||||
TEST_F(JsonArray_PrettyPrintTo_Tests, Empty) { outputMustBe("[]"); }
|
||||
|
||||
TEST_F(JsonArray_PrettyPrintTo_Tests, OneElement) {
|
||||
array.add(1);
|
||||
|
||||
outputMustBe("[\r\n"
|
||||
" 1\r\n"
|
||||
"]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrettyPrintTo_Tests, OneElement)
|
||||
{
|
||||
array.add(1);
|
||||
TEST_F(JsonArray_PrettyPrintTo_Tests, TwoElements) {
|
||||
array.add(1);
|
||||
array.add(2);
|
||||
|
||||
outputMustBe(
|
||||
"[\r\n"
|
||||
" 1\r\n"
|
||||
"]");
|
||||
outputMustBe("[\r\n"
|
||||
" 1,\r\n"
|
||||
" 2\r\n"
|
||||
"]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrettyPrintTo_Tests, TwoElements)
|
||||
{
|
||||
array.add(1);
|
||||
array.add(2);
|
||||
TEST_F(JsonArray_PrettyPrintTo_Tests, EmptyNestedArrays) {
|
||||
array.createNestedArray();
|
||||
array.createNestedArray();
|
||||
|
||||
outputMustBe(
|
||||
"[\r\n"
|
||||
" 1,\r\n"
|
||||
" 2\r\n"
|
||||
"]");
|
||||
outputMustBe("[\r\n"
|
||||
" [],\r\n"
|
||||
" []\r\n"
|
||||
"]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrettyPrintTo_Tests, EmptyNestedArrays)
|
||||
{
|
||||
array.createNestedArray();
|
||||
array.createNestedArray();
|
||||
TEST_F(JsonArray_PrettyPrintTo_Tests, NestedArrays) {
|
||||
JsonArray nested1 = array.createNestedArray();
|
||||
nested1.add(1);
|
||||
nested1.add(2);
|
||||
|
||||
outputMustBe(
|
||||
"[\r\n"
|
||||
" [],\r\n"
|
||||
" []\r\n"
|
||||
"]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrettyPrintTo_Tests, NestedArrays)
|
||||
{
|
||||
JsonArray nested1 = array.createNestedArray();
|
||||
nested1.add(1);
|
||||
nested1.add(2);
|
||||
|
||||
JsonObject nested2 = array.createNestedObject();
|
||||
nested2["key"] = 3;
|
||||
|
||||
outputMustBe(
|
||||
"[\r\n"
|
||||
" [\r\n"
|
||||
" 1,\r\n"
|
||||
" 2\r\n"
|
||||
" ],\r\n"
|
||||
" {\r\n"
|
||||
" \"key\": 3\r\n"
|
||||
" }\r\n"
|
||||
"]");
|
||||
JsonObject nested2 = array.createNestedObject();
|
||||
nested2["key"] = 3;
|
||||
|
||||
outputMustBe("[\r\n"
|
||||
" [\r\n"
|
||||
" 1,\r\n"
|
||||
" 2\r\n"
|
||||
" ],\r\n"
|
||||
" {\r\n"
|
||||
" \"key\": 3\r\n"
|
||||
" }\r\n"
|
||||
"]");
|
||||
}
|
@ -10,141 +10,118 @@
|
||||
|
||||
using namespace ArduinoJson;
|
||||
|
||||
class JsonArray_PrintTo_Tests : public testing::Test
|
||||
{
|
||||
class JsonArray_PrintTo_Tests : public testing::Test {
|
||||
protected:
|
||||
JsonArray array;
|
||||
StaticJsonBuffer<3> json;
|
||||
JsonArray array;
|
||||
StaticJsonBuffer<3> json;
|
||||
|
||||
virtual void SetUp()
|
||||
{
|
||||
array = json.createArray();
|
||||
}
|
||||
virtual void SetUp() { array = json.createArray(); }
|
||||
|
||||
void outputMustBe(const char* expected)
|
||||
{
|
||||
size_t n = array.printTo(buffer, sizeof(buffer));
|
||||
EXPECT_STREQ(expected, buffer);
|
||||
EXPECT_EQ(strlen(expected), n);
|
||||
}
|
||||
void outputMustBe(const char *expected) {
|
||||
size_t n = array.printTo(buffer, sizeof(buffer));
|
||||
EXPECT_STREQ(expected, buffer);
|
||||
EXPECT_EQ(strlen(expected), n);
|
||||
}
|
||||
|
||||
private:
|
||||
char buffer[256];
|
||||
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);
|
||||
|
||||
outputMustBe("[null]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, Null)
|
||||
{
|
||||
array.add((char*) 0);
|
||||
TEST_F(JsonArray_PrintTo_Tests, OneString) {
|
||||
array.add("hello");
|
||||
|
||||
outputMustBe("[null]");
|
||||
outputMustBe("[\"hello\"]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, OneString)
|
||||
{
|
||||
array.add("hello");
|
||||
TEST_F(JsonArray_PrintTo_Tests, TwoStrings) {
|
||||
array.add("hello");
|
||||
array.add("world");
|
||||
|
||||
outputMustBe("[\"hello\"]");
|
||||
outputMustBe("[\"hello\",\"world\"]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, TwoStrings)
|
||||
{
|
||||
array.add("hello");
|
||||
array.add("world");
|
||||
TEST_F(JsonArray_PrintTo_Tests, OneStringOverCapacity) {
|
||||
array.add("hello");
|
||||
array.add("world");
|
||||
array.add("lost");
|
||||
|
||||
outputMustBe("[\"hello\",\"world\"]");
|
||||
outputMustBe("[\"hello\",\"world\"]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, OneStringOverCapacity)
|
||||
{
|
||||
array.add("hello");
|
||||
array.add("world");
|
||||
array.add("lost");
|
||||
|
||||
outputMustBe("[\"hello\",\"world\"]");
|
||||
TEST_F(JsonArray_PrintTo_Tests, OneDoubleDefaultDigits) {
|
||||
array.add(3.14159265358979323846);
|
||||
outputMustBe("[3.14]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, OneDoubleDefaultDigits)
|
||||
{
|
||||
array.add(3.14159265358979323846);
|
||||
outputMustBe("[3.14]");
|
||||
TEST_F(JsonArray_PrintTo_Tests, OneDoubleFourDigits) {
|
||||
array.add(3.14159265358979323846, 4);
|
||||
outputMustBe("[3.1416]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, OneDoubleFourDigits)
|
||||
{
|
||||
array.add(3.14159265358979323846, 4);
|
||||
outputMustBe("[3.1416]");
|
||||
TEST_F(JsonArray_PrintTo_Tests, OneInteger) {
|
||||
array.add(1);
|
||||
|
||||
outputMustBe("[1]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, OneInteger)
|
||||
{
|
||||
array.add(1);
|
||||
TEST_F(JsonArray_PrintTo_Tests, TwoIntegers) {
|
||||
array.add(1);
|
||||
array.add(2);
|
||||
|
||||
outputMustBe("[1]");
|
||||
outputMustBe("[1,2]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, TwoIntegers)
|
||||
{
|
||||
array.add(1);
|
||||
array.add(2);
|
||||
TEST_F(JsonArray_PrintTo_Tests, OneIntegerOverCapacity) {
|
||||
array.add(1);
|
||||
array.add(2);
|
||||
array.add(3);
|
||||
|
||||
outputMustBe("[1,2]");
|
||||
outputMustBe("[1,2]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, OneIntegerOverCapacity)
|
||||
{
|
||||
array.add(1);
|
||||
array.add(2);
|
||||
array.add(3);
|
||||
TEST_F(JsonArray_PrintTo_Tests, OneTrue) {
|
||||
array.add(true);
|
||||
|
||||
outputMustBe("[1,2]");
|
||||
outputMustBe("[true]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, OneTrue)
|
||||
{
|
||||
array.add(true);
|
||||
TEST_F(JsonArray_PrintTo_Tests, OneFalse) {
|
||||
array.add(false);
|
||||
|
||||
outputMustBe("[true]");
|
||||
outputMustBe("[false]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, OneFalse)
|
||||
{
|
||||
array.add(false);
|
||||
TEST_F(JsonArray_PrintTo_Tests, TwoBooleans) {
|
||||
array.add(false);
|
||||
array.add(true);
|
||||
|
||||
outputMustBe("[false]");
|
||||
outputMustBe("[false,true]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, TwoBooleans)
|
||||
{
|
||||
array.add(false);
|
||||
array.add(true);
|
||||
TEST_F(JsonArray_PrintTo_Tests, OneBooleanOverCapacity) {
|
||||
array.add(false);
|
||||
array.add(true);
|
||||
array.add(false);
|
||||
|
||||
outputMustBe("[false,true]");
|
||||
outputMustBe("[false,true]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, OneBooleanOverCapacity)
|
||||
{
|
||||
array.add(false);
|
||||
array.add(true);
|
||||
array.add(false);
|
||||
TEST_F(JsonArray_PrintTo_Tests, OneEmptyNestedArray) {
|
||||
array.createNestedArray();
|
||||
|
||||
outputMustBe("[false,true]");
|
||||
outputMustBe("[[]]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, OneEmptyNestedArray)
|
||||
{
|
||||
array.createNestedArray();
|
||||
TEST_F(JsonArray_PrintTo_Tests, OneEmptyNestedHash) {
|
||||
array.createNestedObject();
|
||||
|
||||
outputMustBe("[[]]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, OneEmptyNestedHash)
|
||||
{
|
||||
array.createNestedObject();
|
||||
|
||||
outputMustBe("[{}]");
|
||||
outputMustBe("[{}]");
|
||||
}
|
@ -4,119 +4,105 @@
|
||||
|
||||
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;
|
||||
StaticJsonBuffer<42> json;
|
||||
JsonObject object;
|
||||
};
|
||||
|
||||
TEST_F(JsonObject_Container_Tests, InitialSizeIsZero)
|
||||
{
|
||||
EXPECT_EQ(0, object.size());
|
||||
TEST_F(JsonObject_Container_Tests, InitialSizeIsZero) {
|
||||
EXPECT_EQ(0, object.size());
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Container_Tests, Grow_WhenValuesAreAdded)
|
||||
{
|
||||
object["hello"];
|
||||
EXPECT_EQ(1, object.size());
|
||||
TEST_F(JsonObject_Container_Tests, Grow_WhenValuesAreAdded) {
|
||||
object["hello"];
|
||||
EXPECT_EQ(1, object.size());
|
||||
|
||||
object["world"];
|
||||
EXPECT_EQ(2, object.size());
|
||||
object["world"];
|
||||
EXPECT_EQ(2, object.size());
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Container_Tests, DoNotGrow_WhenSameValueIsAdded)
|
||||
{
|
||||
object["hello"];
|
||||
EXPECT_EQ(1, object.size());
|
||||
TEST_F(JsonObject_Container_Tests, DoNotGrow_WhenSameValueIsAdded) {
|
||||
object["hello"];
|
||||
EXPECT_EQ(1, object.size());
|
||||
|
||||
object["hello"];
|
||||
EXPECT_EQ(1, object.size());
|
||||
object["hello"];
|
||||
EXPECT_EQ(1, object.size());
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Container_Tests, Shrink_WhenValuesAreRemoved)
|
||||
{
|
||||
object["hello"];
|
||||
object["world"];
|
||||
TEST_F(JsonObject_Container_Tests, Shrink_WhenValuesAreRemoved) {
|
||||
object["hello"];
|
||||
object["world"];
|
||||
|
||||
object.remove("hello");
|
||||
EXPECT_EQ(1, object.size());
|
||||
object.remove("hello");
|
||||
EXPECT_EQ(1, object.size());
|
||||
|
||||
object.remove("world");
|
||||
EXPECT_EQ(0, object.size());
|
||||
object.remove("world");
|
||||
EXPECT_EQ(0, object.size());
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Container_Tests, DoNotShrink_WhenRemoveIsCalledWithAWrongKey)
|
||||
{
|
||||
object["hello"];
|
||||
object["world"];
|
||||
TEST_F(JsonObject_Container_Tests,
|
||||
DoNotShrink_WhenRemoveIsCalledWithAWrongKey) {
|
||||
object["hello"];
|
||||
object["world"];
|
||||
|
||||
object.remove(":-P");
|
||||
object.remove(":-P");
|
||||
|
||||
EXPECT_EQ(2, object.size());
|
||||
EXPECT_EQ(2, object.size());
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Container_Tests, CanStoreIntegers)
|
||||
{
|
||||
object["hello"] = 123;
|
||||
object["world"] = 456;
|
||||
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)
|
||||
{
|
||||
object["hello"] = 123.45;
|
||||
object["world"] = 456.78;
|
||||
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)
|
||||
{
|
||||
object["hello"] = true;
|
||||
object["world"] = false;
|
||||
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)
|
||||
{
|
||||
object["hello"] = "h3110";
|
||||
object["world"] = "w0r1d";
|
||||
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)
|
||||
{
|
||||
JsonArray innerarray1 = json.createArray();
|
||||
JsonArray innerarray2 = json.createArray();
|
||||
TEST_F(JsonObject_Container_Tests, CanStoreInnerArrays) {
|
||||
JsonArray innerarray1 = json.createArray();
|
||||
JsonArray innerarray2 = json.createArray();
|
||||
|
||||
object["hello"] = innerarray1;
|
||||
object["world"] = innerarray2;
|
||||
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)
|
||||
{
|
||||
JsonObject innerObject1 = json.createObject();
|
||||
JsonObject innerObject2 = json.createObject();
|
||||
TEST_F(JsonObject_Container_Tests, CanStoreInnerObjects) {
|
||||
JsonObject innerObject1 = json.createObject();
|
||||
JsonObject innerObject2 = json.createObject();
|
||||
|
||||
object["hello"] = innerObject1;
|
||||
object["world"] = innerObject2;
|
||||
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"]);
|
||||
}
|
@ -4,23 +4,23 @@
|
||||
|
||||
using namespace ArduinoJson;
|
||||
|
||||
TEST(JsonObject_Iterator_Test, SimpleTest)
|
||||
{
|
||||
StaticJsonBuffer<42> jsonBuffer;
|
||||
TEST(JsonObject_Iterator_Test, SimpleTest) {
|
||||
StaticJsonBuffer<42> jsonBuffer;
|
||||
|
||||
JsonObject object = jsonBuffer.createObject();
|
||||
object["ab"] = 12;
|
||||
object["cd"] = 34;
|
||||
JsonObject object = jsonBuffer.createObject();
|
||||
object["ab"] = 12;
|
||||
object["cd"] = 34;
|
||||
|
||||
JsonObjectIterator it = object.begin();
|
||||
JsonObjectIterator end = object.end();
|
||||
JsonObjectIterator it = object.begin();
|
||||
JsonObjectIterator end = object.end();
|
||||
|
||||
EXPECT_NE(end, it);
|
||||
EXPECT_STREQ("ab", it->key());
|
||||
EXPECT_EQ(12, it->value().as<int>()); ++it;
|
||||
EXPECT_NE(end, it);
|
||||
EXPECT_STREQ("cd", it->key());
|
||||
EXPECT_EQ(34, it->value().as<int>());
|
||||
++it;
|
||||
EXPECT_EQ(object.end(), it);
|
||||
EXPECT_NE(end, it);
|
||||
EXPECT_STREQ("ab", it->key());
|
||||
EXPECT_EQ(12, it->value().as<int>());
|
||||
++it;
|
||||
EXPECT_NE(end, it);
|
||||
EXPECT_STREQ("cd", it->key());
|
||||
EXPECT_EQ(34, it->value().as<int>());
|
||||
++it;
|
||||
EXPECT_EQ(object.end(), it);
|
||||
}
|
@ -10,82 +10,66 @@
|
||||
|
||||
using namespace ArduinoJson;
|
||||
|
||||
class JsonObject_PrettyPrintTo_Tests : public testing::Test
|
||||
{
|
||||
class JsonObject_PrettyPrintTo_Tests : public testing::Test {
|
||||
protected:
|
||||
JsonObject object;
|
||||
StaticJsonBuffer<30> json;
|
||||
JsonObject object;
|
||||
StaticJsonBuffer<30> json;
|
||||
|
||||
virtual void SetUp()
|
||||
{
|
||||
object = json.createObject();
|
||||
}
|
||||
virtual void SetUp() { object = json.createObject(); }
|
||||
|
||||
void outputMustBe(const char* expected)
|
||||
{
|
||||
size_t n = object.prettyPrintTo(buffer, sizeof(buffer));
|
||||
EXPECT_STREQ(expected, buffer);
|
||||
EXPECT_EQ(strlen(expected), n);
|
||||
}
|
||||
void outputMustBe(const char *expected) {
|
||||
size_t n = object.prettyPrintTo(buffer, sizeof(buffer));
|
||||
EXPECT_STREQ(expected, buffer);
|
||||
EXPECT_EQ(strlen(expected), n);
|
||||
}
|
||||
|
||||
private:
|
||||
char buffer[256];
|
||||
char buffer[256];
|
||||
};
|
||||
|
||||
TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyObject)
|
||||
{
|
||||
outputMustBe("{}");
|
||||
TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyObject) { outputMustBe("{}"); }
|
||||
|
||||
TEST_F(JsonObject_PrettyPrintTo_Tests, OneMember) {
|
||||
object["key"] = "value";
|
||||
|
||||
outputMustBe("{\r\n"
|
||||
" \"key\": \"value\"\r\n"
|
||||
"}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_PrettyPrintTo_Tests, OneMember)
|
||||
{
|
||||
object["key"] = "value";
|
||||
TEST_F(JsonObject_PrettyPrintTo_Tests, TwoMembers) {
|
||||
object["key1"] = "value1";
|
||||
object["key2"] = "value2";
|
||||
|
||||
outputMustBe(
|
||||
"{\r\n"
|
||||
" \"key\": \"value\"\r\n"
|
||||
"}");
|
||||
outputMustBe("{\r\n"
|
||||
" \"key1\": \"value1\",\r\n"
|
||||
" \"key2\": \"value2\"\r\n"
|
||||
"}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_PrettyPrintTo_Tests, TwoMembers)
|
||||
{
|
||||
object["key1"] = "value1";
|
||||
object["key2"] = "value2";
|
||||
TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyNestedContainers) {
|
||||
object.createNestedObject("key1");
|
||||
object.createNestedArray("key2");
|
||||
|
||||
outputMustBe(
|
||||
"{\r\n"
|
||||
" \"key1\": \"value1\",\r\n"
|
||||
" \"key2\": \"value2\"\r\n"
|
||||
"}");
|
||||
outputMustBe("{\r\n"
|
||||
" \"key1\": {},\r\n"
|
||||
" \"key2\": []\r\n"
|
||||
"}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyNestedContainers)
|
||||
{
|
||||
object.createNestedObject("key1");
|
||||
object.createNestedArray("key2");
|
||||
TEST_F(JsonObject_PrettyPrintTo_Tests, NestedContainers) {
|
||||
JsonObject nested1 = object.createNestedObject("key1");
|
||||
nested1["a"] = 1;
|
||||
|
||||
outputMustBe(
|
||||
"{\r\n"
|
||||
" \"key1\": {},\r\n"
|
||||
" \"key2\": []\r\n"
|
||||
"}");
|
||||
}
|
||||
|
||||
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"
|
||||
" \"key1\": {\r\n"
|
||||
" \"a\": 1\r\n"
|
||||
" },\r\n"
|
||||
" \"key2\": [\r\n"
|
||||
" 2\r\n"
|
||||
" ]\r\n"
|
||||
"}");
|
||||
JsonArray nested2 = object.createNestedArray("key2");
|
||||
nested2.add(2);
|
||||
|
||||
outputMustBe("{\r\n"
|
||||
" \"key1\": {\r\n"
|
||||
" \"a\": 1\r\n"
|
||||
" },\r\n"
|
||||
" \"key2\": [\r\n"
|
||||
" 2\r\n"
|
||||
" ]\r\n"
|
||||
"}");
|
||||
}
|
@ -6,155 +6,130 @@
|
||||
|
||||
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)
|
||||
{
|
||||
char actual[256];
|
||||
int result = object.printTo(actual, sizeof(actual));
|
||||
void outputMustBe(const char *expected) {
|
||||
char actual[256];
|
||||
int result = object.printTo(actual, sizeof(actual));
|
||||
|
||||
EXPECT_STREQ(expected, actual);
|
||||
EXPECT_EQ(strlen(expected), result);
|
||||
}
|
||||
EXPECT_STREQ(expected, actual);
|
||||
EXPECT_EQ(strlen(expected), result);
|
||||
}
|
||||
|
||||
JsonObject object;
|
||||
StaticJsonBuffer<5> json;
|
||||
JsonObject object;
|
||||
StaticJsonBuffer<5> json;
|
||||
};
|
||||
|
||||
TEST_F(JsonObject_Serialization_Tests, EmptyObject)
|
||||
{
|
||||
outputMustBe("{}");
|
||||
TEST_F(JsonObject_Serialization_Tests, EmptyObject) { outputMustBe("{}"); }
|
||||
|
||||
TEST_F(JsonObject_Serialization_Tests, OneString) {
|
||||
object["key"] = "value";
|
||||
|
||||
outputMustBe("{\"key\":\"value\"}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Serialization_Tests, OneString)
|
||||
{
|
||||
object["key"] = "value";
|
||||
TEST_F(JsonObject_Serialization_Tests, TwoStrings) {
|
||||
object["key1"] = "value1";
|
||||
object["key2"] = "value2";
|
||||
|
||||
outputMustBe("{\"key\":\"value\"}");
|
||||
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Serialization_Tests, TwoStrings)
|
||||
{
|
||||
object["key1"] = "value1";
|
||||
object["key2"] = "value2";
|
||||
TEST_F(JsonObject_Serialization_Tests, RemoveFirst) {
|
||||
object["key1"] = "value1";
|
||||
object["key2"] = "value2";
|
||||
object.remove("key1");
|
||||
|
||||
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
||||
outputMustBe("{\"key2\":\"value2\"}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Serialization_Tests, RemoveFirst)
|
||||
{
|
||||
object["key1"] = "value1";
|
||||
object["key2"] = "value2";
|
||||
object.remove("key1");
|
||||
TEST_F(JsonObject_Serialization_Tests, RemoveLast) {
|
||||
object["key1"] = "value1";
|
||||
object["key2"] = "value2";
|
||||
object.remove("key2");
|
||||
|
||||
outputMustBe("{\"key2\":\"value2\"}");
|
||||
outputMustBe("{\"key1\":\"value1\"}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Serialization_Tests, RemoveLast)
|
||||
{
|
||||
object["key1"] = "value1";
|
||||
object["key2"] = "value2";
|
||||
object.remove("key2");
|
||||
TEST_F(JsonObject_Serialization_Tests, RemoveUnexistingKey) {
|
||||
object["key1"] = "value1";
|
||||
object["key2"] = "value2";
|
||||
object.remove("key3");
|
||||
|
||||
outputMustBe("{\"key1\":\"value1\"}");
|
||||
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Serialization_Tests, RemoveUnexistingKey)
|
||||
{
|
||||
object["key1"] = "value1";
|
||||
object["key2"] = "value2";
|
||||
object.remove("key3");
|
||||
TEST_F(JsonObject_Serialization_Tests, ReplaceExistingKey) {
|
||||
object["key"] = "value1";
|
||||
object["key"] = "value2";
|
||||
|
||||
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
||||
outputMustBe("{\"key\":\"value2\"}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Serialization_Tests, ReplaceExistingKey)
|
||||
{
|
||||
object["key"] = "value1";
|
||||
object["key"] = "value2";
|
||||
TEST_F(JsonObject_Serialization_Tests, OneStringOverCapacity) {
|
||||
object["key1"] = "value1";
|
||||
object["key2"] = "value2";
|
||||
object["key3"] = "value3";
|
||||
|
||||
outputMustBe("{\"key\":\"value2\"}");
|
||||
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Serialization_Tests, OneStringOverCapacity)
|
||||
{
|
||||
object["key1"] = "value1";
|
||||
object["key2"] = "value2";
|
||||
object["key3"] = "value3";
|
||||
|
||||
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
||||
TEST_F(JsonObject_Serialization_Tests, OneInteger) {
|
||||
object["key"] = 1;
|
||||
outputMustBe("{\"key\":1}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Serialization_Tests, OneInteger)
|
||||
{
|
||||
object["key"] = 1;
|
||||
outputMustBe("{\"key\":1}");
|
||||
TEST_F(JsonObject_Serialization_Tests, OneDoubleFourDigits) {
|
||||
object["key"].set(3.14159265358979323846, 4);
|
||||
outputMustBe("{\"key\":3.1416}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Serialization_Tests, OneDoubleFourDigits)
|
||||
{
|
||||
object["key"].set(3.14159265358979323846, 4);
|
||||
outputMustBe("{\"key\":3.1416}");
|
||||
TEST_F(JsonObject_Serialization_Tests, OneDoubleDefaultDigits) {
|
||||
object["key"] = 3.14159265358979323846;
|
||||
outputMustBe("{\"key\":3.14}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Serialization_Tests, OneDoubleDefaultDigits)
|
||||
{
|
||||
object["key"] = 3.14159265358979323846;
|
||||
outputMustBe("{\"key\":3.14}");
|
||||
TEST_F(JsonObject_Serialization_Tests, OneNull) {
|
||||
object["key"] = (char *)0;
|
||||
outputMustBe("{\"key\":null}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Serialization_Tests, OneNull)
|
||||
{
|
||||
object["key"] = (char*) 0;
|
||||
outputMustBe("{\"key\":null}");
|
||||
TEST_F(JsonObject_Serialization_Tests, OneTrue) {
|
||||
object["key"] = true;
|
||||
outputMustBe("{\"key\":true}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Serialization_Tests, OneTrue)
|
||||
{
|
||||
object["key"] = true;
|
||||
outputMustBe("{\"key\":true}");
|
||||
TEST_F(JsonObject_Serialization_Tests, OneFalse) {
|
||||
object["key"] = false;
|
||||
outputMustBe("{\"key\":false}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Serialization_Tests, OneFalse)
|
||||
{
|
||||
object["key"] = false;
|
||||
outputMustBe("{\"key\":false}");
|
||||
TEST_F(JsonObject_Serialization_Tests, OneEmptyNestedArrayViaProxy) {
|
||||
JsonArray nestedArray = json.createArray();
|
||||
|
||||
object["key"] = nestedArray;
|
||||
|
||||
outputMustBe("{\"key\":[]}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Serialization_Tests, OneEmptyNestedArrayViaProxy)
|
||||
{
|
||||
JsonArray nestedArray = json.createArray();
|
||||
TEST_F(JsonObject_Serialization_Tests, OneEmptyNestedObjectViaProxy) {
|
||||
JsonObject nestedArray = json.createObject();
|
||||
|
||||
object["key"] = nestedArray;
|
||||
object["key"] = nestedArray;
|
||||
|
||||
outputMustBe("{\"key\":[]}");
|
||||
outputMustBe("{\"key\":{}}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Serialization_Tests, OneEmptyNestedObjectViaProxy)
|
||||
{
|
||||
JsonObject nestedArray = json.createObject();
|
||||
TEST_F(JsonObject_Serialization_Tests, OneEmptyNestedObject) {
|
||||
object.createNestedObject("key");
|
||||
|
||||
object["key"] = nestedArray;
|
||||
|
||||
outputMustBe("{\"key\":{}}");
|
||||
outputMustBe("{\"key\":{}}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Serialization_Tests, OneEmptyNestedObject)
|
||||
{
|
||||
object.createNestedObject("key");
|
||||
TEST_F(JsonObject_Serialization_Tests, OneEmptyNestedArray) {
|
||||
object.createNestedArray("key");
|
||||
|
||||
outputMustBe("{\"key\":{}}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Serialization_Tests, OneEmptyNestedArray)
|
||||
{
|
||||
object.createNestedArray("key");
|
||||
|
||||
outputMustBe("{\"key\":[]}");
|
||||
outputMustBe("{\"key\":[]}");
|
||||
}
|
@ -4,164 +4,136 @@
|
||||
|
||||
using namespace ArduinoJson;
|
||||
|
||||
class JsonParser_Array_Tests : public testing::Test
|
||||
{
|
||||
class JsonParser_Array_Tests : public testing::Test {
|
||||
protected:
|
||||
void whenInputIs(const char *json)
|
||||
{
|
||||
strcpy(_jsonString, json);
|
||||
_array = _jsonBuffer.parseArray(_jsonString);
|
||||
}
|
||||
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()
|
||||
{
|
||||
EXPECT_FALSE(_array.success());
|
||||
EXPECT_EQ(0, _array.size());
|
||||
}
|
||||
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)
|
||||
{
|
||||
elementAtIndexMustBe(0, expected);
|
||||
}
|
||||
template <typename T> void firstElementMustBe(T expected) {
|
||||
elementAtIndexMustBe(0, expected);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void secondElementMustBe(T expected)
|
||||
{
|
||||
elementAtIndexMustBe(1, expected);
|
||||
}
|
||||
template <typename T> void secondElementMustBe(T expected) {
|
||||
elementAtIndexMustBe(1, expected);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void elementAtIndexMustBe(int index, T expected)
|
||||
{
|
||||
EXPECT_EQ(expected, _array[index].as<T>());
|
||||
}
|
||||
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;
|
||||
JsonArray _array;
|
||||
char _jsonString[256];
|
||||
StaticJsonBuffer<42> _jsonBuffer;
|
||||
JsonArray _array;
|
||||
char _jsonString[256];
|
||||
};
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, EmptyArray)
|
||||
{
|
||||
whenInputIs("[]");
|
||||
TEST_F(JsonParser_Array_Tests, EmptyArray) {
|
||||
whenInputIs("[]");
|
||||
|
||||
parseMustSucceed();
|
||||
sizeMustBe(0);
|
||||
parseMustSucceed();
|
||||
sizeMustBe(0);
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, ArrayWithNoEnd)
|
||||
{
|
||||
whenInputIs("[");
|
||||
TEST_F(JsonParser_Array_Tests, ArrayWithNoEnd) {
|
||||
whenInputIs("[");
|
||||
|
||||
parseMustFail();
|
||||
parseMustFail();
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, EmptyArrayWithLeadingSpaces)
|
||||
{
|
||||
whenInputIs(" []");
|
||||
TEST_F(JsonParser_Array_Tests, EmptyArrayWithLeadingSpaces) {
|
||||
whenInputIs(" []");
|
||||
|
||||
parseMustSucceed();
|
||||
sizeMustBe(0);
|
||||
parseMustSucceed();
|
||||
sizeMustBe(0);
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, Garbage)
|
||||
{
|
||||
whenInputIs("%*$£¤");
|
||||
TEST_F(JsonParser_Array_Tests, Garbage) {
|
||||
whenInputIs("%*$£¤");
|
||||
|
||||
parseMustFail();
|
||||
parseMustFail();
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, OneInteger)
|
||||
{
|
||||
whenInputIs("[42]");
|
||||
TEST_F(JsonParser_Array_Tests, OneInteger) {
|
||||
whenInputIs("[42]");
|
||||
|
||||
parseMustSucceed();
|
||||
sizeMustBe(1);
|
||||
firstElementMustBe(42);
|
||||
parseMustSucceed();
|
||||
sizeMustBe(1);
|
||||
firstElementMustBe(42);
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, OneIntegerWithSpacesBefore)
|
||||
{
|
||||
whenInputIs("[ \t\r\n42]");
|
||||
TEST_F(JsonParser_Array_Tests, OneIntegerWithSpacesBefore) {
|
||||
whenInputIs("[ \t\r\n42]");
|
||||
|
||||
parseMustSucceed();
|
||||
sizeMustBe(1);
|
||||
firstElementMustBe(42);
|
||||
parseMustSucceed();
|
||||
sizeMustBe(1);
|
||||
firstElementMustBe(42);
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, OneIntegerWithSpaceAfter)
|
||||
{
|
||||
whenInputIs("[42 \t\r\n]");
|
||||
TEST_F(JsonParser_Array_Tests, OneIntegerWithSpaceAfter) {
|
||||
whenInputIs("[42 \t\r\n]");
|
||||
|
||||
parseMustSucceed();
|
||||
sizeMustBe(1);
|
||||
firstElementMustBe(42);
|
||||
parseMustSucceed();
|
||||
sizeMustBe(1);
|
||||
firstElementMustBe(42);
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, TwoIntegers)
|
||||
{
|
||||
whenInputIs("[42,84]");
|
||||
TEST_F(JsonParser_Array_Tests, TwoIntegers) {
|
||||
whenInputIs("[42,84]");
|
||||
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
firstElementMustBe(42);
|
||||
secondElementMustBe(84);
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
firstElementMustBe(42);
|
||||
secondElementMustBe(84);
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, TwoDoubles)
|
||||
{
|
||||
whenInputIs("[4.2,8.4]");
|
||||
TEST_F(JsonParser_Array_Tests, TwoDoubles) {
|
||||
whenInputIs("[4.2,8.4]");
|
||||
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
firstElementMustBe(4.2);
|
||||
secondElementMustBe(8.4);
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
firstElementMustBe(4.2);
|
||||
secondElementMustBe(8.4);
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, TwoBooleans)
|
||||
{
|
||||
whenInputIs("[true,false]");
|
||||
TEST_F(JsonParser_Array_Tests, TwoBooleans) {
|
||||
whenInputIs("[true,false]");
|
||||
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
firstElementMustBe(true);
|
||||
secondElementMustBe(false);
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
firstElementMustBe(true);
|
||||
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]");
|
||||
whenInputIs("[null,null]");
|
||||
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
firstElementMustBe(nullCharPtr);
|
||||
secondElementMustBe(nullCharPtr);
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
firstElementMustBe(nullCharPtr);
|
||||
secondElementMustBe(nullCharPtr);
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, TwoStrings)
|
||||
{
|
||||
whenInputIs("[\"hello\",\"world\"]");
|
||||
TEST_F(JsonParser_Array_Tests, TwoStrings) {
|
||||
whenInputIs("[\"hello\",\"world\"]");
|
||||
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
firstElementMustBe("hello");
|
||||
secondElementMustBe("world");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
firstElementMustBe("hello");
|
||||
secondElementMustBe("world");
|
||||
}
|
@ -3,49 +3,48 @@
|
||||
|
||||
using namespace ArduinoJson;
|
||||
|
||||
TEST(JsonParser_Nested_Tests, ArrayNestedInObject)
|
||||
{
|
||||
StaticJsonBuffer<42> jsonBuffer;
|
||||
char jsonString[] = " { \"ab\" : [ 1 , 2 ] , \"cd\" : [ 3 , 4 ] } ";
|
||||
TEST(JsonParser_Nested_Tests, ArrayNestedInObject) {
|
||||
StaticJsonBuffer<42> jsonBuffer;
|
||||
char jsonString[] = " { \"ab\" : [ 1 , 2 ] , \"cd\" : [ 3 , 4 ] } ";
|
||||
|
||||
JsonObject object = jsonBuffer.parseObject(jsonString);
|
||||
JsonArray array1 = object["ab"];
|
||||
JsonArray array2 = object["cd"];
|
||||
JsonObject object = jsonBuffer.parseObject(jsonString);
|
||||
JsonArray array1 = object["ab"];
|
||||
JsonArray array2 = object["cd"];
|
||||
|
||||
ASSERT_TRUE(object.success());
|
||||
ASSERT_TRUE(object.success());
|
||||
|
||||
ASSERT_TRUE(array1.success());
|
||||
ASSERT_TRUE(array2.success());
|
||||
ASSERT_TRUE(array1.success());
|
||||
ASSERT_TRUE(array2.success());
|
||||
|
||||
ASSERT_EQ(2, array1.size());
|
||||
ASSERT_EQ(2, array2.size());
|
||||
ASSERT_EQ(2, array1.size());
|
||||
ASSERT_EQ(2, array2.size());
|
||||
|
||||
EXPECT_EQ(1, array1[0].as<int>());
|
||||
EXPECT_EQ(2, array1[1].as<int>());
|
||||
EXPECT_EQ(1, array1[0].as<int>());
|
||||
EXPECT_EQ(2, array1[1].as<int>());
|
||||
|
||||
EXPECT_EQ(3, array2[0].as<int>());
|
||||
EXPECT_EQ(4, array2[1].as<int>());
|
||||
EXPECT_EQ(3, array2[0].as<int>());
|
||||
EXPECT_EQ(4, array2[1].as<int>());
|
||||
}
|
||||
|
||||
TEST(JsonParser_Nested_Tests, ObjectNestedInArray)
|
||||
{
|
||||
StaticJsonBuffer<42> jsonBuffer;
|
||||
char jsonString[] = " [ { \"a\" : 1 , \"b\" : 2 } , { \"c\" : 3 , \"d\" : 4 } ] ";
|
||||
TEST(JsonParser_Nested_Tests, ObjectNestedInArray) {
|
||||
StaticJsonBuffer<42> jsonBuffer;
|
||||
char jsonString[] =
|
||||
" [ { \"a\" : 1 , \"b\" : 2 } , { \"c\" : 3 , \"d\" : 4 } ] ";
|
||||
|
||||
JsonArray array = jsonBuffer.parseArray(jsonString);
|
||||
JsonObject object1 = array[0];
|
||||
JsonObject object2 = array[1];
|
||||
JsonArray array = jsonBuffer.parseArray(jsonString);
|
||||
JsonObject object1 = array[0];
|
||||
JsonObject object2 = array[1];
|
||||
|
||||
ASSERT_TRUE(array.success());
|
||||
ASSERT_TRUE(array.success());
|
||||
|
||||
ASSERT_TRUE(object1.success());
|
||||
ASSERT_TRUE(object2.success());
|
||||
ASSERT_TRUE(object1.success());
|
||||
ASSERT_TRUE(object2.success());
|
||||
|
||||
ASSERT_EQ(2, object1.size());
|
||||
ASSERT_EQ(2, object2.size());
|
||||
ASSERT_EQ(2, object1.size());
|
||||
ASSERT_EQ(2, object2.size());
|
||||
|
||||
EXPECT_EQ(1, object1["a"].as<int>());
|
||||
EXPECT_EQ(2, object1["b"].as<int>());
|
||||
EXPECT_EQ(3, object2["c"].as<int>());
|
||||
EXPECT_EQ(4, object2["d"].as<int>());
|
||||
EXPECT_EQ(1, object1["a"].as<int>());
|
||||
EXPECT_EQ(2, object1["b"].as<int>());
|
||||
EXPECT_EQ(3, object2["c"].as<int>());
|
||||
EXPECT_EQ(4, object2["d"].as<int>());
|
||||
}
|
@ -4,198 +4,164 @@
|
||||
|
||||
using namespace ArduinoJson;
|
||||
|
||||
class JsonParser_Object_Test : public testing::Test
|
||||
{
|
||||
class JsonParser_Object_Test : public testing::Test {
|
||||
protected:
|
||||
void whenInputIs(const char *jsonString) {
|
||||
strcpy(_jsonString, jsonString);
|
||||
_object = _jsonBuffer.parseObject(_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 parseMustFail()
|
||||
{
|
||||
EXPECT_FALSE(_object.success());
|
||||
}
|
||||
void sizeMustBe(int expected) { EXPECT_EQ(expected, _object.size()); }
|
||||
|
||||
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 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)
|
||||
{
|
||||
EXPECT_EQ(expected, _object[key].as<T>());
|
||||
}
|
||||
template <typename T> void keyMustHaveValue(const char *key, T expected) {
|
||||
EXPECT_EQ(expected, _object[key].as<T>());
|
||||
}
|
||||
|
||||
private:
|
||||
StaticJsonBuffer<10> _jsonBuffer;
|
||||
JsonObject _object;
|
||||
char _jsonString[256];
|
||||
StaticJsonBuffer<10> _jsonBuffer;
|
||||
JsonObject _object;
|
||||
char _jsonString[256];
|
||||
};
|
||||
|
||||
TEST_F(JsonParser_Object_Test, EmptyObject)
|
||||
{
|
||||
whenInputIs("{}");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(0);
|
||||
TEST_F(JsonParser_Object_Test, EmptyObject) {
|
||||
whenInputIs("{}");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(0);
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Object_Test, MissingClosingBrace)
|
||||
{
|
||||
whenInputIs("{");
|
||||
parseMustFail();
|
||||
sizeMustBe(0);
|
||||
TEST_F(JsonParser_Object_Test, MissingClosingBrace) {
|
||||
whenInputIs("{");
|
||||
parseMustFail();
|
||||
sizeMustBe(0);
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Object_Test, MissingColonAndValue)
|
||||
{
|
||||
whenInputIs("{\"key\"}");
|
||||
parseMustFail();
|
||||
sizeMustBe(0);
|
||||
TEST_F(JsonParser_Object_Test, MissingColonAndValue) {
|
||||
whenInputIs("{\"key\"}");
|
||||
parseMustFail();
|
||||
sizeMustBe(0);
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Object_Test, MissingQuotesAndColonAndValue)
|
||||
{
|
||||
whenInputIs("{key}");
|
||||
parseMustFail();
|
||||
sizeMustBe(0);
|
||||
TEST_F(JsonParser_Object_Test, MissingQuotesAndColonAndValue) {
|
||||
whenInputIs("{key}");
|
||||
parseMustFail();
|
||||
sizeMustBe(0);
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Object_Test, OneString)
|
||||
{
|
||||
whenInputIs("{\"key\":\"value\"}");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(1);
|
||||
keyMustHaveValue("key", "value");
|
||||
TEST_F(JsonParser_Object_Test, OneString) {
|
||||
whenInputIs("{\"key\":\"value\"}");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(1);
|
||||
keyMustHaveValue("key", "value");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Object_Test, OneStringSingleQuotes)
|
||||
{
|
||||
whenInputIs("{'key':'value'}");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(1);
|
||||
keyMustHaveValue("key", "value");
|
||||
TEST_F(JsonParser_Object_Test, OneStringSingleQuotes) {
|
||||
whenInputIs("{'key':'value'}");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(1);
|
||||
keyMustHaveValue("key", "value");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Object_Test, OneStringSpaceBeforeKey)
|
||||
{
|
||||
whenInputIs("{ \"key\":\"value\"}");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(1);
|
||||
keyMustHaveValue("key", "value");
|
||||
TEST_F(JsonParser_Object_Test, OneStringSpaceBeforeKey) {
|
||||
whenInputIs("{ \"key\":\"value\"}");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(1);
|
||||
keyMustHaveValue("key", "value");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Object_Test, OneStringSpaceAfterKey)
|
||||
{
|
||||
whenInputIs("{\"key\" :\"value\"}");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(1);
|
||||
keyMustHaveValue("key", "value");
|
||||
TEST_F(JsonParser_Object_Test, OneStringSpaceAfterKey) {
|
||||
whenInputIs("{\"key\" :\"value\"}");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(1);
|
||||
keyMustHaveValue("key", "value");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Object_Test, OneStringSpaceBeforeValue)
|
||||
{
|
||||
whenInputIs("{\"key\": \"value\"}");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(1);
|
||||
keyMustHaveValue("key", "value");
|
||||
TEST_F(JsonParser_Object_Test, OneStringSpaceBeforeValue) {
|
||||
whenInputIs("{\"key\": \"value\"}");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(1);
|
||||
keyMustHaveValue("key", "value");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Object_Test, OneStringSpaceAfterValue)
|
||||
{
|
||||
whenInputIs("{\"key\":\"value\" }");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(1);
|
||||
keyMustHaveValue("key", "value");
|
||||
TEST_F(JsonParser_Object_Test, OneStringSpaceAfterValue) {
|
||||
whenInputIs("{\"key\":\"value\" }");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(1);
|
||||
keyMustHaveValue("key", "value");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Object_Test, TwoStrings)
|
||||
{
|
||||
whenInputIs("{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
keyMustHaveValue("key1", "value1");
|
||||
keyMustHaveValue("key2", "value2");
|
||||
TEST_F(JsonParser_Object_Test, TwoStrings) {
|
||||
whenInputIs("{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
keyMustHaveValue("key1", "value1");
|
||||
keyMustHaveValue("key2", "value2");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Object_Test, TwoStringsSpaceBeforeComma)
|
||||
{
|
||||
whenInputIs("{\"key1\":\"value1\" ,\"key2\":\"value2\"}");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
keyMustHaveValue("key1", "value1");
|
||||
keyMustHaveValue("key2", "value2");
|
||||
TEST_F(JsonParser_Object_Test, TwoStringsSpaceBeforeComma) {
|
||||
whenInputIs("{\"key1\":\"value1\" ,\"key2\":\"value2\"}");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
keyMustHaveValue("key1", "value1");
|
||||
keyMustHaveValue("key2", "value2");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Object_Test, TwoStringsSpaceAfterComma)
|
||||
{
|
||||
whenInputIs("{\"key1\":\"value1\" ,\"key2\":\"value2\"}");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
keyMustHaveValue("key1", "value1");
|
||||
keyMustHaveValue("key2", "value2");
|
||||
TEST_F(JsonParser_Object_Test, TwoStringsSpaceAfterComma) {
|
||||
whenInputIs("{\"key1\":\"value1\" ,\"key2\":\"value2\"}");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
keyMustHaveValue("key1", "value1");
|
||||
keyMustHaveValue("key2", "value2");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Object_Test, EndingWithAComma)
|
||||
{
|
||||
whenInputIs("{\"key1\":\"value1\",}");
|
||||
parseMustFail();
|
||||
sizeMustBe(0);
|
||||
TEST_F(JsonParser_Object_Test, EndingWithAComma) {
|
||||
whenInputIs("{\"key1\":\"value1\",}");
|
||||
parseMustFail();
|
||||
sizeMustBe(0);
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Object_Test, TwoIntergers)
|
||||
{
|
||||
whenInputIs("{\"key1\":42,\"key2\":-42}");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
keyMustHaveValue("key1", 42);
|
||||
keyMustHaveValue("key2", -42);
|
||||
TEST_F(JsonParser_Object_Test, TwoIntergers) {
|
||||
whenInputIs("{\"key1\":42,\"key2\":-42}");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
keyMustHaveValue("key1", 42);
|
||||
keyMustHaveValue("key2", -42);
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Object_Test, TwoDoubles)
|
||||
{
|
||||
whenInputIs("{\"key1\":12.345,\"key2\":-7.89}");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
keyMustHaveValue("key1", 12.345);
|
||||
keyMustHaveValue("key2", -7.89);
|
||||
TEST_F(JsonParser_Object_Test, TwoDoubles) {
|
||||
whenInputIs("{\"key1\":12.345,\"key2\":-7.89}");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
keyMustHaveValue("key1", 12.345);
|
||||
keyMustHaveValue("key2", -7.89);
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Object_Test, TwoBooleans)
|
||||
{
|
||||
whenInputIs("{\"key1\":true,\"key2\":false}");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
keyMustHaveValue("key1", true);
|
||||
keyMustHaveValue("key2", false);
|
||||
TEST_F(JsonParser_Object_Test, TwoBooleans) {
|
||||
whenInputIs("{\"key1\":true,\"key2\":false}");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
keyMustHaveValue("key1", true);
|
||||
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();
|
||||
sizeMustBe(2);
|
||||
keyMustHaveValue("key1", nullstr);
|
||||
keyMustHaveValue("key2", nullstr);
|
||||
whenInputIs("{\"key1\":null,\"key2\":null}");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
keyMustHaveValue("key1", nullstr);
|
||||
keyMustHaveValue("key2", nullstr);
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Object_Test, NullForKey)
|
||||
{
|
||||
whenInputIs("null:\"value\"}");
|
||||
parseMustFail();
|
||||
TEST_F(JsonParser_Object_Test, NullForKey) {
|
||||
whenInputIs("null:\"value\"}");
|
||||
parseMustFail();
|
||||
}
|
@ -4,118 +4,102 @@
|
||||
|
||||
using namespace ArduinoJson;
|
||||
|
||||
class JsonValueTests : public ::testing::Test
|
||||
{
|
||||
class JsonValueTests : public ::testing::Test {
|
||||
protected:
|
||||
virtual void SetUp()
|
||||
{
|
||||
jsonValue1 = json.createValue();
|
||||
jsonValue2 = json.createValue();
|
||||
}
|
||||
virtual void SetUp() {
|
||||
jsonValue1 = json.createValue();
|
||||
jsonValue2 = json.createValue();
|
||||
}
|
||||
|
||||
StaticJsonBuffer<42> json;
|
||||
JsonValue jsonValue1;
|
||||
JsonValue jsonValue2;
|
||||
StaticJsonBuffer<42> json;
|
||||
JsonValue jsonValue1;
|
||||
JsonValue jsonValue2;
|
||||
};
|
||||
|
||||
TEST_F(JsonValueTests, CanStoreInteger) {
|
||||
jsonValue1 = 123;
|
||||
|
||||
TEST_F(JsonValueTests, CanStoreInteger)
|
||||
{
|
||||
jsonValue1 = 123;
|
||||
|
||||
EXPECT_EQ(123, (int) jsonValue1);
|
||||
EXPECT_EQ(123, (int)jsonValue1);
|
||||
}
|
||||
|
||||
TEST_F(JsonValueTests, CanStoreDouble)
|
||||
{
|
||||
jsonValue1 = 123.45;
|
||||
TEST_F(JsonValueTests, CanStoreDouble) {
|
||||
jsonValue1 = 123.45;
|
||||
|
||||
EXPECT_EQ(123.45, (double) jsonValue1);
|
||||
EXPECT_EQ(123.45, (double)jsonValue1);
|
||||
}
|
||||
|
||||
TEST_F(JsonValueTests, CanStoreTrue)
|
||||
{
|
||||
jsonValue1 = true;
|
||||
EXPECT_TRUE((bool) jsonValue1);
|
||||
TEST_F(JsonValueTests, CanStoreTrue) {
|
||||
jsonValue1 = true;
|
||||
EXPECT_TRUE((bool)jsonValue1);
|
||||
}
|
||||
|
||||
TEST_F(JsonValueTests, CanStoreFalse)
|
||||
{
|
||||
jsonValue1 = false;
|
||||
EXPECT_FALSE((bool) jsonValue1);
|
||||
TEST_F(JsonValueTests, CanStoreFalse) {
|
||||
jsonValue1 = false;
|
||||
EXPECT_FALSE((bool)jsonValue1);
|
||||
}
|
||||
|
||||
TEST_F(JsonValueTests, CanStoreString)
|
||||
{
|
||||
jsonValue1 = "hello";
|
||||
TEST_F(JsonValueTests, CanStoreString) {
|
||||
jsonValue1 = "hello";
|
||||
|
||||
EXPECT_STREQ("hello", (const char*) jsonValue1);
|
||||
EXPECT_STREQ("hello", (const char *)jsonValue1);
|
||||
}
|
||||
|
||||
TEST_F(JsonValueTests, CanStoreObject)
|
||||
{
|
||||
JsonObject innerObject1 = json.createObject();
|
||||
TEST_F(JsonValueTests, CanStoreObject) {
|
||||
JsonObject innerObject1 = json.createObject();
|
||||
|
||||
jsonValue1 = innerObject1;
|
||||
jsonValue1 = innerObject1;
|
||||
|
||||
EXPECT_EQ(innerObject1, (JsonObject) jsonValue1);
|
||||
EXPECT_EQ(innerObject1, (JsonObject)jsonValue1);
|
||||
}
|
||||
|
||||
TEST_F(JsonValueTests, IntegersAreCopiedByValue)
|
||||
{
|
||||
jsonValue1 = 123;
|
||||
jsonValue2 = jsonValue1;
|
||||
jsonValue1 = 456;
|
||||
TEST_F(JsonValueTests, IntegersAreCopiedByValue) {
|
||||
jsonValue1 = 123;
|
||||
jsonValue2 = jsonValue1;
|
||||
jsonValue1 = 456;
|
||||
|
||||
EXPECT_EQ(123, (int) jsonValue2);
|
||||
EXPECT_EQ(123, (int)jsonValue2);
|
||||
}
|
||||
|
||||
TEST_F(JsonValueTests, DoublesAreCopiedByValue)
|
||||
{
|
||||
jsonValue1 = 123.45;
|
||||
jsonValue2 = jsonValue1;
|
||||
jsonValue1 = 456.78;
|
||||
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)
|
||||
{
|
||||
jsonValue1 = true;
|
||||
jsonValue2 = jsonValue1;
|
||||
jsonValue1 = false;
|
||||
TEST_F(JsonValueTests, BooleansAreCopiedByValue) {
|
||||
jsonValue1 = true;
|
||||
jsonValue2 = jsonValue1;
|
||||
jsonValue1 = false;
|
||||
|
||||
EXPECT_TRUE((bool) jsonValue2);
|
||||
EXPECT_TRUE((bool)jsonValue2);
|
||||
}
|
||||
|
||||
TEST_F(JsonValueTests, StringsAreCopiedByValue)
|
||||
{
|
||||
jsonValue1 = "hello";
|
||||
jsonValue2 = jsonValue1;
|
||||
jsonValue1 = "world";
|
||||
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) {
|
||||
JsonObject object = json.createObject();
|
||||
|
||||
TEST_F(JsonValueTests, ObjectsAreCopiedByReference)
|
||||
{
|
||||
JsonObject object = json.createObject();
|
||||
jsonValue1 = object;
|
||||
|
||||
jsonValue1 = object;
|
||||
object["hello"] = "world";
|
||||
|
||||
object["hello"] = "world";
|
||||
|
||||
EXPECT_EQ(1, ((JsonObject) jsonValue1).size());
|
||||
EXPECT_EQ(1, ((JsonObject)jsonValue1).size());
|
||||
}
|
||||
|
||||
TEST_F(JsonValueTests, ArraysAreCopiedByReference)
|
||||
{
|
||||
JsonArray array = json.createArray();
|
||||
TEST_F(JsonValueTests, ArraysAreCopiedByReference) {
|
||||
JsonArray array = json.createArray();
|
||||
|
||||
jsonValue1 = array;
|
||||
jsonValue1 = array;
|
||||
|
||||
array.add("world");
|
||||
array.add("world");
|
||||
|
||||
EXPECT_EQ(1, ((JsonObject) jsonValue1).size());
|
||||
EXPECT_EQ(1, ((JsonObject)jsonValue1).size());
|
||||
}
|
@ -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)
|
||||
{
|
||||
strcpy(_jsonString, json);
|
||||
_result = QuotedString::extractFrom(_jsonString, &_trailing);
|
||||
}
|
||||
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)
|
||||
{
|
||||
EXPECT_STREQ(expected, _trailing);
|
||||
}
|
||||
void trailingMustBe(const char *expected) {
|
||||
EXPECT_STREQ(expected, _trailing);
|
||||
}
|
||||
|
||||
private:
|
||||
char _jsonString[256];
|
||||
char* _result;
|
||||
char* _trailing;
|
||||
char _jsonString[256];
|
||||
char *_result;
|
||||
char *_trailing;
|
||||
};
|
||||
|
||||
TEST_F(QuotedString_ExtractFrom_Tests, EmptyDoubleQuotedString) {
|
||||
whenInputIs("\"\"");
|
||||
|
||||
TEST_F(QuotedString_ExtractFrom_Tests, EmptyDoubleQuotedString)
|
||||
{
|
||||
whenInputIs("\"\"");
|
||||
|
||||
resultMustBe("");
|
||||
trailingMustBe("");
|
||||
resultMustBe("");
|
||||
trailingMustBe("");
|
||||
}
|
||||
|
||||
TEST_F(QuotedString_ExtractFrom_Tests, NoQuotes)
|
||||
{
|
||||
whenInputIs("hello world");
|
||||
TEST_F(QuotedString_ExtractFrom_Tests, NoQuotes) {
|
||||
whenInputIs("hello world");
|
||||
|
||||
resultMustBe(0);
|
||||
resultMustBe(0);
|
||||
}
|
||||
|
||||
TEST_F(QuotedString_ExtractFrom_Tests, EmptySingleQuotedString)
|
||||
{
|
||||
whenInputIs("''");
|
||||
TEST_F(QuotedString_ExtractFrom_Tests, EmptySingleQuotedString) {
|
||||
whenInputIs("''");
|
||||
|
||||
resultMustBe("");
|
||||
trailingMustBe("");
|
||||
resultMustBe("");
|
||||
trailingMustBe("");
|
||||
}
|
||||
|
||||
TEST_F(QuotedString_ExtractFrom_Tests, SimpleDoubleQuotedString)
|
||||
{
|
||||
whenInputIs("\"hello world\"");
|
||||
TEST_F(QuotedString_ExtractFrom_Tests, SimpleDoubleQuotedString) {
|
||||
whenInputIs("\"hello world\"");
|
||||
|
||||
resultMustBe("hello world");
|
||||
trailingMustBe("");
|
||||
resultMustBe("hello world");
|
||||
trailingMustBe("");
|
||||
}
|
||||
|
||||
TEST_F(QuotedString_ExtractFrom_Tests, DoubleQuotedStringWithTrailing)
|
||||
{
|
||||
whenInputIs("\"hello\" world");
|
||||
TEST_F(QuotedString_ExtractFrom_Tests, DoubleQuotedStringWithTrailing) {
|
||||
whenInputIs("\"hello\" world");
|
||||
|
||||
resultMustBe("hello");
|
||||
trailingMustBe(" world");
|
||||
resultMustBe("hello");
|
||||
trailingMustBe(" world");
|
||||
}
|
||||
|
||||
TEST_F(QuotedString_ExtractFrom_Tests, SingleQuotedStringWithTrailing)
|
||||
{
|
||||
whenInputIs("'hello' world");
|
||||
TEST_F(QuotedString_ExtractFrom_Tests, SingleQuotedStringWithTrailing) {
|
||||
whenInputIs("'hello' world");
|
||||
|
||||
resultMustBe("hello");
|
||||
trailingMustBe(" world");
|
||||
resultMustBe("hello");
|
||||
trailingMustBe(" world");
|
||||
}
|
||||
|
||||
TEST_F(QuotedString_ExtractFrom_Tests, CurlyBraces)
|
||||
{
|
||||
whenInputIs("\"{hello:world}\"");
|
||||
resultMustBe("{hello:world}");
|
||||
TEST_F(QuotedString_ExtractFrom_Tests, CurlyBraces) {
|
||||
whenInputIs("\"{hello:world}\"");
|
||||
resultMustBe("{hello:world}");
|
||||
}
|
||||
|
||||
TEST_F(QuotedString_ExtractFrom_Tests, SquareBraquets)
|
||||
{
|
||||
whenInputIs("\"[hello,world]\"");
|
||||
resultMustBe("[hello,world]");
|
||||
TEST_F(QuotedString_ExtractFrom_Tests, SquareBraquets) {
|
||||
whenInputIs("\"[hello,world]\"");
|
||||
resultMustBe("[hello,world]");
|
||||
}
|
||||
|
||||
TEST_F(QuotedString_ExtractFrom_Tests, EscapedDoubleQuote)
|
||||
{
|
||||
whenInputIs("\"hello \\\"world\\\"\"");
|
||||
resultMustBe("hello \"world\"");
|
||||
TEST_F(QuotedString_ExtractFrom_Tests, EscapedDoubleQuote) {
|
||||
whenInputIs("\"hello \\\"world\\\"\"");
|
||||
resultMustBe("hello \"world\"");
|
||||
}
|
||||
|
||||
TEST_F(QuotedString_ExtractFrom_Tests, EscapedSingleQuote)
|
||||
{
|
||||
whenInputIs("\"hello \\\'world\\\'\"");
|
||||
resultMustBe("hello 'world'");
|
||||
TEST_F(QuotedString_ExtractFrom_Tests, EscapedSingleQuote) {
|
||||
whenInputIs("\"hello \\\'world\\\'\"");
|
||||
resultMustBe("hello 'world'");
|
||||
}
|
||||
|
||||
TEST_F(QuotedString_ExtractFrom_Tests, EscapedSolidus)
|
||||
{
|
||||
whenInputIs("\"hello \\/world\\/\"");
|
||||
resultMustBe("hello /world/");
|
||||
TEST_F(QuotedString_ExtractFrom_Tests, EscapedSolidus) {
|
||||
whenInputIs("\"hello \\/world\\/\"");
|
||||
resultMustBe("hello /world/");
|
||||
}
|
||||
|
||||
TEST_F(QuotedString_ExtractFrom_Tests, EscapedReverseSolidus)
|
||||
{
|
||||
whenInputIs("\"hello \\\\world\\\\\"");
|
||||
resultMustBe("hello \\world\\");
|
||||
TEST_F(QuotedString_ExtractFrom_Tests, EscapedReverseSolidus) {
|
||||
whenInputIs("\"hello \\\\world\\\\\"");
|
||||
resultMustBe("hello \\world\\");
|
||||
}
|
||||
|
||||
TEST_F(QuotedString_ExtractFrom_Tests, EscapedBackspace)
|
||||
{
|
||||
whenInputIs("\"hello \\bworld\\b\"");
|
||||
resultMustBe("hello \bworld\b");
|
||||
TEST_F(QuotedString_ExtractFrom_Tests, EscapedBackspace) {
|
||||
whenInputIs("\"hello \\bworld\\b\"");
|
||||
resultMustBe("hello \bworld\b");
|
||||
}
|
||||
|
||||
TEST_F(QuotedString_ExtractFrom_Tests, EscapedFormfeed)
|
||||
{
|
||||
whenInputIs("\"hello \\fworld\\f\"");
|
||||
resultMustBe("hello \fworld\f");
|
||||
TEST_F(QuotedString_ExtractFrom_Tests, EscapedFormfeed) {
|
||||
whenInputIs("\"hello \\fworld\\f\"");
|
||||
resultMustBe("hello \fworld\f");
|
||||
}
|
||||
|
||||
TEST_F(QuotedString_ExtractFrom_Tests, EscapedNewline)
|
||||
{
|
||||
whenInputIs("\"hello \\nworld\\n\"");
|
||||
resultMustBe("hello \nworld\n");
|
||||
TEST_F(QuotedString_ExtractFrom_Tests, EscapedNewline) {
|
||||
whenInputIs("\"hello \\nworld\\n\"");
|
||||
resultMustBe("hello \nworld\n");
|
||||
}
|
||||
|
||||
TEST_F(QuotedString_ExtractFrom_Tests, EscapedCarriageReturn)
|
||||
{
|
||||
whenInputIs("\"hello \\rworld\\r\"");
|
||||
resultMustBe("hello \rworld\r");
|
||||
TEST_F(QuotedString_ExtractFrom_Tests, EscapedCarriageReturn) {
|
||||
whenInputIs("\"hello \\rworld\\r\"");
|
||||
resultMustBe("hello \rworld\r");
|
||||
}
|
||||
|
||||
TEST_F(QuotedString_ExtractFrom_Tests, EscapedTab)
|
||||
{
|
||||
whenInputIs("\"hello \\tworld\\t\"");
|
||||
resultMustBe("hello \tworld\t");
|
||||
TEST_F(QuotedString_ExtractFrom_Tests, EscapedTab) {
|
||||
whenInputIs("\"hello \\tworld\\t\"");
|
||||
resultMustBe("hello \tworld\t");
|
||||
}
|
||||
|
||||
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");
|
||||
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");
|
||||
}
|
@ -5,82 +5,69 @@
|
||||
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
||||
class QuotedString_PrintTo_Tests : public testing::Test
|
||||
{
|
||||
class QuotedString_PrintTo_Tests : public testing::Test {
|
||||
protected:
|
||||
void whenInputIs(const char* input)
|
||||
{
|
||||
StringBuilder sb(buffer, sizeof(buffer));
|
||||
returnValue = QuotedString::printTo(input, &sb);
|
||||
}
|
||||
void whenInputIs(const char *input) {
|
||||
StringBuilder sb(buffer, sizeof(buffer));
|
||||
returnValue = QuotedString::printTo(input, &sb);
|
||||
}
|
||||
|
||||
void outputMustBe(const char* expected)
|
||||
{
|
||||
EXPECT_STREQ(expected, buffer);
|
||||
EXPECT_EQ(strlen(expected), returnValue);
|
||||
}
|
||||
void outputMustBe(const char *expected) {
|
||||
EXPECT_STREQ(expected, buffer);
|
||||
EXPECT_EQ(strlen(expected), returnValue);
|
||||
}
|
||||
|
||||
private:
|
||||
char buffer[1024];
|
||||
size_t returnValue;
|
||||
char buffer[1024];
|
||||
size_t returnValue;
|
||||
};
|
||||
|
||||
TEST_F(QuotedString_PrintTo_Tests, Null)
|
||||
{
|
||||
whenInputIs(0);
|
||||
outputMustBe("null");
|
||||
TEST_F(QuotedString_PrintTo_Tests, Null) {
|
||||
whenInputIs(0);
|
||||
outputMustBe("null");
|
||||
}
|
||||
|
||||
TEST_F(QuotedString_PrintTo_Tests, EmptyString)
|
||||
{
|
||||
whenInputIs("");
|
||||
outputMustBe("\"\"");
|
||||
TEST_F(QuotedString_PrintTo_Tests, EmptyString) {
|
||||
whenInputIs("");
|
||||
outputMustBe("\"\"");
|
||||
}
|
||||
|
||||
TEST_F(QuotedString_PrintTo_Tests, QuotationMark)
|
||||
{
|
||||
whenInputIs("\"");
|
||||
outputMustBe("\"\\\"\"");
|
||||
TEST_F(QuotedString_PrintTo_Tests, QuotationMark) {
|
||||
whenInputIs("\"");
|
||||
outputMustBe("\"\\\"\"");
|
||||
}
|
||||
|
||||
TEST_F(QuotedString_PrintTo_Tests, ReverseSolidus)
|
||||
{
|
||||
whenInputIs("\\");
|
||||
outputMustBe("\"\\\\\"");
|
||||
TEST_F(QuotedString_PrintTo_Tests, ReverseSolidus) {
|
||||
whenInputIs("\\");
|
||||
outputMustBe("\"\\\\\"");
|
||||
}
|
||||
|
||||
TEST_F(QuotedString_PrintTo_Tests, Solidus)
|
||||
{
|
||||
whenInputIs("/");
|
||||
outputMustBe("\"/\""); // but the JSON format allows \/
|
||||
TEST_F(QuotedString_PrintTo_Tests, Solidus) {
|
||||
whenInputIs("/");
|
||||
outputMustBe("\"/\""); // but the JSON format allows \/
|
||||
}
|
||||
|
||||
TEST_F(QuotedString_PrintTo_Tests, Backspace)
|
||||
{
|
||||
whenInputIs("\b");
|
||||
outputMustBe("\"\\b\"");
|
||||
TEST_F(QuotedString_PrintTo_Tests, Backspace) {
|
||||
whenInputIs("\b");
|
||||
outputMustBe("\"\\b\"");
|
||||
}
|
||||
|
||||
TEST_F(QuotedString_PrintTo_Tests, Formfeed)
|
||||
{
|
||||
whenInputIs("\f");
|
||||
outputMustBe("\"\\f\"");
|
||||
TEST_F(QuotedString_PrintTo_Tests, Formfeed) {
|
||||
whenInputIs("\f");
|
||||
outputMustBe("\"\\f\"");
|
||||
}
|
||||
|
||||
TEST_F(QuotedString_PrintTo_Tests, Newline)
|
||||
{
|
||||
whenInputIs("\n");
|
||||
outputMustBe("\"\\n\"");
|
||||
TEST_F(QuotedString_PrintTo_Tests, Newline) {
|
||||
whenInputIs("\n");
|
||||
outputMustBe("\"\\n\"");
|
||||
}
|
||||
|
||||
TEST_F(QuotedString_PrintTo_Tests, CarriageReturn)
|
||||
{
|
||||
whenInputIs("\r");
|
||||
outputMustBe("\"\\r\"");
|
||||
TEST_F(QuotedString_PrintTo_Tests, CarriageReturn) {
|
||||
whenInputIs("\r");
|
||||
outputMustBe("\"\\r\"");
|
||||
}
|
||||
|
||||
TEST_F(QuotedString_PrintTo_Tests, HorizontalTab)
|
||||
{
|
||||
whenInputIs("\t");
|
||||
outputMustBe("\"\\t\"");
|
||||
TEST_F(QuotedString_PrintTo_Tests, HorizontalTab) {
|
||||
whenInputIs("\t");
|
||||
outputMustBe("\"\\t\"");
|
||||
}
|
@ -4,68 +4,66 @@
|
||||
|
||||
using namespace ArduinoJson;
|
||||
|
||||
TEST(StaticJsonBuffer, CapacityMatchTemplateParameter)
|
||||
{
|
||||
StaticJsonBuffer<42> json;
|
||||
EXPECT_EQ(42, json.capacity());
|
||||
TEST(StaticJsonBuffer, CapacityMatchTemplateParameter) {
|
||||
StaticJsonBuffer<42> json;
|
||||
EXPECT_EQ(42, json.capacity());
|
||||
}
|
||||
|
||||
TEST(StaticJsonBuffer, InitialSizeIsZero)
|
||||
{
|
||||
StaticJsonBuffer<42> json;
|
||||
EXPECT_EQ(0, json.size());
|
||||
TEST(StaticJsonBuffer, InitialSizeIsZero) {
|
||||
StaticJsonBuffer<42> json;
|
||||
EXPECT_EQ(0, json.size());
|
||||
}
|
||||
|
||||
TEST(StaticJsonBuffer, WhenCreateObjectIsCalled_ThenSizeIsIncreasedByOne)
|
||||
{
|
||||
StaticJsonBuffer<42> json;
|
||||
TEST(StaticJsonBuffer, WhenCreateObjectIsCalled_ThenSizeIsIncreasedByOne) {
|
||||
StaticJsonBuffer<42> json;
|
||||
|
||||
json.createObject();
|
||||
EXPECT_EQ(1, json.size());
|
||||
json.createObject();
|
||||
EXPECT_EQ(1, json.size());
|
||||
|
||||
json.createObject();
|
||||
EXPECT_EQ(2, json.size());
|
||||
json.createObject();
|
||||
EXPECT_EQ(2, json.size());
|
||||
}
|
||||
|
||||
TEST(StaticJsonBuffer, GivenBufferIsFull_WhenCreateObjectIsCalled_ThenSizeDoesNotChange)
|
||||
{
|
||||
StaticJsonBuffer<1> json;
|
||||
TEST(StaticJsonBuffer,
|
||||
GivenBufferIsFull_WhenCreateObjectIsCalled_ThenSizeDoesNotChange) {
|
||||
StaticJsonBuffer<1> json;
|
||||
|
||||
json.createObject();
|
||||
EXPECT_EQ(1, json.size());
|
||||
json.createObject();
|
||||
EXPECT_EQ(1, json.size());
|
||||
|
||||
json.createObject();
|
||||
EXPECT_EQ(1, json.size());
|
||||
json.createObject();
|
||||
EXPECT_EQ(1, json.size());
|
||||
}
|
||||
|
||||
TEST(StaticJsonBuffer, WhenCreateObjectIsCalled_ThenAnEmptyJsonObjectIsReturned)
|
||||
{
|
||||
StaticJsonBuffer<42> json;
|
||||
TEST(StaticJsonBuffer,
|
||||
WhenCreateObjectIsCalled_ThenAnEmptyJsonObjectIsReturned) {
|
||||
StaticJsonBuffer<42> json;
|
||||
|
||||
JsonObject obj = json.createObject();
|
||||
EXPECT_EQ(0, obj.size());
|
||||
JsonObject obj = json.createObject();
|
||||
EXPECT_EQ(0, obj.size());
|
||||
}
|
||||
|
||||
TEST(StaticJsonBuffer, GivenAJsonObject_WhenValuesAreAdded_ThenSizeIsIncreasedByTwo)
|
||||
{
|
||||
StaticJsonBuffer<42> json;
|
||||
JsonObject obj = json.createObject();
|
||||
TEST(StaticJsonBuffer,
|
||||
GivenAJsonObject_WhenValuesAreAdded_ThenSizeIsIncreasedByTwo) {
|
||||
StaticJsonBuffer<42> json;
|
||||
JsonObject obj = json.createObject();
|
||||
|
||||
obj["hello"];
|
||||
EXPECT_EQ(3, json.size());
|
||||
obj["hello"];
|
||||
EXPECT_EQ(3, json.size());
|
||||
|
||||
obj["world"];
|
||||
EXPECT_EQ(5, json.size());
|
||||
obj["world"];
|
||||
EXPECT_EQ(5, json.size());
|
||||
}
|
||||
|
||||
TEST(StaticJsonBuffer, GivenAJsonObject_WhenSameValuesAreAddedTwice_ThenSizeIsOnlyIncreasedByTwo)
|
||||
{
|
||||
StaticJsonBuffer<42> json;
|
||||
JsonObject obj = json.createObject();
|
||||
TEST(
|
||||
StaticJsonBuffer,
|
||||
GivenAJsonObject_WhenSameValuesAreAddedTwice_ThenSizeIsOnlyIncreasedByTwo) {
|
||||
StaticJsonBuffer<42> json;
|
||||
JsonObject obj = json.createObject();
|
||||
|
||||
obj["hello"];
|
||||
EXPECT_EQ(3, json.size());
|
||||
obj["hello"];
|
||||
EXPECT_EQ(3, json.size());
|
||||
|
||||
obj["hello"];
|
||||
EXPECT_EQ(3, json.size());
|
||||
obj["hello"];
|
||||
EXPECT_EQ(3, json.size());
|
||||
}
|
@ -3,73 +3,52 @@
|
||||
|
||||
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;
|
||||
size_t returnValue;
|
||||
char buffer[20];
|
||||
Print *sb;
|
||||
size_t returnValue;
|
||||
};
|
||||
|
||||
TEST_F(StringBuilderTests, InitialState)
|
||||
{
|
||||
outputMustBe("");
|
||||
TEST_F(StringBuilderTests, InitialState) { outputMustBe(""); }
|
||||
|
||||
TEST_F(StringBuilderTests, OverCapacity) {
|
||||
print("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
|
||||
resultMustBe(19);
|
||||
|
||||
print("ABC");
|
||||
resultMustBe(0);
|
||||
|
||||
outputMustBe("ABCDEFGHIJKLMNOPQRS");
|
||||
}
|
||||
|
||||
TEST_F(StringBuilderTests, OverCapacity)
|
||||
{
|
||||
print("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
|
||||
resultMustBe(19);
|
||||
|
||||
print("ABC");
|
||||
resultMustBe(0);
|
||||
|
||||
outputMustBe("ABCDEFGHIJKLMNOPQRS");
|
||||
TEST_F(StringBuilderTests, EmptyString) {
|
||||
print("");
|
||||
resultMustBe(0);
|
||||
outputMustBe("");
|
||||
}
|
||||
|
||||
TEST_F(StringBuilderTests, EmptyString)
|
||||
{
|
||||
print("");
|
||||
resultMustBe(0);
|
||||
outputMustBe("");
|
||||
TEST_F(StringBuilderTests, OneString) {
|
||||
print("ABCD");
|
||||
resultMustBe(4);
|
||||
outputMustBe("ABCD");
|
||||
}
|
||||
|
||||
TEST_F(StringBuilderTests, OneString)
|
||||
{
|
||||
print("ABCD");
|
||||
resultMustBe(4);
|
||||
outputMustBe("ABCD");
|
||||
}
|
||||
|
||||
TEST_F(StringBuilderTests, TwoStrings)
|
||||
{
|
||||
print("ABCD");
|
||||
resultMustBe(4);
|
||||
|
||||
print("EFGH");
|
||||
resultMustBe(4);
|
||||
|
||||
outputMustBe("ABCDEFGH");
|
||||
TEST_F(StringBuilderTests, TwoStrings) {
|
||||
print("ABCD");
|
||||
resultMustBe(4);
|
||||
|
||||
print("EFGH");
|
||||
resultMustBe(4);
|
||||
|
||||
outputMustBe("ABCDEFGH");
|
||||
}
|
Reference in New Issue
Block a user