JsonValue.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 "JsonObject.h"
  9. #include "JsonValue.h"
  10. using namespace ArduinoJson::Parser;
  11. // Convert the JsonValue to a bool.
  12. // Returns false if the JsonValue is not a primitve.
  13. JsonValue::operator bool()
  14. {
  15. if (!isPrimitive()) return 0;
  16. char *text = getText();
  17. // "true"
  18. if (text[0] == 't') return true;
  19. // "false"
  20. if (text[0] == 'f') return false;
  21. // "null"
  22. if (text[0] == 'n') return false;
  23. // number
  24. return strtol(text, 0, 0) != 0;
  25. }
  26. // Convert the JsonValue to a floating point value.
  27. // Returns false if the JsonValue is not a number.
  28. JsonValue::operator double()
  29. {
  30. return isPrimitive() ? strtod(getText(), 0) : 0;
  31. }
  32. // Convert the JsonValue to a floating point value.
  33. // Returns false if the JsonValue is not a number.
  34. JsonValue::operator long()
  35. {
  36. return isPrimitive() ? strtol(getText(), 0, 0) : 0;
  37. }
  38. // Convert the JsonValue to a string.
  39. // Returns 0 if the JsonValue is not a string.
  40. JsonValue::operator char*()
  41. {
  42. return isString() || isPrimitive() ? getText() : 0;
  43. }
  44. // Get the nested value at the specified index.
  45. // Returns an invalid JsonValue if the current value is not an array.
  46. JsonValue JsonValue::operator[](int index)
  47. {
  48. // sanity check
  49. if (index < 0 || !isArray() || index >= childrenCount())
  50. return null();
  51. // skip first token, it's the whole object
  52. JsonToken runningToken = firstChild();
  53. // skip all tokens before the specified index
  54. for (int i = 0; i < index; i++)
  55. {
  56. // move forward: current + nested tokens
  57. runningToken = runningToken.nextSibling();
  58. }
  59. return runningToken;
  60. }
  61. // Get the nested value matching the specified index.
  62. // Returns an invalid JsonValue if the current value is not an object.
  63. JsonValue JsonValue::operator[](const char* desiredKey)
  64. {
  65. // sanity check
  66. if (desiredKey == 0 || !isObject())
  67. return null();
  68. // skip first token, it's the whole object
  69. JsonToken runningToken = firstChild();
  70. // scan each keys
  71. for (int i = 0; i < childrenCount() / 2; i++)
  72. {
  73. // get 'key' token string
  74. char* key = runningToken.getText();
  75. // move to the 'value' token
  76. runningToken = runningToken.nextSibling();
  77. // compare with desired name
  78. if (strcmp(desiredKey, key) == 0)
  79. {
  80. // return the value token that follows the key token
  81. return runningToken;
  82. }
  83. // skip nested tokens
  84. runningToken = runningToken.nextSibling();
  85. }
  86. // nothing found, return NULL
  87. return null();
  88. }