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