OLD | NEW |
---|---|
1 // Copyright (c) 2011 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 |
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 { | 20 enum Options { |
21 // Do not escape the string, preserving its UTF8 characters. It is useful | 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 | 22 // if you can pass the resulting string to the JSON parser in binary form |
23 // (as UTF8). | 23 // (as UTF8). |
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 | |
30 // An external consumer of your JSON may expect a 64-bit integer value. | |
31 // However, the |Value| classes only support 32-bit ints. One feasible | |
32 // way to mitigate this is to use a double to represent the integer. | |
33 // However, this class writes all doubles in such a way that causes many | |
34 // JSON readers to parse the number as a double instead of a 64-bit int. | |
35 // This may break the external consumer of the JSON if it expects a | |
36 // 64-bit int. | |
tony
2012/03/06 21:50:13
I would remove the text from line 30-36. It's suf
kkania
2012/03/06 22:41:57
Done.
| |
37 // This option instructs the writer to write doubles that have no fractional | |
38 // part as a normal integer (i.e., without using exponential notation | |
39 // or appending a '.0') as long as the value is within the range of a | |
40 // 64-bit int. | |
41 OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION = 1 << 2 | |
29 }; | 42 }; |
30 | 43 |
31 // Given a root node, generates a JSON string and puts it into |json|. | 44 // Given a root node, generates a JSON string and puts it into |json|. |
32 // If |pretty_print| is true, return a slightly nicer formated json string | 45 // If |pretty_print| is true, return a slightly nicer formated json string |
33 // (pads with whitespace to help readability). If |pretty_print| is false, | 46 // (pads with whitespace to help readability). If |pretty_print| is false, |
34 // we try to generate as compact a string as possible. | 47 // we try to generate as compact a string as possible. |
35 // TODO(tc): Should we generate json if it would be invalid json (e.g., | 48 // TODO(tc): Should we generate json if it would be invalid json (e.g., |
36 // |node| is not a DictionaryValue/ListValue or if there are inf/-inf float | 49 // |node| is not a DictionaryValue/ListValue or if there are inf/-inf float |
37 // values)? | 50 // values)? |
38 static void Write(const Value* const node, bool pretty_print, | 51 static void Write(const Value* const node, bool pretty_print, |
39 std::string* json); | 52 std::string* json); |
40 | 53 |
41 // Same as above but with |options| which is a bunch of JSONWriter::Options | 54 // Same as above but with |options| which is a bunch of JSONWriter::Options |
42 // bitwise ORed together. | 55 // bitwise ORed together. |
43 static void WriteWithOptions(const Value* const node, bool pretty_print, | 56 static void WriteWithOptions(const Value* const node, bool pretty_print, |
44 int options, std::string* json); | 57 int options, std::string* json); |
45 | 58 |
46 // A static, constant JSON string representing an empty array. Useful | 59 // A static, constant JSON string representing an empty array. Useful |
47 // for empty JSON argument passing. | 60 // for empty JSON argument passing. |
48 static const char* kEmptyArray; | 61 static const char* kEmptyArray; |
49 | 62 |
50 private: | 63 private: |
51 JSONWriter(bool pretty_print, std::string* json); | 64 JSONWriter(bool pretty_print, std::string* json); |
52 | 65 |
53 // Called recursively to build the JSON string. Whe completed, value is | 66 // Called recursively to build the JSON string. Whe completed, value is |
54 // json_string_ will contain the JSON. | 67 // json_string_ will contain the JSON. |
55 void BuildJSONString(const Value* const node, int depth, bool escape, | 68 void BuildJSONString(const Value* const node, int depth, bool escape, |
56 bool ignore_binary_values); | 69 bool ignore_binary_values, |
70 bool omit_double_type_preservation); | |
57 | 71 |
58 // Appends a quoted, escaped, version of (UTF-8) str to json_string_. | 72 // Appends a quoted, escaped, version of (UTF-8) str to json_string_. |
59 void AppendQuotedString(const std::string& str); | 73 void AppendQuotedString(const std::string& str); |
60 | 74 |
61 // Adds space to json_string_ for the indent level. | 75 // Adds space to json_string_ for the indent level. |
62 void IndentLine(int depth); | 76 void IndentLine(int depth); |
63 | 77 |
64 // Where we write JSON data as we generate it. | 78 // Where we write JSON data as we generate it. |
65 std::string* json_string_; | 79 std::string* json_string_; |
66 | 80 |
67 bool pretty_print_; | 81 bool pretty_print_; |
68 | 82 |
69 DISALLOW_COPY_AND_ASSIGN(JSONWriter); | 83 DISALLOW_COPY_AND_ASSIGN(JSONWriter); |
70 }; | 84 }; |
71 | 85 |
72 } // namespace base | 86 } // namespace base |
73 | 87 |
74 #endif // BASE_JSON_JSON_WRITER_H_ | 88 #endif // BASE_JSON_JSON_WRITER_H_ |
OLD | NEW |