serializer.h
6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* This file is part of qjson
*
* Copyright (C) 2009 Till Adam <adam@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
*
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef QJSON_SERIALIZER_H
#define QJSON_SERIALIZER_H
#include "qjson_export.h"
QT_BEGIN_NAMESPACE
class QIODevice;
class QString;
class QVariant;
QT_END_NAMESPACE
namespace QJson {
/**
@brief How the indentation should work.
\verbatim
none (default) :
{ "foo" : 0, "foo1" : 1, "foo2" : [ { "bar" : 1, "foo" : 0, "foobar" : 0 }, { "bar" : 1, "foo" : 1, "foobar" : 1 } ], "foo3" : [ 1, 2, 3, 4, 5, 6 ] }
compact :
{"foo":0,"foo1":1,"foo2":[{"bar":1,"foo":0,"foobar":0},{"bar":1,"foo":1,"foobar":1}],"foo3":[1,2,3,4,5,6]}
minimum :
{ "foo" : 0, "foo1" : 1, "foo2" : [
{ "bar" : 1, "foo" : 0, "foobar" : 0 },
{ "bar" : 1, "foo" : 1, "foobar" : 1 }
], "foo3" : [
1,
2,
3,
4,
5,
6
] }
medium :
{
"foo" : 0, "foo1" : 1, "foo2" : [
{
"bar" : 1, "foo" : 0, "foobar" : 0
},
{
"bar" : 1, "foo" : 1, "foobar" : 1
}
], "foo3" : [
1,
2,
3,
4,
5,
6
]
}
full :
{
"foo" : 0,
"foo1" : 1,
"foo2" : [
{
"bar" : 1,
"foo" : 0,
"foobar" : 0
},
{
"bar" : 1,
"foo" : 1,
"foobar" : 1
}
],
"foo3" : [
1,
2,
3,
4,
5,
6
]
}
\endverbatim
*/
enum IndentMode {
IndentNone,
IndentCompact,
IndentMinimum,
IndentMedium,
IndentFull
};
/**
* @brief Main class used to convert QVariant objects to JSON data.
*
* QVariant objects are converted to a string containing the JSON data.
*
*
* Usage:
*
* \code
* QVariantList people;
*
* QVariantMap bob;
* bob.insert("Name", "Bob");
* bob.insert("Phonenumber", 123);
*
* QVariantMap alice;
* alice.insert("Name", "Alice");
* alice.insert("Phonenumber", 321);
*
* people << bob << alice;
*
* QJson::Serializer serializer;
* bool ok;
* QByteArray json = serializer.serialize(people, &ok);
*
* if (ok) {
* qDebug() << json;
* } else {
* qCritical() << "Something went wrong:" << serializer.errorMessage();
* }
* \endcode
*
* The output will be:
*
* \code
* "[ { "Name" : "Bob", "Phonenumber" : 123 },
* { "Name" : "Alice", "Phonenumber" : 321 } ]"
* \endcode
*
* It's possible to tune the indentation level of the resulting string. \sa setIndentMode
*/
class QJSON_EXPORT Serializer {
public:
Serializer();
~Serializer();
/**
* This method generates a textual JSON representation and outputs it to the
* passed in I/O Device.
* @param variant The JSON document in its in-memory representation as generated by the
* parser.
* @param out Input output device
* @param ok if a conversion error occurs, *ok is set to false; otherwise *ok is set to true
*/
void serialize( const QVariant& variant, QIODevice* out, bool* ok);
/**
* This is a method provided for convenience. It turns the passed in in-memory
* representation of the JSON document into a textual one, which is returned.
* If the returned string is empty, the document was empty. If it was null, there
* was a parsing error.
*
* @param variant The JSON document in its in-memory representation as generated by the
* parser.
*
* \deprecated This method is going to be removed with the next major release of QJson.
*/
QByteArray serialize( const QVariant& variant);
/**
* This is a method provided for convenience. It turns the passed in in-memory
* representation of the JSON document into a textual one, which is returned.
* If the returned string is empty, the document was empty. If it was null, there
* was a parsing error.
*
* @param variant The JSON document in its in-memory representation as generated by the
* parser.
* @param ok if a conversion error occurs, *ok is set to false; otherwise *ok is set to true
*/
QByteArray serialize( const QVariant& variant, bool *ok);
/**
* Allow or disallow writing of NaN and/or Infinity (as an extension to QJson)
*/
void allowSpecialNumbers(bool allow);
/**
* Is Nan and/or Infinity allowed?
*/
bool specialNumbersAllowed() const;
/**
* set output indentation mode as defined in QJson::IndentMode
*/
void setIndentMode(IndentMode mode = QJson::IndentNone);
/**
* set double precision used while converting Double
* \sa QByteArray::number
*/
void setDoublePrecision(int precision);
/**
* Returns one of the indentation modes defined in QJson::IndentMode
*/
IndentMode indentMode() const;
/**
* Returns the error message
*/
QString errorMessage() const;
private:
Q_DISABLE_COPY(Serializer)
class SerializerPrivate;
SerializerPrivate* const d;
};
}
#endif // QJSON_SERIALIZER_H