forked from bblanchon/ArduinoJson
Renamed srcs/ into src/
This commit is contained in:
40
src/Arduino/Print.cpp
Normal file
40
src/Arduino/Print.cpp
Normal file
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Arduino JSON library
|
||||
* Benoit Blanchon 2014 - MIT License
|
||||
*/
|
||||
|
||||
#ifndef ARDUINO
|
||||
|
||||
#include "ArduinoJson/Arduino/Print.h"
|
||||
#include <cstdio>
|
||||
|
||||
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, "%.*lg", 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::println()
|
||||
{
|
||||
return write('\r') + write('\n');
|
||||
}
|
||||
|
||||
#endif
|
6
src/CMakeLists.txt
Normal file
6
src/CMakeLists.txt
Normal file
@ -0,0 +1,6 @@
|
||||
file(GLOB_RECURSE INC_FILES ../include/*.h)
|
||||
file(GLOB_RECURSE SRC_FILES *.cpp)
|
||||
|
||||
include_directories(../include)
|
||||
|
||||
add_library(ArduinoJson ${SRC_FILES} ${INC_FILES})
|
1
src/Internals/CompactJsonWriter.cpp
Normal file
1
src/Internals/CompactJsonWriter.cpp
Normal file
@ -0,0 +1 @@
|
||||
#include "ArduinoJson/Internals/CompactJsonWriter.h"
|
45
src/Internals/EscapedString.cpp
Normal file
45
src/Internals/EscapedString.cpp
Normal file
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Arduino JSON library
|
||||
* Benoit Blanchon 2014 - MIT License
|
||||
*/
|
||||
|
||||
#include "ArduinoJson/Internals/EscapedString.h"
|
||||
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
||||
static inline char getSpecialChar(char c)
|
||||
{
|
||||
// Optimized for code size on a 8-bit AVR
|
||||
|
||||
const char* p = "\"\"\\\\\bb\ff\nn\rr\tt\0";
|
||||
|
||||
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 EscapedString::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('\"');
|
||||
}
|
45
src/Internals/IndentedPrint.cpp
Normal file
45
src/Internals/IndentedPrint.cpp
Normal file
@ -0,0 +1,45 @@
|
||||
#include "ArduinoJson/Internals/IndentedPrint.h"
|
||||
|
||||
using namespace ArduinoJson::Generator;
|
||||
|
||||
void IndentedPrint::indent()
|
||||
{
|
||||
if (level < MAX_LEVEL)
|
||||
level++;
|
||||
}
|
||||
|
||||
void IndentedPrint::unindent()
|
||||
{
|
||||
if (level > 0)
|
||||
level--;
|
||||
}
|
||||
|
||||
void IndentedPrint::setTabSize(uint8_t n)
|
||||
{
|
||||
if (n < MAX_TAB_SIZE)
|
||||
tabSize = n;
|
||||
}
|
||||
|
||||
size_t IndentedPrint::write(uint8_t c)
|
||||
{
|
||||
size_t n = 0;
|
||||
|
||||
if (isNewLine)
|
||||
n += writeTabs();
|
||||
|
||||
n += sink->write(c);
|
||||
|
||||
isNewLine = c == '\n';
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
inline size_t IndentedPrint::writeTabs()
|
||||
{
|
||||
size_t n = 0;
|
||||
|
||||
for (int i = 0; i < level*tabSize; i++)
|
||||
n += sink->write(' ');
|
||||
|
||||
return n;
|
||||
}
|
166
src/Internals/JsonNode.cpp
Normal file
166
src/Internals/JsonNode.cpp
Normal file
@ -0,0 +1,166 @@
|
||||
#include "ArduinoJson/Internals/JsonNode.h"
|
||||
|
||||
#include "ArduinoJson/Internals/JsonWriter.h"
|
||||
#include "ArduinoJson/JsonArray.h"
|
||||
#include "ArduinoJson/JsonObject.h"
|
||||
#include "ArduinoJson/JsonBuffer.h"
|
||||
|
||||
void JsonNode::writeTo(JsonWriter& writer)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case JSON_PROXY:
|
||||
content.asProxy.target->writeTo(writer);
|
||||
break;
|
||||
|
||||
case JSON_ARRAY:
|
||||
writeArrayTo(writer);
|
||||
break;
|
||||
|
||||
case JSON_OBJECT:
|
||||
writeObjectTo(writer);
|
||||
break;
|
||||
|
||||
case JSON_STRING:
|
||||
writer.writeString(content.asString);
|
||||
break;
|
||||
|
||||
case JSON_LONG:
|
||||
writer.writeInteger(content.asInteger);
|
||||
break;
|
||||
|
||||
case JSON_BOOLEAN:
|
||||
writer.writeBoolean(content.asBoolean);
|
||||
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);
|
||||
|
||||
if (type != JSON_ARRAY && type != JSON_OBJECT)
|
||||
return;
|
||||
|
||||
JsonNode* lastChild = content.asContainer.child;
|
||||
|
||||
if (!lastChild)
|
||||
{
|
||||
content.asContainer.child = childToAdd;
|
||||
return;
|
||||
}
|
||||
|
||||
while (lastChild->next)
|
||||
lastChild = lastChild->next;
|
||||
|
||||
lastChild->next = childToAdd;
|
||||
}
|
||||
|
||||
void JsonNode::removeChild(JsonNode* childToRemove)
|
||||
{
|
||||
if (type == JSON_PROXY)
|
||||
return content.asProxy.target->removeChild(childToRemove);
|
||||
|
||||
if (type != JSON_ARRAY && type != JSON_OBJECT) 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;
|
||||
}
|
||||
}
|
||||
|
||||
void JsonNode::writeArrayTo(JsonWriter& writer)
|
||||
{
|
||||
JsonNode* child = content.asContainer.child;
|
||||
|
||||
if (child)
|
||||
{
|
||||
writer.beginArray();
|
||||
|
||||
for (;;)
|
||||
{
|
||||
child->writeTo(writer);
|
||||
|
||||
child = child->next;
|
||||
if (!child) break;
|
||||
|
||||
writer.writeComma();
|
||||
}
|
||||
|
||||
writer.endArray();
|
||||
}
|
||||
else
|
||||
{
|
||||
writer.writeEmptyArray();
|
||||
}
|
||||
}
|
||||
|
||||
void JsonNode::writeObjectTo(JsonWriter& writer)
|
||||
{
|
||||
JsonNode* child = content.asContainer.child;
|
||||
|
||||
if (child)
|
||||
{
|
||||
writer.beginObject();
|
||||
|
||||
for (;;)
|
||||
{
|
||||
writer.writeString(child->content.asKey.key);
|
||||
writer.writeColon();
|
||||
child->content.asKey.value->writeTo(writer);
|
||||
|
||||
child = child->next;
|
||||
if (!child) break;
|
||||
|
||||
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;
|
||||
|
||||
*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;
|
||||
}
|
||||
}
|
190
src/Internals/JsonParser.cpp
Normal file
190
src/Internals/JsonParser.cpp
Normal file
@ -0,0 +1,190 @@
|
||||
#include "ArduinoJson/Internals/JsonParser.h"
|
||||
#include "ArduinoJson/JsonBuffer.h"
|
||||
#include <stdlib.h> // for strtol, strtod
|
||||
#include <ctype.h>
|
||||
|
||||
bool JsonParser::isArrayStart()
|
||||
{
|
||||
return *_ptr == '[';
|
||||
}
|
||||
|
||||
bool JsonParser::isArrayStop()
|
||||
{
|
||||
return *_ptr == ']';
|
||||
}
|
||||
|
||||
bool JsonParser::isBoolean()
|
||||
{
|
||||
return *_ptr == 't' || *_ptr == 'f';
|
||||
}
|
||||
|
||||
bool JsonParser::isComma()
|
||||
{
|
||||
return *_ptr == ',';
|
||||
}
|
||||
|
||||
bool JsonParser::isDouble()
|
||||
{
|
||||
char* ptr = _ptr;
|
||||
|
||||
// skip all digits
|
||||
while (isdigit(*ptr))
|
||||
ptr++;
|
||||
|
||||
// same position => 0 digits => not a number
|
||||
if (ptr == _ptr)
|
||||
return false;
|
||||
|
||||
// stopped on a decimal separator => ok it's a double
|
||||
return *ptr == '.';
|
||||
}
|
||||
|
||||
bool JsonParser::isEnd()
|
||||
{
|
||||
return *_ptr == 0;
|
||||
}
|
||||
|
||||
bool JsonParser::isLong()
|
||||
{
|
||||
char* ptr = _ptr;
|
||||
|
||||
// skip all digits
|
||||
while (isdigit(*ptr))
|
||||
ptr++;
|
||||
|
||||
// same position => 0 digits => not a number
|
||||
if (ptr == _ptr)
|
||||
return false;
|
||||
|
||||
// stopped on a decimal separator => not a long but a double
|
||||
return *ptr != '.';
|
||||
}
|
||||
|
||||
bool JsonParser::isNull()
|
||||
{
|
||||
return *_ptr == 'n';
|
||||
}
|
||||
|
||||
bool JsonParser::isSpace()
|
||||
{
|
||||
return *_ptr == ' ' || *_ptr == '\t' || *_ptr == '\n' || *_ptr == '\r';
|
||||
}
|
||||
|
||||
bool JsonParser::isString()
|
||||
{
|
||||
return *_ptr == '\"';
|
||||
}
|
||||
|
||||
void JsonParser::skipOneChar()
|
||||
{
|
||||
_ptr++;
|
||||
}
|
||||
|
||||
void JsonParser::skipSpaces()
|
||||
{
|
||||
while(isSpace()) skipOneChar();
|
||||
}
|
||||
|
||||
JsonNode* JsonParser::parseAnything()
|
||||
{
|
||||
skipSpaces();
|
||||
|
||||
if (isArrayStart())
|
||||
return parseArray();
|
||||
|
||||
if (isBoolean())
|
||||
return parseBoolean();
|
||||
|
||||
if (isDouble())
|
||||
return parseDouble();
|
||||
|
||||
if (isLong())
|
||||
return parseLong();
|
||||
|
||||
if (isNull())
|
||||
return parseNull();
|
||||
|
||||
if (isString())
|
||||
return parseString();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
JsonNode* JsonParser::parseArray()
|
||||
{
|
||||
JsonNode* node = _buffer->createNode();
|
||||
node->setAsArray(_buffer);
|
||||
|
||||
skipOneChar(); // skip the '['
|
||||
skipSpaces();
|
||||
|
||||
if (isEnd())
|
||||
return 0;
|
||||
|
||||
if (isArrayStop())
|
||||
return node;
|
||||
|
||||
for(;;)
|
||||
{
|
||||
node->addChild(parseAnything());
|
||||
|
||||
skipSpaces();
|
||||
|
||||
if (isArrayStop())
|
||||
return node;
|
||||
|
||||
if (!isComma())
|
||||
return 0;
|
||||
|
||||
skipOneChar(); // skip the ','
|
||||
}
|
||||
}
|
||||
|
||||
JsonNode *JsonParser::parseBoolean()
|
||||
{
|
||||
bool value = *_ptr == 't';
|
||||
|
||||
_ptr += value ? 4 : 5;
|
||||
// 4 = strlen("true")
|
||||
// 5 = strlen("false");
|
||||
|
||||
return _buffer->createBoolNode(value);
|
||||
}
|
||||
|
||||
JsonNode *JsonParser::parseDouble()
|
||||
{
|
||||
double value = strtod(_ptr, &_ptr);
|
||||
|
||||
int decimals = 0;
|
||||
while(_ptr[1-decimals] != '.')
|
||||
decimals++;
|
||||
|
||||
return _buffer->createDoubleNode(value, decimals);
|
||||
}
|
||||
|
||||
JsonNode* JsonParser::parseLong()
|
||||
{
|
||||
long value = strtol(_ptr, &_ptr, 10);
|
||||
|
||||
return _buffer->createLongNode(value);
|
||||
}
|
||||
|
||||
JsonNode* JsonParser::parseNull()
|
||||
{
|
||||
_ptr += 4;
|
||||
// 4 = strlen("null")
|
||||
|
||||
return _buffer->createStringNode(0);
|
||||
}
|
||||
|
||||
JsonNode* JsonParser::parseString()
|
||||
{
|
||||
const char* s = ++_ptr;
|
||||
|
||||
while (*_ptr != '\"')
|
||||
_ptr++;
|
||||
*_ptr = 0;
|
||||
_ptr++;
|
||||
|
||||
return _buffer->createStringNode(s);
|
||||
}
|
25
src/Internals/JsonWriter.cpp
Normal file
25
src/Internals/JsonWriter.cpp
Normal file
@ -0,0 +1,25 @@
|
||||
#include "ArduinoJson/Internals/JsonWriter.h"
|
||||
#include "ArduinoJson/Internals/EscapedString.h"
|
||||
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
||||
void JsonWriter::writeString(char const* value)
|
||||
{
|
||||
_length += EscapedString::printTo(value, _sink);
|
||||
}
|
||||
|
||||
void JsonWriter::writeInteger(long value)
|
||||
{
|
||||
|
||||
_length += _sink->print(value);
|
||||
}
|
||||
|
||||
void JsonWriter::writeBoolean(bool value)
|
||||
{
|
||||
_length += _sink->print(value ? "true" : "false");
|
||||
}
|
||||
|
||||
void JsonWriter::writeDouble(double value, int decimals)
|
||||
{
|
||||
_length += _sink->print(value, decimals);
|
||||
}
|
1
src/Internals/PrettyJsonWriter.cpp
Normal file
1
src/Internals/PrettyJsonWriter.cpp
Normal file
@ -0,0 +1 @@
|
||||
#include "ArduinoJson/Internals/PrettyJsonWriter.h"
|
17
src/Internals/StringBuilder.cpp
Normal file
17
src/Internals/StringBuilder.cpp
Normal file
@ -0,0 +1,17 @@
|
||||
/*
|
||||
* Arduino JSON library
|
||||
* Benoit Blanchon 2014 - MIT License
|
||||
*/
|
||||
|
||||
#include "ArduinoJson/Internals/StringBuilder.h"
|
||||
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
||||
size_t StringBuilder::write(uint8_t c)
|
||||
{
|
||||
if (length >= capacity) return 0;
|
||||
|
||||
buffer[length++] = c;
|
||||
buffer[length] = 0;
|
||||
return 1;
|
||||
}
|
86
src/JsonArray.cpp
Normal file
86
src/JsonArray.cpp
Normal file
@ -0,0 +1,86 @@
|
||||
#include "ArduinoJson/JsonArray.h"
|
||||
#include "ArduinoJson/JsonObject.h"
|
||||
#include "ArduinoJson/JsonValue.h"
|
||||
|
||||
JsonValue JsonArray::operator[](int index) const
|
||||
{
|
||||
for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it)
|
||||
{
|
||||
if (!index) return JsonValue(*it);
|
||||
index--;
|
||||
}
|
||||
|
||||
return JsonValue();
|
||||
}
|
||||
|
||||
void JsonArray::add(bool value)
|
||||
{
|
||||
JsonNode* node = createNode();
|
||||
if (!node) return;
|
||||
|
||||
node->setAsBoolean(value);
|
||||
addChild(node);
|
||||
}
|
||||
|
||||
void JsonArray::add(char const* value)
|
||||
{
|
||||
JsonNode* node = createNode();
|
||||
if (!node) return;
|
||||
|
||||
node->setAsString(value);
|
||||
addChild(node);
|
||||
}
|
||||
|
||||
void JsonArray::add(double value, int decimals)
|
||||
{
|
||||
JsonNode* node = createNode();
|
||||
if (!node) return;
|
||||
|
||||
node->setAsDouble(value, decimals);
|
||||
addChild(node);
|
||||
}
|
||||
|
||||
void JsonArray::add(long value)
|
||||
{
|
||||
JsonNode* node = createNode();
|
||||
if (!node) return;
|
||||
|
||||
node->setAsLong(value);
|
||||
addChild(node);
|
||||
}
|
||||
|
||||
// TODO: we should have the same issue as in JsonValue
|
||||
void JsonArray::add(JsonContainer nestedContainer)
|
||||
{
|
||||
JsonNode* node = createNode();
|
||||
if (!node) return;
|
||||
|
||||
node->duplicate(nestedContainer._node);
|
||||
addChild(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);
|
||||
}
|
86
src/JsonBuffer.cpp
Normal file
86
src/JsonBuffer.cpp
Normal file
@ -0,0 +1,86 @@
|
||||
#include "ArduinoJson/JsonBuffer.h"
|
||||
|
||||
#include <new>
|
||||
|
||||
#include "ArduinoJson/JsonValue.h"
|
||||
#include "ArduinoJson/Internals/JsonParser.h"
|
||||
#include "ArduinoJson/Internals/JsonNode.h"
|
||||
|
||||
JsonValue JsonBuffer::createValue()
|
||||
{
|
||||
return JsonValue(createNode());
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
JsonNode* JsonBuffer::createArrayNode()
|
||||
{
|
||||
JsonNode* node = createNode();
|
||||
|
||||
if (node)
|
||||
node->setAsArray(this);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
JsonNode* JsonBuffer::createBoolNode(bool value)
|
||||
{
|
||||
JsonNode* node = createNode();
|
||||
|
||||
if (node)
|
||||
node->setAsBoolean(value);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
JsonNode* JsonBuffer::createDoubleNode(double value, int decimals)
|
||||
{
|
||||
JsonNode* node = createNode();
|
||||
|
||||
if (node)
|
||||
node->setAsDouble(value, decimals);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
JsonNode* JsonBuffer::createLongNode(long value)
|
||||
{
|
||||
JsonNode* node = createNode();
|
||||
|
||||
if (node)
|
||||
node->setAsLong(value);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
JsonNode* JsonBuffer::createObjectNode()
|
||||
{
|
||||
JsonNode* node = createNode();
|
||||
|
||||
if (node)
|
||||
node->setAsObject(this);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
JsonNode* JsonBuffer::createStringNode(const char* value)
|
||||
{
|
||||
JsonNode* node = createNode();
|
||||
|
||||
if (node)
|
||||
node->setAsString(value);
|
||||
|
||||
return node;
|
||||
}
|
82
src/JsonContainer.cpp
Normal file
82
src/JsonContainer.cpp
Normal file
@ -0,0 +1,82 @@
|
||||
#include "ArduinoJson/JsonContainer.h"
|
||||
|
||||
#include "ArduinoJson/JsonBuffer.h"
|
||||
#include "ArduinoJson/Internals/StringBuilder.h"
|
||||
#include "ArduinoJson/Internals/CompactJsonWriter.h"
|
||||
#include "ArduinoJson/Internals/PrettyJsonWriter.h"
|
||||
|
||||
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(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(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);
|
||||
}
|
||||
|
||||
JsonNode* JsonContainer::createNode()
|
||||
{
|
||||
if (!_node) return 0;
|
||||
|
||||
JsonBuffer* buffer = _node->getContainerBuffer();
|
||||
if (!buffer) return 0;
|
||||
|
||||
return buffer->createNode();
|
||||
}
|
||||
|
||||
bool JsonContainer::operator==(const JsonContainer & other) const
|
||||
{
|
||||
if (_node == other._node) return true;
|
||||
if (!_node || !other._node) return false;
|
||||
return _node->getProxyTarget() == other._node->getProxyTarget();
|
||||
}
|
||||
|
||||
void JsonContainer::addChild(JsonNode* childToAdd)
|
||||
{
|
||||
if (_node)
|
||||
_node->addChild(childToAdd);
|
||||
}
|
||||
|
||||
void JsonContainer::removeChild(JsonNode* childToRemove)
|
||||
{
|
||||
if (_node)
|
||||
_node->removeChild(childToRemove);
|
||||
}
|
||||
|
||||
size_t JsonContainer::size() const
|
||||
{
|
||||
int size = 0;
|
||||
|
||||
for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it)
|
||||
{
|
||||
size++;
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
73
src/JsonObject.cpp
Normal file
73
src/JsonObject.cpp
Normal file
@ -0,0 +1,73 @@
|
||||
#include "ArduinoJson/JsonObject.h"
|
||||
|
||||
#include <string.h> // for strcmp
|
||||
|
||||
#include "ArduinoJson/JsonBuffer.h"
|
||||
#include "ArduinoJson/JsonValue.h"
|
||||
#include "ArduinoJson/Internals/EscapedString.h"
|
||||
#include "ArduinoJson/Internals/JsonNode.h"
|
||||
#include "ArduinoJson/Internals/StringBuilder.h"
|
||||
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
||||
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();
|
||||
|
||||
if (!strcmp(childKey, key))
|
||||
{
|
||||
removeChild(*it);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
JsonArray JsonObject::createNestedArray(char const* key)
|
||||
{
|
||||
JsonNode* node = getOrCreateNodeAt(key);
|
||||
|
||||
if (node)
|
||||
node->setAsArray(_node->getContainerBuffer());
|
||||
|
||||
return JsonArray(node);
|
||||
}
|
||||
|
||||
JsonObject JsonObject::createNestedObject(char const* key)
|
||||
{
|
||||
JsonNode* node = getOrCreateNodeAt(key);
|
||||
|
||||
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();
|
||||
|
||||
if (!strcmp(childKey, key))
|
||||
return it->getAsObjectValue();
|
||||
}
|
||||
|
||||
JsonNode* newValueNode = createNode();
|
||||
if (!newValueNode) return 0;
|
||||
|
||||
JsonNode* newKeyNode = createNode();
|
||||
if (!newKeyNode) return 0;
|
||||
|
||||
newKeyNode->setAsObjectKeyValue(key, newValueNode);
|
||||
|
||||
addChild(newKeyNode);
|
||||
|
||||
return newValueNode;
|
||||
}
|
59
src/JsonValue.cpp
Normal file
59
src/JsonValue.cpp
Normal file
@ -0,0 +1,59 @@
|
||||
#include "ArduinoJson/JsonValue.h"
|
||||
|
||||
#include "ArduinoJson/JsonArray.h"
|
||||
#include "ArduinoJson/JsonObject.h"
|
||||
#include "ArduinoJson/Internals/JsonNode.h"
|
||||
|
||||
void JsonValue::operator=(bool value)
|
||||
{
|
||||
if (_node)
|
||||
_node->setAsBoolean(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::operator=(int value)
|
||||
{
|
||||
if (_node)
|
||||
_node->setAsLong(value);
|
||||
}
|
||||
|
||||
JsonValue::operator bool() const
|
||||
{
|
||||
return _node ? _node->getAsBoolean() : false;
|
||||
}
|
||||
|
||||
JsonValue::operator char const*() const
|
||||
{
|
||||
return _node ? _node->getAsString() : 0;
|
||||
}
|
||||
|
||||
JsonValue::operator double() const
|
||||
{
|
||||
return _node ? _node->getAsDouble() : 0;
|
||||
}
|
||||
|
||||
JsonValue::operator long() const
|
||||
{
|
||||
return _node ? _node->getAsInteger() : 0;
|
||||
}
|
||||
|
||||
JsonValue::operator JsonArray() const
|
||||
{
|
||||
return JsonArray(_node);
|
||||
}
|
||||
|
||||
JsonValue::operator JsonObject() const
|
||||
{
|
||||
return JsonObject(_node);
|
||||
}
|
Reference in New Issue
Block a user