Line | Source Code | Coverage |
---|
1 | /**************************************************************************** | - |
2 | ** | - |
3 | ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). | - |
4 | ** Contact: http://www.qt-project.org/legal | - |
5 | ** | - |
6 | ** This file is part of the QtCore module of the Qt Toolkit. | - |
7 | ** | - |
8 | ** $QT_BEGIN_LICENSE:LGPL$ | - |
9 | ** Commercial License Usage | - |
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - |
11 | ** accordance with the commercial license agreement provided with the | - |
12 | ** Software or, alternatively, in accordance with the terms contained in | - |
13 | ** a written agreement between you and Digia. For licensing terms and | - |
14 | ** conditions see http://qt.digia.com/licensing. For further information | - |
15 | ** use the contact form at http://qt.digia.com/contact-us. | - |
16 | ** | - |
17 | ** GNU Lesser General Public License Usage | - |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - |
19 | ** General Public License version 2.1 as published by the Free Software | - |
20 | ** Foundation and appearing in the file LICENSE.LGPL included in the | - |
21 | ** packaging of this file. Please review the following information to | - |
22 | ** ensure the GNU Lesser General Public License version 2.1 requirements | - |
23 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - |
24 | ** | - |
25 | ** In addition, as a special exception, Digia gives you certain additional | - |
26 | ** rights. These rights are described in the Digia Qt LGPL Exception | - |
27 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - |
28 | ** | - |
29 | ** GNU General Public License Usage | - |
30 | ** Alternatively, this file may be used under the terms of the GNU | - |
31 | ** General Public License version 3.0 as published by the Free Software | - |
32 | ** Foundation and appearing in the file LICENSE.GPL included in the | - |
33 | ** packaging of this file. Please review the following information to | - |
34 | ** ensure the GNU General Public License version 3.0 requirements will be | - |
35 | ** met: http://www.gnu.org/copyleft/gpl.html. | - |
36 | ** | - |
37 | ** | - |
38 | ** $QT_END_LICENSE$ | - |
39 | ** | - |
40 | ****************************************************************************/ | - |
41 | | - |
42 | #include "qjsonwriter_p.h" | - |
43 | #include "qjson_p.h" | - |
44 | | - |
45 | QT_BEGIN_NAMESPACE | - |
46 | | - |
47 | using namespace QJsonPrivate; | - |
48 | | - |
49 | static void objectContentToJson(const QJsonPrivate::Object *o, QByteArray &json, int indent, bool compact); | - |
50 | static void arrayContentToJson(const QJsonPrivate::Array *a, QByteArray &json, int indent, bool compact); | - |
51 | | - |
52 | static inline uchar hexdig(uint u) | - |
53 | { | - |
54 | return (u < 0xa ? '0' + u : 'a' + u - 0xa); executed: return (u < 0xa ? '0' + u : 'a' + u - 0xa); Execution Count:10542 | 10542 |
55 | } | - |
56 | | - |
57 | static QByteArray escapedString(const QString &s) | - |
58 | { | - |
59 | const uchar replacement = '?'; executed (the execution status of this line is deduced): const uchar replacement = '?'; | - |
60 | QByteArray ba(s.length(), Qt::Uninitialized); executed (the execution status of this line is deduced): QByteArray ba(s.length(), Qt::Uninitialized); | - |
61 | | - |
62 | uchar *cursor = (uchar *)ba.data(); executed (the execution status of this line is deduced): uchar *cursor = (uchar *)ba.data(); | - |
63 | const uchar *ba_end = cursor + ba.length(); executed (the execution status of this line is deduced): const uchar *ba_end = cursor + ba.length(); | - |
64 | | - |
65 | const QChar *ch = (const QChar *)s.constData(); executed (the execution status of this line is deduced): const QChar *ch = (const QChar *)s.constData(); | - |
66 | const QChar *end = ch + s.length(); executed (the execution status of this line is deduced): const QChar *end = ch + s.length(); | - |
67 | | - |
68 | int surrogate_high = -1; executed (the execution status of this line is deduced): int surrogate_high = -1; | - |
69 | | - |
70 | while (ch < end) { evaluated: ch < end yes Evaluation Count:27278864 | yes Evaluation Count:1332760 |
| 1332760-27278864 |
71 | if (cursor >= ba_end - 6) { evaluated: cursor >= ba_end - 6 yes Evaluation Count:1996087 | yes Evaluation Count:25282777 |
| 1996087-25282777 |
72 | // ensure we have enough space | - |
73 | int pos = cursor - (const uchar *)ba.constData(); executed (the execution status of this line is deduced): int pos = cursor - (const uchar *)ba.constData(); | - |
74 | ba.resize(ba.size()*2); executed (the execution status of this line is deduced): ba.resize(ba.size()*2); | - |
75 | cursor = (uchar *)ba.data() + pos; executed (the execution status of this line is deduced): cursor = (uchar *)ba.data() + pos; | - |
76 | ba_end = (const uchar *)ba.constData() + ba.length(); executed (the execution status of this line is deduced): ba_end = (const uchar *)ba.constData() + ba.length(); | - |
77 | } executed: } Execution Count:1996087 | 1996087 |
78 | | - |
79 | uint u = ch->unicode(); executed (the execution status of this line is deduced): uint u = ch->unicode(); | - |
80 | if (surrogate_high >= 0) { partially evaluated: surrogate_high >= 0 no Evaluation Count:0 | yes Evaluation Count:27278864 |
| 0-27278864 |
81 | if (ch->isLowSurrogate()) { never evaluated: ch->isLowSurrogate() | 0 |
82 | u = QChar::surrogateToUcs4(surrogate_high, u); never executed (the execution status of this line is deduced): u = QChar::surrogateToUcs4(surrogate_high, u); | - |
83 | surrogate_high = -1; never executed (the execution status of this line is deduced): surrogate_high = -1; | - |
84 | } else { | 0 |
85 | // high surrogate without low | - |
86 | *cursor = replacement; never executed (the execution status of this line is deduced): *cursor = replacement; | - |
87 | ++ch; never executed (the execution status of this line is deduced): ++ch; | - |
88 | surrogate_high = -1; never executed (the execution status of this line is deduced): surrogate_high = -1; | - |
89 | continue; never executed: continue; | 0 |
90 | } | - |
91 | } else if (ch->isLowSurrogate()) { partially evaluated: ch->isLowSurrogate() no Evaluation Count:0 | yes Evaluation Count:27278864 |
| 0-27278864 |
92 | // low surrogate without high | - |
93 | *cursor = replacement; never executed (the execution status of this line is deduced): *cursor = replacement; | - |
94 | ++ch; never executed (the execution status of this line is deduced): ++ch; | - |
95 | continue; never executed: continue; | 0 |
96 | } else if (ch->isHighSurrogate()) { partially evaluated: ch->isHighSurrogate() no Evaluation Count:0 | yes Evaluation Count:27278864 |
| 0-27278864 |
97 | surrogate_high = u; never executed (the execution status of this line is deduced): surrogate_high = u; | - |
98 | ++ch; never executed (the execution status of this line is deduced): ++ch; | - |
99 | continue; never executed: continue; | 0 |
100 | } | - |
101 | | - |
102 | if (u < 0x80) { evaluated: u < 0x80 yes Evaluation Count:27262928 | yes Evaluation Count:15936 |
| 15936-27262928 |
103 | if (u < 0x20 || u == 0x22 || u == 0x5c) { evaluated: u < 0x20 yes Evaluation Count:12955 | yes Evaluation Count:27249973 |
evaluated: u == 0x22 yes Evaluation Count:5208 | yes Evaluation Count:27244765 |
evaluated: u == 0x5c yes Evaluation Count:751 | yes Evaluation Count:27244014 |
| 751-27249973 |
104 | *cursor++ = '\\'; executed (the execution status of this line is deduced): *cursor++ = '\\'; | - |
105 | switch (u) { | - |
106 | case 0x22: | - |
107 | *cursor++ = '"'; executed (the execution status of this line is deduced): *cursor++ = '"'; | - |
108 | break; executed: break; Execution Count:5208 | 5208 |
109 | case 0x5c: | - |
110 | *cursor++ = '\\'; executed (the execution status of this line is deduced): *cursor++ = '\\'; | - |
111 | break; executed: break; Execution Count:751 | 751 |
112 | case 0x8: | - |
113 | *cursor++ = 'b'; executed (the execution status of this line is deduced): *cursor++ = 'b'; | - |
114 | break; executed: break; Execution Count:1525 | 1525 |
115 | case 0xc: | - |
116 | *cursor++ = 'f'; executed (the execution status of this line is deduced): *cursor++ = 'f'; | - |
117 | break; executed: break; Execution Count:1564 | 1564 |
118 | case 0xa: | - |
119 | *cursor++ = 'n'; executed (the execution status of this line is deduced): *cursor++ = 'n'; | - |
120 | break; executed: break; Execution Count:1547 | 1547 |
121 | case 0xd: | - |
122 | *cursor++ = 'r'; executed (the execution status of this line is deduced): *cursor++ = 'r'; | - |
123 | break; executed: break; Execution Count:1536 | 1536 |
124 | case 0x9: | - |
125 | *cursor++ = 't'; executed (the execution status of this line is deduced): *cursor++ = 't'; | - |
126 | break; executed: break; Execution Count:1512 | 1512 |
127 | default: | - |
128 | *cursor++ = 'u'; executed (the execution status of this line is deduced): *cursor++ = 'u'; | - |
129 | *cursor++ = '0'; executed (the execution status of this line is deduced): *cursor++ = '0'; | - |
130 | *cursor++ = '0'; executed (the execution status of this line is deduced): *cursor++ = '0'; | - |
131 | *cursor++ = hexdig(u>>4); executed (the execution status of this line is deduced): *cursor++ = hexdig(u>>4); | - |
132 | *cursor++ = hexdig(u & 0xf); executed (the execution status of this line is deduced): *cursor++ = hexdig(u & 0xf); | - |
133 | } executed: } Execution Count:5271 | 5271 |
134 | } else { executed: } Execution Count:18914 | 18914 |
135 | *cursor++ = (uchar)u; executed (the execution status of this line is deduced): *cursor++ = (uchar)u; | - |
136 | } executed: } Execution Count:27244014 | 27244014 |
137 | } else { | - |
138 | if (u < 0x0800) { evaluated: u < 0x0800 yes Evaluation Count:7737 | yes Evaluation Count:8199 |
| 7737-8199 |
139 | *cursor++ = 0xc0 | ((uchar) (u >> 6)); executed (the execution status of this line is deduced): *cursor++ = 0xc0 | ((uchar) (u >> 6)); | - |
140 | } else { executed: } Execution Count:7737 | 7737 |
141 | // is it one of the Unicode non-characters? | - |
142 | if (QChar::isNonCharacter(u)) { partially evaluated: QChar::isNonCharacter(u) no Evaluation Count:0 | yes Evaluation Count:8199 |
| 0-8199 |
143 | *cursor++ = replacement; never executed (the execution status of this line is deduced): *cursor++ = replacement; | - |
144 | ++ch; never executed (the execution status of this line is deduced): ++ch; | - |
145 | continue; never executed: continue; | 0 |
146 | } | - |
147 | | - |
148 | if (QChar::requiresSurrogates(u)) { partially evaluated: QChar::requiresSurrogates(u) no Evaluation Count:0 | yes Evaluation Count:8199 |
| 0-8199 |
149 | *cursor++ = 0xf0 | ((uchar) (u >> 18)); never executed (the execution status of this line is deduced): *cursor++ = 0xf0 | ((uchar) (u >> 18)); | - |
150 | *cursor++ = 0x80 | (((uchar) (u >> 12)) & 0x3f); never executed (the execution status of this line is deduced): *cursor++ = 0x80 | (((uchar) (u >> 12)) & 0x3f); | - |
151 | } else { | 0 |
152 | *cursor++ = 0xe0 | (((uchar) (u >> 12)) & 0x3f); executed (the execution status of this line is deduced): *cursor++ = 0xe0 | (((uchar) (u >> 12)) & 0x3f); | - |
153 | } executed: } Execution Count:8199 | 8199 |
154 | *cursor++ = 0x80 | (((uchar) (u >> 6)) & 0x3f); executed (the execution status of this line is deduced): *cursor++ = 0x80 | (((uchar) (u >> 6)) & 0x3f); | - |
155 | } executed: } Execution Count:8199 | 8199 |
156 | *cursor++ = 0x80 | ((uchar) (u&0x3f)); executed (the execution status of this line is deduced): *cursor++ = 0x80 | ((uchar) (u&0x3f)); | - |
157 | } executed: } Execution Count:15936 | 15936 |
158 | ++ch; executed (the execution status of this line is deduced): ++ch; | - |
159 | } executed: } Execution Count:27278864 | 27278864 |
160 | | - |
161 | ba.resize(cursor - (const uchar *)ba.constData()); executed (the execution status of this line is deduced): ba.resize(cursor - (const uchar *)ba.constData()); | - |
162 | return ba; executed: return ba; Execution Count:1332760 | 1332760 |
163 | } | - |
164 | | - |
165 | static void valueToJson(const QJsonPrivate::Base *b, const QJsonPrivate::Value &v, QByteArray &json, int indent, bool compact) | - |
166 | { | - |
167 | QJsonValue::Type type = (QJsonValue::Type)(uint)v.type; executed (the execution status of this line is deduced): QJsonValue::Type type = (QJsonValue::Type)(uint)v.type; | - |
168 | switch (type) { | - |
169 | case QJsonValue::Bool: | - |
170 | json += v.toBoolean() ? "true" : "false"; evaluated: v.toBoolean() yes Evaluation Count:1477 | yes Evaluation Count:1466 |
| 1466-1477 |
171 | break; executed: break; Execution Count:2943 | 2943 |
172 | case QJsonValue::Double: | - |
173 | json += QByteArray::number(v.toDouble(b)); executed (the execution status of this line is deduced): json += QByteArray::number(v.toDouble(b)); | - |
174 | break; executed: break; Execution Count:450215 | 450215 |
175 | case QJsonValue::String: | - |
176 | json += '"'; executed (the execution status of this line is deduced): json += '"'; | - |
177 | json += escapedString(v.toString(b)); executed (the execution status of this line is deduced): json += escapedString(v.toString(b)); | - |
178 | json += '"'; executed (the execution status of this line is deduced): json += '"'; | - |
179 | break; executed: break; Execution Count:349688 | 349688 |
180 | case QJsonValue::Array: | - |
181 | json += compact ? "[" : "[\n"; partially evaluated: compact no Evaluation Count:0 | yes Evaluation Count:36437 |
| 0-36437 |
182 | arrayContentToJson(static_cast<QJsonPrivate::Array *>(v.base(b)), json, indent + (compact ? 0 : 1), compact); executed (the execution status of this line is deduced): arrayContentToJson(static_cast<QJsonPrivate::Array *>(v.base(b)), json, indent + (compact ? 0 : 1), compact); | - |
183 | json += QByteArray(4*indent, ' '); executed (the execution status of this line is deduced): json += QByteArray(4*indent, ' '); | - |
184 | json += "]"; executed (the execution status of this line is deduced): json += "]"; | - |
185 | break; executed: break; Execution Count:36437 | 36437 |
186 | case QJsonValue::Object: | - |
187 | json += compact ? "{" : "{\n"; partially evaluated: compact no Evaluation Count:0 | yes Evaluation Count:319522 |
| 0-319522 |
188 | objectContentToJson(static_cast<QJsonPrivate::Object *>(v.base(b)), json, indent + (compact ? 0 : 1), compact); executed (the execution status of this line is deduced): objectContentToJson(static_cast<QJsonPrivate::Object *>(v.base(b)), json, indent + (compact ? 0 : 1), compact); | - |
189 | json += QByteArray(4*indent, ' '); executed (the execution status of this line is deduced): json += QByteArray(4*indent, ' '); | - |
190 | json += "}"; executed (the execution status of this line is deduced): json += "}"; | - |
191 | break; executed: break; Execution Count:319522 | 319522 |
192 | case QJsonValue::Null: | - |
193 | default: | - |
194 | json += "null"; executed (the execution status of this line is deduced): json += "null"; | - |
195 | } executed: } Execution Count:1538 | 1538 |
196 | } executed: } Execution Count:1160343 | 1160343 |
197 | | - |
198 | static void arrayContentToJson(const QJsonPrivate::Array *a, QByteArray &json, int indent, bool compact) | - |
199 | { | - |
200 | if (!a || !a->length) partially evaluated: !a no Evaluation Count:0 | yes Evaluation Count:37193 |
evaluated: !a->length yes Evaluation Count:1470 | yes Evaluation Count:35723 |
| 0-37193 |
201 | return; executed: return; Execution Count:1470 | 1470 |
202 | | - |
203 | QByteArray indentString(4*indent, ' '); executed (the execution status of this line is deduced): QByteArray indentString(4*indent, ' '); | - |
204 | | - |
205 | uint i = 0; executed (the execution status of this line is deduced): uint i = 0; | - |
206 | while (1) { partially evaluated: 1 yes Evaluation Count:177271 | no Evaluation Count:0 |
| 0-177271 |
207 | json += indentString; executed (the execution status of this line is deduced): json += indentString; | - |
208 | valueToJson(a, a->at(i), json, indent, compact); executed (the execution status of this line is deduced): valueToJson(a, a->at(i), json, indent, compact); | - |
209 | | - |
210 | if (++i == a->length) { evaluated: ++i == a->length yes Evaluation Count:35723 | yes Evaluation Count:141548 |
| 35723-141548 |
211 | if (!compact) evaluated: !compact yes Evaluation Count:35720 | yes Evaluation Count:3 |
| 3-35720 |
212 | json += '\n'; executed: json += '\n'; Execution Count:35720 | 35720 |
213 | break; executed: break; Execution Count:35723 | 35723 |
214 | } | - |
215 | | - |
216 | json += compact ? "," : ",\n"; evaluated: compact yes Evaluation Count:3 | yes Evaluation Count:141545 |
| 3-141545 |
217 | } executed: } Execution Count:141548 | 141548 |
218 | } executed: } Execution Count:35723 | 35723 |
219 | | - |
220 | | - |
221 | static void objectContentToJson(const QJsonPrivate::Object *o, QByteArray &json, int indent, bool compact) | - |
222 | { | - |
223 | if (!o || !o->length) partially evaluated: !o no Evaluation Count:0 | yes Evaluation Count:319956 |
evaluated: !o->length yes Evaluation Count:1470 | yes Evaluation Count:318486 |
| 0-319956 |
224 | return; executed: return; Execution Count:1470 | 1470 |
225 | | - |
226 | QByteArray indentString(4*indent, ' '); executed (the execution status of this line is deduced): QByteArray indentString(4*indent, ' '); | - |
227 | | - |
228 | uint i = 0; executed (the execution status of this line is deduced): uint i = 0; | - |
229 | while (1) { partially evaluated: 1 yes Evaluation Count:983072 | no Evaluation Count:0 |
| 0-983072 |
230 | QJsonPrivate::Entry *e = o->entryAt(i); executed (the execution status of this line is deduced): QJsonPrivate::Entry *e = o->entryAt(i); | - |
231 | json += indentString; executed (the execution status of this line is deduced): json += indentString; | - |
232 | json += '"'; executed (the execution status of this line is deduced): json += '"'; | - |
233 | json += escapedString(e->key()); executed (the execution status of this line is deduced): json += escapedString(e->key()); | - |
234 | json += "\": "; executed (the execution status of this line is deduced): json += "\": "; | - |
235 | valueToJson(o, e->value, json, indent, compact); executed (the execution status of this line is deduced): valueToJson(o, e->value, json, indent, compact); | - |
236 | | - |
237 | if (++i == o->length) { evaluated: ++i == o->length yes Evaluation Count:318486 | yes Evaluation Count:664586 |
| 318486-664586 |
238 | if (!compact) evaluated: !compact yes Evaluation Count:318483 | yes Evaluation Count:3 |
| 3-318483 |
239 | json += '\n'; executed: json += '\n'; Execution Count:318483 | 318483 |
240 | break; executed: break; Execution Count:318486 | 318486 |
241 | } | - |
242 | | - |
243 | json += compact ? "," : ",\n"; partially evaluated: compact no Evaluation Count:0 | yes Evaluation Count:664586 |
| 0-664586 |
244 | } executed: } Execution Count:664586 | 664586 |
245 | } executed: } Execution Count:318486 | 318486 |
246 | | - |
247 | void Writer::objectToJson(const QJsonPrivate::Object *o, QByteArray &json, int indent, bool compact) | - |
248 | { | - |
249 | json.reserve(json.size() + (o ? (int)o->size : 16)); executed (the execution status of this line is deduced): json.reserve(json.size() + (o ? (int)o->size : 16)); | - |
250 | json += compact ? "{" : "{\n"; evaluated: compact yes Evaluation Count:3 | yes Evaluation Count:431 |
| 3-431 |
251 | objectContentToJson(o, json, indent + (compact ? 0 : 1), compact); executed (the execution status of this line is deduced): objectContentToJson(o, json, indent + (compact ? 0 : 1), compact); | - |
252 | json += QByteArray(4*indent, ' '); executed (the execution status of this line is deduced): json += QByteArray(4*indent, ' '); | - |
253 | json += compact ? "}" : "}\n"; evaluated: compact yes Evaluation Count:3 | yes Evaluation Count:431 |
| 3-431 |
254 | } executed: } Execution Count:434 | 434 |
255 | | - |
256 | void Writer::arrayToJson(const QJsonPrivate::Array *a, QByteArray &json, int indent, bool compact) | - |
257 | { | - |
258 | json.reserve(json.size() + (a ? (int)a->size : 16)); executed (the execution status of this line is deduced): json.reserve(json.size() + (a ? (int)a->size : 16)); | - |
259 | json += compact ? "[" : "[\n"; evaluated: compact yes Evaluation Count:3 | yes Evaluation Count:753 |
| 3-753 |
260 | arrayContentToJson(a, json, indent + (compact ? 0 : 1), compact); executed (the execution status of this line is deduced): arrayContentToJson(a, json, indent + (compact ? 0 : 1), compact); | - |
261 | json += QByteArray(4*indent, ' '); executed (the execution status of this line is deduced): json += QByteArray(4*indent, ' '); | - |
262 | json += compact ? "]" : "]\n"; evaluated: compact yes Evaluation Count:3 | yes Evaluation Count:753 |
| 3-753 |
263 | } executed: } Execution Count:756 | 756 |
264 | | - |
265 | QT_END_NAMESPACE | - |
266 | | - |
| | |