JsonVariant.ipp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. _type = Internals::JSON_BOOLEAN;
  11. _content.asBoolean = value;
  12. }
  13. inline JsonVariant::JsonVariant(const char *value) {
  14. _type = Internals::JSON_STRING;
  15. _content.asString = value;
  16. }
  17. inline JsonVariant::JsonVariant(const String &value) {
  18. _type = Internals::JSON_STRING;
  19. _content.asString = value.c_str();
  20. }
  21. inline JsonVariant::JsonVariant(double value, uint8_t decimals) {
  22. _type = static_cast<Internals::JsonVariantType>(
  23. Internals::JSON_DOUBLE_0_DECIMALS + decimals);
  24. _content.asDouble = value;
  25. }
  26. inline JsonVariant::JsonVariant(float value, uint8_t decimals) {
  27. _type = static_cast<Internals::JsonVariantType>(
  28. Internals::JSON_DOUBLE_0_DECIMALS + decimals);
  29. _content.asDouble = value;
  30. }
  31. inline JsonVariant::JsonVariant(JsonArray &array) {
  32. _type = Internals::JSON_ARRAY;
  33. _content.asArray = &array;
  34. }
  35. inline JsonVariant::JsonVariant(JsonObject &object) {
  36. _type = Internals::JSON_OBJECT;
  37. _content.asObject = &object;
  38. }
  39. inline JsonVariant::JsonVariant(signed char value) {
  40. _type = Internals::JSON_LONG;
  41. _content.asLong = value;
  42. }
  43. inline JsonVariant::JsonVariant(signed int value) {
  44. _type = Internals::JSON_LONG;
  45. _content.asLong = value;
  46. }
  47. inline JsonVariant::JsonVariant(signed long value) {
  48. _type = Internals::JSON_LONG;
  49. _content.asLong = value;
  50. }
  51. inline JsonVariant::JsonVariant(signed short value) {
  52. _type = Internals::JSON_LONG;
  53. _content.asLong = value;
  54. }
  55. inline JsonVariant::JsonVariant(unsigned char value) {
  56. _type = Internals::JSON_LONG;
  57. _content.asLong = value;
  58. }
  59. inline JsonVariant::JsonVariant(unsigned int value) {
  60. _type = Internals::JSON_LONG;
  61. _content.asLong = value;
  62. }
  63. inline JsonVariant::JsonVariant(unsigned long value) {
  64. _type = Internals::JSON_LONG;
  65. _content.asLong = value;
  66. }
  67. inline JsonVariant::JsonVariant(unsigned short value) {
  68. _type = Internals::JSON_LONG;
  69. _content.asLong = value;
  70. }
  71. template <typename T>
  72. inline T JsonVariant::as() const {
  73. return is<T>() ? _content.as<T>() : invalid<T>();
  74. }
  75. template <typename T>
  76. inline T JsonVariant::invalid() {
  77. return T();
  78. }
  79. template <typename T>
  80. inline bool JsonVariant::is() const {
  81. return false;
  82. }
  83. template <>
  84. inline bool JsonVariant::is<bool>() const {
  85. return _type == Internals::JSON_BOOLEAN;
  86. }
  87. template <>
  88. inline bool JsonVariant::is<char const *>() const {
  89. return _type == Internals::JSON_STRING;
  90. }
  91. template <>
  92. inline bool JsonVariant::is<String>() const {
  93. return _type == Internals::JSON_STRING;
  94. }
  95. template <>
  96. inline bool JsonVariant::is<double>() const {
  97. return _type >= Internals::JSON_DOUBLE_0_DECIMALS;
  98. }
  99. template <>
  100. inline bool JsonVariant::is<float>() const {
  101. return _type >= Internals::JSON_DOUBLE_0_DECIMALS;
  102. }
  103. template <>
  104. inline bool JsonVariant::is<JsonArray &>() const {
  105. return _type == Internals::JSON_ARRAY;
  106. }
  107. template <>
  108. inline bool JsonVariant::is<JsonArray const &>() const {
  109. return _type == Internals::JSON_ARRAY;
  110. }
  111. template <>
  112. inline bool JsonVariant::is<JsonObject &>() const {
  113. return _type == Internals::JSON_OBJECT;
  114. }
  115. template <>
  116. inline bool JsonVariant::is<JsonObject const &>() const {
  117. return _type == Internals::JSON_OBJECT;
  118. }
  119. template <>
  120. inline bool JsonVariant::is<signed char>() const {
  121. return _type == Internals::JSON_LONG;
  122. }
  123. template <>
  124. inline bool JsonVariant::is<signed int>() const {
  125. return _type == Internals::JSON_LONG;
  126. }
  127. template <>
  128. inline bool JsonVariant::is<signed long>() const {
  129. return _type == Internals::JSON_LONG;
  130. }
  131. template <>
  132. inline bool JsonVariant::is<signed short>() const {
  133. return _type == Internals::JSON_LONG;
  134. }
  135. template <>
  136. inline bool JsonVariant::is<unsigned char>() const {
  137. return _type == Internals::JSON_LONG;
  138. }
  139. template <>
  140. inline bool JsonVariant::is<unsigned int>() const {
  141. return _type == Internals::JSON_LONG;
  142. }
  143. template <>
  144. inline bool JsonVariant::is<unsigned long>() const {
  145. return _type == Internals::JSON_LONG;
  146. }
  147. template <>
  148. inline bool JsonVariant::is<unsigned short>() const {
  149. return _type == Internals::JSON_LONG;
  150. }
  151. #ifdef ARDUINOJSON_ENABLE_STD_STREAM
  152. inline std::ostream& operator<<(std::ostream& os, const JsonVariant& source) {
  153. return source.printTo(os);
  154. }
  155. #endif
  156. } // namespace ArduinoJson