OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 static const char* kInvalidEscape; | 80 static const char* kInvalidEscape; |
81 static const char* kSyntaxError; | 81 static const char* kSyntaxError; |
82 static const char* kTrailingComma; | 82 static const char* kTrailingComma; |
83 static const char* kTooMuchNesting; | 83 static const char* kTooMuchNesting; |
84 static const char* kUnexpectedDataAfterRoot; | 84 static const char* kUnexpectedDataAfterRoot; |
85 static const char* kUnsupportedEncoding; | 85 static const char* kUnsupportedEncoding; |
86 static const char* kUnquotedDictionaryKey; | 86 static const char* kUnquotedDictionaryKey; |
87 | 87 |
88 // Reads and parses |json|, returning a Value. The caller owns the returned | 88 // Reads and parses |json|, returning a Value. The caller owns the returned |
89 // instance. If |json| is not a properly formed JSON string, returns NULL. | 89 // instance. If |json| is not a properly formed JSON string, returns NULL. |
90 // If allow_trailing_comma is true, we will ignore trailing commas in objects | 90 // If |allow_trailing_comma| is true, we will ignore trailing commas in |
91 // and arrays even though this goes against the RFC. | 91 // objects and arrays even though this goes against the RFC. |
92 static Value* Read(const std::string& json, | 92 static Value* Read(const std::string& json, bool allow_trailing_comma); |
93 bool allow_trailing_comma); | |
94 | 93 |
95 // Reads and parses |json| like Read(). |error_message_out| is optional. If | 94 // Reads and parses |json| like Read(). |error_message_out| is optional. If |
96 // specified and NULL is returned, error_message_out will be populated with | 95 // specified and NULL is returned, |error_message_out| will be populated with |
97 // a string describing the error. Otherwise, error_message_out is unmodified. | 96 // a string describing the error. Otherwise, |error_message_out| is |
| 97 // unmodified. |
98 static Value* ReadAndReturnError(const std::string& json, | 98 static Value* ReadAndReturnError(const std::string& json, |
99 bool allow_trailing_comma, | 99 bool allow_trailing_comma, |
100 std::string *error_message_out); | 100 std::string* error_message_out); |
101 | 101 |
102 private: | 102 private: |
103 static std::string FormatErrorMessage(int line, int column, | 103 static std::string FormatErrorMessage(int line, int column, |
104 const char* description); | 104 const char* description); |
105 | 105 |
106 JSONReader(); | 106 JSONReader(); |
107 DISALLOW_EVIL_CONSTRUCTORS(JSONReader); | 107 DISALLOW_EVIL_CONSTRUCTORS(JSONReader); |
108 | 108 |
109 FRIEND_TEST(JSONReaderTest, Reading); | 109 FRIEND_TEST(JSONReaderTest, Reading); |
110 FRIEND_TEST(JSONReaderTest, ErrorMessages); | 110 FRIEND_TEST(JSONReaderTest, ErrorMessages); |
(...skipping 22 matching lines...) Expand all Loading... |
133 // we can (ie., no overflow), return the value, else return NULL. | 133 // we can (ie., no overflow), return the value, else return NULL. |
134 Value* DecodeNumber(const Token& token); | 134 Value* DecodeNumber(const Token& token); |
135 | 135 |
136 // Parses a sequence of characters into a Token::STRING. If the sequence of | 136 // Parses a sequence of characters into a Token::STRING. If the sequence of |
137 // characters is not a valid string, returns a Token::INVALID_TOKEN. Note | 137 // characters is not a valid string, returns a Token::INVALID_TOKEN. Note |
138 // that DecodeString is used to actually decode the escaped string into an | 138 // that DecodeString is used to actually decode the escaped string into an |
139 // actual wstring. | 139 // actual wstring. |
140 Token ParseStringToken(); | 140 Token ParseStringToken(); |
141 | 141 |
142 // Convert the substring into a value string. This should always succeed | 142 // Convert the substring into a value string. This should always succeed |
143 // (otherwise ParseStringToken would have failed), but returns a success bool | 143 // (otherwise ParseStringToken would have failed). |
144 // just in case. | |
145 Value* DecodeString(const Token& token); | 144 Value* DecodeString(const Token& token); |
146 | 145 |
147 // Grabs the next token in the JSON stream. This does not increment the | 146 // Grabs the next token in the JSON stream. This does not increment the |
148 // stream so it can be used to look ahead at the next token. | 147 // stream so it can be used to look ahead at the next token. |
149 Token ParseToken(); | 148 Token ParseToken(); |
150 | 149 |
151 // Increments json_pos_ past leading whitespace and comments. | 150 // Increments |json_pos_| past leading whitespace and comments. |
152 void EatWhitespaceAndComments(); | 151 void EatWhitespaceAndComments(); |
153 | 152 |
154 // If json_pos_ is at the start of a comment, eat it, otherwise, returns | 153 // If |json_pos_| is at the start of a comment, eat it, otherwise, returns |
155 // false. | 154 // false. |
156 bool EatComment(); | 155 bool EatComment(); |
157 | 156 |
158 // Checks if json_pos_ matches str. | 157 // Checks if |json_pos_| matches str. |
159 bool NextStringMatch(const std::wstring& str); | 158 bool NextStringMatch(const std::wstring& str); |
160 | 159 |
161 // Creates the error message that will be returned to the caller. The current | 160 // Creates the error message that will be returned to the caller. The current |
162 // line and column are determined and added into the final message. | 161 // line and column are determined and added into the final message. |
163 void SetErrorMessage(const char* description, const wchar_t* error_pos); | 162 void SetErrorMessage(const char* description, const wchar_t* error_pos); |
164 | 163 |
165 // Pointer to the starting position in the input string. | 164 // Pointer to the starting position in the input string. |
166 const wchar_t* start_pos_; | 165 const wchar_t* start_pos_; |
167 | 166 |
168 // Pointer to the current position in the input string. | 167 // Pointer to the current position in the input string. |
169 const wchar_t* json_pos_; | 168 const wchar_t* json_pos_; |
170 | 169 |
171 // Used to keep track of how many nested lists/dicts there are. | 170 // Used to keep track of how many nested lists/dicts there are. |
172 int stack_depth_; | 171 int stack_depth_; |
173 | 172 |
174 // A parser flag that allows trailing commas in objects and arrays. | 173 // A parser flag that allows trailing commas in objects and arrays. |
175 bool allow_trailing_comma_; | 174 bool allow_trailing_comma_; |
176 | 175 |
177 // Contains the error message for the last call to JsonToValue(), if any. | 176 // Contains the error message for the last call to JsonToValue(), if any. |
178 std::string error_message_; | 177 std::string error_message_; |
179 }; | 178 }; |
180 | 179 |
181 #endif // BASE_JSON_READER_H_ | 180 #endif // BASE_JSON_READER_H_ |
OLD | NEW |