From efeb50a46ad824aa258472f6ac8da74810f05a55 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sat, 31 Mar 2018 13:16:02 +0900 Subject: Move source files to a separate folder It's been long overdue to separate them from the rest of the stuff (docs etc.). Also, this allows installing to a directory within the checked out git tree (say, ./install/, similar to ./build/). --- lib/converters.h | 177 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 177 insertions(+) create mode 100644 lib/converters.h (limited to 'lib/converters.h') diff --git a/lib/converters.h b/lib/converters.h new file mode 100644 index 00000000..bba298e0 --- /dev/null +++ b/lib/converters.h @@ -0,0 +1,177 @@ +/****************************************************************************** +* 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 +{ + // This catches anything implicitly convertible to QJsonValue/Object/Array + inline QJsonValue toJson(const QJsonValue& val) { return val; } + inline QJsonObject toJson(const QJsonObject& o) { return o; } + inline QJsonArray toJson(const QJsonArray& arr) { return arr; } +#ifdef _MSC_VER // MSVC gets lost and doesn't know which overload to use + inline QJsonValue toJson(const QString& s) { return s; } +#endif + + template + inline QJsonArray toJson(const QVector& vals) + { + QJsonArray ar; + for (const auto& v: vals) + ar.push_back(toJson(v)); + return ar; + } + + inline QJsonArray toJson(const QStringList& strings) + { + return QJsonArray::fromStringList(strings); + } + + inline QJsonValue toJson(const QByteArray& bytes) + { + return QJsonValue(bytes.constData()); + } + + template + inline QJsonObject toJson(const QHash& hashMap) + { + QJsonObject json; + for (auto it = hashMap.begin(); it != hashMap.end(); ++it) + json.insert(it.key(), toJson(it.value())); + return json; + } + + template + struct FromJson + { + T operator()(const QJsonValue& jv) const { return static_cast(jv); } + }; + + template + inline T fromJson(const QJsonValue& jv) + { + return FromJson()(jv); + } + + template <> struct FromJson + { + bool operator()(const QJsonValue& jv) const { return jv.toBool(); } + }; + + template <> struct FromJson + { + int operator()(const QJsonValue& jv) const { return jv.toInt(); } + }; + + template <> struct FromJson + { + double operator()(const QJsonValue& jv) const { return jv.toDouble(); } + }; + + template <> struct FromJson + { + qint64 operator()(const QJsonValue& jv) const { return qint64(jv.toDouble()); } + }; + + template <> struct FromJson + { + QString operator()(const QJsonValue& jv) const { return jv.toString(); } + }; + + template <> struct FromJson + { + QDateTime operator()(const QJsonValue& jv) const + { + return QDateTime::fromMSecsSinceEpoch(fromJson(jv), Qt::UTC); + } + }; + + template <> struct FromJson + { + QDate operator()(const QJsonValue& jv) const + { + return fromJson(jv).date(); + } + }; + + template <> struct FromJson + { + QJsonObject operator()(const QJsonValue& jv) const + { + return jv.toObject(); + } + }; + + template <> struct FromJson + { + QJsonArray operator()(const QJsonValue& jv) const + { + return jv.toArray(); + } + }; + + template struct FromJson> + { + QVector operator()(const 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; + } + }; + + template struct FromJson> + { + QList operator()(const QJsonValue& jv) const + { + const auto jsonArray = jv.toArray(); + QList sl; sl.reserve(jsonArray.size()); + std::transform(jsonArray.begin(), jsonArray.end(), + std::back_inserter(sl), FromJson()); + return sl; + } + }; + + template <> struct FromJson : FromJson> { }; + + template <> struct FromJson + { + inline QByteArray operator()(const QJsonValue& jv) const + { + return fromJson(jv).toLatin1(); + } + }; + + template struct FromJson> + { + QHash operator()(const QJsonValue& jv) const + { + const auto json = jv.toObject(); + QHash h; h.reserve(json.size()); + for (auto it = json.begin(); it != json.end(); ++it) + h.insert(it.key(), fromJson(it.value())); + return h; + } + }; +} // namespace QMatrixClient -- cgit v1.2.3