cutelyst 3.9.1
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
22 : View(new ViewJsonPrivate, parent, name)
23{
24}
25
27{
28 Q_D(const ViewJson);
29 switch (d->format) {
31 return Indented;
32 break;
34 return Compact;
35 }
36 return Compact;
37}
38
40{
41 Q_D(ViewJson);
42 switch (format) {
43 case Indented:
44 d->format = QJsonDocument::Indented;
45 break;
46 case Compact:
47 d->format = QJsonDocument::Compact;
48 }
49}
50
52{
53 Q_D(const ViewJson);
54 return d->exposeMode;
55}
56
58{
59 Q_D(ViewJson);
60 d->exposeMode = ViewJson::String;
61 d->exposeKey = key;
62}
63
65{
66 Q_D(ViewJson);
67 d->exposeMode = ViewJson::StringList;
68 d->exposeKeys = keys;
69}
70
72{
73 Q_D(ViewJson);
74 d->exposeMode = ViewJson::RegularExpression;
75 d->exposeRE = re;
76}
77
78void ViewJson::setXJsonHeader(bool enable)
79{
80 Q_D(ViewJson);
81 d->xJsonHeader = enable;
82}
83
85{
86 Q_D(const ViewJson);
87 return d->xJsonHeader;
88}
89
91{
92 Q_D(const ViewJson);
93
94 QJsonObject obj;
95
96 const QVariantHash stash = c->stash();
97
98 switch (d->exposeMode) {
99 case All:
100 obj = QJsonObject::fromVariantHash(stash);
101 break;
102 case String:
103 {
104 auto it = stash.constFind(d->exposeKey);
105 if (it != stash.constEnd()) {
106 obj.insert(d->exposeKey, QJsonValue::fromVariant(it.value()));
107 }
108 break;
109 }
110 case StringList:
111 {
112 QVariantHash exposedStash;
113
114 auto it = stash.constBegin();
115 while (it != stash.constEnd()) {
116 const QString &key = it.key();
117 if (d->exposeKeys.contains(key)) {
118 exposedStash.insert(key, it.value());
119 }
120 ++it;
121 }
122 obj = QJsonObject::fromVariantHash(exposedStash);
123 break;
124 }
125 case RegularExpression:
126 {
127 QVariantHash exposedStash;
128 QRegularExpression re = d->exposeRE; // thread safety
129
130 auto it = stash.constBegin();
131 while (it != stash.constEnd()) {
132 const QString &key = it.key();
133 if (re.match(key).hasMatch()) {
134 exposedStash.insert(key, it.value());
135 }
136 ++it;
137 }
138 obj = QJsonObject::fromVariantHash(exposedStash);
139 break;
140 }
141 }
142
143 Response *res = c->response();
144 if (d->xJsonHeader && c->request()->headers().contains(QStringLiteral("X_PROTOTYPE_VERSION"))) {
145 res->setHeader(QStringLiteral("X_JSON"),
146 QStringLiteral("eval(\"(\"+this.transport.responseText+\")\")"));
147 }
148
149 res->setContentType(QStringLiteral("application/json"));
150
151 return QJsonDocument(obj).toJson(d->format);
152}
153
154#include "moc_viewjson.cpp"
QString name() const
Definition component.cpp:33
The Cutelyst Context.
Definition context.h:39
void stash(const QVariantHash &unite)
Definition context.cpp:566
Response * response() const noexcept
Definition context.cpp:96
bool contains(const QString &field) const
Definition headers.cpp:421
Headers headers() const noexcept
Definition request.cpp:310
void setHeader(const QString &field, const QString &value)
Definition response.cpp:328
void setContentType(const QString &type)
Definition response.h:220
void setOutputFormat(JsonFormat format)
Definition viewjson.cpp:39
void setExposeStash(const QString &key)
Definition viewjson.cpp:57
bool xJsonHeader() const
Definition viewjson.cpp:84
JsonFormat outputFormat() const
Definition viewjson.cpp:26
QByteArray render(Context *c) const final
Definition viewjson.cpp:90
ViewJson(QObject *parent, const QString &name=QString())
Definition viewjson.cpp:21
ExposeMode exposeStashMode() const
Definition viewjson.cpp:51
void setXJsonHeader(bool enable)
Definition viewjson.cpp:78
View(QObject *parent, const QString &name)
Definition view.cpp:18
The Cutelyst namespace holds all public Cutelyst API.
Definition Mainpage.dox:8
QByteArray toJson() const const
QJsonObject fromVariantHash(const QVariantHash &hash)
iterator insert(const QString &key, const QJsonValue &value)
QJsonValue fromVariant(const QVariant &variant)
QObject(QObject *parent)
QObject * parent() const const
QRegularExpressionMatch match(const QString &subject, int offset, MatchType matchType, MatchOptions matchOptions) const const
bool hasMatch() const const