OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 // TODO(aa): Consider making the constructor public and the static Read() method | 27 // TODO(aa): Consider making the constructor public and the static Read() method |
28 // only a convenience for the common uses with more complex configuration going | 28 // only a convenience for the common uses with more complex configuration going |
29 // on the instance. | 29 // on the instance. |
30 | 30 |
31 #ifndef BASE_JSON_READER_H_ | 31 #ifndef BASE_JSON_JSON_READER_H_ |
32 #define BASE_JSON_READER_H_ | 32 #define BASE_JSON_JSON_READER_H_ |
33 | 33 |
34 #include <string> | 34 #include <string> |
35 | 35 |
36 #include "base/basictypes.h" | 36 #include "base/basictypes.h" |
37 #include "testing/gtest/include/gtest/gtest_prod.h" | 37 #include "testing/gtest/include/gtest/gtest_prod.h" |
38 | 38 |
39 class Value; | 39 class Value; |
40 | 40 |
| 41 namespace base { |
| 42 |
41 class JSONReader { | 43 class JSONReader { |
42 public: | 44 public: |
43 // A struct to hold a JS token. | 45 // A struct to hold a JS token. |
44 class Token { | 46 class Token { |
45 public: | 47 public: |
46 enum Type { | 48 enum Type { |
47 OBJECT_BEGIN, // { | 49 OBJECT_BEGIN, // { |
48 OBJECT_END, // } | 50 OBJECT_END, // } |
49 ARRAY_BEGIN, // [ | 51 ARRAY_BEGIN, // [ |
50 ARRAY_END, // ] | 52 ARRAY_END, // ] |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 // array. Otherwise, it can be any valid JSON type. | 114 // array. Otherwise, it can be any valid JSON type. |
113 // If |allow_trailing_comma| is true, we will ignore trailing commas in | 115 // If |allow_trailing_comma| is true, we will ignore trailing commas in |
114 // objects and arrays even though this goes against the RFC. | 116 // objects and arrays even though this goes against the RFC. |
115 Value* JsonToValue(const std::string& json, bool check_root, | 117 Value* JsonToValue(const std::string& json, bool check_root, |
116 bool allow_trailing_comma); | 118 bool allow_trailing_comma); |
117 | 119 |
118 private: | 120 private: |
119 static std::string FormatErrorMessage(int line, int column, | 121 static std::string FormatErrorMessage(int line, int column, |
120 const char* description); | 122 const char* description); |
121 | 123 |
122 DISALLOW_EVIL_CONSTRUCTORS(JSONReader); | 124 DISALLOW_COPY_AND_ASSIGN(JSONReader); |
123 | 125 |
124 FRIEND_TEST(JSONReaderTest, Reading); | 126 FRIEND_TEST(JSONReaderTest, Reading); |
125 FRIEND_TEST(JSONReaderTest, ErrorMessages); | 127 FRIEND_TEST(JSONReaderTest, ErrorMessages); |
126 | 128 |
127 // Recursively build Value. Returns NULL if we don't have a valid JSON | 129 // Recursively build Value. Returns NULL if we don't have a valid JSON |
128 // string. If |is_root| is true, we verify that the root element is either | 130 // string. If |is_root| is true, we verify that the root element is either |
129 // an object or an array. | 131 // an object or an array. |
130 Value* BuildValue(bool is_root); | 132 Value* BuildValue(bool is_root); |
131 | 133 |
132 // Parses a sequence of characters into a Token::NUMBER. If the sequence of | 134 // Parses a sequence of characters into a Token::NUMBER. If the sequence of |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 // Used to keep track of how many nested lists/dicts there are. | 178 // Used to keep track of how many nested lists/dicts there are. |
177 int stack_depth_; | 179 int stack_depth_; |
178 | 180 |
179 // A parser flag that allows trailing commas in objects and arrays. | 181 // A parser flag that allows trailing commas in objects and arrays. |
180 bool allow_trailing_comma_; | 182 bool allow_trailing_comma_; |
181 | 183 |
182 // Contains the error message for the last call to JsonToValue(), if any. | 184 // Contains the error message for the last call to JsonToValue(), if any. |
183 std::string error_message_; | 185 std::string error_message_; |
184 }; | 186 }; |
185 | 187 |
186 #endif // BASE_JSON_READER_H_ | 188 } // namespace base |
| 189 |
| 190 #endif // BASE_JSON_JSON_READER_H_ |
OLD | NEW |