File indexing completed on 2024-11-24 04:44:39
0001 /* 0002 * SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org> 0003 * SPDX-License-Identifier: LGPL-2.0-or-later 0004 */ 0005 0006 #ifndef JSONLD_H 0007 #define JSONLD_H 0008 0009 #include <QHash> 0010 #include <QString> 0011 0012 #include <functional> 0013 #include <vector> 0014 0015 namespace Rdf { 0016 class Quad; 0017 class Term; 0018 } 0019 0020 class QByteArray; 0021 class QJsonObject; 0022 class QJsonValue; 0023 class QUrl; 0024 0025 using JsonLdDocumentLoader = std::function<QByteArray(const QString&)>; 0026 using JsonLdCurieMap = QHash<QString, QString>; 0027 class JsonLdProperty; 0028 0029 class JsonLdMetaType 0030 { 0031 public: 0032 void load(const QJsonObject &obj); 0033 void addProperty(JsonLdProperty &&property); 0034 0035 QString name; 0036 QString qualifiedName; 0037 std::vector<JsonLdProperty> properties; 0038 }; 0039 0040 class JsonLdProperty { 0041 public: 0042 static JsonLdProperty fromJson(const QString &name, const QJsonValue &value); 0043 0044 QString name; 0045 QString qualifiedName; 0046 QString type; 0047 QString prefix; 0048 JsonLdMetaType metaType; 0049 }; 0050 0051 class JsonLdContext 0052 { 0053 public: 0054 void load(const QByteArray &contextData, const JsonLdDocumentLoader &loader); 0055 void load(const QJsonObject &context); 0056 void resolve(); 0057 JsonLdMetaType metaType(const QString &type) const; 0058 0059 std::vector<JsonLdMetaType> metaTypes; 0060 JsonLdCurieMap curieMap; 0061 std::vector<JsonLdProperty> globalProperties; 0062 }; 0063 0064 0065 /** JSON-LD to RDF conversion. 0066 * @note This is far from a complete implementation of the full spec, this barely 0067 * covers enough for the needs of DIVOC JWS verification. 0068 */ 0069 class JsonLd 0070 { 0071 public: 0072 // we only support offline data, so a synchronous interface is fine 0073 void setDocumentLoader(const JsonLdDocumentLoader &loader); 0074 0075 /** Convert JSON-LD object to RDF */ 0076 std::vector<Rdf::Quad> toRdf(const QJsonObject &obj) const; 0077 0078 private: 0079 Rdf::Term toRdfRecursive(const JsonLdContext &context, const QJsonObject &obj, std::vector<Rdf::Quad> &quads) const; 0080 void toRdfRecursive(const JsonLdContext &context, const JsonLdMetaType &mt, const Rdf::Term &id, const QJsonObject &obj, std::vector<Rdf::Quad> &quads) const; 0081 Rdf::Term idForObject(const QJsonObject &obj) const; 0082 0083 JsonLdDocumentLoader m_documentLoader; 0084 mutable int m_blankNodeCounter = 0; 0085 }; 0086 0087 #endif // JSONLD_H