Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(283)

Side by Side Diff: base/json/json_writer.h

Issue 100823007: Stop doing unnecessary UTF-8 to UTF-16 conversions in JSONWriter. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix ChromeOS page encodings Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/json/json_value_serializer_unittest.cc ('k') | base/json/json_writer.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 7
8 #include <string> 8 #include <string>
9 9
10 #include "base/base_export.h" 10 #include "base/base_export.h"
11 #include "base/basictypes.h" 11 #include "base/basictypes.h"
12 12
13 namespace base { 13 namespace base {
14 14
15 class Value; 15 class Value;
16 16
17 class BASE_EXPORT JSONWriter { 17 class BASE_EXPORT JSONWriter {
18 public: 18 public:
19 enum Options { 19 enum Options {
20 // Do not escape the string, preserving its UTF8 characters. It is useful
21 // if you can pass the resulting string to the JSON parser in binary form
22 // (as UTF8).
23 OPTIONS_DO_NOT_ESCAPE = 1 << 0,
24
25 // For values of binary type, the value (and key if within a dictionary) 20 // For values of binary type, the value (and key if within a dictionary)
26 // will be omitted from the output. 21 // will be omitted from the output.
27 OPTIONS_OMIT_BINARY_VALUES = 1 << 1, 22 OPTIONS_OMIT_BINARY_VALUES = 1 << 0,
28 23
29 // This option instructs the writer to write doubles that have no fractional 24 // This option instructs the writer to write doubles that have no fractional
30 // part as a normal integer (i.e., without using exponential notation 25 // part as a normal integer (i.e., without using exponential notation
31 // or appending a '.0') as long as the value is within the range of a 26 // or appending a '.0') as long as the value is within the range of a
32 // 64-bit int. 27 // 64-bit int.
33 OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION = 1 << 2, 28 OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION = 1 << 1,
34 29
35 // Return a slightly nicer formatted json string (pads with whitespace to 30 // Return a slightly nicer formatted json string (pads with whitespace to
36 // help with readability). 31 // help with readability).
37 OPTIONS_PRETTY_PRINT = 1 << 3 32 OPTIONS_PRETTY_PRINT = 1 << 2,
38 }; 33 };
39 34
40 // Given a root node, generates a JSON string and puts it into |json|. 35 // Given a root node, generates a JSON string and puts it into |json|.
41 // TODO(tc): Should we generate json if it would be invalid json (e.g., 36 // 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 37 // |node| is not a DictionaryValue/ListValue or if there are inf/-inf float
43 // values)? 38 // values)?
44 static void Write(const Value* const node, std::string* json); 39 static void Write(const Value* const node, std::string* json);
45 40
46 // Same as above but with |options| which is a bunch of JSONWriter::Options 41 // Same as above but with |options| which is a bunch of JSONWriter::Options
47 // bitwise ORed together. 42 // bitwise ORed together.
48 static void WriteWithOptions(const Value* const node, int options, 43 static void WriteWithOptions(const Value* const node, int options,
49 std::string* json); 44 std::string* json);
50 45
51 // A static, constant JSON string representing an empty array. Useful
52 // for empty JSON argument passing.
53 static const char* kEmptyArray;
54
55 private: 46 private:
56 JSONWriter(bool escape, bool omit_binary_values, 47 JSONWriter(bool omit_binary_values,
57 bool omit_double_type_preservation, bool pretty_print, 48 bool omit_double_type_preservation, bool pretty_print,
58 std::string* json); 49 std::string* json);
59 50
60 // Called recursively to build the JSON string. Whe completed, value is 51 // Called recursively to build the JSON string. Whe completed, value is
61 // json_string_ will contain the JSON. 52 // json_string_ will contain the JSON.
62 void BuildJSONString(const Value* const node, int depth); 53 void BuildJSONString(const Value* const node, int depth);
63 54
64 // Appends a quoted, escaped, version of (UTF-8) str to json_string_.
65 void AppendQuotedString(const std::string& str);
66
67 // Adds space to json_string_ for the indent level. 55 // Adds space to json_string_ for the indent level.
68 void IndentLine(int depth); 56 void IndentLine(int depth);
69 57
70 bool escape_;
71 bool omit_binary_values_; 58 bool omit_binary_values_;
72 bool omit_double_type_preservation_; 59 bool omit_double_type_preservation_;
73 bool pretty_print_; 60 bool pretty_print_;
74 61
75 // Where we write JSON data as we generate it. 62 // Where we write JSON data as we generate it.
76 std::string* json_string_; 63 std::string* json_string_;
77 64
78 DISALLOW_COPY_AND_ASSIGN(JSONWriter); 65 DISALLOW_COPY_AND_ASSIGN(JSONWriter);
79 }; 66 };
80 67
81 } // namespace base 68 } // namespace base
82 69
83 #endif // BASE_JSON_JSON_WRITER_H_ 70 #endif // BASE_JSON_JSON_WRITER_H_
OLDNEW
« no previous file with comments | « base/json/json_value_serializer_unittest.cc ('k') | base/json/json_writer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698