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.
5 #include "base/json/json_writer.h"
6 #include "base/values.h"
7 #include "testing/gtest/include/gtest/gtest.h"
11 TEST(JSONWriterTest
, BasicTypes
) {
12 std::string output_js
;
15 EXPECT_TRUE(JSONWriter::Write(*Value::CreateNullValue(), &output_js
));
16 EXPECT_EQ("null", output_js
);
19 EXPECT_TRUE(JSONWriter::Write(DictionaryValue(), &output_js
));
20 EXPECT_EQ("{}", output_js
);
23 EXPECT_TRUE(JSONWriter::Write(ListValue(), &output_js
));
24 EXPECT_EQ("[]", output_js
);
26 // Test integer values.
27 EXPECT_TRUE(JSONWriter::Write(FundamentalValue(42), &output_js
));
28 EXPECT_EQ("42", output_js
);
30 // Test boolean values.
31 EXPECT_TRUE(JSONWriter::Write(FundamentalValue(true), &output_js
));
32 EXPECT_EQ("true", output_js
);
34 // Test Real values should always have a decimal or an 'e'.
35 EXPECT_TRUE(JSONWriter::Write(FundamentalValue(1.0), &output_js
));
36 EXPECT_EQ("1.0", output_js
);
38 // Test Real values in the the range (-1, 1) must have leading zeros
39 EXPECT_TRUE(JSONWriter::Write(FundamentalValue(0.2), &output_js
));
40 EXPECT_EQ("0.2", output_js
);
42 // Test Real values in the the range (-1, 1) must have leading zeros
43 EXPECT_TRUE(JSONWriter::Write(FundamentalValue(-0.8), &output_js
));
44 EXPECT_EQ("-0.8", output_js
);
46 // Test String values.
47 EXPECT_TRUE(JSONWriter::Write(StringValue("foo"), &output_js
));
48 EXPECT_EQ("\"foo\"", output_js
);
51 TEST(JSONWriterTest
, NestedTypes
) {
52 std::string output_js
;
54 // Writer unittests like empty list/dict nesting,
55 // list list nesting, etc.
56 DictionaryValue root_dict
;
57 scoped_ptr
<ListValue
> list(new ListValue());
58 scoped_ptr
<DictionaryValue
> inner_dict(new DictionaryValue());
59 inner_dict
->SetInteger("inner int", 10);
60 list
->Append(inner_dict
.Pass());
61 list
->Append(make_scoped_ptr(new ListValue()));
62 list
->AppendBoolean(true);
63 root_dict
.Set("list", list
.Pass());
65 // Test the pretty-printer.
66 EXPECT_TRUE(JSONWriter::Write(root_dict
, &output_js
));
67 EXPECT_EQ("{\"list\":[{\"inner int\":10},[],true]}", output_js
);
68 EXPECT_TRUE(JSONWriter::WriteWithOptions(
69 root_dict
, JSONWriter::OPTIONS_PRETTY_PRINT
, &output_js
));
71 // The pretty-printer uses a different newline style on Windows than on
74 #define JSON_NEWLINE "\r\n"
76 #define JSON_NEWLINE "\n"
78 EXPECT_EQ("{" JSON_NEWLINE
79 " \"list\": [ {" JSON_NEWLINE
80 " \"inner int\": 10" JSON_NEWLINE
81 " }, [ ], true ]" JSON_NEWLINE
87 TEST(JSONWriterTest
, KeysWithPeriods
) {
88 std::string output_js
;
90 DictionaryValue period_dict
;
91 period_dict
.SetIntegerWithoutPathExpansion("a.b", 3);
92 period_dict
.SetIntegerWithoutPathExpansion("c", 2);
93 scoped_ptr
<DictionaryValue
> period_dict2(new DictionaryValue());
94 period_dict2
->SetIntegerWithoutPathExpansion("g.h.i.j", 1);
95 period_dict
.SetWithoutPathExpansion("d.e.f", period_dict2
.Pass());
96 EXPECT_TRUE(JSONWriter::Write(period_dict
, &output_js
));
97 EXPECT_EQ("{\"a.b\":3,\"c\":2,\"d.e.f\":{\"g.h.i.j\":1}}", output_js
);
99 DictionaryValue period_dict3
;
100 period_dict3
.SetInteger("a.b", 2);
101 period_dict3
.SetIntegerWithoutPathExpansion("a.b", 1);
102 EXPECT_TRUE(JSONWriter::Write(period_dict3
, &output_js
));
103 EXPECT_EQ("{\"a\":{\"b\":2},\"a.b\":1}", output_js
);
106 TEST(JSONWriterTest
, BinaryValues
) {
107 std::string output_js
;
109 // Binary values should return errors unless suppressed via the
110 // OPTIONS_OMIT_BINARY_VALUES flag.
111 scoped_ptr
<Value
> root(BinaryValue::CreateWithCopiedBuffer("asdf", 4));
112 EXPECT_FALSE(JSONWriter::Write(*root
, &output_js
));
113 EXPECT_TRUE(JSONWriter::WriteWithOptions(
114 *root
, JSONWriter::OPTIONS_OMIT_BINARY_VALUES
, &output_js
));
115 EXPECT_TRUE(output_js
.empty());
117 ListValue binary_list
;
118 binary_list
.Append(BinaryValue::CreateWithCopiedBuffer("asdf", 4));
119 binary_list
.Append(make_scoped_ptr(new FundamentalValue(5)));
120 binary_list
.Append(BinaryValue::CreateWithCopiedBuffer("asdf", 4));
121 binary_list
.Append(make_scoped_ptr(new FundamentalValue(2)));
122 binary_list
.Append(BinaryValue::CreateWithCopiedBuffer("asdf", 4));
123 EXPECT_FALSE(JSONWriter::Write(binary_list
, &output_js
));
124 EXPECT_TRUE(JSONWriter::WriteWithOptions(
125 binary_list
, JSONWriter::OPTIONS_OMIT_BINARY_VALUES
, &output_js
));
126 EXPECT_EQ("[5,2]", output_js
);
128 DictionaryValue binary_dict
;
130 "a", make_scoped_ptr(BinaryValue::CreateWithCopiedBuffer("asdf", 4)));
131 binary_dict
.SetInteger("b", 5);
133 "c", make_scoped_ptr(BinaryValue::CreateWithCopiedBuffer("asdf", 4)));
134 binary_dict
.SetInteger("d", 2);
136 "e", make_scoped_ptr(BinaryValue::CreateWithCopiedBuffer("asdf", 4)));
137 EXPECT_FALSE(JSONWriter::Write(binary_dict
, &output_js
));
138 EXPECT_TRUE(JSONWriter::WriteWithOptions(
139 binary_dict
, JSONWriter::OPTIONS_OMIT_BINARY_VALUES
, &output_js
));
140 EXPECT_EQ("{\"b\":5,\"d\":2}", output_js
);
143 TEST(JSONWriterTest
, DoublesAsInts
) {
144 std::string output_js
;
146 // Test allowing a double with no fractional part to be written as an integer.
147 FundamentalValue
double_value(1e10
);
148 EXPECT_TRUE(JSONWriter::WriteWithOptions(
149 double_value
, JSONWriter::OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION
,
151 EXPECT_EQ("10000000000", output_js
);