JsonVariant.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright Benoit Blanchon 2014-2015
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://github.com/bblanchon/ArduinoJson
  6. #include "../include/ArduinoJson/JsonVariant.hpp"
  7. #include "../include/ArduinoJson/JsonArray.hpp"
  8. #include "../include/ArduinoJson/JsonObject.hpp"
  9. #include <errno.h> // for errno
  10. #include <stdlib.h> // for strtol, strtod
  11. using namespace ArduinoJson::Internals;
  12. namespace ArduinoJson {
  13. template <typename TFloat>
  14. static TFloat parse(const char *);
  15. template <>
  16. float parse<float>(const char *s) {
  17. return static_cast<float>(strtod(s, NULL));
  18. }
  19. template <>
  20. double parse<double>(const char *s) {
  21. return strtod(s, NULL);
  22. }
  23. template <>
  24. long parse<long>(const char *s) {
  25. return strtol(s, NULL, 10);
  26. }
  27. template <>
  28. int parse<int>(const char *s) {
  29. return atoi(s);
  30. }
  31. template <>
  32. const char *JsonVariant::as<const char *>() const {
  33. if (_type == JSON_UNPARSED && _content.asString &&
  34. !strcmp("null", _content.asString))
  35. return NULL;
  36. if (_type == JSON_STRING || _type == JSON_UNPARSED) return _content.asString;
  37. return NULL;
  38. }
  39. JsonFloat JsonVariant::asFloat() const {
  40. if (_type >= JSON_FLOAT_0_DECIMALS) return _content.asFloat;
  41. if (_type == JSON_INTEGER || _type == JSON_BOOLEAN)
  42. return static_cast<JsonFloat>(_content.asInteger);
  43. if ((_type == JSON_STRING || _type == JSON_UNPARSED) && _content.asString)
  44. return parse<JsonFloat>(_content.asString);
  45. return 0.0;
  46. }
  47. JsonInteger JsonVariant::asInteger() const {
  48. if (_type == JSON_INTEGER || _type == JSON_BOOLEAN) return _content.asInteger;
  49. if (_type >= JSON_FLOAT_0_DECIMALS)
  50. return static_cast<JsonInteger>(_content.asFloat);
  51. if ((_type == JSON_STRING || _type == JSON_UNPARSED) && _content.asString) {
  52. if (!strcmp("true", _content.asString)) return 1;
  53. return parse<JsonInteger>(_content.asString);
  54. }
  55. return 0L;
  56. }
  57. template <>
  58. String JsonVariant::as<String>() const {
  59. String s;
  60. if ((_type == JSON_STRING || _type == JSON_UNPARSED) &&
  61. _content.asString != NULL)
  62. s = _content.asString;
  63. else
  64. printTo(s);
  65. return s;
  66. }
  67. template <>
  68. bool JsonVariant::is<signed long>() const {
  69. if (_type == JSON_INTEGER) return true;
  70. if (_type != JSON_UNPARSED || _content.asString == NULL) return false;
  71. char *end;
  72. errno = 0;
  73. strtol(_content.asString, &end, 10);
  74. return *end == '\0' && errno == 0;
  75. }
  76. template <>
  77. bool JsonVariant::is<double>() const {
  78. if (_type >= JSON_FLOAT_0_DECIMALS) return true;
  79. if (_type != JSON_UNPARSED || _content.asString == NULL) return false;
  80. char *end;
  81. errno = 0;
  82. strtod(_content.asString, &end);
  83. return *end == '\0' && errno == 0 && !is<long>();
  84. }
  85. void JsonVariant::writeTo(JsonWriter &writer) const {
  86. if (_type == JSON_ARRAY)
  87. _content.asArray->writeTo(writer);
  88. else if (_type == JSON_OBJECT)
  89. _content.asObject->writeTo(writer);
  90. else if (_type == JSON_STRING)
  91. writer.writeString(_content.asString);
  92. else if (_type == JSON_UNPARSED)
  93. writer.writeRaw(_content.asString);
  94. else if (_type == JSON_INTEGER)
  95. writer.writeInteger(_content.asInteger);
  96. else if (_type == JSON_BOOLEAN)
  97. writer.writeBoolean(_content.asInteger != 0);
  98. else if (_type >= JSON_FLOAT_0_DECIMALS) {
  99. uint8_t decimals = static_cast<uint8_t>(_type - JSON_FLOAT_0_DECIMALS);
  100. writer.writeFloat(_content.asFloat, decimals);
  101. }
  102. }
  103. }