JsonHashTable.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Arduino JSON library
  3. * Benoit Blanchon 2014 - MIT License
  4. */
  5. #include <string.h> // for strcmp()
  6. #include "JsonHashTable.h"
  7. #include "JsonArray.h"
  8. #include "JsonValue.h"
  9. using namespace ArduinoJson::Parser;
  10. JsonHashTable::JsonHashTable(char* json, jsmntok_t* tokens)
  11. : JsonObjectBase(json, tokens)
  12. {
  13. if (tokens == 0 || tokens[0].type != JSMN_OBJECT)
  14. makeInvalid();
  15. }
  16. /*
  17. * Returns the token for the value associated with the specified key
  18. */
  19. JsonValue JsonHashTable::operator [](const char* desiredKey)
  20. {
  21. // sanity check
  22. if (json == 0 || tokens == 0 || desiredKey == 0)
  23. return JsonValue();
  24. // skip first token, it's the whole object
  25. jsmntok_t* currentToken = tokens + 1;
  26. // scan each keys
  27. for (int i = 0; i < tokens[0].size / 2 ; i++)
  28. {
  29. // get key token string
  30. char* key = JsonValue(json, currentToken);
  31. // compare with desired name
  32. if (strcmp(desiredKey, key) == 0)
  33. {
  34. // return the value token that follows the key token
  35. return JsonValue(json, currentToken + 1);
  36. }
  37. // move forward: key + value + nested tokens
  38. currentToken += 2 + getNestedTokenCount(currentToken + 1);
  39. }
  40. // nothing found, return NULL
  41. return JsonValue();
  42. }
  43. JsonArray JsonHashTable::getArray(const char* key) DEPRECATED
  44. {
  45. return (JsonArray) (*this)[key];
  46. }