/****************************************************************************** * Copyright (C) 2017 Kitsune Ral * * 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 */ #pragma once #include #include // Includes #include namespace QMatrixClient { template inline QJsonValue toJson(T val) { return QJsonValue(val); } template inline QJsonValue toJson(const QVector& vals) { QJsonArray ar; for (const auto& v: vals) ar.push_back(toJson(v)); return ar; } inline QJsonValue toJson(const QStringList& strings) { return QJsonArray::fromStringList(strings); } template struct FromJson { T operator()(QJsonValue jv) const { return static_cast(jv); } }; template inline T fromJson(const QJsonValue& jv) { return FromJson()(jv); } template <> struct FromJson { bool operator()(QJsonValue jv) const { return jv.toBool(); } }; template <> struct FromJson { int operator()(QJsonValue jv) const { return jv.toInt(); } }; template <> struct FromJson { double operator()(QJsonValue jv) const { return jv.toDouble(); } }; template <> struct FromJson { qint64 operator()(QJsonValue jv) const { return qint64(jv.toDouble()); } }; template <> struct FromJson { QString operator()(QJsonValue jv) const { return jv.toString(); } }; template <> struct FromJson { QDateTime operator()(QJsonValue jv) const { return QDateTime::fromMSecsSinceEpoch(fromJson(jv), Qt::UTC); } }; template <> struct FromJson { QDate operator()(QJsonValue jv) const { return fromJson(jv).date(); } }; template <> struct FromJson { QJsonObject operator()(QJsonValue jv) const { return jv.toObject(); } }; template <> struct FromJson { QJsonArray operator()(QJsonValue jv) const { return jv.toArray(); } }; template struct FromJson> { QVector operator()(QJsonValue jv) const { const auto jsonArray = jv.toArray(); QVector vect; vect.resize(jsonArray.size()); std::transform(jsonArray.begin(), jsonArray.end(), vect.begin(), FromJson()); return vect; } }; } // namespace QMatrixClient