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 // 5 // A JSON parser. Converts strings of JSON into a Value object (see 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 #include <string_view> 34 35 namespace base { 36 37 class Value; 38 39 namespace internal { 40 class JSONParser; 41 } 42 43 enum JSONParserOptions { 44 // Parses the input strictly according to RFC 4627, except for where noted 45 // above. 46 JSON_PARSE_RFC = 0, 47 48 // Allows commas to exist after the last element in structures. 49 JSON_ALLOW_TRAILING_COMMAS = 1 << 0, 50 51 // If set the parser replaces invalid characters with the Unicode replacement 52 // character (U+FFFD). If not set, invalid characters trigger a hard error and 53 // parsing fails. 54 JSON_REPLACE_INVALID_CHARACTERS = 1 << 1, 55 }; 56 57 class JSONReader { 58 public: 59 static const int kStackMaxDepth; 60 61 // Error codes during parsing. 62 enum JsonParseError { 63 JSON_NO_ERROR = 0, 64 JSON_INVALID_ESCAPE, 65 JSON_SYNTAX_ERROR, 66 JSON_UNEXPECTED_TOKEN, 67 JSON_TRAILING_COMMA, 68 JSON_TOO_MUCH_NESTING, 69 JSON_UNEXPECTED_DATA_AFTER_ROOT, 70 JSON_UNSUPPORTED_ENCODING, 71 JSON_UNQUOTED_DICTIONARY_KEY, 72 JSON_TOO_LARGE, 73 JSON_PARSE_ERROR_COUNT 74 }; 75 76 // String versions of parse error codes. 77 static const char kInvalidEscape[]; 78 static const char kSyntaxError[]; 79 static const char kUnexpectedToken[]; 80 static const char kTrailingComma[]; 81 static const char kTooMuchNesting[]; 82 static const char kUnexpectedDataAfterRoot[]; 83 static const char kUnsupportedEncoding[]; 84 static const char kUnquotedDictionaryKey[]; 85 static const char kInputTooLarge[]; 86 87 // Constructs a reader. 88 JSONReader(int options = JSON_PARSE_RFC, int max_depth = kStackMaxDepth); 89 90 ~JSONReader(); 91 92 // Reads and parses |json|, returning a Value. 93 // If |json| is not a properly formed JSON string, returns nullptr. 94 // Wrap this in base::FooValue::From() to check the Value is of type Foo and 95 // convert to a FooValue at the same time. 96 static std::unique_ptr<Value> Read(std::string_view json, 97 int options = JSON_PARSE_RFC, 98 int max_depth = kStackMaxDepth); 99 100 // Reads and parses |json| like Read(). |error_code_out| and |error_msg_out| 101 // are optional. If specified and nullptr is returned, they will be populated 102 // an error code and a formatted error message (including error location if 103 // appropriate). Otherwise, they will be unmodified. 104 static std::unique_ptr<Value> ReadAndReturnError( 105 std::string_view json, 106 int options, // JSONParserOptions 107 int* error_code_out, 108 std::string* error_msg_out, 109 int* error_line_out = nullptr, 110 int* error_column_out = nullptr); 111 112 // Converts a JSON parse error code into a human readable message. 113 // Returns an empty string if error_code is JSON_NO_ERROR. 114 static std::string ErrorCodeToString(JsonParseError error_code); 115 116 // Non-static version of Read() above. 117 std::unique_ptr<Value> ReadToValue(std::string_view json); 118 119 // Returns the error code if the last call to ReadToValue() failed. 120 // Returns JSON_NO_ERROR otherwise. 121 JsonParseError error_code() const; 122 123 // Converts error_code_ to a human-readable string, including line and column 124 // numbers if appropriate. 125 std::string GetErrorMessage() const; 126 127 private: 128 std::unique_ptr<internal::JSONParser> parser_; 129 }; 130 131 } // namespace base 132 133 #endif // BASE_JSON_JSON_READER_H_ 134