mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-16 03:52:16 +02:00
Extracted class JsonHashTableBase to reduce the size of the code.
This commit is contained in:
@ -5,6 +5,7 @@
|
||||
|
||||
// This file is here to help the Arduino IDE find the .cpp files
|
||||
|
||||
#include "JsonGenerator/EscapedString.cpp"
|
||||
#include "JsonGenerator/JsonValue.cpp"
|
||||
#include "JsonGenerator/StringBuilder.cpp"
|
||||
#include "JsonGenerator/EscapedString.cpp"
|
||||
#include "JsonGenerator/JsonHashTableBase.cpp"
|
||||
#include "JsonGenerator/StringBuilder.cpp"
|
@ -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];
|
||||
};
|
||||
}
|
||||
}
|
26
JsonGenerator/JsonHashTableBase.cpp
Normal file
26
JsonGenerator/JsonHashTableBase.cpp
Normal file
@ -0,0 +1,26 @@
|
||||
#include "JsonHashTable.h"
|
||||
|
||||
using namespace ArduinoJson::Generator;
|
||||
|
||||
size_t JsonHashTableBase::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;
|
||||
}
|
62
JsonGenerator/JsonHashTableBase.h
Normal file
62
JsonGenerator/JsonHashTableBase.h
Normal file
@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Arduino JSON library
|
||||
* Benoit Blanchon 2014 - MIT License
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "JsonObjectBase.h"
|
||||
#include "EscapedString.h"
|
||||
|
||||
namespace ArduinoJson
|
||||
{
|
||||
namespace Generator
|
||||
{
|
||||
class JsonHashTableBase : public JsonObjectBase
|
||||
{
|
||||
public:
|
||||
|
||||
template<typename T>
|
||||
void add(const char* key, T value)
|
||||
{
|
||||
if (itemCount >= capacity) return;
|
||||
|
||||
items[itemCount].key.set(key);
|
||||
items[itemCount].value.set(value);
|
||||
itemCount++;
|
||||
}
|
||||
|
||||
template<int DIGITS>
|
||||
void add(const char* key, double value)
|
||||
{
|
||||
if (itemCount >= capacity) return;
|
||||
|
||||
items[itemCount].key.set(key);
|
||||
items[itemCount].value.set<DIGITS>(value);
|
||||
itemCount++;
|
||||
}
|
||||
|
||||
using JsonObjectBase::printTo;
|
||||
|
||||
virtual size_t printTo(Print& p) const;
|
||||
|
||||
protected:
|
||||
|
||||
struct KeyValuePair
|
||||
{
|
||||
Internals::EscapedString key;
|
||||
Internals::JsonValue value;
|
||||
};
|
||||
|
||||
JsonHashTableBase(KeyValuePair* items, int capacity)
|
||||
: items(items), capacity(capacity), itemCount(0)
|
||||
{
|
||||
}
|
||||
|
||||
private:
|
||||
KeyValuePair* items;
|
||||
int itemCount;
|
||||
int capacity;
|
||||
};
|
||||
}
|
||||
}
|
@ -164,4 +164,4 @@ This table is for an 8-bit Arduino, types would be bigger on a 32-bit processor.
|
||||
| Type | Size in bytes |
|
||||
| ---------------------- | ------------- |
|
||||
| JsonArray<N> | 4 + 6 x N |
|
||||
| JsonHashTable<N> | 4 + 8 x N |
|
||||
| JsonHashTable<N> | 8 + 8 x N |
|
||||
|
@ -85,6 +85,7 @@
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="..\JsonGenerator\EscapedString.cpp" />
|
||||
<ClCompile Include="..\JsonGenerator\JsonHashTableBase.cpp" />
|
||||
<ClCompile Include="..\JsonGenerator\JsonValue.cpp" />
|
||||
<ClCompile Include="..\JsonGenerator\StringBuilder.cpp" />
|
||||
<ClCompile Include="JsonArrayTests.cpp" />
|
||||
@ -97,6 +98,7 @@
|
||||
<ClInclude Include="..\JsonGenerator\EscapedString.h" />
|
||||
<ClInclude Include="..\JsonGenerator\JsonArray.h" />
|
||||
<ClInclude Include="..\JsonGenerator\JsonHashTable.h" />
|
||||
<ClInclude Include="..\JsonGenerator\JsonHashTableBase.h" />
|
||||
<ClInclude Include="..\JsonGenerator\JsonObjectBase.h" />
|
||||
<ClInclude Include="..\JsonGenerator\JsonValue.h" />
|
||||
<ClInclude Include="..\JsonGenerator\Print.h" />
|
||||
|
@ -39,6 +39,9 @@
|
||||
<ClCompile Include="..\JsonGenerator\EscapedString.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\JsonGenerator\JsonHashTableBase.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="..\JsonGenerator\JsonArray.h">
|
||||
@ -65,5 +68,8 @@
|
||||
<ClInclude Include="..\JsonGenerator\EscapedString.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\JsonGenerator\JsonHashTableBase.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
Reference in New Issue
Block a user