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
7 #include <stdint.h>
8
9 #include <cmath>
10 #include <limits>
11
12 #include "base/json/string_escape.h"
13 #include "base/logging.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/values.h"
17 #include "build/build_config.h"
18
19 namespace base {
20
21 #if defined(OS_WIN)
22 const char kPrettyPrintLineEnding[] = "\r\n";
23 #else
24 const char kPrettyPrintLineEnding[] = "\n";
25 #endif
26
27 // static
Write(const Value & node,std::string * json)28 bool JSONWriter::Write(const Value& node, std::string* json) {
29 return WriteWithOptions(node, 0, json);
30 }
31
32 // static
WriteWithOptions(const Value & node,int options,std::string * json)33 bool JSONWriter::WriteWithOptions(const Value& node,
34 int options,
35 std::string* json) {
36 json->clear();
37 // Is there a better way to estimate the size of the output?
38 json->reserve(1024);
39
40 JSONWriter writer(options, json);
41 bool result = writer.BuildJSONString(node, 0U);
42
43 if (options & OPTIONS_PRETTY_PRINT)
44 json->append(kPrettyPrintLineEnding);
45
46 return result;
47 }
48
JSONWriter(int options,std::string * json)49 JSONWriter::JSONWriter(int options, std::string* json)
50 : omit_binary_values_((options & OPTIONS_OMIT_BINARY_VALUES) != 0),
51 omit_double_type_preservation_(
52 (options & OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION) != 0),
53 pretty_print_((options & OPTIONS_PRETTY_PRINT) != 0),
54 json_string_(json) {
55 DCHECK(json);
56 }
57
BuildJSONString(const Value & node,size_t depth)58 bool JSONWriter::BuildJSONString(const Value& node, size_t depth) {
59 switch (node.type()) {
60 case Value::Type::NONE: {
61 json_string_->append("null");
62 return true;
63 }
64
65 case Value::Type::BOOLEAN: {
66 bool value;
67 bool result = node.GetAsBoolean(&value);
68 DCHECK(result);
69 json_string_->append(value ? "true" : "false");
70 return result;
71 }
72
73 case Value::Type::INTEGER: {
74 int value;
75 bool result = node.GetAsInteger(&value);
76 DCHECK(result);
77 json_string_->append(IntToString(value));
78 return result;
79 }
80
81 case Value::Type::DOUBLE: {
82 double value;
83 bool result = node.GetAsDouble(&value);
84 DCHECK(result);
85 if (omit_double_type_preservation_ &&
86 value <= std::numeric_limits<int64_t>::max() &&
87 value >= std::numeric_limits<int64_t>::min() &&
88 std::floor(value) == value) {
89 json_string_->append(Int64ToString(static_cast<int64_t>(value)));
90 return result;
91 }
92 std::string real = NumberToString(value);
93 // Ensure that the number has a .0 if there's no decimal or 'e'. This
94 // makes sure that when we read the JSON back, it's interpreted as a
95 // real rather than an int.
96 if (real.find('.') == std::string::npos &&
97 real.find('e') == std::string::npos &&
98 real.find('E') == std::string::npos) {
99 real.append(".0");
100 }
101 // The JSON spec requires that non-integer values in the range (-1,1)
102 // have a zero before the decimal point - ".52" is not valid, "0.52" is.
103 if (real[0] == '.') {
104 real.insert(static_cast<size_t>(0), static_cast<size_t>(1), '0');
105 } else if (real.length() > 1 && real[0] == '-' && real[1] == '.') {
106 // "-.1" bad "-0.1" good
107 real.insert(static_cast<size_t>(1), static_cast<size_t>(1), '0');
108 }
109 json_string_->append(real);
110 return result;
111 }
112
113 case Value::Type::STRING: {
114 std::string value;
115 bool result = node.GetAsString(&value);
116 DCHECK(result);
117 EscapeJSONString(value, true, json_string_);
118 return result;
119 }
120
121 case Value::Type::LIST: {
122 json_string_->push_back('[');
123 if (pretty_print_)
124 json_string_->push_back(' ');
125
126 const ListValue* list = nullptr;
127 bool first_value_has_been_output = false;
128 bool result = node.GetAsList(&list);
129 DCHECK(result);
130 for (const auto& value : *list) {
131 if (omit_binary_values_ && value.type() == Value::Type::BINARY)
132 continue;
133
134 if (first_value_has_been_output) {
135 json_string_->push_back(',');
136 if (pretty_print_)
137 json_string_->push_back(' ');
138 }
139
140 if (!BuildJSONString(value, depth))
141 result = false;
142
143 first_value_has_been_output = true;
144 }
145
146 if (pretty_print_)
147 json_string_->push_back(' ');
148 json_string_->push_back(']');
149 return result;
150 }
151
152 case Value::Type::DICTIONARY: {
153 json_string_->push_back('{');
154 if (pretty_print_)
155 json_string_->append(kPrettyPrintLineEnding);
156
157 const DictionaryValue* dict = nullptr;
158 bool first_value_has_been_output = false;
159 bool result = node.GetAsDictionary(&dict);
160 DCHECK(result);
161 for (DictionaryValue::Iterator itr(*dict); !itr.IsAtEnd();
162 itr.Advance()) {
163 if (omit_binary_values_ && itr.value().type() == Value::Type::BINARY) {
164 continue;
165 }
166
167 if (first_value_has_been_output) {
168 json_string_->push_back(',');
169 if (pretty_print_)
170 json_string_->append(kPrettyPrintLineEnding);
171 }
172
173 if (pretty_print_)
174 IndentLine(depth + 1U);
175
176 EscapeJSONString(itr.key(), true, json_string_);
177 json_string_->push_back(':');
178 if (pretty_print_)
179 json_string_->push_back(' ');
180
181 if (!BuildJSONString(itr.value(), depth + 1U))
182 result = false;
183
184 first_value_has_been_output = true;
185 }
186
187 if (pretty_print_) {
188 json_string_->append(kPrettyPrintLineEnding);
189 IndentLine(depth);
190 }
191
192 json_string_->push_back('}');
193 return result;
194 }
195
196 case Value::Type::BINARY:
197 // Successful only if we're allowed to omit it.
198 DLOG_IF(ERROR, !omit_binary_values_) << "Cannot serialize binary value.";
199 return omit_binary_values_;
200 }
201 NOTREACHED();
202 return false;
203 }
204
IndentLine(size_t depth)205 void JSONWriter::IndentLine(size_t depth) {
206 json_string_->append(depth * 3U, ' ');
207 }
208
209 } // namespace base
210