1 //
2 //
3 // Copyright 2015 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18
19 #include <grpc/support/port_platform.h>
20 #include <stdint.h>
21 #include <stdlib.h>
22
23 #include <map>
24 #include <string>
25 #include <utility>
26 #include <vector>
27
28 #include "absl/strings/string_view.h"
29 #include "src/core/util/json/json.h"
30
31 namespace grpc_core {
32
33 namespace {
34
35 // The idea of the writer is basically symmetrical of the reader. While the
36 // reader emits various calls to your code, the writer takes basically the
37 // same calls and emit json out of it. It doesn't try to make any check on
38 // the order of the calls you do on it. Meaning you can theoretically force
39 // it to generate invalid json.
40 //
41 // Also, unlike the reader, the writer expects UTF-8 encoded input strings.
42 // These strings will be UTF-8 validated, and any invalid character will
43 // cut the conversion short, before any invalid UTF-8 sequence, thus forming
44 // a valid UTF-8 string overall.
45 //
46 class JsonWriter {
47 public:
48 static std::string Dump(const Json& value, int indent);
49
50 private:
JsonWriter(int indent)51 explicit JsonWriter(int indent) : indent_(indent) {}
52
53 void OutputCheck(size_t needed);
54 void OutputChar(char c);
55 void OutputString(const absl::string_view str);
56 void OutputIndent();
57 void ValueEnd();
58 void EscapeUtf16(uint16_t utf16);
59 void EscapeString(const std::string& string);
60 void ContainerBegins(Json::Type type);
61 void ContainerEnds(Json::Type type);
62 void ObjectKey(const std::string& string);
63 void ValueRaw(const std::string& string);
64 void ValueString(const std::string& string);
65
66 void DumpObject(const Json::Object& object);
67 void DumpArray(const Json::Array& array);
68 void DumpValue(const Json& value);
69
70 int indent_;
71 int depth_ = 0;
72 bool container_empty_ = true;
73 bool got_key_ = false;
74 std::string output_;
75 };
76
77 // This function checks if there's enough space left in the output buffer,
78 // and will enlarge it if necessary. We're only allocating chunks of 256
79 // bytes at a time (or multiples thereof).
80 //
OutputCheck(size_t needed)81 void JsonWriter::OutputCheck(size_t needed) {
82 size_t free_space = output_.capacity() - output_.size();
83 if (free_space >= needed) return;
84 needed -= free_space;
85 // Round up by 256 bytes.
86 needed = (needed + 0xff) & ~0xffU;
87 output_.reserve(output_.capacity() + needed);
88 }
89
OutputChar(char c)90 void JsonWriter::OutputChar(char c) {
91 OutputCheck(1);
92 output_.push_back(c);
93 }
94
OutputString(const absl::string_view str)95 void JsonWriter::OutputString(const absl::string_view str) {
96 OutputCheck(str.size());
97 output_.append(str.data(), str.size());
98 }
99
OutputIndent()100 void JsonWriter::OutputIndent() {
101 static const char spacesstr[] =
102 " "
103 " "
104 " "
105 " ";
106 unsigned spaces = static_cast<unsigned>(depth_ * indent_);
107 if (indent_ == 0) return;
108 if (got_key_) {
109 OutputChar(' ');
110 return;
111 }
112 while (spaces >= (sizeof(spacesstr) - 1)) {
113 OutputString(absl::string_view(spacesstr, sizeof(spacesstr) - 1));
114 spaces -= static_cast<unsigned>(sizeof(spacesstr) - 1);
115 }
116 if (spaces == 0) return;
117 OutputString(
118 absl::string_view(spacesstr + sizeof(spacesstr) - 1 - spaces, spaces));
119 }
120
ValueEnd()121 void JsonWriter::ValueEnd() {
122 if (container_empty_) {
123 container_empty_ = false;
124 if (indent_ == 0 || depth_ == 0) return;
125 OutputChar('\n');
126 } else {
127 OutputChar(',');
128 if (indent_ == 0) return;
129 OutputChar('\n');
130 }
131 }
132
EscapeUtf16(uint16_t utf16)133 void JsonWriter::EscapeUtf16(uint16_t utf16) {
134 static const char hex[] = "0123456789abcdef";
135 OutputString(absl::string_view("\\u", 2));
136 OutputChar(hex[(utf16 >> 12) & 0x0f]);
137 OutputChar(hex[(utf16 >> 8) & 0x0f]);
138 OutputChar(hex[(utf16 >> 4) & 0x0f]);
139 OutputChar(hex[(utf16) & 0x0f]);
140 }
141
EscapeString(const std::string & string)142 void JsonWriter::EscapeString(const std::string& string) {
143 OutputChar('"');
144 for (size_t idx = 0; idx < string.size(); ++idx) {
145 uint8_t c = static_cast<uint8_t>(string[idx]);
146 if (c >= 32 && c <= 126) {
147 if (c == '\\' || c == '"') OutputChar('\\');
148 OutputChar(static_cast<char>(c));
149 } else if (c < 32 || c == 127) {
150 switch (c) {
151 case '\b':
152 OutputString(absl::string_view("\\b", 2));
153 break;
154 case '\f':
155 OutputString(absl::string_view("\\f", 2));
156 break;
157 case '\n':
158 OutputString(absl::string_view("\\n", 2));
159 break;
160 case '\r':
161 OutputString(absl::string_view("\\r", 2));
162 break;
163 case '\t':
164 OutputString(absl::string_view("\\t", 2));
165 break;
166 default:
167 EscapeUtf16(c);
168 break;
169 }
170 } else {
171 uint32_t utf32 = 0;
172 int extra = 0;
173 int i;
174 int valid = 1;
175 if ((c & 0xe0) == 0xc0) {
176 utf32 = c & 0x1f;
177 extra = 1;
178 } else if ((c & 0xf0) == 0xe0) {
179 utf32 = c & 0x0f;
180 extra = 2;
181 } else if ((c & 0xf8) == 0xf0) {
182 utf32 = c & 0x07;
183 extra = 3;
184 } else {
185 break;
186 }
187 for (i = 0; i < extra; i++) {
188 utf32 <<= 6;
189 ++idx;
190 // Breaks out and bail if we hit the end of the string.
191 if (idx == string.size()) {
192 valid = 0;
193 break;
194 }
195 c = static_cast<uint8_t>(string[idx]);
196 // Breaks out and bail on any invalid UTF-8 sequence, including \0.
197 if ((c & 0xc0) != 0x80) {
198 valid = 0;
199 break;
200 }
201 utf32 |= c & 0x3f;
202 }
203 if (!valid) break;
204 // The range 0xd800 - 0xdfff is reserved by the surrogates ad vitam.
205 // Any other range is technically reserved for future usage, so if we
206 // don't want the software to break in the future, we have to allow
207 // anything else. The first non-unicode character is 0x110000.
208 if (((utf32 >= 0xd800) && (utf32 <= 0xdfff)) || (utf32 >= 0x110000)) {
209 break;
210 }
211 if (utf32 >= 0x10000) {
212 // If utf32 contains a character that is above 0xffff, it needs to be
213 // broken down into a utf-16 surrogate pair. A surrogate pair is first
214 // a high surrogate, followed by a low surrogate. Each surrogate holds
215 // 10 bits of usable data, thus allowing a total of 20 bits of data.
216 // The high surrogate marker is 0xd800, while the low surrogate marker
217 // is 0xdc00. The low 10 bits of each will be the usable data.
218 //
219 // After re-combining the 20 bits of data, one has to add 0x10000 to
220 // the resulting value, in order to obtain the original character.
221 // This is obviously because the range 0x0000 - 0xffff can be written
222 // without any special trick.
223 //
224 // Since 0x10ffff is the highest allowed character, we're working in
225 // the range 0x00000 - 0xfffff after we decrement it by 0x10000.
226 // That range is exactly 20 bits.
227 //
228 utf32 -= 0x10000;
229 EscapeUtf16(static_cast<uint16_t>(0xd800 | (utf32 >> 10)));
230 EscapeUtf16(static_cast<uint16_t>(0xdc00 | (utf32 & 0x3ff)));
231 } else {
232 EscapeUtf16(static_cast<uint16_t>(utf32));
233 }
234 }
235 }
236 OutputChar('"');
237 }
238
ContainerBegins(Json::Type type)239 void JsonWriter::ContainerBegins(Json::Type type) {
240 if (!got_key_) ValueEnd();
241 OutputIndent();
242 OutputChar(type == Json::Type::kObject ? '{' : '[');
243 container_empty_ = true;
244 got_key_ = false;
245 depth_++;
246 }
247
ContainerEnds(Json::Type type)248 void JsonWriter::ContainerEnds(Json::Type type) {
249 if (indent_ && !container_empty_) OutputChar('\n');
250 depth_--;
251 if (!container_empty_) OutputIndent();
252 OutputChar(type == Json::Type::kObject ? '}' : ']');
253 container_empty_ = false;
254 got_key_ = false;
255 }
256
ObjectKey(const std::string & string)257 void JsonWriter::ObjectKey(const std::string& string) {
258 ValueEnd();
259 OutputIndent();
260 EscapeString(string);
261 OutputChar(':');
262 got_key_ = true;
263 }
264
ValueRaw(const std::string & string)265 void JsonWriter::ValueRaw(const std::string& string) {
266 if (!got_key_) ValueEnd();
267 OutputIndent();
268 OutputString(string);
269 got_key_ = false;
270 }
271
ValueString(const std::string & string)272 void JsonWriter::ValueString(const std::string& string) {
273 if (!got_key_) ValueEnd();
274 OutputIndent();
275 EscapeString(string);
276 got_key_ = false;
277 }
278
DumpObject(const Json::Object & object)279 void JsonWriter::DumpObject(const Json::Object& object) {
280 ContainerBegins(Json::Type::kObject);
281 for (const auto& p : object) {
282 ObjectKey(p.first);
283 DumpValue(p.second);
284 }
285 ContainerEnds(Json::Type::kObject);
286 }
287
DumpArray(const Json::Array & array)288 void JsonWriter::DumpArray(const Json::Array& array) {
289 ContainerBegins(Json::Type::kArray);
290 for (const auto& v : array) {
291 DumpValue(v);
292 }
293 ContainerEnds(Json::Type::kArray);
294 }
295
DumpValue(const Json & value)296 void JsonWriter::DumpValue(const Json& value) {
297 switch (value.type()) {
298 case Json::Type::kObject:
299 DumpObject(value.object());
300 break;
301 case Json::Type::kArray:
302 DumpArray(value.array());
303 break;
304 case Json::Type::kString:
305 ValueString(value.string());
306 break;
307 case Json::Type::kNumber:
308 ValueRaw(value.string());
309 break;
310 case Json::Type::kBoolean:
311 if (value.boolean()) {
312 ValueRaw(std::string("true", 4));
313 } else {
314 ValueRaw(std::string("false", 5));
315 }
316 break;
317 case Json::Type::kNull:
318 ValueRaw(std::string("null", 4));
319 break;
320 default:
321 GPR_UNREACHABLE_CODE(abort());
322 }
323 }
324
Dump(const Json & value,int indent)325 std::string JsonWriter::Dump(const Json& value, int indent) {
326 JsonWriter writer(indent);
327 writer.DumpValue(value);
328 return std::move(writer.output_);
329 }
330
331 } // namespace
332
JsonDump(const Json & json,int indent)333 std::string JsonDump(const Json& json, int indent) {
334 return JsonWriter::Dump(json, indent);
335 }
336
337 } // namespace grpc_core
338