Return a JsonValue& instead of a KeyValuePair* (+40 bytes)

This commit is contained in:
Benoit Blanchon
2014-07-31 19:42:09 +02:00
parent 85ffb83aa6
commit 2c29327ebd
4 changed files with 19 additions and 15 deletions

View File

@ -6,6 +6,7 @@
#include "JsonObjectBase.h"
using namespace ArduinoJson::Generator;
using namespace ArduinoJson::Internals;
size_t JsonObjectBase::printTo(Print& p) const
{
@ -35,19 +36,19 @@ size_t JsonObjectBase::printTo(Print& p) const
return n;
}
JsonObjectBase::KeyValuePair* JsonObjectBase::getMatchingPair(char const* key)
JsonValue& JsonObjectBase::getValue(char const* key)
{
for (int i = 0; i < count; ++i)
{
if (items[i].key.equals(key))
{
return &items[i];
return items[i].value;
}
}
if (count >= capacity) return 0;
if (count >= capacity)
return JsonValue::null();
KeyValuePair* p = &items[count++];
p->key.set(key);
return p;
items[count].key.set(key);
return items[count++].value;
}

View File

@ -19,19 +19,13 @@ namespace ArduinoJson
template<typename T>
void add(const char* key, T value)
{
KeyValuePair* pair = getMatchingPair(key);
if (!pair) return;
pair->value.set(value);
getValue(key).set(value);
}
template<int DIGITS>
void add(const char* key, double value)
{
KeyValuePair* pair = getMatchingPair(key);
if (!pair) return;
pair->value.set<DIGITS>(value);
getValue(key).set<DIGITS>(value);
}
using JsonPrintable::printTo;
@ -51,7 +45,7 @@ namespace ArduinoJson
{
}
KeyValuePair* getMatchingPair(const char* key);
Internals::JsonValue& getValue(const char* key);
private:
KeyValuePair* items;

View File

@ -8,6 +8,8 @@
using namespace ArduinoJson::Internals;
JsonValue JsonValue::nullInstance;
size_t JsonValue::printBoolTo(const Content& c, Print& p)
{
return p.print(c.asBool ? "true" : "false");

View File

@ -65,6 +65,11 @@ namespace ArduinoJson
return printToImpl(content, p);
}
static JsonValue& null()
{
return nullInstance;
}
private:
union Content
{
@ -89,6 +94,8 @@ namespace ArduinoJson
{
return p.print(c.asDouble, DIGITS);
}
static JsonValue nullInstance;
};
}
}