JsonObjectBase.cpp 1.0 KB

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