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 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
(...skipping 13 matching lines...) Expand all Loading... |
24 OPTIONS_DO_NOT_ESCAPE = 1 << 0, | 24 OPTIONS_DO_NOT_ESCAPE = 1 << 0, |
25 | 25 |
26 // For values of binary type, the value (and key if within a dictionary) | 26 // For values of binary type, the value (and key if within a dictionary) |
27 // will be omitted from the output. | 27 // will be omitted from the output. |
28 OPTIONS_OMIT_BINARY_VALUES = 1 << 1, | 28 OPTIONS_OMIT_BINARY_VALUES = 1 << 1, |
29 | 29 |
30 // This option instructs the writer to write doubles that have no fractional | 30 // This option instructs the writer to write doubles that have no fractional |
31 // part as a normal integer (i.e., without using exponential notation | 31 // part as a normal integer (i.e., without using exponential notation |
32 // or appending a '.0') as long as the value is within the range of a | 32 // or appending a '.0') as long as the value is within the range of a |
33 // 64-bit int. | 33 // 64-bit int. |
34 OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION = 1 << 2 | 34 OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION = 1 << 2, |
| 35 |
| 36 // Return a slightly nicer formatted json string (pads with whitespace to |
| 37 // help with readability). |
| 38 OPTIONS_PRETTY_PRINT = 1 << 3 |
35 }; | 39 }; |
36 | 40 |
37 // Given a root node, generates a JSON string and puts it into |json|. | 41 // Given a root node, generates a JSON string and puts it into |json|. |
38 // If |pretty_print| is true, return a slightly nicer formated json string | |
39 // (pads with whitespace to help readability). If |pretty_print| is false, | |
40 // we try to generate as compact a string as possible. | |
41 // TODO(tc): Should we generate json if it would be invalid json (e.g., | 42 // TODO(tc): Should we generate json if it would be invalid json (e.g., |
42 // |node| is not a DictionaryValue/ListValue or if there are inf/-inf float | 43 // |node| is not a DictionaryValue/ListValue or if there are inf/-inf float |
43 // values)? | 44 // values)? |
44 static void Write(const Value* const node, bool pretty_print, | 45 static void Write(const Value* const node, std::string* json); |
45 std::string* json); | |
46 | 46 |
47 // Same as above but with |options| which is a bunch of JSONWriter::Options | 47 // Same as above but with |options| which is a bunch of JSONWriter::Options |
48 // bitwise ORed together. | 48 // bitwise ORed together. |
49 static void WriteWithOptions(const Value* const node, bool pretty_print, | 49 static void WriteWithOptions(const Value* const node, int options, |
50 int options, std::string* json); | 50 std::string* json); |
51 | 51 |
52 // A static, constant JSON string representing an empty array. Useful | 52 // A static, constant JSON string representing an empty array. Useful |
53 // for empty JSON argument passing. | 53 // for empty JSON argument passing. |
54 static const char* kEmptyArray; | 54 static const char* kEmptyArray; |
55 | 55 |
56 private: | 56 private: |
57 JSONWriter(bool pretty_print, std::string* json); | 57 JSONWriter(bool escape, bool omit_binary_values, |
| 58 bool omit_double_type_preservation, bool pretty_print, |
| 59 std::string* json); |
58 | 60 |
59 // Called recursively to build the JSON string. Whe completed, value is | 61 // Called recursively to build the JSON string. Whe completed, value is |
60 // json_string_ will contain the JSON. | 62 // json_string_ will contain the JSON. |
61 void BuildJSONString(const Value* const node, int depth, bool escape, | 63 void BuildJSONString(const Value* const node, int depth); |
62 bool ignore_binary_values, | |
63 bool omit_double_type_preservation); | |
64 | 64 |
65 // Appends a quoted, escaped, version of (UTF-8) str to json_string_. | 65 // Appends a quoted, escaped, version of (UTF-8) str to json_string_. |
66 void AppendQuotedString(const std::string& str); | 66 void AppendQuotedString(const std::string& str); |
67 | 67 |
68 // Adds space to json_string_ for the indent level. | 68 // Adds space to json_string_ for the indent level. |
69 void IndentLine(int depth); | 69 void IndentLine(int depth); |
70 | 70 |
| 71 bool escape_; |
| 72 bool omit_binary_values_; |
| 73 bool omit_double_type_preservation_; |
| 74 bool pretty_print_; |
| 75 |
71 // Where we write JSON data as we generate it. | 76 // Where we write JSON data as we generate it. |
72 std::string* json_string_; | 77 std::string* json_string_; |
73 | 78 |
74 bool pretty_print_; | |
75 | |
76 DISALLOW_COPY_AND_ASSIGN(JSONWriter); | 79 DISALLOW_COPY_AND_ASSIGN(JSONWriter); |
77 }; | 80 }; |
78 | 81 |
79 } // namespace base | 82 } // namespace base |
80 | 83 |
81 #endif // BASE_JSON_JSON_WRITER_H_ | 84 #endif // BASE_JSON_JSON_WRITER_H_ |
OLD | NEW |