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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
|
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/
#include "json.h"
#ifdef TODO_USE_CREATOR
#include <utils/qtcassert.h>
#endif // TODO_USE_CREATOR
#include <QByteArray>
#include <QTextStream>
#include <QDebug>
#include <QStringList>
#include <QVariant>
#include <ctype.h>
#ifdef DEBUG_JASON
#define JDEBUG(s) qDebug() << s
#else
#define JDEBUG(s)
#endif
using namespace Json;
static void skipSpaces(const char *&from, const char *to)
{
while (from != to && isspace(*from))
++from;
}
QTextStream &operator<<(QTextStream &os, const JsonValue &mi)
{
return os << mi.toString();
}
void JsonValue::parsePair(const char *&from, const char *to)
{
skipSpaces(from, to);
JDEBUG("parsePair: " << QByteArray(from, to - from));
m_name = parseCString(from, to);
skipSpaces(from, to);
while (from < to && *from != ':') {
JDEBUG("not a colon" << *from);
++from;
}
++from;
parseValue(from, to);
skipSpaces(from, to);
}
QByteArray JsonValue::parseNumber(const char *&from, const char *to)
{
QByteArray result;
if (from < to && *from == '-') // Leading '-'.
result.append(*from++);
while (from < to && *from >= '0' && *from <= '9')
result.append(*from++);
return result;
}
QByteArray JsonValue::parseCString(const char *&from, const char *to)
{
QByteArray result;
const char * const fromSaved = from;
JDEBUG("parseCString: " << QByteArray(from, to - from));
if (*from != '"') {
qDebug() << "JSON Parse Error, double quote expected";
++from; // So we don't hang
return QByteArray();
}
const char *ptr = from;
++ptr;
while (ptr < to) {
if (*ptr == '"') {
++ptr;
result = QByteArray(from + 1, ptr - from - 2);
break;
}
if (*ptr == '\\') {
++ptr;
if (ptr == to) {
qWarning("JSON Parse Error, unterminated backslash escape in '%s'",
QByteArray(fromSaved, to - fromSaved).constData());
from = ptr; // So we don't hang
return QByteArray();
}
}
++ptr;
}
from = ptr;
int idx = result.indexOf('\\');
if (idx >= 0) {
char *dst = result.data() + idx;
const char *src = dst + 1, *end = result.data() + result.length();
do {
char c = *src++;
switch (c) {
case 'a': *dst++ = '\a'; break;
case 'b': *dst++ = '\b'; break;
case 'f': *dst++ = '\f'; break;
case 'n': *dst++ = '\n'; break;
case 'r': *dst++ = '\r'; break;
case 't': *dst++ = '\t'; break;
case 'v': *dst++ = '\v'; break;
case '"': *dst++ = '"'; break;
case '\\': *dst++ = '\\'; break;
case 'u': { // 4 digit hex escape as in '\u000a'
if (end - src < 4) {
qWarning("JSON Parse Error, too few hex digits in \\u-escape in '%s' obtained from '%s'",
result.constData(), QByteArray(fromSaved, to - fromSaved).constData());
return QByteArray();
}
bool ok;
const uchar prod = QByteArray(src, 4).toUInt(&ok, 16);
if (!ok) {
qWarning("JSON Parse Error, invalid hex digits in \\u-escape in '%s' obtained from '%s'",
result.constData(), QByteArray(fromSaved, to - fromSaved).constData());
return QByteArray();
}
*dst++ = prod;
src += 4;
}
break;
default: { // Up to 3 decimal digits: Not sure if this is supported in JSON?
int chars = 0;
uchar prod = 0;
forever {
if (c < '0' || c > '7') {
--src;
break;
}
prod = prod * 8 + c - '0';
if (++chars == 3 || src == end)
break;
c = *src++;
}
if (!chars) {
qWarning("JSON Parse Error, unrecognized backslash escape in string '%s' obtained from '%s'",
result.constData(), QByteArray(fromSaved, to - fromSaved).constData());
return QByteArray();
}
*dst++ = prod;
}
}
while (src != end) {
char c = *src++;
if (c == '\\')
break;
*dst++ = c;
}
} while (src != end);
*dst = 0;
result.truncate(dst - result.data());
}
JDEBUG("parseCString, got " << result);
return result;
}
void JsonValue::parseValue(const char *&from, const char *to)
{
JDEBUG("parseValue: " << QByteArray(from, to - from));
switch (*from) {
case '{':
parseObject(from, to);
break;
case 't':
if (to - from >= 4 && qstrncmp(from, "true", 4) == 0) {
m_data = QByteArray(from, 4);
from += m_data.size();
m_type = Boolean;
}
break;
case 'f':
if (to - from >= 5 && qstrncmp(from, "false", 5) == 0) {
m_data = QByteArray(from, 5);
from += m_data.size();
m_type = Boolean;
}
break;
case 'n':
if (to - from >= 4 && qstrncmp(from, "null", 4) == 0) {
m_data = QByteArray(from, 4);
from += m_data.size();
m_type = NullObject;
}
break;
case '[':
parseArray(from, to);
break;
case '"':
m_type = String;
m_data = parseCString(from, to);
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
case '-':
m_type = Number;
m_data = parseNumber(from, to);
default:
break;
}
}
void JsonValue::parseObject(const char *&from, const char *to)
{
JDEBUG("parseObject: " << QByteArray(from, to - from));
#ifdef TODO_USE_CREATOR
QTC_CHECK(*from == '{');
#endif
++from;
m_type = Object;
while (from < to) {
if (*from == '}') {
++from;
break;
}
JsonValue child;
child.parsePair(from, to);
if (!child.isValid())
return;
m_children += child;
if (*from == ',')
++from;
}
}
void JsonValue::parseArray(const char *&from, const char *to)
{
JDEBUG("parseArray: " << QByteArray(from, to - from));
#ifdef TODO_USE_CREATOR
QTC_CHECK(*from == '[');
#endif
++from;
m_type = Array;
while (from < to) {
if (*from == ']') {
++from;
break;
}
JsonValue child;
child.parseValue(from, to);
if (child.isValid())
m_children += child;
if (*from == ',')
++from;
}
}
void JsonValue::setStreamOutput(const QByteArray &name, const QByteArray &content)
{
if (content.isEmpty())
return;
JsonValue child;
child.m_type = String;
child.m_name = name;
child.m_data = content;
m_children += child;
if (m_type == Invalid)
m_type = Object;
}
static QByteArray ind(int indent)
{
return QByteArray(2 * indent, ' ');
}
void JsonValue::dumpChildren(QByteArray * str, bool multiline, int indent) const
{
for (int i = 0; i < m_children.size(); ++i) {
if (i != 0) {
*str += ',';
if (multiline)
*str += '\n';
}
if (multiline)
*str += ind(indent);
*str += m_children.at(i).toString(multiline, indent);
}
}
class MyString : public QString {
public:
ushort at(int i) const { return constData()[i].unicode(); }
};
template<class ST, typename CT>
inline ST escapeCStringTpl(const ST &ba)
{
ST ret;
ret.reserve(ba.length() * 2);
for (int i = 0; i < ba.length(); ++i) {
CT c = ba.at(i);
switch (c) {
case '\\': ret += "\\\\"; break;
case '\a': ret += "\\a"; break;
case '\b': ret += "\\b"; break;
case '\f': ret += "\\f"; break;
case '\n': ret += "\\n"; break;
case '\r': ret += "\\r"; break;
case '\t': ret += "\\t"; break;
case '\v': ret += "\\v"; break;
case '"': ret += "\\\""; break;
default:
if (c < 32 || c == 127) {
ret += '\\';
ret += '0' + (c >> 6);
ret += '0' + ((c >> 3) & 7);
ret += '0' + (c & 7);
} else {
ret += c;
}
}
}
return ret;
}
QString JsonValue::escapeCString(const QString &ba)
{
return escapeCStringTpl<MyString, ushort>(static_cast<const MyString &>(ba));
}
QByteArray JsonValue::escapeCString(const QByteArray &ba)
{
return escapeCStringTpl<QByteArray, uchar>(ba);
}
QByteArray JsonValue::toString(bool multiline, int indent) const
{
QByteArray result;
switch (m_type) {
case Invalid:
if (multiline)
result += ind(indent) + "Invalid\n";
else
result += "Invalid";
break;
case String:
if (!m_name.isEmpty())
result += m_name + '=';
result += '"' + escapeCString(m_data) + '"';
break;
case Number:
if (!m_name.isEmpty())
result += '"' + m_name + "\":";
result += m_data;
break;
case Boolean:
case NullObject:
if (!m_name.isEmpty())
result += '"' + m_name + "\":";
result += m_data;
break;
case Object:
if (!m_name.isEmpty())
result += m_name + '=';
if (multiline) {
result += "{\n";
dumpChildren(&result, multiline, indent + 1);
result += '\n' + ind(indent) + '}';
} else {
result += '{';
dumpChildren(&result, multiline, indent + 1);
result += '}';
}
break;
case Array:
if (!m_name.isEmpty())
result += m_name + '=';
if (multiline) {
result += "[\n";
dumpChildren(&result, multiline, indent + 1);
result += '\n' + ind(indent) + ']';
} else {
result += '[';
dumpChildren(&result, multiline, indent + 1);
result += ']';
}
break;
}
return result;
}
QVariant JsonValue::toVariant() const
{
switch (m_type) {
case String:
return QString(m_data);
case Number: {
bool ok;
qint64 val = QString(m_data).toLongLong(&ok);
if (ok)
return val;
return QVariant();
}
case Object: {
QHash<QString, QVariant> hash;
for (int i = 0; i < m_children.size(); ++i) {
QString name(m_children[i].name());
QVariant val = m_children[i].toVariant();
hash.insert(name, val);
}
return hash;
}
case Array: {
QList<QVariant> list;
for (int i = 0; i < m_children.size(); ++i) {
list.append(m_children[i].toVariant());
}
return list;
}
case Boolean:
return data() == QByteArray("true");
case Invalid:
case NullObject:
default:
return QVariant();
}
}
void JsonValue::fromString(const QByteArray &ba)
{
const char *from = ba.constBegin();
const char *to = ba.constEnd();
parseValue(from, to);
}
JsonValue JsonValue::findChild(const char *name) const
{
for (int i = 0; i < m_children.size(); ++i)
if (m_children.at(i).m_name == name)
return m_children.at(i);
return JsonValue();
}
void JsonInputStream::appendCString(const char *s)
{
m_target.append('"');
for (const char *p = s; *p; p++) {
if (*p == '"' || *p == '\\')
m_target.append('\\');
m_target.append(*p);
}
m_target.append('"');
}
void JsonInputStream::appendString(const QString &in)
{
if (in.isEmpty()) {
m_target.append("\"\"");
return;
}
const QChar doubleQuote('"');
const QChar backSlash('\\');
QString rc;
const int inSize = in.size();
rc.reserve(in.size() + 5);
rc.append(doubleQuote);
for (int i = 0; i < inSize; i++) {
const QChar c = in.at(i);
if (c == doubleQuote || c == backSlash)
rc.append(backSlash);
rc.append(c);
}
rc.append(doubleQuote);
m_target.append(rc.toUtf8());
return;
}
JsonInputStream &JsonInputStream::operator<<(const QStringList &in)
{
m_target.append('[');
const int count = in.size();
for (int i = 0 ; i < count; i++) {
if (i)
m_target.append(',');
appendString(in.at(i));
}
m_target.append(']');
return *this;
}
JsonInputStream &JsonInputStream::operator<<(const QVector<QByteArray> &ba)
{
m_target.append('[');
const int count = ba.size();
for (int i = 0 ; i < count; i++) {
if (i)
m_target.append(',');
appendCString(ba.at(i).constData());
}
m_target.append(']');
return *this;
}
JsonInputStream &JsonInputStream::operator<<(const QList<int> &in)
{
m_target.append('[');
const int count = in.size();
for (int i = 0 ; i < count; i++) {
if (i)
m_target.append(',');
m_target.append(QByteArray::number(in.at(i)));
}
m_target.append(']');
return *this;
}
JsonInputStream &JsonInputStream::operator<<(bool b)
{
m_target.append(b ? "true" : "false");
return *this;
}
|