Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/json/qjsonwriter.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||
2 | ** | - | ||||||
3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||
4 | ** Copyright (C) 2016 Intel Corporation. | - | ||||||
5 | ** Contact: https://www.qt.io/licensing/ | - | ||||||
6 | ** | - | ||||||
7 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||
8 | ** | - | ||||||
9 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||
10 | ** Commercial License Usage | - | ||||||
11 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||
12 | ** accordance with the commercial license agreement provided with the | - | ||||||
13 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||
14 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||
15 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||
16 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||
17 | ** | - | ||||||
18 | ** GNU Lesser General Public License Usage | - | ||||||
19 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||
20 | ** General Public License version 3 as published by the Free Software | - | ||||||
21 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||
22 | ** packaging of this file. Please review the following information to | - | ||||||
23 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||
24 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||
25 | ** | - | ||||||
26 | ** GNU General Public License Usage | - | ||||||
27 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||
28 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||
29 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||
30 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||
31 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||
32 | ** included in the packaging of this file. Please review the following | - | ||||||
33 | ** information to ensure the GNU General Public License requirements will | - | ||||||
34 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||
35 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||
36 | ** | - | ||||||
37 | ** $QT_END_LICENSE$ | - | ||||||
38 | ** | - | ||||||
39 | ****************************************************************************/ | - | ||||||
40 | - | |||||||
41 | #include <qlocale.h> | - | ||||||
42 | #include "qjsonwriter_p.h" | - | ||||||
43 | #include "qjson_p.h" | - | ||||||
44 | #include "private/qutfcodec_p.h" | - | ||||||
45 | - | |||||||
46 | QT_BEGIN_NAMESPACE | - | ||||||
47 | - | |||||||
48 | using namespace QJsonPrivate; | - | ||||||
49 | - | |||||||
50 | static void objectContentToJson(const QJsonPrivate::Object *o, QByteArray &json, int indent, bool compact); | - | ||||||
51 | static void arrayContentToJson(const QJsonPrivate::Array *a, QByteArray &json, int indent, bool compact); | - | ||||||
52 | - | |||||||
53 | static inline uchar hexdig(uint u) | - | ||||||
54 | { | - | ||||||
55 | return (u < 0xa ? '0' + u : 'a' + u - 0xa); | - | ||||||
56 | } | - | ||||||
57 | - | |||||||
58 | static QByteArray escapedString(const QString &s) | - | ||||||
59 | { | - | ||||||
60 | const uchar replacement = '?'; | - | ||||||
61 | QByteArray ba(s.length(), Qt::Uninitialized); | - | ||||||
62 | - | |||||||
63 | uchar *cursor = reinterpret_cast<uchar *>(const_cast<char *>(ba.constData())); | - | ||||||
64 | const uchar *ba_end = cursor + ba.length(); | - | ||||||
65 | const ushort *src = reinterpret_cast<const ushort *>(s.constBegin()); | - | ||||||
66 | const ushort *const end = reinterpret_cast<const ushort *>(s.constEnd()); | - | ||||||
67 | - | |||||||
68 | while (src != end) { | - | ||||||
69 | if (cursor >= ba_end - 6) { | - | ||||||
70 | // ensure we have enough space | - | ||||||
71 | int pos = cursor - (const uchar *)ba.constData(); | - | ||||||
72 | ba.resize(ba.size()*2); | - | ||||||
73 | cursor = (uchar *)ba.data() + pos; | - | ||||||
74 | ba_end = (const uchar *)ba.constData() + ba.length(); | - | ||||||
75 | } | - | ||||||
76 | - | |||||||
77 | uint u = *src++; | - | ||||||
78 | if (u < 0x80) { | - | ||||||
79 | if (u < 0x20 || u == 0x22 || u == 0x5c) { | - | ||||||
80 | *cursor++ = '\\'; | - | ||||||
81 | switch (u) { | - | ||||||
82 | case 0x22: | - | ||||||
83 | *cursor++ = '"'; | - | ||||||
84 | break; | - | ||||||
85 | case 0x5c: | - | ||||||
86 | *cursor++ = '\\'; | - | ||||||
87 | break; | - | ||||||
88 | case 0x8: | - | ||||||
89 | *cursor++ = 'b'; | - | ||||||
90 | break; | - | ||||||
91 | case 0xc: | - | ||||||
92 | *cursor++ = 'f'; | - | ||||||
93 | break; | - | ||||||
94 | case 0xa: | - | ||||||
95 | *cursor++ = 'n'; | - | ||||||
96 | break; | - | ||||||
97 | case 0xd: | - | ||||||
98 | *cursor++ = 'r'; | - | ||||||
99 | break; | - | ||||||
100 | case 0x9: | - | ||||||
101 | *cursor++ = 't'; | - | ||||||
102 | break; | - | ||||||
103 | default: | - | ||||||
104 | *cursor++ = 'u'; | - | ||||||
105 | *cursor++ = '0'; | - | ||||||
106 | *cursor++ = '0'; | - | ||||||
107 | *cursor++ = hexdig(u>>4); | - | ||||||
108 | *cursor++ = hexdig(u & 0xf); | - | ||||||
109 | } | - | ||||||
110 | } else { | - | ||||||
111 | *cursor++ = (uchar)u; | - | ||||||
112 | } | - | ||||||
113 | } else { | - | ||||||
114 | if (QUtf8Functions::toUtf8<QUtf8BaseTraits>(u, cursor, src, end) < 0) | - | ||||||
115 | *cursor++ = replacement; | - | ||||||
116 | } | - | ||||||
117 | } | - | ||||||
118 | - | |||||||
119 | ba.resize(cursor - (const uchar *)ba.constData()); | - | ||||||
120 | return ba; | - | ||||||
121 | } | - | ||||||
122 | - | |||||||
123 | static void valueToJson(const QJsonPrivate::Base *b, const QJsonPrivate::Value &v, QByteArray &json, int indent, bool compact) | - | ||||||
124 | { | - | ||||||
125 | QJsonValue::Type type = (QJsonValue::Type)(uint)v.type; | - | ||||||
126 | switch (type) { | - | ||||||
127 | case QJsonValue::Bool: never executed: case QJsonValue::Bool: | 0 | ||||||
128 | json += v.toBoolean() ? "true" : "false";
| 0 | ||||||
129 | break; never executed: break; | 0 | ||||||
130 | case QJsonValue::Double: { never executed: case QJsonValue::Double: | 0 | ||||||
131 | const double d = v.toDouble(b); | - | ||||||
132 | if (qIsFinite(d)) // +2 to format to ensure the expected precision
| 0 | ||||||
133 | json += QByteArray::number(d, 'g', stdQLocale::numeric_limits<double>::digits10 + 2FloatingPointShortest); // ::digits10 is 15 never executed: json += QByteArray::number(d, 'g', QLocale::FloatingPointShortest); | 0 | ||||||
134 | else | - | ||||||
135 | json += "null"; // +INF || -INF || NaN (see RFC4627#section2.4) never executed: json += "null"; | 0 | ||||||
136 | break; never executed: break; | 0 | ||||||
137 | } | - | ||||||
138 | case QJsonValue::String: never executed: case QJsonValue::String: | 0 | ||||||
139 | json += '"'; | - | ||||||
140 | json += escapedString(v.toString(b)); | - | ||||||
141 | json += '"'; | - | ||||||
142 | break; never executed: break; | 0 | ||||||
143 | case QJsonValue::Array: never executed: case QJsonValue::Array: | 0 | ||||||
144 | json += compact ? "[" : "[\n";
| 0 | ||||||
145 | arrayContentToJson(static_cast<QJsonPrivate::Array *>(v.base(b)), json, indent + (compact ? 0 : 1), compact); | - | ||||||
146 | json += QByteArray(4*indent, ' '); | - | ||||||
147 | json += ']'; | - | ||||||
148 | break; never executed: break; | 0 | ||||||
149 | case QJsonValue::Object: never executed: case QJsonValue::Object: | 0 | ||||||
150 | json += compact ? "{" : "{\n";
| 0 | ||||||
151 | objectContentToJson(static_cast<QJsonPrivate::Object *>(v.base(b)), json, indent + (compact ? 0 : 1), compact); | - | ||||||
152 | json += QByteArray(4*indent, ' '); | - | ||||||
153 | json += '}'; | - | ||||||
154 | break; never executed: break; | 0 | ||||||
155 | case QJsonValue::Null: never executed: case QJsonValue::Null: | 0 | ||||||
156 | default: never executed: default: | 0 | ||||||
157 | json += "null"; | - | ||||||
158 | } never executed: end of block | 0 | ||||||
159 | } | - | ||||||
160 | - | |||||||
161 | static void arrayContentToJson(const QJsonPrivate::Array *a, QByteArray &json, int indent, bool compact) | - | ||||||
162 | { | - | ||||||
163 | if (!a || !a->length) | - | ||||||
164 | return; | - | ||||||
165 | - | |||||||
166 | QByteArray indentString(4*indent, ' '); | - | ||||||
167 | - | |||||||
168 | uint i = 0; | - | ||||||
169 | while (1) { | - | ||||||
170 | json += indentString; | - | ||||||
171 | valueToJson(a, a->at(i), json, indent, compact); | - | ||||||
172 | - | |||||||
173 | if (++i == a->length) { | - | ||||||
174 | if (!compact) | - | ||||||
175 | json += '\n'; | - | ||||||
176 | break; | - | ||||||
177 | } | - | ||||||
178 | - | |||||||
179 | json += compact ? "," : ",\n"; | - | ||||||
180 | } | - | ||||||
181 | } | - | ||||||
182 | - | |||||||
183 | - | |||||||
184 | static void objectContentToJson(const QJsonPrivate::Object *o, QByteArray &json, int indent, bool compact) | - | ||||||
185 | { | - | ||||||
186 | if (!o || !o->length) | - | ||||||
187 | return; | - | ||||||
188 | - | |||||||
189 | QByteArray indentString(4*indent, ' '); | - | ||||||
190 | - | |||||||
191 | uint i = 0; | - | ||||||
192 | while (1) { | - | ||||||
193 | QJsonPrivate::Entry *e = o->entryAt(i); | - | ||||||
194 | json += indentString; | - | ||||||
195 | json += '"'; | - | ||||||
196 | json += escapedString(e->key()); | - | ||||||
197 | json += compact ? "\":" : "\": "; | - | ||||||
198 | valueToJson(o, e->value, json, indent, compact); | - | ||||||
199 | - | |||||||
200 | if (++i == o->length) { | - | ||||||
201 | if (!compact) | - | ||||||
202 | json += '\n'; | - | ||||||
203 | break; | - | ||||||
204 | } | - | ||||||
205 | - | |||||||
206 | json += compact ? "," : ",\n"; | - | ||||||
207 | } | - | ||||||
208 | } | - | ||||||
209 | - | |||||||
210 | void Writer::objectToJson(const QJsonPrivate::Object *o, QByteArray &json, int indent, bool compact) | - | ||||||
211 | { | - | ||||||
212 | json.reserve(json.size() + (o ? (int)o->size : 16)); | - | ||||||
213 | json += compact ? "{" : "{\n"; | - | ||||||
214 | objectContentToJson(o, json, indent + (compact ? 0 : 1), compact); | - | ||||||
215 | json += QByteArray(4*indent, ' '); | - | ||||||
216 | json += compact ? "}" : "}\n"; | - | ||||||
217 | } | - | ||||||
218 | - | |||||||
219 | void Writer::arrayToJson(const QJsonPrivate::Array *a, QByteArray &json, int indent, bool compact) | - | ||||||
220 | { | - | ||||||
221 | json.reserve(json.size() + (a ? (int)a->size : 16)); | - | ||||||
222 | json += compact ? "[" : "[\n"; | - | ||||||
223 | arrayContentToJson(a, json, indent + (compact ? 0 : 1), compact); | - | ||||||
224 | json += QByteArray(4*indent, ' '); | - | ||||||
225 | json += compact ? "]" : "]\n"; | - | ||||||
226 | } | - | ||||||
227 | - | |||||||
228 | QT_END_NAMESPACE | - | ||||||
Source code | Switch to Preprocessed file |