7#include <Cutelyst/context.h>
8#include <Cutelyst/response.h>
10#include <QtCore/QJsonDocument>
11#include <QtCore/QJsonObject>
22 :
View(new ViewJsonPrivate, parent, name)
30 case QJsonDocument::Indented:
33 case QJsonDocument::Compact:
44 d->format = QJsonDocument::Indented;
47 d->format = QJsonDocument::Compact;
60 d->exposeMode = ViewJson::String;
67 d->exposeMode = ViewJson::StringList;
74 d->exposeMode = ViewJson::RegularExpression;
81 d->xJsonHeader = enable;
87 return d->xJsonHeader;
96 const QVariantHash stash = c->
stash();
98 switch (d->exposeMode) {
100 obj = QJsonObject::fromVariantHash(stash);
104 auto it = stash.constFind(d->exposeKey);
105 if (it != stash.constEnd()) {
106 obj.insert(d->exposeKey, QJsonValue::fromVariant(it.value()));
112 QVariantHash exposedStash;
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());
122 obj = QJsonObject::fromVariantHash(exposedStash);
125 case RegularExpression:
127 QVariantHash exposedStash;
128 QRegularExpression re = d->exposeRE;
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());
138 obj = QJsonObject::fromVariantHash(exposedStash);
144 if (d->xJsonHeader && c->request()->
headers().
contains(QStringLiteral(
"X_PROTOTYPE_VERSION"))) {
146 QStringLiteral(
"eval(\"(\"+this.transport.responseText+\")\")"));
151 return QJsonDocument(obj).toJson(d->format);
154#include "moc_viewjson.cpp"
void stash(const QVariantHash &unite)
Response * response() const noexcept
Headers headers() const noexcept
void setHeader(const QString &field, const QString &value)
void setContentType(const QString &type)
void setOutputFormat(JsonFormat format)
void setExposeStash(const QString &key)
JsonFormat outputFormat() const
QByteArray render(Context *c) const final
ViewJson(QObject *parent, const QString &name=QString())
ExposeMode exposeStashMode() const
void setXJsonHeader(bool enable)
Cutelyst View abstract view component
The Cutelyst namespace holds all public Cutelyst API.