Roll WPR. (Remove old WPR code. Make WPR cert file name unique.)
[chromium-blink-merge.git] / base / json / json_writer.h
blob9709c7e723f05a25f3351607b73afc2c263e6f2e
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.
5 #ifndef BASE_JSON_JSON_WRITER_H_
6 #define BASE_JSON_JSON_WRITER_H_
8 #include <string>
10 #include "base/base_export.h"
11 #include "base/basictypes.h"
13 namespace base {
15 class Value;
17 class BASE_EXPORT JSONWriter {
18 public:
19 enum Options {
20 // This option instructs the writer that if a Binary value is encountered,
21 // the value (and key if within a dictionary) will be omitted from the
22 // output, and success will be returned. Otherwise, if a binary value is
23 // encountered, failure will be returned.
24 OPTIONS_OMIT_BINARY_VALUES = 1 << 0,
26 // This option instructs the writer to write doubles that have no fractional
27 // part as a normal integer (i.e., without using exponential notation
28 // or appending a '.0') as long as the value is within the range of a
29 // 64-bit int.
30 OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION = 1 << 1,
32 // Return a slightly nicer formatted json string (pads with whitespace to
33 // help with readability).
34 OPTIONS_PRETTY_PRINT = 1 << 2,
37 // Given a root node, generates a JSON string and puts it into |json|.
38 // TODO(tc): Should we generate json if it would be invalid json (e.g.,
39 // |node| is not a DictionaryValue/ListValue or if there are inf/-inf float
40 // values)? Return true on success and false on failure.
41 static bool Write(const Value* const node, std::string* json);
43 // Same as above but with |options| which is a bunch of JSONWriter::Options
44 // bitwise ORed together. Return true on success and false on failure.
45 static bool WriteWithOptions(const Value* const node, int options,
46 std::string* json);
48 private:
49 JSONWriter(int options, std::string* json);
51 // Called recursively to build the JSON string. When completed,
52 // |json_string_| will contain the JSON.
53 bool BuildJSONString(const Value* const node, size_t depth);
55 // Adds space to json_string_ for the indent level.
56 void IndentLine(size_t depth);
58 bool omit_binary_values_;
59 bool omit_double_type_preservation_;
60 bool pretty_print_;
62 // Where we write JSON data as we generate it.
63 std::string* json_string_;
65 DISALLOW_COPY_AND_ASSIGN(JSONWriter);
68 } // namespace base
70 #endif // BASE_JSON_JSON_WRITER_H_