| 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 15 matching lines...) Expand all Loading... |
| 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 #include "base/memory/scoped_ptr.h" | 37 #include "base/memory/scoped_ptr.h" |
| 37 | 38 |
| 38 namespace base { | 39 namespace base { |
| 39 class Value; | 40 class Value; |
| 40 | 41 |
| 41 namespace internal { | 42 namespace internal { |
| 42 class JSONParser; | 43 class JSONParser; |
| 43 } | 44 } |
| 44 } | 45 } |
| 45 | 46 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 // Constructs a reader with the default options, JSON_PARSE_RFC. | 89 // Constructs a reader with the default options, JSON_PARSE_RFC. |
| 89 JSONReader(); | 90 JSONReader(); |
| 90 | 91 |
| 91 // Constructs a reader with custom options. | 92 // Constructs a reader with custom options. |
| 92 explicit JSONReader(int options); | 93 explicit JSONReader(int options); |
| 93 | 94 |
| 94 ~JSONReader(); | 95 ~JSONReader(); |
| 95 | 96 |
| 96 // Reads and parses |json|, returning a Value. The caller owns the returned | 97 // Reads and parses |json|, returning a Value. The caller owns the returned |
| 97 // instance. If |json| is not a properly formed JSON string, returns NULL. | 98 // instance. If |json| is not a properly formed JSON string, returns NULL. |
| 98 static Value* Read(const std::string& json); | 99 static Value* Read(const StringPiece& json); |
| 99 | 100 |
| 100 // Reads and parses |json|, returning a Value owned by the caller. The | 101 // Reads and parses |json|, returning a Value owned by the caller. The |
| 101 // parser respects the given |options|. If the input is not properly formed, | 102 // parser respects the given |options|. If the input is not properly formed, |
| 102 // returns NULL. | 103 // returns NULL. |
| 103 static Value* Read(const std::string& json, int options); | 104 static Value* Read(const StringPiece& json, int options); |
| 104 | 105 |
| 105 // Reads and parses |json| like Read(). |error_code_out| and |error_msg_out| | 106 // Reads and parses |json| like Read(). |error_code_out| and |error_msg_out| |
| 106 // are optional. If specified and NULL is returned, they will be populated | 107 // are optional. If specified and NULL is returned, they will be populated |
| 107 // an error code and a formatted error message (including error location if | 108 // an error code and a formatted error message (including error location if |
| 108 // appropriate). Otherwise, they will be unmodified. | 109 // appropriate). Otherwise, they will be unmodified. |
| 109 static Value* ReadAndReturnError(const std::string& json, | 110 static Value* ReadAndReturnError(const StringPiece& json, |
| 110 int options, // JSONParserOptions | 111 int options, // JSONParserOptions |
| 111 int* error_code_out, | 112 int* error_code_out, |
| 112 std::string* error_msg_out); | 113 std::string* error_msg_out); |
| 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 Value* ReadToValue(const std::string& json); | 120 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 scoped_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 |