summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/qjson/src/json.cpp
blob: dba777999b2626e2864021cc418eaa3e2eadd5c1 (plain)
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/****************************************************************************
**
** Copyright (c) 2010 Girish Ramakrishnan <girish@forwardbias.in>
**
** Use, modification and distribution is allowed without limitation,
** warranty, liability or support of any kind.
**
****************************************************************************/

#include "json.h"
#include "jsonparser.cpp"

#include <QTextCodec>
#include <qnumeric.h>

/*!
  \class JsonReader
  \internal
  \reentrant

  \brief The JsonReader class provides a fast parser for reading
  well-formed JSON into a QVariant.

  The parser converts JSON types into QVariant types. For example, JSON
  arrays are translated into QVariantList and JSON objects are translated 
  into QVariantMap. For example,
  \code
  JsonReader reader;
  if (reader.parse("{ \"id\": 123, \"company\": \"Nokia\", \"authors\": [\"Denis\",\"Ettrich\",\"Girish\"])) {
    QVariant result = reader.result();
    QVariantMap map = result.toMap(); // the JSON object
    qDebug() << map.count(); // 3
    qDebug() << map["id"].toInt(); // 123
    qDebug() << map["company"].toString(); // "Nokia"
    QVariantList list = map["authors"].toList();
    qDebug() << list[1].toString(); // "Girish"
  } else {
    qDebug() << reader.errorString();
  }
  \endcode

  As seen above, the reader converts the JSON into a QVariant with arbitrary nesting.
  A complete listing of JSON to QVariant conversion is documented at parse().

  JsonWriter can be used to convert a QVariant into JSON string.
*/

/*!
  Constructs a JSON reader.
 */
JsonReader::JsonReader()
{
}

/*!
  Destructor
 */
JsonReader::~JsonReader()
{
}

/*!
  Parses the JSON \a ba as a QVariant.

  If the parse succeeds, this function returns true and the QVariant can
  be accessed using result(). If the parse fails, this function returns
  false and the error message can be accessed using errorMessage().

  The encoding of \ba is auto-detected based on the pattern of nulls in the
  initial 4 octets as described in "Section 3. Encoding" of RFC 2647. If an 
  encoding could not be auto-detected, this function assumes UTF-8.

  The conversion from JSON type into QVariant type is as listed below:
  \table
  \row
  \li false
  \li QVariant::Bool with the value false.
  \row
  \li true
  \li QVariant::Bool with the value true.
  \row
  \li null
  \li QVariant::Invalid i.e QVariant()
  \row
  \li object
  \li QVariant::Map i.e QVariantMap
  \row
  \li array
  \li QVariant::List i.e QVariantList
  \row
  \li string
  \li QVariant::String i.e QString
  \row
  \li number
  \li QVariant::Double or QVariant::LongLong. If the JSON number contains a '.' or 'e' 
     or 'E', QVariant::Double is used.
  \endtable

  The byte array \ba may or may not contain a BOM.
 */
bool JsonReader::parse(const QByteArray &ba)
{
    int mib = 106; // utf-8

    QTextCodec *codec = QTextCodec::codecForUtfText(ba, 0); // try BOM detection
    if (!codec) {
        if (ba.length() > 3) { // auto-detect
            const char *data = ba.constData();
            if (data[0] != 0) {
                if (data[1] != 0)
                    mib = 106; // utf-8
                else if (data[2] != 0)
                    mib = 1014; // utf16 le
                else
                    mib = 1019; // utf32 le
            } else if (data[1] != 0)
                mib = 1013; // utf16 be
            else
                mib = 1018; // utf32 be
        }
        codec = QTextCodec::codecForMib(mib);
    }
    QString str = codec->toUnicode(ba);
    return parse(str);
}

/*!
  Parses the JSON string \a str as a QVariant.

  If the parse succeeds, this function returns true and the QVariant can
  be accessed using result(). If the parse fails, this function returns
  false and the error message can be accessed using errorMessage().
 */
bool JsonReader::parse(const QString &str)
{
    JsonLexer lexer(str);
    JsonParser parser;
    if (!parser.parse(&lexer)) {
        m_errorString = parser.errorMessage();
        m_result = QVariant();
        return false;
    }
    m_errorString.clear();
    m_result = parser.result();
    return true;
}

/*!
  Returns the result of the last parse() call.

  If parse() failed, this function returns an invalid QVariant.
 */
QVariant JsonReader::result() const
{
    return m_result;
}

