OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // A JSON parser. Converts strings of JSON into a Value object (see | 5 // A JSON parser. Converts strings of JSON into a Value object (see |
6 // base/values.h). | 6 // base/values.h). |
7 // http://www.ietf.org/rfc/rfc4627.txt?number=4627 | 7 // http://www.ietf.org/rfc/rfc4627.txt?number=4627 |
8 // | 8 // |
9 // Known limitations/deviations from the RFC: | 9 // Known limitations/deviations from the RFC: |
10 // - Only knows how to parse ints within the range of a signed 32 bit int and | 10 // - Only knows how to parse ints within the range of a signed 32 bit int and |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 // Constructs a reader with the default options, JSON_PARSE_RFC. | 86 // Constructs a reader with the default options, JSON_PARSE_RFC. |
87 JSONReader(); | 87 JSONReader(); |
88 | 88 |
89 // Constructs a reader with custom options. | 89 // Constructs a reader with custom options. |
90 explicit JSONReader(int options); | 90 explicit JSONReader(int options); |
91 | 91 |
92 ~JSONReader(); | 92 ~JSONReader(); |
93 | 93 |
94 // Reads and parses |json|, returning a Value. The caller owns the returned | 94 // Reads and parses |json|, returning a Value. The caller owns the returned |
95 // instance. If |json| is not a properly formed JSON string, returns NULL. | 95 // instance. If |json| is not a properly formed JSON string, returns NULL. |
96 static std::unique_ptr<Value> Read(const StringPiece& json); | 96 static std::unique_ptr<Value> Read(StringPiece json); |
97 | 97 |
98 // Reads and parses |json|, returning a Value owned by the caller. The | 98 // Reads and parses |json|, returning a Value owned by the caller. The |
99 // parser respects the given |options|. If the input is not properly formed, | 99 // parser respects the given |options|. If the input is not properly formed, |
100 // returns NULL. | 100 // returns NULL. |
101 static std::unique_ptr<Value> Read(const StringPiece& json, int options); | 101 static std::unique_ptr<Value> Read(StringPiece json, int options); |
102 | 102 |
103 // Reads and parses |json| like Read(). |error_code_out| and |error_msg_out| | 103 // Reads and parses |json| like Read(). |error_code_out| and |error_msg_out| |
104 // are optional. If specified and NULL is returned, they will be populated | 104 // are optional. If specified and NULL is returned, they will be populated |
105 // an error code and a formatted error message (including error location if | 105 // an error code and a formatted error message (including error location if |
106 // appropriate). Otherwise, they will be unmodified. | 106 // appropriate). Otherwise, they will be unmodified. |
107 static std::unique_ptr<Value> ReadAndReturnError( | 107 static std::unique_ptr<Value> ReadAndReturnError( |
108 const StringPiece& json, | 108 const StringPiece& json, |
109 int options, // JSONParserOptions | 109 int options, // JSONParserOptions |
110 int* error_code_out, | 110 int* error_code_out, |
111 std::string* error_msg_out, | 111 std::string* error_msg_out, |
112 int* error_line_out = nullptr, | 112 int* error_line_out = nullptr, |
113 int* error_column_out = nullptr); | 113 int* error_column_out = nullptr); |
114 | 114 |
115 // Converts a JSON parse error code into a human readable message. | 115 // Converts a JSON parse error code into a human readable message. |
116 // Returns an empty string if error_code is JSON_NO_ERROR. | 116 // Returns an empty string if error_code is JSON_NO_ERROR. |
117 static std::string ErrorCodeToString(JsonParseError error_code); | 117 static std::string ErrorCodeToString(JsonParseError error_code); |
118 | 118 |
119 // Parses an input string into a Value that is owned by the caller. | 119 // Parses an input string into a Value that is owned by the caller. |
120 std::unique_ptr<Value> ReadToValue(const std::string& json); | 120 std::unique_ptr<Value> ReadToValue(StringPiece json); |
121 | 121 |
122 // Returns the error code if the last call to ReadToValue() failed. | 122 // Returns the error code if the last call to ReadToValue() failed. |
123 // Returns JSON_NO_ERROR otherwise. | 123 // Returns JSON_NO_ERROR otherwise. |
124 JsonParseError error_code() const; | 124 JsonParseError error_code() const; |
125 | 125 |
126 // Converts error_code_ to a human-readable string, including line and column | 126 // Converts error_code_ to a human-readable string, including line and column |
127 // numbers if appropriate. | 127 // numbers if appropriate. |
128 std::string GetErrorMessage() const; | 128 std::string GetErrorMessage() const; |
129 | 129 |
130 private: | 130 private: |
131 std::unique_ptr<internal::JSONParser> parser_; | 131 std::unique_ptr<internal::JSONParser> parser_; |
132 }; | 132 }; |
133 | 133 |
134 } // namespace base | 134 } // namespace base |
135 | 135 |
136 #endif // BASE_JSON_JSON_READER_H_ | 136 #endif // BASE_JSON_JSON_READER_H_ |
OLD | NEW |