Files
ArduinoJson/JsonGenerator/JsonHashTable.h

82 lines
1.8 KiB
C
Raw Normal View History

2014-06-27 13:00:27 +02:00
/*
* Arduino JSON library
* Benoit Blanchon 2014 - MIT License
*/
#pragma once
#include "EscapedString.h"
2014-06-27 13:00:27 +02:00
#include "JsonObjectBase.h"
2014-07-03 13:54:27 +02:00
namespace ArduinoJson
2014-06-27 13:00:27 +02:00
{
2014-07-03 13:54:27 +02:00
namespace Generator
{
2014-07-03 13:54:27 +02:00
template<int N>
class JsonHashTable : public JsonObjectBase
{
public:
2014-07-03 13:54:27 +02:00
JsonHashTable()
{
itemCount = 0;
}
2014-07-03 13:54:27 +02:00
template<typename T>
void add(const char* key, T value)
{
if (itemCount >= N) return;
2014-06-27 13:42:26 +02:00
items[itemCount].key.set(key);
items[itemCount].value.set(value);
itemCount++;
2014-07-03 13:54:27 +02:00
}
2014-06-27 13:00:27 +02:00
template<int DIGITS>
void add(const char* key, double value)
2014-07-03 13:54:27 +02:00
{
if (itemCount >= N) return;
2014-06-27 13:00:27 +02:00
items[itemCount].key.set(key);
items[itemCount].value.set<DIGITS>(value);
2014-07-03 13:54:27 +02:00
itemCount++;
}
2014-07-03 13:54:27 +02:00
using JsonObjectBase::printTo;
2014-06-27 13:00:27 +02:00
2014-07-03 13:54:27 +02:00
private:
2014-07-03 13:54:27 +02:00
struct KeyValuePair
{
EscapedString key;
2014-07-03 13:54:27 +02:00
JsonValue value;
};
2014-06-27 13:00:27 +02:00
2014-07-03 13:54:27 +02:00
KeyValuePair items[N];
int itemCount;
2014-06-30 19:19:39 +02:00
2014-07-03 13:54:27 +02:00
virtual size_t printTo(Print& p) const
{
2014-07-03 13:54:27 +02:00
size_t n = 0;
2014-07-03 13:54:27 +02:00
n += p.write('{');
2014-06-27 13:00:27 +02:00
2014-07-03 13:54:27 +02:00
for (int i = 0; i < itemCount; i++)
{
if (i > 0)
{
n += p.write(',');
}
n += items[i].key.printTo(p);
2014-07-03 13:54:27 +02:00
n += p.write(':');
n += items[i].value.printTo(p);
}
2014-06-27 13:00:27 +02:00
2014-07-03 13:54:27 +02:00
n += p.write('}');
return n;
}
};
}
}