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 10 matching lines...) Expand all Loading... |
21 // character, the function skips a Unicode BOM at the beginning of the | 21 // character, the function skips a Unicode BOM at the beginning of the |
22 // Unicode string (converted from the input UTF-8 string) before parsing it. | 22 // Unicode string (converted from the input UTF-8 string) before parsing it. |
23 // | 23 // |
24 // TODO(tc): Add a parsing option to to relax object keys being wrapped in | 24 // TODO(tc): Add a parsing option to to relax object keys being wrapped in |
25 // double quotes | 25 // double quotes |
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 | 30 |
| 31 #include <memory> |
31 #include <string> | 32 #include <string> |
32 | 33 |
33 #include "base/base_export.h" | 34 #include "base/base_export.h" |
34 #include "base/memory/scoped_ptr.h" | |
35 #include "base/strings/string_piece.h" | 35 #include "base/strings/string_piece.h" |
36 | 36 |
37 namespace base { | 37 namespace base { |
38 | 38 |
39 class Value; | 39 class Value; |
40 | 40 |
41 namespace internal { | 41 namespace internal { |
42 class JSONParser; | 42 class JSONParser; |
43 } | 43 } |
44 | 44 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 // Constructs a reader with the default options, JSON_PARSE_RFC. | 86 // Constructs a reader with the default options, JSON_PARSE_RFC. |
87 JSONReader(); | 87 JSONReader(); |
88 | 88 |
89 // Constructs a reader with custom options. | 89 // Constructs a reader with custom options. |
90 explicit JSONReader(int options); | 90 explicit JSONReader(int options); |
91 | 91 |
92 ~JSONReader(); | 92 ~JSONReader(); |
93 | 93 |
94 // Reads and parses |json|, returning a Value. The caller owns the returned | 94 // Reads and parses |json|, returning a Value. The caller owns the returned |
95 // instance. If |json| is not a properly formed JSON string, returns NULL. | 95 // instance. If |json| is not a properly formed JSON string, returns NULL. |
96 static scoped_ptr<Value> Read(const StringPiece& json); | 96 static std::unique_ptr<Value> Read(const StringPiece& json); |
97 | 97 |
98 // Reads and parses |json|, returning a Value owned by the caller. The | 98 // Reads and parses |json|, returning a Value owned by the caller. The |
99 // parser respects the given |options|. If the input is not properly formed, | 99 // parser respects the given |options|. If the input is not properly formed, |
100 // returns NULL. | 100 // returns NULL. |
101 static scoped_ptr<Value> Read(const StringPiece& json, int options); | 101 static std::unique_ptr<Value> Read(const StringPiece& json, int options); |
102 | 102 |
103 // Reads and parses |json| like Read(). |error_code_out| and |error_msg_out| | 103 // Reads and parses |json| like Read(). |error_code_out| and |error_msg_out| |
104 // are optional. If specified and NULL is returned, they will be populated | 104 // are optional. If specified and NULL is returned, they will be populated |
105 // an error code and a formatted error message (including error location if | 105 // an error code and a formatted error message (including error location if |
106 // appropriate). Otherwise, they will be unmodified. | 106 // appropriate). Otherwise, they will be unmodified. |
107 static scoped_ptr<Value> ReadAndReturnError(const StringPiece& json, | 107 static std::unique_ptr<Value> ReadAndReturnError( |
108 int options, // JSONParserOptions | 108 const StringPiece& json, |
109 int* error_code_out, | 109 int options, // JSONParserOptions |
110 std::string* error_msg_out, | 110 int* error_code_out, |
111 int* error_line_out = nullptr, | 111 std::string* error_msg_out, |
112 int* error_column_out = nullptr); | 112 int* error_line_out = nullptr, |
| 113 int* error_column_out = nullptr); |
113 | 114 |
114 // Converts a JSON parse error code into a human readable message. | 115 // Converts a JSON parse error code into a human readable message. |
115 // Returns an empty string if error_code is JSON_NO_ERROR. | 116 // Returns an empty string if error_code is JSON_NO_ERROR. |
116 static std::string ErrorCodeToString(JsonParseError error_code); | 117 static std::string ErrorCodeToString(JsonParseError error_code); |
117 | 118 |
118 // Parses an input string into a Value that is owned by the caller. | 119 // Parses an input string into a Value that is owned by the caller. |
119 scoped_ptr<Value> ReadToValue(const std::string& json); | 120 std::unique_ptr<Value> ReadToValue(const std::string& json); |
120 | 121 |
121 // Returns the error code if the last call to ReadToValue() failed. | 122 // Returns the error code if the last call to ReadToValue() failed. |
122 // Returns JSON_NO_ERROR otherwise. | 123 // Returns JSON_NO_ERROR otherwise. |
123 JsonParseError error_code() const; | 124 JsonParseError error_code() const; |
124 | 125 |
125 // Converts error_code_ to a human-readable string, including line and column | 126 // Converts error_code_ to a human-readable string, including line and column |
126 // numbers if appropriate. | 127 // numbers if appropriate. |
127 std::string GetErrorMessage() const; | 128 std::string GetErrorMessage() const; |
128 | 129 |
129 private: | 130 private: |
130 scoped_ptr<internal::JSONParser> parser_; | 131 std::unique_ptr<internal::JSONParser> parser_; |
131 }; | 132 }; |
132 | 133 |
133 } // namespace base | 134 } // namespace base |
134 | 135 |
135 #endif // BASE_JSON_JSON_READER_H_ | 136 #endif // BASE_JSON_JSON_READER_H_ |
OLD | NEW |