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

Unified Diff: base/json/json_reader.h

Issue 1120006: detect preferences errors (Closed)
Patch Set: changes from review Created 10 years, 9 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | base/json/json_reader.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/json/json_reader.h
diff --git a/base/json/json_reader.h b/base/json/json_reader.h
index 47598f42a6e2c230717421664bda044c28c4ad1a..aa0c2a72de189e50b87c1e300aec03368c59dd00 100644
--- a/base/json/json_reader.h
+++ b/base/json/json_reader.h
@@ -82,7 +82,20 @@ class JSONReader {
}
};
- // Error messages that can be returned.
+ // Error codes during parsing.
+ enum JsonParseError {
+ JSON_NO_ERROR = 0,
+ JSON_BAD_ROOT_ELEMENT_TYPE,
+ JSON_INVALID_ESCAPE,
+ JSON_SYNTAX_ERROR,
+ JSON_TRAILING_COMMA,
+ JSON_TOO_MUCH_NESTING,
+ JSON_UNEXPECTED_DATA_AFTER_ROOT,
+ JSON_UNSUPPORTED_ENCODING,
+ JSON_UNQUOTED_DICTIONARY_KEY,
+ };
+
+ // String versions of parse error codes.
static const char* kBadRootElementType;
static const char* kInvalidEscape;
static const char* kSyntaxError;
@@ -100,17 +113,26 @@ class JSONReader {
// objects and arrays even though this goes against the RFC.
static Value* Read(const std::string& json, bool allow_trailing_comma);
- // Reads and parses |json| like Read(). |error_message_out| is optional. If
- // specified and NULL is returned, |error_message_out| will be populated with
- // a string describing the error. Otherwise, |error_message_out| is
- // unmodified.
+ // Reads and parses |json| like Read(). |error_code_out| and |error_msg_out|
+ // are optional. If specified and NULL is returned, they will be populated
+ // an error code and a formatted error message (including error location if
+ // appropriate). Otherwise, they will be unmodified.
static Value* ReadAndReturnError(const std::string& json,
bool allow_trailing_comma,
- std::string* error_message_out);
+ int* error_code_out,
+ std::string* error_msg_out);
+
+ // Converts a JSON parse error code into a human readable message.
+ // Returns an empty string if error_code is JSON_NO_ERROR.
+ static std::string ErrorCodeToString(JsonParseError error_code);
+
+ // Returns the error code if the last call to JsonToValue() failed.
+ // Returns JSON_NO_ERROR otherwise.
+ JsonParseError error_code() const { return error_code_; }
- // Returns the error message if the last call to JsonToValue() failed. If the
- // last call did not fail, returns a valid empty string.
- std::string error_message() { return error_message_; }
+ // Converts error_code_ to a human-readable string, including line and column
+ // numbers if appropriate.
+ std::string GetErrorMessage() const;
// Reads and parses |json|, returning a Value. The caller owns the returned
// instance. If |json| is not a properly formed JSON string, returns NULL and
@@ -124,7 +146,7 @@ class JSONReader {
private:
static std::string FormatErrorMessage(int line, int column,
- const char* description);
+ const std::string& description);
DISALLOW_COPY_AND_ASSIGN(JSONReader);
@@ -170,9 +192,9 @@ class JSONReader {
// Checks if |json_pos_| matches str.
bool NextStringMatch(const std::wstring& str);
- // Creates the error message that will be returned to the caller. The current
+ // Sets the error code that will be returned to the caller. The current
// line and column are determined and added into the final message.
- void SetErrorMessage(const char* description, const wchar_t* error_pos);
+ void SetErrorCode(const JsonParseError error, const wchar_t* error_pos);
// Pointer to the starting position in the input string.
const wchar_t* start_pos_;
@@ -186,8 +208,10 @@ class JSONReader {
// A parser flag that allows trailing commas in objects and arrays.
bool allow_trailing_comma_;
- // Contains the error message for the last call to JsonToValue(), if any.
- std::string error_message_;
+ // Contains the error code for the last call to JsonToValue(), if any.
+ JsonParseError error_code_;
+ int error_line_;
+ int error_col_;
};
} // namespace base
« no previous file with comments | « no previous file | base/json/json_reader.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698