Formated code with clang-format

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

View File

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

View File

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

View File

@ -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;
writer.writeComma();
}
child = child->next;
if (!child)
break;
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;
JsonNode* newNode = buffer->createNode();
if (!newNode) return;
void JsonNode::setAsProxyOfSelf() {
JsonBuffer *buffer = content.asContainer.buffer;
if (!buffer)
return;
*newNode = *this;
JsonNode *newNode = buffer->createNode();
if (!newNode)
return;
setAsProxyOf(newNode);
*newNode = *this;
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;
}
}

View File

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

View File

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

View File

@ -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];
return p[1];
}
static inline size_t printCharTo(char c, Print* p)
{
char specialChar = getSpecialChar(c);
static inline size_t printCharTo(char c, Print *p) {
char specialChar = getSpecialChar(c);
return specialChar != 0
? p->write('\\') + p->write(specialChar)
: p->write(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('\"');
size_t QuotedString::printTo(const char *s, Print *p) {
if (!s)
return p->print("null");
while (*s)
{
n += printCharTo(*s++, p);
}
size_t n = p->write('\"');
return 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
static char unescapeChar(char c) {
// Optimized for code size on a 8-bit AVR
const char* p = "b\bf\fn\nr\rt\t";
const char *p = "b\bf\fn\nr\rt\t";
for (;;)
{
if (p[0] == 0) return c;
if (p[0] == c) return p[1];
p += 2;
}
for (;;) {
if (p[0] == 0)
return c;
if (p[0] == c)
return p[1];
p += 2;
}
}
static inline bool isQuote(char c)
{
return c == '\"' || c == '\'';
}
static inline bool isQuote(char c) { return c == '\"' || c == '\''; }
char* QuotedString::extractFrom(char* input, char** endPtr)
{
char firstChar = *input;
char *QuotedString::extractFrom(char *input, char **endPtr) {
char firstChar = *input;
if (!isQuote(firstChar))
{
// must start with a quote
return 0;
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;
}
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;
}
if (c == stopChar)
{
// closing quote
break;
}
if (c == '\\')
{
// replace char
c = unescapeChar(*readPtr++);
}
*writePtr++ = c;
if (c == stopChar) {
// closing quote
break;
}
// end the string here
*writePtr = 0;
if (c == '\\') {
// replace char
c = unescapeChar(*readPtr++);
}
// update end ptr
*endPtr = readPtr;
*writePtr++ = c;
}
return startPtr;
// end the string here
*writePtr = 0;
// update end ptr
*endPtr = readPtr;
return startPtr;
}

View File

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

View File

@ -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();
if (node)
{
node->setAsArray(_node->getContainerBuffer());
addChild(node);
}
return JsonArray(node);
JsonObject JsonArray::createNestedObject() {
JsonNode *node = createNode();
if (node) {
node->setAsObject(_node->getContainerBuffer());
addChild(node);
}
return JsonObject(node);
}
JsonObject JsonArray::createNestedObject()
{
JsonNode* node = createNode();
if (node)
{
node->setAsObject(_node->getContainerBuffer());
addChild(node);
}
return JsonObject(node);
}
JsonArrayIterator JsonArray::begin() {
if (!_node)
return end();
JsonArrayIterator JsonArray::begin()
{
if (!_node)
return end();
return JsonArrayIterator(_node->getContainerChild());
return JsonArrayIterator(_node->getContainerChild());
}

View File

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

View File

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

View File

@ -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);
if (node)
node->setAsObject(_node->getContainerBuffer());
JsonObject JsonObject::createNestedObject(char const *key) {
JsonNode *node = getOrCreateNodeAt(key);
return JsonObject(node);
if (node)
node->setAsObject(_node->getContainerBuffer());
return JsonObject(node);
}
JsonNode* JsonObject::getOrCreateNodeAt(const char* key)
{
for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it)
{
const char* childKey = it->getAsObjectKey();
JsonNode *JsonObject::getOrCreateNodeAt(const char *key) {
for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it) {
const char *childKey = it->getAsObjectKey();
if (!strcmp(childKey, key))
return it->getAsObjectValue();
}
JsonNode* newValueNode = createNode();
if (!newValueNode) return 0;
JsonNode* newKeyNode = createNode();
if (!newKeyNode) return 0;
if (!strcmp(childKey, key))
return it->getAsObjectValue();
}
newKeyNode->setAsObjectKeyValue(key, newValueNode);
JsonNode *newValueNode = createNode();
if (!newValueNode)
return 0;
addChild(newKeyNode);
JsonNode *newKeyNode = createNode();
if (!newKeyNode)
return 0;
return newValueNode;
newKeyNode->setAsObjectKeyValue(key, newValueNode);
addChild(newKeyNode);
return newValueNode;
}
JsonObjectIterator JsonObject::begin()
{
return JsonObjectIterator(_node->getContainerChild());
JsonObjectIterator JsonObject::begin() {
return JsonObjectIterator(_node->getContainerChild());
}

View File

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