blob: cb88cdef3bf539c3f6b40147ce4564089826a644 [file] [log] [blame]
Vitaly Bukacbed2062015-08-17 12:54:05 -07001// 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.
4
5#include "base/json/json_writer.h"
Vitaly Buka8750b272015-08-18 18:39:08 -07006
7#include <gtest/gtest.h>
8
Vitaly Bukacbed2062015-08-17 12:54:05 -07009#include "base/values.h"
Vitaly Bukacbed2062015-08-17 12:54:05 -070010
11namespace base {
12
13TEST(JSONWriterTest, BasicTypes) {
14 std::string output_js;
15
16 // Test null.
17 EXPECT_TRUE(JSONWriter::Write(*Value::CreateNullValue(), &output_js));
18 EXPECT_EQ("null", output_js);
19
20 // Test empty dict.
21 EXPECT_TRUE(JSONWriter::Write(DictionaryValue(), &output_js));
22 EXPECT_EQ("{}", output_js);
23
24 // Test empty list.
25 EXPECT_TRUE(JSONWriter::Write(ListValue(), &output_js));
26 EXPECT_EQ("[]", output_js);
27
28 // Test integer values.
29 EXPECT_TRUE(JSONWriter::Write(FundamentalValue(42), &output_js));
30 EXPECT_EQ("42", output_js);
31
32 // Test boolean values.
33 EXPECT_TRUE(JSONWriter::Write(FundamentalValue(true), &output_js));
34 EXPECT_EQ("true", output_js);
35
36 // Test Real values should always have a decimal or an 'e'.
37 EXPECT_TRUE(JSONWriter::Write(FundamentalValue(1.0), &output_js));
38 EXPECT_EQ("1.0", output_js);
39
40 // Test Real values in the the range (-1, 1) must have leading zeros
41 EXPECT_TRUE(JSONWriter::Write(FundamentalValue(0.2), &output_js));
42 EXPECT_EQ("0.2", output_js);
43
44 // Test Real values in the the range (-1, 1) must have leading zeros
45 EXPECT_TRUE(JSONWriter::Write(FundamentalValue(-0.8), &output_js));
46 EXPECT_EQ("-0.8", output_js);
47
48 // Test String values.
49 EXPECT_TRUE(JSONWriter::Write(StringValue("foo"), &output_js));
50 EXPECT_EQ("\"foo\"", output_js);
51}
52
53TEST(JSONWriterTest, NestedTypes) {
54 std::string output_js;
55
56 // Writer unittests like empty list/dict nesting,
57 // list list nesting, etc.
58 DictionaryValue root_dict;
59 scoped_ptr<ListValue> list(new ListValue());
60 scoped_ptr<DictionaryValue> inner_dict(new DictionaryValue());
61 inner_dict->SetInteger("inner int", 10);
62 list->Append(inner_dict.Pass());
63 list->Append(make_scoped_ptr(new ListValue()));
64 list->AppendBoolean(true);
65 root_dict.Set("list", list.Pass());
66
67 // Test the pretty-printer.
68 EXPECT_TRUE(JSONWriter::Write(root_dict, &output_js));
69 EXPECT_EQ("{\"list\":[{\"inner int\":10},[],true]}", output_js);
70 EXPECT_TRUE(JSONWriter::WriteWithOptions(
71 root_dict, JSONWriter::OPTIONS_PRETTY_PRINT, &output_js));
72
73 // The pretty-printer uses a different newline style on Windows than on
74 // other platforms.
75#if defined(OS_WIN)
76#define JSON_NEWLINE "\r\n"
77#else
78#define JSON_NEWLINE "\n"
79#endif
80 EXPECT_EQ("{" JSON_NEWLINE
81 " \"list\": [ {" JSON_NEWLINE
82 " \"inner int\": 10" JSON_NEWLINE
83 " }, [ ], true ]" JSON_NEWLINE
84 "}" JSON_NEWLINE,
85 output_js);
86#undef JSON_NEWLINE
87}
88
89TEST(JSONWriterTest, KeysWithPeriods) {
90 std::string output_js;
91
92 DictionaryValue period_dict;
93 period_dict.SetIntegerWithoutPathExpansion("a.b", 3);
94 period_dict.SetIntegerWithoutPathExpansion("c", 2);
95 scoped_ptr<DictionaryValue> period_dict2(new DictionaryValue());
96 period_dict2->SetIntegerWithoutPathExpansion("g.h.i.j", 1);
97 period_dict.SetWithoutPathExpansion("d.e.f", period_dict2.Pass());
98 EXPECT_TRUE(JSONWriter::Write(period_dict, &output_js));
99 EXPECT_EQ("{\"a.b\":3,\"c\":2,\"d.e.f\":{\"g.h.i.j\":1}}", output_js);
100
101 DictionaryValue period_dict3;
102 period_dict3.SetInteger("a.b", 2);
103 period_dict3.SetIntegerWithoutPathExpansion("a.b", 1);
104 EXPECT_TRUE(JSONWriter::Write(period_dict3, &output_js));
105 EXPECT_EQ("{\"a\":{\"b\":2},\"a.b\":1}", output_js);
106}
107
108TEST(JSONWriterTest, BinaryValues) {
109 std::string output_js;
110
111 // Binary values should return errors unless suppressed via the
112 // OPTIONS_OMIT_BINARY_VALUES flag.
113 scoped_ptr<Value> root(BinaryValue::CreateWithCopiedBuffer("asdf", 4));
114 EXPECT_FALSE(JSONWriter::Write(*root, &output_js));
115 EXPECT_TRUE(JSONWriter::WriteWithOptions(
116 *root, JSONWriter::OPTIONS_OMIT_BINARY_VALUES, &output_js));
117 EXPECT_TRUE(output_js.empty());
118
119 ListValue binary_list;
120 binary_list.Append(BinaryValue::CreateWithCopiedBuffer("asdf", 4));
121 binary_list.Append(make_scoped_ptr(new FundamentalValue(5)));
122 binary_list.Append(BinaryValue::CreateWithCopiedBuffer("asdf", 4));
123 binary_list.Append(make_scoped_ptr(new FundamentalValue(2)));
124 binary_list.Append(BinaryValue::CreateWithCopiedBuffer("asdf", 4));
125 EXPECT_FALSE(JSONWriter::Write(binary_list, &output_js));
126 EXPECT_TRUE(JSONWriter::WriteWithOptions(
127 binary_list, JSONWriter::OPTIONS_OMIT_BINARY_VALUES, &output_js));
128 EXPECT_EQ("[5,2]", output_js);
129
130 DictionaryValue binary_dict;
131 binary_dict.Set(
132 "a", make_scoped_ptr(BinaryValue::CreateWithCopiedBuffer("asdf", 4)));
133 binary_dict.SetInteger("b", 5);
134 binary_dict.Set(
135 "c", make_scoped_ptr(BinaryValue::CreateWithCopiedBuffer("asdf", 4)));
136 binary_dict.SetInteger("d", 2);
137 binary_dict.Set(
138 "e", make_scoped_ptr(BinaryValue::CreateWithCopiedBuffer("asdf", 4)));
139 EXPECT_FALSE(JSONWriter::Write(binary_dict, &output_js));
140 EXPECT_TRUE(JSONWriter::WriteWithOptions(
141 binary_dict, JSONWriter::OPTIONS_OMIT_BINARY_VALUES, &output_js));
142 EXPECT_EQ("{\"b\":5,\"d\":2}", output_js);
143}
144
145TEST(JSONWriterTest, DoublesAsInts) {
146 std::string output_js;
147
148 // Test allowing a double with no fractional part to be written as an integer.
149 FundamentalValue double_value(1e10);
150 EXPECT_TRUE(JSONWriter::WriteWithOptions(
151 double_value, JSONWriter::OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION,
152 &output_js));
153 EXPECT_EQ("10000000000", output_js);
154}
155
156} // namespace base