OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
43 #define FRIEND_TEST_ALL_PREFIXES(test_case_name, test_name) \ | 43 #define FRIEND_TEST_ALL_PREFIXES(test_case_name, test_name) \ |
44 FRIEND_TEST(test_case_name, test_name); \ | 44 FRIEND_TEST(test_case_name, test_name); \ |
45 FRIEND_TEST(test_case_name, DISABLED_##test_name); \ | 45 FRIEND_TEST(test_case_name, DISABLED_##test_name); \ |
46 FRIEND_TEST(test_case_name, FLAKY_##test_name); \ | 46 FRIEND_TEST(test_case_name, FLAKY_##test_name); \ |
47 FRIEND_TEST(test_case_name, FAILS_##test_name) | 47 FRIEND_TEST(test_case_name, FAILS_##test_name) |
48 | 48 |
49 namespace base { | 49 namespace base { |
50 | 50 |
51 class Value; | 51 class Value; |
52 | 52 |
53 enum JSONParserOptions { | |
54 JSON_PARSE_RFC = 0, | |
Mark Mentovai
2012/04/10 21:51:02
…the idea being 0 is strict, and every other weird
Robert Sesek
2012/04/10 22:06:15
Correct. Other weird features may include single-q
| |
55 JSON_ALLOW_TRAILING_COMMAS = 1 << 1, | |
Mark Mentovai
2012/04/10 21:51:02
Because if so, this should be 1 << 0.
Robert Sesek
2012/04/10 22:06:15
Oops. Forgot to cherry-pick that fix from the rewr
| |
56 }; | |
57 | |
53 class BASE_EXPORT JSONReader { | 58 class BASE_EXPORT JSONReader { |
54 public: | 59 public: |
55 // A struct to hold a JS token. | 60 // A struct to hold a JS token. |
56 class Token { | 61 class Token { |
57 public: | 62 public: |
58 enum Type { | 63 enum Type { |
59 OBJECT_BEGIN, // { | 64 OBJECT_BEGIN, // { |
60 OBJECT_END, // } | 65 OBJECT_END, // } |
61 ARRAY_BEGIN, // [ | 66 ARRAY_BEGIN, // [ |
62 ARRAY_END, // ] | 67 ARRAY_END, // ] |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
112 static const char* kTrailingComma; | 117 static const char* kTrailingComma; |
113 static const char* kTooMuchNesting; | 118 static const char* kTooMuchNesting; |
114 static const char* kUnexpectedDataAfterRoot; | 119 static const char* kUnexpectedDataAfterRoot; |
115 static const char* kUnsupportedEncoding; | 120 static const char* kUnsupportedEncoding; |
116 static const char* kUnquotedDictionaryKey; | 121 static const char* kUnquotedDictionaryKey; |
117 | 122 |
118 JSONReader(); | 123 JSONReader(); |
119 | 124 |
120 // Reads and parses |json|, returning a Value. The caller owns the returned | 125 // Reads and parses |json|, returning a Value. The caller owns the returned |
121 // instance. If |json| is not a properly formed JSON string, returns NULL. | 126 // instance. If |json| is not a properly formed JSON string, returns NULL. |
122 // If |allow_trailing_comma| is true, we will ignore trailing commas in | 127 static Value* Read(const std::string& json); |
123 // objects and arrays even though this goes against the RFC. | 128 |
124 static Value* Read(const std::string& json, bool allow_trailing_comma); | 129 // Reads and parses |json|, returning a Value owned by the caller. The |
130 // parser respects the given |options|. If the input is not properly formed, | |
131 // returns NULL. | |
132 static Value* Read(const std::string& json, JSONParserOptions options); | |
Mark Mentovai
2012/04/10 21:51:02
More importantly, if so, you can’t use JSONParserO
Robert Sesek
2012/04/10 22:06:15
I want C++11 typesafe enums.
| |
125 | 133 |
126 // Reads and parses |json| like Read(). |error_code_out| and |error_msg_out| | 134 // Reads and parses |json| like Read(). |error_code_out| and |error_msg_out| |
127 // are optional. If specified and NULL is returned, they will be populated | 135 // are optional. If specified and NULL is returned, they will be populated |
128 // an error code and a formatted error message (including error location if | 136 // an error code and a formatted error message (including error location if |
129 // appropriate). Otherwise, they will be unmodified. | 137 // appropriate). Otherwise, they will be unmodified. |
130 static Value* ReadAndReturnError(const std::string& json, | 138 static Value* ReadAndReturnError(const std::string& json, |
131 bool allow_trailing_comma, | 139 JSONParserOptions options, |
132 int* error_code_out, | 140 int* error_code_out, |
133 std::string* error_msg_out); | 141 std::string* error_msg_out); |
134 | 142 |
135 // Converts a JSON parse error code into a human readable message. | 143 // Converts a JSON parse error code into a human readable message. |
136 // Returns an empty string if error_code is JSON_NO_ERROR. | 144 // Returns an empty string if error_code is JSON_NO_ERROR. |
137 static std::string ErrorCodeToString(JsonParseError error_code); | 145 static std::string ErrorCodeToString(JsonParseError error_code); |
138 | 146 |
139 // Returns the error code if the last call to JsonToValue() failed. | 147 // Returns the error code if the last call to JsonToValue() failed. |
140 // Returns JSON_NO_ERROR otherwise. | 148 // Returns JSON_NO_ERROR otherwise. |
141 JsonParseError error_code() const { return error_code_; } | 149 JsonParseError error_code() const { return error_code_; } |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
234 JsonParseError error_code_; | 242 JsonParseError error_code_; |
235 int error_line_; | 243 int error_line_; |
236 int error_col_; | 244 int error_col_; |
237 | 245 |
238 DISALLOW_COPY_AND_ASSIGN(JSONReader); | 246 DISALLOW_COPY_AND_ASSIGN(JSONReader); |
239 }; | 247 }; |
240 | 248 |
241 } // namespace base | 249 } // namespace base |
242 | 250 |
243 #endif // BASE_JSON_JSON_READER_H_ | 251 #endif // BASE_JSON_JSON_READER_H_ |
OLD | NEW |