OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 // instance. If |json| is not a properly formed JSON string, returns NULL and | 139 // instance. If |json| is not a properly formed JSON string, returns NULL and |
140 // a detailed error can be retrieved from |error_message()|. | 140 // a detailed error can be retrieved from |error_message()|. |
141 // If |check_root| is true, we require that the root object be an object or | 141 // If |check_root| is true, we require that the root object be an object or |
142 // array. Otherwise, it can be any valid JSON type. | 142 // array. Otherwise, it can be any valid JSON type. |
143 // If |allow_trailing_comma| is true, we will ignore trailing commas in | 143 // If |allow_trailing_comma| is true, we will ignore trailing commas in |
144 // objects and arrays even though this goes against the RFC. | 144 // objects and arrays even though this goes against the RFC. |
145 Value* JsonToValue(const std::string& json, bool check_root, | 145 Value* JsonToValue(const std::string& json, bool check_root, |
146 bool allow_trailing_comma); | 146 bool allow_trailing_comma); |
147 | 147 |
148 private: | 148 private: |
| 149 FRIEND_TEST(JSONReaderTest, Reading); |
| 150 FRIEND_TEST(JSONReaderTest, ErrorMessages); |
| 151 |
149 static std::string FormatErrorMessage(int line, int column, | 152 static std::string FormatErrorMessage(int line, int column, |
150 const std::string& description); | 153 const std::string& description); |
151 | 154 |
152 DISALLOW_COPY_AND_ASSIGN(JSONReader); | |
153 | |
154 FRIEND_TEST(JSONReaderTest, Reading); | |
155 FRIEND_TEST(JSONReaderTest, ErrorMessages); | |
156 | |
157 // Recursively build Value. Returns NULL if we don't have a valid JSON | 155 // Recursively build Value. Returns NULL if we don't have a valid JSON |
158 // string. If |is_root| is true, we verify that the root element is either | 156 // string. If |is_root| is true, we verify that the root element is either |
159 // an object or an array. | 157 // an object or an array. |
160 Value* BuildValue(bool is_root); | 158 Value* BuildValue(bool is_root); |
161 | 159 |
162 // Parses a sequence of characters into a Token::NUMBER. If the sequence of | 160 // Parses a sequence of characters into a Token::NUMBER. If the sequence of |
163 // characters is not a valid number, returns a Token::INVALID_TOKEN. Note | 161 // characters is not a valid number, returns a Token::INVALID_TOKEN. Note |
164 // that DecodeNumber is used to actually convert from a string to an | 162 // that DecodeNumber is used to actually convert from a string to an |
165 // int/double. | 163 // int/double. |
166 Token ParseNumberToken(); | 164 Token ParseNumberToken(); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 // Used to keep track of how many nested lists/dicts there are. | 204 // Used to keep track of how many nested lists/dicts there are. |
207 int stack_depth_; | 205 int stack_depth_; |
208 | 206 |
209 // A parser flag that allows trailing commas in objects and arrays. | 207 // A parser flag that allows trailing commas in objects and arrays. |
210 bool allow_trailing_comma_; | 208 bool allow_trailing_comma_; |
211 | 209 |
212 // Contains the error code for the last call to JsonToValue(), if any. | 210 // Contains the error code for the last call to JsonToValue(), if any. |
213 JsonParseError error_code_; | 211 JsonParseError error_code_; |
214 int error_line_; | 212 int error_line_; |
215 int error_col_; | 213 int error_col_; |
| 214 |
| 215 DISALLOW_COPY_AND_ASSIGN(JSONReader); |
216 }; | 216 }; |
217 | 217 |
218 } // namespace base | 218 } // namespace base |
219 | 219 |
220 #endif // BASE_JSON_JSON_READER_H_ | 220 #endif // BASE_JSON_JSON_READER_H_ |
OLD | NEW |