mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-14 19:16:35 +02:00
Switched to Google coding style to match cpplint expectations
This commit is contained in:
@ -1 +0,0 @@
|
|||||||
BasedOnStyle: LLVM
|
|
@ -104,16 +104,14 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
JsonBuffer *getContainerBuffer() {
|
JsonBuffer *getContainerBuffer() {
|
||||||
if (type == JSON_PROXY)
|
if (type == JSON_PROXY) return content.asProxy.target->getContainerBuffer();
|
||||||
return content.asProxy.target->getContainerBuffer();
|
|
||||||
return type == JSON_ARRAY || type == JSON_OBJECT
|
return type == JSON_ARRAY || type == JSON_OBJECT
|
||||||
? content.asContainer.buffer
|
? content.asContainer.buffer
|
||||||
: 0;
|
: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonNode *getContainerChild() {
|
JsonNode *getContainerChild() {
|
||||||
if (type == JSON_PROXY)
|
if (type == JSON_PROXY) return content.asProxy.target->getContainerChild();
|
||||||
return content.asProxy.target->getContainerChild();
|
|
||||||
return type == JSON_ARRAY || type == JSON_OBJECT ? content.asContainer.child
|
return type == JSON_ARRAY || type == JSON_OBJECT ? content.asContainer.child
|
||||||
: 0;
|
: 0;
|
||||||
}
|
}
|
||||||
@ -144,9 +142,10 @@ private:
|
|||||||
JsonNodeType type;
|
JsonNodeType type;
|
||||||
JsonNodeContent content;
|
JsonNodeContent content;
|
||||||
|
|
||||||
inline void writeArrayTo(JsonWriter &); // TODO: <- move in JsonNodeSerializer
|
inline void writeArrayTo(
|
||||||
inline void
|
JsonWriter &); // TODO: <- move in JsonNodeSerializer
|
||||||
writeObjectTo(JsonWriter &); // TODO: <- move in JsonNodeSerializer
|
inline void writeObjectTo(
|
||||||
|
JsonWriter &); // TODO: <- move in JsonNodeSerializer
|
||||||
|
|
||||||
void setAsProxyOfSelf();
|
void setAsProxyOfSelf();
|
||||||
|
|
||||||
|
@ -32,6 +32,9 @@ public:
|
|||||||
|
|
||||||
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);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
@ -4,7 +4,8 @@
|
|||||||
#include "JsonObject.hpp"
|
#include "JsonObject.hpp"
|
||||||
|
|
||||||
namespace ArduinoJson {
|
namespace ArduinoJson {
|
||||||
template <int CAPACITY> class StaticJsonBuffer : public JsonBuffer {
|
template <int CAPACITY>
|
||||||
|
class StaticJsonBuffer : public JsonBuffer {
|
||||||
friend class JsonObject;
|
friend class JsonObject;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@ -18,8 +19,7 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void *allocateNode() {
|
virtual void *allocateNode() {
|
||||||
if (_size >= CAPACITY)
|
if (_size >= CAPACITY) return 0;
|
||||||
return 0;
|
|
||||||
|
|
||||||
return &_buffer[_size++];
|
return &_buffer[_size++];
|
||||||
}
|
}
|
||||||
|
@ -1 +1,2 @@
|
|||||||
find .. -regex ".*\.[ch]pp$" -exec clang-format -i {} \;
|
cd ..
|
||||||
|
find include src test -regex ".*\.[ch]pp$" -exec clang-format -style=Google -i {} \;
|
||||||
|
@ -3,25 +3,21 @@
|
|||||||
using namespace ArduinoJson::Internals;
|
using namespace ArduinoJson::Internals;
|
||||||
|
|
||||||
void IndentedPrint::indent() {
|
void IndentedPrint::indent() {
|
||||||
if (level < MAX_LEVEL)
|
if (level < MAX_LEVEL) level++;
|
||||||
level++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void IndentedPrint::unindent() {
|
void IndentedPrint::unindent() {
|
||||||
if (level > 0)
|
if (level > 0) level--;
|
||||||
level--;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void IndentedPrint::setTabSize(uint8_t n) {
|
void IndentedPrint::setTabSize(uint8_t n) {
|
||||||
if (n < MAX_TAB_SIZE)
|
if (n < MAX_TAB_SIZE) tabSize = n;
|
||||||
tabSize = n;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t IndentedPrint::write(uint8_t c) {
|
size_t IndentedPrint::write(uint8_t c) {
|
||||||
size_t n = 0;
|
size_t n = 0;
|
||||||
|
|
||||||
if (isNewLine)
|
if (isNewLine) n += writeTabs();
|
||||||
n += writeTabs();
|
|
||||||
|
|
||||||
n += sink->write(c);
|
n += sink->write(c);
|
||||||
|
|
||||||
@ -33,8 +29,7 @@ size_t IndentedPrint::write(uint8_t c) {
|
|||||||
inline size_t IndentedPrint::writeTabs() {
|
inline size_t IndentedPrint::writeTabs() {
|
||||||
size_t n = 0;
|
size_t n = 0;
|
||||||
|
|
||||||
for (int i = 0; i < level * tabSize; i++)
|
for (int i = 0; i < level * tabSize; i++) n += sink->write(' ');
|
||||||
n += sink->write(' ');
|
|
||||||
|
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
@ -40,11 +40,9 @@ void JsonNode::writeTo(JsonWriter &writer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void JsonNode::addChild(JsonNode *childToAdd) {
|
void JsonNode::addChild(JsonNode *childToAdd) {
|
||||||
if (type == JSON_PROXY)
|
if (type == JSON_PROXY) return content.asProxy.target->addChild(childToAdd);
|
||||||
return content.asProxy.target->addChild(childToAdd);
|
|
||||||
|
|
||||||
if (type != JSON_ARRAY && type != JSON_OBJECT)
|
if (type != JSON_ARRAY && type != JSON_OBJECT) return;
|
||||||
return;
|
|
||||||
|
|
||||||
JsonNode *lastChild = content.asContainer.child;
|
JsonNode *lastChild = content.asContainer.child;
|
||||||
|
|
||||||
@ -53,8 +51,7 @@ void JsonNode::addChild(JsonNode *childToAdd) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (lastChild->next)
|
while (lastChild->next) lastChild = lastChild->next;
|
||||||
lastChild = lastChild->next;
|
|
||||||
|
|
||||||
lastChild->next = childToAdd;
|
lastChild->next = childToAdd;
|
||||||
}
|
}
|
||||||
@ -63,8 +60,7 @@ void JsonNode::removeChild(JsonNode *childToRemove) {
|
|||||||
if (type == JSON_PROXY)
|
if (type == JSON_PROXY)
|
||||||
return content.asProxy.target->removeChild(childToRemove);
|
return content.asProxy.target->removeChild(childToRemove);
|
||||||
|
|
||||||
if (type != JSON_ARRAY && type != JSON_OBJECT)
|
if (type != JSON_ARRAY && type != JSON_OBJECT) return;
|
||||||
return;
|
|
||||||
|
|
||||||
if (content.asContainer.child == childToRemove) {
|
if (content.asContainer.child == childToRemove) {
|
||||||
content.asContainer.child = childToRemove->next;
|
content.asContainer.child = childToRemove->next;
|
||||||
@ -73,8 +69,7 @@ void JsonNode::removeChild(JsonNode *childToRemove) {
|
|||||||
|
|
||||||
for (JsonNode *child = content.asContainer.child; child;
|
for (JsonNode *child = content.asContainer.child; child;
|
||||||
child = child->next) {
|
child = child->next) {
|
||||||
if (child->next == childToRemove)
|
if (child->next == childToRemove) child->next = childToRemove->next;
|
||||||
child->next = childToRemove->next;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,8 +83,7 @@ void JsonNode::writeArrayTo(JsonWriter &writer) {
|
|||||||
child->writeTo(writer);
|
child->writeTo(writer);
|
||||||
|
|
||||||
child = child->next;
|
child = child->next;
|
||||||
if (!child)
|
if (!child) break;
|
||||||
break;
|
|
||||||
|
|
||||||
writer.writeComma();
|
writer.writeComma();
|
||||||
}
|
}
|
||||||
@ -112,8 +106,7 @@ void JsonNode::writeObjectTo(JsonWriter &writer) {
|
|||||||
child->content.asKeyValue.value->writeTo(writer);
|
child->content.asKeyValue.value->writeTo(writer);
|
||||||
|
|
||||||
child = child->next;
|
child = child->next;
|
||||||
if (!child)
|
if (!child) break;
|
||||||
break;
|
|
||||||
|
|
||||||
writer.writeComma();
|
writer.writeComma();
|
||||||
}
|
}
|
||||||
@ -126,12 +119,10 @@ void JsonNode::writeObjectTo(JsonWriter &writer) {
|
|||||||
|
|
||||||
void JsonNode::setAsProxyOfSelf() {
|
void JsonNode::setAsProxyOfSelf() {
|
||||||
JsonBuffer *buffer = content.asContainer.buffer;
|
JsonBuffer *buffer = content.asContainer.buffer;
|
||||||
if (!buffer)
|
if (!buffer) return;
|
||||||
return;
|
|
||||||
|
|
||||||
JsonNode *newNode = buffer->createNode();
|
JsonNode *newNode = buffer->createNode();
|
||||||
if (!newNode)
|
if (!newNode) return;
|
||||||
return;
|
|
||||||
|
|
||||||
*newNode = *this;
|
*newNode = *this;
|
||||||
|
|
||||||
|
@ -9,14 +9,12 @@
|
|||||||
using namespace ArduinoJson::Internals;
|
using namespace ArduinoJson::Internals;
|
||||||
|
|
||||||
void JsonParser::skipSpaces() {
|
void JsonParser::skipSpaces() {
|
||||||
while (isspace(*_ptr))
|
while (isspace(*_ptr)) _ptr++;
|
||||||
_ptr++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool JsonParser::skip(char charToSkip) {
|
bool JsonParser::skip(char charToSkip) {
|
||||||
skipSpaces();
|
skipSpaces();
|
||||||
if (*_ptr != charToSkip)
|
if (*_ptr != charToSkip) return false;
|
||||||
return false;
|
|
||||||
_ptr++;
|
_ptr++;
|
||||||
skipSpaces();
|
skipSpaces();
|
||||||
return true;
|
return true;
|
||||||
@ -67,25 +65,20 @@ JsonNode *JsonParser::parseArray() {
|
|||||||
|
|
||||||
skip('[');
|
skip('[');
|
||||||
|
|
||||||
if (isEnd())
|
if (isEnd()) return 0;
|
||||||
return 0;
|
|
||||||
|
|
||||||
if (skip(']'))
|
if (skip(']')) return node; // empty array
|
||||||
return node; // empty array
|
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
JsonNode *child = parseAnything();
|
JsonNode *child = parseAnything();
|
||||||
|
|
||||||
if (!child)
|
if (!child) return 0; // child parsing failed
|
||||||
return 0; // child parsing failed
|
|
||||||
|
|
||||||
node->addChild(child);
|
node->addChild(child);
|
||||||
|
|
||||||
if (skip(']'))
|
if (skip(']')) return node; // end of the array
|
||||||
return node; // end of the array
|
|
||||||
|
|
||||||
if (!skip(','))
|
if (!skip(',')) return 0; // comma is missing
|
||||||
return 0; // comma is missing
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,41 +118,33 @@ JsonNode *JsonParser::parseObject() {
|
|||||||
|
|
||||||
skip('{');
|
skip('{');
|
||||||
|
|
||||||
if (isEnd())
|
if (isEnd()) return 0; // premature ending
|
||||||
return 0; // premature ending
|
|
||||||
|
|
||||||
if (skip('}'))
|
if (skip('}')) return node; // empty object
|
||||||
return node; // empty object
|
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
JsonNode *child = parseObjectKeyValue();
|
JsonNode *child = parseObjectKeyValue();
|
||||||
|
|
||||||
if (!child)
|
if (!child) return 0; // child parsing failed
|
||||||
return 0; // child parsing failed
|
|
||||||
|
|
||||||
node->addChild(child);
|
node->addChild(child);
|
||||||
|
|
||||||
if (skip('}'))
|
if (skip('}')) return node; // end of the object
|
||||||
return node; // end of the object
|
|
||||||
|
|
||||||
if (!skip(','))
|
if (!skip(',')) return 0; // comma is missing
|
||||||
return 0; // comma is missing
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonNode *JsonParser::parseObjectKeyValue() {
|
JsonNode *JsonParser::parseObjectKeyValue() {
|
||||||
const char *key = QuotedString::extractFrom(_ptr, &_ptr);
|
const char *key = QuotedString::extractFrom(_ptr, &_ptr);
|
||||||
|
|
||||||
if (!key)
|
if (!key) return 0; // failed to extract key
|
||||||
return 0; // failed to extract key
|
|
||||||
|
|
||||||
if (!skip(':'))
|
if (!skip(':')) return 0; // colon is missing
|
||||||
return 0; // colon is missing
|
|
||||||
|
|
||||||
JsonNode *value = parseAnything();
|
JsonNode *value = parseAnything();
|
||||||
|
|
||||||
if (!value)
|
if (!value) return 0; // value parsing failed
|
||||||
return 0; // value parsing failed
|
|
||||||
|
|
||||||
return _buffer->createObjectKeyValueNode(key, value);
|
return _buffer->createObjectKeyValueNode(key, value);
|
||||||
}
|
}
|
||||||
|
@ -27,8 +27,7 @@ static inline size_t printCharTo(char c, Print *p) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
size_t QuotedString::printTo(const char *s, Print *p) {
|
size_t QuotedString::printTo(const char *s, Print *p) {
|
||||||
if (!s)
|
if (!s) return p->print("null");
|
||||||
return p->print("null");
|
|
||||||
|
|
||||||
size_t n = p->write('\"');
|
size_t n = p->write('\"');
|
||||||
|
|
||||||
@ -45,10 +44,8 @@ static char unescapeChar(char c) {
|
|||||||
const char *p = "b\bf\fn\nr\rt\t";
|
const char *p = "b\bf\fn\nr\rt\t";
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (p[0] == 0)
|
if (p[0] == 0) return c;
|
||||||
return c;
|
if (p[0] == c) return p[1];
|
||||||
if (p[0] == c)
|
|
||||||
return p[1];
|
|
||||||
p += 2;
|
p += 2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,8 +8,7 @@
|
|||||||
using namespace ArduinoJson::Internals;
|
using namespace ArduinoJson::Internals;
|
||||||
|
|
||||||
size_t StringBuilder::write(uint8_t c) {
|
size_t StringBuilder::write(uint8_t c) {
|
||||||
if (length >= capacity)
|
if (length >= capacity) return 0;
|
||||||
return 0;
|
|
||||||
|
|
||||||
buffer[length++] = c;
|
buffer[length++] = c;
|
||||||
buffer[length] = 0;
|
buffer[length] = 0;
|
||||||
|
@ -7,8 +7,7 @@ using namespace ArduinoJson::Internals;
|
|||||||
|
|
||||||
JsonValue JsonArray::operator[](int index) const {
|
JsonValue JsonArray::operator[](int index) const {
|
||||||
for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it) {
|
for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it) {
|
||||||
if (!index)
|
if (!index) return JsonValue(*it);
|
||||||
return JsonValue(*it);
|
|
||||||
index--;
|
index--;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -17,8 +16,7 @@ JsonValue JsonArray::operator[](int index) const {
|
|||||||
|
|
||||||
void JsonArray::add(bool value) {
|
void JsonArray::add(bool value) {
|
||||||
JsonNode *node = createNode();
|
JsonNode *node = createNode();
|
||||||
if (!node)
|
if (!node) return;
|
||||||
return;
|
|
||||||
|
|
||||||
node->setAsBoolean(value);
|
node->setAsBoolean(value);
|
||||||
addChild(node);
|
addChild(node);
|
||||||
@ -26,8 +24,7 @@ void JsonArray::add(bool value) {
|
|||||||
|
|
||||||
void JsonArray::add(char const *value) {
|
void JsonArray::add(char const *value) {
|
||||||
JsonNode *node = createNode();
|
JsonNode *node = createNode();
|
||||||
if (!node)
|
if (!node) return;
|
||||||
return;
|
|
||||||
|
|
||||||
node->setAsString(value);
|
node->setAsString(value);
|
||||||
addChild(node);
|
addChild(node);
|
||||||
@ -35,8 +32,7 @@ void JsonArray::add(char const *value) {
|
|||||||
|
|
||||||
void JsonArray::add(double value, int decimals) {
|
void JsonArray::add(double value, int decimals) {
|
||||||
JsonNode *node = createNode();
|
JsonNode *node = createNode();
|
||||||
if (!node)
|
if (!node) return;
|
||||||
return;
|
|
||||||
|
|
||||||
node->setAsDouble(value, decimals);
|
node->setAsDouble(value, decimals);
|
||||||
addChild(node);
|
addChild(node);
|
||||||
@ -44,8 +40,7 @@ void JsonArray::add(double value, int decimals) {
|
|||||||
|
|
||||||
void JsonArray::add(long value) {
|
void JsonArray::add(long value) {
|
||||||
JsonNode *node = createNode();
|
JsonNode *node = createNode();
|
||||||
if (!node)
|
if (!node) return;
|
||||||
return;
|
|
||||||
|
|
||||||
node->setAsLong(value);
|
node->setAsLong(value);
|
||||||
addChild(node);
|
addChild(node);
|
||||||
@ -54,8 +49,7 @@ void JsonArray::add(long value) {
|
|||||||
// TODO: we should have the same issue as in JsonValue
|
// TODO: we should have the same issue as in JsonValue
|
||||||
void JsonArray::add(JsonContainer nestedContainer) {
|
void JsonArray::add(JsonContainer nestedContainer) {
|
||||||
JsonNode *node = createNode();
|
JsonNode *node = createNode();
|
||||||
if (!node)
|
if (!node) return;
|
||||||
return;
|
|
||||||
|
|
||||||
node->duplicate(nestedContainer._node);
|
node->duplicate(nestedContainer._node);
|
||||||
addChild(node);
|
addChild(node);
|
||||||
@ -84,8 +78,7 @@ JsonObject JsonArray::createNestedObject() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
JsonArrayIterator JsonArray::begin() {
|
JsonArrayIterator JsonArray::begin() {
|
||||||
if (!_node)
|
if (!_node) return end();
|
||||||
return end();
|
|
||||||
|
|
||||||
return JsonArrayIterator(_node->getContainerChild());
|
return JsonArrayIterator(_node->getContainerChild());
|
||||||
}
|
}
|
@ -13,8 +13,7 @@ JsonValue JsonBuffer::createValue() { return JsonValue(createNode()); }
|
|||||||
|
|
||||||
JsonNode *JsonBuffer::createNode() {
|
JsonNode *JsonBuffer::createNode() {
|
||||||
void *node = allocateNode();
|
void *node = allocateNode();
|
||||||
if (!node)
|
if (!node) return 0;
|
||||||
return 0;
|
|
||||||
|
|
||||||
return new (node) JsonNode();
|
return new (node) JsonNode();
|
||||||
}
|
}
|
||||||
@ -37,8 +36,7 @@ JsonValue JsonBuffer::parseValue(char *json) {
|
|||||||
JsonNode *JsonBuffer::createArrayNode() {
|
JsonNode *JsonBuffer::createArrayNode() {
|
||||||
JsonNode *node = createNode();
|
JsonNode *node = createNode();
|
||||||
|
|
||||||
if (node)
|
if (node) node->setAsArray(this);
|
||||||
node->setAsArray(this);
|
|
||||||
|
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
@ -46,8 +44,7 @@ JsonNode *JsonBuffer::createArrayNode() {
|
|||||||
JsonNode *JsonBuffer::createBoolNode(bool value) {
|
JsonNode *JsonBuffer::createBoolNode(bool value) {
|
||||||
JsonNode *node = createNode();
|
JsonNode *node = createNode();
|
||||||
|
|
||||||
if (node)
|
if (node) node->setAsBoolean(value);
|
||||||
node->setAsBoolean(value);
|
|
||||||
|
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
@ -55,8 +52,7 @@ JsonNode *JsonBuffer::createBoolNode(bool value) {
|
|||||||
JsonNode *JsonBuffer::createDoubleNode(double value, int decimals) {
|
JsonNode *JsonBuffer::createDoubleNode(double value, int decimals) {
|
||||||
JsonNode *node = createNode();
|
JsonNode *node = createNode();
|
||||||
|
|
||||||
if (node)
|
if (node) node->setAsDouble(value, decimals);
|
||||||
node->setAsDouble(value, decimals);
|
|
||||||
|
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
@ -64,8 +60,7 @@ JsonNode *JsonBuffer::createDoubleNode(double value, int decimals) {
|
|||||||
JsonNode *JsonBuffer::createLongNode(long value) {
|
JsonNode *JsonBuffer::createLongNode(long value) {
|
||||||
JsonNode *node = createNode();
|
JsonNode *node = createNode();
|
||||||
|
|
||||||
if (node)
|
if (node) node->setAsLong(value);
|
||||||
node->setAsLong(value);
|
|
||||||
|
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
@ -73,8 +68,7 @@ JsonNode *JsonBuffer::createLongNode(long value) {
|
|||||||
JsonNode *JsonBuffer::createObjectNode() {
|
JsonNode *JsonBuffer::createObjectNode() {
|
||||||
JsonNode *node = createNode();
|
JsonNode *node = createNode();
|
||||||
|
|
||||||
if (node)
|
if (node) node->setAsObject(this);
|
||||||
node->setAsObject(this);
|
|
||||||
|
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
@ -83,8 +77,7 @@ Internals::JsonNode *JsonBuffer::createObjectKeyValueNode(const char *key,
|
|||||||
JsonNode *value) {
|
JsonNode *value) {
|
||||||
JsonNode *node = createNode();
|
JsonNode *node = createNode();
|
||||||
|
|
||||||
if (node)
|
if (node) node->setAsObjectKeyValue(key, value);
|
||||||
node->setAsObjectKeyValue(key, value);
|
|
||||||
|
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
@ -92,8 +85,7 @@ Internals::JsonNode *JsonBuffer::createObjectKeyValueNode(const char *key,
|
|||||||
JsonNode *JsonBuffer::createStringNode(const char *value) {
|
JsonNode *JsonBuffer::createStringNode(const char *value) {
|
||||||
JsonNode *node = createNode();
|
JsonNode *node = createNode();
|
||||||
|
|
||||||
if (node)
|
if (node) node->setAsString(value);
|
||||||
node->setAsString(value);
|
|
||||||
|
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
@ -36,32 +36,26 @@ size_t JsonContainer::prettyPrintTo(Print &print) const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
JsonNode *JsonContainer::createNode() {
|
JsonNode *JsonContainer::createNode() {
|
||||||
if (!_node)
|
if (!_node) return 0;
|
||||||
return 0;
|
|
||||||
|
|
||||||
JsonBuffer *buffer = _node->getContainerBuffer();
|
JsonBuffer *buffer = _node->getContainerBuffer();
|
||||||
if (!buffer)
|
if (!buffer) return 0;
|
||||||
return 0;
|
|
||||||
|
|
||||||
return buffer->createNode();
|
return buffer->createNode();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool JsonContainer::operator==(const JsonContainer &other) const {
|
bool JsonContainer::operator==(const JsonContainer &other) const {
|
||||||
if (_node == other._node)
|
if (_node == other._node) return true;
|
||||||
return true;
|
if (!_node || !other._node) return false;
|
||||||
if (!_node || !other._node)
|
|
||||||
return false;
|
|
||||||
return _node->getProxyTarget() == other._node->getProxyTarget();
|
return _node->getProxyTarget() == other._node->getProxyTarget();
|
||||||
}
|
}
|
||||||
|
|
||||||
void JsonContainer::addChild(JsonNode *childToAdd) {
|
void JsonContainer::addChild(JsonNode *childToAdd) {
|
||||||
if (_node)
|
if (_node) _node->addChild(childToAdd);
|
||||||
_node->addChild(childToAdd);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void JsonContainer::removeChild(JsonNode *childToRemove) {
|
void JsonContainer::removeChild(JsonNode *childToRemove) {
|
||||||
if (_node)
|
if (_node) _node->removeChild(childToRemove);
|
||||||
_node->removeChild(childToRemove);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t JsonContainer::size() const {
|
size_t JsonContainer::size() const {
|
||||||
|
@ -28,8 +28,7 @@ void JsonObject::remove(char const *key) {
|
|||||||
JsonArray JsonObject::createNestedArray(char const *key) {
|
JsonArray JsonObject::createNestedArray(char const *key) {
|
||||||
JsonNode *node = getOrCreateNodeAt(key);
|
JsonNode *node = getOrCreateNodeAt(key);
|
||||||
|
|
||||||
if (node)
|
if (node) node->setAsArray(_node->getContainerBuffer());
|
||||||
node->setAsArray(_node->getContainerBuffer());
|
|
||||||
|
|
||||||
return JsonArray(node);
|
return JsonArray(node);
|
||||||
}
|
}
|
||||||
@ -37,8 +36,7 @@ JsonArray JsonObject::createNestedArray(char const *key) {
|
|||||||
JsonObject JsonObject::createNestedObject(char const *key) {
|
JsonObject JsonObject::createNestedObject(char const *key) {
|
||||||
JsonNode *node = getOrCreateNodeAt(key);
|
JsonNode *node = getOrCreateNodeAt(key);
|
||||||
|
|
||||||
if (node)
|
if (node) node->setAsObject(_node->getContainerBuffer());
|
||||||
node->setAsObject(_node->getContainerBuffer());
|
|
||||||
|
|
||||||
return JsonObject(node);
|
return JsonObject(node);
|
||||||
}
|
}
|
||||||
@ -47,17 +45,14 @@ JsonNode *JsonObject::getOrCreateNodeAt(const char *key) {
|
|||||||
for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it) {
|
for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it) {
|
||||||
const char *childKey = it->getAsObjectKey();
|
const char *childKey = it->getAsObjectKey();
|
||||||
|
|
||||||
if (!strcmp(childKey, key))
|
if (!strcmp(childKey, key)) return it->getAsObjectValue();
|
||||||
return it->getAsObjectValue();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonNode *newValueNode = createNode();
|
JsonNode *newValueNode = createNode();
|
||||||
if (!newValueNode)
|
if (!newValueNode) return 0;
|
||||||
return 0;
|
|
||||||
|
|
||||||
JsonNode *newKeyNode = createNode();
|
JsonNode *newKeyNode = createNode();
|
||||||
if (!newKeyNode)
|
if (!newKeyNode) return 0;
|
||||||
return 0;
|
|
||||||
|
|
||||||
newKeyNode->setAsObjectKeyValue(key, newValueNode);
|
newKeyNode->setAsObjectKeyValue(key, newValueNode);
|
||||||
|
|
||||||
|
@ -7,23 +7,19 @@
|
|||||||
using namespace ArduinoJson;
|
using namespace ArduinoJson;
|
||||||
|
|
||||||
void JsonValue::operator=(bool value) {
|
void JsonValue::operator=(bool value) {
|
||||||
if (_node)
|
if (_node) _node->setAsBoolean(value);
|
||||||
_node->setAsBoolean(value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void JsonValue::operator=(char const *value) {
|
void JsonValue::operator=(char const *value) {
|
||||||
if (_node)
|
if (_node) _node->setAsString(value);
|
||||||
_node->setAsString(value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void JsonValue::set(double value, int decimals) {
|
void JsonValue::set(double value, int decimals) {
|
||||||
if (_node)
|
if (_node) _node->setAsDouble(value, decimals);
|
||||||
_node->setAsDouble(value, decimals);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void JsonValue::operator=(int value) {
|
void JsonValue::operator=(int value) {
|
||||||
if (_node)
|
if (_node) _node->setAsLong(value);
|
||||||
_node->setAsLong(value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonValue::operator bool() const {
|
JsonValue::operator bool() const {
|
||||||
|
@ -10,11 +10,13 @@ protected:
|
|||||||
|
|
||||||
void nodeCountMustBe(int expected) { EXPECT_EQ(expected, json.size()); }
|
void nodeCountMustBe(int expected) { EXPECT_EQ(expected, json.size()); }
|
||||||
|
|
||||||
template <typename T> void firstElementMustBe(T expected) {
|
template <typename T>
|
||||||
|
void firstElementMustBe(T expected) {
|
||||||
elementAtIndexMustBe(0, expected);
|
elementAtIndexMustBe(0, expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T> void secondElementMustBe(T expected) {
|
template <typename T>
|
||||||
|
void secondElementMustBe(T expected) {
|
||||||
elementAtIndexMustBe(1, expected);
|
elementAtIndexMustBe(1, expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -24,7 +26,8 @@ protected:
|
|||||||
JsonArray array;
|
JsonArray array;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
template <typename T> void elementAtIndexMustBe(int index, T expected) {
|
template <typename T>
|
||||||
|
void elementAtIndexMustBe(int index, T expected) {
|
||||||
EXPECT_EQ(expected, array[index].as<T>());
|
EXPECT_EQ(expected, array[index].as<T>());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -33,7 +33,8 @@ TEST_F(JsonArray_PrettyPrintTo_Tests, Empty) { outputMustBe("[]"); }
|
|||||||
TEST_F(JsonArray_PrettyPrintTo_Tests, OneElement) {
|
TEST_F(JsonArray_PrettyPrintTo_Tests, OneElement) {
|
||||||
array.add(1);
|
array.add(1);
|
||||||
|
|
||||||
outputMustBe("[\r\n"
|
outputMustBe(
|
||||||
|
"[\r\n"
|
||||||
" 1\r\n"
|
" 1\r\n"
|
||||||
"]");
|
"]");
|
||||||
}
|
}
|
||||||
@ -42,7 +43,8 @@ TEST_F(JsonArray_PrettyPrintTo_Tests, TwoElements) {
|
|||||||
array.add(1);
|
array.add(1);
|
||||||
array.add(2);
|
array.add(2);
|
||||||
|
|
||||||
outputMustBe("[\r\n"
|
outputMustBe(
|
||||||
|
"[\r\n"
|
||||||
" 1,\r\n"
|
" 1,\r\n"
|
||||||
" 2\r\n"
|
" 2\r\n"
|
||||||
"]");
|
"]");
|
||||||
@ -52,7 +54,8 @@ TEST_F(JsonArray_PrettyPrintTo_Tests, EmptyNestedArrays) {
|
|||||||
array.createNestedArray();
|
array.createNestedArray();
|
||||||
array.createNestedArray();
|
array.createNestedArray();
|
||||||
|
|
||||||
outputMustBe("[\r\n"
|
outputMustBe(
|
||||||
|
"[\r\n"
|
||||||
" [],\r\n"
|
" [],\r\n"
|
||||||
" []\r\n"
|
" []\r\n"
|
||||||
"]");
|
"]");
|
||||||
@ -66,7 +69,8 @@ TEST_F(JsonArray_PrettyPrintTo_Tests, NestedArrays) {
|
|||||||
JsonObject nested2 = array.createNestedObject();
|
JsonObject nested2 = array.createNestedObject();
|
||||||
nested2["key"] = 3;
|
nested2["key"] = 3;
|
||||||
|
|
||||||
outputMustBe("[\r\n"
|
outputMustBe(
|
||||||
|
"[\r\n"
|
||||||
" [\r\n"
|
" [\r\n"
|
||||||
" 1,\r\n"
|
" 1,\r\n"
|
||||||
" 2\r\n"
|
" 2\r\n"
|
||||||
|
@ -32,7 +32,8 @@ TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyObject) { outputMustBe("{}"); }
|
|||||||
TEST_F(JsonObject_PrettyPrintTo_Tests, OneMember) {
|
TEST_F(JsonObject_PrettyPrintTo_Tests, OneMember) {
|
||||||
object["key"] = "value";
|
object["key"] = "value";
|
||||||
|
|
||||||
outputMustBe("{\r\n"
|
outputMustBe(
|
||||||
|
"{\r\n"
|
||||||
" \"key\": \"value\"\r\n"
|
" \"key\": \"value\"\r\n"
|
||||||
"}");
|
"}");
|
||||||
}
|
}
|
||||||
@ -41,7 +42,8 @@ TEST_F(JsonObject_PrettyPrintTo_Tests, TwoMembers) {
|
|||||||
object["key1"] = "value1";
|
object["key1"] = "value1";
|
||||||
object["key2"] = "value2";
|
object["key2"] = "value2";
|
||||||
|
|
||||||
outputMustBe("{\r\n"
|
outputMustBe(
|
||||||
|
"{\r\n"
|
||||||
" \"key1\": \"value1\",\r\n"
|
" \"key1\": \"value1\",\r\n"
|
||||||
" \"key2\": \"value2\"\r\n"
|
" \"key2\": \"value2\"\r\n"
|
||||||
"}");
|
"}");
|
||||||
@ -51,7 +53,8 @@ TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyNestedContainers) {
|
|||||||
object.createNestedObject("key1");
|
object.createNestedObject("key1");
|
||||||
object.createNestedArray("key2");
|
object.createNestedArray("key2");
|
||||||
|
|
||||||
outputMustBe("{\r\n"
|
outputMustBe(
|
||||||
|
"{\r\n"
|
||||||
" \"key1\": {},\r\n"
|
" \"key1\": {},\r\n"
|
||||||
" \"key2\": []\r\n"
|
" \"key2\": []\r\n"
|
||||||
"}");
|
"}");
|
||||||
@ -64,7 +67,8 @@ TEST_F(JsonObject_PrettyPrintTo_Tests, NestedContainers) {
|
|||||||
JsonArray nested2 = object.createNestedArray("key2");
|
JsonArray nested2 = object.createNestedArray("key2");
|
||||||
nested2.add(2);
|
nested2.add(2);
|
||||||
|
|
||||||
outputMustBe("{\r\n"
|
outputMustBe(
|
||||||
|
"{\r\n"
|
||||||
" \"key1\": {\r\n"
|
" \"key1\": {\r\n"
|
||||||
" \"a\": 1\r\n"
|
" \"a\": 1\r\n"
|
||||||
" },\r\n"
|
" },\r\n"
|
||||||
|
@ -20,15 +20,18 @@ protected:
|
|||||||
|
|
||||||
void sizeMustBe(int expected) { EXPECT_EQ(expected, _array.size()); }
|
void sizeMustBe(int expected) { EXPECT_EQ(expected, _array.size()); }
|
||||||
|
|
||||||
template <typename T> void firstElementMustBe(T expected) {
|
template <typename T>
|
||||||
|
void firstElementMustBe(T expected) {
|
||||||
elementAtIndexMustBe(0, expected);
|
elementAtIndexMustBe(0, expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T> void secondElementMustBe(T expected) {
|
template <typename T>
|
||||||
|
void secondElementMustBe(T expected) {
|
||||||
elementAtIndexMustBe(1, expected);
|
elementAtIndexMustBe(1, expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T> void elementAtIndexMustBe(int index, T expected) {
|
template <typename T>
|
||||||
|
void elementAtIndexMustBe(int index, T expected) {
|
||||||
EXPECT_EQ(expected, _array[index].as<T>());
|
EXPECT_EQ(expected, _array[index].as<T>());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,7 +21,8 @@ protected:
|
|||||||
EXPECT_STREQ(expected, _object[key].as<const char *>());
|
EXPECT_STREQ(expected, _object[key].as<const char *>());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T> void keyMustHaveValue(const char *key, T expected) {
|
template <typename T>
|
||||||
|
void keyMustHaveValue(const char *key, T expected) {
|
||||||
EXPECT_EQ(expected, _object[key].as<T>());
|
EXPECT_EQ(expected, _object[key].as<T>());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user