JsonValue.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Arduino JSON library
  3. * Benoit Blanchon 2014 - MIT License
  4. */
  5. #include <stdlib.h> // for strtol, strtod
  6. #include <string.h> // for strcmp()
  7. #include "JsonArray.h"
  8. #include "JsonHashTable.h"
  9. #include "JsonValue.h"
  10. using namespace ArduinoJson::Parser;
  11. JsonValue::operator bool()
  12. {
  13. if (tokens == 0 || tokens->type != JSMN_PRIMITIVE) return 0;
  14. // "true"
  15. if (json[tokens->start] == 't') return true;
  16. // "false"
  17. if (json[tokens->start] == 'f') return false;
  18. // "null"
  19. if (json[tokens->start] == 'n') return false;
  20. // number
  21. return strtol(json + tokens->start, 0, 0) != 0;
  22. }
  23. JsonValue::operator double()
  24. {
  25. if (tokens == 0 || tokens->type != JSMN_PRIMITIVE) return 0;
  26. return strtod(json + tokens->start, 0);
  27. }
  28. JsonValue::operator long()
  29. {
  30. if (tokens == 0 || tokens->type != JSMN_PRIMITIVE) return 0;
  31. return strtol(json + tokens->start, 0, 0);
  32. }
  33. JsonValue::operator char*()
  34. {
  35. if (tokens == 0 || tokens->type != JSMN_PRIMITIVE && tokens->type != JSMN_STRING)
  36. return 0;
  37. // add null terminator to the string
  38. json[tokens->end] = 0;
  39. return json + tokens->start;
  40. }
  41. JsonValue::operator JsonArray()
  42. {
  43. return tokens != 0 && tokens->type == JSMN_ARRAY
  44. ? JsonArray(*this)
  45. : JsonArray();
  46. }
  47. JsonValue::operator JsonHashTable()
  48. {
  49. return tokens != 0 && tokens->type == JSMN_OBJECT
  50. ? JsonHashTable(*this)
  51. : JsonHashTable();
  52. }
  53. int JsonValue::size()
  54. {
  55. return tokens != 0 && tokens->type == JSMN_ARRAY ? tokens->size : 0;
  56. }
  57. /*
  58. * Returns the token for the value associated with the specified key
  59. */
  60. JsonValue JsonValue::operator [](const char* desiredKey)
  61. {
  62. // sanity check
  63. if (json == 0 || desiredKey == 0 || tokens->type != JSMN_OBJECT)
  64. return JsonValue();
  65. // skip first token, it's the whole object
  66. jsmntok_t* currentToken = tokens + 1;
  67. // scan each keys
  68. for (int i = 0; i < tokens[0].size / 2; i++)
  69. {
  70. // get key token string
  71. char* key = JsonValue(json, currentToken);
  72. // compare with desired name
  73. if (strcmp(desiredKey, key) == 0)
  74. {
  75. // return the value token that follows the key token
  76. return JsonValue(json, currentToken + 1);
  77. }
  78. // move forward: key + value + nested tokens
  79. currentToken += 2 + getNestedTokenCount(currentToken + 1);
  80. }
  81. // nothing found, return NULL
  82. return JsonValue();
  83. }
  84. /*
  85. * Returns the token for the value at the specified index
  86. */
  87. JsonValue JsonValue::operator[](int index)
  88. {
  89. // sanity check
  90. if (index < 0 || index >= size())
  91. return JsonValue();
  92. // skip first token, it's the whole object
  93. jsmntok_t* currentToken = tokens + 1;
  94. // skip all tokens before the specified index
  95. for (int i = 0; i < index; i++)
  96. {
  97. // move forward: current + nested tokens
  98. currentToken += 1 + getNestedTokenCount(currentToken);
  99. }
  100. return JsonValue(json, currentToken);
  101. }
  102. int JsonValue::getNestedTokenCount(jsmntok_t* token)
  103. {
  104. int tokensToVisit = token->size;
  105. int count = 0;
  106. while (tokensToVisit)
  107. {
  108. count++;
  109. token++;
  110. tokensToVisit--;
  111. tokensToVisit += token->size;
  112. }
  113. return count;
  114. }