Moved files into Internals/ and Arduino/

This commit is contained in:
Benoit Blanchon
2014-10-01 16:08:32 +02:00
parent 1a98fd5dfc
commit d66a7adc22
19 changed files with 43 additions and 36 deletions

View File

@ -0,0 +1,45 @@
/*
* Arduino JSON library
* Benoit Blanchon 2014 - MIT License
*/
#include "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('\"');
}

View File

@ -0,0 +1,20 @@
/*
* Arduino JSON library
* Benoit Blanchon 2014 - MIT License
*/
#pragma once
#include "../Arduino/Print.h"
namespace ArduinoJson
{
namespace Internals
{
class EscapedString
{
public:
static size_t printTo(const char*, Print&);
};
}
}

52
srcs/Internals/JsonNode.h Normal file
View File

@ -0,0 +1,52 @@
#pragma once
class JsonBuffer;
enum JsonNodeType
{
JSON_UNDEFINED,
JSON_PROXY,
JSON_NULL,
JSON_ARRAY,
JSON_OBJECT,
JSON_KEY,
JSON_BOOLEAN,
JSON_STRING,
JSON_INTEGER,
JSON_DOUBLE_0_DECIMALS,
JSON_DOUBLE_1_DECIMAL,
JSON_DOUBLE_2_DECIMALS,
// etc.
};
struct JsonNode
{
JsonNode* next;
JsonNodeType type;
union
{
bool asBoolean;
double asDouble;
long asInteger;
const char* asString;
struct
{
const char* key;
JsonNode* value;
} asKey;
struct
{
JsonNode* child;
JsonBuffer* buffer;
} asObject;
struct
{
JsonNode* target;
} asProxy;
} content;
};

View File

@ -0,0 +1,70 @@
#include "JsonNodeSerializer.h"
#include "EscapedString.h"
#include "JsonNode.h"
using namespace ArduinoJson::Internals;
size_t JsonNodeSerializer::serialize(const JsonNode* node)
{
if (!node) return 0;
switch (node->type)
{
case JSON_OBJECT:
return serializeObject(node);
case JSON_STRING:
return EscapedString::printTo(node->content.asString, _sink);
case JSON_INTEGER:
return _sink.print(node->content.asInteger);
case JSON_BOOLEAN:
return _sink.print(node->content.asBoolean ? "true" : "false");
case JSON_PROXY:
return serialize(node->content.asProxy.target);
}
if (node->type >= JSON_DOUBLE_0_DECIMALS)
{
return _sink.print(node->content.asDouble, node->type - JSON_DOUBLE_0_DECIMALS);
}
return 0;
}
size_t JsonNodeSerializer::serializeObject(const JsonNode* node)
{
size_t n = 0;
n += _sink.write('{');
JsonNode* firstChild = node->content.asObject.child;
for (JsonNode* child = firstChild; child; child = child->next)
{
n += serializeKeyValue(child);
if (child->next)
{
n += _sink.write(',');
}
}
n += _sink.write('}');
return n;
}
size_t JsonNodeSerializer::serializeKeyValue(JsonNode const* node)
{
const char* childKey = node->content.asKey.key;
JsonNode* childValue = node->content.asKey.value;
return
EscapedString::printTo(childKey, _sink) +
_sink.write(':') +
serialize(childValue);
}

View File

@ -0,0 +1,22 @@
#pragma once
class Print;
struct JsonNode;
class JsonNodeSerializer
{
public:
explicit JsonNodeSerializer(Print& sink)
: _sink(sink)
{
}
size_t serialize(const JsonNode* node);
private:
Print& _sink;
size_t serializeObject(const JsonNode* node);
size_t serializeKeyValue(const JsonNode* node);
};

View File

@ -0,0 +1,17 @@
/*
* Arduino JSON library
* Benoit Blanchon 2014 - MIT License
*/
#include "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;
}

View File

@ -0,0 +1,31 @@
/*
* Arduino JSON library
* Benoit Blanchon 2014 - MIT License
*/
#pragma once
#include "../Arduino/Print.h"
namespace ArduinoJson
{
namespace Internals
{
class StringBuilder : public Print
{
public:
StringBuilder(char* buf, int size)
: buffer(buf), capacity(size - 1), length(0)
{
buffer[0] = 0;
}
virtual size_t write(uint8_t c);
private:
char* buffer;
int capacity;
int length;
};
}
}