File indexing completed on 2024-04-28 04:41:32
0001 /* 0002 SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "gbfsreader.h" 0008 0009 #include <QJsonDocument> 0010 #include <QJsonObject> 0011 0012 #include <cmath> 0013 0014 using namespace KPublicTransport; 0015 0016 static double readDouble(const QJsonValue &v) 0017 { 0018 if (v.isDouble()) { 0019 return v.toDouble(NAN); 0020 } 0021 if (v.isString()) { 0022 bool valid = false; 0023 const auto d = v.toString().toDouble(&valid); 0024 return valid ? d : NAN; 0025 } 0026 return NAN; 0027 } 0028 0029 double GBFSReader::readLatitude(const QJsonObject &obj) 0030 { 0031 return readDouble(obj.value(QLatin1String("lat"))); 0032 } 0033 0034 double GBFSReader::readLongitude(const QJsonObject &obj) 0035 { 0036 return readDouble(obj.value(QLatin1String("lon"))); 0037 } 0038 0039 QJsonObject GBFSReader::dataObject(const QJsonDocument &doc) 0040 { 0041 const auto obj = doc.object(); 0042 const auto it = obj.find(QLatin1String("data")); 0043 if (it == obj.end() || !(*it).isObject()) { 0044 return obj; 0045 } 0046 return (*it).toObject(); 0047 } 0048 0049 QJsonValue GBFSReader::dataValue(const QJsonDocument &doc, QLatin1String name) 0050 { 0051 return dataObject(doc).value(name); 0052 }