• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef BASE_JSON_JSON_WRITER_H_
6 #define BASE_JSON_JSON_WRITER_H_
7 
8 #include <stddef.h>
9 #include <cstdint>
10 #include <string>
11 
12 #include "base/base_export.h"
13 #include "base/json/json_common.h"
14 #include "base/memory/raw_ptr.h"
15 #include "base/strings/string_piece.h"
16 #include "base/values.h"
17 #include "third_party/abseil-cpp/absl/types/optional.h"
18 
19 namespace base {
20 
21 enum JsonOptions {
22   // This option instructs the writer that if a Binary value is encountered,
23   // the value (and key if within a dictionary) will be omitted from the
24   // output, and success will be returned. Otherwise, if a binary value is
25   // encountered, failure will be returned.
26   OPTIONS_OMIT_BINARY_VALUES = 1 << 0,
27 
28   // This option instructs the writer to write doubles that have no fractional
29   // part as a normal integer (i.e., without using exponential notation
30   // or appending a '.0') as long as the value is within the range of a
31   // 64-bit int.
32   OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION = 1 << 1,
33 
34   // Return a slightly nicer formatted json string (pads with whitespace to
35   // help with readability).
36   OPTIONS_PRETTY_PRINT = 1 << 2,
37 };
38 
39 // Given a root node, generates and returns a JSON string.
40 //
41 // Returns `absl::nullopt` if
42 //    * the nesting depth exceeds `max_depth`, or
43 //    * the JSON contains binary values.
44 BASE_EXPORT absl::optional<std::string> WriteJson(
45     ValueView node,
46     size_t max_depth = internal::kAbsoluteMaxDepth);
47 
48 // Given a root node, generates and returns a JSON string.
49 // The string is formatted according to `options` which is a bitmask of
50 // `JsonOptions`.
51 //
52 // Returns `absl::nullopt` if
53 //    * the nesting depth exceeds `max_depth,` or
54 //    * the JSON contains binary values
55 //      (unless `JsonOptions::OPTIONS_OMIT_BINARY_VALUES` is passed).
56 BASE_EXPORT absl::optional<std::string> WriteJsonWithOptions(
57     ValueView node,
58     uint32_t options,
59     size_t max_depth = internal::kAbsoluteMaxDepth);
60 
61 class BASE_EXPORT JSONWriter {
62  public:
63   using Options = JsonOptions;
64   // TODO: Once we support c++20 we replace these with
65   // `using enum ::JsonOptions`.
66   static constexpr auto OPTIONS_OMIT_BINARY_VALUES =
67       JsonOptions::OPTIONS_OMIT_BINARY_VALUES;
68   static constexpr auto OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION =
69       JsonOptions::OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION;
70   static constexpr auto OPTIONS_PRETTY_PRINT =
71       JsonOptions::OPTIONS_PRETTY_PRINT;
72 
73   JSONWriter(const JSONWriter&) = delete;
74   JSONWriter& operator=(const JSONWriter&) = delete;
75 
76   // Given a root node, generates a JSON string and puts it into |json|.
77   // The output string is overwritten and not appended.
78   //
79   // TODO(tc): Should we generate json if it would be invalid json (e.g.,
80   // |node| is not a dictionary/list Value or if there are inf/-inf float
81   // values)? Return true on success and false on failure.
82   //
83   // Deprecated: use the standalone method `WriteJson()` instead.
84   static bool Write(ValueView node,
85                     std::string* json,
86                     size_t max_depth = internal::kAbsoluteMaxDepth);
87 
88   // Same as above but with |options| which is a bunch of JSONWriter::Options
89   // bitwise ORed together. Return true on success and false on failure.
90   //
91   // Deprecated: use the standalone method `WriteJsonWithOptions()` instead.
92   static bool WriteWithOptions(ValueView node,
93                                int options,
94                                std::string* json,
95                                size_t max_depth = internal::kAbsoluteMaxDepth);
96 
97  private:
98   JSONWriter(int options,
99              std::string* json,
100              size_t max_depth = internal::kAbsoluteMaxDepth);
101 
102   // Called recursively to build the JSON string. When completed,
103   // |json_string_| will contain the JSON.
104   bool BuildJSONString(absl::monostate node, size_t depth);
105   bool BuildJSONString(bool node, size_t depth);
106   bool BuildJSONString(int node, size_t depth);
107   bool BuildJSONString(double node, size_t depth);
108   bool BuildJSONString(StringPiece node, size_t depth);
109   bool BuildJSONString(const Value::BlobStorage& node, size_t depth);
110   bool BuildJSONString(const Value::Dict& node, size_t depth);
111   bool BuildJSONString(const Value::List& node, size_t depth);
112 
113   // Adds space to json_string_ for the indent level.
114   void IndentLine(size_t depth);
115 
116   bool omit_binary_values_;
117   bool omit_double_type_preservation_;
118   bool pretty_print_;
119 
120   // Where we write JSON data as we generate it.
121   raw_ptr<std::string> json_string_;
122 
123   // Maximum depth to write.
124   const size_t max_depth_;
125 
126   // The number of times the writer has recursed (current stack depth).
127   size_t stack_depth_;
128 };
129 
130 }  // namespace base
131 
132 #endif  // BASE_JSON_JSON_WRITER_H_
133