JsonObjectBase.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Arduino JSON library
  3. * Benoit Blanchon 2014 - MIT License
  4. */
  5. #include "JsonObjectBase.h"
  6. #include <string.h> // for strcmp
  7. using namespace ArduinoJson::Generator;
  8. using namespace ArduinoJson::Internals;
  9. size_t JsonObjectBase::printTo(Print& p) const
  10. {
  11. size_t n = 0;
  12. n += p.write('{');
  13. // NB: the code has been optimized for a small size on a 8-bit AVR
  14. const KeyValuePair* current = items;
  15. for (int i = count; i > 0; i--)
  16. {
  17. n += EscapedString::printTo(current->key, p);
  18. n += p.write(':');
  19. n += current->value.printTo(p);
  20. current++;
  21. if (i > 1)
  22. {
  23. n += p.write(',');
  24. }
  25. }
  26. n += p.write('}');
  27. return n;
  28. }
  29. JsonValue& JsonObjectBase::operator[](char const* key)
  30. {
  31. for (int i = 0; i < count; ++i)
  32. {
  33. if (!strcmp(items[i].key, key))
  34. {
  35. return items[i].value;
  36. }
  37. }
  38. if (count >= capacity)
  39. return JsonValue::null();
  40. items[count].key = key;
  41. return items[count++].value;
  42. }