JsonObjectBase.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. JsonValue JsonObjectBase::nullValue;
  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. if (p->matches(key))
  35. return p->value;
  36. p++;
  37. }
  38. JsonValue* value;
  39. if (count < capacity)
  40. {
  41. count++;
  42. p->key = key;
  43. value = &p->value;
  44. }
  45. else
  46. {
  47. value = &nullValue;
  48. }
  49. value->reset();
  50. return *value;
  51. }
  52. bool JsonObjectBase::containsKey(char const* key) const
  53. {
  54. KeyValuePair* p = items;
  55. for (int i = count; i > 0; --i)
  56. {
  57. if (p->matches(key))
  58. return true;
  59. p++;
  60. }
  61. return false;
  62. }