JsonValue.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Arduino JSON library
  3. * Benoit Blanchon 2014 - MIT License
  4. */
  5. #include <stdlib.h> // for strtol, strtod
  6. #include "JsonArray.h"
  7. #include "JsonObject.h"
  8. #include "JsonValue.h"
  9. using namespace ArduinoJson::Parser;
  10. using namespace ArduinoJson::Internal;
  11. JsonValue JsonValue::operator[](int index)
  12. {
  13. return JsonArray(json, token)[index];
  14. }
  15. JsonValue JsonValue::operator[](const char* key)
  16. {
  17. return JsonObject(json, token)[key];
  18. }
  19. JsonValue::operator bool()
  20. {
  21. if (!token.isPrimitive()) return 0;
  22. char *text = token.getText(json);
  23. // "true"
  24. if (text[0] == 't') return true;
  25. // "false"
  26. if (text[0] == 'f') return false;
  27. // "null"
  28. if (text[0] == 'n') return false;
  29. // number
  30. return strtol(text, 0, 0) != 0;
  31. }
  32. JsonValue::operator double()
  33. {
  34. return token.isPrimitive() ? strtod(token.getText(json), 0) : 0;
  35. }
  36. JsonValue::operator long()
  37. {
  38. return token.isPrimitive() ? strtol(token.getText(json), 0, 0) : 0;
  39. }
  40. JsonValue::operator char*()
  41. {
  42. return token.isString() || token.isPrimitive() ? token.getText(json) : 0;
  43. }
  44. JsonValue::operator JsonArray()
  45. {
  46. return token.isArray()
  47. ? JsonArray(json, token)
  48. : JsonArray::null();
  49. }
  50. JsonValue::operator JsonObject()
  51. {
  52. return token.isObject()
  53. ? JsonObject(json, token)
  54. : JsonObject::null();
  55. }