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

View File

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

View File

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

View File

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