json_reader.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. // A JSON parser, converting from a base::StringPiece to a base::Value.
  5. //
  6. // The JSON spec is:
  7. // https://tools.ietf.org/rfc/rfc8259.txt
  8. // which obsoletes the earlier RFCs 4627, 7158 and 7159.
  9. //
  10. // This RFC should be equivalent to the informal spec:
  11. // https://www.json.org/json-en.html
  12. //
  13. // Implementation choices permitted by the RFC:
  14. // - Nesting is limited (to a configurable depth, 200 by default).
  15. // - Numbers are limited to those representable by a finite double. The
  16. // conversion from a JSON number (in the base::StringPiece input) to a
  17. // double-flavored base::Value may also be lossy.
  18. // - The input (which must be UTF-8) may begin with a BOM (Byte Order Mark).
  19. // - Duplicate object keys (strings) are silently allowed. Last key-value pair
  20. // wins. Previous pairs are discarded.
  21. //
  22. // Configurable (see the JSONParserOptions type) deviations from the RFC:
  23. // - Allow trailing commas: "[1,2,]".
  24. // - Replace invalid Unicode with U+FFFD REPLACEMENT CHARACTER.
  25. //
  26. // Non-configurable deviations from the RFC:
  27. // - Allow "// etc\n" and "/* etc */" C-style comments.
  28. // - Allow ASCII control characters, including literal (not escaped) NUL bytes
  29. // and new lines, within a JSON string.
  30. // - Allow "\\v" escapes within a JSON string, producing a vertical tab.
  31. // - Allow "\\x23" escapes within a JSON string. Subtly, the 2-digit hex value
  32. // is a Unicode code point, not a UTF-8 byte. For example, "\\xFF" in the
  33. // JSON source decodes to a base::Value whose string contains "\xC3\xBF", the
  34. // UTF-8 encoding of U+00FF LATIN SMALL LETTER Y WITH DIAERESIS. Converting
  35. // from UTF-8 to UTF-16, e.g. via UTF8ToWide, will recover a 16-bit 0x00FF.
  36. #ifndef BASE_JSON_JSON_READER_H_
  37. #define BASE_JSON_JSON_READER_H_
  38. #include <memory>
  39. #include <string>
  40. #include "base/base_export.h"
  41. #include "base/json/json_common.h"
  42. #include "base/optional.h"
  43. #include "base/strings/string_piece.h"
  44. #include "base/values.h"
  45. namespace base {
  46. namespace internal {
  47. class JSONParser;
  48. }
  49. enum JSONParserOptions {
  50. // Parses the input strictly according to RFC 8259, except for where noted
  51. // above.
  52. JSON_PARSE_RFC = 0,
  53. // Allows commas to exist after the last element in structures.
  54. JSON_ALLOW_TRAILING_COMMAS = 1 << 0,
  55. // If set the parser replaces invalid code points (i.e. lone
  56. // surrogates) with the Unicode replacement character (U+FFFD). If
  57. // not set, invalid code points trigger a hard error and parsing
  58. // fails.
  59. JSON_REPLACE_INVALID_CHARACTERS = 1 << 1,
  60. };
  61. class BASE_EXPORT JSONReader {
  62. public:
  63. // Error codes during parsing.
  64. //
  65. // TODO(crbug.com/1069271, crbug.com/1070409): move this enum from JSONReader
  66. // (a higher level API, which can be backed by multiple implementations) to
  67. // JSONParser (a lower level API, a single implementation). Such a move would
  68. // also remove the ValueWithError.error_code field and move the
  69. // kInvalidEscape, kSyntaxError, etc. strings defined in this .h file.
  70. enum JsonParseError {
  71. JSON_NO_ERROR = 0,
  72. JSON_INVALID_ESCAPE,
  73. JSON_SYNTAX_ERROR,
  74. JSON_UNEXPECTED_TOKEN,
  75. JSON_TRAILING_COMMA,
  76. JSON_TOO_MUCH_NESTING,
  77. JSON_UNEXPECTED_DATA_AFTER_ROOT,
  78. JSON_UNSUPPORTED_ENCODING,
  79. JSON_UNQUOTED_DICTIONARY_KEY,
  80. JSON_TOO_LARGE,
  81. JSON_UNREPRESENTABLE_NUMBER,
  82. JSON_PARSE_ERROR_COUNT
  83. };
  84. struct BASE_EXPORT ValueWithError {
  85. ValueWithError();
  86. ValueWithError(ValueWithError&& other);
  87. ValueWithError& operator=(ValueWithError&& other);
  88. ~ValueWithError();
  89. Optional<Value> value;
  90. // Contains default values if |value| exists, or the error status if |value|
  91. // is base::nullopt.
  92. JsonParseError error_code = JSON_NO_ERROR;
  93. std::string error_message;
  94. int error_line = 0;
  95. int error_column = 0;
  96. DISALLOW_COPY_AND_ASSIGN(ValueWithError);
  97. };
  98. // String versions of parse error codes.
  99. static const char kInvalidEscape[];
  100. static const char kSyntaxError[];
  101. static const char kUnexpectedToken[];
  102. static const char kTrailingComma[];
  103. static const char kTooMuchNesting[];
  104. static const char kUnexpectedDataAfterRoot[];
  105. static const char kUnsupportedEncoding[];
  106. static const char kUnquotedDictionaryKey[];
  107. static const char kInputTooLarge[];
  108. static const char kUnrepresentableNumber[];
  109. // Constructs a reader.
  110. JSONReader(int options = JSON_PARSE_RFC,
  111. size_t max_depth = internal::kAbsoluteMaxDepth);
  112. ~JSONReader();
  113. // Reads and parses |json|, returning a Value.
  114. // If |json| is not a properly formed JSON string, returns base::nullopt.
  115. static Optional<Value> Read(StringPiece json,
  116. int options = JSON_PARSE_RFC,
  117. size_t max_depth = internal::kAbsoluteMaxDepth);
  118. // Deprecated. Use the Read() method above.
  119. // Reads and parses |json|, returning a Value.
  120. // If |json| is not a properly formed JSON string, returns nullptr.
  121. // Wrap this in base::FooValue::From() to check the Value is of type Foo and
  122. // convert to a FooValue at the same time.
  123. static std::unique_ptr<Value> ReadDeprecated(
  124. StringPiece json,
  125. int options = JSON_PARSE_RFC,
  126. size_t max_depth = internal::kAbsoluteMaxDepth);
  127. // Reads and parses |json| like Read(). Returns a ValueWithError, which on
  128. // error, will be populated with a formatted error message, an error code, and
  129. // the error location if appropriate.
  130. static ValueWithError ReadAndReturnValueWithError(
  131. StringPiece json,
  132. int options = JSON_PARSE_RFC);
  133. // Deprecated. Use the ReadAndReturnValueWithError() method above.
  134. // Reads and parses |json| like Read(). |error_code_out| and |error_msg_out|
  135. // are optional. If specified and nullptr is returned, they will be populated
  136. // an error code and a formatted error message (including error location if
  137. // appropriate). Otherwise, they will be unmodified.
  138. static std::unique_ptr<Value> ReadAndReturnErrorDeprecated(
  139. StringPiece json,
  140. int options, // JSONParserOptions
  141. int* error_code_out,
  142. std::string* error_msg_out,
  143. int* error_line_out = nullptr,
  144. int* error_column_out = nullptr);
  145. // Converts a JSON parse error code into a human readable message.
  146. // Returns an empty string if error_code is JSON_NO_ERROR.
  147. static std::string ErrorCodeToString(JsonParseError error_code);
  148. // Non-static version of Read() above.
  149. Optional<Value> ReadToValue(StringPiece json);
  150. // Deprecated. Use the ReadToValue() method above.
  151. // Non-static version of Read() above.
  152. std::unique_ptr<Value> ReadToValueDeprecated(StringPiece json);
  153. // Converts error_code_ to a human-readable string, including line and column
  154. // numbers if appropriate.
  155. std::string GetErrorMessage() const;
  156. private:
  157. std::unique_ptr<internal::JSONParser> parser_;
  158. };
  159. } // namespace base
  160. #endif // BASE_JSON_JSON_READER_H_