Files
ArduinoJson/JsonGenerator/JsonObjectBase.cpp

60 lines
1.1 KiB
C++
Raw Normal View History

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-01 15:22:30 +02:00
#include <string.h> // for strcmp
using namespace ArduinoJson::Generator;
using namespace ArduinoJson::Internals;
2014-07-22 21:02:16 +02:00
size_t JsonObjectBase::printTo(Print& p) const
{
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
const KeyValuePair* current = items;
for (int i = count; i > 0; i--)
{
2014-08-01 15:32:05 +02:00
n += EscapedString::printTo(current->key, p);
n += p.write(':');
n += current->value.printTo(p);
current++;
if (i > 1)
{
n += p.write(',');
}
}
n += p.write('}');
return n;
2014-07-31 18:50:01 +02:00
}
2014-07-31 20:11:55 +02:00
JsonValue& JsonObjectBase::operator[](char const* key)
2014-07-31 18:50:01 +02:00
{
KeyValuePair* p = items;
for (int i = count; i > 0; --i)
2014-07-31 18:50:01 +02:00
{
bool keyMatches = strcmp(p->key, key) == 0;
if (keyMatches)
return p->value;
p++;
2014-07-31 18:50:01 +02:00
}
if (count >= capacity)
return JsonValue::null();
2014-07-31 18:50:01 +02:00
count++;
p->key = key;
return p->value;
}