Moved implementation is a new folder

This commit is contained in:
Benoît Blanchon
2014-07-01 13:50:54 +02:00
parent 4d5a7114c1
commit a9b8e280fe
11 changed files with 24 additions and 22 deletions

61
JsonGenerator/JsonArray.h Normal file
View File

@ -0,0 +1,61 @@
/*
* Arduino JSON library
* Benoit Blanchon 2014 - MIT License
*/
#pragma once
#include "JsonObjectBase.h"
#include "StringBuilder.h"
template<int N>
class JsonArray : public JsonObjectBase
{
public:
JsonArray()
{
itemCount = 0;
}
template<typename T>
void add(T value)
{
add(JsonValue(value));
}
void add(JsonValue value)
{
if (itemCount >= N) return;
items[itemCount] = value;
itemCount++;
}
using JsonObjectBase::writeTo;
private:
JsonValue items[N];
int itemCount;
virtual size_t writeTo(Print& p) const
{
size_t n = 0;
n += p.write("[");
for (int i = 0; i < itemCount; i++)
{
if (i > 0)
{
n += p.write(",");
}
n += items[i].writeTo(p);
}
n += p.write("]");
return n;
}
};

View File

@ -0,0 +1,73 @@
/*
* Arduino JSON library
* Benoit Blanchon 2014 - MIT License
*/
#pragma once
#include "JsonObjectBase.h"
template<int N>
class JsonHashTable : public JsonObjectBase
{
public:
JsonHashTable()
{
itemCount = 0;
}
template<typename T>
void add(const char* key, T value)
{
add(key, JsonValue(value));
}
void add(const char* key, JsonValue value)
{
if (itemCount >= N) return;
items[itemCount].key = key;
items[itemCount].value = value;
itemCount++;
}
using JsonObjectBase::writeTo;
private:
struct KeyValuePair
{
const char* key;
JsonValue value;
};
KeyValuePair items[N];
int itemCount;
virtual size_t writeTo(Print& p) const
{
size_t n = 0;
n += p.write('{');
for (int i = 0; i < itemCount; i++)
{
JsonValue key(items[i].key);
if (i > 0)
{
n += p.write(',');
}
n += key.writeTo(p);
n += p.write(':');
n += items[i].value.writeTo(p);
}
n += p.write('}');
return n;
}
};

View File

@ -0,0 +1,24 @@
/*
* Arduino JSON library
* Benoit Blanchon 2014 - MIT License
*/
#pragma once
#include "JsonValue.h"
#include "Print.h"
#include "Printable.h"
class JsonObjectBase : public Printable
{
public:
size_t writeTo(char* buffer, size_t bufferSize)
{
StringBuilder sb(buffer, bufferSize);
return writeTo(sb);
}
virtual size_t writeTo(Print& p) const = 0;
};

View File

@ -0,0 +1,89 @@
/*
* Arduino JSON library
* Benoit Blanchon 2014 - MIT License
*/
#include "JsonValue.h"
#include "JsonObjectBase.h"
#include <cstdio>
#include <cstring>
size_t JsonValue::writeBooleanTo(Print& p) const
{
return p.write(content.boolean ? "true" : "false");
}
size_t JsonValue::writeNumberTo(Print& p) const
{
char tmp[16];
_snprintf(tmp, sizeof(tmp), "%lg", content.number);
return p.write(tmp);
}
size_t JsonValue::writeObjectTo(Print& p) const
{
if (content.object)
return ((JsonObjectBase*)content.object)->writeTo(p);
else
return p.write("null");
}
size_t JsonValue::writeStringTo(Print& p) const
{
auto s = content.string;
if (!s)
{
return p.write("null");
}
size_t n = 0;
n += p.write('\"');
while (*s)
{
switch (*s)
{
case '"':
n += p.write("\\\"");
break;
case '\\':
n += p.write("\\\\");
break;
case '\b':
n += p.write("\\b");
break;
case '\f':
n += p.write("\\f");
break;
case '\n':
n += p.write("\\n");
break;
case '\r':
n += p.write("\\r");
break;
case '\t':
n += p.write("\\t");
break;
default:
n += p.write(*s);
break;
}
s++;
}
n += p.write('\"');
return n;
}

67
JsonGenerator/JsonValue.h Normal file
View File

@ -0,0 +1,67 @@
/*
* Arduino JSON library
* Benoit Blanchon 2014 - MIT License
*/
#pragma once
#include "Printable.h"
#include "StringBuilder.h"
class JsonValue : public Printable
{
public:
JsonValue()
{
}
JsonValue(const char* value)
: implementation(&JsonValue::writeStringTo)
{
content.string = value;
}
JsonValue(double value)
: implementation(&JsonValue::writeNumberTo)
{
content.number = value;
}
JsonValue(bool value)
: implementation(&JsonValue::writeBooleanTo)
{
content.boolean = value;
}
JsonValue(Printable& value)
: implementation(&JsonValue::writeObjectTo)
{
content.object = &value;
}
virtual size_t writeTo(Print& p) const
{
// handmade polymorphism
return (this->*implementation)(p);
}
private:
union Content
{
bool boolean;
double number;
Printable* object;
const char* string;
};
Content content;
size_t(JsonValue::*implementation)(Print& p)const;
size_t writeBooleanTo(Print& p) const;
size_t writeNumberTo(Print& p) const;
size_t writeObjectTo(Print& p) const;
size_t writeStringTo(Print& p) const;
};

24
JsonGenerator/Print.h Normal file
View File

@ -0,0 +1,24 @@
/*
* Arduino JSON library
* Benoit Blanchon 2014 - MIT License
*/
#pragma once
class Print
{
public:
virtual size_t write(char c) = 0;
size_t write(const char* s)
{
size_t n = 0;
while (*s)
{
n += write(*s++);
}
return n;
}
};

16
JsonGenerator/Printable.h Normal file
View File

@ -0,0 +1,16 @@
/*
* Arduino JSON library
* Benoit Blanchon 2014 - MIT License
*/
#pragma once
class Print;
class Printable
{
public:
virtual size_t writeTo(Print& p) const = 0;
};

View File

@ -0,0 +1,15 @@
/*
* Arduino JSON library
* Benoit Blanchon 2014 - MIT License
*/
#include "StringBuilder.h"
size_t StringBuilder::write(char c)
{
if (length >= capacity) return 0;
buffer[length++] = c;
buffer[length] = 0;
return 1;
}

View File

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