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
3 // found in the LICENSE file.
7 #include "base/files/file_util.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/json/json_file_value_serializer.h"
10 #include "base/json/json_reader.h"
11 #include "base/json/json_string_value_serializer.h"
12 #include "base/json/json_writer.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/path_service.h"
15 #include "base/strings/string_piece.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/values.h"
19 #include "testing/gtest/include/gtest/gtest.h"
25 // Some proper JSON to test with:
26 const char kProperJSON
[] =
32 " \"some_String\": \"1337\",\n"
33 " \"some_int\": 42,\n"
34 " \"the_list\": [ \"val1\", \"val2\" ]\n"
37 // Some proper JSON with trailing commas:
38 const char kProperJSONWithCommas
[] =
40 "\t\"some_int\": 42,\n"
41 "\t\"some_String\": \"1337\",\n"
42 "\t\"the_list\": [\"val1\", \"val2\", ],\n"
43 "\t\"compound\": { \"a\": 1, \"b\": 2, },\n"
46 // kProperJSON with a few misc characters at the begin and end.
47 const char kProperJSONPadded
[] =
54 " \"some_String\": \"1337\",\n"
55 " \"some_int\": 42,\n"
56 " \"the_list\": [ \"val1\", \"val2\" ]\n"
60 const char kWinLineEnds
[] = "\r\n";
61 const char kLinuxLineEnds
[] = "\n";
63 // Verifies the generated JSON against the expected output.
64 void CheckJSONIsStillTheSame(Value
& value
) {
65 // Serialize back the output.
66 std::string serialized_json
;
67 JSONStringValueSerializer
str_serializer(&serialized_json
);
68 str_serializer
.set_pretty_print(true);
69 ASSERT_TRUE(str_serializer
.Serialize(value
));
70 // Unify line endings between platforms.
71 ReplaceSubstringsAfterOffset(&serialized_json
, 0,
72 kWinLineEnds
, kLinuxLineEnds
);
73 // Now compare the input with the output.
74 ASSERT_EQ(kProperJSON
, serialized_json
);
77 void ValidateJsonList(const std::string
& json
) {
78 scoped_ptr
<Value
> root(JSONReader::Read(json
));
79 ASSERT_TRUE(root
.get() && root
->IsType(Value::TYPE_LIST
));
80 ListValue
* list
= static_cast<ListValue
*>(root
.get());
81 ASSERT_EQ(1U, list
->GetSize());
83 ASSERT_TRUE(list
->Get(0, &elt
));
85 ASSERT_TRUE(elt
&& elt
->GetAsInteger(&value
));
89 // Test proper JSON [de]serialization from string is working.
90 TEST(JSONValueSerializerTest
, ReadProperJSONFromString
) {
91 // Try to deserialize it through the serializer.
92 JSONStringValueSerializer
str_deserializer(kProperJSON
);
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
);
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
);
113 std::string error_message
;
114 scoped_ptr
<Value
> value(
115 str_deserializer
.Deserialize(&error_code
, &error_message
));
116 ASSERT_TRUE(value
.get());
117 ASSERT_EQ(0, error_code
);
118 ASSERT_TRUE(error_message
.empty());
119 // Verify if the same JSON is still there.
120 CheckJSONIsStillTheSame(*value
);
123 // Test proper JSON deserialization from a StringPiece substring.
124 TEST(JSONValueSerializerTest
, ReadProperJSONFromStringPiece
) {
125 // Create a StringPiece for the substring of kProperJSONPadded that matches
127 base::StringPiece
proper_json(kProperJSONPadded
);
128 proper_json
= proper_json
.substr(5, proper_json
.length() - 10);
129 JSONStringValueSerializer
str_deserializer(proper_json
);
132 std::string error_message
;
133 scoped_ptr
<Value
> value(
134 str_deserializer
.Deserialize(&error_code
, &error_message
));
135 ASSERT_TRUE(value
.get());
136 ASSERT_EQ(0, error_code
);
137 ASSERT_TRUE(error_message
.empty());
138 // Verify if the same JSON is still there.
139 CheckJSONIsStillTheSame(*value
);
142 // Test that trialing commas are only properly deserialized from string when
143 // the proper flag for that is set.
144 TEST(JSONValueSerializerTest
, ReadJSONWithTrailingCommasFromString
) {
145 // Try to deserialize it through the serializer.
146 JSONStringValueSerializer
str_deserializer(kProperJSONWithCommas
);
149 std::string error_message
;
150 scoped_ptr
<Value
> value(
151 str_deserializer
.Deserialize(&error_code
, &error_message
));
152 ASSERT_FALSE(value
.get());
153 ASSERT_NE(0, error_code
);
154 ASSERT_FALSE(error_message
.empty());
155 // Now the flag is set and it must pass.
156 str_deserializer
.set_allow_trailing_comma(true);
157 value
.reset(str_deserializer
.Deserialize(&error_code
, &error_message
));
158 ASSERT_TRUE(value
.get());
159 ASSERT_EQ(JSONReader::JSON_TRAILING_COMMA
, error_code
);
160 // Verify if the same JSON is still there.
161 CheckJSONIsStillTheSame(*value
);
164 // Test proper JSON [de]serialization from file is working.
165 TEST(JSONValueSerializerTest
, ReadProperJSONFromFile
) {
166 ScopedTempDir tempdir
;
167 ASSERT_TRUE(tempdir
.CreateUniqueTempDir());
168 // Write it down in the file.
169 FilePath
temp_file(tempdir
.path().AppendASCII("test.json"));
170 ASSERT_EQ(static_cast<int>(strlen(kProperJSON
)),
171 WriteFile(temp_file
, kProperJSON
, strlen(kProperJSON
)));
173 // Try to deserialize it through the serializer.
174 JSONFileValueSerializer
file_deserializer(temp_file
);
177 std::string error_message
;
178 scoped_ptr
<Value
> value(
179 file_deserializer
.Deserialize(&error_code
, &error_message
));
180 ASSERT_TRUE(value
.get());
181 ASSERT_EQ(0, error_code
);
182 ASSERT_TRUE(error_message
.empty());
183 // Verify if the same JSON is still there.
184 CheckJSONIsStillTheSame(*value
);
187 // Test that trialing commas are only properly deserialized from file when
188 // the proper flag for that is set.
189 TEST(JSONValueSerializerTest
, ReadJSONWithCommasFromFile
) {
190 ScopedTempDir tempdir
;
191 ASSERT_TRUE(tempdir
.CreateUniqueTempDir());
192 // Write it down in the file.
193 FilePath
temp_file(tempdir
.path().AppendASCII("test.json"));
194 ASSERT_EQ(static_cast<int>(strlen(kProperJSONWithCommas
)),
195 WriteFile(temp_file
, kProperJSONWithCommas
,
196 strlen(kProperJSONWithCommas
)));
198 // Try to deserialize it through the serializer.
199 JSONFileValueSerializer
file_deserializer(temp_file
);
200 // This must fail without the proper flag.
202 std::string error_message
;
203 scoped_ptr
<Value
> value(
204 file_deserializer
.Deserialize(&error_code
, &error_message
));
205 ASSERT_FALSE(value
.get());
206 ASSERT_NE(0, error_code
);
207 ASSERT_FALSE(error_message
.empty());
208 // Now the flag is set and it must pass.
209 file_deserializer
.set_allow_trailing_comma(true);
210 value
.reset(file_deserializer
.Deserialize(&error_code
, &error_message
));
211 ASSERT_TRUE(value
.get());
212 ASSERT_EQ(JSONReader::JSON_TRAILING_COMMA
, error_code
);
213 // Verify if the same JSON is still there.
214 CheckJSONIsStillTheSame(*value
);
217 TEST(JSONValueSerializerTest
, Roundtrip
) {
218 static const char kOriginalSerialization
[] =
219 "{\"bool\":true,\"double\":3.14,\"int\":42,\"list\":[1,2],\"null\":null}";
220 JSONStringValueSerializer
serializer(kOriginalSerialization
);
221 scoped_ptr
<Value
> root(serializer
.Deserialize(NULL
, NULL
));
222 ASSERT_TRUE(root
.get());
223 ASSERT_TRUE(root
->IsType(Value::TYPE_DICTIONARY
));
225 DictionaryValue
* root_dict
= static_cast<DictionaryValue
*>(root
.get());
227 Value
* null_value
= NULL
;
228 ASSERT_TRUE(root_dict
->Get("null", &null_value
));
229 ASSERT_TRUE(null_value
);
230 ASSERT_TRUE(null_value
->IsType(Value::TYPE_NULL
));
232 bool bool_value
= false;
233 ASSERT_TRUE(root_dict
->GetBoolean("bool", &bool_value
));
234 ASSERT_TRUE(bool_value
);
237 ASSERT_TRUE(root_dict
->GetInteger("int", &int_value
));
238 ASSERT_EQ(42, int_value
);
240 double double_value
= 0.0;
241 ASSERT_TRUE(root_dict
->GetDouble("double", &double_value
));
242 ASSERT_DOUBLE_EQ(3.14, double_value
);
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
));
248 std::string test_serialization
;
249 JSONStringValueSerializer
mutable_serializer(&test_serialization
);
250 ASSERT_TRUE(mutable_serializer
.Serialize(*root_dict
));
251 ASSERT_EQ(kOriginalSerialization
, test_serialization
);
253 mutable_serializer
.set_pretty_print(true);
254 ASSERT_TRUE(mutable_serializer
.Serialize(*root_dict
));
255 // JSON output uses a different newline style on Windows than on other
258 #define JSON_NEWLINE "\r\n"
260 #define JSON_NEWLINE "\n"
262 const std::string pretty_serialization
=
264 " \"bool\": true," JSON_NEWLINE
265 " \"double\": 3.14," JSON_NEWLINE
266 " \"int\": 42," JSON_NEWLINE
267 " \"list\": [ 1, 2 ]," JSON_NEWLINE
268 " \"null\": null" JSON_NEWLINE
271 ASSERT_EQ(pretty_serialization
, test_serialization
);
274 TEST(JSONValueSerializerTest
, StringEscape
) {
276 for (int i
= 1; i
< 256; ++i
) {
277 all_chars
+= static_cast<char16
>(i
);
279 // Generated in in Firefox using the following js (with an extra backslash for
282 // for (var i = 1; i < 256; ++i) { s += String.fromCharCode(i); }
283 // uneval(s).replace(/\\/g, "\\\\");
284 std::string all_chars_expected
=
285 "\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007\\b\\t\\n\\u000B\\f\\r"
286 "\\u000E\\u000F\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017"
287 "\\u0018\\u0019\\u001A\\u001B\\u001C\\u001D\\u001E\\u001F !\\\"#$%&'()*+,"
288 "-./0123456789:;\\u003C=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcde"
289 "fghijklmnopqrstuvwxyz{|}~\x7F\xC2\x80\xC2\x81\xC2\x82\xC2\x83\xC2\x84"
290 "\xC2\x85\xC2\x86\xC2\x87\xC2\x88\xC2\x89\xC2\x8A\xC2\x8B\xC2\x8C\xC2\x8D"
291 "\xC2\x8E\xC2\x8F\xC2\x90\xC2\x91\xC2\x92\xC2\x93\xC2\x94\xC2\x95\xC2\x96"
292 "\xC2\x97\xC2\x98\xC2\x99\xC2\x9A\xC2\x9B\xC2\x9C\xC2\x9D\xC2\x9E\xC2\x9F"
293 "\xC2\xA0\xC2\xA1\xC2\xA2\xC2\xA3\xC2\xA4\xC2\xA5\xC2\xA6\xC2\xA7\xC2\xA8"
294 "\xC2\xA9\xC2\xAA\xC2\xAB\xC2\xAC\xC2\xAD\xC2\xAE\xC2\xAF\xC2\xB0\xC2\xB1"
295 "\xC2\xB2\xC2\xB3\xC2\xB4\xC2\xB5\xC2\xB6\xC2\xB7\xC2\xB8\xC2\xB9\xC2\xBA"
296 "\xC2\xBB\xC2\xBC\xC2\xBD\xC2\xBE\xC2\xBF\xC3\x80\xC3\x81\xC3\x82\xC3\x83"
297 "\xC3\x84\xC3\x85\xC3\x86\xC3\x87\xC3\x88\xC3\x89\xC3\x8A\xC3\x8B\xC3\x8C"
298 "\xC3\x8D\xC3\x8E\xC3\x8F\xC3\x90\xC3\x91\xC3\x92\xC3\x93\xC3\x94\xC3\x95"
299 "\xC3\x96\xC3\x97\xC3\x98\xC3\x99\xC3\x9A\xC3\x9B\xC3\x9C\xC3\x9D\xC3\x9E"
300 "\xC3\x9F\xC3\xA0\xC3\xA1\xC3\xA2\xC3\xA3\xC3\xA4\xC3\xA5\xC3\xA6\xC3\xA7"
301 "\xC3\xA8\xC3\xA9\xC3\xAA\xC3\xAB\xC3\xAC\xC3\xAD\xC3\xAE\xC3\xAF\xC3\xB0"
302 "\xC3\xB1\xC3\xB2\xC3\xB3\xC3\xB4\xC3\xB5\xC3\xB6\xC3\xB7\xC3\xB8\xC3\xB9"
303 "\xC3\xBA\xC3\xBB\xC3\xBC\xC3\xBD\xC3\xBE\xC3\xBF";
305 std::string expected_output
= "{\"all_chars\":\"" + all_chars_expected
+
307 // Test JSONWriter interface
308 std::string output_js
;
309 DictionaryValue valueRoot
;
310 valueRoot
.SetString("all_chars", all_chars
);
311 JSONWriter::Write(&valueRoot
, &output_js
);
312 ASSERT_EQ(expected_output
, output_js
);
314 // Test JSONValueSerializer interface (uses JSONWriter).
315 JSONStringValueSerializer
serializer(&output_js
);
316 ASSERT_TRUE(serializer
.Serialize(valueRoot
));
317 ASSERT_EQ(expected_output
, output_js
);
320 TEST(JSONValueSerializerTest
, UnicodeStrings
) {
321 // unicode string json -> escaped ascii text
322 DictionaryValue root
;
323 string16
test(WideToUTF16(L
"\x7F51\x9875"));
324 root
.SetString("web", test
);
326 static const char kExpected
[] = "{\"web\":\"\xE7\xBD\x91\xE9\xA1\xB5\"}";
329 JSONStringValueSerializer
serializer(&actual
);
330 ASSERT_TRUE(serializer
.Serialize(root
));
331 ASSERT_EQ(kExpected
, actual
);
333 // escaped ascii text -> json
334 JSONStringValueSerializer
deserializer(kExpected
);
335 scoped_ptr
<Value
> deserial_root(deserializer
.Deserialize(NULL
, NULL
));
336 ASSERT_TRUE(deserial_root
.get());
337 DictionaryValue
* dict_root
=
338 static_cast<DictionaryValue
*>(deserial_root
.get());
340 ASSERT_TRUE(dict_root
->GetString("web", &web_value
));
341 ASSERT_EQ(test
, web_value
);
344 TEST(JSONValueSerializerTest
, HexStrings
) {
345 // hex string json -> escaped ascii text
346 DictionaryValue root
;
347 string16
test(WideToUTF16(L
"\x01\x02"));
348 root
.SetString("test", test
);
350 static const char kExpected
[] = "{\"test\":\"\\u0001\\u0002\"}";
353 JSONStringValueSerializer
serializer(&actual
);
354 ASSERT_TRUE(serializer
.Serialize(root
));
355 ASSERT_EQ(kExpected
, actual
);
357 // escaped ascii text -> json
358 JSONStringValueSerializer
deserializer(kExpected
);
359 scoped_ptr
<Value
> deserial_root(deserializer
.Deserialize(NULL
, NULL
));
360 ASSERT_TRUE(deserial_root
.get());
361 DictionaryValue
* dict_root
=
362 static_cast<DictionaryValue
*>(deserial_root
.get());
364 ASSERT_TRUE(dict_root
->GetString("test", &test_value
));
365 ASSERT_EQ(test
, test_value
);
367 // Test converting escaped regular chars
368 static const char kEscapedChars
[] = "{\"test\":\"\\u0067\\u006f\"}";
369 JSONStringValueSerializer
deserializer2(kEscapedChars
);
370 deserial_root
.reset(deserializer2
.Deserialize(NULL
, NULL
));
371 ASSERT_TRUE(deserial_root
.get());
372 dict_root
= static_cast<DictionaryValue
*>(deserial_root
.get());
373 ASSERT_TRUE(dict_root
->GetString("test", &test_value
));
374 ASSERT_EQ(ASCIIToUTF16("go"), test_value
);
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]}";
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()));
393 TEST(JSONValueSerializerTest
, JSONReaderComments
) {
394 ValidateJsonList("[ // 2, 3, ignore me ] \n1 ]");
395 ValidateJsonList("[ /* 2, \n3, ignore me ]*/ \n1 ]");
396 ValidateJsonList("//header\n[ // 2, \n// 3, \n1 ]// footer");
397 ValidateJsonList("/*\n[ // 2, \n// 3, \n1 ]*/[1]");
398 ValidateJsonList("[ 1 /* one */ ] /* end */");
399 ValidateJsonList("[ 1 //// ,2\r\n ]");
401 scoped_ptr
<Value
> root
;
403 // It's ok to have a comment in a string.
404 root
.reset(JSONReader::Read("[\"// ok\\n /* foo */ \"]"));
405 ASSERT_TRUE(root
.get() && root
->IsType(Value::TYPE_LIST
));
406 ListValue
* list
= static_cast<ListValue
*>(root
.get());
407 ASSERT_EQ(1U, list
->GetSize());
409 ASSERT_TRUE(list
->Get(0, &elt
));
411 ASSERT_TRUE(elt
&& elt
->GetAsString(&value
));
412 ASSERT_EQ("// ok\n /* foo */ ", value
);
414 // You can't nest comments.
415 root
.reset(JSONReader::Read("/* /* inner */ outer */ [ 1 ]"));
416 ASSERT_FALSE(root
.get());
418 // Not a open comment token.
419 root
.reset(JSONReader::Read("/ * * / [1]"));
420 ASSERT_FALSE(root
.get());
423 class JSONFileValueSerializerTest
: public testing::Test
{
425 void SetUp() override
{ ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir()); }
427 base::ScopedTempDir temp_dir_
;
430 TEST_F(JSONFileValueSerializerTest
, Roundtrip
) {
431 base::FilePath original_file_path
;
432 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA
, &original_file_path
));
434 original_file_path
.Append(FILE_PATH_LITERAL("serializer_test.json"));
436 ASSERT_TRUE(PathExists(original_file_path
));
438 JSONFileValueSerializer
deserializer(original_file_path
);
439 scoped_ptr
<Value
> root
;
440 root
.reset(deserializer
.Deserialize(NULL
, NULL
));
442 ASSERT_TRUE(root
.get());
443 ASSERT_TRUE(root
->IsType(Value::TYPE_DICTIONARY
));
445 DictionaryValue
* root_dict
= static_cast<DictionaryValue
*>(root
.get());
447 Value
* null_value
= NULL
;
448 ASSERT_TRUE(root_dict
->Get("null", &null_value
));
449 ASSERT_TRUE(null_value
);
450 ASSERT_TRUE(null_value
->IsType(Value::TYPE_NULL
));
452 bool bool_value
= false;
453 ASSERT_TRUE(root_dict
->GetBoolean("bool", &bool_value
));
454 ASSERT_TRUE(bool_value
);
457 ASSERT_TRUE(root_dict
->GetInteger("int", &int_value
));
458 ASSERT_EQ(42, int_value
);
460 std::string string_value
;
461 ASSERT_TRUE(root_dict
->GetString("string", &string_value
));
462 ASSERT_EQ("hello", string_value
);
465 const base::FilePath written_file_path
=
466 temp_dir_
.path().Append(FILE_PATH_LITERAL("test_output.js"));
468 ASSERT_FALSE(PathExists(written_file_path
));
469 JSONFileValueSerializer
serializer(written_file_path
);
470 ASSERT_TRUE(serializer
.Serialize(*root
));
471 ASSERT_TRUE(PathExists(written_file_path
));
473 // Now compare file contents.
474 EXPECT_TRUE(TextContentsEqual(original_file_path
, written_file_path
));
475 EXPECT_TRUE(base::DeleteFile(written_file_path
, false));
478 TEST_F(JSONFileValueSerializerTest
, RoundtripNested
) {
479 base::FilePath original_file_path
;
480 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA
, &original_file_path
));
481 original_file_path
= original_file_path
.Append(
482 FILE_PATH_LITERAL("serializer_nested_test.json"));
484 ASSERT_TRUE(PathExists(original_file_path
));
486 JSONFileValueSerializer
deserializer(original_file_path
);
487 scoped_ptr
<Value
> root
;
488 root
.reset(deserializer
.Deserialize(NULL
, NULL
));
489 ASSERT_TRUE(root
.get());
492 base::FilePath written_file_path
= temp_dir_
.path().Append(
493 FILE_PATH_LITERAL("test_output.json"));
495 ASSERT_FALSE(PathExists(written_file_path
));
496 JSONFileValueSerializer
serializer(written_file_path
);
497 ASSERT_TRUE(serializer
.Serialize(*root
));
498 ASSERT_TRUE(PathExists(written_file_path
));
500 // Now compare file contents.
501 EXPECT_TRUE(TextContentsEqual(original_file_path
, written_file_path
));
502 EXPECT_TRUE(base::DeleteFile(written_file_path
, false));
505 TEST_F(JSONFileValueSerializerTest
, NoWhitespace
) {
506 base::FilePath source_file_path
;
507 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA
, &source_file_path
));
508 source_file_path
= source_file_path
.Append(
509 FILE_PATH_LITERAL("serializer_test_nowhitespace.json"));
510 ASSERT_TRUE(PathExists(source_file_path
));
511 JSONFileValueSerializer
serializer(source_file_path
);
512 scoped_ptr
<Value
> root
;
513 root
.reset(serializer
.Deserialize(NULL
, NULL
));
514 ASSERT_TRUE(root
.get());