forked from bblanchon/ArduinoJson
Renamed srcs/ into src/
This commit is contained in:
@@ -0,0 +1 @@
|
||||
#include "ArduinoJson/Internals/CompactJsonWriter.h"
|
||||
@@ -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('\"');
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
#include "ArduinoJson/Internals/PrettyJsonWriter.h"
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user