fix: merge back in relevant changes from ad5fa4030489ac6dadd6f29b3b5338ec3d4f3ea4

This commit is contained in:
D3SOX 2022-05-27 21:17:43 +02:00
parent 5f49ede204
commit d93c906850
No known key found for this signature in database
GPG Key ID: 39EC1673FC37B048
4 changed files with 61 additions and 37 deletions

@ -1 +1 @@
Subproject commit 5ec7201e2f968c6bcba5f18bf9aa5ab2c15e3365 Subproject commit 9b901bad31f9809f4bfaefc8795589db3ce46250

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <core/enums/enums.hpp> #include <core/enums/enums.hpp>
#include <string> #include <string>
#include <optional>
#include <vector> #include <vector>
namespace Soundux namespace Soundux

View File

@ -4,8 +4,45 @@
#include <helper/version/check.hpp> #include <helper/version/check.hpp>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
namespace Soundux
{
namespace traits
{
template <typename T> struct is_optional
{
private:
static std::uint8_t test(...);
template <typename O> static auto test(std::optional<O> *) -> std::uint16_t;
public:
static const bool value = sizeof(test(reinterpret_cast<std::decay_t<T> *>(0))) == sizeof(std::uint16_t);
};
} // namespace traits
} // namespace Soundux
namespace nlohmann namespace nlohmann
{ {
template <typename T> struct adl_serializer<std::optional<T>>
{
static void to_json(json &j, const std::optional<T> &obj)
{
if (obj)
{
j = *obj;
}
else
{
j = nullptr;
}
}
static void from_json(const json &j, const std::optional<T> &obj)
{
if (!j.is_null())
{
obj = j.get<T>();
}
}
}; // namespace nlohmann
template <> struct adl_serializer<Soundux::Objects::Sound> template <> struct adl_serializer<Soundux::Objects::Sound>
{ {
static void to_json(json &j, const Soundux::Objects::Sound &obj) static void to_json(json &j, const Soundux::Objects::Sound &obj)
@ -18,25 +55,10 @@ namespace nlohmann
{"id", obj.id}, {"id", obj.id},
{"path", obj.path}, {"path", obj.path},
{"isFavorite", obj.isFavorite}, {"isFavorite", obj.isFavorite},
{"localVolume", obj.localVolume},
{"remoteVolume", obj.remoteVolume},
{"modifiedDate", obj.modifiedDate}, {"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) 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.find(key) != j.end())
{ {
if (j.at(key).type_name() == nlohmann::basic_json(T{}).type_name()) if constexpr (Soundux::traits::is_optional<T>::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<typename T::value_type>();
}
}
}
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<std::optional<Soundux::Objects::RecordingDevice>>
{
static void to_json(json &j, const std::optional<Soundux::Objects::RecordingDevice> &obj)
{
if (obj)
{
j = {
{"name", obj->getName()},
{"guid", obj->getGUID()},
};
}
else
{
j = "null";
}
}
};
#endif #endif
} // namespace nlohmann } // namespace nlohmann

View File

@ -5,7 +5,12 @@
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
#include <optional> #include <optional>
#pragma push_macro("UNICOCDE")
#undef UNICODE
#include <process.hpp> #include <process.hpp>
#pragma pop_macro("UNICOCDE")
#include <regex> #include <regex>
#include <system_error> #include <system_error>