JsonArray.cpp 999 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. * Arduino JSON library
  3. * Benoit Blanchon 2014 - MIT License
  4. */
  5. #include "JsonArray.h"
  6. #include "JsonHashTable.h"
  7. using namespace ArduinoJson::Parser;
  8. JsonArray::JsonArray(char* json, jsmntok_t* tokens)
  9. : JsonObjectBase(json, tokens)
  10. {
  11. if (tokens == 0 || tokens[0].type != JSMN_ARRAY)
  12. makeInvalid();
  13. }
  14. /*
  15. * Returns the token for the value at the specified index
  16. */
  17. JsonValue JsonArray::operator[](int index)
  18. {
  19. // sanity check
  20. if (json == 0 || tokens == 0 || index < 0 || index >= tokens[0].size)
  21. return JsonValue();
  22. // skip first token, it's the whole object
  23. jsmntok_t* currentToken = tokens + 1;
  24. // skip all tokens before the specified index
  25. for (int i = 0; i < index; i++)
  26. {
  27. // move forward: current + nested tokens
  28. currentToken += 1 + getNestedTokenCount(currentToken);
  29. }
  30. return JsonValue(json, currentToken);
  31. }
  32. JsonHashTable JsonArray::getHashTable(int index) DEPRECATED
  33. {
  34. return (JsonHashTable) (*this)[index];
  35. }