JsonVariant.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright Benoit Blanchon 2014
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://github.com/bblanchon/ArduinoJson
  6. #include "../include/ArduinoJson/JsonVariant.hpp"
  7. #include "../include/ArduinoJson/Internals/PrettyJsonWriter.hpp"
  8. #include "../include/ArduinoJson/JsonArray.hpp"
  9. #include "../include/ArduinoJson/JsonObject.hpp"
  10. using namespace ArduinoJson;
  11. using namespace ArduinoJson::Internals;
  12. JsonVariant JsonVariant::_invalid(JSON_INVALID);
  13. JsonVariant::operator JsonArray &() const {
  14. return _type == JSON_ARRAY ? *_content.asArray : JsonArray::invalid();
  15. }
  16. JsonVariant::operator JsonObject &() const {
  17. return _type == JSON_OBJECT ? *_content.asObject : JsonObject::invalid();
  18. }
  19. JsonVariant::operator bool() const {
  20. return _type == JSON_BOOLEAN ? _content.asBoolean : false;
  21. }
  22. JsonVariant::operator const char *() const {
  23. return _type == JSON_STRING ? _content.asString : NULL;
  24. }
  25. JsonVariant::operator double() const {
  26. return _type >= JSON_DOUBLE_0_DECIMALS ? _content.asDouble : 0;
  27. }
  28. JsonVariant::operator long() const {
  29. return _type == JSON_LONG ? _content.asLong : 0;
  30. }
  31. void JsonVariant::set(bool value) {
  32. if (_type == JSON_INVALID) return;
  33. _type = Internals::JSON_BOOLEAN;
  34. _content.asBoolean = value;
  35. }
  36. void JsonVariant::set(const char *value) {
  37. if (_type == JSON_INVALID) return;
  38. _type = JSON_STRING;
  39. _content.asString = value;
  40. }
  41. void JsonVariant::set(double value, uint8_t decimals) {
  42. if (_type == JSON_INVALID) return;
  43. _type = static_cast<JsonVariantType>(JSON_DOUBLE_0_DECIMALS + decimals);
  44. _content.asDouble = value;
  45. }
  46. void JsonVariant::set(long value) {
  47. if (_type == JSON_INVALID) return;
  48. _type = JSON_LONG;
  49. _content.asLong = value;
  50. }
  51. void JsonVariant::set(JsonArray &array) {
  52. if (_type == JSON_INVALID) return;
  53. _type = JSON_ARRAY;
  54. _content.asArray = &array;
  55. }
  56. void JsonVariant::set(JsonObject &object) {
  57. if (_type == JSON_INVALID) return;
  58. _type = JSON_OBJECT;
  59. _content.asObject = &object;
  60. }
  61. size_t JsonVariant::size() const {
  62. if (_type == JSON_ARRAY) return _content.asArray->size();
  63. if (_type == JSON_OBJECT) return _content.asObject->size();
  64. return 0;
  65. }
  66. JsonVariant &JsonVariant::operator[](int index) {
  67. if (_type != JSON_ARRAY) return JsonVariant::invalid();
  68. return _content.asArray->operator[](index);
  69. }
  70. JsonVariant &JsonVariant::operator[](const char *key) {
  71. if (_type != JSON_OBJECT) return JsonVariant::invalid();
  72. return _content.asObject->operator[](key);
  73. }
  74. template <typename T>
  75. void JsonVariant::writeTo(T &writer) const {
  76. switch (_type) {
  77. case JSON_ARRAY:
  78. _content.asArray->writeTo(writer);
  79. break;
  80. case JSON_OBJECT:
  81. _content.asObject->writeTo(writer);
  82. break;
  83. case JSON_STRING:
  84. writer.writeString(_content.asString);
  85. break;
  86. case JSON_LONG:
  87. writer.writeInteger(_content.asLong);
  88. break;
  89. case JSON_BOOLEAN:
  90. writer.writeBoolean(_content.asBoolean);
  91. break;
  92. default: // >= JSON_DOUBLE_0_DECIMALS
  93. writer.writeDouble(_content.asDouble, _type - JSON_DOUBLE_0_DECIMALS);
  94. break;
  95. }
  96. }
  97. template void JsonVariant::writeTo(JsonWriter &) const;
  98. template void JsonVariant::writeTo(PrettyJsonWriter &) const;