| 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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 NUMBER, | 64 NUMBER, |
| 65 BOOL_TRUE, // true | 65 BOOL_TRUE, // true |
| 66 BOOL_FALSE, // false | 66 BOOL_FALSE, // false |
| 67 NULL_TOKEN, // null | 67 NULL_TOKEN, // null |
| 68 LIST_SEPARATOR, // , | 68 LIST_SEPARATOR, // , |
| 69 OBJECT_PAIR_SEPARATOR, // : | 69 OBJECT_PAIR_SEPARATOR, // : |
| 70 END_OF_INPUT, | 70 END_OF_INPUT, |
| 71 INVALID_TOKEN, | 71 INVALID_TOKEN, |
| 72 }; | 72 }; |
| 73 | 73 |
| 74 Token(Type t, const wchar_t* b, int len) | 74 Token(Type t, const char* b, int len) |
| 75 : type(t), begin(b), length(len) {} | 75 : type(t), begin(b), length(len) {} |
| 76 | 76 |
| 77 // Get the character that's one past the end of this token. | 77 // Get the character that's one past the end of this token. |
| 78 wchar_t NextChar() { | 78 char NextChar() { |
| 79 return *(begin + length); | 79 return *(begin + length); |
| 80 } | 80 } |
| 81 | 81 |
| 82 static Token CreateInvalidToken() { | 82 static Token CreateInvalidToken() { |
| 83 return Token(INVALID_TOKEN, 0, 0); | 83 return Token(INVALID_TOKEN, 0, 0); |
| 84 } | 84 } |
| 85 | 85 |
| 86 Type type; | 86 Type type; |
| 87 | 87 |
| 88 // A pointer into JSONReader::json_pos_ that's the beginning of this token. | 88 // A pointer into JSONReader::json_pos_ that's the beginning of this token. |
| 89 const wchar_t* begin; | 89 const char* begin; |
| 90 | 90 |
| 91 // End should be one char past the end of the token. | 91 // End should be one char past the end of the token. |
| 92 int length; | 92 int length; |
| 93 }; | 93 }; |
| 94 | 94 |
| 95 // Error codes during parsing. | 95 // Error codes during parsing. |
| 96 enum JsonParseError { | 96 enum JsonParseError { |
| 97 JSON_NO_ERROR = 0, | 97 JSON_NO_ERROR = 0, |
| 98 JSON_BAD_ROOT_ELEMENT_TYPE, | 98 JSON_BAD_ROOT_ELEMENT_TYPE, |
| 99 JSON_INVALID_ESCAPE, | 99 JSON_INVALID_ESCAPE, |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 Token ParseToken(); | 191 Token ParseToken(); |
| 192 | 192 |
| 193 // Increments |json_pos_| past leading whitespace and comments. | 193 // Increments |json_pos_| past leading whitespace and comments. |
| 194 void EatWhitespaceAndComments(); | 194 void EatWhitespaceAndComments(); |
| 195 | 195 |
| 196 // If |json_pos_| is at the start of a comment, eat it, otherwise, returns | 196 // If |json_pos_| is at the start of a comment, eat it, otherwise, returns |
| 197 // false. | 197 // false. |
| 198 bool EatComment(); | 198 bool EatComment(); |
| 199 | 199 |
| 200 // Checks if |json_pos_| matches str. | 200 // Checks if |json_pos_| matches str. |
| 201 bool NextStringMatch(const wchar_t* str, size_t length); | 201 bool NextStringMatch(const char* str, size_t length); |
| 202 | 202 |
| 203 // Sets the error code that will be returned to the caller. The current | 203 // Sets the error code that will be returned to the caller. The current |
| 204 // line and column are determined and added into the final message. | 204 // line and column are determined and added into the final message. |
| 205 void SetErrorCode(const JsonParseError error, const wchar_t* error_pos); | 205 void SetErrorCode(const JsonParseError error, const char* error_pos); |
| 206 | 206 |
| 207 // Pointer to the starting position in the input string. | 207 // Pointer to the starting position in the input string. |
| 208 const wchar_t* start_pos_; | 208 const char* start_pos_; |
| 209 | 209 |
| 210 // Pointer to the current position in the input string. | 210 // Pointer to the current position in the input string. |
| 211 const wchar_t* json_pos_; | 211 const char* json_pos_; |
| 212 |
| 213 // Pointer to the last position in the input string. |
| 214 const char* end_pos_; |
| 212 | 215 |
| 213 // Used to keep track of how many nested lists/dicts there are. | 216 // Used to keep track of how many nested lists/dicts there are. |
| 214 int stack_depth_; | 217 int stack_depth_; |
| 215 | 218 |
| 216 // A parser flag that allows trailing commas in objects and arrays. | 219 // A parser flag that allows trailing commas in objects and arrays. |
| 217 bool allow_trailing_comma_; | 220 bool allow_trailing_comma_; |
| 218 | 221 |
| 219 // Contains the error code for the last call to JsonToValue(), if any. | 222 // Contains the error code for the last call to JsonToValue(), if any. |
| 220 JsonParseError error_code_; | 223 JsonParseError error_code_; |
| 221 int error_line_; | 224 int error_line_; |
| 222 int error_col_; | 225 int error_col_; |
| 223 | 226 |
| 224 DISALLOW_COPY_AND_ASSIGN(JSONReader); | 227 DISALLOW_COPY_AND_ASSIGN(JSONReader); |
| 225 }; | 228 }; |
| 226 | 229 |
| 227 } // namespace base | 230 } // namespace base |
| 228 | 231 |
| 229 #endif // BASE_JSON_JSON_READER_H_ | 232 #endif // BASE_JSON_JSON_READER_H_ |
| OLD | NEW |