JsonObject.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include "JsonObject.h"
  2. #include <string.h> // for strcmp
  3. #include "JsonBuffer.h"
  4. #include "JsonValue.h"
  5. #include "Internals/EscapedString.h"
  6. #include "Internals/JsonNode.h"
  7. #include "Internals/JsonNodeSerializer.h"
  8. #include "Internals/StringBuilder.h"
  9. using namespace ArduinoJson::Internals;
  10. JsonValue JsonObject::operator[](char const* key)
  11. {
  12. JsonNode* node = getOrCreateNodeAt(key);
  13. return JsonValue(node);
  14. }
  15. void JsonObject::remove(char const* key)
  16. {
  17. JsonNode* lastChild = 0;
  18. for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it)
  19. {
  20. const char* childKey = it->content.asKey.key;
  21. if (!strcmp(childKey, key))
  22. {
  23. removeChildAfter(*it, lastChild);
  24. }
  25. lastChild = *it;
  26. }
  27. }
  28. JsonNode* JsonObject::getOrCreateNodeAt(const char* key)
  29. {
  30. if (!checkNodeType(JSON_OBJECT)) return 0;
  31. for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it)
  32. {
  33. const char* childKey = it->content.asKey.key;
  34. if (!strcmp(childKey, key))
  35. return it->content.asKey.value;
  36. }
  37. JsonNode* newValueNode = createNode(JSON_UNDEFINED);
  38. if (!newValueNode) return 0;
  39. JsonNode* newKeyNode = createNode(JSON_KEY);
  40. if (!newKeyNode) return 0;
  41. newKeyNode->content.asKey.key = key;
  42. newKeyNode->content.asKey.value = newValueNode;
  43. addChild(newKeyNode);
  44. return newValueNode;
  45. }