JsonVariant.ipp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright Benoit Blanchon 2014-2016
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://github.com/bblanchon/ArduinoJson
  6. // If you like this project, please add a star!
  7. #pragma once
  8. #include "Configuration.hpp"
  9. #include "JsonVariant.hpp"
  10. #include "Internals/Parse.hpp"
  11. #include "JsonArray.hpp"
  12. #include "JsonObject.hpp"
  13. #include <string.h> // for strcmp
  14. #include <errno.h> // for errno
  15. #include <stdlib.h> // for strtol, strtod
  16. namespace ArduinoJson {
  17. inline Internals::JsonInteger JsonVariant::asInteger() const {
  18. using namespace Internals;
  19. switch (_type) {
  20. case JSON_UNDEFINED:
  21. return 0;
  22. case JSON_POSITIVE_INTEGER:
  23. case JSON_BOOLEAN:
  24. return _content.asInteger;
  25. case JSON_NEGATIVE_INTEGER:
  26. return -static_cast<Internals::JsonInteger>(_content.asInteger);
  27. case JSON_STRING:
  28. case JSON_UNPARSED:
  29. if (!_content.asString) return 0;
  30. if (!strcmp("true", _content.asString)) return 1;
  31. return parse<Internals::JsonInteger>(_content.asString);
  32. default:
  33. return static_cast<Internals::JsonInteger>(_content.asFloat);
  34. }
  35. }
  36. inline Internals::JsonUInt JsonVariant::asUnsignedInteger() const {
  37. using namespace Internals;
  38. switch (_type) {
  39. case JSON_UNDEFINED:
  40. return 0;
  41. case JSON_POSITIVE_INTEGER:
  42. case JSON_BOOLEAN:
  43. case JSON_NEGATIVE_INTEGER:
  44. return _content.asInteger;
  45. case JSON_STRING:
  46. case JSON_UNPARSED:
  47. if (!_content.asString) return 0;
  48. if (!strcmp("true", _content.asString)) return 1;
  49. return parse<Internals::JsonUInt>(_content.asString);
  50. default:
  51. return static_cast<Internals::JsonUInt>(_content.asFloat);
  52. }
  53. }
  54. inline const char *JsonVariant::asString() const {
  55. using namespace Internals;
  56. if (_type == JSON_UNPARSED && _content.asString &&
  57. !strcmp("null", _content.asString))
  58. return NULL;
  59. if (_type == JSON_STRING || _type == JSON_UNPARSED) return _content.asString;
  60. return NULL;
  61. }
  62. inline Internals::JsonFloat JsonVariant::asFloat() const {
  63. using namespace Internals;
  64. switch (_type) {
  65. case JSON_UNDEFINED:
  66. return 0;
  67. case JSON_POSITIVE_INTEGER:
  68. case JSON_BOOLEAN:
  69. return static_cast<JsonFloat>(_content.asInteger);
  70. case JSON_NEGATIVE_INTEGER:
  71. return -static_cast<JsonFloat>(_content.asInteger);
  72. case JSON_STRING:
  73. case JSON_UNPARSED:
  74. return _content.asString ? parse<JsonFloat>(_content.asString) : 0;
  75. default:
  76. return _content.asFloat;
  77. }
  78. }
  79. inline String JsonVariant::toString() const {
  80. using namespace Internals;
  81. String s;
  82. if ((_type == JSON_STRING || _type == JSON_UNPARSED) &&
  83. _content.asString != NULL)
  84. s = _content.asString;
  85. else
  86. printTo(s);
  87. return s;
  88. }
  89. inline bool JsonVariant::isBoolean() const {
  90. using namespace Internals;
  91. if (_type == JSON_BOOLEAN) return true;
  92. if (_type != JSON_UNPARSED || _content.asString == NULL) return false;
  93. return !strcmp(_content.asString, "true") ||
  94. !strcmp(_content.asString, "false");
  95. }
  96. inline bool JsonVariant::isInteger() const {
  97. using namespace Internals;
  98. if (_type == JSON_POSITIVE_INTEGER || _type == JSON_NEGATIVE_INTEGER)
  99. return true;
  100. if (_type != JSON_UNPARSED || _content.asString == NULL) return false;
  101. char *end;
  102. errno = 0;
  103. strtol(_content.asString, &end, 10);
  104. return *end == '\0' && errno == 0;
  105. }
  106. inline bool JsonVariant::isFloat() const {
  107. using namespace Internals;
  108. if (_type >= JSON_FLOAT_0_DECIMALS) return true;
  109. if (_type != JSON_UNPARSED || _content.asString == NULL) return false;
  110. char *end;
  111. errno = 0;
  112. strtod(_content.asString, &end);
  113. return *end == '\0' && errno == 0 && !is<long>();
  114. }
  115. #if ARDUINOJSON_ENABLE_STD_STREAM
  116. inline std::ostream &operator<<(std::ostream &os, const JsonVariant &source) {
  117. return source.printTo(os);
  118. }
  119. #endif
  120. } // namespace ArduinoJson