JsonObjectBase.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. KeyValuePair* p = items;
  32. for (int i = count; i > 0; --i)
  33. {
  34. bool keyMatches = strcmp(p->key, key) == 0;
  35. if (keyMatches)
  36. return p->value;
  37. p++;
  38. }
  39. if (count >= capacity)
  40. return JsonValue::null();
  41. count++;
  42. p->key = key;
  43. return p->value;
  44. }