| 6 | // base/values.h). |
| 7 | // http://www.ietf.org/rfc/rfc4627.txt?number=4627 |
| 8 | // |
| 9 | // Known limitations/deviations from the RFC: |
| 10 | // - Only knows how to parse ints within the range of a signed 32 bit int and |
| 11 | // decimal numbers within a double. |
| 12 | // - Assumes input is encoded as UTF8. The spec says we should allow UTF-16 |
| 13 | // (BE or LE) and UTF-32 (BE or LE) as well. |
| 14 | // - We limit nesting to 100 levels to prevent stack overflow (this is allowed |
| 15 | // by the RFC). |
| 16 | // - A Unicode FAQ ("http://unicode.org/faq/utf_bom.html") writes a data |
| 17 | // stream may start with a Unicode Byte-Order-Mark (U+FEFF), i.e. the input |
| 18 | // UTF-8 string for the JSONReader::JsonToValue() function may start with a |
| 19 | // UTF-8 BOM (0xEF, 0xBB, 0xBF). |
| 20 | // To avoid the function from mis-treating a UTF-8 BOM as an invalid |
| 21 | // character, the function skips a Unicode BOM at the beginning of the |
| 22 | // Unicode string (converted from the input UTF-8 string) before parsing it. |
| 23 | // |
| 24 | // TODO(tc): Add a parsing option to to relax object keys being wrapped in |
| 25 | // double quotes |
| 26 | // TODO(tc): Add an option to disable comment stripping |
| 27 |
| 28 | #ifndef BASE_JSON_JSON_READER_H_ |
| 29 | #define BASE_JSON_JSON_READER_H_ |
| 30 |
| 31 | #include <memory> |
| 32 | #include <string> |
| 33 |
| 34 | #include "base/base_export.h" |
| 35 | #include "base/json/json_common.h" |
| 36 | #include "base/optional.h" |
| 37 | #include "base/strings/string_piece.h" |
| 38 | #include "base/values.h" |
| 39 |
| 40 | namespace base { |
| 41 |
| 42 | namespace internal { |
| 43 | class JSONParser; |
| 44 | } |
| 45 |
| 46 | enum JSONParserOptions { |
| 47 | // Parses the input strictly according to RFC 4627, except for where noted |
| 48 | // above. |
| 49 | JSON_PARSE_RFC = 0, |
| 50 |
| 51 | // Allows commas to exist after the last element in structures. |
| 52 | JSON_ALLOW_TRAILING_COMMAS = 1 << 0, |
| 53 |
| 54 | // If set the parser replaces invalid code points (i.e. lone |
| 55 | // surrogates) with the Unicode replacement character (U+FFFD). If |
| 56 | // not set, invalid code points trigger a hard error and parsing |
| 57 | // fails. |
| 58 | JSON_REPLACE_INVALID_CHARACTERS = 1 << 1, |
| 59 | }; |
| 60 |
| 61 | class BASE_EXPORT JSONReader { |
| 62 | public: |
| 63 | // Error codes during parsing. |
| 64 | enum JsonParseError { |
| 65 | JSON_NO_ERROR = 0, |
| 66 | JSON_INVALID_ESCAPE, |
| 67 | JSON_SYNTAX_ERROR, |
| 68 | JSON_UNEXPECTED_TOKEN, |
| 69 | JSON_TRAILING_COMMA, |
| 70 | JSON_TOO_MUCH_NESTING, |
| 71 | JSON_UNEXPECTED_DATA_AFTER_ROOT, |
| 72 | JSON_UNSUPPORTED_ENCODING, |
| 73 | JSON_UNQUOTED_DICTIONARY_KEY, |
| 74 | JSON_TOO_LARGE, |
| 75 | JSON_UNREPRESENTABLE_NUMBER, |
| 76 | JSON_PARSE_ERROR_COUNT |
| 77 | }; |
| 78 |
| 79 | struct BASE_EXPORT ValueWithError { |
| 80 | ValueWithError(); |
| 81 | ValueWithError(ValueWithError&& other); |
| 82 | ValueWithError& operator=(ValueWithError&& other); |
| 83 | ~ValueWithError(); |
| 84 |
| 85 | Optional<Value> value; |
| 86 |
| 87 | // Contains default values if |value| exists, or the error status if |value| |
| 88 | // is base::nullopt. |
| 89 | JsonParseError error_code = JSON_NO_ERROR; |
| 90 | std::string error_message; |
| 91 | int error_line = 0; |
| 92 | int error_column = 0; |
| 93 |
| 94 | DISALLOW_COPY_AND_ASSIGN(ValueWithError); |
| 95 | }; |
| 96 |
| 97 | // String versions of parse error codes. |
| 98 | static const char kInvalidEscape[]; |
| 99 | static const char kSyntaxError[]; |
| 100 | static const char kUnexpectedToken[]; |
| 101 | static const char kTrailingComma[]; |
| 102 | static const char kTooMuchNesting[]; |
| 103 | static const char kUnexpectedDataAfterRoot[]; |
| 104 | static const char kUnsupportedEncoding[]; |
| 105 | static const char kUnquotedDictionaryKey[]; |
| 106 | static const char kInputTooLarge[]; |
| 107 | static const char kUnrepresentableNumber[]; |
| 108 |
| 109 | // Constructs a reader. |
| 110 | JSONReader(int options = JSON_PARSE_RFC, |
| 111 | size_t max_depth = internal::kAbsoluteMaxDepth); |
| 112 |
| 113 | ~JSONReader(); |
| 114 |
| 115 | // Reads and parses |json|, returning a Value. |
| 116 | // If |json| is not a properly formed JSON string, returns base::nullopt. |
| 117 | static Optional<Value> Read(StringPiece json, |
| 118 | int options = JSON_PARSE_RFC, |
| 119 | size_t max_depth = internal::kAbsoluteMaxDepth); |
| 120 |
| 121 | // Deprecated. Use the Read() method above. |
| 122 | // Reads and parses |json|, returning a Value. |
| 123 | // If |json| is not a properly formed JSON string, returns nullptr. |
| 124 | // Wrap this in base::FooValue::From() to check the Value is of type Foo and |
| 125 | // convert to a FooValue at the same time. |
| 126 | static std::unique_ptr<Value> ReadDeprecated( |
| 127 | StringPiece json, |
| 128 | int options = JSON_PARSE_RFC, |
| 129 | size_t max_depth = internal::kAbsoluteMaxDepth); |
| 130 |
| 131 | // Reads and parses |json| like Read(). Returns a ValueWithError, which on |
| 132 | // error, will be populated with a formatted error message, an error code, and |
| 133 | // the error location if appropriate. |
| 134 | static ValueWithError ReadAndReturnValueWithError( |
| 135 | StringPiece json, |
| 136 | int options = JSON_PARSE_RFC); |
| 137 |
| 138 | // Deprecated. Use the ReadAndReturnValueWithError() method above. |
| 139 | // Reads and parses |json| like Read(). |error_code_out| and |error_msg_out| |
| 140 | // are optional. If specified and nullptr is returned, they will be populated |
| 141 | // an error code and a formatted error message (including error location if |
| 142 | // appropriate). Otherwise, they will be unmodified. |
| 143 | static std::unique_ptr<Value> ReadAndReturnErrorDeprecated( |
| 144 | StringPiece json, |
| 145 | int options, // JSONParserOptions |
| 146 | int* error_code_out, |
| 147 | std::string* error_msg_out, |
| 148 | int* error_line_out = nullptr, |
| 149 | int* error_column_out = nullptr); |
| 150 |
| 151 | // Converts a JSON parse error code into a human readable message. |
| 152 | // Returns an empty string if error_code is JSON_NO_ERROR. |
| 153 | static std::string ErrorCodeToString(JsonParseError error_code); |
| 154 |
| 155 | // Non-static version of Read() above. |
| 156 | Optional<Value> ReadToValue(StringPiece json); |
| 157 |
| 158 | // Deprecated. Use the ReadToValue() method above. |
| 159 | // Non-static version of Read() above. |
| 160 | std::unique_ptr<Value> ReadToValueDeprecated(StringPiece json); |
| 161 |
| 162 | // Returns the error code if the last call to ReadToValue() failed. |
| 163 | // Returns JSON_NO_ERROR otherwise. |
| 164 | JsonParseError error_code() const; |
| 165 |
| 166 | // Converts error_code_ to a human-readable string, including line and column |
| 167 | // numbers if appropriate. |
| 168 | std::string GetErrorMessage() const; |
| 169 |
| 170 | private: |
| 171 | std::unique_ptr<internal::JSONParser> parser_; |
| 172 | }; |
| 173 |
| 174 | } // namespace base |
| 175 |
| 176 | #endif // BASE_JSON_JSON_READER_H_ |
| 177 |