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