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