Cutelyst  2.3.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(parent, name)
35  , d_ptr(new ViewJsonPrivate)
36 {
37 
38 }
39 
40 ViewJson::~ViewJson()
41 {
42  delete d_ptr;
43 }
44 
46 {
47  Q_D(const ViewJson);
48  switch (d->format) {
49  case QJsonDocument::Indented:
50  return Indented;
51  break;
52  case Compact:
53  return Compact;
54  }
55  return Compact;
56 }
57 
59 {
60  Q_D(ViewJson);
61  switch (format) {
62  case Indented:
63  d->format = QJsonDocument::Indented;
64  break;
65  case Compact:
66  d->format = QJsonDocument::Compact;
67  }
68 }
69 
71 {
72  Q_D(const ViewJson);
73  return d->exposeMode;
74 }
75 
76 void ViewJson::setExposeStash(const QString &key)
77 {
78  Q_D(ViewJson);
79  d->exposeMode = ViewJson::String;
80  d->exposeKey = key;
81 }
82 
83 void ViewJson::setExposeStash(const QStringList &keys)
84 {
85  Q_D(ViewJson);
86  d->exposeMode = ViewJson::StringList;
87  d->exposeKeys = keys;
88 }
89 
90 void ViewJson::setExposeStash(const QRegularExpression &re)
91 {
92  Q_D(ViewJson);
93  d->exposeMode = ViewJson::RegularExpression;
94  d->exposeRE = re;
95 }
96 
97 void ViewJson::setXJsonHeader(bool enable)
98 {
99  Q_D(ViewJson);
100  d->xJsonHeader = enable;
101 }
102 
104 {
105  Q_D(const ViewJson);
106  return d->xJsonHeader;
107 }
108 
109 QByteArray ViewJson::render(Context *c) const
110 {
111  Q_D(const ViewJson);
112 
113  QJsonObject obj;
114 
115  const QVariantHash stash = c->stash();
116 
117  switch (d->exposeMode) {
118  case All:
119  obj = QJsonObject::fromVariantHash(stash);
120  break;
121  case String:
122  {
123  auto it = stash.constFind(d->exposeKey);
124  if (it != stash.constEnd()) {
125  obj.insert(d->exposeKey, QJsonValue::fromVariant(it.value()));
126  }
127  break;
128  }
129  case StringList:
130  {
131  QVariantHash exposedStash;
132 
133  auto it = stash.constBegin();
134  while (it != stash.constEnd()) {
135  const QString &key = it.key();
136  if (d->exposeKeys.contains(key)) {
137  exposedStash.insertMulti(key, it.value());
138  }
139  ++it;
140  }
141  obj = QJsonObject::fromVariantHash(exposedStash);
142  break;
143  }
144  case RegularExpression:
145  {
146  QVariantHash exposedStash;
147  QRegularExpression re = d->exposeRE; // thread safety
148 
149  auto it = stash.constBegin();
150  while (it != stash.constEnd()) {
151  const QString &key = it.key();
152  if (re.match(key).hasMatch()) {
153  exposedStash.insertMulti(key, it.value());
154  }
155  ++it;
156  }
157  obj = QJsonObject::fromVariantHash(exposedStash);
158  break;
159  }
160  }
161 
162  Response *res = c->response();
163  if (d->xJsonHeader && c->request()->headers().contains(QStringLiteral("X_PROTOTYPE_VERSION"))) {
164  res->setHeader(QStringLiteral("X_JSON"), QStringLiteral("eval(\"(\"+this.transport.responseText+\")\")"));
165  }
166 
167  res->setContentType(QStringLiteral("application/json"));
168 
169  return QJsonDocument(obj).toJson(d->format);
170 }
171 
172 #include "moc_viewjson.cpp"
void setXJsonHeader(bool enable)
Definition: viewjson.cpp:97
void setContentType(const QString &type)
Definition: response.h:205
void setHeader(const QString &field, const QString &value)
Definition: response.cpp:280
void setExposeStash(const QString &key)
Definition: viewjson.cpp:76
The Cutelyst Context.
Definition: context.h:50
JsonFormat outputFormat() const
Definition: viewjson.cpp:45
ViewJson(QObject *parent, const QString &name=QString())
Definition: viewjson.cpp:34
bool xJsonHeader() const
Definition: viewjson.cpp:103
void setOutputFormat(JsonFormat format)
Definition: viewjson.cpp:58
Response * response() const
Definition: context.cpp:110
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
JSON view for your data.
Definition: viewjson.h:27
QByteArray render(Context *c) const final
Definition: viewjson.cpp:109
Cutelyst View abstract view component
Definition: view.h:33
ExposeMode exposeStashMode() const
Definition: viewjson.cpp:70
void stash(const QVariantHash &unite)
Definition: context.h:515