/*!
  Returns the error message for the last parse() call.

  If parse() succeeded, this functions return an empty string. The error message
  should be used for debugging purposes only.
 */
QString JsonReader::errorString() const
{
    return m_errorString;
}

/*!
  \class JsonWriter
  \internal
  \reentrant

  \brief The JsonWriter class converts a QVariant into a JSON string.

  The writer converts specific supported types stored in a QVariant into JSON.
  For example,
  \code
    QVariant v;
    QVariantMap map;
    map["id"] = 123;
    map["company"] = "Nokia";
    QVariantList list;
    list << "Denis" << "Ettrich" << "Girish";
    map["authors"] = list;

    JsonWriter writer;
    QString json = writer.toString(v);
    qDebug() << json; // {"authors": ["Denis", "Ettrich", "Girish"], "company": "Nokia", "id": 123 }
  \endcode

  The list of QVariant types that the writer supports is listed in toString(). Note that
  custom C++ types registered using Q_DECLARE_METATYPE are not supported.
*/

/*!
  Creates a JsonWriter.
 */
JsonWriter::JsonWriter()
    : m_autoFormatting(false), m_autoFormattingIndent(4, QLatin1Char(' '))
{
}

/*!
  Destructor.
 */
JsonWriter::~JsonWriter()
{
}

/*!
  Enables auto formatting if \a enable is \c true, otherwise
  disables it.
 
  When auto formatting is enabled, the writer automatically inserts
  spaces and new lines to make the output more human readable.
 
  The default value is \c false.
 */
void JsonWriter::setAutoFormatting(bool enable)
{
    m_autoFormatting = enable;
}

/*!
  Returns \c true if auto formattting is enabled, otherwise \c false.
 */
bool JsonWriter::autoFormatting() const
{
    return m_autoFormatting;
}

/*!
  Sets the number of spaces or tabs used for indentation when
  auto-formatting is enabled. Positive numbers indicate spaces,
  negative numbers tabs.

  The default indentation is 4.

  \sa setAutoFormatting()
*/
void JsonWriter::setAutoFormattingIndent(int spacesOrTabs)
{
    m_autoFormattingIndent = QString(qAbs(spacesOrTabs), QLatin1Char(spacesOrTabs >= 0 ? ' ' : '\t'));
}

/*!
  Retuns the numbers of spaces or tabs used for indentation when
  auto-formatting is enabled. Positive numbers indicate spaces,
  negative numbers tabs.

  The default indentation is 4.

  \sa setAutoFormatting()
*/
int JsonWriter::autoFormattingIndent() const
{
    return m_autoFormattingIndent.count(QLatin1Char(' ')) - m_autoFormattingIndent.count(QLatin1Char('\t'));
}

/*! \internal
  Inserts escape character \ for characters in string as described in JSON specification.
 */
static QString escape(const QVariant &variant)
{
    QString str = variant.toString();
    QString res;
    res.reserve(str.length());
    for (int i = 0; i < str.length(); i++) {
        if (str[i] == QLatin1Char('\b')) {
            res += QLatin1String("\\b");
        } else if (str[i] == QLatin1Char('\f')) {
            res += QLatin1String("\\f");
        } else if (str[i] == QLatin1Char('\n')) {
            res += QLatin1String("\\n");
        } else if (str[i] == QLatin1Char('\r')) {
            res += QLatin1String("\\r");
        } else if (str[i] == QLatin1Char('\t')) {
            res += QLatin1String("\\t");
        } else if (str[i] == QLatin1Char('\"')) {
            res += QLatin1String("\\\"");
        } else if (str[i] == QLatin1Char('\\')) {
            res += QLatin1String("\\\\");
        } else if (str[i] == QLatin1Char('/')) {
            res += QLatin1String("\\/");
        } else if (str[i].unicode() > 127) {
            res += QLatin1String("\\u") + QString::number(str[i].unicode(), 16).rightJustified(4, QLatin1Char('0'));
        } else {
            res += str[i];
        }
    }
    return res;
}

/*! \internal
  Stringifies \a variant.
 */
