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 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
7 #include "base/json_reader.h" | 7 #include "base/json_reader.h" |
8 #include "base/json_writer.h" | 8 #include "base/json_writer.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 std::wstring all_chars; | 66 std::wstring all_chars; |
67 for (int i = 1; i < 256; ++i) { | 67 for (int i = 1; i < 256; ++i) { |
68 all_chars += static_cast<wchar_t>(i); | 68 all_chars += static_cast<wchar_t>(i); |
69 } | 69 } |
70 // Generated in in Firefox using the following js (with an extra backslash for | 70 // Generated in in Firefox using the following js (with an extra backslash for |
71 // double quote): | 71 // double quote): |
72 // var s = ''; | 72 // var s = ''; |
73 // for (var i = 1; i < 256; ++i) { s += String.fromCharCode(i); } | 73 // for (var i = 1; i < 256; ++i) { s += String.fromCharCode(i); } |
74 // uneval(s).replace(/\\/g, "\\\\"); | 74 // uneval(s).replace(/\\/g, "\\\\"); |
75 std::string all_chars_expected = | 75 std::string all_chars_expected = |
76 "\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\b\\t\\n\\v\\f\\r\\x0E\\x0F\\x10" | 76 "\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007\\b\\t\\n\\u000B\\f\\r" |
77 "\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1A\\x1B\\x1C\\x1D\\x1E" | 77 "\\u000E\\u000F\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017" |
78 "\\x1F !\\\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\" | 78 "\\u0018\\u0019\\u001A\\u001B\\u001C\\u001D\\u001E" |
79 "\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\\x7F\\x80\\x81\\x82\\x83\\x84\\x85" | 79 "\\u001F !\\\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\" |
80 "\\x86\\x87\\x88\\x89\\x8A\\x8B\\x8C\\x8D\\x8E\\x8F\\x90\\x91\\x92\\x93" | 80 "\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\\u007F\\u0080\\u0081\\u0082\\u0083" |
81 "\\x94\\x95\\x96\\x97\\x98\\x99\\x9A\\x9B\\x9C\\x9D\\x9E\\x9F\\xA0\\xA1" | 81 "\\u0084\\u0085\\u0086\\u0087\\u0088\\u0089\\u008A\\u008B\\u008C\\u008D" |
82 "\\xA2\\xA3\\xA4\\xA5\\xA6\\xA7\\xA8\\xA9\\xAA\\xAB\\xAC\\xAD\\xAE\\xAF" | 82 "\\u008E\\u008F\\u0090\\u0091\\u0092\\u0093\\u0094\\u0095\\u0096\\u0097" |
83 "\\xB0\\xB1\\xB2\\xB3\\xB4\\xB5\\xB6\\xB7\\xB8\\xB9\\xBA\\xBB\\xBC\\xBD" | 83 "\\u0098\\u0099\\u009A\\u009B\\u009C\\u009D\\u009E\\u009F\\u00A0\\u00A1" |
84 "\\xBE\\xBF\\xC0\\xC1\\xC2\\xC3\\xC4\\xC5\\xC6\\xC7\\xC8\\xC9\\xCA\\xCB" | 84 "\\u00A2\\u00A3\\u00A4\\u00A5\\u00A6\\u00A7\\u00A8\\u00A9\\u00AA\\u00AB" |
85 "\\xCC\\xCD\\xCE\\xCF\\xD0\\xD1\\xD2\\xD3\\xD4\\xD5\\xD6\\xD7\\xD8\\xD9" | 85 "\\u00AC\\u00AD\\u00AE\\u00AF\\u00B0\\u00B1\\u00B2\\u00B3\\u00B4\\u00B5" |
86 "\\xDA\\xDB\\xDC\\xDD\\xDE\\xDF\\xE0\\xE1\\xE2\\xE3\\xE4\\xE5\\xE6\\xE7" | 86 "\\u00B6\\u00B7\\u00B8\\u00B9\\u00BA\\u00BB\\u00BC\\u00BD\\u00BE\\u00BF" |
87 "\\xE8\\xE9\\xEA\\xEB\\xEC\\xED\\xEE\\xEF\\xF0\\xF1\\xF2\\xF3\\xF4\\xF5" | 87 "\\u00C0\\u00C1\\u00C2\\u00C3\\u00C4\\u00C5\\u00C6\\u00C7\\u00C8\\u00C9" |
88 "\\xF6\\xF7\\xF8\\xF9\\xFA\\xFB\\xFC\\xFD\\xFE\\xFF"; | 88 "\\u00CA\\u00CB\\u00CC\\u00CD\\u00CE\\u00CF\\u00D0\\u00D1\\u00D2\\u00D3" |
| 89 "\\u00D4\\u00D5\\u00D6\\u00D7\\u00D8\\u00D9\\u00DA\\u00DB\\u00DC\\u00DD" |
| 90 "\\u00DE\\u00DF\\u00E0\\u00E1\\u00E2\\u00E3\\u00E4\\u00E5\\u00E6\\u00E7" |
| 91 "\\u00E8\\u00E9\\u00EA\\u00EB\\u00EC\\u00ED\\u00EE\\u00EF\\u00F0\\u00F1" |
| 92 "\\u00F2\\u00F3\\u00F4\\u00F5\\u00F6\\u00F7\\u00F8\\u00F9\\u00FA\\u00FB" |
| 93 "\\u00FC\\u00FD\\u00FE\\u00FF"; |
89 | 94 |
90 std::string expected_output = "{\"all_chars\":\"" + all_chars_expected + | 95 std::string expected_output = "{\"all_chars\":\"" + all_chars_expected + |
91 "\"}"; | 96 "\"}"; |
92 // Test JSONWriter interface | 97 // Test JSONWriter interface |
93 std::string output_js; | 98 std::string output_js; |
94 DictionaryValue valueRoot; | 99 DictionaryValue valueRoot; |
95 valueRoot.SetString(L"all_chars", all_chars); | 100 valueRoot.SetString(L"all_chars", all_chars); |
96 JSONWriter::Write(&valueRoot, false, &output_js); | 101 JSONWriter::Write(&valueRoot, false, &output_js); |
97 ASSERT_EQ(expected_output, output_js); | 102 ASSERT_EQ(expected_output, output_js); |
98 | 103 |
(...skipping 26 matching lines...) Expand all Loading... |
125 ASSERT_TRUE(dict_root->GetString(L"web", &web_value)); | 130 ASSERT_TRUE(dict_root->GetString(L"web", &web_value)); |
126 ASSERT_EQ(test, web_value); | 131 ASSERT_EQ(test, web_value); |
127 } | 132 } |
128 | 133 |
129 TEST(JSONValueSerializerTest, HexStrings) { | 134 TEST(JSONValueSerializerTest, HexStrings) { |
130 // hex string json -> escaped ascii text | 135 // hex string json -> escaped ascii text |
131 DictionaryValue root; | 136 DictionaryValue root; |
132 std::wstring test(L"\x01\x02"); | 137 std::wstring test(L"\x01\x02"); |
133 root.SetString(L"test", test); | 138 root.SetString(L"test", test); |
134 | 139 |
135 std::string expected = "{\"test\":\"\\x01\\x02\"}"; | 140 std::string expected = "{\"test\":\"\\u0001\\u0002\"}"; |
136 | 141 |
137 std::string actual; | 142 std::string actual; |
138 JSONStringValueSerializer serializer(&actual); | 143 JSONStringValueSerializer serializer(&actual); |
139 ASSERT_TRUE(serializer.Serialize(root)); | 144 ASSERT_TRUE(serializer.Serialize(root)); |
140 ASSERT_EQ(expected, actual); | 145 ASSERT_EQ(expected, actual); |
141 | 146 |
142 // escaped ascii text -> json | 147 // escaped ascii text -> json |
143 JSONStringValueSerializer deserializer(expected); | 148 JSONStringValueSerializer deserializer(expected); |
144 scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL)); | 149 scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL)); |
145 ASSERT_TRUE(deserial_root.get()); | 150 ASSERT_TRUE(deserial_root.get()); |
146 DictionaryValue* dict_root = | 151 DictionaryValue* dict_root = |
147 static_cast<DictionaryValue*>(deserial_root.get()); | 152 static_cast<DictionaryValue*>(deserial_root.get()); |
148 std::wstring test_value; | 153 std::wstring test_value; |
149 ASSERT_TRUE(dict_root->GetString(L"test", &test_value)); | 154 ASSERT_TRUE(dict_root->GetString(L"test", &test_value)); |
150 ASSERT_EQ(test, test_value); | 155 ASSERT_EQ(test, test_value); |
151 | 156 |
152 // Test converting escaped regular chars | 157 // Test converting escaped regular chars |
153 std::string escaped_chars = "{\"test\":\"\\x67\\x6f\"}"; | 158 std::string escaped_chars = "{\"test\":\"\\u0067\\u006f\"}"; |
154 JSONStringValueSerializer deserializer2(escaped_chars); | 159 JSONStringValueSerializer deserializer2(escaped_chars); |
155 deserial_root.reset(deserializer2.Deserialize(NULL)); | 160 deserial_root.reset(deserializer2.Deserialize(NULL)); |
156 ASSERT_TRUE(deserial_root.get()); | 161 ASSERT_TRUE(deserial_root.get()); |
157 dict_root = static_cast<DictionaryValue*>(deserial_root.get()); | 162 dict_root = static_cast<DictionaryValue*>(deserial_root.get()); |
158 ASSERT_TRUE(dict_root->GetString(L"test", &test_value)); | 163 ASSERT_TRUE(dict_root->GetString(L"test", &test_value)); |
159 ASSERT_EQ(std::wstring(L"go"), test_value); | 164 ASSERT_EQ(std::wstring(L"go"), test_value); |
160 } | 165 } |
161 | 166 |
162 TEST(JSONValueSerializerTest, AllowTrailingComma) { | 167 TEST(JSONValueSerializerTest, AllowTrailingComma) { |
163 scoped_ptr<Value> root; | 168 scoped_ptr<Value> root; |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
324 FilePath source_file_path; | 329 FilePath source_file_path; |
325 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_file_path)); | 330 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_file_path)); |
326 source_file_path = source_file_path.Append( | 331 source_file_path = source_file_path.Append( |
327 FILE_PATH_LITERAL("serializer_test_nowhitespace.js")); | 332 FILE_PATH_LITERAL("serializer_test_nowhitespace.js")); |
328 ASSERT_TRUE(file_util::PathExists(source_file_path)); | 333 ASSERT_TRUE(file_util::PathExists(source_file_path)); |
329 JSONFileValueSerializer serializer(source_file_path); | 334 JSONFileValueSerializer serializer(source_file_path); |
330 scoped_ptr<Value> root; | 335 scoped_ptr<Value> root; |
331 root.reset(serializer.Deserialize(NULL)); | 336 root.reset(serializer.Deserialize(NULL)); |
332 ASSERT_TRUE(root.get()); | 337 ASSERT_TRUE(root.get()); |
333 } | 338 } |
OLD | NEW |