JsonHashTable.cpp 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * malloc-free JSON parser for Arduino
  3. * Benoit Blanchon 2014
  4. * MIT License
  5. */
  6. #include "JsonArray.h"
  7. #include "JsonHashTable.h"
  8. #include <string.h> // for strcmp()
  9. jsmntok_t* JsonHashTable::getToken(char* name)
  10. {
  11. // sanity check
  12. if (json == 0 || tokens == 0 || name == 0)
  13. return 0;
  14. // skip first token, it's the whole object
  15. int currentToken = 1;
  16. // scan each keys
  17. for (int i = 0; i < tokens[0].size / 2 ; i++)
  18. {
  19. // Get key token string
  20. char* key = json + tokens[currentToken].start;
  21. // Compare with desired name
  22. if (strcmp(name, key) == 0)
  23. {
  24. return &tokens[currentToken + 1];
  25. }
  26. // move forward: key + value + nested tokens
  27. currentToken += 2 + getNestedTokenCounts(currentToken + 1);
  28. }
  29. // nothing found, return NULL
  30. return 0;
  31. }
  32. JsonArray JsonHashTable::getArray(char* key)
  33. {
  34. jsmntok_t* token = getToken(key);
  35. return JsonArray(json, token);
  36. }
  37. char* JsonHashTable::getString(char* key)
  38. {
  39. jsmntok_t* token = getToken(key);
  40. return token != 0 ? json + token->start : 0;
  41. }