28 QByteArray ba(qMax(s.size(), 16), Qt::Uninitialized);
30 auto ba_const_start = [&]() {
return reinterpret_cast<
const uchar *>(ba.constData()); };
31 uchar *cursor =
reinterpret_cast<uchar *>(
const_cast<
char *>(ba.constData()));
32 const uchar *ba_end = cursor + ba.size();
33 const char16_t *src = s.utf16();
34 const char16_t *
const end = s.utf16() + s.size();
37 if (cursor >= ba_end - 6) {
39 qptrdiff pos = cursor - ba_const_start();
40 ba.resize(ba.size()*2);
41 cursor =
reinterpret_cast<uchar *>(ba.data()) + pos;
42 ba_end = ba_const_start() + ba.size();
47 if (u < 0x20 || u == 0x22 || u == 0x5c) {
75 *cursor++ = hexdig(u>>4);
76 *cursor++ = hexdig(u & 0xf);
81 }
else if (QUtf8Functions::toUtf8<QUtf8BaseTraits>(u, cursor, src, end) < 0) {
85 *cursor++ = hexdig(u>>12 & 0x0f);
86 *cursor++ = hexdig(u>>8 & 0x0f);
87 *cursor++ = hexdig(u>>4 & 0x0f);
88 *cursor++ = hexdig(u & 0x0f);
92 ba.resize(cursor - ba_const_start());
96static void valueToJson(
const QCborValue &v, QByteArray &json,
int indent,
bool compact)
98 QCborValue::Type type = v.type();
100 case QCborValue::True:
103 case QCborValue::False:
106 case QCborValue::Integer:
107 json += QByteArray::number(v.toInteger());
109 case QCborValue::Double: {
110 const double d = v.toDouble();
112 json += QByteArray::number(d,
'g', QLocale::FloatingPointShortest);
117 case QCborValue::String:
119 json += escapedString(v.toString());
122 case QCborValue::Array:
123 json += compact ?
"[" :
"[\n";
126 json += QByteArray(4*indent,
' ');
129 case QCborValue::Map:
130 json += compact ?
"{" :
"{\n";
133 json += QByteArray(4*indent,
' ');
136 case QCborValue::Null:
144 if (!a || a->elements.empty())
147 QByteArray indentString(4*indent,
' ');
151 json += indentString;
152 valueToJson(a->valueAt(i), json, indent, compact);
154 if (++i == a->elements.size()) {
160 json += compact ?
"," :
",\n";
167 if (!o || o->elements.empty())
170 QByteArray indentString(4*indent,
' ');
175 json += indentString;
177 json += escapedString(o->valueAt(i).toString());
178 json += compact ?
"\":" :
"\": ";
179 valueToJson(o->valueAt(i + 1), json, indent, compact);
181 if ((i += 2) == o->elements.size()) {
187 json += compact ?
"," :
",\n";
191void Writer::objectToJson(
const QCborContainerPrivate *o, QByteArray &json,
int indent,
bool compact)
193 json.reserve(json.size() + (o ? (
int)o->elements.size() : 16));
194 json += compact ?
"{" :
"{\n";
195 objectContentToJson(o, json, indent + (compact ? 0 : 1), compact);
196 json += QByteArray(4*indent,
' ');
197 json += compact ?
"}" :
"}\n";
200void Writer::arrayToJson(
const QCborContainerPrivate *a, QByteArray &json,
int indent,
bool compact)
202 json.reserve(json.size() + (a ? (
int)a->elements.size() : 16));
203 json += compact ?
"[" :
"[\n";
204 arrayContentToJson(a, json, indent + (compact ? 0 : 1), compact);
205 json += QByteArray(4*indent,
' ');
206 json += compact ?
"]" :
"]\n";