xref: /aosp_15_r20/external/libchrome/base/json/json_writer.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_JSON_JSON_WRITER_H_
6*635a8641SAndroid Build Coastguard Worker #define BASE_JSON_JSON_WRITER_H_
7*635a8641SAndroid Build Coastguard Worker 
8*635a8641SAndroid Build Coastguard Worker #include <stddef.h>
9*635a8641SAndroid Build Coastguard Worker 
10*635a8641SAndroid Build Coastguard Worker #include <string>
11*635a8641SAndroid Build Coastguard Worker 
12*635a8641SAndroid Build Coastguard Worker #include "base/base_export.h"
13*635a8641SAndroid Build Coastguard Worker #include "base/macros.h"
14*635a8641SAndroid Build Coastguard Worker 
15*635a8641SAndroid Build Coastguard Worker namespace base {
16*635a8641SAndroid Build Coastguard Worker 
17*635a8641SAndroid Build Coastguard Worker class Value;
18*635a8641SAndroid Build Coastguard Worker 
19*635a8641SAndroid Build Coastguard Worker class BASE_EXPORT JSONWriter {
20*635a8641SAndroid Build Coastguard Worker  public:
21*635a8641SAndroid Build Coastguard Worker   enum Options {
22*635a8641SAndroid Build Coastguard Worker     // This option instructs the writer that if a Binary value is encountered,
23*635a8641SAndroid Build Coastguard Worker     // the value (and key if within a dictionary) will be omitted from the
24*635a8641SAndroid Build Coastguard Worker     // output, and success will be returned. Otherwise, if a binary value is
25*635a8641SAndroid Build Coastguard Worker     // encountered, failure will be returned.
26*635a8641SAndroid Build Coastguard Worker     OPTIONS_OMIT_BINARY_VALUES = 1 << 0,
27*635a8641SAndroid Build Coastguard Worker 
28*635a8641SAndroid Build Coastguard Worker     // This option instructs the writer to write doubles that have no fractional
29*635a8641SAndroid Build Coastguard Worker     // part as a normal integer (i.e., without using exponential notation
30*635a8641SAndroid Build Coastguard Worker     // or appending a '.0') as long as the value is within the range of a
31*635a8641SAndroid Build Coastguard Worker     // 64-bit int.
32*635a8641SAndroid Build Coastguard Worker     OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION = 1 << 1,
33*635a8641SAndroid Build Coastguard Worker 
34*635a8641SAndroid Build Coastguard Worker     // Return a slightly nicer formatted json string (pads with whitespace to
35*635a8641SAndroid Build Coastguard Worker     // help with readability).
36*635a8641SAndroid Build Coastguard Worker     OPTIONS_PRETTY_PRINT = 1 << 2,
37*635a8641SAndroid Build Coastguard Worker   };
38*635a8641SAndroid Build Coastguard Worker 
39*635a8641SAndroid Build Coastguard Worker   // Given a root node, generates a JSON string and puts it into |json|.
40*635a8641SAndroid Build Coastguard Worker   // The output string is overwritten and not appended.
41*635a8641SAndroid Build Coastguard Worker   //
42*635a8641SAndroid Build Coastguard Worker   // TODO(tc): Should we generate json if it would be invalid json (e.g.,
43*635a8641SAndroid Build Coastguard Worker   // |node| is not a DictionaryValue/ListValue or if there are inf/-inf float
44*635a8641SAndroid Build Coastguard Worker   // values)? Return true on success and false on failure.
45*635a8641SAndroid Build Coastguard Worker   static bool Write(const Value& node, std::string* json);
46*635a8641SAndroid Build Coastguard Worker 
47*635a8641SAndroid Build Coastguard Worker   // Same as above but with |options| which is a bunch of JSONWriter::Options
48*635a8641SAndroid Build Coastguard Worker   // bitwise ORed together. Return true on success and false on failure.
49*635a8641SAndroid Build Coastguard Worker   static bool WriteWithOptions(const Value& node,
50*635a8641SAndroid Build Coastguard Worker                                int options,
51*635a8641SAndroid Build Coastguard Worker                                std::string* json);
52*635a8641SAndroid Build Coastguard Worker 
53*635a8641SAndroid Build Coastguard Worker  private:
54*635a8641SAndroid Build Coastguard Worker   JSONWriter(int options, std::string* json);
55*635a8641SAndroid Build Coastguard Worker 
56*635a8641SAndroid Build Coastguard Worker   // Called recursively to build the JSON string. When completed,
57*635a8641SAndroid Build Coastguard Worker   // |json_string_| will contain the JSON.
58*635a8641SAndroid Build Coastguard Worker   bool BuildJSONString(const Value& node, size_t depth);
59*635a8641SAndroid Build Coastguard Worker 
60*635a8641SAndroid Build Coastguard Worker   // Adds space to json_string_ for the indent level.
61*635a8641SAndroid Build Coastguard Worker   void IndentLine(size_t depth);
62*635a8641SAndroid Build Coastguard Worker 
63*635a8641SAndroid Build Coastguard Worker   bool omit_binary_values_;
64*635a8641SAndroid Build Coastguard Worker   bool omit_double_type_preservation_;
65*635a8641SAndroid Build Coastguard Worker   bool pretty_print_;
66*635a8641SAndroid Build Coastguard Worker 
67*635a8641SAndroid Build Coastguard Worker   // Where we write JSON data as we generate it.
68*635a8641SAndroid Build Coastguard Worker   std::string* json_string_;
69*635a8641SAndroid Build Coastguard Worker 
70*635a8641SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(JSONWriter);
71*635a8641SAndroid Build Coastguard Worker };
72*635a8641SAndroid Build Coastguard Worker 
73*635a8641SAndroid Build Coastguard Worker }  // namespace base
74*635a8641SAndroid Build Coastguard Worker 
75*635a8641SAndroid Build Coastguard Worker #endif  // BASE_JSON_JSON_WRITER_H_
76