diff --git a/include/ArduinoJson/JsonVariant.hpp b/include/ArduinoJson/JsonVariant.hpp index ff39130b..362da4e2 100644 --- a/include/ArduinoJson/JsonVariant.hpp +++ b/include/ArduinoJson/JsonVariant.hpp @@ -194,6 +194,36 @@ inline bool JsonVariant::is() const { return _type >= Internals::JSON_DOUBLE_0_DECIMALS; } +template <> +inline bool JsonVariant::is() const { + return _type == Internals::JSON_BOOLEAN; +} + +template <> +inline bool JsonVariant::is() const { + return _type == Internals::JSON_STRING; +} + +template <> +inline bool JsonVariant::is() const { + return _type == Internals::JSON_ARRAY; +} + +template <> +inline bool JsonVariant::is() const { + return _type == Internals::JSON_ARRAY; +} + +template <> +inline bool JsonVariant::is() const { + return _type == Internals::JSON_OBJECT; +} + +template <> +inline bool JsonVariant::is() const { + return _type == Internals::JSON_OBJECT; +} + template inline bool operator==(const JsonVariant &left, T right) { return left.as() == right; diff --git a/src/JsonVariant.cpp b/src/JsonVariant.cpp index 8e365f69..c08ae54a 100644 --- a/src/JsonVariant.cpp +++ b/src/JsonVariant.cpp @@ -91,34 +91,18 @@ JsonVariant &JsonVariant::operator[](const char *key) { } void JsonVariant::writeTo(JsonWriter &writer) const { - switch (_type) { - case JSON_ARRAY: - _content.asArray->writeTo(writer); - break; - - case JSON_OBJECT: - _content.asObject->writeTo(writer); - break; - - case JSON_STRING: - writer.writeString(_content.asString); - break; - - case JSON_LONG: - writer.writeLong(_content.asLong); - break; - - case JSON_BOOLEAN: - writer.writeBoolean(_content.asBoolean); - break; - - case JSON_INVALID: - case JSON_UNDEFINED: - break; - - default: // >= JSON_DOUBLE_0_DECIMALS - uint8_t decimals = static_cast(_type - JSON_DOUBLE_0_DECIMALS); - writer.writeDouble(_content.asDouble, decimals); - break; + if (is()) + as().writeTo(writer); + else if (is()) + as().writeTo(writer); + else if (is()) + writer.writeString(as()); + else if (is()) + writer.writeLong(as()); + else if (is()) + writer.writeBoolean(as()); + else if (is()) { + uint8_t decimals = static_cast(_type - JSON_DOUBLE_0_DECIMALS); + writer.writeDouble(as(), decimals); } }