JsonVariant.ipp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // Copyright Benoit Blanchon 2014-2015
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://github.com/bblanchon/ArduinoJson
  6. #pragma once
  7. #include "JsonVariant.hpp"
  8. namespace ArduinoJson {
  9. inline JsonVariant::JsonVariant(bool value) {
  10. using namespace Internals;
  11. _type = JSON_BOOLEAN;
  12. _content.asInteger = static_cast<JsonInteger>(value);
  13. }
  14. inline JsonVariant::JsonVariant(const char *value) {
  15. _type = Internals::JSON_STRING;
  16. _content.asString = value;
  17. }
  18. inline JsonVariant::JsonVariant(Internals::Unparsed value) {
  19. _type = Internals::JSON_UNPARSED;
  20. _content.asString = value;
  21. }
  22. inline JsonVariant::JsonVariant(double value, uint8_t decimals) {
  23. using namespace Internals;
  24. _type = static_cast<JsonVariantType>(JSON_FLOAT_0_DECIMALS + decimals);
  25. _content.asFloat = static_cast<JsonFloat>(value);
  26. }
  27. inline JsonVariant::JsonVariant(float value, uint8_t decimals) {
  28. using namespace Internals;
  29. _type = static_cast<JsonVariantType>(JSON_FLOAT_0_DECIMALS + decimals);
  30. _content.asFloat = static_cast<JsonFloat>(value);
  31. }
  32. inline JsonVariant::JsonVariant(JsonArray &array) {
  33. _type = Internals::JSON_ARRAY;
  34. _content.asArray = &array;
  35. }
  36. inline JsonVariant::JsonVariant(JsonObject &object) {
  37. _type = Internals::JSON_OBJECT;
  38. _content.asObject = &object;
  39. }
  40. inline JsonVariant::JsonVariant(signed char value) {
  41. using namespace Internals;
  42. _type = JSON_INTEGER;
  43. _content.asInteger = static_cast<JsonInteger>(value);
  44. }
  45. inline JsonVariant::JsonVariant(signed int value) {
  46. using namespace Internals;
  47. _type = JSON_INTEGER;
  48. _content.asInteger = static_cast<JsonInteger>(value);
  49. }
  50. inline JsonVariant::JsonVariant(signed long value) {
  51. using namespace Internals;
  52. _type = JSON_INTEGER;
  53. _content.asInteger = static_cast<JsonInteger>(value);
  54. }
  55. inline JsonVariant::JsonVariant(signed short value) {
  56. using namespace Internals;
  57. _type = JSON_INTEGER;
  58. _content.asInteger = static_cast<JsonInteger>(value);
  59. }
  60. inline JsonVariant::JsonVariant(unsigned char value) {
  61. using namespace Internals;
  62. _type = JSON_INTEGER;
  63. _content.asInteger = static_cast<JsonInteger>(value);
  64. }
  65. inline JsonVariant::JsonVariant(unsigned int value) {
  66. using namespace Internals;
  67. _type = JSON_INTEGER;
  68. _content.asInteger = static_cast<JsonInteger>(value);
  69. }
  70. inline JsonVariant::JsonVariant(unsigned long value) {
  71. using namespace Internals;
  72. _type = JSON_INTEGER;
  73. _content.asInteger = static_cast<JsonInteger>(value);
  74. }
  75. inline JsonVariant::JsonVariant(unsigned short value) {
  76. using namespace Internals;
  77. _type = JSON_INTEGER;
  78. _content.asInteger = static_cast<JsonInteger>(value);
  79. }
  80. template <>
  81. String JsonVariant::as<String>() const;
  82. template <>
  83. const char *JsonVariant::as<const char *>() const;
  84. template <>
  85. inline bool JsonVariant::as<bool>() const {
  86. return asInteger() != 0;
  87. }
  88. template <>
  89. inline signed char JsonVariant::as<signed char>() const {
  90. return static_cast<signed char>(asInteger());
  91. }
  92. template <>
  93. inline unsigned char JsonVariant::as<unsigned char>() const {
  94. return static_cast<unsigned char>(asInteger());
  95. }
  96. template <>
  97. inline signed short JsonVariant::as<signed short>() const {
  98. return static_cast<signed short>(asInteger());
  99. }
  100. template <>
  101. inline unsigned short JsonVariant::as<unsigned short>() const {
  102. return static_cast<unsigned short>(asInteger());
  103. }
  104. template <>
  105. inline signed int JsonVariant::as<signed int>() const {
  106. return static_cast<signed int>(asInteger());
  107. }
  108. template <>
  109. inline unsigned int JsonVariant::as<unsigned int>() const {
  110. return static_cast<unsigned int>(asInteger());
  111. }
  112. template <>
  113. inline unsigned long JsonVariant::as<unsigned long>() const {
  114. return static_cast<unsigned long>(asInteger());
  115. }
  116. template <>
  117. inline signed long JsonVariant::as<signed long>() const {
  118. return static_cast<unsigned long>(asInteger());
  119. }
  120. template <>
  121. inline double JsonVariant::as<double>() const {
  122. return static_cast<double>(asFloat());
  123. }
  124. template <>
  125. inline float JsonVariant::as<float>() const {
  126. return static_cast<float>(asFloat());
  127. }
  128. template <typename T>
  129. inline T JsonVariant::invalid() {
  130. return T();
  131. }
  132. template <typename T>
  133. inline bool JsonVariant::is() const {
  134. return false;
  135. }
  136. template <> // in .cpp
  137. bool JsonVariant::is<signed long>() const;
  138. template <> // in .cpp
  139. bool JsonVariant::is<double>() const;
  140. template <>
  141. inline bool JsonVariant::is<bool>() const {
  142. return _type == Internals::JSON_BOOLEAN;
  143. }
  144. template <>
  145. inline bool JsonVariant::is<char const *>() const {
  146. return _type == Internals::JSON_STRING;
  147. }
  148. template <>
  149. inline bool JsonVariant::is<float>() const {
  150. return is<double>();
  151. }
  152. template <>
  153. inline bool JsonVariant::is<JsonArray &>() const {
  154. return _type == Internals::JSON_ARRAY;
  155. }
  156. template <>
  157. inline bool JsonVariant::is<JsonArray const &>() const {
  158. return _type == Internals::JSON_ARRAY;
  159. }
  160. template <>
  161. inline bool JsonVariant::is<JsonObject &>() const {
  162. return _type == Internals::JSON_OBJECT;
  163. }
  164. template <>
  165. inline bool JsonVariant::is<JsonObject const &>() const {
  166. return _type == Internals::JSON_OBJECT;
  167. }
  168. template <>
  169. inline bool JsonVariant::is<signed char>() const {
  170. return is<signed long>();
  171. }
  172. template <>
  173. inline bool JsonVariant::is<signed int>() const {
  174. return is<signed long>();
  175. }
  176. template <>
  177. inline bool JsonVariant::is<signed short>() const {
  178. return is<signed long>();
  179. }
  180. template <>
  181. inline bool JsonVariant::is<unsigned char>() const {
  182. return is<signed long>();
  183. }
  184. template <>
  185. inline bool JsonVariant::is<unsigned int>() const {
  186. return is<signed long>();
  187. }
  188. template <>
  189. inline bool JsonVariant::is<unsigned long>() const {
  190. return is<signed long>();
  191. }
  192. template <>
  193. inline bool JsonVariant::is<unsigned short>() const {
  194. return is<signed long>();
  195. }
  196. #ifdef ARDUINOJSON_ENABLE_STD_STREAM
  197. inline std::ostream &operator<<(std::ostream &os, const JsonVariant &source) {
  198. return source.printTo(os);
  199. }
  200. #endif
  201. } // namespace ArduinoJson