Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(325)

Side by Side Diff: base/json/json_reader.h

Issue 9960077: Modify the base::JSONReader interface to take a set of options rather than a boolean flag. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/debug/trace_event_unittest.cc ('k') | base/json/json_reader.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 // Parses the input strictly according to RFC 4627, except for where noted
55 // above.
56 JSON_PARSE_RFC = 0,
57 // Allows commas to exist after the last element in structures.
Mark Mentovai 2012/04/10 22:08:28 Blank line before.
58 JSON_ALLOW_TRAILING_COMMAS = 1 << 0,
59 };
60
53 class BASE_EXPORT JSONReader { 61 class BASE_EXPORT JSONReader {
54 public: 62 public:
55 // A struct to hold a JS token. 63 // A struct to hold a JS token.
56 class Token { 64 class Token {
57 public: 65 public:
58 enum Type { 66 enum Type {
59 OBJECT_BEGIN, // { 67 OBJECT_BEGIN, // {
60 OBJECT_END, // } 68 OBJECT_END, // }
61 ARRAY_BEGIN, // [ 69 ARRAY_BEGIN, // [
62 ARRAY_END, // ] 70 ARRAY_END, // ]
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 static const char* kTrailingComma; 120 static const char* kTrailingComma;
113 static const char* kTooMuchNesting; 121 static const char* kTooMuchNesting;
114 static const char* kUnexpectedDataAfterRoot; 122 static const char* kUnexpectedDataAfterRoot;
115 static const char* kUnsupportedEncoding; 123 static const char* kUnsupportedEncoding;
116 static const char* kUnquotedDictionaryKey; 124 static const char* kUnquotedDictionaryKey;
117 125
118 JSONReader(); 126 JSONReader();
119 127
120 // Reads and parses |json|, returning a Value. The caller owns the returned 128 // 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. 129 // 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 130 static Value* Read(const std::string& json);
123 // objects and arrays even though this goes against the RFC. 131
124 static Value* Read(const std::string& json, bool allow_trailing_comma); 132 // Reads and parses |json|, returning a Value owned by the caller. The
133 // parser respects the given |options|. If the input is not properly formed,
134 // returns NULL.
135 static Value* Read(const std::string& json, int options);
125 136
126 // Reads and parses |json| like Read(). |error_code_out| and |error_msg_out| 137 // 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 138 // 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 139 // an error code and a formatted error message (including error location if
129 // appropriate). Otherwise, they will be unmodified. 140 // appropriate). Otherwise, they will be unmodified.
130 static Value* ReadAndReturnError(const std::string& json, 141 static Value* ReadAndReturnError(const std::string& json,
131 bool allow_trailing_comma, 142 int options, // JSONParserOptions
132 int* error_code_out, 143 int* error_code_out,
133 std::string* error_msg_out); 144 std::string* error_msg_out);
134 145
135 // Converts a JSON parse error code into a human readable message. 146 // Converts a JSON parse error code into a human readable message.
136 // Returns an empty string if error_code is JSON_NO_ERROR. 147 // Returns an empty string if error_code is JSON_NO_ERROR.
137 static std::string ErrorCodeToString(JsonParseError error_code); 148 static std::string ErrorCodeToString(JsonParseError error_code);
138 149
139 // Returns the error code if the last call to JsonToValue() failed. 150 // Returns the error code if the last call to JsonToValue() failed.
140 // Returns JSON_NO_ERROR otherwise. 151 // Returns JSON_NO_ERROR otherwise.
141 JsonParseError error_code() const { return error_code_; } 152 JsonParseError error_code() const { return error_code_; }
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
234 JsonParseError error_code_; 245 JsonParseError error_code_;
235 int error_line_; 246 int error_line_;
236 int error_col_; 247 int error_col_;
237 248
238 DISALLOW_COPY_AND_ASSIGN(JSONReader); 249 DISALLOW_COPY_AND_ASSIGN(JSONReader);
239 }; 250 };
240 251
241 } // namespace base 252 } // namespace base
242 253
243 #endif // BASE_JSON_JSON_READER_H_ 254 #endif // BASE_JSON_JSON_READER_H_
OLDNEW
« no previous file with comments | « base/debug/trace_event_unittest.cc ('k') | base/json/json_reader.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698