OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 |
11 #include "base/base_export.h" | 11 #include "base/base_export.h" |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 | 13 |
14 namespace base { | 14 namespace base { |
15 | 15 |
16 class Value; | 16 class Value; |
17 | 17 |
18 class BASE_EXPORT JSONWriter { | 18 class BASE_EXPORT JSONWriter { |
19 public: | 19 public: |
20 enum Options { | |
21 // Do not escape the string, preserving its UTF8 characters. It is useful | |
22 // if you can pass the resulting string to the JSON parser in binary form | |
23 // (as UTF8). | |
24 OPTIONS_DO_NOT_ESCAPE = 1 << 0, | |
25 | |
26 // Ignore values of binary type. They will be omitted from the output. | |
Jói
2011/11/10 11:08:16
This is slightly vague: _values_ ... will be omit
Eric Dingle
2011/11/10 15:05:25
Done.
| |
27 OPTIONS_IGNORE_BINARY_VALUES = 1 << 1 | |
28 }; | |
29 | |
20 // Given a root node, generates a JSON string and puts it into |json|. | 30 // Given a root node, generates a JSON string and puts it into |json|. |
21 // If |pretty_print| is true, return a slightly nicer formated json string | 31 // If |pretty_print| is true, return a slightly nicer formated json string |
22 // (pads with whitespace to help readability). If |pretty_print| is false, | 32 // (pads with whitespace to help readability). If |pretty_print| is false, |
23 // we try to generate as compact a string as possible. | 33 // we try to generate as compact a string as possible. |
24 // TODO(tc): Should we generate json if it would be invalid json (e.g., | 34 // TODO(tc): Should we generate json if it would be invalid json (e.g., |
25 // |node| is not a DictionaryValue/ListValue or if there are inf/-inf float | 35 // |node| is not a DictionaryValue/ListValue or if there are inf/-inf float |
26 // values)? | 36 // values)? |
27 static void Write(const Value* const node, bool pretty_print, | 37 static void Write(const Value* const node, bool pretty_print, |
28 std::string* json); | 38 std::string* json); |
29 | 39 |
30 // Same as above, but has an option to not escape the string, preserving its | 40 // Same as above but with |options| which is a bunch of JSONWriter::Options |
31 // UTF8 characters. It is useful if you can pass resulting string to the | 41 // bitwise ORed together. |
32 // JSON parser in binary form (as UTF8). | 42 static void Write(const Value* const node, bool pretty_print, |
33 static void WriteWithOptionalEscape(const Value* const node, | 43 int options, std::string* json); |
34 bool pretty_print, | |
35 bool escape, | |
36 std::string* json); | |
37 | 44 |
38 // A static, constant JSON string representing an empty array. Useful | 45 // A static, constant JSON string representing an empty array. Useful |
39 // for empty JSON argument passing. | 46 // for empty JSON argument passing. |
40 static const char* kEmptyArray; | 47 static const char* kEmptyArray; |
41 | 48 |
42 private: | 49 private: |
43 JSONWriter(bool pretty_print, std::string* json); | 50 JSONWriter(bool pretty_print, std::string* json); |
44 | 51 |
45 // Called recursively to build the JSON string. Whe completed, value is | 52 // Called recursively to build the JSON string. Whe completed, value is |
46 // json_string_ will contain the JSON. | 53 // json_string_ will contain the JSON. |
47 void BuildJSONString(const Value* const node, int depth, bool escape); | 54 void BuildJSONString(const Value* const node, int depth, bool escape, |
55 bool ignore_binary_values); | |
48 | 56 |
49 // Appends a quoted, escaped, version of (UTF-8) str to json_string_. | 57 // Appends a quoted, escaped, version of (UTF-8) str to json_string_. |
50 void AppendQuotedString(const std::string& str); | 58 void AppendQuotedString(const std::string& str); |
51 | 59 |
52 // Adds space to json_string_ for the indent level. | 60 // Adds space to json_string_ for the indent level. |
53 void IndentLine(int depth); | 61 void IndentLine(int depth); |
54 | 62 |
55 // Where we write JSON data as we generate it. | 63 // Where we write JSON data as we generate it. |
56 std::string* json_string_; | 64 std::string* json_string_; |
57 | 65 |
58 bool pretty_print_; | 66 bool pretty_print_; |
59 | 67 |
60 DISALLOW_COPY_AND_ASSIGN(JSONWriter); | 68 DISALLOW_COPY_AND_ASSIGN(JSONWriter); |
61 }; | 69 }; |
62 | 70 |
63 } // namespace base | 71 } // namespace base |
64 | 72 |
65 #endif // BASE_JSON_JSON_WRITER_H_ | 73 #endif // BASE_JSON_JSON_WRITER_H_ |
OLD | NEW |