Soundux/src/helper/json/bindings.hpp

227 lines
8.8 KiB
C++
Raw Normal View History

2021-02-26 19:31:24 +01:00
#pragma once
2021-04-13 20:31:51 +02:00
#include <core/global/globals.hpp>
#include <helper/version/check.hpp>
2021-02-26 19:31:24 +01:00
#include <nlohmann/json.hpp>
namespace nlohmann
{
template <> struct adl_serializer<Soundux::Objects::Sound>
{
static void to_json(json &j, const Soundux::Objects::Sound &obj)
{
j = {{"name", obj.name},
{"hotkeys", obj.hotkeys},
{"hotkeySequence",
Soundux::Globals::gHotKeys.getKeySequence(obj.hotkeys)}, //* For frontend and config readability
{"id", obj.id},
{"path", obj.path},
{"isFavorite", obj.isFavorite},
2021-02-26 19:31:24 +01:00
{"modifiedDate", obj.modifiedDate}};
}
static void from_json(const json &j, Soundux::Objects::Sound &obj)
{
j.at("name").get_to(obj.name);
j.at("hotkeys").get_to(obj.hotkeys);
j.at("id").get_to(obj.id);
j.at("path").get_to(obj.path);
j.at("modifiedDate").get_to(obj.modifiedDate);
if (j.find("isFavorite") != j.end())
2021-03-09 20:38:09 +01:00
{
j.at("isFavorite").get_to(obj.isFavorite);
2021-03-09 20:38:09 +01:00
}
2021-02-26 19:31:24 +01:00
}
};
template <> struct adl_serializer<Soundux::Objects::AudioDevice>
{
static void to_json(json &j, const Soundux::Objects::AudioDevice &obj)
{
j = {{"name", obj.name}, {"isDefault", obj.isDefault}};
}
static void from_json(const json &j, Soundux::Objects::AudioDevice &obj)
{
j.at("name").get_to(obj.name);
j.at("isDefault").get_to(obj.isDefault);
}
};
template <> struct adl_serializer<Soundux::Objects::PlayingSound>
{
static void to_json(json &j, const Soundux::Objects::PlayingSound &obj)
{
j = {
{"sound", obj.sound}, {"id", obj.id},
{"length", obj.length}, {"paused", obj.paused.load()},
{"lengthInMs", obj.lengthInMs}, {"repeat", obj.repeat.load()},
{"readFrames", obj.readFrames}, {"readInMs", obj.readInMs.load()},
2021-02-26 19:31:24 +01:00
};
}
static void from_json(const json &j, Soundux::Objects::PlayingSound &obj)
{
j.at("id").get_to(obj.id);
2021-02-26 19:31:24 +01:00
j.at("sound").get_to(obj.sound);
j.at("length").get_to(obj.length);
j.at("readFrames").get_to(obj.readFrames);
j.at("lengthInMs").get_to(obj.lengthInMs);
obj.paused.store(j.at("paused").get<bool>());
obj.repeat.store(j.at("repeat").get<bool>());
obj.readInMs.store(j.at("readInMs").get<std::uint64_t>());
2021-02-26 19:31:24 +01:00
}
};
template <> struct adl_serializer<Soundux::Objects::Settings>
{
static void to_json(json &j, const Soundux::Objects::Settings &obj)
{
j = {
{"theme", obj.theme},
2021-05-24 01:08:47 +02:00
{"outputs", obj.outputs},
{"viewMode", obj.viewMode},
{"sortMode", obj.sortMode},
{"stopHotkey", obj.stopHotkey},
{"syncVolumes", obj.syncVolumes},
{"selectedTab", obj.selectedTab},
{"localVolume", obj.localVolume},
{"remoteVolume", obj.remoteVolume},
{"audioBackend", obj.audioBackend},
{"deleteToTrash", obj.deleteToTrash},
{"pushToTalkKeys", obj.pushToTalkKeys},
{"tabHotkeysOnly", obj.tabHotkeysOnly},
{"minimizeToTray", obj.minimizeToTray},
{"allowOverlapping", obj.allowOverlapping},
{"muteDuringPlayback", obj.muteDuringPlayback},
{"useAsDefaultDevice", obj.useAsDefaultDevice},
{"allowMultipleOutputs", obj.allowMultipleOutputs},
};
2021-02-26 19:31:24 +01:00
}
template <typename T> static void get_to_safe(const json &j, const std::string &key, T &member) noexcept
2021-02-26 19:31:24 +01:00
{
if (j.find(key) != j.end())
{
2021-05-23 17:23:44 +02:00
if (j.at(key).type_name() == nlohmann::basic_json(T{}).type_name())
{
j.at(key).get_to(member);
}
2021-04-13 21:35:59 +02:00
}
2021-02-26 19:31:24 +01:00
}
static void from_json(const json &j, Soundux::Objects::Settings &obj)
{
get_to_safe(j, "theme", obj.theme);
2021-05-24 01:08:47 +02:00
get_to_safe(j, "outputs", obj.outputs);
get_to_safe(j, "sortMode", obj.sortMode);
get_to_safe(j, "viewMode", obj.viewMode);
get_to_safe(j, "stopHotkey", obj.stopHotkey);
get_to_safe(j, "localVolume", obj.localVolume);
get_to_safe(j, "selectedTab", obj.selectedTab);
get_to_safe(j, "syncVolumes", obj.syncVolumes);
get_to_safe(j, "audioBackend", obj.audioBackend);
get_to_safe(j, "remoteVolume", obj.remoteVolume);
get_to_safe(j, "deleteToTrash", obj.deleteToTrash);
get_to_safe(j, "pushToTalkKeys", obj.pushToTalkKeys);
get_to_safe(j, "minimizeToTray", obj.minimizeToTray);
get_to_safe(j, "tabHotkeysOnly", obj.tabHotkeysOnly);
get_to_safe(j, "allowOverlapping", obj.allowOverlapping);
get_to_safe(j, "useAsDefaultDevice", obj.useAsDefaultDevice);
get_to_safe(j, "muteDuringPlayback", obj.muteDuringPlayback);
get_to_safe(j, "allowMultipleOutputs", obj.allowMultipleOutputs);
}
2021-02-26 19:31:24 +01:00
};
template <> struct adl_serializer<Soundux::Objects::Tab>
{
static void to_json(json &j, const Soundux::Objects::Tab &obj)
{
j = {{"id", obj.id}, {"name", obj.name}, {"path", obj.path}, {"sounds", obj.sounds}};
}
static void from_json(const json &j, Soundux::Objects::Tab &obj)
{
j.at("id").get_to(obj.id);
j.at("name").get_to(obj.name);
j.at("path").get_to(obj.path);
j.at("sounds").get_to(obj.sounds);
}
};
template <> struct adl_serializer<Soundux::Objects::Data>
{
static void to_json(json &j, const Soundux::Objects::Data &obj)
{
j = {{"height", obj.height},
{"width", obj.width},
{"tabs", obj.tabs},
{"soundIdCounter", obj.soundIdCounter}};
}
static void from_json(const json &j, Soundux::Objects::Data &obj)
{
j.at("soundIdCounter").get_to(obj.soundIdCounter);
j.at("height").get_to(obj.height);
j.at("width").get_to(obj.width);
j.at("tabs").get_to(obj.tabs);
}
};
template <> struct adl_serializer<Soundux::Objects::Config>
{
static void to_json(json &j, const Soundux::Objects::Config &obj)
{
j = {{"data", obj.data}, {"settings", obj.settings}};
}
static void from_json(const json &j, Soundux::Objects::Config &obj)
{
j.at("data").get_to(obj.data);
j.at("settings").get_to(obj.settings);
}
};
2021-03-19 21:22:32 +01:00
template <> struct adl_serializer<Soundux::Objects::VersionStatus>
{
static void to_json(json &j, const Soundux::Objects::VersionStatus &obj)
{
j = {{"current", obj.current}, {"latest", obj.latest}, {"outdated", obj.outdated}};
}
static void from_json(const json &j, Soundux::Objects::VersionStatus &obj)
{
j.at("latest").get_to(obj.latest);
j.at("current").get_to(obj.current);
j.at("outdated").get_to(obj.outdated);
}
};
2021-02-26 19:31:24 +01:00
#if defined(__linux__)
2021-05-06 11:27:56 +02:00
template <> struct adl_serializer<std::shared_ptr<Soundux::Objects::IconRecordingApp>>
2021-02-26 19:31:24 +01:00
{
2021-05-06 11:27:56 +02:00
static void to_json(json &j, const std::shared_ptr<Soundux::Objects::IconRecordingApp> &obj)
2021-02-26 19:31:24 +01:00
{
j = {
{"name", obj->name},
2021-05-06 11:27:56 +02:00
{"appIcon", obj->appIcon},
{"application", obj->application},
};
2021-02-26 19:31:24 +01:00
}
2021-05-06 11:27:56 +02:00
static void from_json(const json &j, std::shared_ptr<Soundux::Objects::IconRecordingApp> &obj)
2021-02-26 19:31:24 +01:00
{
if (obj)
{
j.at("name").get_to(obj->name);
2021-05-06 11:27:56 +02:00
j.at("appIcon").get_to(obj->appIcon);
j.at("application").get_to(obj->application);
}
2021-02-26 19:31:24 +01:00
}
};
2021-05-06 11:27:56 +02:00
template <> struct adl_serializer<std::shared_ptr<Soundux::Objects::IconPlaybackApp>>
2021-02-26 19:31:24 +01:00
{
2021-05-06 11:27:56 +02:00
static void to_json(json &j, const std::shared_ptr<Soundux::Objects::IconPlaybackApp> &obj)
2021-02-26 19:31:24 +01:00
{
j = {
{"name", obj->name},
2021-05-06 11:27:56 +02:00
{"appIcon", obj->appIcon},
{"application", obj->application},
};
2021-02-26 19:31:24 +01:00
}
2021-05-06 11:27:56 +02:00
static void from_json(const json &j, std::shared_ptr<Soundux::Objects::IconPlaybackApp> &obj)
2021-02-26 19:31:24 +01:00
{
if (obj)
{
j.at("name").get_to(obj->name);
2021-05-06 11:27:56 +02:00
j.at("appIcon").get_to(obj->appIcon);
j.at("application").get_to(obj->application);
}
2021-02-26 19:31:24 +01:00
}
};
#endif
} // namespace nlohmann