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/qstringconverter_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 // give it a minimum size to ensure the resize() below always adds enough space
64 QByteArray ba(qMax(s.length(), 16), Qt::Uninitialized);
65
66 uchar *cursor = reinterpret_cast<uchar *>(const_cast<char *>(ba.constData()));
67 const uchar *ba_end = cursor + ba.length();
68 const ushort *src = reinterpret_cast<const ushort *>(s.constBegin());
69 const ushort *const end = reinterpret_cast<const ushort *>(s.constEnd());
70
71 while (src != end) {
72 if (cursor >= ba_end - 6) {
73 // ensure we have enough space
74 int pos = cursor - (const uchar *)ba.constData();
75 ba.resize(ba.size()*2);
76 cursor = (uchar *)ba.data() + pos;
77 ba_end = (const uchar *)ba.constData() + ba.length();
78 }
79
80 uint u = *src++;
81 if (u < 0x80) {
82 if (u < 0x20 || u == 0x22 || u == 0x5c) {
83 *cursor++ = '\\';
84 switch (u) {
85 case 0x22:
86 *cursor++ = '"';
87 break;
88 case 0x5c:
89 *cursor++ = '\\';
90 break;
91 case 0x8:
92 *cursor++ = 'b';
93 break;
94 case 0xc:
95 *cursor++ = 'f';
96 break;
97 case 0xa:
98 *cursor++ = 'n';
99 break;
100 case 0xd:
101 *cursor++ = 'r';
102 break;
103 case 0x9:
104 *cursor++ = 't';
105 break;
106 default:
107 *cursor++ = 'u';
108 *cursor++ = '0';
109 *cursor++ = '0';
110 *cursor++ = hexdig(u>>4);
111 *cursor++ = hexdig(u & 0xf);
112 }
113 } else {
114 *cursor++ = (uchar)u;
115 }
116 } else if (QUtf8Functions::toUtf8<QUtf8BaseTraits>(u, cursor, src, end) < 0) {
117 // failed to get valid utf8 use JSON escape sequence
118 *cursor++ = '\\';
119 *cursor++ = 'u';
120 *cursor++ = hexdig(u>>12 & 0x0f);
121 *cursor++ = hexdig(u>>8 & 0x0f);
122 *cursor++ = hexdig(u>>4 & 0x0f);
123 *cursor++ = hexdig(u & 0x0f);
124 }
125 }
126
127 ba.resize(cursor - (const uchar *)ba.constData());
128 return ba;
129}
130
131static void valueToJson(const QCborValue &v, QByteArray &json, int indent, bool compact)
132{
133 QCborValue::Type type = v.type();
134 switch (type) {
135 case QCborValue::True:
136 json += "true";
137 break;
138 case QCborValue::False:
139 json += "false";
140 break;
141 case QCborValue::Integer:
142 json += QByteArray::number(v.toInteger());
143 break;
144 case QCborValue::Double: {
145 const double d = v.toDouble();
146 if (qIsFinite(d))
147 json += QByteArray::number(d, 'g', QLocale::FloatingPointShortest);
148 else
149 json += "null"; // +INF || -INF || NaN (see RFC4627#section2.4)
150 break;
151 }
152 case QCborValue::String:
153 json += '"';
154 json += escapedString(v.toString());
155 json += '"';
156 break;
157 case QCborValue::Array:
158 json += compact ? "[" : "[\n";
159 arrayContentToJson(
160 QJsonPrivate::Value::container(v), json, 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 QJsonPrivate::Value::container(v), json, 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(a->valueAt(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(i);
210 json += indentString;
211 json += '"';
212 json += escapedString(o->valueAt(i).toString());
213 json += compact ? "\":" : "\": ";
214 valueToJson(o->valueAt(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(json.size() + (o ? (int)o->elements.size() : 16));
229 json += compact ? "{" : "{\n";
230 objectContentToJson(o, json, 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(json.size() + (a ? (int)a->elements.size() : 16));
238 json += compact ? "[" : "[\n";
239 arrayContentToJson(a, json, indent + (compact ? 0 : 1), compact);
240 json += QByteArray(4*indent, ' ');
241 json += compact ? "]" : "]\n";
242}
243
244QT_END_NAMESPACE
245