QString JsonWriter::stringify(const QVariant &variant, int depth)
{
    QString result;
    if (variant.type() == QVariant::List || variant.type() == QVariant::StringList) {
        result += QLatin1Char('[');
        QVariantList list = variant.toList();
        for (int i = 0; i < list.count(); i++) {
            if (i != 0) {
                result += QLatin1Char(',');
                if (m_autoFormatting)
                    result += QLatin1Char(' ');
            }
            result += stringify(list[i], depth+1);
        }
        result += QLatin1Char(']');
    } else if (variant.type() == QVariant::Map) {
        QString indent = m_autoFormattingIndent.repeated(depth);
        QVariantMap map = variant.toMap();
        if (m_autoFormatting && depth != 0) {
            result += QLatin1Char('\n');
            result += indent;
            result += QLatin1String("{\n");
        } else {
            result += QLatin1Char('{');
        }
        for (QVariantMap::const_iterator it = map.constBegin(); it != map.constEnd(); ++it) {
            if (it != map.constBegin()) {
                result += QLatin1Char(',');
                if (m_autoFormatting)
                    result += QLatin1Char('\n');
            }
            if (m_autoFormatting)
                result += indent + QLatin1Char(' ');
            result += QLatin1Char('\"') + escape(it.key()) + QLatin1String("\":");
            result += stringify(it.value(), depth+1);
        }
        if (m_autoFormatting) {
            result += QLatin1Char('\n');
            result += indent;
        }
        result += QLatin1Char('}');
    } else if (variant.type() == QVariant::String || variant.type() == QVariant::ByteArray) {
        result = QLatin1Char('\"') + escape(variant) + QLatin1Char('\"');
    } else if (variant.type() == QVariant::Double || (int)variant.type() == (int)QMetaType::Float) {
        double d = variant.toDouble();
        if (qIsFinite(d))
            result.setNum(variant.toDouble(), 'g', 15);
        else
            result = QLatin1String("null");
    } else if (variant.type() == QVariant::Bool) {
        result = variant.toBool() ? QLatin1String("true") : QLatin1String("false");
    } else if (variant.type() == QVariant::Invalid) {
        result = QLatin1String("null");
    } else if (variant.type() == QVariant::ULongLong) {
        result = QString::number(variant.toULongLong());
    } else if (variant.type() == QVariant::LongLong) {
        result = QString::number(variant.toLongLong());
    } else if (variant.type() == QVariant::Int) {
        result = QString::number(variant.toInt());
    } else if (variant.type() == QVariant::UInt) {
        result = QString::number(variant.toUInt());
    } else if (variant.type() == QVariant::Char) {
        QChar c = variant.toChar();
        if (c.unicode() > 127)
            result = QLatin1String("\"\\u") + QString::number(c.unicode(), 16).rightJustified(4, QLatin1Char('0')) + QLatin1Char('\"');
        else
            result = QLatin1Char('\"') + c + QLatin1Char('\"');
    } else if (variant.canConvert<qlonglong>()) {
        result = QString::number(variant.toLongLong());
    } else if (variant.canConvert<QString>()) {
        result = QLatin1Char('\"') + escape(variant) + QLatin1Char('\"');
    } else {
        result = QLatin1String("null");
    }

    return result;
}

/*!
  Converts the variant \a var into a JSON string.

  The stringizer converts \a var into JSON based on the type of it's contents. The supported
  types and their conversion into JSON is as listed below:

  \table
  \row
  \li QVariant::List, QVariant::StringList
  \li JSON array []
  \row
  \li QVariant::Map
  \li JSON object {}
  \row
  \li QVariant::String, QVariant::ByteArray
  \li JSON string encapsulated in double quotes. String contents are escaped using '\' if necessary.
  \row
  \li QVariant::Double, QMetaType::Float
  \li JSON number with a precision 15. Infinity and NaN are converted into null.
  \row
  \li QVariant::Bool
  \li JSON boolean true and false
  \row
  \li QVariant::Invalid
  \li JSON null
  \row
  \li QVariant::ULongLong, QVariant::LongLong, QVariant::Int, QVariant::UInt, 
  \li JSON number
  \row
  \li QVariant::Char
  \li JSON string. Non-ASCII characters are converted into the \uXXXX notation.
  \endtable

  As a fallback, the writer attempts to convert a type not listed above into a long long or a
  QString using QVariant::canConvert. See the QVariant documentation for possible conversions.

  JsonWriter does not support stringizing custom user types stored in the QVariant. Any such
  value would be converted into JSON null.
 */
QString JsonWriter::toString(const QVariant &var)
{
    return stringify(var);
}

/*!
  Converts the variant \a var into a JSON string.

  The returned QByteArray is UTF-8 encoded and does not contain a BOM. The list of supported
  C++ types is documented in toString().

  \sa toString()
 */
QByteArray JsonWriter::toByteArray(const QVariant &var)
{
    return stringify(var).toUtf8();
}