9#include "json/jsonldfilterengine.h"
11#include <KItinerary/JsonLdDocument>
18#include <QMetaProperty>
19#include <QRegularExpression>
30JsApi::JsonLd::~JsonLd() =
default;
41 const auto addr = newObject(QStringLiteral(
"PostalAddress"));
42 const auto geo = newObject(QStringLiteral(
"GeoCoordinates"));
44 auto p = newObject(type);
45 p.setProperty(QStringLiteral(
"address"), addr);
46 p.setProperty(QStringLiteral(
"geo"), geo);
53 const auto dep = newObject(QStringLiteral(
"Airport"));
54 const auto arr = newObject(QStringLiteral(
"Airport"));
55 const auto airline = newObject(QStringLiteral(
"Airline"));
56 const auto person = newObject(QStringLiteral(
"Person"));
58 auto resFor = newObject(QStringLiteral(
"Flight"));
59 resFor.setProperty(QStringLiteral(
"departureAirport"), dep);
60 resFor.setProperty(QStringLiteral(
"arrivalAirport"), arr);
61 resFor.setProperty(QStringLiteral(
"airline"), airline);
63 const auto ticket = newObject(QStringLiteral(
"Ticket"));
65 auto res = newObject(QStringLiteral(
"FlightReservation"));
66 res.setProperty(QStringLiteral(
"reservationFor"), resFor);
67 res.setProperty(QStringLiteral(
"underName"), person);
68 res.setProperty(QStringLiteral(
"reservedTicket"), ticket);
75 const auto dep = newPlace(QStringLiteral(
"TrainStation"));
76 const auto arr = newPlace(QStringLiteral(
"TrainStation"));
77 const auto person = newObject(QStringLiteral(
"Person"));
78 const auto seat = newObject(QStringLiteral(
"Seat"));
80 auto ticket = newObject(QStringLiteral(
"Ticket"));
81 ticket.setProperty(QStringLiteral(
"ticketedSeat"), seat);
83 auto resFor = newObject(QStringLiteral(
"TrainTrip"));
84 resFor.setProperty(QStringLiteral(
"departureStation"), dep);
85 resFor.setProperty(QStringLiteral(
"arrivalStation"), arr);
86 resFor.setProperty(QStringLiteral(
"provider"), newObject(QStringLiteral(
"Organization")));
88 auto res = newObject(QStringLiteral(
"TrainReservation"));
89 res.setProperty(QStringLiteral(
"reservationFor"), resFor);
90 res.setProperty(QStringLiteral(
"underName"), person);
91 res.setProperty(QStringLiteral(
"reservedTicket"), ticket);
92 res.setProperty(QStringLiteral(
"programMembershipUsed"), newObject(QStringLiteral(
"ProgramMembership")));
99 const auto dep = newPlace(QStringLiteral(
"BusStation"));
100 const auto arr = newPlace(QStringLiteral(
"BusStation"));
101 const auto person = newObject(QStringLiteral(
"Person"));
102 const auto seat = newObject(QStringLiteral(
"Seat"));
104 auto ticket = newObject(QStringLiteral(
"Ticket"));
105 ticket.setProperty(QStringLiteral(
"ticketedSeat"), seat);
107 auto resFor = newObject(QStringLiteral(
"BusTrip"));
108 resFor.setProperty(QStringLiteral(
"departureBusStop"), dep);
109 resFor.setProperty(QStringLiteral(
"arrivalBusStop"), arr);
111 auto res = newObject(QStringLiteral(
"BusReservation"));
112 res.setProperty(QStringLiteral(
"reservationFor"), resFor);
113 res.setProperty(QStringLiteral(
"underName"), person);
114 res.setProperty(QStringLiteral(
"reservedTicket"), ticket);
121 const auto person = newObject(QStringLiteral(
"Person"));
122 const auto resFor = newPlace(QStringLiteral(
"LodgingBusiness"));
124 auto res = newObject(QStringLiteral(
"LodgingReservation"));
125 res.setProperty(QStringLiteral(
"reservationFor"), resFor);
126 res.setProperty(QStringLiteral(
"underName"), person);
133 auto resFor = newObject(QStringLiteral(
"Event"));
134 resFor.setProperty(QStringLiteral(
"location"), newPlace(QStringLiteral(
"Place")));
136 auto res = newObject(QStringLiteral(
"EventReservation"));
137 res.setProperty(QStringLiteral(
"reservationFor"), resFor);
139 auto ticket = newObject(QStringLiteral(
"Ticket"));
140 ticket.setProperty(QStringLiteral(
"ticketedSeat"), newObject(QStringLiteral(
"Seat")));
141 res.setProperty(QStringLiteral(
"reservedTicket"), ticket);
143 const auto person = newObject(QStringLiteral(
"Person"));
144 res.setProperty(QStringLiteral(
"underName"), person);
151 const auto person = newObject(QStringLiteral(
"Person"));
152 const auto resFor = newPlace(QStringLiteral(
"FoodEstablishment"));
154 auto res = newObject(QStringLiteral(
"FoodEstablishmentReservation"));
155 res.setProperty(QStringLiteral(
"reservationFor"), resFor);
156 res.setProperty(QStringLiteral(
"underName"), person);
163 const auto pickup = newPlace(QStringLiteral(
"Place"));
164 const auto dropoff = newPlace(QStringLiteral(
"Place"));
165 const auto person = newObject(QStringLiteral(
"Person"));
166 const auto org = newObject(QStringLiteral(
"Organization"));
168 auto resFor = newObject(QStringLiteral(
"RentalCar"));
169 resFor.setProperty(QStringLiteral(
"rentalCompany"),
org);
171 auto res = newObject(QStringLiteral(
"RentalCarReservation"));
172 res.setProperty(QStringLiteral(
"reservationFor"), resFor);
173 res.setProperty(QStringLiteral(
"pickupLocation"), pickup);
174 res.setProperty(QStringLiteral(
"dropoffLocation"), dropoff);
175 res.setProperty(QStringLiteral(
"underName"), person);
182 const auto dep = newPlace(QStringLiteral(
"BoatTerminal"));
183 const auto arr = newPlace(QStringLiteral(
"BoatTerminal"));
184 const auto person = newObject(QStringLiteral(
"Person"));
185 const auto ticket = newObject(QStringLiteral(
"Ticket"));
187 auto resFor = newObject(QStringLiteral(
"BoatTrip"));
188 resFor.setProperty(QStringLiteral(
"departureBoatTerminal"), dep);
189 resFor.setProperty(QStringLiteral(
"arrivalBoatTerminal"), arr);
191 auto res = newObject(QStringLiteral(
"BoatReservation"));
192 res.setProperty(QStringLiteral(
"reservationFor"), resFor);
193 res.setProperty(QStringLiteral(
"underName"), person);
194 res.setProperty(QStringLiteral(
"reservedTicket"), ticket);
200 {
"TrainReservation",
"BusReservation" },
201 {
"TrainStation",
"BusStation" },
202 {
"TrainTrip",
"BusTrip" },
206 {
"BusTrip",
"arrivalStation",
"arrivalBusStop" },
207 {
"BusTrip",
"departureStation",
"departureBusStop" },
208 {
"BusTrip",
"trainName",
"busName" },
209 {
"BusTrip",
"trainNumber",
"busNumber" },
216 filterEngine.setTypeMappings(train_to_bus_type_map);
217 filterEngine.setPropertyMappings(train_to_bus_property_map);
219 return m_engine->toScriptValue(obj);
223 {
"BusReservation",
"TrainReservation" },
224 {
"BusStation",
"TrainStation" },
225 {
"BusStop",
"TrainStation" },
226 {
"BusTrip",
"TrainTrip" },
230 {
"TrainTrip",
"arrivalBusStop",
"arrivalStation" },
231 {
"TrainTrip",
"departureBusStop",
"departureStation" },
232 {
"TrainTrip",
"busCompany",
"provider" },
233 {
"TrainTrip",
"busName",
"trainName" },
234 {
"TrainTrip",
"busNumber",
"trainNumber" },
241 filterEngine.setTypeMappings(bus_to_train_type_map);
242 filterEngine.setPropertyMappings(bus_to_train_property_map);
244 return m_engine->toScriptValue(obj);
250 auto dt = locale.toDateTime(dtStr, format);
257 auto dtStrFixed = dtStr;
258 for (
int i = 1; i <= 12; ++i) {
262 dt = locale.toDateTime(dtStrFixed, format);
270 auto dtStrFixed = dtStr;
271 for (
int i = 1; i <= 12; ++i) {
273 const auto beginIdx = dtStr.
indexOf(monthName.at(0));
277 auto endIdx = beginIdx;
278 for (
auto nameIdx = 0;
279 endIdx < dtStr.
size() && nameIdx < monthName.size(); ++nameIdx) {
281 monthName.at(nameIdx).toCaseFolded()) {
285 if (endIdx - beginIdx >= 3) {
286 dtStrFixed.replace(beginIdx, endIdx - beginIdx,
291 dt = locale.toDateTime(dtStrFixed, format);
305 if (!hasDay && !hasMonth && !hasYear) {
306 dt.setDate({1970, 1, 1});
310 else if (!hasYear && m_contextDate.isValid()) {
311 dt.setDate({m_contextDate.date().year(), dt.date().month(), dt.date().day()});
314 if (dt < m_contextDate.addDays(-1)) {
320 else if (!hasFullYear && dt.date().year() / 100 == 19) {
323 dt.setDate(dt.date().addYears(100));
336 for (
auto i = 0; i < count; ++i) {
337 const auto dt = toDateTime(dtStr, format.
property(i), localeName);
346 for (
auto i = 0; i < count; ++i) {
347 const auto dt = toDateTime(dtStr, format.
toString(),
360 return m_engine->toScriptValue(
365 return m_engine->toScriptValue(json);
370 return m_engine->toScriptValue(v.
toVariant());
378 QStringLiteral(
"[/=](-?\\d+\\.\\d+),(-?\\d+\\.\\d+)"));
380 if (!match.hasMatch()) {
384 if (match.hasMatch()) {
385 auto geo = m_engine->newObject();
386 geo.setProperty(QStringLiteral(
"@type"),
387 QStringLiteral(
"GeoCoordinates"));
388 geo.setProperty(QStringLiteral(
"latitude"),
389 match.capturedView(1).toDouble());
390 geo.setProperty(QStringLiteral(
"longitude"),
391 match.capturedView(2).toDouble());
407 qWarning() <<
"Unknown property name:" << mo->className() << propName;
410 const auto prop = mo->property(propIdx);
411 const auto dt = prop.readOnGadget(obj.
constData());
423void JsApi::JsonLd::setContextDate(
const QDateTime& dt)
428#include "moc_jsonld.cpp"
Q_INVOKABLE QJSValue newEventReservation() const
Convenience method that generates a full EventReservation JS object.
Q_INVOKABLE QJSValue toGeoCoordinates(const QString &mapUrl)
Parses geo coordinates from a given mapping service URLs.
Q_INVOKABLE QJSValue newLodgingReservation() const
Convenience method that generates a full LodgingReservation JS object.
Q_INVOKABLE QJSValue newBusReservation() const
Convenience method that generates a full BusReservation JS object.
Q_INVOKABLE QJSValue busToTrainReservation(const QJSValue &busRes) const
Convert a bus reservation to a train reservation.
Q_INVOKABLE QJSValue newObject(const QString &typeName) const
Create a new JSON-LD object of type typeName.
Q_INVOKABLE QJSValue apply(const QJSValue &lhs, const QJSValue &rhs) const
Q_INVOKABLE QJSValue clone(const QJSValue &v) const
Clones the given JS object.
Q_INVOKABLE QJSValue toJson(const QVariant &v) const
Convert object v to a JSON-LD object.
Q_INVOKABLE QJSValue newRentalCarReservation() const
Convenience method that generates a full RentalCarReservation JS object.
Q_INVOKABLE QDateTime toDateTime(const QString &dtStr, const QJSValue &format, const QJSValue &localeName) const
Convert a date/time string to a date/time value.
Q_INVOKABLE QJSValue readQDateTime(const QVariant &obj, const QString &propName) const
Read a QDateTime property and return a JSON-LD serialization of it.
Q_INVOKABLE QJSValue newFoodEstablishmentReservation() const
Convenience method that generates a full FoodEstablishmentReservation JS object.
Q_INVOKABLE QJSValue newTrainReservation() const
Convenience method that generates a full TrainReservation JS object.
Q_INVOKABLE QJSValue newFlightReservation() const
Convenience method that generates a full FlightReservation JS object.
Q_INVOKABLE QJSValue newBoatReservation() const
Convenience method that generates a full BoatReservation JS object.
Q_INVOKABLE QJSValue trainToBusReservation(const QJSValue &trainRes) const
Convert a train reservation to a bus reservation.
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 QJsonArray toJson(const QList< QVariant > &data)
Serialize instantiated data types to JSON-LD.
static QVariant fromJsonSingular(const QJsonObject &obj)
Convert a single JSON-LD object into an instantiated data type.
JSON-LD filtering for input normalization or type transforms.
void filterRecursive(QJsonObject &obj)
Recursively apply filtering rules to obj.
Classes for reservation/travel data models, data extraction and data augmentation.
const char * constData() const const
char32_t toCaseFolded(char32_t ucs4)
QJsonValue fromVariant(const QVariant &variant)
QJsonObject toObject() const const
bool isArray() const const
bool isString() const const
QJSValue property(const QString &name) const const
void setProperty(const QString &name, const QJSValue &value)
qint32 toInt() const const
QString toString() const const
QVariant toVariant() const const
QRegularExpressionMatch match(QStringView subjectView, qsizetype offset, MatchType matchType, MatchOptions matchOptions) const const
const QChar at(qsizetype position) const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
qsizetype indexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) const const
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
qsizetype size() const const
QByteArray toUtf8() const const
QString host(ComponentFormattingOptions options) const const
QString path(ComponentFormattingOptions options) const const
QString query(ComponentFormattingOptions options) const const
bool canConvert() const const
const void * constData() const const
int userType() const const