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

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

Issue 8528051: Revert 110021 - Broke CrOS compile (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years, 1 month 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.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) 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 // For values of binary type, the value (and key if within a dictionary)
27 // will be omitted from the output.
28 OPTIONS_OMIT_BINARY_VALUES = 1 << 1
29 };
30
31 // Given a root node, generates a JSON string and puts it into |json|. 20 // 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 21 // If |pretty_print| is true, return a slightly nicer formated json string
33 // (pads with whitespace to help readability). If |pretty_print| is false, 22 // (pads with whitespace to help readability). If |pretty_print| is false,
34 // we try to generate as compact a string as possible. 23 // 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., 24 // 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 25 // |node| is not a DictionaryValue/ListValue or if there are inf/-inf float
37 // values)? 26 // values)?
38 static void Write(const Value* const node, bool pretty_print, 27 static void Write(const Value* const node, bool pretty_print,
39 std::string* json); 28 std::string* json);
40 29
41 // Same as above but with |options| which is a bunch of JSONWriter::Options 30 // Same as above, but has an option to not escape the string, preserving its
42 // bitwise ORed together. 31 // UTF8 characters. It is useful if you can pass resulting string to the
43 static void WriteWithOptions(const Value* const node, bool pretty_print, 32 // JSON parser in binary form (as UTF8).
44 int options, std::string* json); 33 static void WriteWithOptionalEscape(const Value* const node,
34 bool pretty_print,
35 bool escape,
36 std::string* json);
45 37
46 // A static, constant JSON string representing an empty array. Useful 38 // A static, constant JSON string representing an empty array. Useful
47 // for empty JSON argument passing. 39 // for empty JSON argument passing.
48 static const char* kEmptyArray; 40 static const char* kEmptyArray;
49 41
50 private: 42 private:
51 JSONWriter(bool pretty_print, std::string* json); 43 JSONWriter(bool pretty_print, std::string* json);
52 44
53 // Called recursively to build the JSON string. Whe completed, value is 45 // Called recursively to build the JSON string. Whe completed, value is
54 // json_string_ will contain the JSON. 46 // json_string_ will contain the JSON.
55 void BuildJSONString(const Value* const node, int depth, bool escape, 47 void BuildJSONString(const Value* const node, int depth, bool escape);
56 bool ignore_binary_values);
57 48
58 // Appends a quoted, escaped, version of (UTF-8) str to json_string_. 49 // Appends a quoted, escaped, version of (UTF-8) str to json_string_.
59 void AppendQuotedString(const std::string& str); 50 void AppendQuotedString(const std::string& str);
60 51
61 // Adds space to json_string_ for the indent level. 52 // Adds space to json_string_ for the indent level.
62 void IndentLine(int depth); 53 void IndentLine(int depth);
63 54
64 // Where we write JSON data as we generate it. 55 // Where we write JSON data as we generate it.
65 std::string* json_string_; 56 std::string* json_string_;
66 57
67 bool pretty_print_; 58 bool pretty_print_;
68 59
69 DISALLOW_COPY_AND_ASSIGN(JSONWriter); 60 DISALLOW_COPY_AND_ASSIGN(JSONWriter);
70 }; 61 };
71 62
72 } // namespace base 63 } // namespace base
73 64
74 #endif // BASE_JSON_JSON_WRITER_H_ 65 #endif // BASE_JSON_JSON_WRITER_H_
OLDNEW
« no previous file with comments | « base/json/json_value_serializer.cc ('k') | base/json/json_writer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698