Cutelyst  2.13.0
viewjson.cpp
1 /*
2  * Copyright (C) 2015-2017 Daniel Nicoletti <dantti12@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 #include "viewjson_p.h"
19 
20 #include <Cutelyst/context.h>
21 #include <Cutelyst/response.h>
22 
23 #include <QtCore/QJsonDocument>
24 #include <QtCore/QJsonObject>
25 
26 using namespace Cutelyst;
27 
34 ViewJson::ViewJson(QObject *parent, const QString &name) : View(new ViewJsonPrivate, parent, name)
35 {
36 
37 }
38 
40 {
41  Q_D(const ViewJson);
42  switch (d->format) {
43  case QJsonDocument::Indented:
44  return Indented;
45  break;
46  case Compact:
47  return Compact;
48  }
49  return Compact;
50 }
51 
53 {
54  Q_D(ViewJson);
55  switch (format) {
56  case Indented:
57  d->format = QJsonDocument::Indented;
58  break;
59  case Compact:
60  d->format = QJsonDocument::Compact;
61  }
62 }
63 
65 {
66  Q_D(const ViewJson);
67  return d->exposeMode;
68 }
69 
70 void ViewJson::setExposeStash(const QString &key)
71 {
72  Q_D(ViewJson);
73  d->exposeMode = ViewJson::String;
74  d->exposeKey = key;
75 }
76 
77 void ViewJson::setExposeStash(const QStringList &keys)
78 {
79  Q_D(ViewJson);
80  d->exposeMode = ViewJson::StringList;
81  d->exposeKeys = keys;
82 }
83 
84 void ViewJson::setExposeStash(const QRegularExpression &re)
85 {
86  Q_D(ViewJson);
87  d->exposeMode = ViewJson::RegularExpression;
88  d->exposeRE = re;
89 }
90 
91 void ViewJson::setXJsonHeader(bool enable)
92 {
93  Q_D(ViewJson);
94  d->xJsonHeader = enable;
95 }
96 
98 {
99  Q_D(const ViewJson);
100  return d->xJsonHeader;
101 }
102 
103 QByteArray ViewJson::render(Context *c) const
104 {
105  Q_D(const ViewJson);
106 
107  QJsonObject obj;
108 
109  const QVariantHash stash = c->stash();
110 
111  switch (d->exposeMode) {
112  case All:
113  obj = QJsonObject::fromVariantHash(stash);
114  break;
115  case String:
116  {
117  auto it = stash.constFind(d->exposeKey);
118  if (it != stash.constEnd()) {
119  obj.insert(d->exposeKey, QJsonValue::fromVariant(it.value()));
120  }
121  break;
122  }
123  case StringList:
124  {
125  QVariantHash exposedStash;
126 
127  auto it = stash.constBegin();
128  while (it != stash.constEnd()) {
129  const QString &key = it.key();
130  if (d->exposeKeys.contains(key)) {
131  exposedStash.insertMulti(key, it.value());
132  }
133  ++it;
134  }
135  obj = QJsonObject::fromVariantHash(exposedStash);
136  break;
137  }
138  case RegularExpression:
139  {
140  QVariantHash exposedStash;
141  QRegularExpression re = d->exposeRE; // thread safety
142 
143  auto it = stash.constBegin();
144  while (it != stash.constEnd()) {
145  const QString &key = it.key();
146  if (re.match(key).hasMatch()) {
147  exposedStash.insertMulti(key, it.value());
148  }
149  ++it;
150  }
151  obj = QJsonObject::fromVariantHash(exposedStash);
152  break;
153  }
154  }
155 
156  Response *res = c->response();
157  if (d->xJsonHeader && c->request()->headers().contains(QStringLiteral("X_PROTOTYPE_VERSION"))) {
158  res->setHeader(QStringLiteral("X_JSON"), QStringLiteral("eval(\"(\"+this.transport.responseText+\")\")"));
159  }
160 
161  res->setContentType(QStringLiteral("application/json"));
162 
163  return QJsonDocument(obj).toJson(d->format);
164 }
165 
166 #include "moc_viewjson.cpp"
Cutelyst::ViewJson::ExposeMode
ExposeMode
Definition: viewjson.h:56
Cutelyst::Context
The Cutelyst Context.
Definition: context.h:50
Cutelyst::ViewJson::xJsonHeader
bool xJsonHeader() const
Definition: viewjson.cpp:97
Cutelyst::Context::response
Response * response() const
Definition: context.cpp:110
Cutelyst::ViewJson::exposeStashMode
ExposeMode exposeStashMode() const
Definition: viewjson.cpp:64
Cutelyst::ViewJson::Compact
@ Compact
Definition: viewjson.h:40
Cutelyst::Response::setContentType
void setContentType(const QString &type)
Definition: response.h:218
Cutelyst::ViewJson::Indented
@ Indented
Definition: viewjson.h:39
Cutelyst::ViewJson::render
QByteArray render(Context *c) const final
Definition: viewjson.cpp:103
Cutelyst::ViewJson::setExposeStash
void setExposeStash(const QString &key)
Definition: viewjson.cpp:70
Cutelyst::ViewJson::setXJsonHeader
void setXJsonHeader(bool enable)
Definition: viewjson.cpp:91
Cutelyst::Response::setHeader
void setHeader(const QString &field, const QString &value)
Definition: response.cpp:306
Cutelyst::ViewJson
JSON view for your data.
Definition: viewjson.h:27
Cutelyst::ViewJson::outputFormat
JsonFormat outputFormat() const
Definition: viewjson.cpp:39
Cutelyst::ViewJson::JsonFormat
JsonFormat
Definition: viewjson.h:38
Cutelyst::View
Cutelyst View abstract view component
Definition: view.h:34
Cutelyst::ViewJson::ViewJson
ViewJson(QObject *parent, const QString &name=QString())
Definition: viewjson.cpp:34
Cutelyst
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
Cutelyst::ViewJson::setOutputFormat
void setOutputFormat(JsonFormat format)
Definition: viewjson.cpp:52
Cutelyst::Response
Definition: response.h:34
Cutelyst::Context::stash
void stash(const QVariantHash &unite)
Definition: context.h:558