diff --git a/lib/lockpp b/lib/lockpp index 5ec7201..9b901ba 160000 --- a/lib/lockpp +++ b/lib/lockpp @@ -1 +1 @@ -Subproject commit 5ec7201e2f968c6bcba5f18bf9aa5ab2c15e3365 +Subproject commit 9b901bad31f9809f4bfaefc8795589db3ce46250 diff --git a/src/core/objects/settings.hpp b/src/core/objects/settings.hpp index 15ceb48..03db880 100644 --- a/src/core/objects/settings.hpp +++ b/src/core/objects/settings.hpp @@ -1,6 +1,7 @@ #pragma once #include #include +#include #include namespace Soundux diff --git a/src/helper/json/bindings.hpp b/src/helper/json/bindings.hpp index bda3f00..6ea4575 100644 --- a/src/helper/json/bindings.hpp +++ b/src/helper/json/bindings.hpp @@ -4,8 +4,45 @@ #include #include +namespace Soundux +{ + namespace traits + { + template struct is_optional + { + private: + static std::uint8_t test(...); + template static auto test(std::optional *) -> std::uint16_t; + + public: + static const bool value = sizeof(test(reinterpret_cast *>(0))) == sizeof(std::uint16_t); + }; + } // namespace traits +} // namespace Soundux + namespace nlohmann { + template struct adl_serializer> + { + static void to_json(json &j, const std::optional &obj) + { + if (obj) + { + j = *obj; + } + else + { + j = nullptr; + } + } + static void from_json(const json &j, const std::optional &obj) + { + if (!j.is_null()) + { + obj = j.get(); + } + } + }; // namespace nlohmann template <> struct adl_serializer { static void to_json(json &j, const Soundux::Objects::Sound &obj) @@ -18,25 +55,10 @@ namespace nlohmann {"id", obj.id}, {"path", obj.path}, {"isFavorite", obj.isFavorite}, + {"localVolume", obj.localVolume}, + {"remoteVolume", obj.remoteVolume}, {"modifiedDate", obj.modifiedDate}, }; - - if (obj.localVolume) - { - j["localVolume"] = *obj.localVolume; - } - else - { - j["localVolume"] = nullptr; - } - if (obj.remoteVolume) - { - j["remoteVolume"] = *obj.remoteVolume; - } - else - { - j["remoteVolume"] = nullptr; - } } static void from_json(const json &j, Soundux::Objects::Sound &obj) { @@ -131,9 +153,22 @@ namespace nlohmann { if (j.find(key) != j.end()) { - if (j.at(key).type_name() == nlohmann::basic_json(T{}).type_name()) + if constexpr (Soundux::traits::is_optional::value) { - j.at(key).get_to(member); + if (j.at(key).type_name() == nlohmann::basic_json(typename T::value_type{}).type_name()) + { + if (!j.at(key).is_null()) + { + member = j.at(key).get(); + } + } + } + else + { + if (j.at(key).type_name() == nlohmann::basic_json(T{}).type_name()) + { + j.at(key).get_to(member); + } } } } @@ -277,22 +312,5 @@ namespace nlohmann }; } }; - template <> struct adl_serializer> - { - static void to_json(json &j, const std::optional &obj) - { - if (obj) - { - j = { - {"name", obj->getName()}, - {"guid", obj->getGUID()}, - }; - } - else - { - j = "null"; - } - } - }; #endif } // namespace nlohmann \ No newline at end of file diff --git a/src/helper/misc/misc.cpp b/src/helper/misc/misc.cpp index 11ed51e..399bc8e 100644 --- a/src/helper/misc/misc.cpp +++ b/src/helper/misc/misc.cpp @@ -5,7 +5,12 @@ #include #include #include + +#pragma push_macro("UNICOCDE") +#undef UNICODE #include +#pragma pop_macro("UNICOCDE") + #include #include