KPublicTransport

jsonpointer.cpp
1/*
2 SPDX-FileCopyrightText: 2023 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "jsonpointer_p.h"
7
8#include <QDebug>
9#include <QJsonObject>
10
11using namespace KPublicTransport;
12
13QJsonValue JsonPointer::evaluate(const QJsonValue &obj, QStringView jsonPointer)
14{
15 if (jsonPointer.startsWith(QLatin1Char('/'))) {
16 jsonPointer = jsonPointer.mid(1);
17 }
18 if (jsonPointer.isEmpty()) {
19 return obj;
20 }
21
22 const auto idx = jsonPointer.indexOf(QLatin1Char('/'));
23 const auto key = idx >= 0 ? jsonPointer.left(idx) : jsonPointer;
24
25 if (obj.isObject()) {
26 const auto value = obj.toObject().value(key);
27 if (idx >= 0) {
28 return JsonPointer::evaluate(value, jsonPointer.mid(idx + 1));
29 }
30 return value;
31 }
32 if (obj.isArray()) {
33 qWarning() << "JSON Pointer array indexing not implemented yet!";
34 return {};
35 }
36
37 qWarning() << "JSON Pointer expression applied to primitive value!" << jsonPointer;
38 return {};
39}
Query operations and data types for accessing realtime public transport information from online servi...
QJsonValue value(QLatin1StringView key) const const
bool isArray() const const
bool isObject() const const
QJsonObject toObject() const const
QStringView left(qsizetype length) const const
QStringView mid(qsizetype start, qsizetype length) const const
qsizetype indexOf(QChar c, qsizetype from, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
bool startsWith(QChar ch) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:07:52 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.