| 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); never executed: return (u < 0xa ? '0' + u : 'a' + u - 0xa); | 0 | ||||||||||||||||||
| 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) {
| 0 | ||||||||||||||||||
| 69 | if (cursor >= ba_end - 6) {
| 0 | ||||||||||||||||||
| 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 | } never executed: end of block | 0 | ||||||||||||||||||
| 76 | - | |||||||||||||||||||
| 77 | uint u = *src++; | - | ||||||||||||||||||
| 78 | if (u < 0x80) {
| 0 | ||||||||||||||||||
| 79 | if (u < 0x20 || u == 0x22 || u == 0x5c) {
| 0 | ||||||||||||||||||
| 80 | *cursor++ = '\\'; | - | ||||||||||||||||||
| 81 | switch (u) { | - | ||||||||||||||||||
| 82 | case 0x22: never executed: case 0x22: | 0 | ||||||||||||||||||
| 83 | *cursor++ = '"'; | - | ||||||||||||||||||
| 84 | break; never executed: break; | 0 | ||||||||||||||||||
| 85 | case 0x5c: never executed: case 0x5c: | 0 | ||||||||||||||||||
| 86 | *cursor++ = '\\'; | - | ||||||||||||||||||
| 87 | break; never executed: break; | 0 | ||||||||||||||||||
| 88 | case 0x8: never executed: case 0x8: | 0 | ||||||||||||||||||
| 89 | *cursor++ = 'b'; | - | ||||||||||||||||||
| 90 | break; never executed: break; | 0 | ||||||||||||||||||
| 91 | case 0xc: never executed: case 0xc: | 0 | ||||||||||||||||||
| 92 | *cursor++ = 'f'; | - | ||||||||||||||||||
| 93 | break; never executed: break; | 0 | ||||||||||||||||||
| 94 | case 0xa: never executed: case 0xa: | 0 | ||||||||||||||||||
| 95 | *cursor++ = 'n'; | - | ||||||||||||||||||
| 96 | break; never executed: break; | 0 | ||||||||||||||||||
| 97 | case 0xd: never executed: case 0xd: | 0 | ||||||||||||||||||
| 98 | *cursor++ = 'r'; | - | ||||||||||||||||||
| 99 | break; never executed: break; | 0 | ||||||||||||||||||
| 100 | case 0x9: never executed: case 0x9: | 0 | ||||||||||||||||||
| 101 | *cursor++ = 't'; | - | ||||||||||||||||||
| 102 | break; never executed: break; | 0 | ||||||||||||||||||
| 103 | default: never executed: default: | 0 | ||||||||||||||||||
| 104 | *cursor++ = 'u'; | - | ||||||||||||||||||
| 105 | *cursor++ = '0'; | - | ||||||||||||||||||
| 106 | *cursor++ = '0'; | - | ||||||||||||||||||
| 107 | *cursor++ = hexdig(u>>4); | - | ||||||||||||||||||
| 108 | *cursor++ = hexdig(u & 0xf); | - | ||||||||||||||||||
| 109 | } never executed: end of block | 0 | ||||||||||||||||||
| 110 | } else { | - | ||||||||||||||||||
| 111 | *cursor++ = (uchar)u; | - | ||||||||||||||||||
| 112 | } never executed: end of block | 0 | ||||||||||||||||||
| 113 | } else { | - | ||||||||||||||||||
| 114 | if (QUtf8Functions::toUtf8<QUtf8BaseTraits>(u, cursor, src, end) < 0)
| 0 | ||||||||||||||||||
| 115 | *cursor++ = replacement; never executed: *cursor++ = replacement; | 0 | ||||||||||||||||||
| 116 | } never executed: end of block | 0 | ||||||||||||||||||
| 117 | } | - | ||||||||||||||||||
| 118 | - | |||||||||||||||||||
| 119 | ba.resize(cursor - (const uchar *)ba.constData()); | - | ||||||||||||||||||
| 120 | return ba; never executed: return ba; | 0 | ||||||||||||||||||
| 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', QLocale::FloatingPointShortest); 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)
| 0 | ||||||||||||||||||
| 164 | return; never executed: return; | 0 | ||||||||||||||||||
| 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) {
| 0 | ||||||||||||||||||
| 174 | if (!compact)
| 0 | ||||||||||||||||||
| 175 | json += '\n'; never executed: json += '\n'; | 0 | ||||||||||||||||||
| 176 | break; never executed: break; | 0 | ||||||||||||||||||
| 177 | } | - | ||||||||||||||||||
| 178 | - | |||||||||||||||||||
| 179 | json += compact ? "," : ",\n";
| 0 | ||||||||||||||||||
| 180 | } never executed: end of block | 0 | ||||||||||||||||||
| 181 | } never executed: end of block | 0 | ||||||||||||||||||
| 182 | - | |||||||||||||||||||
| 183 | - | |||||||||||||||||||
| 184 | static void objectContentToJson(const QJsonPrivate::Object *o, QByteArray &json, int indent, bool compact) | - | ||||||||||||||||||
| 185 | { | - | ||||||||||||||||||
| 186 | if (!o || !o->length)
| 0 | ||||||||||||||||||
| 187 | return; never executed: return; | 0 | ||||||||||||||||||
| 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 ? "\":" : "\": ";
| 0 | ||||||||||||||||||
| 198 | valueToJson(o, e->value, json, indent, compact); | - | ||||||||||||||||||
| 199 | - | |||||||||||||||||||
| 200 | if (++i == o->length) {
| 0 | ||||||||||||||||||
| 201 | if (!compact)
| 0 | ||||||||||||||||||
| 202 | json += '\n'; never executed: json += '\n'; | 0 | ||||||||||||||||||
| 203 | break; never executed: break; | 0 | ||||||||||||||||||
| 204 | } | - | ||||||||||||||||||
| 205 | - | |||||||||||||||||||
| 206 | json += compact ? "," : ",\n";
| 0 | ||||||||||||||||||
| 207 | } never executed: end of block | 0 | ||||||||||||||||||
| 208 | } never executed: end of block | 0 | ||||||||||||||||||
| 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";
| 0 | ||||||||||||||||||
| 214 | objectContentToJson(o, json, indent + (compact ? 0 : 1), compact); | - | ||||||||||||||||||
| 215 | json += QByteArray(4*indent, ' '); | - | ||||||||||||||||||
| 216 | json += compact ? "}" : "}\n";
| 0 | ||||||||||||||||||
| 217 | } never executed: end of block | 0 | ||||||||||||||||||
| 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";
| 0 | ||||||||||||||||||
| 223 | arrayContentToJson(a, json, indent + (compact ? 0 : 1), compact); | - | ||||||||||||||||||
| 224 | json += QByteArray(4*indent, ' '); | - | ||||||||||||||||||
| 225 | json += compact ? "]" : "]\n";
| 0 | ||||||||||||||||||
| 226 | } never executed: end of block | 0 | ||||||||||||||||||
| 227 | - | |||||||||||||||||||
| 228 | QT_END_NAMESPACE | - | ||||||||||||||||||
| Source code | Switch to Preprocessed file |