JsonHashTable.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. /*
  11. * Returns the token for the value associated with the specified key
  12. */
  13. JsonValue JsonHashTable::operator [](const char* desiredKey)
  14. {
  15. // sanity check
  16. if (!success() || desiredKey == 0)
  17. return JsonValue();
  18. // skip first token, it's the whole object
  19. jsmntok_t* currentToken = tokens + 1;
  20. // scan each keys
  21. for (int i = 0; i < tokens[0].size / 2 ; i++)
  22. {
  23. // get key token string
  24. char* key = JsonValue(json, currentToken);
  25. // compare with desired name
  26. if (strcmp(desiredKey, key) == 0)
  27. {
  28. // return the value token that follows the key token
  29. return JsonValue(json, currentToken + 1);
  30. }
  31. // move forward: key + value + nested tokens
  32. currentToken += 2 + getNestedTokenCount(currentToken + 1);
  33. }
  34. // nothing found, return NULL
  35. return JsonValue();
  36. }
  37. JsonArray JsonHashTable::getArray(const char* key) DEPRECATED
  38. {
  39. return (JsonArray) (*this)[key];
  40. }