json_reader.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015-2016 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 
20 
21 #include <inttypes.h>
22 #include <stdlib.h>
23 
24 #include <algorithm>
25 #include <map>
26 #include <string>
27 #include <utility>
28 #include <vector>
29 
30 #include "absl/base/attributes.h"
31 #include "absl/strings/str_cat.h"
32 #include "absl/strings/str_format.h"
33 #include "absl/strings/string_view.h"
34 
35 #include <grpc/support/log.h>
36 
38 #include "src/core/lib/json/json.h"
39 
40 #define GRPC_JSON_MAX_DEPTH 255
41 #define GRPC_JSON_MAX_ERRORS 16
42 
43 namespace grpc_core {
44 
45 namespace {
46 
47 class JsonReader {
48  public:
50 
51  private:
52  enum class Status {
53  GRPC_JSON_DONE, /* The parser finished successfully. */
54  GRPC_JSON_PARSE_ERROR, /* The parser found an error in the json stream. */
55  GRPC_JSON_INTERNAL_ERROR /* The parser got an internal error. */
56  };
57 
58  enum class State {
59  GRPC_JSON_STATE_OBJECT_KEY_BEGIN,
60  GRPC_JSON_STATE_OBJECT_KEY_STRING,
61  GRPC_JSON_STATE_OBJECT_KEY_END,
62  GRPC_JSON_STATE_VALUE_BEGIN,
63  GRPC_JSON_STATE_VALUE_STRING,
64  GRPC_JSON_STATE_STRING_ESCAPE,
65  GRPC_JSON_STATE_STRING_ESCAPE_U1,
66  GRPC_JSON_STATE_STRING_ESCAPE_U2,
67  GRPC_JSON_STATE_STRING_ESCAPE_U3,
68  GRPC_JSON_STATE_STRING_ESCAPE_U4,
69  GRPC_JSON_STATE_VALUE_NUMBER,
70  GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL,
71  GRPC_JSON_STATE_VALUE_NUMBER_ZERO,
72  GRPC_JSON_STATE_VALUE_NUMBER_DOT,
73  GRPC_JSON_STATE_VALUE_NUMBER_E,
74  GRPC_JSON_STATE_VALUE_NUMBER_EPM,
75  GRPC_JSON_STATE_VALUE_TRUE_R,
76  GRPC_JSON_STATE_VALUE_TRUE_U,
77  GRPC_JSON_STATE_VALUE_TRUE_E,
78  GRPC_JSON_STATE_VALUE_FALSE_A,
79  GRPC_JSON_STATE_VALUE_FALSE_L,
80  GRPC_JSON_STATE_VALUE_FALSE_S,
81  GRPC_JSON_STATE_VALUE_FALSE_E,
82  GRPC_JSON_STATE_VALUE_NULL_U,
83  GRPC_JSON_STATE_VALUE_NULL_L1,
84  GRPC_JSON_STATE_VALUE_NULL_L2,
85  GRPC_JSON_STATE_VALUE_END,
86  GRPC_JSON_STATE_END
87  };
88 
89  /* The first non-unicode value is 0x110000. But let's pick
90  * a value high enough to start our error codes from. These
91  * values are safe to return from the read_char function.
92  */
93  static constexpr uint32_t GRPC_JSON_READ_CHAR_EOF = 0x7ffffff0;
94 
95  explicit JsonReader(absl::string_view input)
96  : original_input_(reinterpret_cast<const uint8_t*>(input.data())),
99 
100  Status Run();
101  uint32_t ReadChar();
102  bool IsComplete();
103 
104  size_t CurrentIndex() const { return input_ - original_input_ - 1; }
105 
106  GRPC_MUST_USE_RESULT bool StringAddChar(uint32_t c);
107  GRPC_MUST_USE_RESULT bool StringAddUtf32(uint32_t c);
108 
109  Json* CreateAndLinkValue();
110  bool StartContainer(Json::Type type);
111  void EndContainer();
112  void SetKey();
113  void SetString();
114  bool SetNumber();
115  void SetTrue();
116  void SetFalse();
117  void SetNull();
118 
120  const uint8_t* input_;
122 
123  State state_ = State::GRPC_JSON_STATE_VALUE_BEGIN;
125  bool container_just_begun_ = false;
128  std::vector<grpc_error_handle> errors_;
129  bool truncated_errors_ = false;
131 
133  std::vector<Json*> stack_;
134 
137 };
138 
139 bool JsonReader::StringAddChar(uint32_t c) {
140  switch (utf8_bytes_remaining_) {
141  case 0:
142  if ((c & 0x80) == 0) {
144  } else if ((c & 0xe0) == 0xc0) {
146  } else if ((c & 0xf0) == 0xe0) {
148  } else if ((c & 0xf8) == 0xf0) {
150  } else {
151  return false;
152  }
153  break;
154  case 1:
155  case 2:
156  case 3:
157  if ((c & 0xc0) != 0x80) return false;
159  break;
160  default:
161  abort();
162  }
163  string_.push_back(static_cast<uint8_t>(c));
164  return true;
165 }
166 
167 bool JsonReader::StringAddUtf32(uint32_t c) {
168  if (c <= 0x7f) {
169  return StringAddChar(c);
170  } else if (c <= 0x7ff) {
171  uint32_t b1 = 0xc0 | ((c >> 6) & 0x1f);
172  uint32_t b2 = 0x80 | (c & 0x3f);
173  return StringAddChar(b1) && StringAddChar(b2);
174  } else if (c <= 0xffff) {
175  uint32_t b1 = 0xe0 | ((c >> 12) & 0x0f);
176  uint32_t b2 = 0x80 | ((c >> 6) & 0x3f);
177  uint32_t b3 = 0x80 | (c & 0x3f);
178  return StringAddChar(b1) && StringAddChar(b2) && StringAddChar(b3);
179  } else if (c <= 0x1fffff) {
180  uint32_t b1 = 0xf0 | ((c >> 18) & 0x07);
181  uint32_t b2 = 0x80 | ((c >> 12) & 0x3f);
182  uint32_t b3 = 0x80 | ((c >> 6) & 0x3f);
183  uint32_t b4 = 0x80 | (c & 0x3f);
184  return StringAddChar(b1) && StringAddChar(b2) && StringAddChar(b3) &&
185  StringAddChar(b4);
186  } else {
187  return false;
188  }
189 }
190 
191 uint32_t JsonReader::ReadChar() {
192  if (remaining_input_ == 0) return GRPC_JSON_READ_CHAR_EOF;
193  const uint32_t r = *input_++;
195  if (r == 0) {
196  remaining_input_ = 0;
198  }
199  return r;
200 }
201 
202 Json* JsonReader::CreateAndLinkValue() {
203  Json* value;
204  if (stack_.empty()) {
205  value = &root_value_;
206  } else {
207  Json* parent = stack_.back();
208  if (parent->type() == Json::Type::OBJECT) {
209  if (parent->object_value().find(key_) != parent->object_value().end()) {
210  if (errors_.size() == GRPC_JSON_MAX_ERRORS) {
211  truncated_errors_ = true;
212  } else {
214  absl::StrFormat("duplicate key \"%s\" at index %" PRIuPTR, key_,
215  CurrentIndex())));
216  }
217  }
218  value = &(*parent->mutable_object())[std::move(key_)];
219  } else {
220  GPR_ASSERT(parent->type() == Json::Type::ARRAY);
221  parent->mutable_array()->emplace_back();
222  value = &parent->mutable_array()->back();
223  }
224  }
225  return value;
226 }
227 
228 bool JsonReader::StartContainer(Json::Type type) {
229  if (stack_.size() == GRPC_JSON_MAX_DEPTH) {
230  if (errors_.size() == GRPC_JSON_MAX_ERRORS) {
231  truncated_errors_ = true;
232  } else {
234  absl::StrFormat("exceeded max stack depth (%d) at index %" PRIuPTR,
235  GRPC_JSON_MAX_DEPTH, CurrentIndex())));
236  }
237  return false;
238  }
239  Json* value = CreateAndLinkValue();
240  if (type == Json::Type::OBJECT) {
241  *value = Json::Object();
242  } else {
244  *value = Json::Array();
245  }
246  stack_.push_back(value);
247  return true;
248 }
249 
250 void JsonReader::EndContainer() {
251  GPR_ASSERT(!stack_.empty());
252  stack_.pop_back();
253 }
254 
255 void JsonReader::SetKey() {
257  string_.clear();
258 }
259 
260 void JsonReader::SetString() {
261  Json* value = CreateAndLinkValue();
262  *value = std::move(string_);
263  string_.clear();
264 }
265 
266 bool JsonReader::SetNumber() {
267  Json* value = CreateAndLinkValue();
268  *value = Json(string_, /*is_number=*/true);
269  string_.clear();
270  return true;
271 }
272 
273 void JsonReader::SetTrue() {
274  Json* value = CreateAndLinkValue();
275  *value = true;
276  string_.clear();
277 }
278 
279 void JsonReader::SetFalse() {
280  Json* value = CreateAndLinkValue();
281  *value = false;
282  string_.clear();
283 }
284 
285 void JsonReader::SetNull() { CreateAndLinkValue(); }
286 
287 bool JsonReader::IsComplete() {
288  return (stack_.empty() && (state_ == State::GRPC_JSON_STATE_END ||
289  state_ == State::GRPC_JSON_STATE_VALUE_END));
290 }
291 
292 /* Call this function to start parsing the input. It will return the following:
293  * . GRPC_JSON_DONE if the input got eof, and the parsing finished
294  * successfully.
295  * . GRPC_JSON_PARSE_ERROR if the input was somehow invalid.
296  * . GRPC_JSON_INTERNAL_ERROR if the parser somehow ended into an invalid
297  * internal state.
298  */
300  uint32_t c;
301 
302  /* This state-machine is a strict implementation of ECMA-404 */
303  while (true) {
304  c = ReadChar();
305  switch (c) {
306  /* Let's process the error case first. */
308  switch (state_) {
309  case State::GRPC_JSON_STATE_VALUE_NUMBER:
310  case State::GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL:
311  case State::GRPC_JSON_STATE_VALUE_NUMBER_ZERO:
312  case State::GRPC_JSON_STATE_VALUE_NUMBER_EPM:
313  if (!SetNumber()) return Status::GRPC_JSON_PARSE_ERROR;
314  state_ = State::GRPC_JSON_STATE_VALUE_END;
315  break;
316 
317  default:
318  break;
319  }
320  if (IsComplete()) {
321  return Status::GRPC_JSON_DONE;
322  }
323  return Status::GRPC_JSON_PARSE_ERROR;
324 
325  /* Processing whitespaces. */
326  case ' ':
327  case '\t':
328  case '\n':
329  case '\r':
330  switch (state_) {
331  case State::GRPC_JSON_STATE_OBJECT_KEY_BEGIN:
332  case State::GRPC_JSON_STATE_OBJECT_KEY_END:
333  case State::GRPC_JSON_STATE_VALUE_BEGIN:
334  case State::GRPC_JSON_STATE_VALUE_END:
335  case State::GRPC_JSON_STATE_END:
336  break;
337 
338  case State::GRPC_JSON_STATE_OBJECT_KEY_STRING:
339  case State::GRPC_JSON_STATE_VALUE_STRING:
340  if (c != ' ') return Status::GRPC_JSON_PARSE_ERROR;
341  if (unicode_high_surrogate_ != 0) {
342  return Status::GRPC_JSON_PARSE_ERROR;
343  }
344  if (!StringAddChar(c)) return Status::GRPC_JSON_PARSE_ERROR;
345  break;
346 
347  case State::GRPC_JSON_STATE_VALUE_NUMBER:
348  case State::GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL:
349  case State::GRPC_JSON_STATE_VALUE_NUMBER_ZERO:
350  case State::GRPC_JSON_STATE_VALUE_NUMBER_EPM:
351  if (!SetNumber()) return Status::GRPC_JSON_PARSE_ERROR;
352  state_ = State::GRPC_JSON_STATE_VALUE_END;
353  break;
354 
355  default:
356  return Status::GRPC_JSON_PARSE_ERROR;
357  }
358  break;
359 
360  /* Value, object or array terminations. */
361  case ',':
362  case '}':
363  case ']':
364  switch (state_) {
365  case State::GRPC_JSON_STATE_OBJECT_KEY_STRING:
366  case State::GRPC_JSON_STATE_VALUE_STRING:
367  if (unicode_high_surrogate_ != 0) {
368  return Status::GRPC_JSON_PARSE_ERROR;
369  }
370  if (!StringAddChar(c)) return Status::GRPC_JSON_PARSE_ERROR;
371  break;
372 
373  case State::GRPC_JSON_STATE_VALUE_NUMBER:
374  case State::GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL:
375  case State::GRPC_JSON_STATE_VALUE_NUMBER_ZERO:
376  case State::GRPC_JSON_STATE_VALUE_NUMBER_EPM:
377  if (stack_.empty()) {
378  return Status::GRPC_JSON_PARSE_ERROR;
379  } else if (c == '}' &&
380  stack_.back()->type() != Json::Type::OBJECT) {
381  return Status::GRPC_JSON_PARSE_ERROR;
382  } else if (c == ']' && stack_.back()->type() != Json::Type::ARRAY) {
383  return Status::GRPC_JSON_PARSE_ERROR;
384  }
385  if (!SetNumber()) return Status::GRPC_JSON_PARSE_ERROR;
386  state_ = State::GRPC_JSON_STATE_VALUE_END;
388 
389  case State::GRPC_JSON_STATE_VALUE_END:
390  case State::GRPC_JSON_STATE_OBJECT_KEY_BEGIN:
391  case State::GRPC_JSON_STATE_VALUE_BEGIN:
392  if (c == ',') {
393  if (state_ != State::GRPC_JSON_STATE_VALUE_END) {
394  return Status::GRPC_JSON_PARSE_ERROR;
395  }
396  if (!stack_.empty() &&
397  stack_.back()->type() == Json::Type::OBJECT) {
398  state_ = State::GRPC_JSON_STATE_OBJECT_KEY_BEGIN;
399  } else if (!stack_.empty() &&
400  stack_.back()->type() == Json::Type::ARRAY) {
401  state_ = State::GRPC_JSON_STATE_VALUE_BEGIN;
402  } else {
403  return Status::GRPC_JSON_PARSE_ERROR;
404  }
405  } else {
406  if (stack_.empty()) {
407  return Status::GRPC_JSON_PARSE_ERROR;
408  }
409  if (c == '}' && stack_.back()->type() != Json::Type::OBJECT) {
410  return Status::GRPC_JSON_PARSE_ERROR;
411  }
412  if (c == '}' &&
413  state_ == State::GRPC_JSON_STATE_OBJECT_KEY_BEGIN &&
415  return Status::GRPC_JSON_PARSE_ERROR;
416  }
417  if (c == ']' && stack_.back()->type() != Json::Type::ARRAY) {
418  return Status::GRPC_JSON_PARSE_ERROR;
419  }
420  if (c == ']' && state_ == State::GRPC_JSON_STATE_VALUE_BEGIN &&
422  return Status::GRPC_JSON_PARSE_ERROR;
423  }
424  state_ = State::GRPC_JSON_STATE_VALUE_END;
425  EndContainer();
426  if (stack_.empty()) {
427  state_ = State::GRPC_JSON_STATE_END;
428  }
429  }
430  break;
431 
432  default:
433  return Status::GRPC_JSON_PARSE_ERROR;
434  }
435  break;
436 
437  /* In-string escaping. */
438  case '\\':
439  switch (state_) {
440  case State::GRPC_JSON_STATE_OBJECT_KEY_STRING:
442  state_ = State::GRPC_JSON_STATE_STRING_ESCAPE;
443  break;
444 
445  case State::GRPC_JSON_STATE_VALUE_STRING:
446  escaped_string_was_key_ = false;
447  state_ = State::GRPC_JSON_STATE_STRING_ESCAPE;
448  break;
449 
450  /* This is the \\ case. */
451  case State::GRPC_JSON_STATE_STRING_ESCAPE:
452  if (unicode_high_surrogate_ != 0) {
453  return Status::GRPC_JSON_PARSE_ERROR;
454  }
455  if (!StringAddChar('\\')) return Status::GRPC_JSON_PARSE_ERROR;
457  state_ = State::GRPC_JSON_STATE_OBJECT_KEY_STRING;
458  } else {
459  state_ = State::GRPC_JSON_STATE_VALUE_STRING;
460  }
461  break;
462 
463  default:
464  return Status::GRPC_JSON_PARSE_ERROR;
465  }
466  break;
467 
468  default:
469  container_just_begun_ = false;
470  switch (state_) {
471  case State::GRPC_JSON_STATE_OBJECT_KEY_BEGIN:
472  if (c != '"') return Status::GRPC_JSON_PARSE_ERROR;
473  state_ = State::GRPC_JSON_STATE_OBJECT_KEY_STRING;
474  break;
475 
476  case State::GRPC_JSON_STATE_OBJECT_KEY_STRING:
477  if (unicode_high_surrogate_ != 0) {
478  return Status::GRPC_JSON_PARSE_ERROR;
479  }
480  if (c == '"') {
481  state_ = State::GRPC_JSON_STATE_OBJECT_KEY_END;
482  // Once the key is parsed, there should no un-matched utf8
483  // encoded bytes.
484  if (utf8_bytes_remaining_ != 0) {
485  return Status::GRPC_JSON_PARSE_ERROR;
486  }
487  SetKey();
488  } else {
489  if (c < 32) return Status::GRPC_JSON_PARSE_ERROR;
490  if (!StringAddChar(c)) return Status::GRPC_JSON_PARSE_ERROR;
491  }
492  break;
493 
494  case State::GRPC_JSON_STATE_VALUE_STRING:
495  if (unicode_high_surrogate_ != 0) {
496  return Status::GRPC_JSON_PARSE_ERROR;
497  }
498  if (c == '"') {
499  state_ = State::GRPC_JSON_STATE_VALUE_END;
500  // Once the value is parsed, there should no un-matched utf8
501  // encoded bytes.
502  if (utf8_bytes_remaining_ != 0) {
503  return Status::GRPC_JSON_PARSE_ERROR;
504  }
505  SetString();
506  } else {
507  if (c < 32) return Status::GRPC_JSON_PARSE_ERROR;
508  if (!StringAddChar(c)) return Status::GRPC_JSON_PARSE_ERROR;
509  }
510  break;
511 
512  case State::GRPC_JSON_STATE_OBJECT_KEY_END:
513  if (c != ':') return Status::GRPC_JSON_PARSE_ERROR;
514  state_ = State::GRPC_JSON_STATE_VALUE_BEGIN;
515  break;
516 
517  case State::GRPC_JSON_STATE_VALUE_BEGIN:
518  switch (c) {
519  case 't':
520  state_ = State::GRPC_JSON_STATE_VALUE_TRUE_R;
521  break;
522 
523  case 'f':
524  state_ = State::GRPC_JSON_STATE_VALUE_FALSE_A;
525  break;
526 
527  case 'n':
528  state_ = State::GRPC_JSON_STATE_VALUE_NULL_U;
529  break;
530 
531  case '"':
532  state_ = State::GRPC_JSON_STATE_VALUE_STRING;
533  break;
534 
535  case '0':
536  if (!StringAddChar(c)) return Status::GRPC_JSON_PARSE_ERROR;
537  state_ = State::GRPC_JSON_STATE_VALUE_NUMBER_ZERO;
538  break;
539 
540  case '1':
541  case '2':
542  case '3':
543  case '4':
544  case '5':
545  case '6':
546  case '7':
547  case '8':
548  case '9':
549  case '-':
550  if (!StringAddChar(c)) return Status::GRPC_JSON_PARSE_ERROR;
551  state_ = State::GRPC_JSON_STATE_VALUE_NUMBER;
552  break;
553 
554  case '{':
555  container_just_begun_ = true;
556  if (!StartContainer(Json::Type::OBJECT)) {
557  return Status::GRPC_JSON_PARSE_ERROR;
558  }
559  state_ = State::GRPC_JSON_STATE_OBJECT_KEY_BEGIN;
560  break;
561 
562  case '[':
563  container_just_begun_ = true;
564  if (!StartContainer(Json::Type::ARRAY)) {
565  return Status::GRPC_JSON_PARSE_ERROR;
566  }
567  break;
568  default:
569  return Status::GRPC_JSON_PARSE_ERROR;
570  }
571  break;
572 
573  case State::GRPC_JSON_STATE_STRING_ESCAPE:
575  state_ = State::GRPC_JSON_STATE_OBJECT_KEY_STRING;
576  } else {
577  state_ = State::GRPC_JSON_STATE_VALUE_STRING;
578  }
579  if (unicode_high_surrogate_ && c != 'u') {
580  return Status::GRPC_JSON_PARSE_ERROR;
581  }
582  switch (c) {
583  case '"':
584  case '/':
585  if (!StringAddChar(c)) return Status::GRPC_JSON_PARSE_ERROR;
586  break;
587  case 'b':
588  if (!StringAddChar('\b')) return Status::GRPC_JSON_PARSE_ERROR;
589  break;
590  case 'f':
591  if (!StringAddChar('\f')) return Status::GRPC_JSON_PARSE_ERROR;
592  break;
593  case 'n':
594  if (!StringAddChar('\n')) return Status::GRPC_JSON_PARSE_ERROR;
595  break;
596  case 'r':
597  if (!StringAddChar('\r')) return Status::GRPC_JSON_PARSE_ERROR;
598  break;
599  case 't':
600  if (!StringAddChar('\t')) return Status::GRPC_JSON_PARSE_ERROR;
601  break;
602  case 'u':
603  state_ = State::GRPC_JSON_STATE_STRING_ESCAPE_U1;
604  unicode_char_ = 0;
605  break;
606  default:
607  return Status::GRPC_JSON_PARSE_ERROR;
608  }
609  break;
610 
611  case State::GRPC_JSON_STATE_STRING_ESCAPE_U1:
612  case State::GRPC_JSON_STATE_STRING_ESCAPE_U2:
613  case State::GRPC_JSON_STATE_STRING_ESCAPE_U3:
614  case State::GRPC_JSON_STATE_STRING_ESCAPE_U4:
615  if ((c >= '0') && (c <= '9')) {
616  c -= '0';
617  } else if ((c >= 'A') && (c <= 'F')) {
618  c -= 'A' - 10;
619  } else if ((c >= 'a') && (c <= 'f')) {
620  c -= 'a' - 10;
621  } else {
622  return Status::GRPC_JSON_PARSE_ERROR;
623  }
624  unicode_char_ = static_cast<uint16_t>(unicode_char_ << 4);
625  unicode_char_ = static_cast<uint16_t>(unicode_char_ | c);
626 
627  switch (state_) {
628  case State::GRPC_JSON_STATE_STRING_ESCAPE_U1:
629  state_ = State::GRPC_JSON_STATE_STRING_ESCAPE_U2;
630  break;
631  case State::GRPC_JSON_STATE_STRING_ESCAPE_U2:
632  state_ = State::GRPC_JSON_STATE_STRING_ESCAPE_U3;
633  break;
634  case State::GRPC_JSON_STATE_STRING_ESCAPE_U3:
635  state_ = State::GRPC_JSON_STATE_STRING_ESCAPE_U4;
636  break;
637  case State::GRPC_JSON_STATE_STRING_ESCAPE_U4:
638  /* See grpc_json_writer_escape_string to have a description
639  * of what's going on here.
640  */
641  if ((unicode_char_ & 0xfc00) == 0xd800) {
642  /* high surrogate utf-16 */
643  if (unicode_high_surrogate_ != 0) {
644  return Status::GRPC_JSON_PARSE_ERROR;
645  }
647  } else if ((unicode_char_ & 0xfc00) == 0xdc00) {
648  /* low surrogate utf-16 */
649  uint32_t utf32;
650  if (unicode_high_surrogate_ == 0) {
651  return Status::GRPC_JSON_PARSE_ERROR;
652  }
653  utf32 = 0x10000;
654  utf32 += static_cast<uint32_t>(
655  (unicode_high_surrogate_ - 0xd800) * 0x400);
656  utf32 += static_cast<uint32_t>(unicode_char_ - 0xdc00);
657  if (!StringAddUtf32(utf32)) {
658  return Status::GRPC_JSON_PARSE_ERROR;
659  }
661  } else {
662  /* anything else */
663  if (unicode_high_surrogate_ != 0) {
664  return Status::GRPC_JSON_PARSE_ERROR;
665  }
666  if (!StringAddUtf32(unicode_char_)) {
667  return Status::GRPC_JSON_PARSE_ERROR;
668  }
669  }
671  state_ = State::GRPC_JSON_STATE_OBJECT_KEY_STRING;
672  } else {
673  state_ = State::GRPC_JSON_STATE_VALUE_STRING;
674  }
675  break;
676  default:
677  GPR_UNREACHABLE_CODE(return Status::GRPC_JSON_INTERNAL_ERROR);
678  }
679  break;
680 
681  case State::GRPC_JSON_STATE_VALUE_NUMBER:
682  if (!StringAddChar(c)) return Status::GRPC_JSON_PARSE_ERROR;
683  switch (c) {
684  case '0':
685  case '1':
686  case '2':
687  case '3':
688  case '4':
689  case '5':
690  case '6':
691  case '7':
692  case '8':
693  case '9':
694  break;
695  case 'e':
696  case 'E':
697  state_ = State::GRPC_JSON_STATE_VALUE_NUMBER_E;
698  break;
699  case '.':
700  state_ = State::GRPC_JSON_STATE_VALUE_NUMBER_DOT;
701  break;
702  default:
703  return Status::GRPC_JSON_PARSE_ERROR;
704  }
705  break;
706 
707  case State::GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL:
708  if (!StringAddChar(c)) return Status::GRPC_JSON_PARSE_ERROR;
709  switch (c) {
710  case '0':
711  case '1':
712  case '2':
713  case '3':
714  case '4':
715  case '5':
716  case '6':
717  case '7':
718  case '8':
719  case '9':
720  break;
721  case 'e':
722  case 'E':
723  state_ = State::GRPC_JSON_STATE_VALUE_NUMBER_E;
724  break;
725  default:
726  return Status::GRPC_JSON_PARSE_ERROR;
727  }
728  break;
729 
730  case State::GRPC_JSON_STATE_VALUE_NUMBER_ZERO:
731  if (c != '.') return Status::GRPC_JSON_PARSE_ERROR;
732  if (!StringAddChar(c)) return Status::GRPC_JSON_PARSE_ERROR;
733  state_ = State::GRPC_JSON_STATE_VALUE_NUMBER_DOT;
734  break;
735 
736  case State::GRPC_JSON_STATE_VALUE_NUMBER_DOT:
737  if (!StringAddChar(c)) return Status::GRPC_JSON_PARSE_ERROR;
738  switch (c) {
739  case '0':
740  case '1':
741  case '2':
742  case '3':
743  case '4':
744  case '5':
745  case '6':
746  case '7':
747  case '8':
748  case '9':
749  state_ = State::GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL;
750  break;
751  default:
752  return Status::GRPC_JSON_PARSE_ERROR;
753  }
754  break;
755 
756  case State::GRPC_JSON_STATE_VALUE_NUMBER_E:
757  if (!StringAddChar(c)) return Status::GRPC_JSON_PARSE_ERROR;
758  switch (c) {
759  case '0':
760  case '1':
761  case '2':
762  case '3':
763  case '4':
764  case '5':
765  case '6':
766  case '7':
767  case '8':
768  case '9':
769  case '+':
770  case '-':
771  state_ = State::GRPC_JSON_STATE_VALUE_NUMBER_EPM;
772  break;
773  default:
774  return Status::GRPC_JSON_PARSE_ERROR;
775  }
776  break;
777 
778  case State::GRPC_JSON_STATE_VALUE_NUMBER_EPM:
779  if (!StringAddChar(c)) return Status::GRPC_JSON_PARSE_ERROR;
780  switch (c) {
781  case '0':
782  case '1':
783  case '2':
784  case '3':
785  case '4':
786  case '5':
787  case '6':
788  case '7':
789  case '8':
790  case '9':
791  break;
792  default:
793  return Status::GRPC_JSON_PARSE_ERROR;
794  }
795  break;
796 
797  case State::GRPC_JSON_STATE_VALUE_TRUE_R:
798  if (c != 'r') return Status::GRPC_JSON_PARSE_ERROR;
799  state_ = State::GRPC_JSON_STATE_VALUE_TRUE_U;
800  break;
801 
802  case State::GRPC_JSON_STATE_VALUE_TRUE_U:
803  if (c != 'u') return Status::GRPC_JSON_PARSE_ERROR;
804  state_ = State::GRPC_JSON_STATE_VALUE_TRUE_E;
805  break;
806 
807  case State::GRPC_JSON_STATE_VALUE_TRUE_E:
808  if (c != 'e') return Status::GRPC_JSON_PARSE_ERROR;
809  SetTrue();
810  state_ = State::GRPC_JSON_STATE_VALUE_END;
811  break;
812 
813  case State::GRPC_JSON_STATE_VALUE_FALSE_A:
814  if (c != 'a') return Status::GRPC_JSON_PARSE_ERROR;
815  state_ = State::GRPC_JSON_STATE_VALUE_FALSE_L;
816  break;
817 
818  case State::GRPC_JSON_STATE_VALUE_FALSE_L:
819  if (c != 'l') return Status::GRPC_JSON_PARSE_ERROR;
820  state_ = State::GRPC_JSON_STATE_VALUE_FALSE_S;
821  break;
822 
823  case State::GRPC_JSON_STATE_VALUE_FALSE_S:
824  if (c != 's') return Status::GRPC_JSON_PARSE_ERROR;
825  state_ = State::GRPC_JSON_STATE_VALUE_FALSE_E;
826  break;
827 
828  case State::GRPC_JSON_STATE_VALUE_FALSE_E:
829  if (c != 'e') return Status::GRPC_JSON_PARSE_ERROR;
830  SetFalse();
831  state_ = State::GRPC_JSON_STATE_VALUE_END;
832  break;
833 
834  case State::GRPC_JSON_STATE_VALUE_NULL_U:
835  if (c != 'u') return Status::GRPC_JSON_PARSE_ERROR;
836  state_ = State::GRPC_JSON_STATE_VALUE_NULL_L1;
837  break;
838 
839  case State::GRPC_JSON_STATE_VALUE_NULL_L1:
840  if (c != 'l') return Status::GRPC_JSON_PARSE_ERROR;
841  state_ = State::GRPC_JSON_STATE_VALUE_NULL_L2;
842  break;
843 
844  case State::GRPC_JSON_STATE_VALUE_NULL_L2:
845  if (c != 'l') return Status::GRPC_JSON_PARSE_ERROR;
846  SetNull();
847  state_ = State::GRPC_JSON_STATE_VALUE_END;
848  break;
849 
850  /* All of the VALUE_END cases are handled in the specialized case
851  * above. */
852  case State::GRPC_JSON_STATE_VALUE_END:
853  switch (c) {
854  case ',':
855  case '}':
856  case ']':
857  GPR_UNREACHABLE_CODE(return Status::GRPC_JSON_INTERNAL_ERROR);
858  break;
859 
860  default:
861  return Status::GRPC_JSON_PARSE_ERROR;
862  }
863  break;
864 
865  case State::GRPC_JSON_STATE_END:
866  return Status::GRPC_JSON_PARSE_ERROR;
867  }
868  }
869  }
870 
871  GPR_UNREACHABLE_CODE(return Status::GRPC_JSON_INTERNAL_ERROR);
872 }
873 
875  JsonReader reader(input);
876  Status status = reader.Run();
877  if (reader.truncated_errors_) {
879  "too many errors encountered during JSON parsing -- fix reported "
880  "errors and try again to see additional errors"));
881  }
882  if (status == Status::GRPC_JSON_INTERNAL_ERROR) {
884  "internal error in JSON parser at index ", reader.CurrentIndex())));
885  } else if (status == Status::GRPC_JSON_PARSE_ERROR) {
886  reader.errors_.push_back(GRPC_ERROR_CREATE_FROM_CPP_STRING(
887  absl::StrCat("JSON parse error at index ", reader.CurrentIndex())));
888  }
889  if (!reader.errors_.empty()) {
890  return GRPC_ERROR_CREATE_FROM_VECTOR("JSON parsing failed",
891  &reader.errors_);
892  }
893  *output = std::move(reader.root_value_);
894  return GRPC_ERROR_NONE;
895 }
896 
897 } // namespace
898 
900  Json value;
901  *error = JsonReader::Parse(json_str, &value);
902  return value;
903 }
904 
905 } // namespace grpc_core
grpc_core::Json::Array
std::vector< Json > Array
Definition: src/core/lib/json/json.h:55
escaped_string_was_key_
bool escaped_string_was_key_
Definition: json_reader.cc:124
key_
std::string key_
Definition: json_reader.cc:135
cleanup.Json
Json
Definition: cleanup.py:49
original_input_
const uint8_t * original_input_
Definition: json_reader.cc:119
unicode_char_
uint16_t unicode_char_
Definition: json_reader.cc:126
GRPC_ERROR_NONE
#define GRPC_ERROR_NONE
Definition: error.h:234
log.h
const
#define const
Definition: bloaty/third_party/zlib/zconf.h:230
absl::StrCat
std::string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: abseil-cpp/absl/strings/str_cat.cc:98
absl::StrFormat
ABSL_MUST_USE_RESULT std::string StrFormat(const FormatSpec< Args... > &format, const Args &... args)
Definition: abseil-cpp/absl/strings/str_format.h:338
fix_build_deps.c
list c
Definition: fix_build_deps.py:490
grpc_core::Json::Type::OBJECT
@ OBJECT
uint16_t
unsigned short uint16_t
Definition: stdint-msvc2008.h:79
grpc_core
Definition: call_metric_recorder.h:31
truncated_errors_
bool truncated_errors_
Definition: json_reader.cc:129
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
error
grpc_error_handle error
Definition: retry_filter.cc:499
status
absl::Status status
Definition: rls.cc:251
utf8_bytes_remaining_
uint8_t utf8_bytes_remaining_
Definition: json_reader.cc:130
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
grpc_core::Json::Type
Type
Definition: src/core/lib/json/json.h:44
GRPC_ERROR_CREATE_FROM_VECTOR
#define GRPC_ERROR_CREATE_FROM_VECTOR(desc, error_list)
Definition: error.h:314
input_
const uint8_t * input_
Definition: json_reader.cc:120
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
Json
JSON (JavaScript Object Notation).
Definition: third_party/bloaty/third_party/protobuf/conformance/third_party/jsoncpp/json.h:227
b2
T::second_type b2
Definition: abseil-cpp/absl/container/internal/hash_function_defaults_test.cc:308
gmock_output_test.output
output
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
root_value_
Json root_value_
Definition: json_reader.cc:132
absl::flags_internal::Parse
bool Parse(FlagOpFn op, absl::string_view text, void *dst, std::string *error)
Definition: abseil-cpp/absl/flags/internal/flag.h:125
error.h
grpc_core::Json::Type::ARRAY
@ ARRAY
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
stack_
std::vector< Json * > stack_
Definition: json_reader.cc:133
json.h
string_
std::string string_
Definition: json_reader.cc:136
GPR_UNREACHABLE_CODE
#define GPR_UNREACHABLE_CODE(STATEMENT)
Definition: impl/codegen/port_platform.h:652
GRPC_ERROR_CREATE_FROM_STATIC_STRING
#define GRPC_ERROR_CREATE_FROM_STATIC_STRING(desc)
Definition: error.h:291
GRPC_JSON_MAX_ERRORS
#define GRPC_JSON_MAX_ERRORS
Definition: json_reader.cc:41
grpc_core::Json::Parse
static Json Parse(absl::string_view json_str, grpc_error_handle *error)
Definition: json_reader.cc:899
value
const char * value
Definition: hpack_parser_table.cc:165
b1
T::second_type b1
Definition: abseil-cpp/absl/container/internal/hash_function_defaults_test.cc:306
grpc_core::Json::Object
std::map< std::string, Json > Object
Definition: src/core/lib/json/json.h:54
errors_
std::vector< grpc_error_handle > errors_
Definition: json_reader.cc:128
GRPC_MUST_USE_RESULT
#define GRPC_MUST_USE_RESULT
Definition: impl/codegen/port_platform.h:584
remaining_input_
size_t remaining_input_
Definition: json_reader.cc:121
GRPC_ERROR_CREATE_FROM_CPP_STRING
#define GRPC_ERROR_CREATE_FROM_CPP_STRING(desc)
Definition: error.h:297
fix_build_deps.r
r
Definition: fix_build_deps.py:491
grpc::protobuf::util::Status
GRPC_CUSTOM_UTIL_STATUS Status
Definition: include/grpcpp/impl/codegen/config_protobuf.h:93
input
std::string input
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/tokenizer_unittest.cc:197
GRPC_JSON_MAX_DEPTH
#define GRPC_JSON_MAX_DEPTH
Definition: json_reader.cc:40
GRPC_JSON_READ_CHAR_EOF
static constexpr uint32_t GRPC_JSON_READ_CHAR_EOF
Definition: json_reader.cc:93
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
googletest-break-on-failure-unittest.Run
def Run(command)
Definition: bloaty/third_party/googletest/googletest/test/googletest-break-on-failure-unittest.py:76
grpc_error
Definition: error_internal.h:42
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
container_just_begun_
bool container_just_begun_
Definition: json_reader.cc:125
ABSL_FALLTHROUGH_INTENDED
#define ABSL_FALLTHROUGH_INTENDED
Definition: abseil-cpp/absl/base/attributes.h:641
unicode_high_surrogate_
uint16_t unicode_high_surrogate_
Definition: json_reader.cc:127
state_
State state_
Definition: json_reader.cc:123
reader
void reader(void *n)
Definition: libuv/docs/code/locks/main.c:8
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:25