KItinerary

jsonlddocument.h
1/*
2 SPDX-FileCopyrightText: 2017 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#pragma once
8
9#include "kitinerary_export.h"
10
11#include <QList>
12#include <QVariant>
13
14class QJsonArray;
15class QJsonObject;
16struct QMetaObject;
17
18namespace KItinerary {
19
20namespace JsonLd {
21
22/** Checks whether @p v holds a null-like value.
23 * This is similar to QVariant::isNull in Qt5, but differs
24 * from the "shallow" QVariant::isNull in Qt6 which doesn't
25 * check the content at all.
26 */
27KITINERARY_EXPORT bool valueIsNull(const QVariant &v);
28
29}
30
31/** Serialization/deserialization code for JSON-LD data.
32 * @see https://www.w3.org/TR/json-ld/
33 */
35
36public:
37 /** Convert JSON-LD array into instantiated data types. */
38 static KITINERARY_EXPORT QList<QVariant> fromJson(const QJsonArray &array);
39 /** Convert JSON-LD object into an instantiated data type. */
40 static KITINERARY_EXPORT QList<QVariant> fromJson(const QJsonObject &obj);
41
42 /** Convert a single JSON-LD object into an instantiated data type.
43 * @note Use this only if you are sure the JSON-LD object does not expand to
44 * multiple objects! That is usually only the case for objects you have
45 * written yourself and that semantically are guaranteed to be a single
46 * object. Anything received from external sources can expand and should not
47 * use this method.
48 * @since 20.04
49 */
50 static KITINERARY_EXPORT QVariant fromJsonSingular(const QJsonObject &obj);
51
52 /** Serialize instantiated data types to JSON-LD. */
53 static KITINERARY_EXPORT QJsonArray toJson(const QList<QVariant> &data);
54 /** Serialize instantiated data type to JSON-LD. */
55 static KITINERARY_EXPORT QJsonObject toJson(const QVariant &data);
56
57 /** JSON-LD serrialization of an invidividual data value.
58 * Unlike the above this also works with primitive types.
59 */
60 static KITINERARY_EXPORT QJsonValue toJsonValue(const QVariant &data);
61
62 /** Read property @p name on object @p obj. */
63 static KITINERARY_EXPORT QVariant readProperty(const QVariant &obj,
64 const char *name);
65 /** Set property @p name on object @p obj to value @p value. */
66 static KITINERARY_EXPORT void writeProperty(QVariant &obj, const char *name,
67 const QVariant &value);
68 /** Set property @p name on object @p obj to value @p value. */
69 template <typename T>
70 inline static void writeProperty(T &obj, const char *name,
71 const QVariant &value);
72
73 /** Removes property @p name on object @p obj. */
74 KITINERARY_EXPORT static void removeProperty(QVariant &obj, const char *name);
75
76 /** Apply all properties of @p rhs on to @p lhs.
77 * Use this to merge two top-level objects of the same type, with
78 * @p rhs containing newer information.
79 */
80 KITINERARY_EXPORT static QVariant apply(const QVariant &lhs,
81 const QVariant &rhs);
82
83 /** Register a custom type for deserialization. */
84 template <typename T> static inline void registerType() {
85 registerType(T::typeName(), &T::staticMetaObject, qMetaTypeId<T>());
86 }
87
88private:
89 KITINERARY_EXPORT static void writePropertyImpl(const QMetaObject *mo, void *obj, const char *name, const QVariant &value);
90 KITINERARY_EXPORT static void registerType(const char *typeName, const QMetaObject *mo, int metaTypeId);
91};
92
93template <typename T>
94inline void JsonLdDocument::writeProperty(T &obj, const char *name, const QVariant &value)
95{
96 writePropertyImpl(&T::staticMetaObject, &obj, name, value);
97}
98
99}
100
Serialization/deserialization code for JSON-LD data.
static void writeProperty(QVariant &obj, const char *name, const QVariant &value)
Set property name on object obj to value value.
static QJsonValue toJsonValue(const QVariant &data)
JSON-LD serrialization of an invidividual data value.
static QVariant apply(const QVariant &lhs, const QVariant &rhs)
Apply all properties of rhs on to lhs.
static QVariant readProperty(const QVariant &obj, const char *name)
Read property name on object obj.
static QJsonArray toJson(const QList< QVariant > &data)
Serialize instantiated data types to JSON-LD.
static void registerType()
Register a custom type for deserialization.
static void removeProperty(QVariant &obj, const char *name)
Removes property name on object obj.
static QList< QVariant > fromJson(const QJsonArray &array)
Convert JSON-LD array into instantiated data types.
static QVariant fromJsonSingular(const QJsonObject &obj)
Convert a single JSON-LD object into an instantiated data type.
bool valueIsNull(const QVariant &v)
Checks whether v holds a null-like value.
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:28:48 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.