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(const 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 deserialization from string is working.
90 TEST(JSONValueDeserializerTest
, ReadProperJSONFromString
) {
91 // Try to deserialize it through the serializer.
92 JSONStringValueDeserializer
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 StringPiece substring.
106 TEST(JSONValueDeserializerTest
, ReadProperJSONFromStringPiece
) {
107 // Create a StringPiece for the substring of kProperJSONPadded that matches
109 base::StringPiece
proper_json(kProperJSONPadded
);
110 proper_json
= proper_json
.substr(5, proper_json
.length() - 10);
111 JSONStringValueDeserializer
str_deserializer(proper_json
);
114 std::string error_message
;
115 scoped_ptr
<Value
> value(
116 str_deserializer
.Deserialize(&error_code
, &error_message
));
117 ASSERT_TRUE(value
.get());
118 ASSERT_EQ(0, error_code
);
119 ASSERT_TRUE(error_message
.empty());
120 // Verify if the same JSON is still there.
121 CheckJSONIsStillTheSame(*value
);
124 // Test that trialing commas are only properly deserialized from string when
125 // the proper flag for that is set.
126 TEST(JSONValueDeserializerTest
, ReadJSONWithTrailingCommasFromString
) {
127 // Try to deserialize it through the serializer.
128 JSONStringValueDeserializer
str_deserializer(kProperJSONWithCommas
);
131 std::string error_message
;
132 scoped_ptr
<Value
> value(
133 str_deserializer
.Deserialize(&error_code
, &error_message
));
134 ASSERT_FALSE(value
.get());
135 ASSERT_NE(0, error_code
);
136 ASSERT_FALSE(error_message
.empty());
137 // Now the flag is set and it must pass.
138 str_deserializer
.set_allow_trailing_comma(true);
139 value
.reset(str_deserializer
.Deserialize(&error_code
, &error_message
));
140 ASSERT_TRUE(value
.get());
141 ASSERT_EQ(JSONReader::JSON_TRAILING_COMMA
, error_code
);
142 // Verify if the same JSON is still there.
143 CheckJSONIsStillTheSame(*value
);
146 // Test proper JSON deserialization from file is working.
147 TEST(JSONValueDeserializerTest
, ReadProperJSONFromFile
) {
148 ScopedTempDir tempdir
;
149 ASSERT_TRUE(tempdir
.CreateUniqueTempDir());
150 // Write it down in the file.
151 FilePath
temp_file(tempdir
.path().AppendASCII("test.json"));
152 ASSERT_EQ(static_cast<int>(strlen(kProperJSON
)),
153 WriteFile(temp_file
, kProperJSON
, strlen(kProperJSON
)));
155 // Try to deserialize it through the serializer.
156 JSONFileValueDeserializer
file_deserializer(temp_file
);
159 std::string error_message
;
160 scoped_ptr
<Value
> value(
161 file_deserializer
.Deserialize(&error_code
, &error_message
));
162 ASSERT_TRUE(value
.get());
163 ASSERT_EQ(0, error_code
);
164 ASSERT_TRUE(error_message
.empty());
165 // Verify if the same JSON is still there.
166 CheckJSONIsStillTheSame(*value
);
169 // Test that trialing commas are only properly deserialized from file when
170 // the proper flag for that is set.
171 TEST(JSONValueDeserializerTest
, ReadJSONWithCommasFromFile
) {
172 ScopedTempDir tempdir
;
173 ASSERT_TRUE(tempdir
.CreateUniqueTempDir());
174 // Write it down in the file.
175 FilePath
temp_file(tempdir
.path().AppendASCII("test.json"));
176 ASSERT_EQ(static_cast<int>(strlen(kProperJSONWithCommas
)),
177 WriteFile(temp_file
, kProperJSONWithCommas
,
178 strlen(kProperJSONWithCommas
)));
180 // Try to deserialize it through the serializer.
181 JSONFileValueDeserializer
file_deserializer(temp_file
);
182 // This must fail without the proper flag.
184 std::string error_message
;
185 scoped_ptr
<Value
> value(
186 file_deserializer
.Deserialize(&error_code
, &error_message
));
187 ASSERT_FALSE(value
.get());
188 ASSERT_NE(0, error_code
);
189 ASSERT_FALSE(error_message
.empty());
190 // Now the flag is set and it must pass.
191 file_deserializer
.set_allow_trailing_comma(true);
192 value
.reset(file_deserializer
.Deserialize(&error_code
, &error_message
));
193 ASSERT_TRUE(value
.get());
194 ASSERT_EQ(JSONReader::JSON_TRAILING_COMMA
, error_code
);
195 // Verify if the same JSON is still there.
196 CheckJSONIsStillTheSame(*value
);
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]}";
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()));
215 TEST(JSONValueSerializerTest
, Roundtrip
) {
216 static const char kOriginalSerialization
[] =
217 "{\"bool\":true,\"double\":3.14,\"int\":42,\"list\":[1,2],\"null\":null}";
218 JSONStringValueDeserializer
deserializer(kOriginalSerialization
);
219 scoped_ptr
<Value
> root(deserializer
.Deserialize(NULL
, NULL
));
220 ASSERT_TRUE(root
.get());
221 ASSERT_TRUE(root
->IsType(Value::TYPE_DICTIONARY
));
223 DictionaryValue
* root_dict
= static_cast<DictionaryValue
*>(root
.get());
225 Value
* null_value
= NULL
;
226 ASSERT_TRUE(root_dict
->Get("null", &null_value
));
227 ASSERT_TRUE(null_value
);
228 ASSERT_TRUE(null_value
->IsType(Value::TYPE_NULL
));
230 bool bool_value
= false;
231 ASSERT_TRUE(root_dict
->GetBoolean("bool", &bool_value
));
232 ASSERT_TRUE(bool_value
);
235 ASSERT_TRUE(root_dict
->GetInteger("int", &int_value
));
236 ASSERT_EQ(42, int_value
);
238 double double_value
= 0.0;
239 ASSERT_TRUE(root_dict
->GetDouble("double", &double_value
));
240 ASSERT_DOUBLE_EQ(3.14, double_value
);
242 std::string test_serialization
;
243 JSONStringValueSerializer
mutable_serializer(&test_serialization
);
244 ASSERT_TRUE(mutable_serializer
.Serialize(*root_dict
));
245 ASSERT_EQ(kOriginalSerialization
, test_serialization
);
247 mutable_serializer
.set_pretty_print(true);
248 ASSERT_TRUE(mutable_serializer
.Serialize(*root_dict
));
249 // JSON output uses a different newline style on Windows than on other
252 #define JSON_NEWLINE "\r\n"
254 #define JSON_NEWLINE "\n"
256 const std::string pretty_serialization
=
258 " \"bool\": true," JSON_NEWLINE
259 " \"double\": 3.14," JSON_NEWLINE
260 " \"int\": 42," JSON_NEWLINE
261 " \"list\": [ 1, 2 ]," JSON_NEWLINE
262 " \"null\": null" JSON_NEWLINE
265 ASSERT_EQ(pretty_serialization
, test_serialization
);
268 TEST(JSONValueSerializerTest
, StringEscape
) {
270 for (int i
= 1; i
< 256; ++i
) {
271 all_chars
+= static_cast<char16
>(i
);
273 // Generated in in Firefox using the following js (with an extra backslash for
276 // for (var i = 1; i < 256; ++i) { s += String.fromCharCode(i); }
277 // uneval(s).replace(/\\/g, "\\\\");
278 std::string all_chars_expected
=
279 "\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007\\b\\t\\n\\u000B\\f\\r"
280 "\\u000E\\u000F\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017"
281 "\\u0018\\u0019\\u001A\\u001B\\u001C\\u001D\\u001E\\u001F !\\\"#$%&'()*+,"
282 "-./0123456789:;\\u003C=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcde"
283 "fghijklmnopqrstuvwxyz{|}~\x7F\xC2\x80\xC2\x81\xC2\x82\xC2\x83\xC2\x84"
284 "\xC2\x85\xC2\x86\xC2\x87\xC2\x88\xC2\x89\xC2\x8A\xC2\x8B\xC2\x8C\xC2\x8D"
285 "\xC2\x8E\xC2\x8F\xC2\x90\xC2\x91\xC2\x92\xC2\x93\xC2\x94\xC2\x95\xC2\x96"
286 "\xC2\x97\xC2\x98\xC2\x99\xC2\x9A\xC2\x9B\xC2\x9C\xC2\x9D\xC2\x9E\xC2\x9F"
287 "\xC2\xA0\xC2\xA1\xC2\xA2\xC2\xA3\xC2\xA4\xC2\xA5\xC2\xA6\xC2\xA7\xC2\xA8"
288 "\xC2\xA9\xC2\xAA\xC2\xAB\xC2\xAC\xC2\xAD\xC2\xAE\xC2\xAF\xC2\xB0\xC2\xB1"
289 "\xC2\xB2\xC2\xB3\xC2\xB4\xC2\xB5\xC2\xB6\xC2\xB7\xC2\xB8\xC2\xB9\xC2\xBA"
290 "\xC2\xBB\xC2\xBC\xC2\xBD\xC2\xBE\xC2\xBF\xC3\x80\xC3\x81\xC3\x82\xC3\x83"
291 "\xC3\x84\xC3\x85\xC3\x86\xC3\x87\xC3\x88\xC3\x89\xC3\x8A\xC3\x8B\xC3\x8C"
292 "\xC3\x8D\xC3\x8E\xC3\x8F\xC3\x90\xC3\x91\xC3\x92\xC3\x93\xC3\x94\xC3\x95"
293 "\xC3\x96\xC3\x97\xC3\x98\xC3\x99\xC3\x9A\xC3\x9B\xC3\x9C\xC3\x9D\xC3\x9E"
294 "\xC3\x9F\xC3\xA0\xC3\xA1\xC3\xA2\xC3\xA3\xC3\xA4\xC3\xA5\xC3\xA6\xC3\xA7"
295 "\xC3\xA8\xC3\xA9\xC3\xAA\xC3\xAB\xC3\xAC\xC3\xAD\xC3\xAE\xC3\xAF\xC3\xB0"
296 "\xC3\xB1\xC3\xB2\xC3\xB3\xC3\xB4\xC3\xB5\xC3\xB6\xC3\xB7\xC3\xB8\xC3\xB9"
297 "\xC3\xBA\xC3\xBB\xC3\xBC\xC3\xBD\xC3\xBE\xC3\xBF";
299 std::string expected_output
= "{\"all_chars\":\"" + all_chars_expected
+
301 // Test JSONWriter interface
302 std::string output_js
;
303 DictionaryValue valueRoot
;
304 valueRoot
.SetString("all_chars", all_chars
);
305 JSONWriter::Write(&valueRoot
, &output_js
);
306 ASSERT_EQ(expected_output
, output_js
);
308 // Test JSONValueSerializer interface (uses JSONWriter).
309 JSONStringValueSerializer
serializer(&output_js
);
310 ASSERT_TRUE(serializer
.Serialize(valueRoot
));
311 ASSERT_EQ(expected_output
, output_js
);
314 TEST(JSONValueSerializerTest
, UnicodeStrings
) {
315 // unicode string json -> escaped ascii text
316 DictionaryValue root
;
317 string16
test(WideToUTF16(L
"\x7F51\x9875"));
318 root
.SetString("web", test
);
320 static const char kExpected
[] = "{\"web\":\"\xE7\xBD\x91\xE9\xA1\xB5\"}";
323 JSONStringValueSerializer
serializer(&actual
);
324 ASSERT_TRUE(serializer
.Serialize(root
));
325 ASSERT_EQ(kExpected
, actual
);
327 // escaped ascii text -> json
328 JSONStringValueDeserializer
deserializer(kExpected
);
329 scoped_ptr
<Value
> deserial_root(deserializer
.Deserialize(NULL
, NULL
));
330 ASSERT_TRUE(deserial_root
.get());
331 DictionaryValue
* dict_root
=
332 static_cast<DictionaryValue
*>(deserial_root
.get());
334 ASSERT_TRUE(dict_root
->GetString("web", &web_value
));
335 ASSERT_EQ(test
, web_value
);
338 TEST(JSONValueSerializerTest
, HexStrings
) {
339 // hex string json -> escaped ascii text
340 DictionaryValue root
;
341 string16
test(WideToUTF16(L
"\x01\x02"));
342 root
.SetString("test", test
);
344 static const char kExpected
[] = "{\"test\":\"\\u0001\\u0002\"}";
347 JSONStringValueSerializer
serializer(&actual
);
348 ASSERT_TRUE(serializer
.Serialize(root
));
349 ASSERT_EQ(kExpected
, actual
);
351 // escaped ascii text -> json
352 JSONStringValueDeserializer
deserializer(kExpected
);
353 scoped_ptr
<Value
> deserial_root(deserializer
.Deserialize(NULL
, NULL
));
354 ASSERT_TRUE(deserial_root
.get());
355 DictionaryValue
* dict_root
=
356 static_cast<DictionaryValue
*>(deserial_root
.get());
358 ASSERT_TRUE(dict_root
->GetString("test", &test_value
));
359 ASSERT_EQ(test
, test_value
);
361 // Test converting escaped regular chars
362 static const char kEscapedChars
[] = "{\"test\":\"\\u0067\\u006f\"}";
363 JSONStringValueDeserializer
deserializer2(kEscapedChars
);
364 deserial_root
.reset(deserializer2
.Deserialize(NULL
, NULL
));
365 ASSERT_TRUE(deserial_root
.get());
366 dict_root
= static_cast<DictionaryValue
*>(deserial_root
.get());
367 ASSERT_TRUE(dict_root
->GetString("test", &test_value
));
368 ASSERT_EQ(ASCIIToUTF16("go"), test_value
);
371 TEST(JSONValueSerializerTest
, JSONReaderComments
) {
372 ValidateJsonList("[ // 2, 3, ignore me ] \n1 ]");
373 ValidateJsonList("[ /* 2, \n3, ignore me ]*/ \n1 ]");
374 ValidateJsonList("//header\n[ // 2, \n// 3, \n1 ]// footer");
375 ValidateJsonList("/*\n[ // 2, \n// 3, \n1 ]*/[1]");
376 ValidateJsonList("[ 1 /* one */ ] /* end */");
377 ValidateJsonList("[ 1 //// ,2\r\n ]");
379 scoped_ptr
<Value
> root
;
381 // It's ok to have a comment in a string.
382 root
.reset(JSONReader::Read("[\"// ok\\n /* foo */ \"]"));
383 ASSERT_TRUE(root
.get() && root
->IsType(Value::TYPE_LIST
));
384 ListValue
* list
= static_cast<ListValue
*>(root
.get());
385 ASSERT_EQ(1U, list
->GetSize());
387 ASSERT_TRUE(list
->Get(0, &elt
));
389 ASSERT_TRUE(elt
&& elt
->GetAsString(&value
));
390 ASSERT_EQ("// ok\n /* foo */ ", value
);
392 // You can't nest comments.
393 root
.reset(JSONReader::Read("/* /* inner */ outer */ [ 1 ]"));
394 ASSERT_FALSE(root
.get());
396 // Not a open comment token.
397 root
.reset(JSONReader::Read("/ * * / [1]"));
398 ASSERT_FALSE(root
.get());
401 class JSONFileValueSerializerTest
: public testing::Test
{
403 void SetUp() override
{ ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir()); }
405 base::ScopedTempDir temp_dir_
;
408 TEST_F(JSONFileValueSerializerTest
, Roundtrip
) {
409 base::FilePath original_file_path
;
410 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA
, &original_file_path
));
412 original_file_path
.Append(FILE_PATH_LITERAL("serializer_test.json"));
414 ASSERT_TRUE(PathExists(original_file_path
));
416 JSONFileValueDeserializer
deserializer(original_file_path
);
417 scoped_ptr
<Value
> root
;
418 root
.reset(deserializer
.Deserialize(NULL
, NULL
));
420 ASSERT_TRUE(root
.get());
421 ASSERT_TRUE(root
->IsType(Value::TYPE_DICTIONARY
));
423 DictionaryValue
* root_dict
= static_cast<DictionaryValue
*>(root
.get());
425 Value
* null_value
= NULL
;
426 ASSERT_TRUE(root_dict
->Get("null", &null_value
));
427 ASSERT_TRUE(null_value
);
428 ASSERT_TRUE(null_value
->IsType(Value::TYPE_NULL
));
430 bool bool_value
= false;
431 ASSERT_TRUE(root_dict
->GetBoolean("bool", &bool_value
));
432 ASSERT_TRUE(bool_value
);
435 ASSERT_TRUE(root_dict
->GetInteger("int", &int_value
));
436 ASSERT_EQ(42, int_value
);
438 std::string string_value
;
439 ASSERT_TRUE(root_dict
->GetString("string", &string_value
));
440 ASSERT_EQ("hello", string_value
);
443 const base::FilePath written_file_path
=
444 temp_dir_
.path().Append(FILE_PATH_LITERAL("test_output.js"));
446 ASSERT_FALSE(PathExists(written_file_path
));
447 JSONFileValueSerializer
serializer(written_file_path
);
448 ASSERT_TRUE(serializer
.Serialize(*root
));
449 ASSERT_TRUE(PathExists(written_file_path
));
451 // Now compare file contents.
452 EXPECT_TRUE(TextContentsEqual(original_file_path
, written_file_path
));
453 EXPECT_TRUE(base::DeleteFile(written_file_path
, false));
456 TEST_F(JSONFileValueSerializerTest
, RoundtripNested
) {
457 base::FilePath original_file_path
;
458 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA
, &original_file_path
));
459 original_file_path
= original_file_path
.Append(
460 FILE_PATH_LITERAL("serializer_nested_test.json"));
462 ASSERT_TRUE(PathExists(original_file_path
));
464 JSONFileValueDeserializer
deserializer(original_file_path
);
465 scoped_ptr
<Value
> root
;
466 root
.reset(deserializer
.Deserialize(NULL
, NULL
));
467 ASSERT_TRUE(root
.get());
470 base::FilePath written_file_path
= temp_dir_
.path().Append(
471 FILE_PATH_LITERAL("test_output.json"));
473 ASSERT_FALSE(PathExists(written_file_path
));
474 JSONFileValueSerializer
serializer(written_file_path
);
475 ASSERT_TRUE(serializer
.Serialize(*root
));
476 ASSERT_TRUE(PathExists(written_file_path
));
478 // Now compare file contents.
479 EXPECT_TRUE(TextContentsEqual(original_file_path
, written_file_path
));
480 EXPECT_TRUE(base::DeleteFile(written_file_path
, false));
483 TEST_F(JSONFileValueSerializerTest
, NoWhitespace
) {
484 base::FilePath source_file_path
;
485 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA
, &source_file_path
));
486 source_file_path
= source_file_path
.Append(
487 FILE_PATH_LITERAL("serializer_test_nowhitespace.json"));
488 ASSERT_TRUE(PathExists(source_file_path
));
489 JSONFileValueDeserializer
deserializer(source_file_path
);
490 scoped_ptr
<Value
> root
;
491 root
.reset(deserializer
.Deserialize(NULL
, NULL
));
492 ASSERT_TRUE(root
.get());