Files
ArduinoJson/JsonGenerator/JsonObjectBase.cpp

55 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:22:30 +02:00
n += EscapedString(current->key).printTo(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
{
for (int i = 0; i < count; ++i)
{
2014-08-01 15:22:30 +02:00
if (!strcmp(items[i].key, key))
2014-07-31 18:50:01 +02:00
{
return items[i].value;
2014-07-31 18:50:01 +02:00
}
}
if (count >= capacity)
return JsonValue::null();
2014-07-31 18:50:01 +02:00
2014-08-01 15:22:30 +02:00
items[count].key = key;
return items[count++].value;
}