/* * Sailfin: a Jellyfin client written using Qt * Copyright (C) 2021 Chris Josten and the Sailfin Contributors. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #ifndef JELLYFIN_SUPPORT_JSONCONVIMPL_H #define JELLYFIN_SUPPORT_JSONCONVIMPL_H #include #include #include #include #include #include #include #include #include #include namespace Jellyfin { namespace Support { // Helper functions //QString uuidToString(const QUuid &source); //QUuid stringToUuid(const QString &source); // https://www.fluentcpp.com/2017/08/15/function-templates-partial-specialization-cpp/ template struct convertType{}; /** * Template for converting types from JSON into their respective type. */ template T fromJsonValue(const QJsonValue &source, convertType) { Q_UNUSED(source) Q_ASSERT_X(false, "fromJsonValue", "fromJsonValue called with unimplemented type"); } /** * Template for converting types from their type to JSON. */ template QJsonValue toJsonValue(const T &source, convertType) { Q_UNUSED(source) std::string msg = "toJsonValue called with unimplemented type "; msg += typeid (T).name(); Q_ASSERT_X(false, "toJsonValue", msg.c_str()); return QJsonValue(); } template T fromJsonValue(const QJsonValue &source) { return fromJsonValue(source, convertType{}); } template QJsonValue toJsonValue(const T &source) { return toJsonValue(source, convertType{}); } // QList template QList fromJsonValue(const QJsonValue &source, convertType>) { QList result; QJsonArray arr = source.toArray(); result.reserve(arr.size()); for (auto it = arr.constBegin(); it != arr.constEnd(); it++) { result.append(fromJsonValue(*it)); } return result; } template QJsonValue toJsonValue(const QList &source, convertType>) { QJsonArray result; for (auto it = source.cbegin(); it != source.cend(); it++) { result.push_back(toJsonValue(*it)); } return result; } // Optional template std::optional fromJsonValue(const QJsonValue &source, convertType>) { if (source.isNull()) { return std::nullopt; } else { return fromJsonValue(source, convertType{}); } } template QJsonValue toJsonValue(const std::optional &source, convertType>) { if (source.has_value()) { return toJsonValue(source.value(), convertType{}); } else { // Null return QJsonValue(); } } // QSharedPointer template QSharedPointer fromJsonValue(const QJsonValue &source, convertType>) { if (source.isNull()) { return QSharedPointer(); } return QSharedPointer::create(fromJsonValue(source)); } template QJsonValue toJsonValue(const QSharedPointer &source, convertType>) { if (source.isNull()) { return QJsonValue(); } return toJsonValue(*source); } /** * Templates for string conversion. */ template QString toString(const T &source, convertType) { return toJsonValue(source).toString(); } template QString toString(const std::optional &source, convertType>) { if (source.has_value()) { return toString(source.value(), convertType{}); } else { return QString(); } } template QString toString(const T &source) { return toString(source, convertType{}); } } // NS Support } // NS Jellyfin #endif // JELLYFIN_SUPPORT_JSONCONVIMPL_H