2014-07-08 21:29:19 +02:00
|
|
|
/*
|
|
|
|
* Arduino JSON library
|
|
|
|
* Benoit Blanchon 2014 - MIT License
|
|
|
|
*/
|
|
|
|
|
2014-07-22 21:02:16 +02:00
|
|
|
#include "JsonObjectBase.h"
|
2014-08-02 16:25:18 +02:00
|
|
|
#include <string.h> // for strcmp
|
2014-07-08 13:27:29 +02:00
|
|
|
|
|
|
|
using namespace ArduinoJson::Generator;
|
2014-07-31 19:42:09 +02:00
|
|
|
using namespace ArduinoJson::Internals;
|
2014-07-08 13:27:29 +02:00
|
|
|
|
2014-08-02 15:55:46 +02:00
|
|
|
JsonValue JsonObjectBase::nullValue;
|
|
|
|
|
2014-07-22 21:02:16 +02:00
|
|
|
size_t JsonObjectBase::printTo(Print& p) const
|
2014-07-08 13:27:29 +02:00
|
|
|
{
|
|
|
|
size_t n = 0;
|
|
|
|
|
|
|
|
n += p.write('{');
|
|
|
|
|
2014-07-08 21:29:19 +02:00
|
|
|
// NB: the code has been optimized for a small size on a 8-bit AVR
|
|
|
|
|
2014-07-09 13:40:09 +02:00
|
|
|
const KeyValuePair* current = items;
|
2014-07-08 13:49:35 +02:00
|
|
|
for (int i = count; i > 0; i--)
|
|
|
|
{
|
2014-08-01 15:32:05 +02:00
|
|
|
n += EscapedString::printTo(current->key, p);
|
2014-07-08 13:27:29 +02:00
|
|
|
n += p.write(':');
|
2014-07-08 13:46:34 +02:00
|
|
|
n += current->value.printTo(p);
|
|
|
|
|
|
|
|
current++;
|
2014-07-08 13:49:35 +02:00
|
|
|
|
|
|
|
if (i > 1)
|
|
|
|
{
|
|
|
|
n += p.write(',');
|
|
|
|
}
|
2014-07-08 13:27:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
n += p.write('}');
|
|
|
|
|
|
|
|
return n;
|
2014-07-31 18:50:01 +02:00
|
|
|
}
|
|
|
|
|
2014-08-03 13:09:07 +02:00
|
|
|
JsonObjectBase::KeyValuePair* JsonObjectBase::getMatchingPair(JsonKey key) const
|
2014-07-31 18:50:01 +02:00
|
|
|
{
|
2014-08-02 15:37:01 +02:00
|
|
|
KeyValuePair* p = items;
|
|
|
|
|
|
|
|
for (int i = count; i > 0; --i)
|
2014-07-31 18:50:01 +02:00
|
|
|
{
|
2014-08-02 16:25:18 +02:00
|
|
|
if (!strcmp(p->key, key))
|
|
|
|
return p;
|
2014-08-02 15:37:01 +02:00
|
|
|
|
|
|
|
p++;
|
2014-07-31 18:50:01 +02:00
|
|
|
}
|
|
|
|
|
2014-08-02 16:25:18 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-03 13:09:07 +02:00
|
|
|
JsonValue& JsonObjectBase::operator[](JsonKey key)
|
2014-08-02 16:25:18 +02:00
|
|
|
{
|
|
|
|
KeyValuePair* match = getMatchingPair(key);
|
2014-08-02 15:55:46 +02:00
|
|
|
|
2014-08-02 16:25:18 +02:00
|
|
|
if (match)
|
|
|
|
return match->value;
|
|
|
|
|
2014-08-03 13:09:07 +02:00
|
|
|
JsonValue* value;
|
|
|
|
|
2014-08-02 15:55:46 +02:00
|
|
|
if (count < capacity)
|
|
|
|
{
|
2014-08-02 16:25:18 +02:00
|
|
|
items[count].key = key;
|
|
|
|
value = &items[count].value;
|
2014-08-02 15:55:46 +02:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
value = &nullValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
value->reset();
|
|
|
|
return *value;
|
2014-08-02 16:11:02 +02:00
|
|
|
}
|
|
|
|
|
2014-08-03 13:09:07 +02:00
|
|
|
bool JsonObjectBase::containsKey(JsonKey key) const
|
2014-08-02 16:11:02 +02:00
|
|
|
{
|
2014-08-02 16:25:18 +02:00
|
|
|
return getMatchingPair(key) != 0;
|
2014-08-03 13:16:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void JsonObjectBase::remove(JsonKey key)
|
|
|
|
{
|
|
|
|
KeyValuePair* match = getMatchingPair(key);
|
|
|
|
if (match == 0) return;
|
|
|
|
|
|
|
|
*match = items[--count];
|
2014-07-08 13:27:29 +02:00
|
|
|
}
|