Torc  0.1
torcjsonserialiser.cpp
Go to the documentation of this file.
1 /* Class TorcJSONSerialiser
2 *
3 * This file is part of the Torc project.
4 *
5 * Copyright (C) Mark Kendall 2012-18
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 * USA.
21 */
22 
23 // Qt
24 #include <QJsonDocument>
25 
26 // Torc
27 #include "torcjsonserialiser.h"
28 
30  : TorcSerialiser(),
31  m_javaScriptType(Javascript)
32 {
33 }
34 
36 {
37  return m_javaScriptType ? HTTPResponseJSONJavascript : HTTPResponseJSON;
38 }
39 
40 void TorcJSONSerialiser::Prepare(QByteArray &)
41 {
42 }
43 
44 void TorcJSONSerialiser::Begin(QByteArray &)
45 {
46 }
47 
48 void TorcJSONSerialiser::AddProperty(QByteArray &Dest, const QString &Name, const QVariant &Value)
49 {
50  if (Name.isEmpty())
51  {
52  Dest = QByteArray(QJsonDocument::fromVariant(Value).toJson(QJsonDocument::Compact));
53  }
54  else
55  {
56  QVariantMap map;
57  map.insert(Name, Value);
58  Dest = QByteArray(QJsonDocument::fromVariant(map).toJson(QJsonDocument::Compact));
59  }
60 }
61 
62 void TorcJSONSerialiser::End(QByteArray &)
63 {
64 }
65 
67 {
68  public:
69  TorcJSONSerialiserFactory() : TorcSerialiserFactory(QStringLiteral("application"), QStringLiteral("json"), QStringLiteral("JSON"))
70  {
71  }
72 
74  {
75  return new TorcJSONSerialiser();
76  }
78 
80 {
81  public:
82  TorcJavascriptSerialiserFactory() : TorcSerialiserFactory(QStringLiteral("text"), QStringLiteral("javascript"), QStringLiteral("JSON"))
83  {
84  }
85 
87  {
88  return new TorcJSONSerialiser(true /*javascript*/);
89  }
void Prepare(QByteArray &) override
TorcSerialiser * Create(void)
TorcJavascriptSerialiserFactory TorcJavascriptSerialiserFactory
TorcJSONSerialiserFactory TorcJSONSerialiserFactory
HTTPResponseType
void Begin(QByteArray &) override
HTTPResponseType ResponseType(void) override
void End(QByteArray &) override
TorcJSONSerialiser(bool Javascript=false)
void AddProperty(QByteArray &Dest, const QString &Name, const QVariant &Value) override