OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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_WRITER_H_ | 5 #ifndef BASE_JSON_WRITER_H_ |
6 #define BASE_JSON_WRITER_H_ | 6 #define BASE_JSON_WRITER_H_ |
7 | 7 |
8 #include <string> | |
9 | |
10 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/string16.h" |
11 | 10 |
12 class Value; | 11 class Value; |
13 | 12 |
14 class JSONWriter { | 13 class JSONWriter { |
15 public: | 14 public: |
16 // Given a root node, generates a JSON string and puts it into |json|. | 15 // Given a root node, generates a JSON string and puts it into |json|. |
17 // If |pretty_print| is true, return a slightly nicer formated json string | 16 // If |pretty_print| is true, return a slightly nicer formated json string |
18 // (pads with whitespace to help readability). If |pretty_print| is false, | 17 // (pads with whitespace to help readability). If |pretty_print| is false, |
19 // we try to generate as compact a string as possible. | 18 // we try to generate as compact a string as possible. |
20 // TODO(tc): Should we generate json if it would be invalid json (e.g., | 19 // TODO(tc): Should we generate json if it would be invalid json (e.g., |
21 // |node| is not a DictionaryValue/ListValue or if there are inf/-inf float | 20 // |node| is not a DictionaryValue/ListValue or if there are inf/-inf float |
22 // values)? | 21 // values)? |
23 static void Write(const Value* const node, bool pretty_print, | 22 static void Write(const Value* const node, bool pretty_print, |
24 std::string* json); | 23 std::string* json); |
25 | 24 |
26 private: | 25 private: |
27 JSONWriter(bool pretty_print, std::string* json); | 26 JSONWriter(bool pretty_print, std::string* json); |
28 | 27 |
29 // Called recursively to build the JSON string. Whe completed, value is | 28 // Called recursively to build the JSON string. Whe completed, value is |
30 // json_string_ will contain the JSON. | 29 // json_string_ will contain the JSON. |
31 void BuildJSONString(const Value* const node, int depth); | 30 void BuildJSONString(const Value* const node, int depth); |
32 | 31 |
33 // Appends a quoted, escaped, version of str to json_string_. | 32 // Appends a quoted, escaped, version of str to json_string_. |
34 void AppendQuotedString(const std::wstring& str); | 33 void AppendQuotedString(const string16& str); |
35 | 34 |
36 // Adds space to json_string_ for the indent level. | 35 // Adds space to json_string_ for the indent level. |
37 void IndentLine(int depth); | 36 void IndentLine(int depth); |
38 | 37 |
39 // Where we write JSON data as we generate it. | 38 // Where we write JSON data as we generate it. |
40 std::string* json_string_; | 39 std::string* json_string_; |
41 | 40 |
42 bool pretty_print_; | 41 bool pretty_print_; |
43 | 42 |
44 DISALLOW_COPY_AND_ASSIGN(JSONWriter); | 43 DISALLOW_COPY_AND_ASSIGN(JSONWriter); |
45 }; | 44 }; |
46 | 45 |
47 #endif // BASE_JSON_WRITER_H_ | 46 #endif // BASE_JSON_WRITER_H_ |
48 | |
OLD | NEW |