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 <string> | 5 #include <string> |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
9 #include "base/json/json_file_value_serializer.h" | 9 #include "base/json/json_file_value_serializer.h" |
10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
11 #include "base/json/json_string_value_serializer.h" | 11 #include "base/json/json_string_value_serializer.h" |
| 12 #include "base/json/json_writer.h" |
12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
13 #include "base/string_util.h" | 14 #include "base/string_util.h" |
| 15 #include "base/utf_string_conversions.h" |
14 #include "base/values.h" | 16 #include "base/values.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
16 | 18 |
17 namespace base { | 19 namespace base { |
18 | 20 |
19 namespace { | 21 namespace { |
20 | 22 |
21 // Some proper JSON to test with: | 23 // Some proper JSON to test with: |
22 const char kProperJSON[] = | 24 const char kProperJSON[] = |
23 "{\n" | 25 "{\n" |
(...skipping 25 matching lines...) Expand all Loading... |
49 JSONStringValueSerializer str_serializer(&serialized_json); | 51 JSONStringValueSerializer str_serializer(&serialized_json); |
50 str_serializer.set_pretty_print(true); | 52 str_serializer.set_pretty_print(true); |
51 ASSERT_TRUE(str_serializer.Serialize(value)); | 53 ASSERT_TRUE(str_serializer.Serialize(value)); |
52 // Unify line endings between platforms. | 54 // Unify line endings between platforms. |
53 ReplaceSubstringsAfterOffset(&serialized_json, 0, | 55 ReplaceSubstringsAfterOffset(&serialized_json, 0, |
54 kWinLineEnds, kLinuxLineEnds); | 56 kWinLineEnds, kLinuxLineEnds); |
55 // Now compare the input with the output. | 57 // Now compare the input with the output. |
56 ASSERT_EQ(kProperJSON, serialized_json); | 58 ASSERT_EQ(kProperJSON, serialized_json); |
57 } | 59 } |
58 | 60 |
| 61 void ValidateJsonList(const std::string& json) { |
| 62 scoped_ptr<Value> root(JSONReader::Read(json)); |
| 63 ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST)); |
| 64 ListValue* list = static_cast<ListValue*>(root.get()); |
| 65 ASSERT_EQ(1U, list->GetSize()); |
| 66 Value* elt = NULL; |
| 67 ASSERT_TRUE(list->Get(0, &elt)); |
| 68 int value = 0; |
| 69 ASSERT_TRUE(elt && elt->GetAsInteger(&value)); |
| 70 ASSERT_EQ(1, value); |
| 71 } |
| 72 |
59 // Test proper JSON [de]serialization from string is working. | 73 // Test proper JSON [de]serialization from string is working. |
60 TEST(JSONValueSerializerTest, ReadProperJSONFromString) { | 74 TEST(JSONValueSerializerTest, ReadProperJSONFromString) { |
61 // Try to deserialize it through the serializer. | 75 // Try to deserialize it through the serializer. |
62 std::string proper_json(kProperJSON); | 76 std::string proper_json(kProperJSON); |
63 JSONStringValueSerializer str_deserializer(proper_json); | 77 JSONStringValueSerializer str_deserializer(proper_json); |
64 | 78 |
65 int error_code = 0; | 79 int error_code = 0; |
66 std::string error_message; | 80 std::string error_message; |
67 scoped_ptr<Value> value( | 81 scoped_ptr<Value> value( |
68 str_deserializer.Deserialize(&error_code, &error_message)); | 82 str_deserializer.Deserialize(&error_code, &error_message)); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 ASSERT_FALSE(error_message.empty()); | 157 ASSERT_FALSE(error_message.empty()); |
144 // Now the flag is set and it must pass. | 158 // Now the flag is set and it must pass. |
145 file_deserializer.set_allow_trailing_comma(true); | 159 file_deserializer.set_allow_trailing_comma(true); |
146 value.reset(file_deserializer.Deserialize(&error_code, &error_message)); | 160 value.reset(file_deserializer.Deserialize(&error_code, &error_message)); |
147 ASSERT_TRUE(value.get()); | 161 ASSERT_TRUE(value.get()); |
148 ASSERT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code); | 162 ASSERT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code); |
149 // Verify if the same JSON is still there. | 163 // Verify if the same JSON is still there. |
150 CheckJSONIsStillTheSame(*value); | 164 CheckJSONIsStillTheSame(*value); |
151 } | 165 } |
152 | 166 |
| 167 TEST(JSONValueSerializerTest, Roundtrip) { |
| 168 const std::string original_serialization = |
| 169 "{\"bool\":true,\"double\":3.14,\"int\":42,\"list\":[1,2],\"null\":null}"; |
| 170 JSONStringValueSerializer serializer(original_serialization); |
| 171 scoped_ptr<Value> root(serializer.Deserialize(NULL, NULL)); |
| 172 ASSERT_TRUE(root.get()); |
| 173 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); |
| 174 |
| 175 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get()); |
| 176 |
| 177 Value* null_value = NULL; |
| 178 ASSERT_TRUE(root_dict->Get("null", &null_value)); |
| 179 ASSERT_TRUE(null_value); |
| 180 ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL)); |
| 181 |
| 182 bool bool_value = false; |
| 183 ASSERT_TRUE(root_dict->GetBoolean("bool", &bool_value)); |
| 184 ASSERT_TRUE(bool_value); |
| 185 |
| 186 int int_value = 0; |
| 187 ASSERT_TRUE(root_dict->GetInteger("int", &int_value)); |
| 188 ASSERT_EQ(42, int_value); |
| 189 |
| 190 double double_value = 0.0; |
| 191 ASSERT_TRUE(root_dict->GetDouble("double", &double_value)); |
| 192 ASSERT_DOUBLE_EQ(3.14, double_value); |
| 193 |
| 194 // We shouldn't be able to write using this serializer, since it was |
| 195 // initialized with a const string. |
| 196 ASSERT_FALSE(serializer.Serialize(*root_dict)); |
| 197 |
| 198 std::string test_serialization = ""; |
| 199 JSONStringValueSerializer mutable_serializer(&test_serialization); |
| 200 ASSERT_TRUE(mutable_serializer.Serialize(*root_dict)); |
| 201 ASSERT_EQ(original_serialization, test_serialization); |
| 202 |
| 203 mutable_serializer.set_pretty_print(true); |
| 204 ASSERT_TRUE(mutable_serializer.Serialize(*root_dict)); |
| 205 // JSON output uses a different newline style on Windows than on other |
| 206 // platforms. |
| 207 #if defined(OS_WIN) |
| 208 #define JSON_NEWLINE "\r\n" |
| 209 #else |
| 210 #define JSON_NEWLINE "\n" |
| 211 #endif |
| 212 const std::string pretty_serialization = |
| 213 "{" JSON_NEWLINE |
| 214 " \"bool\": true," JSON_NEWLINE |
| 215 " \"double\": 3.14," JSON_NEWLINE |
| 216 " \"int\": 42," JSON_NEWLINE |
| 217 " \"list\": [ 1, 2 ]," JSON_NEWLINE |
| 218 " \"null\": null" JSON_NEWLINE |
| 219 "}" JSON_NEWLINE; |
| 220 #undef JSON_NEWLINE |
| 221 ASSERT_EQ(pretty_serialization, test_serialization); |
| 222 } |
| 223 |
| 224 TEST(JSONValueSerializerTest, StringEscape) { |
| 225 string16 all_chars; |
| 226 for (int i = 1; i < 256; ++i) { |
| 227 all_chars += static_cast<char16>(i); |
| 228 } |
| 229 // Generated in in Firefox using the following js (with an extra backslash for |
| 230 // double quote): |
| 231 // var s = ''; |
| 232 // for (var i = 1; i < 256; ++i) { s += String.fromCharCode(i); } |
| 233 // uneval(s).replace(/\\/g, "\\\\"); |
| 234 std::string all_chars_expected = |
| 235 "\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007\\b\\t\\n\\u000B\\f\\r" |
| 236 "\\u000E\\u000F\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017" |
| 237 "\\u0018\\u0019\\u001A\\u001B\\u001C\\u001D\\u001E\\u001F !\\\"" |
| 238 "#$%&'()*+,-./0123456789:;\\u003C=\\u003E?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\" |
| 239 "\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\\u007F\\u0080\\u0081\\u0082\\u0083" |
| 240 "\\u0084\\u0085\\u0086\\u0087\\u0088\\u0089\\u008A\\u008B\\u008C\\u008D" |
| 241 "\\u008E\\u008F\\u0090\\u0091\\u0092\\u0093\\u0094\\u0095\\u0096\\u0097" |
| 242 "\\u0098\\u0099\\u009A\\u009B\\u009C\\u009D\\u009E\\u009F\\u00A0\\u00A1" |
| 243 "\\u00A2\\u00A3\\u00A4\\u00A5\\u00A6\\u00A7\\u00A8\\u00A9\\u00AA\\u00AB" |
| 244 "\\u00AC\\u00AD\\u00AE\\u00AF\\u00B0\\u00B1\\u00B2\\u00B3\\u00B4\\u00B5" |
| 245 "\\u00B6\\u00B7\\u00B8\\u00B9\\u00BA\\u00BB\\u00BC\\u00BD\\u00BE\\u00BF" |
| 246 "\\u00C0\\u00C1\\u00C2\\u00C3\\u00C4\\u00C5\\u00C6\\u00C7\\u00C8\\u00C9" |
| 247 "\\u00CA\\u00CB\\u00CC\\u00CD\\u00CE\\u00CF\\u00D0\\u00D1\\u00D2\\u00D3" |
| 248 "\\u00D4\\u00D5\\u00D6\\u00D7\\u00D8\\u00D9\\u00DA\\u00DB\\u00DC\\u00DD" |
| 249 "\\u00DE\\u00DF\\u00E0\\u00E1\\u00E2\\u00E3\\u00E4\\u00E5\\u00E6\\u00E7" |
| 250 "\\u00E8\\u00E9\\u00EA\\u00EB\\u00EC\\u00ED\\u00EE\\u00EF\\u00F0\\u00F1" |
| 251 "\\u00F2\\u00F3\\u00F4\\u00F5\\u00F6\\u00F7\\u00F8\\u00F9\\u00FA\\u00FB" |
| 252 "\\u00FC\\u00FD\\u00FE\\u00FF"; |
| 253 |
| 254 std::string expected_output = "{\"all_chars\":\"" + all_chars_expected + |
| 255 "\"}"; |
| 256 // Test JSONWriter interface |
| 257 std::string output_js; |
| 258 DictionaryValue valueRoot; |
| 259 valueRoot.SetString("all_chars", all_chars); |
| 260 JSONWriter::Write(&valueRoot, &output_js); |
| 261 ASSERT_EQ(expected_output, output_js); |
| 262 |
| 263 // Test JSONValueSerializer interface (uses JSONWriter). |
| 264 JSONStringValueSerializer serializer(&output_js); |
| 265 ASSERT_TRUE(serializer.Serialize(valueRoot)); |
| 266 ASSERT_EQ(expected_output, output_js); |
| 267 } |
| 268 |
| 269 TEST(JSONValueSerializerTest, UnicodeStrings) { |
| 270 // unicode string json -> escaped ascii text |
| 271 DictionaryValue root; |
| 272 string16 test(WideToUTF16(L"\x7F51\x9875")); |
| 273 root.SetString("web", test); |
| 274 |
| 275 std::string expected = "{\"web\":\"\\u7F51\\u9875\"}"; |
| 276 |
| 277 std::string actual; |
| 278 JSONStringValueSerializer serializer(&actual); |
| 279 ASSERT_TRUE(serializer.Serialize(root)); |
| 280 ASSERT_EQ(expected, actual); |
| 281 |
| 282 // escaped ascii text -> json |
| 283 JSONStringValueSerializer deserializer(expected); |
| 284 scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL, NULL)); |
| 285 ASSERT_TRUE(deserial_root.get()); |
| 286 DictionaryValue* dict_root = |
| 287 static_cast<DictionaryValue*>(deserial_root.get()); |
| 288 string16 web_value; |
| 289 ASSERT_TRUE(dict_root->GetString("web", &web_value)); |
| 290 ASSERT_EQ(test, web_value); |
| 291 } |
| 292 |
| 293 TEST(JSONValueSerializerTest, HexStrings) { |
| 294 // hex string json -> escaped ascii text |
| 295 DictionaryValue root; |
| 296 string16 test(WideToUTF16(L"\x01\x02")); |
| 297 root.SetString("test", test); |
| 298 |
| 299 std::string expected = "{\"test\":\"\\u0001\\u0002\"}"; |
| 300 |
| 301 std::string actual; |
| 302 JSONStringValueSerializer serializer(&actual); |
| 303 ASSERT_TRUE(serializer.Serialize(root)); |
| 304 ASSERT_EQ(expected, actual); |
| 305 |
| 306 // escaped ascii text -> json |
| 307 JSONStringValueSerializer deserializer(expected); |
| 308 scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL, NULL)); |
| 309 ASSERT_TRUE(deserial_root.get()); |
| 310 DictionaryValue* dict_root = |
| 311 static_cast<DictionaryValue*>(deserial_root.get()); |
| 312 string16 test_value; |
| 313 ASSERT_TRUE(dict_root->GetString("test", &test_value)); |
| 314 ASSERT_EQ(test, test_value); |
| 315 |
| 316 // Test converting escaped regular chars |
| 317 std::string escaped_chars = "{\"test\":\"\\u0067\\u006f\"}"; |
| 318 JSONStringValueSerializer deserializer2(escaped_chars); |
| 319 deserial_root.reset(deserializer2.Deserialize(NULL, NULL)); |
| 320 ASSERT_TRUE(deserial_root.get()); |
| 321 dict_root = static_cast<DictionaryValue*>(deserial_root.get()); |
| 322 ASSERT_TRUE(dict_root->GetString("test", &test_value)); |
| 323 ASSERT_EQ(ASCIIToUTF16("go"), test_value); |
| 324 } |
| 325 |
| 326 TEST(JSONValueSerializerTest, AllowTrailingComma) { |
| 327 scoped_ptr<Value> root; |
| 328 scoped_ptr<Value> root_expected; |
| 329 std::string test_with_commas("{\"key\": [true,],}"); |
| 330 std::string test_no_commas("{\"key\": [true]}"); |
| 331 |
| 332 JSONStringValueSerializer serializer(test_with_commas); |
| 333 serializer.set_allow_trailing_comma(true); |
| 334 JSONStringValueSerializer serializer_expected(test_no_commas); |
| 335 root.reset(serializer.Deserialize(NULL, NULL)); |
| 336 ASSERT_TRUE(root.get()); |
| 337 root_expected.reset(serializer_expected.Deserialize(NULL, NULL)); |
| 338 ASSERT_TRUE(root_expected.get()); |
| 339 ASSERT_TRUE(root->Equals(root_expected.get())); |
| 340 } |
| 341 |
| 342 TEST(JSONValueSerializerTest, JSONReaderComments) { |
| 343 ValidateJsonList("[ // 2, 3, ignore me ] \n1 ]"); |
| 344 ValidateJsonList("[ /* 2, \n3, ignore me ]*/ \n1 ]"); |
| 345 ValidateJsonList("//header\n[ // 2, \n// 3, \n1 ]// footer"); |
| 346 ValidateJsonList("/*\n[ // 2, \n// 3, \n1 ]*/[1]"); |
| 347 ValidateJsonList("[ 1 /* one */ ] /* end */"); |
| 348 ValidateJsonList("[ 1 //// ,2\r\n ]"); |
| 349 |
| 350 scoped_ptr<Value> root; |
| 351 |
| 352 // It's ok to have a comment in a string. |
| 353 root.reset(JSONReader::Read("[\"// ok\\n /* foo */ \"]")); |
| 354 ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST)); |
| 355 ListValue* list = static_cast<ListValue*>(root.get()); |
| 356 ASSERT_EQ(1U, list->GetSize()); |
| 357 Value* elt = NULL; |
| 358 ASSERT_TRUE(list->Get(0, &elt)); |
| 359 std::string value; |
| 360 ASSERT_TRUE(elt && elt->GetAsString(&value)); |
| 361 ASSERT_EQ("// ok\n /* foo */ ", value); |
| 362 |
| 363 // You can't nest comments. |
| 364 root.reset(JSONReader::Read("/* /* inner */ outer */ [ 1 ]")); |
| 365 ASSERT_FALSE(root.get()); |
| 366 |
| 367 // Not a open comment token. |
| 368 root.reset(JSONReader::Read("/ * * / [1]")); |
| 369 ASSERT_FALSE(root.get()); |
| 370 } |
| 371 |
153 } // namespace | 372 } // namespace |
154 | 373 |
155 } // namespace base | 374 } // namespace base |
156 | |
OLD | NEW |