1 // Copyright 2012 The Chromium Authors
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/numerics/safe_conversions.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "base/values.h"
17 #include "build/build_config.h"
18
19 namespace base {
20
21 #if BUILDFLAG(IS_WIN)
22 const char kPrettyPrintLineEnding[] = "\r\n";
23 #else
24 const char kPrettyPrintLineEnding[] = "\n";
25 #endif
26
27 // static
Write(ValueView node,std::string * json,size_t max_depth)28 bool JSONWriter::Write(ValueView node, std::string* json, size_t max_depth) {
29 return WriteWithOptions(node, 0, json, max_depth);
30 }
31
32 // static
WriteWithOptions(ValueView node,int options,std::string * json,size_t max_depth)33 bool JSONWriter::WriteWithOptions(ValueView node,
34 int options,
35 std::string* json,
36 size_t max_depth) {
37 json->clear();
38 // Is there a better way to estimate the size of the output?
39 if (json->capacity() < 1024) {
40 json->reserve(1024);
41 }
42
43 JSONWriter writer(options, json, max_depth);
44 bool result = node.Visit([&writer](const auto& member) {
45 return writer.BuildJSONString(member, 0);
46 });
47
48 if (options & OPTIONS_PRETTY_PRINT) {
49 json->append(kPrettyPrintLineEnding);
50 }
51
52 return result;
53 }
54
JSONWriter(int options,std::string * json,size_t max_depth)55 JSONWriter::JSONWriter(int options, std::string* json, size_t max_depth)
56 : omit_binary_values_((options & OPTIONS_OMIT_BINARY_VALUES) != 0),
57 omit_double_type_preservation_(
58 (options & OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION) != 0),
59 pretty_print_((options & OPTIONS_PRETTY_PRINT) != 0),
60 json_string_(json),
61 max_depth_(max_depth),
62 stack_depth_(0) {
63 DCHECK(json);
64 CHECK_LE(max_depth, internal::kAbsoluteMaxDepth);
65 }
66
BuildJSONString(absl::monostate node,size_t depth)67 bool JSONWriter::BuildJSONString(absl::monostate node, size_t depth) {
68 json_string_->append("null");
69 return true;
70 }
71
BuildJSONString(bool node,size_t depth)72 bool JSONWriter::BuildJSONString(bool node, size_t depth) {
73 json_string_->append(node ? "true" : "false");
74 return true;
75 }
76
BuildJSONString(int node,size_t depth)77 bool JSONWriter::BuildJSONString(int node, size_t depth) {
78 json_string_->append(NumberToString(node));
79 return true;
80 }
81
BuildJSONString(double node,size_t depth)82 bool JSONWriter::BuildJSONString(double node, size_t depth) {
83 if (omit_double_type_preservation_ &&
84 IsValueInRangeForNumericType<int64_t>(node) && std::floor(node) == node) {
85 json_string_->append(NumberToString(static_cast<int64_t>(node)));
86 return true;
87 }
88
89 std::string real = NumberToString(node);
90 // Ensure that the number has a .0 if there's no decimal or 'e'. This
91 // makes sure that when we read the JSON back, it's interpreted as a
92 // real rather than an int.
93 if (real.find_first_of(".eE") == std::string::npos) {
94 real.append(".0");
95 }
96
97 // The JSON spec requires that non-integer values in the range (-1,1)
98 // have a zero before the decimal point - ".52" is not valid, "0.52" is.
99 if (real[0] == '.') {
100 real.insert(0, 1, '0');
101 } else if (real.length() > 1 && real[0] == '-' && real[1] == '.') {
102 // "-.1" bad "-0.1" good
103 real.insert(1, 1, '0');
104 }
105 json_string_->append(real);
106 return true;
107 }
108
BuildJSONString(StringPiece node,size_t depth)109 bool JSONWriter::BuildJSONString(StringPiece node, size_t depth) {
110 EscapeJSONString(node, true, json_string_);
111 return true;
112 }
113
BuildJSONString(const Value::BlobStorage & node,size_t depth)114 bool JSONWriter::BuildJSONString(const Value::BlobStorage& node, size_t depth) {
115 // Successful only if we're allowed to omit it.
116 DLOG_IF(ERROR, !omit_binary_values_) << "Cannot serialize binary value.";
117 return omit_binary_values_;
118 }
119
BuildJSONString(const Value::Dict & node,size_t depth)120 bool JSONWriter::BuildJSONString(const Value::Dict& node, size_t depth) {
121 internal::StackMarker depth_check(max_depth_, &stack_depth_);
122
123 if (depth_check.IsTooDeep()) {
124 return false;
125 }
126
127 json_string_->push_back('{');
128 if (pretty_print_) {
129 json_string_->append(kPrettyPrintLineEnding);
130 }
131
132 bool first_value_has_been_output = false;
133 bool result = true;
134 for (const auto [key, value] : node) {
135 if (omit_binary_values_ && value.type() == Value::Type::BINARY) {
136 continue;
137 }
138
139 if (first_value_has_been_output) {
140 json_string_->push_back(',');
141 if (pretty_print_) {
142 json_string_->append(kPrettyPrintLineEnding);
143 }
144 }
145
146 if (pretty_print_) {
147 IndentLine(depth + 1U);
148 }
149
150 EscapeJSONString(key, true, json_string_);
151 json_string_->push_back(':');
152 if (pretty_print_) {
153 json_string_->push_back(' ');
154 }
155
156 result &= value.Visit([this, depth = depth + 1](const auto& member) {
157 return BuildJSONString(member, depth);
158 });
159
160 first_value_has_been_output = true;
161 }
162
163 if (pretty_print_) {
164 if (first_value_has_been_output) {
165 json_string_->append(kPrettyPrintLineEnding);
166 }
167 IndentLine(depth);
168 }
169
170 json_string_->push_back('}');
171 return result;
172 }
173
BuildJSONString(const Value::List & node,size_t depth)174 bool JSONWriter::BuildJSONString(const Value::List& node, size_t depth) {
175 internal::StackMarker depth_check(max_depth_, &stack_depth_);
176
177 if (depth_check.IsTooDeep()) {
178 return false;
179 }
180
181 json_string_->push_back('[');
182 if (pretty_print_) {
183 json_string_->push_back(' ');
184 }
185
186 bool first_value_has_been_output = false;
187 bool result = true;
188 for (const auto& value : node) {
189 if (omit_binary_values_ && value.type() == Value::Type::BINARY) {
190 continue;
191 }
192
193 if (first_value_has_been_output) {
194 json_string_->push_back(',');
195 if (pretty_print_) {
196 json_string_->push_back(' ');
197 }
198 }
199
200 result &= value.Visit([this, depth](const auto& member) {
201 return BuildJSONString(member, depth);
202 });
203
204 first_value_has_been_output = true;
205 }
206
207 if (pretty_print_) {
208 json_string_->push_back(' ');
209 }
210 json_string_->push_back(']');
211 return result;
212 }
213
IndentLine(size_t depth)214 void JSONWriter::IndentLine(size_t depth) {
215 json_string_->append(depth * 3U, ' ');
216 }
217
WriteJson(ValueView node,size_t max_depth)218 absl::optional<std::string> WriteJson(ValueView node, size_t max_depth) {
219 std::string result;
220 if (!JSONWriter::Write(node, &result, max_depth)) {
221 return absl::nullopt;
222 }
223 return result;
224 }
225
WriteJsonWithOptions(ValueView node,uint32_t options,size_t max_depth)226 absl::optional<std::string> WriteJsonWithOptions(ValueView node,
227 uint32_t options,
228 size_t max_depth) {
229 std::string result;
230 if (!JSONWriter::WriteWithOptions(node, static_cast<int>(options), &result,
231 max_depth)) {
232 return absl::nullopt;
233 }
234 return result;
235 }
236
237 } // namespace base
238