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 <cmath>
42#include <qlocale.h>
43#include "qjsonwriter_p.h"
44#include "qjson_p.h"
45#include "private/qutfcodec_p.h"
46#include <private/qnumeric_p.h>
47#include <private/qcborvalue_p.h>
48
49QT_BEGIN_NAMESPACE
50
51using namespace QJsonPrivate;
52
53static void objectContentToJson(const QCborContainerPrivate *o, QByteArray &json, int indent, bool compact);
54static void arrayContentToJson(const QCborContainerPrivate *a, QByteArray &json, int indent, bool compact);
55
56static inline uchar hexdig(uint u)
57{
58 return (u < 0xa ? '0' + u : 'a' + u - 0xa);
59}
60
61static QByteArray escapedString(const QString &s)
62{
63 QByteArray ba(s.length(), Qt::Uninitialized);
64
65 uchar *cursor = reinterpret_cast<uchar *>(const_cast<char *>(ba.constData()));
66 const uchar *ba_end = cursor + ba.length();
67 const ushort *src = reinterpret_cast<const ushort *>(s.constBegin());
68 const ushort *const end = reinterpret_cast<const ushort *>(s.constEnd());
69
70 while (src != end) {
71 if (cursor >= ba_end - 6) {
72 // ensure we have enough space
73 int pos = cursor - (const uchar *)ba.constData();
74 ba.resize(size: ba.size()*2);
75 cursor = (uchar *)ba.data() + pos;
76 ba_end = (const uchar *)ba.constData() + ba.length();
77 }
78
79 uint u = *src++;
80 if (u < 0x80) {
81 if (u < 0x20 || u == 0x22 || u == 0x5c) {
82 *cursor++ = '\\';
83 switch (u) {
84 case 0x22:
85 *cursor++ = '"';
86 break;
87 case 0x5c:
88 *cursor++ = '\\';
89 break;
90 case 0x8:
91 *cursor++ = 'b';
92 break;
93 case 0xc:
94 *cursor++ = 'f';
95 break;
96 case 0xa:
97 *cursor++ = 'n';
98 break;
99 case 0xd:
100 *cursor++ = 'r';
101 break;
102 case 0x9:
103 *cursor++ = 't';
104 break;
105 default:
106 *cursor++ = 'u';
107 *cursor++ = '0';
108 *cursor++ = '0';
109 *cursor++ = hexdig(u: u>>4);
110 *cursor++ = hexdig(u: u & 0xf);
111 }
112 } else {
113 *cursor++ = (uchar)u;
114 }
115 } else if (QUtf8Functions::toUtf8<QUtf8BaseTraits>(u, dst&: cursor, src, end) < 0) {
116 // failed to get valid utf8 use JSON escape sequence
117 *cursor++ = '\\';
118 *cursor++ = 'u';
119 *cursor++ = hexdig(u: u>>12 & 0x0f);
120 *cursor++ = hexdig(u: u>>8 & 0x0f);
121 *cursor++ = hexdig(u: u>>4 & 0x0f);
122 *cursor++ = hexdig(u: u & 0x0f);
123 }
124 }
125
126 ba.resize(size: cursor - (const uchar *)ba.constData());
127 return ba;
128}
129
130static void valueToJson(const QCborValue &v, QByteArray &json, int indent, bool compact)
131{
132 QCborValue::Type type = v.type();
133 switch (type) {
134 case QCborValue::True:
135 json += "true";
136 break;
137 case QCborValue::False:
138 json += "false";
139 break;
140 case QCborValue::Integer:
141 case QCborValue::Double: {
142 const double d = v.toDouble();
143 if (qIsFinite(d)) {
144 quint64 absInt;
145 json += QByteArray::number(d, f: convertDoubleTo(v: std::abs(x: d), value: &absInt) ? 'f' : 'g',
146 prec: QLocale::FloatingPointShortest);
147 } else {
148 json += "null"; // +INF || -INF || NaN (see RFC4627#section2.4)
149 }
150 break;
151 }
152 case QCborValue::String:
153 json += '"';
154 json += escapedString(s: v.toString());
155 json += '"';
156 break;
157 case QCborValue::Array:
158 json += compact ? "[" : "[\n";
159 arrayContentToJson(
160 a: QJsonPrivate::Value::container(v), json, indent: indent + (compact ? 0 : 1), compact);
161 json += QByteArray(4*indent, ' ');
162 json += ']';
163 break;
164 case QCborValue::Map:
165 json += compact ? "{" : "{\n";
166 objectContentToJson(
167 o: QJsonPrivate::Value::container(v), json, indent: indent + (compact ? 0 : 1), compact);
168 json += QByteArray(4*indent, ' ');
169 json += '}';
170 break;
171 case QCborValue::Null:
172 default:
173 json += "null";
174 }
175}
176
177static void arrayContentToJson(const QCborContainerPrivate *a, QByteArray &json, int indent, bool compact)
178{
179 if (!a || a->elements.empty())
180 return;
181
182 QByteArray indentString(4*indent, ' ');
183
184 qsizetype i = 0;
185 while (true) {
186 json += indentString;
187 valueToJson(v: a->valueAt(idx: i), json, indent, compact);
188
189 if (++i == a->elements.size()) {
190 if (!compact)
191 json += '\n';
192 break;
193 }
194
195 json += compact ? "," : ",\n";
196 }
197}
198
199
200static void objectContentToJson(const QCborContainerPrivate *o, QByteArray &json, int indent, bool compact)
201{
202 if (!o || o->elements.empty())
203 return;
204
205 QByteArray indentString(4*indent, ' ');
206
207 qsizetype i = 0;
208 while (true) {
209 QCborValue e = o->valueAt(idx: i);
210 json += indentString;
211 json += '"';
212 json += escapedString(s: o->valueAt(idx: i).toString());
213 json += compact ? "\":" : "\": ";
214 valueToJson(v: o->valueAt(idx: i + 1), json, indent, compact);
215
216 if ((i += 2) == o->elements.size()) {
217 if (!compact)
218 json += '\n';
219 break;
220 }
221
222 json += compact ? "," : ",\n";
223 }
224}
225
226void Writer::objectToJson(const QCborContainerPrivate *o, QByteArray &json, int indent, bool compact)
227{
228 json.reserve(asize: json.size() + (o ? (int)o->elements.size() : 16));
229 json += compact ? "{" : "{\n";
230 objectContentToJson(o, json, indent: indent + (compact ? 0 : 1), compact);
231 json += QByteArray(4*indent, ' ');
232 json += compact ? "}" : "}\n";
233}
234
235void Writer::arrayToJson(const QCborContainerPrivate *a, QByteArray &json, int indent, bool compact)
236{
237 json.reserve(asize: json.size() + (a ? (int)a->elements.size() : 16));
238 json += compact ? "[" : "[\n";
239 arrayContentToJson(a, json, indent: indent + (compact ? 0 : 1), compact);
240 json += QByteArray(4*indent, ' ');
241 json += compact ? "]" : "]\n";
242}
243
244QT_END_NAMESPACE
245

source code of qtbase/src/corelib/serialization/qjsonwriter.cpp