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 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 // Error messages that can be returned. | 78 // Error messages that can be returned. |
79 static const char* kBadRootElementType; | 79 static const char* kBadRootElementType; |
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| and populates |root|. If |json| is not a properly | 88 // Reads and parses |json|, returning a Value. The caller owns the returned |
89 // formed JSON string, returns false, leaves root unaltered and sets | 89 // instance. If |json| is not a properly formed JSON string, returns NULL. |
90 // error_message if it was non-null. If allow_trailing_comma is true, we will | 90 // If allow_trailing_comma is true, we will ignore trailing commas in objects |
91 // ignore trailing commas in objects and arrays even though this goes against | 91 // and arrays even though this goes against the RFC. |
92 // the RFC. | 92 static Value* Read(const std::string& json, |
93 static bool Read(const std::string& json, | 93 bool allow_trailing_comma); |
94 Value** root, | |
95 bool allow_trailing_comma); | |
96 | 94 |
97 // Reads and parses |json| like Read(). |error_message_out| is optional. If | 95 // Reads and parses |json| like Read(). |error_message_out| is optional. If |
98 // specified and false is returned, error_message_out will be populated with | 96 // specified and NULL is returned, error_message_out will be populated with |
99 // a string describing the error. Otherwise, error_message_out is unmodified. | 97 // a string describing the error. Otherwise, error_message_out is unmodified. |
100 static bool ReadAndReturnError(const std::string& json, | 98 static Value* ReadAndReturnError(const std::string& json, |
101 Value** root, | 99 bool allow_trailing_comma, |
102 bool allow_trailing_comma, | 100 std::string *error_message_out); |
103 std::string *error_message_out); | |
104 | 101 |
105 private: | 102 private: |
106 static std::string FormatErrorMessage(int line, int column, | 103 static std::string FormatErrorMessage(int line, int column, |
107 const char* description); | 104 const char* description); |
108 | 105 |
109 JSONReader(); | 106 JSONReader(); |
110 DISALLOW_EVIL_CONSTRUCTORS(JSONReader); | 107 DISALLOW_EVIL_CONSTRUCTORS(JSONReader); |
111 | 108 |
112 FRIEND_TEST(JSONReaderTest, Reading); | 109 FRIEND_TEST(JSONReaderTest, Reading); |
113 FRIEND_TEST(JSONReaderTest, ErrorMessages); | 110 FRIEND_TEST(JSONReaderTest, ErrorMessages); |
114 | 111 |
115 // Returns the error message if the last call to JsonToValue() failed. If the | 112 // Returns the error message if the last call to JsonToValue() failed. If the |
116 // last call did not fail, returns a valid empty string. | 113 // last call did not fail, returns a valid empty string. |
117 std::string error_message() { return error_message_; } | 114 std::string error_message() { return error_message_; } |
118 | 115 |
119 // Pass through method from JSONReader::Read. We have this so unittests can | 116 // Pass through method from JSONReader::Read. We have this so unittests can |
120 // disable the root check. | 117 // disable the root check. |
121 bool JsonToValue(const std::string& json, Value** root, bool check_root, | 118 Value* JsonToValue(const std::string& json, bool check_root, |
122 bool allow_trailing_comma); | 119 bool allow_trailing_comma); |
123 | 120 |
124 // Recursively build Value. Returns false if we don't have a valid JSON | 121 // Recursively build Value. Returns NULL if we don't have a valid JSON |
125 // string. If |is_root| is true, we verify that the root element is either | 122 // string. If |is_root| is true, we verify that the root element is either |
126 // an object or an array. | 123 // an object or an array. |
127 bool BuildValue(Value** root, bool is_root); | 124 Value* BuildValue(bool is_root); |
128 | 125 |
129 // Parses a sequence of characters into a Token::NUMBER. If the sequence of | 126 // Parses a sequence of characters into a Token::NUMBER. If the sequence of |
130 // characters is not a valid number, returns a Token::INVALID_TOKEN. Note | 127 // characters is not a valid number, returns a Token::INVALID_TOKEN. Note |
131 // that DecodeNumber is used to actually convert from a string to an | 128 // that DecodeNumber is used to actually convert from a string to an |
132 // int/double. | 129 // int/double. |
133 Token ParseNumberToken(); | 130 Token ParseNumberToken(); |
134 | 131 |
135 // Try and convert the substring that token holds into an int or a double. If | 132 // Try and convert the substring that token holds into an int or a double. If |
136 // we can (ie., no overflow), return true and create the appropriate value | 133 // we can (ie., no overflow), return the value, else return NULL. |
137 // for |node|. Return false if we can't do the conversion. | 134 Value* DecodeNumber(const Token& token); |
138 bool DecodeNumber(const Token& token, Value** node); | |
139 | 135 |
140 // 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 |
141 // 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 |
142 // 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 |
143 // actual wstring. | 139 // actual wstring. |
144 Token ParseStringToken(); | 140 Token ParseStringToken(); |
145 | 141 |
146 // Convert the substring into a value string. This should always succeed | 142 // Convert the substring into a value string. This should always succeed |
147 // (otherwise ParseStringToken would have failed), but returns a success bool | 143 // (otherwise ParseStringToken would have failed), but returns a success bool |
148 // just in case. | 144 // just in case. |
149 bool DecodeString(const Token& token, Value** node); | 145 Value* DecodeString(const Token& token); |
150 | 146 |
151 // Grabs the next token in the JSON stream. This does not increment the | 147 // Grabs the next token in the JSON stream. This does not increment the |
152 // stream so it can be used to look ahead at the next token. | 148 // stream so it can be used to look ahead at the next token. |
153 Token ParseToken(); | 149 Token ParseToken(); |
154 | 150 |
155 // Increments json_pos_ past leading whitespace and comments. | 151 // Increments json_pos_ past leading whitespace and comments. |
156 void EatWhitespaceAndComments(); | 152 void EatWhitespaceAndComments(); |
157 | 153 |
158 // If json_pos_ is at the start of a comment, eat it, otherwise, returns | 154 // If json_pos_ is at the start of a comment, eat it, otherwise, returns |
159 // false. | 155 // false. |
(...skipping 16 matching lines...) Expand all Loading... |
176 int stack_depth_; | 172 int stack_depth_; |
177 | 173 |
178 // A parser flag that allows trailing commas in objects and arrays. | 174 // A parser flag that allows trailing commas in objects and arrays. |
179 bool allow_trailing_comma_; | 175 bool allow_trailing_comma_; |
180 | 176 |
181 // Contains the error message for the last call to JsonToValue(), if any. | 177 // Contains the error message for the last call to JsonToValue(), if any. |
182 std::string error_message_; | 178 std::string error_message_; |
183 }; | 179 }; |
184 | 180 |
185 #endif // BASE_JSON_READER_H_ | 181 #endif // BASE_JSON_READER_H_ |
OLD | NEW |