KItinerary

jsonldfilterengine.cpp
1/*
2 SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "jsonldfilterengine.h"
8#include "jsonld.h"
9
10#include <QJsonArray>
11#include <QJsonObject>
12
13#include <cstring>
14
15using namespace KItinerary;
16
17void KItinerary::JsonLd::renameProperty(QJsonObject &obj, const char *oldName, const char *newName)
18{
19 const auto value = obj.value(QLatin1StringView(oldName));
20 if (!value.isUndefined() && !obj.contains(QLatin1StringView(newName))) {
21 obj.insert(QLatin1StringView(newName), value);
22 obj.remove(QLatin1StringView(oldName));
23 }
24}
25
26JsonLdFilterEngine::JsonLdFilterEngine() = default;
27JsonLdFilterEngine::~JsonLdFilterEngine() = default;
28
30{
31 auto type = JsonLd::typeName(obj).toUtf8();
32
33 // normalize type
34 if (m_typeMappings) {
35 const auto it = std::lower_bound(m_typeMappings, m_typeMappings + m_typeMappingsSize, type, [](const auto &lhs, const auto &rhs) {
36 return std::strcmp(lhs.fromType, rhs.constData()) < 0;
37 });
38 if (it != (m_typeMappings + m_typeMappingsSize) && std::strcmp((*it).fromType, type.constData()) == 0) {
39 type = it->toType;
40 obj.insert(QStringLiteral("@type"), QLatin1StringView(type));
41 }
42 }
43
44 for (auto it = obj.begin(); it != obj.end(); ++it) {
45 if ((*it).type() == QJsonValue::Object) {
46 QJsonObject subObj = (*it).toObject();
47 filterRecursive(subObj);
48 *it = subObj;
49 } else if ((*it).type() == QJsonValue::Array) {
50 QJsonArray array = (*it).toArray();
51 filterRecursive(array);
52 *it = array;
53 }
54 }
55
56 // rename properties
57 if (m_propertyMappings) {
58 const auto [pBegin, pEnd] = std::equal_range(m_propertyMappings, m_propertyMappings + m_propertyMappingsSize, type, [](const auto &lhs, const auto &rhs) {
59 if constexpr (std::is_same_v<std::decay_t<decltype(lhs)>, QByteArray>) {
60 return std::strcmp(lhs.constData(), rhs.type) < 0;
61 } else {
62 return std::strcmp(lhs.type, rhs.constData()) < 0;
63 }
64 });
65 for (auto it = pBegin; it != pEnd; ++it) {
66 JsonLd::renameProperty(obj, (*it).fromName, (*it).toName);
67 }
68 }
69
70 // apply filter functions
71 if (m_typeFilters) {
72 const auto filterIt = std::lower_bound(m_typeFilters, m_typeFilters + m_typeFiltersSize, type, [](const auto &lhs, const auto &rhs) {
73 return std::strcmp(lhs.type, rhs.constData()) < 0;
74 });
75 if (filterIt != (m_typeFilters + m_typeFiltersSize) && std::strcmp((*filterIt).type, type.constData()) == 0) {
76 (*filterIt).filterFunc(obj);
77 }
78 }
79}
80
82{
83 for (auto it = array.begin(); it != array.end(); ++it) {
84 if ((*it).type() == QJsonValue::Object) {
85 QJsonObject subObj = (*it).toObject();
86 filterRecursive(subObj);
87 *it = subObj;
88 } else if ((*it).type() == QJsonValue::Array) {
89 QJsonArray array = (*it).toArray();
90 filterRecursive(array);
91 *it = array;
92 }
93 }
94}
95
96void JsonLdFilterEngine::setTypeMappings(const JsonLdFilterEngine::TypeMapping *typeMappings, std::size_t count)
97{
98 m_typeMappings = typeMappings;
99 m_typeMappingsSize = count;
100}
101
102void JsonLdFilterEngine::setTypeFilters(const JsonLdFilterEngine::TypeFilter *typeFilters, std::size_t count)
103{
104 m_typeFilters = typeFilters;
105 m_typeFiltersSize = count;
106}
107
108void JsonLdFilterEngine::setPropertyMappings(const JsonLdFilterEngine::PropertyMapping *propertyMappings, std::size_t count)
109{
110 m_propertyMappings = propertyMappings;
111 m_propertyMappingsSize = count;
112}
void filterRecursive(QJsonObject &obj)
Recursively apply filtering rules to obj.
QString typeName(const QJsonObject &obj)
Normalized type name from object.
void renameProperty(QJsonObject &obj, const char *oldName, const char *newName)
Rename a property, if present and the new name isn't in use already.
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
iterator begin()
iterator end()
iterator begin()
bool contains(QLatin1StringView key) const const
iterator end()
iterator insert(QLatin1StringView key, const QJsonValue &value)
void remove(QLatin1StringView key)
QJsonValue value(QLatin1StringView key) const const
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 20 2024 11:52:36 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.