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