Files
ArduinoJson/JsonGeneratorTests/JsonHashTable.h

63 lines
1.1 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 "JsonObjectBase.h"
template<int N>
class JsonHashTable : public JsonObjectBase
{
public:
JsonHashTable()
{
itemCount = 0;
}
void add(const char* key, const char* value)
{
2014-06-27 13:43:26 +02:00
add(key, JsonValue(value));
}
2014-06-27 13:43:26 +02:00
void add(const char* key, JsonValue value)
2014-06-27 13:42:26 +02:00
{
if (itemCount >= N) return;
items[itemCount].key = key;
items[itemCount].value = value;
itemCount++;
}
2014-06-27 13:00:27 +02:00
2014-06-27 13:42:26 +02:00
using JsonObjectBase::writeTo;
2014-06-27 13:00:27 +02:00
private:
struct KeyValuePair
2014-06-27 13:00:27 +02:00
{
const char* key;
2014-06-27 13:18:38 +02:00
JsonValue value;
};
KeyValuePair items[N];
int itemCount;
2014-06-27 13:00:27 +02:00
virtual void writeTo(StringBuilder& sb)
{
sb.append("{");
for (int i = 0; i < itemCount; i++)
2014-06-27 13:00:27 +02:00
{
if (i>0) sb.append(",");
sb.appendEscaped(items[i].key);
sb.append(":");
2014-06-27 13:42:26 +02:00
items[i].value.writeTo(sb);
}
2014-06-27 13:00:27 +02:00
sb.append("}");
}
};