| 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 JSONReader(); |
| 89 |
| 88 // Reads and parses |json|, returning a Value. The caller owns the returned | 90 // 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. | 91 // 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 | 92 // If |allow_trailing_comma| is true, we will ignore trailing commas in |
| 91 // objects and arrays even though this goes against the RFC. | 93 // objects and arrays even though this goes against the RFC. |
| 92 static Value* Read(const std::string& json, bool allow_trailing_comma); | 94 static Value* Read(const std::string& json, bool allow_trailing_comma); |
| 93 | 95 |
| 94 // Reads and parses |json| like Read(). |error_message_out| is optional. If | 96 // Reads and parses |json| like Read(). |error_message_out| is optional. If |
| 95 // specified and NULL is returned, |error_message_out| will be populated with | 97 // specified and NULL is returned, |error_message_out| will be populated with |
| 96 // a string describing the error. Otherwise, |error_message_out| is | 98 // a string describing the error. Otherwise, |error_message_out| is |
| 97 // unmodified. | 99 // unmodified. |
| 98 static Value* ReadAndReturnError(const std::string& json, | 100 static Value* ReadAndReturnError(const std::string& json, |
| 99 bool allow_trailing_comma, | 101 bool allow_trailing_comma, |
| 100 std::string* error_message_out); | 102 std::string* error_message_out); |
| 101 | 103 |
| 104 // Returns the error message if the last call to JsonToValue() failed. If the |
| 105 // last call did not fail, returns a valid empty string. |
| 106 std::string error_message() { return error_message_; } |
| 107 |
| 108 // Reads and parses |json|, returning a Value. The caller owns the returned |
| 109 // instance. If |json| is not a properly formed JSON string, returns NULL and |
| 110 // a detailed error can be retrieved from |error_message()|. |
| 111 // If |check_root| is true, we require that the root object be an object or |
| 112 // array. Otherwise, it can be any valid JSON type. |
| 113 // If |allow_trailing_comma| is true, we will ignore trailing commas in |
| 114 // objects and arrays even though this goes against the RFC. |
| 115 Value* JsonToValue(const std::string& json, bool check_root, |
| 116 bool allow_trailing_comma); |
| 117 |
| 102 private: | 118 private: |
| 103 static std::string FormatErrorMessage(int line, int column, | 119 static std::string FormatErrorMessage(int line, int column, |
| 104 const char* description); | 120 const char* description); |
| 105 | 121 |
| 106 JSONReader(); | |
| 107 DISALLOW_EVIL_CONSTRUCTORS(JSONReader); | 122 DISALLOW_EVIL_CONSTRUCTORS(JSONReader); |
| 108 | 123 |
| 109 FRIEND_TEST(JSONReaderTest, Reading); | 124 FRIEND_TEST(JSONReaderTest, Reading); |
| 110 FRIEND_TEST(JSONReaderTest, ErrorMessages); | 125 FRIEND_TEST(JSONReaderTest, ErrorMessages); |
| 111 | 126 |
| 112 // Returns the error message if the last call to JsonToValue() failed. If the | |
| 113 // last call did not fail, returns a valid empty string. | |
| 114 std::string error_message() { return error_message_; } | |
| 115 | |
| 116 // Pass through method from JSONReader::Read. We have this so unittests can | |
| 117 // disable the root check. | |
| 118 Value* JsonToValue(const std::string& json, bool check_root, | |
| 119 bool allow_trailing_comma); | |
| 120 | |
| 121 // Recursively build Value. Returns NULL if we don't have a valid JSON | 127 // Recursively build Value. Returns NULL if we don't have a valid JSON |
| 122 // string. If |is_root| is true, we verify that the root element is either | 128 // string. If |is_root| is true, we verify that the root element is either |
| 123 // an object or an array. | 129 // an object or an array. |
| 124 Value* BuildValue(bool is_root); | 130 Value* BuildValue(bool is_root); |
| 125 | 131 |
| 126 // Parses a sequence of characters into a Token::NUMBER. If the sequence of | 132 // Parses a sequence of characters into a Token::NUMBER. If the sequence of |
| 127 // characters is not a valid number, returns a Token::INVALID_TOKEN. Note | 133 // characters is not a valid number, returns a Token::INVALID_TOKEN. Note |
| 128 // that DecodeNumber is used to actually convert from a string to an | 134 // that DecodeNumber is used to actually convert from a string to an |
| 129 // int/double. | 135 // int/double. |
| 130 Token ParseNumberToken(); | 136 Token ParseNumberToken(); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 int stack_depth_; | 177 int stack_depth_; |
| 172 | 178 |
| 173 // A parser flag that allows trailing commas in objects and arrays. | 179 // A parser flag that allows trailing commas in objects and arrays. |
| 174 bool allow_trailing_comma_; | 180 bool allow_trailing_comma_; |
| 175 | 181 |
| 176 // Contains the error message for the last call to JsonToValue(), if any. | 182 // Contains the error message for the last call to JsonToValue(), if any. |
| 177 std::string error_message_; | 183 std::string error_message_; |
| 178 }; | 184 }; |
| 179 | 185 |
| 180 #endif // BASE_JSON_READER_H_ | 186 #endif // BASE_JSON_READER_H_ |
| OLD | NEW |