KItinerary

jsonlddocumentprocessor.cpp
1/*
2 SPDX-FileCopyrightText: 2017-2021 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "jsonlddocumentprocessor.h"
8#include "logging.h"
9#include "stringutil.h"
10
11#include <KItinerary/ExtractorResult>
12
13#include <QJsonArray>
14#include <QJsonDocument>
15#include <QJsonObject>
16
17using namespace KItinerary;
18
19bool JsonLdDocumentProcessor::canHandleData(const QByteArray &encodedData, QStringView fileName) const
20{
21 return StringUtil::startsWithIgnoreSpace(encodedData, "[") ||
22 StringUtil::startsWithIgnoreSpace(encodedData, "{") ||
25}
26
28{
29 QJsonParseError error;
30 const auto doc = QJsonDocument::fromJson(encodedData, &error);
31 if (error.error != QJsonParseError::NoError) {
32 qCDebug(Log) << error.errorString() << error.offset;
33 return {};
34 }
35
36 QJsonArray data;
37 if (doc.isObject()) {
38 data.push_back(doc.object());
39 } else if (doc.isArray()) {
40 data = doc.array();
41 } else {
42 return {};
43 }
44
46 node.setContent(data);
47 return node;
48}
49
50void JsonLdDocumentProcessor::preExtract(ExtractorDocumentNode &node, [[maybe_unused]] const ExtractorEngine *engine) const
51{
52 // pass through JSON data, so the using code can apply post-processing to that
53 node.addResult(node.content<QJsonArray>());
54}
A node in the extracted document object tree.
QJSValue content
The decoded content of this node.
void addResult(ExtractorResult &&result)
Add additional results from an extraction step.
void setContent(const QVariant &content)
Set decoded content.
Semantic data extraction engine.
bool canHandleData(const QByteArray &encodedData, QStringView fileName) const override
Fast check whether the given encoded data can possibly be processed by this instance.
ExtractorDocumentNode createNodeFromData(const QByteArray &encodedData) const override
Create a document node from raw data.
void preExtract(ExtractorDocumentNode &node, const ExtractorEngine *engine) const override
Called before extractors are applied to node.
bool startsWithIgnoreSpace(const QByteArray &data, const char *pattern)
Same as QByteArray::startsWith, but ignoring leading whitespaces.
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
void push_back(const QJsonValue &value)
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
bool endsWith(QChar ch) const const
CaseInsensitive
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 11 2024 12:14:42 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.