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 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
8 #include "base/json/json_file_value_serializer.h" | 8 #include "base/json/json_file_value_serializer.h" |
9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
10 #include "base/json/json_string_value_serializer.h" | 10 #include "base/json/json_string_value_serializer.h" |
11 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
12 #include "base/path_service.h" | 12 #include "base/path_service.h" |
13 #include "base/string16.h" | 13 #include "base/string16.h" |
14 #include "base/string_util.h" | 14 #include "base/string_util.h" |
15 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
16 #include "base/values.h" | 16 #include "base/values.h" |
17 #include "chrome/common/chrome_paths.h" | 17 #include "chrome/common/chrome_paths.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
19 | 19 |
20 TEST(JSONValueSerializerTest, Roundtrip) { | |
21 const std::string original_serialization = | |
22 "{\"bool\":true,\"double\":3.14,\"int\":42,\"list\":[1,2],\"null\":null}"; | |
23 JSONStringValueSerializer serializer(original_serialization); | |
24 scoped_ptr<Value> root(serializer.Deserialize(NULL, NULL)); | |
25 ASSERT_TRUE(root.get()); | |
26 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); | |
27 | |
28 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get()); | |
29 | |
30 Value* null_value = NULL; | |
31 ASSERT_TRUE(root_dict->Get("null", &null_value)); | |
32 ASSERT_TRUE(null_value); | |
33 ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL)); | |
34 | |
35 bool bool_value = false; | |
36 ASSERT_TRUE(root_dict->GetBoolean("bool", &bool_value)); | |
37 ASSERT_TRUE(bool_value); | |
38 | |
39 int int_value = 0; | |
40 ASSERT_TRUE(root_dict->GetInteger("int", &int_value)); | |
41 ASSERT_EQ(42, int_value); | |
42 | |
43 double double_value = 0.0; | |
44 ASSERT_TRUE(root_dict->GetDouble("double", &double_value)); | |
45 ASSERT_DOUBLE_EQ(3.14, double_value); | |
46 | |
47 // We shouldn't be able to write using this serializer, since it was | |
48 // initialized with a const string. | |
49 ASSERT_FALSE(serializer.Serialize(*root_dict)); | |
50 | |
51 std::string test_serialization = ""; | |
52 JSONStringValueSerializer mutable_serializer(&test_serialization); | |
53 ASSERT_TRUE(mutable_serializer.Serialize(*root_dict)); | |
54 ASSERT_EQ(original_serialization, test_serialization); | |
55 | |
56 mutable_serializer.set_pretty_print(true); | |
57 ASSERT_TRUE(mutable_serializer.Serialize(*root_dict)); | |
58 // JSON output uses a different newline style on Windows than on other | |
59 // platforms. | |
60 #if defined(OS_WIN) | |
61 #define JSON_NEWLINE "\r\n" | |
62 #else | |
63 #define JSON_NEWLINE "\n" | |
64 #endif | |
65 const std::string pretty_serialization = | |
66 "{" JSON_NEWLINE | |
67 " \"bool\": true," JSON_NEWLINE | |
68 " \"double\": 3.14," JSON_NEWLINE | |
69 " \"int\": 42," JSON_NEWLINE | |
70 " \"list\": [ 1, 2 ]," JSON_NEWLINE | |
71 " \"null\": null" JSON_NEWLINE | |
72 "}" JSON_NEWLINE; | |
73 #undef JSON_NEWLINE | |
74 ASSERT_EQ(pretty_serialization, test_serialization); | |
75 } | |
76 | |
77 TEST(JSONValueSerializerTest, StringEscape) { | |
78 string16 all_chars; | |
79 for (int i = 1; i < 256; ++i) { | |
80 all_chars += static_cast<char16>(i); | |
81 } | |
82 // Generated in in Firefox using the following js (with an extra backslash for | |
83 // double quote): | |
84 // var s = ''; | |
85 // for (var i = 1; i < 256; ++i) { s += String.fromCharCode(i); } | |
86 // uneval(s).replace(/\\/g, "\\\\"); | |
87 std::string all_chars_expected = | |
88 "\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007\\b\\t\\n\\u000B\\f\\r" | |
89 "\\u000E\\u000F\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017" | |
90 "\\u0018\\u0019\\u001A\\u001B\\u001C\\u001D\\u001E\\u001F !\\\"" | |
91 "#$%&'()*+,-./0123456789:;\\u003C=\\u003E?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\" | |
92 "\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\\u007F\\u0080\\u0081\\u0082\\u0083" | |
93 "\\u0084\\u0085\\u0086\\u0087\\u0088\\u0089\\u008A\\u008B\\u008C\\u008D" | |
94 "\\u008E\\u008F\\u0090\\u0091\\u0092\\u0093\\u0094\\u0095\\u0096\\u0097" | |
95 "\\u0098\\u0099\\u009A\\u009B\\u009C\\u009D\\u009E\\u009F\\u00A0\\u00A1" | |
96 "\\u00A2\\u00A3\\u00A4\\u00A5\\u00A6\\u00A7\\u00A8\\u00A9\\u00AA\\u00AB" | |
97 "\\u00AC\\u00AD\\u00AE\\u00AF\\u00B0\\u00B1\\u00B2\\u00B3\\u00B4\\u00B5" | |
98 "\\u00B6\\u00B7\\u00B8\\u00B9\\u00BA\\u00BB\\u00BC\\u00BD\\u00BE\\u00BF" | |
99 "\\u00C0\\u00C1\\u00C2\\u00C3\\u00C4\\u00C5\\u00C6\\u00C7\\u00C8\\u00C9" | |
100 "\\u00CA\\u00CB\\u00CC\\u00CD\\u00CE\\u00CF\\u00D0\\u00D1\\u00D2\\u00D3" | |
101 "\\u00D4\\u00D5\\u00D6\\u00D7\\u00D8\\u00D9\\u00DA\\u00DB\\u00DC\\u00DD" | |
102 "\\u00DE\\u00DF\\u00E0\\u00E1\\u00E2\\u00E3\\u00E4\\u00E5\\u00E6\\u00E7" | |
103 "\\u00E8\\u00E9\\u00EA\\u00EB\\u00EC\\u00ED\\u00EE\\u00EF\\u00F0\\u00F1" | |
104 "\\u00F2\\u00F3\\u00F4\\u00F5\\u00F6\\u00F7\\u00F8\\u00F9\\u00FA\\u00FB" | |
105 "\\u00FC\\u00FD\\u00FE\\u00FF"; | |
106 | |
107 std::string expected_output = "{\"all_chars\":\"" + all_chars_expected + | |
108 "\"}"; | |
109 // Test JSONWriter interface | |
110 std::string output_js; | |
111 DictionaryValue valueRoot; | |
112 valueRoot.SetString("all_chars", all_chars); | |
113 base::JSONWriter::Write(&valueRoot, &output_js); | |
114 ASSERT_EQ(expected_output, output_js); | |
115 | |
116 // Test JSONValueSerializer interface (uses JSONWriter). | |
117 JSONStringValueSerializer serializer(&output_js); | |
118 ASSERT_TRUE(serializer.Serialize(valueRoot)); | |
119 ASSERT_EQ(expected_output, output_js); | |
120 } | |
121 | |
122 TEST(JSONValueSerializerTest, UnicodeStrings) { | |
123 // unicode string json -> escaped ascii text | |
124 DictionaryValue root; | |
125 string16 test(WideToUTF16(L"\x7F51\x9875")); | |
126 root.SetString("web", test); | |
127 | |
128 std::string expected = "{\"web\":\"\\u7F51\\u9875\"}"; | |
129 | |
130 std::string actual; | |
131 JSONStringValueSerializer serializer(&actual); | |
132 ASSERT_TRUE(serializer.Serialize(root)); | |
133 ASSERT_EQ(expected, actual); | |
134 | |
135 // escaped ascii text -> json | |
136 JSONStringValueSerializer deserializer(expected); | |
137 scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL, NULL)); | |
138 ASSERT_TRUE(deserial_root.get()); | |
139 DictionaryValue* dict_root = | |
140 static_cast<DictionaryValue*>(deserial_root.get()); | |
141 string16 web_value; | |
142 ASSERT_TRUE(dict_root->GetString("web", &web_value)); | |
143 ASSERT_EQ(test, web_value); | |
144 } | |
145 | |
146 TEST(JSONValueSerializerTest, HexStrings) { | |
147 // hex string json -> escaped ascii text | |
148 DictionaryValue root; | |
149 string16 test(WideToUTF16(L"\x01\x02")); | |
150 root.SetString("test", test); | |
151 | |
152 std::string expected = "{\"test\":\"\\u0001\\u0002\"}"; | |
153 | |
154 std::string actual; | |
155 JSONStringValueSerializer serializer(&actual); | |
156 ASSERT_TRUE(serializer.Serialize(root)); | |
157 ASSERT_EQ(expected, actual); | |
158 | |
159 // escaped ascii text -> json | |
160 JSONStringValueSerializer deserializer(expected); | |
161 scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL, NULL)); | |
162 ASSERT_TRUE(deserial_root.get()); | |
163 DictionaryValue* dict_root = | |
164 static_cast<DictionaryValue*>(deserial_root.get()); | |
165 string16 test_value; | |
166 ASSERT_TRUE(dict_root->GetString("test", &test_value)); | |
167 ASSERT_EQ(test, test_value); | |
168 | |
169 // Test converting escaped regular chars | |
170 std::string escaped_chars = "{\"test\":\"\\u0067\\u006f\"}"; | |
171 JSONStringValueSerializer deserializer2(escaped_chars); | |
172 deserial_root.reset(deserializer2.Deserialize(NULL, NULL)); | |
173 ASSERT_TRUE(deserial_root.get()); | |
174 dict_root = static_cast<DictionaryValue*>(deserial_root.get()); | |
175 ASSERT_TRUE(dict_root->GetString("test", &test_value)); | |
176 ASSERT_EQ(ASCIIToUTF16("go"), test_value); | |
177 } | |
178 | |
179 TEST(JSONValueSerializerTest, AllowTrailingComma) { | |
180 scoped_ptr<Value> root; | |
181 scoped_ptr<Value> root_expected; | |
182 std::string test_with_commas("{\"key\": [true,],}"); | |
183 std::string test_no_commas("{\"key\": [true]}"); | |
184 | |
185 JSONStringValueSerializer serializer(test_with_commas); | |
186 serializer.set_allow_trailing_comma(true); | |
187 JSONStringValueSerializer serializer_expected(test_no_commas); | |
188 root.reset(serializer.Deserialize(NULL, NULL)); | |
189 ASSERT_TRUE(root.get()); | |
190 root_expected.reset(serializer_expected.Deserialize(NULL, NULL)); | |
191 ASSERT_TRUE(root_expected.get()); | |
192 ASSERT_TRUE(root->Equals(root_expected.get())); | |
193 } | |
194 | |
195 namespace { | |
196 | |
197 void ValidateJsonList(const std::string& json) { | |
198 scoped_ptr<Value> root(base::JSONReader::Read(json)); | |
199 ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST)); | |
200 ListValue* list = static_cast<ListValue*>(root.get()); | |
201 ASSERT_EQ(1U, list->GetSize()); | |
202 Value* elt = NULL; | |
203 ASSERT_TRUE(list->Get(0, &elt)); | |
204 int value = 0; | |
205 ASSERT_TRUE(elt && elt->GetAsInteger(&value)); | |
206 ASSERT_EQ(1, value); | |
207 } | |
208 | |
209 } // namespace | |
210 | |
211 TEST(JSONValueSerializerTest, JSONReaderComments) { | |
212 ValidateJsonList("[ // 2, 3, ignore me ] \n1 ]"); | |
213 ValidateJsonList("[ /* 2, \n3, ignore me ]*/ \n1 ]"); | |
214 ValidateJsonList("//header\n[ // 2, \n// 3, \n1 ]// footer"); | |
215 ValidateJsonList("/*\n[ // 2, \n// 3, \n1 ]*/[1]"); | |
216 ValidateJsonList("[ 1 /* one */ ] /* end */"); | |
217 ValidateJsonList("[ 1 //// ,2\r\n ]"); | |
218 | |
219 scoped_ptr<Value> root; | |
220 | |
221 // It's ok to have a comment in a string. | |
222 root.reset(base::JSONReader::Read("[\"// ok\\n /* foo */ \"]")); | |
223 ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST)); | |
224 ListValue* list = static_cast<ListValue*>(root.get()); | |
225 ASSERT_EQ(1U, list->GetSize()); | |
226 Value* elt = NULL; | |
227 ASSERT_TRUE(list->Get(0, &elt)); | |
228 std::string value; | |
229 ASSERT_TRUE(elt && elt->GetAsString(&value)); | |
230 ASSERT_EQ("// ok\n /* foo */ ", value); | |
231 | |
232 // You can't nest comments. | |
233 root.reset(base::JSONReader::Read("/* /* inner */ outer */ [ 1 ]")); | |
234 ASSERT_FALSE(root.get()); | |
235 | |
236 // Not a open comment token. | |
237 root.reset(base::JSONReader::Read("/ * * / [1]")); | |
238 ASSERT_FALSE(root.get()); | |
239 } | |
240 | |
241 class JSONFileValueSerializerTest : public testing::Test { | 20 class JSONFileValueSerializerTest : public testing::Test { |
242 protected: | 21 protected: |
243 virtual void SetUp() { | 22 virtual void SetUp() OVERRIDE { |
244 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 23 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
245 } | 24 } |
246 | 25 |
247 base::ScopedTempDir temp_dir_; | 26 base::ScopedTempDir temp_dir_; |
248 }; | 27 }; |
249 | 28 |
250 TEST_F(JSONFileValueSerializerTest, Roundtrip) { | 29 TEST_F(JSONFileValueSerializerTest, Roundtrip) { |
251 base::FilePath original_file_path; | 30 base::FilePath original_file_path; |
252 ASSERT_TRUE( | 31 ASSERT_TRUE( |
253 PathService::Get(chrome::DIR_TEST_DATA, &original_file_path)); | 32 PathService::Get(chrome::DIR_TEST_DATA, &original_file_path)); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 base::FilePath source_file_path; | 109 base::FilePath source_file_path; |
331 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_file_path)); | 110 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_file_path)); |
332 source_file_path = source_file_path.Append( | 111 source_file_path = source_file_path.Append( |
333 FILE_PATH_LITERAL("serializer_test_nowhitespace.js")); | 112 FILE_PATH_LITERAL("serializer_test_nowhitespace.js")); |
334 ASSERT_TRUE(file_util::PathExists(source_file_path)); | 113 ASSERT_TRUE(file_util::PathExists(source_file_path)); |
335 JSONFileValueSerializer serializer(source_file_path); | 114 JSONFileValueSerializer serializer(source_file_path); |
336 scoped_ptr<Value> root; | 115 scoped_ptr<Value> root; |
337 root.reset(serializer.Deserialize(NULL, NULL)); | 116 root.reset(serializer.Deserialize(NULL, NULL)); |
338 ASSERT_TRUE(root.get()); | 117 ASSERT_TRUE(root.get()); |
339 } | 118 } |
OLD | NEW |