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