Extracted class JsonHashTableBase to reduce the size of the code.

This commit is contained in:
Benoît Blanchon
2014-07-08 13:27:29 +02:00
parent 6d3b70f4a3
commit bbc18b5ca4
7 changed files with 105 additions and 63 deletions

View File

@ -5,78 +5,23 @@
#pragma once
#include "EscapedString.h"
#include "JsonObjectBase.h"
#include "JsonHashTableBase.h"
namespace ArduinoJson
{
namespace Generator
{
template<int N>
class JsonHashTable : public JsonObjectBase
class JsonHashTable : public JsonHashTableBase
{
public:
JsonHashTable()
: JsonHashTableBase(items, N)
{
itemCount = 0;
}
template<typename T>
void add(const char* key, T value)
{
if (itemCount >= N) return;
items[itemCount].key.set(key);
items[itemCount].value.set(value);
itemCount++;
}
template<int DIGITS>
void add(const char* key, double value)
{
if (itemCount >= N) return;
items[itemCount].key.set(key);
items[itemCount].value.set<DIGITS>(value);
itemCount++;
}
using JsonObjectBase::printTo;
}
private:
struct KeyValuePair
{
Internals::EscapedString key;
Internals::JsonValue value;
};
KeyValuePair items[N];
int itemCount;
virtual size_t printTo(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].key.printTo(p);
n += p.write(':');
n += items[i].value.printTo(p);
}
n += p.write('}');
return n;
}
KeyValuePair items[N];
};
}
}