Cutelyst  1.11.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 void ViewJson::setExposeStashString(const QString &key)
110 {
111  setExposeStash(key);
112 }
113 
114 void ViewJson::setExposeStashStringList(const QStringList &keys)
115 {
116  setExposeStash(keys);
117 }
118 
119 void ViewJson::setExposeStashRegularExpression(const QRegularExpression &re)
120 {
121  setExposeStash(re);
122 }
123 
124 QByteArray ViewJson::render(Context *c) const
125 {
126  Q_D(const ViewJson);
127 
128  QJsonObject obj;
129 
130  const QVariantHash stash = c->stash();
131 
132  switch (d->exposeMode) {
133  case All:
134  obj = QJsonObject::fromVariantHash(stash);
135  break;
136  case String:
137  {
138  auto it = stash.constFind(d->exposeKey);
139  if (it != stash.constEnd()) {
140  obj.insert(d->exposeKey, QJsonValue::fromVariant(it.value()));
141  }
142  break;
143  }
144  case StringList:
145  {
146  QVariantHash exposedStash;
147 
148  auto it = stash.constBegin();
149  while (it != stash.constEnd()) {
150  const QString key = it.key();
151  if (d->exposeKeys.contains(key)) {
152  exposedStash.insertMulti(it.key(), it.value());
153  }
154  ++it;
155  }
156  obj = QJsonObject::fromVariantHash(exposedStash);
157  break;
158  }
159  case RegularExpression:
160  {
161  QVariantHash exposedStash;
162  QRegularExpression re = d->exposeRE; // thread safety
163 
164  auto it = stash.constBegin();
165  while (it != stash.constEnd()) {
166  const QString key = it.key();
167  if (re.match(key).hasMatch()) {
168  exposedStash.insertMulti(key, it.value());
169  }
170  ++it;
171  }
172  obj = QJsonObject::fromVariantHash(exposedStash);
173  break;
174  }
175  }
176 
177  Response *res = c->response();
178  if (d->xJsonHeader && c->request()->headers().contains(QStringLiteral("X_PROTOTYPE_VERSION"))) {
179  res->setHeader(QStringLiteral("X_JSON"), QStringLiteral("eval(\"(\"+this.transport.responseText+\")\")"));
180  }
181 
182  res->setContentType(QStringLiteral("application/json"));
183 
184  return QJsonDocument(obj).toJson(d->format);
185 }
186 
187 #include "moc_viewjson.cpp"
void setXJsonHeader(bool enable)
Definition: viewjson.cpp:97
Q_DECL_DEPRECATED void setExposeStashStringList(const QStringList &keys)
Definition: viewjson.cpp:114
bool xJsonHeader() const
Definition: viewjson.cpp:103
void setContentType(const QString &type)
Definition: response.h:205
void setHeader(const QString &field, const QString &value)
Definition: response.cpp:287
void setExposeStash(const QString &key)
Definition: viewjson.cpp:76
The Cutelyst Context.
Definition: context.h:50
ViewJson(QObject *parent, const QString &name=QString())
Definition: viewjson.cpp:34
void setOutputFormat(JsonFormat format)
Definition: viewjson.cpp:58
Q_DECL_DEPRECATED void setExposeStashRegularExpression(const QRegularExpression &re)
Definition: viewjson.cpp:119
ExposeMode exposeStashMode() const
Definition: viewjson.cpp:70
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
JsonFormat outputFormat() const
Definition: viewjson.cpp:45
JSON view for your data.
Definition: viewjson.h:27
Response * response() const
Definition: context.cpp:111
QByteArray render(Context *c) const final
Definition: viewjson.cpp:124
Cutelyst View abstract view component
Definition: view.h:33
Q_DECL_DEPRECATED void setExposeStashString(const QString &key)
Definition: viewjson.cpp:109
void stash(const QVariantHash &unite)
Definition: context.h:499