7#include <Cutelyst/context.h>
8#include <Cutelyst/response.h>
10#include <QtCore/QJsonDocument>
11#include <QtCore/QJsonObject>
14using namespace Qt::Literals::StringLiterals;
17 :
View(new ViewJsonPrivate, parent, name)
25 case QJsonDocument::Indented:
28 case QJsonDocument::Compact:
39 d->format = QJsonDocument::Indented;
42 d->format = QJsonDocument::Compact;
76 d->xJsonHeader = enable;
82 return d->xJsonHeader;
91 const QVariantHash stash = c->
stash();
93 switch (d->exposeMode) {
95 obj = QJsonObject::fromVariantHash(stash);
99 auto it = stash.constFind(d->exposeKey);
100 if (it != stash.constEnd()) {
101 obj.insert(d->exposeKey, QJsonValue::fromVariant(it.value()));
107 QVariantHash exposedStash;
109 auto it = stash.constBegin();
110 while (it != stash.constEnd()) {
111 const QString &key = it.key();
112 if (d->exposeKeys.contains(key)) {
113 exposedStash.insert(key, it.value());
117 obj = QJsonObject::fromVariantHash(exposedStash);
122 QVariantHash exposedStash;
123 QRegularExpression re = d->exposeRE;
125 auto it = stash.constBegin();
126 while (it != stash.constEnd()) {
127 const QString &key = it.key();
128 if (re.match(key).hasMatch()) {
129 exposedStash.insert(key, it.value());
133 obj = QJsonObject::fromVariantHash(exposedStash);
140 res->
setHeader(
"X-Json"_ba,
"eval(\"(\"+this.transport.responseText+\")\")"_ba);
145 return QJsonDocument(obj).toJson(d->format);
148#include "moc_viewjson.cpp"
void stash(const QVariantHash &unite)
Response * response() const noexcept
Headers headers() const noexcept
void setContentType(const QByteArray &type)
void setHeader(const QByteArray &key, const QByteArray &value)
A view that returns stash data in JSON format.
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)
Abstract View component for Cutelyst.
The Cutelyst namespace holds all public Cutelyst API.