aboutsummaryrefslogtreecommitdiff
path: root/lib/events
diff options
context:
space:
mode:
authorTobias Fella <fella@posteo.de>2022-01-30 23:13:01 +0100
committerTobias Fella <fella@posteo.de>2022-01-30 23:13:01 +0100
commit023ef3005d3fae80637c6ce140e84db26250d564 (patch)
tree1790eae9c7a8e3a89f26cca43dde2d8b0860c0e2 /lib/events
parent2fc62d38e8f748d1a78baca8b6f2df40e7cfa1a9 (diff)
downloadlibquotient-023ef3005d3fae80637c6ce140e84db26250d564.tar.gz
libquotient-023ef3005d3fae80637c6ce140e84db26250d564.zip
Port devices list to database
Diffstat (limited to 'lib/events')
0 files changed, 0 insertions, 0 deletions
d='n113' href='#n113'>113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
/******************************************************************************
* Copyright (C) 2017 Kitsune Ral <kitsune-ral@users.sf.net>
*
* 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 <QtCore/QJsonObject>
#include <QtCore/QJsonArray> // Includes <QtCore/QJsonValue>
#include <QtCore/QDate>

namespace QMatrixClient
{
    template <typename T>
    inline QJsonValue toJson(T&& val)
    {
        return QJsonValue(std::forward<T>(val));
    }

    template <typename T>
    inline QJsonValue toJson(const QVector<T>& 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);
    }

    inline QJsonValue toJson(const QByteArray& bytes)
    {
#if QT_VERSION < QT_VERSION_CHECK(5, 3, 0)
        return QJsonValue(QLatin1String(bytes.constData()));
#else
        return QJsonValue(bytes.constData());
#endif
    }

    template <typename T>
    struct FromJson
    {
        T operator()(QJsonValue jv) const { return static_cast<T>(jv); }
    };

    template <typename T>
    inline T fromJson(const QJsonValue& jv)
    {
        return FromJson<T>()(jv);
    }

    template <> struct FromJson<bool>
    {
        bool operator()(QJsonValue jv) const { return jv.toBool(); }
    };

    template <> struct FromJson<int>
    {
        int operator()(QJsonValue jv) const { return jv.toInt(); }
    };

    template <> struct FromJson<double>
    {
        double operator()(QJsonValue jv) const { return jv.toDouble(); }
    };

    template <> struct FromJson<qint64>
    {
        qint64 operator()(QJsonValue jv) const { return qint64(jv.toDouble()); }
    };

    template <> struct FromJson<QString>
    {
        QString operator()(QJsonValue jv) const { return jv.toString(); }
    };

    template <> struct FromJson<QDateTime>
    {
        QDateTime operator()(QJsonValue jv) const
        {
            return QDateTime::fromMSecsSinceEpoch(fromJson<qint64>(jv), Qt::UTC);
        }
    };

    template <> struct FromJson<QDate>
    {
        QDate operator()(QJsonValue jv) const
        {
            return fromJson<QDateTime>(jv).date();
        }
    };

    template <> struct FromJson<QJsonObject>
    {
        QJsonObject operator()(QJsonValue jv) const { return jv.toObject(); }
    };

    template <> struct FromJson<QJsonArray>
    {
        QJsonArray operator()(QJsonValue jv) const { return jv.toArray(); }
    };

    template <typename T> struct FromJson<QVector<T>>
    {
        QVector<T> operator()(QJsonValue jv) const
        {
            const auto jsonArray = jv.toArray();
            QVector<T> vect; vect.resize(jsonArray.size());
            std::transform(jsonArray.begin(), jsonArray.end(),
                           vect.begin(), FromJson<T>());
            return vect;
        }
    };

    template <typename T> struct FromJson<QList<T>>
    {
        QList<T> operator()(QJsonValue jv) const
        {
            const auto jsonArray = jv.toArray();
            QList<T> sl; sl.reserve(jsonArray.size());
            std::transform(jsonArray.begin(), jsonArray.end(),
                           std::back_inserter(sl), FromJson<T>());
            return sl;
        }
    };

    template <> struct FromJson<QStringList> : FromJson<QList<QString>> { };

    template <> struct FromJson<QByteArray>
    {
        QByteArray operator()(QJsonValue jv) const
        {
            return fromJson<QString>(jv).toLatin1();
        }
    };

}  // namespace QMatrixClient