JsonValue.cpp 1005 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. JsonValue JsonValue::operator[](int index)
  11. {
  12. return JsonArray(*this)[index];
  13. }
  14. JsonValue JsonValue::operator[](const char* key)
  15. {
  16. return JsonObject(*this)[key];
  17. }
  18. JsonValue::operator bool()
  19. {
  20. if (!isPrimitive()) return 0;
  21. char *text = getText();
  22. // "true"
  23. if (text[0] == 't') return true;
  24. // "false"
  25. if (text[0] == 'f') return false;
  26. // "null"
  27. if (text[0] == 'n') return false;
  28. // number
  29. return strtol(text, 0, 0) != 0;
  30. }
  31. JsonValue::operator double()
  32. {
  33. return isPrimitive() ? strtod(getText(), 0) : 0;
  34. }
  35. JsonValue::operator long()
  36. {
  37. return isPrimitive() ? strtol(getText(), 0, 0) : 0;
  38. }
  39. JsonValue::operator char*()
  40. {
  41. return isString() || isPrimitive() ? getText() : 0;
  42. }