cutelyst 4.3.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
viewjson.cpp
1/*
2 * SPDX-FileCopyrightText: (C) 2015-2022 Daniel Nicoletti <dantti12@gmail.com>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5#include "viewjson_p.h"
6
7#include <Cutelyst/context.h>
8#include <Cutelyst/response.h>
9
10#include <QtCore/QJsonDocument>
11#include <QtCore/QJsonObject>
12
13using namespace Cutelyst;
14
15ViewJson::ViewJson(QObject *parent, const QString &name)
16 : View(new ViewJsonPrivate, parent, name)
17{
18}
19
21{
22 Q_D(const ViewJson);
23 switch (d->format) {
25 return Indented;
26 break;
28 return Compact;
29 }
30 return Compact;
31}
32
34{
35 Q_D(ViewJson);
36 switch (format) {
37 case Indented:
38 d->format = QJsonDocument::Indented;
39 break;
40 case Compact:
41 d->format = QJsonDocument::Compact;
42 }
43}
44
46{
47 Q_D(const ViewJson);
48 return d->exposeMode;
49}
50
52{
53 Q_D(ViewJson);
54 d->exposeMode = ViewJson::String;
55 d->exposeKey = key;
56}
57
59{
60 Q_D(ViewJson);
61 d->exposeMode = ViewJson::StringList;
62 d->exposeKeys = keys;
63}
64
66{
67 Q_D(ViewJson);
68 d->exposeMode = ViewJson::RegularExpression;
69 d->exposeRE = re;
70}
71
72void ViewJson::setXJsonHeader(bool enable)
73{
74 Q_D(ViewJson);
75 d->xJsonHeader = enable;
76}
77
79{
80 Q_D(const ViewJson);
81 return d->xJsonHeader;
82}
83
85{
86 Q_D(const ViewJson);
87
88 QJsonObject obj;
89
90 const QVariantHash stash = c->stash();
91
92 switch (d->exposeMode) {
93 case All:
95 break;
96 case String:
97 {
98 auto it = stash.constFind(d->exposeKey);
99 if (it != stash.constEnd()) {
100 obj.insert(d->exposeKey, QJsonValue::fromVariant(it.value()));
101 }
102 break;
103 }
104 case StringList:
105 {
106 QVariantHash exposedStash;
107
108 auto it = stash.constBegin();
109 while (it != stash.constEnd()) {
110 const QString &key = it.key();
111 if (d->exposeKeys.contains(key)) {
112 exposedStash.insert(key, it.value());
113 }
114 ++it;
115 }
116 obj = QJsonObject::fromVariantHash(exposedStash);
117 break;
118 }
120 {
121 QVariantHash exposedStash;
122 QRegularExpression re = d->exposeRE; // thread safety
123
124 auto it = stash.constBegin();
125 while (it != stash.constEnd()) {
126 const QString &key = it.key();
127 if (re.match(key).hasMatch()) {
128 exposedStash.insert(key, it.value());
129 }
130 ++it;
131 }
132 obj = QJsonObject::fromVariantHash(exposedStash);
133 break;
134 }
135 }
136
137 Response *res = c->response();
138 if (d->xJsonHeader && c->request()->headers().contains("X-Prototype-Version")) {
139 res->setHeader("X-Json"_qba, "eval(\"(\"+this.transport.responseText+\")\")"_qba);
140 }
141
142 res->setContentType("application/json"_qba);
143
144 return QJsonDocument(obj).toJson(d->format);
145}
146
147#include "moc_viewjson.cpp"
The Cutelyst Context.
Definition context.h:42
void stash(const QVariantHash &unite)
Definition context.cpp:562
Request * request
Definition context.h:71
Response * response() const noexcept
Definition context.cpp:97
bool contains(QByteArrayView key) const noexcept
Definition headers.cpp:474
Headers headers() const noexcept
Definition request.cpp:312
A Cutelyst response.
Definition response.h:29
void setContentType(const QByteArray &type)
Definition response.h:238
void setHeader(const QByteArray &key, const QByteArray &value)
A view that returns stash data in JSON format.
Definition viewjson.h:26
void setOutputFormat(JsonFormat format)
Definition viewjson.cpp:33
void setExposeStash(const QString &key)
Definition viewjson.cpp:51
bool xJsonHeader() const
Definition viewjson.cpp:78
JsonFormat outputFormat() const
Definition viewjson.cpp:20
QByteArray render(Context *c) const final
Definition viewjson.cpp:84
ViewJson(QObject *parent, const QString &name=QString())
Definition viewjson.cpp:15
ExposeMode exposeStashMode() const
Definition viewjson.cpp:45
void setXJsonHeader(bool enable)
Definition viewjson.cpp:72
Abstract View component for Cutelyst.
Definition view.h:25
The Cutelyst namespace holds all public Cutelyst API.
QByteArray toJson(QJsonDocument::JsonFormat format) const const
QJsonObject fromVariantHash(const QVariantHash &hash)
QJsonObject::iterator insert(QLatin1StringView key, const QJsonValue &value)
QJsonValue fromVariant(const QVariant &variant)
QRegularExpressionMatch match(QStringView subjectView, qsizetype offset, QRegularExpression::MatchType matchType, QRegularExpression::MatchOptions matchOptions) const const
bool hasMatch() const const
QString & insert(qsizetype position, QChar ch)