qjsonwriter.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/corelib/json/qjsonwriter.cpp
Switch to Source codePreprocessed file
LineSourceCount
1-
2-
3-
4using namespace QJsonPrivate;-
5-
6static void objectContentToJson(const QJsonPrivate::Object *o, QByteArray &json, int indent, bool compact);-
7static void arrayContentToJson(const QJsonPrivate::Array *a, QByteArray &json, int indent, bool compact);-
8-
9static inline uchar hexdig(uint u)-
10{-
11 return
never executed: return (u < 0xa ? '0' + u : 'a' + u - 0xa);
(u < 0xa ? '0' + u : 'a' + u - 0xa);
never executed: return (u < 0xa ? '0' + u : 'a' + u - 0xa);
0
12}-
13-
14static QByteArray escapedString(const QString &s)-
15{-
16 const uchar replacement = '?';-
17 QByteArray ba(s.length(), Qt::Uninitialized);-
18-
19 uchar *cursor = reinterpret_cast<uchar *>(const_cast<char *>(ba.constData()));-
20 const uchar *ba_end = cursor + ba.length();-
21 const ushort *src = reinterpret_cast<const ushort *>(s.constBegin());-
22 const ushort *const end = reinterpret_cast<const ushort *>(s.constEnd());-
23-
24 while (src != end
src != endDescription
TRUEnever evaluated
FALSEnever evaluated
) {
0
25 if (cursor >= ba_end - 6
cursor >= ba_end - 6Description
TRUEnever evaluated
FALSEnever evaluated
) {
0
26-
27 int pos = cursor - (const uchar *)ba.constData();-
28 ba.resize(ba.size()*2);-
29 cursor = (uchar *)ba.data() + pos;-
30 ba_end = (const uchar *)ba.constData() + ba.length();-
31 }
never executed: end of block
0
32-
33 uint u = *src++;-
34 if (u < 0x80
u < 0x80Description
TRUEnever evaluated
FALSEnever evaluated
) {
0
35 if (u < 0x20
u < 0x20Description
TRUEnever evaluated
FALSEnever evaluated
|| u == 0x22
u == 0x22Description
TRUEnever evaluated
FALSEnever evaluated
|| u == 0x5c
u == 0x5cDescription
TRUEnever evaluated
FALSEnever evaluated
) {
0
36 *cursor++ = '\\';-
37 switch (u) {-
38 case
never executed: case 0x22:
0x22:
never executed: case 0x22:
0
39 *cursor++ = '"';-
40 break;
never executed: break;
0
41 case
never executed: case 0x5c:
0x5c:
never executed: case 0x5c:
0
42 *cursor++ = '\\';-
43 break;
never executed: break;
0
44 case
never executed: case 0x8:
0x8:
never executed: case 0x8:
0
45 *cursor++ = 'b';-
46 break;
never executed: break;
0
47 case
never executed: case 0xc:
0xc:
never executed: case 0xc:
0
48 *cursor++ = 'f';-
49 break;
never executed: break;
0
50 case
never executed: case 0xa:
0xa:
never executed: case 0xa:
0
51 *cursor++ = 'n';-
52 break;
never executed: break;
0
53 case
never executed: case 0xd:
0xd:
never executed: case 0xd:
0
54 *cursor++ = 'r';-
55 break;
never executed: break;
0
56 case
never executed: case 0x9:
0x9:
never executed: case 0x9:
0
57 *cursor++ = 't';-
58 break;
never executed: break;
0
59 default
never executed: default:
:
never executed: default:
0
60 *cursor++ = 'u';-
61 *cursor++ = '0';-
62 *cursor++ = '0';-
63 *cursor++ = hexdig(u>>4);-
64 *cursor++ = hexdig(u & 0xf);-
65 }
never executed: end of block
0
66 } else {-
67 *cursor++ = (uchar)u;-
68 }
never executed: end of block
0
69 } else {-
70 if (QUtf8Functions::toUtf8<QUtf8BaseTraits>(u, cursor, src, end) < 0
QUtf8Functions... src, end) < 0Description
TRUEnever evaluated
FALSEnever evaluated
)
0
71 *
never executed: *cursor++ = replacement;
cursor++ = replacement;
never executed: *cursor++ = replacement;
0
72 }
never executed: end of block
0
73 }-
74-
75 ba.resize(cursor - (const uchar *)ba.constData());-
76 return
never executed: return ba;
ba;
never executed: return ba;
0
77}-
78-
79static void valueToJson(const QJsonPrivate::Base *b, const QJsonPrivate::Value &v, QByteArray &json, int indent, bool compact)-
80{-
81 QJsonValue::Type type = (QJsonValue::Type)(uint)v.type;-
82 switch (type) {-
83 case
never executed: case QJsonValue::Bool:
QJsonValue::Bool:
never executed: case QJsonValue::Bool:
0
84 json += v.toBoolean()
v.toBoolean()Description
TRUEnever evaluated
FALSEnever evaluated
? "true" : "false";
0
85 break;
never executed: break;
0
86 case
never executed: case QJsonValue::Double:
QJsonValue::Double:
never executed: case QJsonValue::Double:
{
0
87 const double d = v.toDouble(b);-
88 if (qIsFinite(d)
qIsFinite(d)Description
TRUEnever evaluated
FALSEnever evaluated
)
0
89 json += QByteArray::number(d, 'g', QLocale::FloatingPointShortest);
never executed: json += QByteArray::number(d, 'g', QLocale::FloatingPointShortest);
0
90 else-
91 json += "null";
never executed: json += "null";
0
92 break;
never executed: break;
0
93 }-
94 case
never executed: case QJsonValue::String:
QJsonValue::String:
never executed: case QJsonValue::String:
0
95 json += '"';-
96 json += escapedString(v.toString(b));-
97 json += '"';-
98 break;
never executed: break;
0
99 case
never executed: case QJsonValue::Array:
QJsonValue::Array:
never executed: case QJsonValue::Array:
0
100 json += compact
compactDescription
TRUEnever evaluated
FALSEnever evaluated
? "[" : "[\n";
0
101 arrayContentToJson(static_cast<QJsonPrivate::Array *>(v.base(b)), json, indent + (compact ? 0 : 1), compact);-
102 json += QByteArray(4*indent, ' ');-
103 json += ']';-
104 break;
never executed: break;
0
105 case
never executed: case QJsonValue::Object:
QJsonValue::Object:
never executed: case QJsonValue::Object:
0
106 json += compact
compactDescription
TRUEnever evaluated
FALSEnever evaluated
? "{" : "{\n";
0
107 objectContentToJson(static_cast<QJsonPrivate::Object *>(v.base(b)), json, indent + (compact ? 0 : 1), compact);-
108 json += QByteArray(4*indent, ' ');-
109 json += '}';-
110 break;
never executed: break;
0
111 case
never executed: case QJsonValue::Null:
QJsonValue::Null:
never executed: case QJsonValue::Null:
0
112 default
never executed: default:
:
never executed: default:
0
113 json += "null";-
114 }
never executed: end of block
0
115}-
116-
117static void arrayContentToJson(const QJsonPrivate::Array *a, QByteArray &json, int indent, bool compact)-
118{-
119 if (!a
!aDescription
TRUEnever evaluated
FALSEnever evaluated
|| !a->length
!a->lengthDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
120 return;
never executed: return;
0
121-
122 QByteArray indentString(4*indent, ' ');-
123-
124 uint i = 0;-
125 while (1) {-
126 json += indentString;-
127 valueToJson(a, a->at(i), json, indent, compact);-
128-
129 if (++
++i == a->lengthDescription
TRUEnever evaluated
FALSEnever evaluated
i == a->length
++i == a->lengthDescription
TRUEnever evaluated
FALSEnever evaluated
) {
0
130 if (!compact
!compactDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
131 json += '\n';
never executed: json += '\n';
0
132 break;
never executed: break;
0
133 }-
134-
135 json += compact
compactDescription
TRUEnever evaluated
FALSEnever evaluated
? "," : ",\n";
0
136 }
never executed: end of block
0
137}
never executed: end of block
0
138-
139-
140static void objectContentToJson(const QJsonPrivate::Object *o, QByteArray &json, int indent, bool compact)-
141{-
142 if (!o
!oDescription
TRUEnever evaluated
FALSEnever evaluated
|| !o->length
!o->lengthDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
143 return;
never executed: return;
0
144-
145 QByteArray indentString(4*indent, ' ');-
146-
147 uint i = 0;-
148 while (1) {-
149 QJsonPrivate::Entry *e = o->entryAt(i);-
150 json += indentString;-
151 json += '"';-
152 json += escapedString(e->key());-
153 json += compact
compactDescription
TRUEnever evaluated
FALSEnever evaluated
? "\":" : "\": ";
0
154 valueToJson(o, e->value, json, indent, compact);-
155-
156 if (++
++i == o->lengthDescription
TRUEnever evaluated
FALSEnever evaluated
i == o->length
++i == o->lengthDescription
TRUEnever evaluated
FALSEnever evaluated
) {
0
157 if (!compact
!compactDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
158 json += '\n';
never executed: json += '\n';
0
159 break;
never executed: break;
0
160 }-
161-
162 json += compact
compactDescription
TRUEnever evaluated
FALSEnever evaluated
? "," : ",\n";
0
163 }
never executed: end of block
0
164}
never executed: end of block
0
165-
166void Writer::objectToJson(const QJsonPrivate::Object *o, QByteArray &json, int indent, bool compact)-
167{-
168 json.reserve(json.size() + (o ? (int)o->size : 16));-
169 json += compact
compactDescription
TRUEnever evaluated
FALSEnever evaluated
? "{" : "{\n";
0
170 objectContentToJson(o, json, indent + (compact ? 0 : 1), compact);-
171 json += QByteArray(4*indent, ' ');-
172 json += compact
compactDescription
TRUEnever evaluated
FALSEnever evaluated
? "}" : "}\n";
0
173}
never executed: end of block
0
174-
175void Writer::arrayToJson(const QJsonPrivate::Array *a, QByteArray &json, int indent, bool compact)-
176{-
177 json.reserve(json.size() + (a ? (int)a->size : 16));-
178 json += compact
compactDescription
TRUEnever evaluated
FALSEnever evaluated
? "[" : "[\n";
0
179 arrayContentToJson(a, json, indent + (compact ? 0 : 1), compact);-
180 json += QByteArray(4*indent, ' ');-
181 json += compact
compactDescription
TRUEnever evaluated
FALSEnever evaluated
? "]" : "]\n";
0
182}
never executed: end of block
0
183-
184-
Switch to Source codePreprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9