Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/json/qjsonwriter.cpp |
Switch to Source code | Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
1 | - | |||||||
2 | - | |||||||
3 | - | |||||||
4 | using namespace QJsonPrivate; | - | ||||||
5 | - | |||||||
6 | static void objectContentToJson(const QJsonPrivate::Object *o, QByteArray &json, int indent, bool compact); | - | ||||||
7 | static void arrayContentToJson(const QJsonPrivate::Array *a, QByteArray &json, int indent, bool compact); | - | ||||||
8 | - | |||||||
9 | static inline uchar hexdig(uint u) | - | ||||||
10 | { | - | ||||||
11 | return (u < 0xa ? '0' + u : 'a' + u - 0xa); | - | ||||||
12 | } | - | ||||||
13 | - | |||||||
14 | static QByteArray escapedString(const QString &s) | - | ||||||
15 | { | - | ||||||
16 | const uchar replacement = '?'; | - | ||||||
17 | QByteArray ba(s.length(), Qt::Uninitialized); | - | ||||||
18 | - | |||||||
19 | uchar *cursor = reinterpret_cast<uchar *>(const_cast<char *>(ba.constData())); | - | ||||||
20 | const uchar *ba_end = cursor + ba.length(); | - | ||||||
21 | const ushort *src = reinterpret_cast<const ushort *>(s.constBegin()); | - | ||||||
22 | const ushort *const end = reinterpret_cast<const ushort *>(s.constEnd()); | - | ||||||
23 | - | |||||||
24 | while (src != end) { | - | ||||||
25 | if (cursor >= ba_end - 6) { | - | ||||||
26 | - | |||||||
27 | int pos = cursor - (const uchar *)ba.constData(); | - | ||||||
28 | ba.resize(ba.size()*2); | - | ||||||
29 | cursor = (uchar *)ba.data() + pos; | - | ||||||
30 | ba_end = (const uchar *)ba.constData() + ba.length(); | - | ||||||
31 | } | - | ||||||
32 | - | |||||||
33 | uint u = *src++; | - | ||||||
34 | if (u < 0x80) { | - | ||||||
35 | if (u < 0x20 || u == 0x22 || u == 0x5c) { | - | ||||||
36 | *cursor++ = '\\'; | - | ||||||
37 | switch (u) { | - | ||||||
38 | case 0x22: | - | ||||||
39 | *cursor++ = '"'; | - | ||||||
40 | break; | - | ||||||
41 | case 0x5c: | - | ||||||
42 | *cursor++ = '\\'; | - | ||||||
43 | break; | - | ||||||
44 | case 0x8: | - | ||||||
45 | *cursor++ = 'b'; | - | ||||||
46 | break; | - | ||||||
47 | case 0xc: | - | ||||||
48 | *cursor++ = 'f'; | - | ||||||
49 | break; | - | ||||||
50 | case 0xa: | - | ||||||
51 | *cursor++ = 'n'; | - | ||||||
52 | break; | - | ||||||
53 | case 0xd: | - | ||||||
54 | *cursor++ = 'r'; | - | ||||||
55 | break; | - | ||||||
56 | case 0x9: | - | ||||||
57 | *cursor++ = 't'; | - | ||||||
58 | break; | - | ||||||
59 | default: | - | ||||||
60 | *cursor++ = 'u'; | - | ||||||
61 | *cursor++ = '0'; | - | ||||||
62 | *cursor++ = '0'; | - | ||||||
63 | *cursor++ = hexdig(u>>4); | - | ||||||
64 | *cursor++ = hexdig(u & 0xf); | - | ||||||
65 | } | - | ||||||
66 | } else { | - | ||||||
67 | *cursor++ = (uchar)u; | - | ||||||
68 | } | - | ||||||
69 | } else { | - | ||||||
70 | if (QUtf8Functions::toUtf8<QUtf8BaseTraits>(u, cursor, src, end) < 0) | - | ||||||
71 | *cursor++ = replacement; | - | ||||||
72 | } | - | ||||||
73 | } | - | ||||||
74 | - | |||||||
75 | ba.resize(cursor - (const uchar *)ba.constData()); | - | ||||||
76 | return ba; | - | ||||||
77 | } | - | ||||||
78 | - | |||||||
79 | static void valueToJson(const QJsonPrivate::Base *b, const QJsonPrivate::Value &v, QByteArray &json, int indent, bool compact) | - | ||||||
80 | { | - | ||||||
81 | QJsonValue::Type type = (QJsonValue::Type)(uint)v.type; | - | ||||||
82 | switch (type) { | - | ||||||
83 | case never executed: QJsonValue::Bool:case QJsonValue::Bool: never executed: case QJsonValue::Bool: | 0 | ||||||
84 | json += v.toBoolean()
| 0 | ||||||
85 | break; never executed: break; | 0 | ||||||
86 | case never executed: QJsonValue::Double:case QJsonValue::Double: never executed: {case QJsonValue::Double: | 0 | ||||||
87 | const double d = v.toDouble(b); | - | ||||||
88 | if (qIsFinite(d)
| 0 | ||||||
89 | json += QByteArray::number(d, 'g', stdQLocale::numeric_limits<double>::digits10 + 2FloatingPointShortest); never executed: json += QByteArray::number(d, 'g', QLocale::FloatingPointShortest); | 0 | ||||||
90 | else | - | ||||||
91 | json += "null"; never executed: json += "null"; | 0 | ||||||
92 | break; never executed: break; | 0 | ||||||
93 | } | - | ||||||
94 | case never executed: QJsonValue::String:case QJsonValue::String: never executed: case QJsonValue::String: | 0 | ||||||
95 | json += '"'; | - | ||||||
96 | json += escapedString(v.toString(b)); | - | ||||||
97 | json += '"'; | - | ||||||
98 | break; never executed: break; | 0 | ||||||
99 | case never executed: QJsonValue::Array:case QJsonValue::Array: never executed: case QJsonValue::Array: | 0 | ||||||
100 | json += compact
| 0 | ||||||
101 | arrayContentToJson(static_cast<QJsonPrivate::Array *>(v.base(b)), json, indent + (compact ? 0 : 1), compact); | - | ||||||
102 | json += QByteArray(4*indent, ' '); | - | ||||||
103 | json += ']'; | - | ||||||
104 | break; never executed: break; | 0 | ||||||
105 | case never executed: QJsonValue::Object:case QJsonValue::Object: never executed: case QJsonValue::Object: | 0 | ||||||
106 | json += compact
| 0 | ||||||
107 | objectContentToJson(static_cast<QJsonPrivate::Object *>(v.base(b)), json, indent + (compact ? 0 : 1), compact); | - | ||||||
108 | json += QByteArray(4*indent, ' '); | - | ||||||
109 | json += '}'; | - | ||||||
110 | break; never executed: break; | 0 | ||||||
111 | case never executed: QJsonValue::Null:case QJsonValue::Null: never executed: case QJsonValue::Null: | 0 | ||||||
112 | default never executed: :default: never executed: default: | 0 | ||||||
113 | json += "null"; | - | ||||||
114 | } never executed: end of block | 0 | ||||||
115 | } | - | ||||||
116 | - | |||||||
117 | static void arrayContentToJson(const QJsonPrivate::Array *a, QByteArray &json, int indent, bool compact) | - | ||||||
118 | { | - | ||||||
119 | if (!a || !a->length) | - | ||||||
120 | return; | - | ||||||
121 | - | |||||||
122 | QByteArray indentString(4*indent, ' '); | - | ||||||
123 | - | |||||||
124 | uint i = 0; | - | ||||||
125 | while (1) { | - | ||||||
126 | json += indentString; | - | ||||||
127 | valueToJson(a, a->at(i), json, indent, compact); | - | ||||||
128 | - | |||||||
129 | if (++i == a->length) { | - | ||||||
130 | if (!compact) | - | ||||||
131 | json += '\n'; | - | ||||||
132 | break; | - | ||||||
133 | } | - | ||||||
134 | - | |||||||
135 | json += compact ? "," : ",\n"; | - | ||||||
136 | } | - | ||||||
137 | } | - | ||||||
138 | - | |||||||
139 | - | |||||||
140 | static void objectContentToJson(const QJsonPrivate::Object *o, QByteArray &json, int indent, bool compact) | - | ||||||
141 | { | - | ||||||
142 | if (!o || !o->length) | - | ||||||
143 | return; | - | ||||||
144 | - | |||||||
145 | QByteArray indentString(4*indent, ' '); | - | ||||||
146 | - | |||||||
147 | uint i = 0; | - | ||||||
148 | while (1) { | - | ||||||
149 | QJsonPrivate::Entry *e = o->entryAt(i); | - | ||||||
150 | json += indentString; | - | ||||||
151 | json += '"'; | - | ||||||
152 | json += escapedString(e->key()); | - | ||||||
153 | json += compact ? "\":" : "\": "; | - | ||||||
154 | valueToJson(o, e->value, json, indent, compact); | - | ||||||
155 | - | |||||||
156 | if (++i == o->length) { | - | ||||||
157 | if (!compact) | - | ||||||
158 | json += '\n'; | - | ||||||
159 | break; | - | ||||||
160 | } | - | ||||||
161 | - | |||||||
162 | json += compact ? "," : ",\n"; | - | ||||||
163 | } | - | ||||||
164 | } | - | ||||||
165 | - | |||||||
166 | void Writer::objectToJson(const QJsonPrivate::Object *o, QByteArray &json, int indent, bool compact) | - | ||||||
167 | { | - | ||||||
168 | json.reserve(json.size() + (o ? (int)o->size : 16)); | - | ||||||
169 | json += compact ? "{" : "{\n"; | - | ||||||
170 | objectContentToJson(o, json, indent + (compact ? 0 : 1), compact); | - | ||||||
171 | json += QByteArray(4*indent, ' '); | - | ||||||
172 | json += compact ? "}" : "}\n"; | - | ||||||
173 | } | - | ||||||
174 | - | |||||||
175 | void Writer::arrayToJson(const QJsonPrivate::Array *a, QByteArray &json, int indent, bool compact) | - | ||||||
176 | { | - | ||||||
177 | json.reserve(json.size() + (a ? (int)a->size : 16)); | - | ||||||
178 | json += compact ? "[" : "[\n"; | - | ||||||
179 | arrayContentToJson(a, json, indent + (compact ? 0 : 1), compact); | - | ||||||
180 | json += QByteArray(4*indent, ' '); | - | ||||||
181 | json += compact ? "]" : "]\n"; | - | ||||||
182 | } | - | ||||||
183 | - | |||||||
184 | - | |||||||
Switch to Source code | Preprocessed file |