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 // This option instructs the writer to write doubles that have no fractional |
| 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 |
| 33 // 64-bit int. |
| 34 OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION = 1 << 2 |
29 }; | 35 }; |
30 | 36 |
31 // 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|. |
32 // If |pretty_print| is true, return a slightly nicer formated json string | 38 // If |pretty_print| is true, return a slightly nicer formated json string |
33 // (pads with whitespace to help readability). If |pretty_print| is false, | 39 // (pads with whitespace to help readability). If |pretty_print| is false, |
34 // we try to generate as compact a string as possible. | 40 // 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., | 41 // 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 | 42 // |node| is not a DictionaryValue/ListValue or if there are inf/-inf float |
37 // values)? | 43 // values)? |
38 static void Write(const Value* const node, bool pretty_print, | 44 static void Write(const Value* const node, bool pretty_print, |
39 std::string* json); | 45 std::string* json); |
40 | 46 |
41 // 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 |
42 // bitwise ORed together. | 48 // bitwise ORed together. |
43 static void WriteWithOptions(const Value* const node, bool pretty_print, | 49 static void WriteWithOptions(const Value* const node, bool pretty_print, |
44 int options, std::string* json); | 50 int options, std::string* json); |
45 | 51 |
46 // A static, constant JSON string representing an empty array. Useful | 52 // A static, constant JSON string representing an empty array. Useful |
47 // for empty JSON argument passing. | 53 // for empty JSON argument passing. |
48 static const char* kEmptyArray; | 54 static const char* kEmptyArray; |
49 | 55 |
50 private: | 56 private: |
51 JSONWriter(bool pretty_print, std::string* json); | 57 JSONWriter(bool pretty_print, std::string* json); |
52 | 58 |
53 // Called recursively to build the JSON string. Whe completed, value is | 59 // Called recursively to build the JSON string. Whe completed, value is |
54 // json_string_ will contain the JSON. | 60 // json_string_ will contain the JSON. |
55 void BuildJSONString(const Value* const node, int depth, bool escape, | 61 void BuildJSONString(const Value* const node, int depth, bool escape, |
56 bool ignore_binary_values); | 62 bool ignore_binary_values, |
| 63 bool omit_double_type_preservation); |
57 | 64 |
58 // 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_. |
59 void AppendQuotedString(const std::string& str); | 66 void AppendQuotedString(const std::string& str); |
60 | 67 |
61 // Adds space to json_string_ for the indent level. | 68 // Adds space to json_string_ for the indent level. |
62 void IndentLine(int depth); | 69 void IndentLine(int depth); |
63 | 70 |
64 // Where we write JSON data as we generate it. | 71 // Where we write JSON data as we generate it. |
65 std::string* json_string_; | 72 std::string* json_string_; |
66 | 73 |
67 bool pretty_print_; | 74 bool pretty_print_; |
68 | 75 |
69 DISALLOW_COPY_AND_ASSIGN(JSONWriter); | 76 DISALLOW_COPY_AND_ASSIGN(JSONWriter); |
70 }; | 77 }; |
71 | 78 |
72 } // namespace base | 79 } // namespace base |
73 | 80 |
74 #endif // BASE_JSON_JSON_WRITER_H_ | 81 #endif // BASE_JSON_JSON_WRITER_H_ |
OLD | NEW |