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

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

Issue 10003001: Update JSONReader to take base::StringPiece instead of std::string (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: 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
« no previous file with comments | « no previous file | base/json/json_reader.cc » ('j') | chrome/browser/extensions/component_loader.h » ('J')
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 15 matching lines...) Expand all
26 // TODO(tc): Add an option to disable comment stripping 26 // TODO(tc): Add an option to disable comment stripping
27 27
28 #ifndef BASE_JSON_JSON_READER_H_ 28 #ifndef BASE_JSON_JSON_READER_H_
29 #define BASE_JSON_JSON_READER_H_ 29 #define BASE_JSON_JSON_READER_H_
30 #pragma once 30 #pragma once
31 31
32 #include <string> 32 #include <string>
33 33
34 #include "base/base_export.h" 34 #include "base/base_export.h"
35 #include "base/basictypes.h" 35 #include "base/basictypes.h"
36 #include "base/string_piece.h"
36 37
37 // Chromium and Chromium OS check out gtest to different places, so we're 38 // Chromium and Chromium OS check out gtest to different places, so we're
38 // unable to compile on both if we include gtest_prod.h here. Instead, include 39 // unable to compile on both if we include gtest_prod.h here. Instead, include
39 // its only contents -- this will need to be updated if the macro ever changes. 40 // its only contents -- this will need to be updated if the macro ever changes.
40 #define FRIEND_TEST(test_case_name, test_name)\ 41 #define FRIEND_TEST(test_case_name, test_name)\
41 friend class test_case_name##_##test_name##_Test 42 friend class test_case_name##_##test_name##_Test
42 43
43 #define FRIEND_TEST_ALL_PREFIXES(test_case_name, test_name) \ 44 #define FRIEND_TEST_ALL_PREFIXES(test_case_name, test_name) \
44 FRIEND_TEST(test_case_name, test_name); \ 45 FRIEND_TEST(test_case_name, test_name); \
45 FRIEND_TEST(test_case_name, DISABLED_##test_name); \ 46 FRIEND_TEST(test_case_name, DISABLED_##test_name); \
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 static const char* kUnexpectedDataAfterRoot; 115 static const char* kUnexpectedDataAfterRoot;
115 static const char* kUnsupportedEncoding; 116 static const char* kUnsupportedEncoding;
116 static const char* kUnquotedDictionaryKey; 117 static const char* kUnquotedDictionaryKey;
117 118
118 JSONReader(); 119 JSONReader();
119 120
120 // Reads and parses |json|, returning a Value. The caller owns the returned 121 // 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. 122 // 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 123 // If |allow_trailing_comma| is true, we will ignore trailing commas in
123 // objects and arrays even though this goes against the RFC. 124 // objects and arrays even though this goes against the RFC.
124 static Value* Read(const std::string& json, bool allow_trailing_comma); 125 static Value* Read(const StringPiece& json, bool allow_trailing_comma);
125 126
126 // Reads and parses |json| like Read(). |error_code_out| and |error_msg_out| 127 // 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 128 // 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 129 // an error code and a formatted error message (including error location if
129 // appropriate). Otherwise, they will be unmodified. 130 // appropriate). Otherwise, they will be unmodified.
130 static Value* ReadAndReturnError(const std::string& json, 131 static Value* ReadAndReturnError(const StringPiece& json,
131 bool allow_trailing_comma, 132 bool allow_trailing_comma,
132 int* error_code_out, 133 int* error_code_out,
133 std::string* error_msg_out); 134 std::string* error_msg_out);
134 135
135 // Converts a JSON parse error code into a human readable message. 136 // Converts a JSON parse error code into a human readable message.
136 // Returns an empty string if error_code is JSON_NO_ERROR. 137 // Returns an empty string if error_code is JSON_NO_ERROR.
137 static std::string ErrorCodeToString(JsonParseError error_code); 138 static std::string ErrorCodeToString(JsonParseError error_code);
138 139
139 // Returns the error code if the last call to JsonToValue() failed. 140 // Returns the error code if the last call to JsonToValue() failed.
140 // Returns JSON_NO_ERROR otherwise. 141 // Returns JSON_NO_ERROR otherwise.
141 JsonParseError error_code() const { return error_code_; } 142 JsonParseError error_code() const { return error_code_; }
142 143
143 // Converts error_code_ to a human-readable string, including line and column 144 // Converts error_code_ to a human-readable string, including line and column
144 // numbers if appropriate. 145 // numbers if appropriate.
145 std::string GetErrorMessage() const; 146 std::string GetErrorMessage() const;
146 147
147 // Reads and parses |json|, returning a Value. The caller owns the returned 148 // Reads and parses |json|, returning a Value. The caller owns the returned
148 // instance. If |json| is not a properly formed JSON string, returns NULL and 149 // instance. If |json| is not a properly formed JSON string, returns NULL and
149 // a detailed error can be retrieved from |error_message()|. 150 // a detailed error can be retrieved from |error_message()|.
150 // If |check_root| is true, we require that the root object be an object or 151 // If |check_root| is true, we require that the root object be an object or
151 // array. Otherwise, it can be any valid JSON type. 152 // array. Otherwise, it can be any valid JSON type.
152 // If |allow_trailing_comma| is true, we will ignore trailing commas in 153 // If |allow_trailing_comma| is true, we will ignore trailing commas in
153 // objects and arrays even though this goes against the RFC. 154 // objects and arrays even though this goes against the RFC.
154 Value* JsonToValue(const std::string& json, bool check_root, 155 Value* JsonToValue(const StringPiece& json, bool check_root,
155 bool allow_trailing_comma); 156 bool allow_trailing_comma);
156 157
157 private: 158 private:
158 FRIEND_TEST_ALL_PREFIXES(JSONReaderTest, Reading); 159 FRIEND_TEST_ALL_PREFIXES(JSONReaderTest, Reading);
159 FRIEND_TEST_ALL_PREFIXES(JSONReaderTest, ErrorMessages); 160 FRIEND_TEST_ALL_PREFIXES(JSONReaderTest, ErrorMessages);
160 161
161 static std::string FormatErrorMessage(int line, int column, 162 static std::string FormatErrorMessage(int line, int column,
162 const std::string& description); 163 const std::string& description);
163 164
164 // Recursively build Value. Returns NULL if we don't have a valid JSON 165 // Recursively build Value. Returns NULL if we don't have a valid JSON
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
234 JsonParseError error_code_; 235 JsonParseError error_code_;
235 int error_line_; 236 int error_line_;
236 int error_col_; 237 int error_col_;
237 238
238 DISALLOW_COPY_AND_ASSIGN(JSONReader); 239 DISALLOW_COPY_AND_ASSIGN(JSONReader);
239 }; 240 };
240 241
241 } // namespace base 242 } // namespace base
242 243
243 #endif // BASE_JSON_JSON_READER_H_ 244 #endif // BASE_JSON_JSON_READER_H_
OLDNEW
« no previous file with comments | « no previous file | base/json/json_reader.cc » ('j') | chrome/browser/extensions/component_loader.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698