| 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/files/file_util.h" | 7 #include "base/files/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" | 
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 79   ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST)); | 79   ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST)); | 
| 80   ListValue* list = static_cast<ListValue*>(root.get()); | 80   ListValue* list = static_cast<ListValue*>(root.get()); | 
| 81   ASSERT_EQ(1U, list->GetSize()); | 81   ASSERT_EQ(1U, list->GetSize()); | 
| 82   Value* elt = NULL; | 82   Value* elt = NULL; | 
| 83   ASSERT_TRUE(list->Get(0, &elt)); | 83   ASSERT_TRUE(list->Get(0, &elt)); | 
| 84   int value = 0; | 84   int value = 0; | 
| 85   ASSERT_TRUE(elt && elt->GetAsInteger(&value)); | 85   ASSERT_TRUE(elt && elt->GetAsInteger(&value)); | 
| 86   ASSERT_EQ(1, value); | 86   ASSERT_EQ(1, value); | 
| 87 } | 87 } | 
| 88 | 88 | 
| 89 // Test proper JSON [de]serialization from string is working. | 89 // Test proper JSON deserialization from string is working. | 
| 90 TEST(JSONValueSerializerTest, ReadProperJSONFromString) { | 90 TEST(JSONValueDeserializerTest, ReadProperJSONFromString) { | 
| 91   // Try to deserialize it through the serializer. | 91   // Try to deserialize it through the serializer. | 
| 92   JSONStringValueSerializer str_deserializer(kProperJSON); | 92   JSONStringValueDeserializer str_deserializer(kProperJSON); | 
| 93 | 93 | 
| 94   int error_code = 0; | 94   int error_code = 0; | 
| 95   std::string error_message; | 95   std::string error_message; | 
| 96   scoped_ptr<Value> value( |  | 
| 97       str_deserializer.Deserialize(&error_code, &error_message)); |  | 
| 98   ASSERT_TRUE(value.get()); |  | 
| 99   ASSERT_EQ(0, error_code); |  | 
| 100   ASSERT_TRUE(error_message.empty()); |  | 
| 101   // Verify if the same JSON is still there. |  | 
| 102   CheckJSONIsStillTheSame(*value); |  | 
| 103 } |  | 
| 104 |  | 
| 105 // Test proper JSON deserialization from a string pointer is working. |  | 
| 106 TEST(JSONValueSerializerTest, ReadProperJSONFromStringPointer) { |  | 
| 107   // Try to deserialize a string pointer through the serializer. (This exercises |  | 
| 108   // a separate code path to passing a StringPiece.) |  | 
| 109   std::string proper_json(kProperJSON); |  | 
| 110   JSONStringValueSerializer str_deserializer(&proper_json); |  | 
| 111 |  | 
| 112   int error_code = 0; |  | 
| 113   std::string error_message; |  | 
| 114   scoped_ptr<Value> value( | 96   scoped_ptr<Value> value( | 
| 115       str_deserializer.Deserialize(&error_code, &error_message)); | 97       str_deserializer.Deserialize(&error_code, &error_message)); | 
| 116   ASSERT_TRUE(value.get()); | 98   ASSERT_TRUE(value.get()); | 
| 117   ASSERT_EQ(0, error_code); | 99   ASSERT_EQ(0, error_code); | 
| 118   ASSERT_TRUE(error_message.empty()); | 100   ASSERT_TRUE(error_message.empty()); | 
| 119   // Verify if the same JSON is still there. | 101   // Verify if the same JSON is still there. | 
| 120   CheckJSONIsStillTheSame(*value); | 102   CheckJSONIsStillTheSame(*value); | 
| 121 } | 103 } | 
| 122 | 104 | 
| 123 // Test proper JSON deserialization from a StringPiece substring. | 105 // Test proper JSON deserialization from a StringPiece substring. | 
| 124 TEST(JSONValueSerializerTest, ReadProperJSONFromStringPiece) { | 106 TEST(JSONValueDeserializerTest, ReadProperJSONFromStringPiece) { | 
| 125   // Create a StringPiece for the substring of kProperJSONPadded that matches | 107   // Create a StringPiece for the substring of kProperJSONPadded that matches | 
| 126   // kProperJSON. | 108   // kProperJSON. | 
| 127   base::StringPiece proper_json(kProperJSONPadded); | 109   base::StringPiece proper_json(kProperJSONPadded); | 
| 128   proper_json = proper_json.substr(5, proper_json.length() - 10); | 110   proper_json = proper_json.substr(5, proper_json.length() - 10); | 
| 129   JSONStringValueSerializer str_deserializer(proper_json); | 111   JSONStringValueDeserializer str_deserializer(proper_json); | 
| 130 | 112 | 
| 131   int error_code = 0; | 113   int error_code = 0; | 
| 132   std::string error_message; | 114   std::string error_message; | 
| 133   scoped_ptr<Value> value( | 115   scoped_ptr<Value> value( | 
| 134       str_deserializer.Deserialize(&error_code, &error_message)); | 116       str_deserializer.Deserialize(&error_code, &error_message)); | 
| 135   ASSERT_TRUE(value.get()); | 117   ASSERT_TRUE(value.get()); | 
| 136   ASSERT_EQ(0, error_code); | 118   ASSERT_EQ(0, error_code); | 
| 137   ASSERT_TRUE(error_message.empty()); | 119   ASSERT_TRUE(error_message.empty()); | 
| 138   // Verify if the same JSON is still there. | 120   // Verify if the same JSON is still there. | 
| 139   CheckJSONIsStillTheSame(*value); | 121   CheckJSONIsStillTheSame(*value); | 
| 140 } | 122 } | 
| 141 | 123 | 
| 142 // Test that trialing commas are only properly deserialized from string when | 124 // Test that trialing commas are only properly deserialized from string when | 
| 143 // the proper flag for that is set. | 125 // the proper flag for that is set. | 
| 144 TEST(JSONValueSerializerTest, ReadJSONWithTrailingCommasFromString) { | 126 TEST(JSONValueDeserializerTest, ReadJSONWithTrailingCommasFromString) { | 
| 145   // Try to deserialize it through the serializer. | 127   // Try to deserialize it through the serializer. | 
| 146   JSONStringValueSerializer str_deserializer(kProperJSONWithCommas); | 128   JSONStringValueDeserializer str_deserializer(kProperJSONWithCommas); | 
| 147 | 129 | 
| 148   int error_code = 0; | 130   int error_code = 0; | 
| 149   std::string error_message; | 131   std::string error_message; | 
| 150   scoped_ptr<Value> value( | 132   scoped_ptr<Value> value( | 
| 151       str_deserializer.Deserialize(&error_code, &error_message)); | 133       str_deserializer.Deserialize(&error_code, &error_message)); | 
| 152   ASSERT_FALSE(value.get()); | 134   ASSERT_FALSE(value.get()); | 
| 153   ASSERT_NE(0, error_code); | 135   ASSERT_NE(0, error_code); | 
| 154   ASSERT_FALSE(error_message.empty()); | 136   ASSERT_FALSE(error_message.empty()); | 
| 155   // Now the flag is set and it must pass. | 137   // Now the flag is set and it must pass. | 
| 156   str_deserializer.set_allow_trailing_comma(true); | 138   str_deserializer.set_allow_trailing_comma(true); | 
| 157   value.reset(str_deserializer.Deserialize(&error_code, &error_message)); | 139   value.reset(str_deserializer.Deserialize(&error_code, &error_message)); | 
| 158   ASSERT_TRUE(value.get()); | 140   ASSERT_TRUE(value.get()); | 
| 159   ASSERT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code); | 141   ASSERT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code); | 
| 160   // Verify if the same JSON is still there. | 142   // Verify if the same JSON is still there. | 
| 161   CheckJSONIsStillTheSame(*value); | 143   CheckJSONIsStillTheSame(*value); | 
| 162 } | 144 } | 
| 163 | 145 | 
| 164 // Test proper JSON [de]serialization from file is working. | 146 // Test proper JSON deserialization from file is working. | 
| 165 TEST(JSONValueSerializerTest, ReadProperJSONFromFile) { | 147 TEST(JSONValueDeserializerTest, ReadProperJSONFromFile) { | 
| 166   ScopedTempDir tempdir; | 148   ScopedTempDir tempdir; | 
| 167   ASSERT_TRUE(tempdir.CreateUniqueTempDir()); | 149   ASSERT_TRUE(tempdir.CreateUniqueTempDir()); | 
| 168   // Write it down in the file. | 150   // Write it down in the file. | 
| 169   FilePath temp_file(tempdir.path().AppendASCII("test.json")); | 151   FilePath temp_file(tempdir.path().AppendASCII("test.json")); | 
| 170   ASSERT_EQ(static_cast<int>(strlen(kProperJSON)), | 152   ASSERT_EQ(static_cast<int>(strlen(kProperJSON)), | 
| 171             WriteFile(temp_file, kProperJSON, strlen(kProperJSON))); | 153             WriteFile(temp_file, kProperJSON, strlen(kProperJSON))); | 
| 172 | 154 | 
| 173   // Try to deserialize it through the serializer. | 155   // Try to deserialize it through the serializer. | 
| 174   JSONFileValueSerializer file_deserializer(temp_file); | 156   JSONFileValueDeserializer file_deserializer(temp_file); | 
| 175 | 157 | 
| 176   int error_code = 0; | 158   int error_code = 0; | 
| 177   std::string error_message; | 159   std::string error_message; | 
| 178   scoped_ptr<Value> value( | 160   scoped_ptr<Value> value( | 
| 179       file_deserializer.Deserialize(&error_code, &error_message)); | 161       file_deserializer.Deserialize(&error_code, &error_message)); | 
| 180   ASSERT_TRUE(value.get()); | 162   ASSERT_TRUE(value.get()); | 
| 181   ASSERT_EQ(0, error_code); | 163   ASSERT_EQ(0, error_code); | 
| 182   ASSERT_TRUE(error_message.empty()); | 164   ASSERT_TRUE(error_message.empty()); | 
| 183   // Verify if the same JSON is still there. | 165   // Verify if the same JSON is still there. | 
| 184   CheckJSONIsStillTheSame(*value); | 166   CheckJSONIsStillTheSame(*value); | 
| 185 } | 167 } | 
| 186 | 168 | 
| 187 // Test that trialing commas are only properly deserialized from file when | 169 // Test that trialing commas are only properly deserialized from file when | 
| 188 // the proper flag for that is set. | 170 // the proper flag for that is set. | 
| 189 TEST(JSONValueSerializerTest, ReadJSONWithCommasFromFile) { | 171 TEST(JSONValueDeserializerTest, ReadJSONWithCommasFromFile) { | 
| 190   ScopedTempDir tempdir; | 172   ScopedTempDir tempdir; | 
| 191   ASSERT_TRUE(tempdir.CreateUniqueTempDir()); | 173   ASSERT_TRUE(tempdir.CreateUniqueTempDir()); | 
| 192   // Write it down in the file. | 174   // Write it down in the file. | 
| 193   FilePath temp_file(tempdir.path().AppendASCII("test.json")); | 175   FilePath temp_file(tempdir.path().AppendASCII("test.json")); | 
| 194   ASSERT_EQ(static_cast<int>(strlen(kProperJSONWithCommas)), | 176   ASSERT_EQ(static_cast<int>(strlen(kProperJSONWithCommas)), | 
| 195             WriteFile(temp_file, kProperJSONWithCommas, | 177             WriteFile(temp_file, kProperJSONWithCommas, | 
| 196                       strlen(kProperJSONWithCommas))); | 178                       strlen(kProperJSONWithCommas))); | 
| 197 | 179 | 
| 198   // Try to deserialize it through the serializer. | 180   // Try to deserialize it through the serializer. | 
| 199   JSONFileValueSerializer file_deserializer(temp_file); | 181   JSONFileValueDeserializer file_deserializer(temp_file); | 
| 200   // This must fail without the proper flag. | 182   // This must fail without the proper flag. | 
| 201   int error_code = 0; | 183   int error_code = 0; | 
| 202   std::string error_message; | 184   std::string error_message; | 
| 203   scoped_ptr<Value> value( | 185   scoped_ptr<Value> value( | 
| 204       file_deserializer.Deserialize(&error_code, &error_message)); | 186       file_deserializer.Deserialize(&error_code, &error_message)); | 
| 205   ASSERT_FALSE(value.get()); | 187   ASSERT_FALSE(value.get()); | 
| 206   ASSERT_NE(0, error_code); | 188   ASSERT_NE(0, error_code); | 
| 207   ASSERT_FALSE(error_message.empty()); | 189   ASSERT_FALSE(error_message.empty()); | 
| 208   // Now the flag is set and it must pass. | 190   // Now the flag is set and it must pass. | 
| 209   file_deserializer.set_allow_trailing_comma(true); | 191   file_deserializer.set_allow_trailing_comma(true); | 
| 210   value.reset(file_deserializer.Deserialize(&error_code, &error_message)); | 192   value.reset(file_deserializer.Deserialize(&error_code, &error_message)); | 
| 211   ASSERT_TRUE(value.get()); | 193   ASSERT_TRUE(value.get()); | 
| 212   ASSERT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code); | 194   ASSERT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code); | 
| 213   // Verify if the same JSON is still there. | 195   // Verify if the same JSON is still there. | 
| 214   CheckJSONIsStillTheSame(*value); | 196   CheckJSONIsStillTheSame(*value); | 
| 215 } | 197 } | 
| 216 | 198 | 
|  | 199 TEST(JSONValueDeserializerTest, AllowTrailingComma) { | 
|  | 200   scoped_ptr<Value> root; | 
|  | 201   scoped_ptr<Value> root_expected; | 
|  | 202   static const char kTestWithCommas[] = "{\"key\": [true,],}"; | 
|  | 203   static const char kTestNoCommas[] = "{\"key\": [true]}"; | 
|  | 204 | 
|  | 205   JSONStringValueDeserializer deserializer(kTestWithCommas); | 
|  | 206   deserializer.set_allow_trailing_comma(true); | 
|  | 207   JSONStringValueDeserializer deserializer_expected(kTestNoCommas); | 
|  | 208   root.reset(deserializer.Deserialize(NULL, NULL)); | 
|  | 209   ASSERT_TRUE(root.get()); | 
|  | 210   root_expected.reset(deserializer_expected.Deserialize(NULL, NULL)); | 
|  | 211   ASSERT_TRUE(root_expected.get()); | 
|  | 212   ASSERT_TRUE(root->Equals(root_expected.get())); | 
|  | 213 } | 
|  | 214 | 
| 217 TEST(JSONValueSerializerTest, Roundtrip) { | 215 TEST(JSONValueSerializerTest, Roundtrip) { | 
| 218   static const char kOriginalSerialization[] = | 216   static const char kOriginalSerialization[] = | 
| 219     "{\"bool\":true,\"double\":3.14,\"int\":42,\"list\":[1,2],\"null\":null}"; | 217     "{\"bool\":true,\"double\":3.14,\"int\":42,\"list\":[1,2],\"null\":null}"; | 
| 220   JSONStringValueSerializer serializer(kOriginalSerialization); | 218   JSONStringValueDeserializer deserializer(kOriginalSerialization); | 
| 221   scoped_ptr<Value> root(serializer.Deserialize(NULL, NULL)); | 219   scoped_ptr<Value> root(deserializer.Deserialize(NULL, NULL)); | 
| 222   ASSERT_TRUE(root.get()); | 220   ASSERT_TRUE(root.get()); | 
| 223   ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); | 221   ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); | 
| 224 | 222 | 
| 225   DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get()); | 223   DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get()); | 
| 226 | 224 | 
| 227   Value* null_value = NULL; | 225   Value* null_value = NULL; | 
| 228   ASSERT_TRUE(root_dict->Get("null", &null_value)); | 226   ASSERT_TRUE(root_dict->Get("null", &null_value)); | 
| 229   ASSERT_TRUE(null_value); | 227   ASSERT_TRUE(null_value); | 
| 230   ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL)); | 228   ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL)); | 
| 231 | 229 | 
| 232   bool bool_value = false; | 230   bool bool_value = false; | 
| 233   ASSERT_TRUE(root_dict->GetBoolean("bool", &bool_value)); | 231   ASSERT_TRUE(root_dict->GetBoolean("bool", &bool_value)); | 
| 234   ASSERT_TRUE(bool_value); | 232   ASSERT_TRUE(bool_value); | 
| 235 | 233 | 
| 236   int int_value = 0; | 234   int int_value = 0; | 
| 237   ASSERT_TRUE(root_dict->GetInteger("int", &int_value)); | 235   ASSERT_TRUE(root_dict->GetInteger("int", &int_value)); | 
| 238   ASSERT_EQ(42, int_value); | 236   ASSERT_EQ(42, int_value); | 
| 239 | 237 | 
| 240   double double_value = 0.0; | 238   double double_value = 0.0; | 
| 241   ASSERT_TRUE(root_dict->GetDouble("double", &double_value)); | 239   ASSERT_TRUE(root_dict->GetDouble("double", &double_value)); | 
| 242   ASSERT_DOUBLE_EQ(3.14, double_value); | 240   ASSERT_DOUBLE_EQ(3.14, double_value); | 
| 243 | 241 | 
| 244   // We shouldn't be able to write using this serializer, since it was |  | 
| 245   // initialized with a const string. |  | 
| 246   ASSERT_FALSE(serializer.Serialize(*root_dict)); |  | 
| 247 |  | 
| 248   std::string test_serialization; | 242   std::string test_serialization; | 
| 249   JSONStringValueSerializer mutable_serializer(&test_serialization); | 243   JSONStringValueSerializer mutable_serializer(&test_serialization); | 
| 250   ASSERT_TRUE(mutable_serializer.Serialize(*root_dict)); | 244   ASSERT_TRUE(mutable_serializer.Serialize(*root_dict)); | 
| 251   ASSERT_EQ(kOriginalSerialization, test_serialization); | 245   ASSERT_EQ(kOriginalSerialization, test_serialization); | 
| 252 | 246 | 
| 253   mutable_serializer.set_pretty_print(true); | 247   mutable_serializer.set_pretty_print(true); | 
| 254   ASSERT_TRUE(mutable_serializer.Serialize(*root_dict)); | 248   ASSERT_TRUE(mutable_serializer.Serialize(*root_dict)); | 
| 255   // JSON output uses a different newline style on Windows than on other | 249   // JSON output uses a different newline style on Windows than on other | 
| 256   // platforms. | 250   // platforms. | 
| 257 #if defined(OS_WIN) | 251 #if defined(OS_WIN) | 
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 324   root.SetString("web", test); | 318   root.SetString("web", test); | 
| 325 | 319 | 
| 326   static const char kExpected[] = "{\"web\":\"\xE7\xBD\x91\xE9\xA1\xB5\"}"; | 320   static const char kExpected[] = "{\"web\":\"\xE7\xBD\x91\xE9\xA1\xB5\"}"; | 
| 327 | 321 | 
| 328   std::string actual; | 322   std::string actual; | 
| 329   JSONStringValueSerializer serializer(&actual); | 323   JSONStringValueSerializer serializer(&actual); | 
| 330   ASSERT_TRUE(serializer.Serialize(root)); | 324   ASSERT_TRUE(serializer.Serialize(root)); | 
| 331   ASSERT_EQ(kExpected, actual); | 325   ASSERT_EQ(kExpected, actual); | 
| 332 | 326 | 
| 333   // escaped ascii text -> json | 327   // escaped ascii text -> json | 
| 334   JSONStringValueSerializer deserializer(kExpected); | 328   JSONStringValueDeserializer deserializer(kExpected); | 
| 335   scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL, NULL)); | 329   scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL, NULL)); | 
| 336   ASSERT_TRUE(deserial_root.get()); | 330   ASSERT_TRUE(deserial_root.get()); | 
| 337   DictionaryValue* dict_root = | 331   DictionaryValue* dict_root = | 
| 338       static_cast<DictionaryValue*>(deserial_root.get()); | 332       static_cast<DictionaryValue*>(deserial_root.get()); | 
| 339   string16 web_value; | 333   string16 web_value; | 
| 340   ASSERT_TRUE(dict_root->GetString("web", &web_value)); | 334   ASSERT_TRUE(dict_root->GetString("web", &web_value)); | 
| 341   ASSERT_EQ(test, web_value); | 335   ASSERT_EQ(test, web_value); | 
| 342 } | 336 } | 
| 343 | 337 | 
| 344 TEST(JSONValueSerializerTest, HexStrings) { | 338 TEST(JSONValueSerializerTest, HexStrings) { | 
| 345   // hex string json -> escaped ascii text | 339   // hex string json -> escaped ascii text | 
| 346   DictionaryValue root; | 340   DictionaryValue root; | 
| 347   string16 test(WideToUTF16(L"\x01\x02")); | 341   string16 test(WideToUTF16(L"\x01\x02")); | 
| 348   root.SetString("test", test); | 342   root.SetString("test", test); | 
| 349 | 343 | 
| 350   static const char kExpected[] = "{\"test\":\"\\u0001\\u0002\"}"; | 344   static const char kExpected[] = "{\"test\":\"\\u0001\\u0002\"}"; | 
| 351 | 345 | 
| 352   std::string actual; | 346   std::string actual; | 
| 353   JSONStringValueSerializer serializer(&actual); | 347   JSONStringValueSerializer serializer(&actual); | 
| 354   ASSERT_TRUE(serializer.Serialize(root)); | 348   ASSERT_TRUE(serializer.Serialize(root)); | 
| 355   ASSERT_EQ(kExpected, actual); | 349   ASSERT_EQ(kExpected, actual); | 
| 356 | 350 | 
| 357   // escaped ascii text -> json | 351   // escaped ascii text -> json | 
| 358   JSONStringValueSerializer deserializer(kExpected); | 352   JSONStringValueDeserializer deserializer(kExpected); | 
| 359   scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL, NULL)); | 353   scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL, NULL)); | 
| 360   ASSERT_TRUE(deserial_root.get()); | 354   ASSERT_TRUE(deserial_root.get()); | 
| 361   DictionaryValue* dict_root = | 355   DictionaryValue* dict_root = | 
| 362       static_cast<DictionaryValue*>(deserial_root.get()); | 356       static_cast<DictionaryValue*>(deserial_root.get()); | 
| 363   string16 test_value; | 357   string16 test_value; | 
| 364   ASSERT_TRUE(dict_root->GetString("test", &test_value)); | 358   ASSERT_TRUE(dict_root->GetString("test", &test_value)); | 
| 365   ASSERT_EQ(test, test_value); | 359   ASSERT_EQ(test, test_value); | 
| 366 | 360 | 
| 367   // Test converting escaped regular chars | 361   // Test converting escaped regular chars | 
| 368   static const char kEscapedChars[] = "{\"test\":\"\\u0067\\u006f\"}"; | 362   static const char kEscapedChars[] = "{\"test\":\"\\u0067\\u006f\"}"; | 
| 369   JSONStringValueSerializer deserializer2(kEscapedChars); | 363   JSONStringValueDeserializer deserializer2(kEscapedChars); | 
| 370   deserial_root.reset(deserializer2.Deserialize(NULL, NULL)); | 364   deserial_root.reset(deserializer2.Deserialize(NULL, NULL)); | 
| 371   ASSERT_TRUE(deserial_root.get()); | 365   ASSERT_TRUE(deserial_root.get()); | 
| 372   dict_root = static_cast<DictionaryValue*>(deserial_root.get()); | 366   dict_root = static_cast<DictionaryValue*>(deserial_root.get()); | 
| 373   ASSERT_TRUE(dict_root->GetString("test", &test_value)); | 367   ASSERT_TRUE(dict_root->GetString("test", &test_value)); | 
| 374   ASSERT_EQ(ASCIIToUTF16("go"), test_value); | 368   ASSERT_EQ(ASCIIToUTF16("go"), test_value); | 
| 375 } | 369 } | 
| 376 | 370 | 
| 377 TEST(JSONValueSerializerTest, AllowTrailingComma) { |  | 
| 378   scoped_ptr<Value> root; |  | 
| 379   scoped_ptr<Value> root_expected; |  | 
| 380   static const char kTestWithCommas[] = "{\"key\": [true,],}"; |  | 
| 381   static const char kTestNoCommas[] = "{\"key\": [true]}"; |  | 
| 382 |  | 
| 383   JSONStringValueSerializer serializer(kTestWithCommas); |  | 
| 384   serializer.set_allow_trailing_comma(true); |  | 
| 385   JSONStringValueSerializer serializer_expected(kTestNoCommas); |  | 
| 386   root.reset(serializer.Deserialize(NULL, NULL)); |  | 
| 387   ASSERT_TRUE(root.get()); |  | 
| 388   root_expected.reset(serializer_expected.Deserialize(NULL, NULL)); |  | 
| 389   ASSERT_TRUE(root_expected.get()); |  | 
| 390   ASSERT_TRUE(root->Equals(root_expected.get())); |  | 
| 391 } |  | 
| 392 |  | 
| 393 TEST(JSONValueSerializerTest, JSONReaderComments) { | 371 TEST(JSONValueSerializerTest, JSONReaderComments) { | 
| 394   ValidateJsonList("[ // 2, 3, ignore me ] \n1 ]"); | 372   ValidateJsonList("[ // 2, 3, ignore me ] \n1 ]"); | 
| 395   ValidateJsonList("[ /* 2, \n3, ignore me ]*/ \n1 ]"); | 373   ValidateJsonList("[ /* 2, \n3, ignore me ]*/ \n1 ]"); | 
| 396   ValidateJsonList("//header\n[ // 2, \n// 3, \n1 ]// footer"); | 374   ValidateJsonList("//header\n[ // 2, \n// 3, \n1 ]// footer"); | 
| 397   ValidateJsonList("/*\n[ // 2, \n// 3, \n1 ]*/[1]"); | 375   ValidateJsonList("/*\n[ // 2, \n// 3, \n1 ]*/[1]"); | 
| 398   ValidateJsonList("[ 1 /* one */ ] /* end */"); | 376   ValidateJsonList("[ 1 /* one */ ] /* end */"); | 
| 399   ValidateJsonList("[ 1 //// ,2\r\n ]"); | 377   ValidateJsonList("[ 1 //// ,2\r\n ]"); | 
| 400 | 378 | 
| 401   scoped_ptr<Value> root; | 379   scoped_ptr<Value> root; | 
| 402 | 380 | 
| (...skipping 25 matching lines...) Expand all  Loading... | 
| 428 }; | 406 }; | 
| 429 | 407 | 
| 430 TEST_F(JSONFileValueSerializerTest, Roundtrip) { | 408 TEST_F(JSONFileValueSerializerTest, Roundtrip) { | 
| 431   base::FilePath original_file_path; | 409   base::FilePath original_file_path; | 
| 432   ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &original_file_path)); | 410   ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &original_file_path)); | 
| 433   original_file_path = | 411   original_file_path = | 
| 434       original_file_path.Append(FILE_PATH_LITERAL("serializer_test.json")); | 412       original_file_path.Append(FILE_PATH_LITERAL("serializer_test.json")); | 
| 435 | 413 | 
| 436   ASSERT_TRUE(PathExists(original_file_path)); | 414   ASSERT_TRUE(PathExists(original_file_path)); | 
| 437 | 415 | 
| 438   JSONFileValueSerializer deserializer(original_file_path); | 416   JSONFileValueDeserializer deserializer(original_file_path); | 
| 439   scoped_ptr<Value> root; | 417   scoped_ptr<Value> root; | 
| 440   root.reset(deserializer.Deserialize(NULL, NULL)); | 418   root.reset(deserializer.Deserialize(NULL, NULL)); | 
| 441 | 419 | 
| 442   ASSERT_TRUE(root.get()); | 420   ASSERT_TRUE(root.get()); | 
| 443   ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); | 421   ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); | 
| 444 | 422 | 
| 445   DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get()); | 423   DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get()); | 
| 446 | 424 | 
| 447   Value* null_value = NULL; | 425   Value* null_value = NULL; | 
| 448   ASSERT_TRUE(root_dict->Get("null", &null_value)); | 426   ASSERT_TRUE(root_dict->Get("null", &null_value)); | 
| (...skipping 27 matching lines...) Expand all  Loading... | 
| 476 } | 454 } | 
| 477 | 455 | 
| 478 TEST_F(JSONFileValueSerializerTest, RoundtripNested) { | 456 TEST_F(JSONFileValueSerializerTest, RoundtripNested) { | 
| 479   base::FilePath original_file_path; | 457   base::FilePath original_file_path; | 
| 480   ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &original_file_path)); | 458   ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &original_file_path)); | 
| 481   original_file_path = original_file_path.Append( | 459   original_file_path = original_file_path.Append( | 
| 482       FILE_PATH_LITERAL("serializer_nested_test.json")); | 460       FILE_PATH_LITERAL("serializer_nested_test.json")); | 
| 483 | 461 | 
| 484   ASSERT_TRUE(PathExists(original_file_path)); | 462   ASSERT_TRUE(PathExists(original_file_path)); | 
| 485 | 463 | 
| 486   JSONFileValueSerializer deserializer(original_file_path); | 464   JSONFileValueDeserializer deserializer(original_file_path); | 
| 487   scoped_ptr<Value> root; | 465   scoped_ptr<Value> root; | 
| 488   root.reset(deserializer.Deserialize(NULL, NULL)); | 466   root.reset(deserializer.Deserialize(NULL, NULL)); | 
| 489   ASSERT_TRUE(root.get()); | 467   ASSERT_TRUE(root.get()); | 
| 490 | 468 | 
| 491   // Now try writing. | 469   // Now try writing. | 
| 492   base::FilePath written_file_path = temp_dir_.path().Append( | 470   base::FilePath written_file_path = temp_dir_.path().Append( | 
| 493       FILE_PATH_LITERAL("test_output.json")); | 471       FILE_PATH_LITERAL("test_output.json")); | 
| 494 | 472 | 
| 495   ASSERT_FALSE(PathExists(written_file_path)); | 473   ASSERT_FALSE(PathExists(written_file_path)); | 
| 496   JSONFileValueSerializer serializer(written_file_path); | 474   JSONFileValueSerializer serializer(written_file_path); | 
| 497   ASSERT_TRUE(serializer.Serialize(*root)); | 475   ASSERT_TRUE(serializer.Serialize(*root)); | 
| 498   ASSERT_TRUE(PathExists(written_file_path)); | 476   ASSERT_TRUE(PathExists(written_file_path)); | 
| 499 | 477 | 
| 500   // Now compare file contents. | 478   // Now compare file contents. | 
| 501   EXPECT_TRUE(TextContentsEqual(original_file_path, written_file_path)); | 479   EXPECT_TRUE(TextContentsEqual(original_file_path, written_file_path)); | 
| 502   EXPECT_TRUE(base::DeleteFile(written_file_path, false)); | 480   EXPECT_TRUE(base::DeleteFile(written_file_path, false)); | 
| 503 } | 481 } | 
| 504 | 482 | 
| 505 TEST_F(JSONFileValueSerializerTest, NoWhitespace) { | 483 TEST_F(JSONFileValueSerializerTest, NoWhitespace) { | 
| 506   base::FilePath source_file_path; | 484   base::FilePath source_file_path; | 
| 507   ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &source_file_path)); | 485   ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &source_file_path)); | 
| 508   source_file_path = source_file_path.Append( | 486   source_file_path = source_file_path.Append( | 
| 509       FILE_PATH_LITERAL("serializer_test_nowhitespace.json")); | 487       FILE_PATH_LITERAL("serializer_test_nowhitespace.json")); | 
| 510   ASSERT_TRUE(PathExists(source_file_path)); | 488   ASSERT_TRUE(PathExists(source_file_path)); | 
| 511   JSONFileValueSerializer serializer(source_file_path); | 489   JSONFileValueDeserializer deserializer(source_file_path); | 
| 512   scoped_ptr<Value> root; | 490   scoped_ptr<Value> root; | 
| 513   root.reset(serializer.Deserialize(NULL, NULL)); | 491   root.reset(deserializer.Deserialize(NULL, NULL)); | 
| 514   ASSERT_TRUE(root.get()); | 492   ASSERT_TRUE(root.get()); | 
| 515 } | 493 } | 
| 516 | 494 | 
| 517 }  // namespace | 495 }  // namespace | 
| 518 | 496 | 
| 519 }  // namespace base | 497 }  // namespace base | 
| OLD | NEW | 
|---|