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 
13 using namespace Cutelyst;
14 
21 ViewJson::ViewJson(QObject *parent, const QString &name)
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 
78 void 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"
void setXJsonHeader(bool enable)
Definition: viewjson.cpp:78
QRegularExpressionMatch match(const QString &subject, int offset, QRegularExpression::MatchType matchType, QRegularExpression::MatchOptions matchOptions) const const
void setContentType(const QString &type)
Definition: response.h:220
QByteArray toJson() const const
void setHeader(const QString &field, const QString &value)
QJsonObject fromVariantHash(const QVariantHash &hash)
QJsonValue fromVariant(const QVariant &variant)
void setExposeStash(const QString &key)
Definition: viewjson.cpp:57
The Cutelyst Context.
Definition: context.h:38
JsonFormat outputFormat() const
Definition: viewjson.cpp:26
QString & insert(int position, QChar ch)
ViewJson(QObject *parent, const QString &name=QString())
Definition: viewjson.cpp:21
void stash(const QVariantHash &unite)
Definition: context.cpp:566
Headers headers() const noexcept
Definition: request.cpp:310
bool xJsonHeader() const
Definition: viewjson.cpp:84
void setOutputFormat(JsonFormat format)
Definition: viewjson.cpp:39
bool hasMatch() const const
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
JSON view for your data.
Definition: viewjson.h:14
QByteArray render(Context *c) const final
Definition: viewjson.cpp:90
Cutelyst View abstract view component
Definition: view.h:21
bool contains(const QString &field) const
Definition: headers.cpp:421
ExposeMode exposeStashMode() const
Definition: viewjson.cpp:51
Response * response() const noexcept
Definition: context.cpp:96
QJsonObject::iterator insert(const QString &key, const QJsonValue &value)