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

Side by Side Diff: third_party/protobuf/src/google/protobuf/util/json_util.h

Issue 2495533002: third_party/protobuf: Update to HEAD (83d681ee2c) (Closed)
Patch Set: Make chrome settings proto generated file a component Created 4 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
OLDNEW
1 // Protocol Buffers - Google's data interchange format 1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved. 2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/ 3 // https://developers.google.com/protocol-buffers/
4 // 4 //
5 // Redistribution and use in source and binary forms, with or without 5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are 6 // modification, are permitted provided that the following conditions are
7 // met: 7 // met:
8 // 8 //
9 // * Redistributions of source code must retain the above copyright 9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer. 10 // notice, this list of conditions and the following disclaimer.
(...skipping 15 matching lines...) Expand all
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 30
31 // Utility functions to convert between protobuf binary format and proto3 JSON 31 // Utility functions to convert between protobuf binary format and proto3 JSON
32 // format. 32 // format.
33 #ifndef GOOGLE_PROTOBUF_UTIL_JSON_UTIL_H__ 33 #ifndef GOOGLE_PROTOBUF_UTIL_JSON_UTIL_H__
34 #define GOOGLE_PROTOBUF_UTIL_JSON_UTIL_H__ 34 #define GOOGLE_PROTOBUF_UTIL_JSON_UTIL_H__
35 35
36 #include <google/protobuf/message.h>
36 #include <google/protobuf/util/type_resolver.h> 37 #include <google/protobuf/util/type_resolver.h>
37 #include <google/protobuf/stubs/bytestream.h> 38 #include <google/protobuf/stubs/bytestream.h>
38 39
39 namespace google { 40 namespace google {
40 namespace protobuf { 41 namespace protobuf {
41 namespace io { 42 namespace io {
42 class ZeroCopyInputStream; 43 class ZeroCopyInputStream;
43 class ZeroCopyOutputStream; 44 class ZeroCopyOutputStream;
44 } // namespace io 45 } // namespace io
45 namespace util { 46 namespace util {
46 47
47 struct JsonOptions { 48 struct JsonParseOptions {
49 // Whether to ignore unknown JSON fields during parsing
50 bool ignore_unknown_fields;
51
52 JsonParseOptions() : ignore_unknown_fields(false) {}
53 };
54
55 struct JsonPrintOptions {
48 // Whether to add spaces, line breaks and indentation to make the JSON output 56 // Whether to add spaces, line breaks and indentation to make the JSON output
49 // easy to read. 57 // easy to read.
50 bool add_whitespace; 58 bool add_whitespace;
51 // Whether to always print primitive fields. By default primitive fields with 59 // Whether to always print primitive fields. By default primitive fields with
52 // default values will be omitted in JSON joutput. For example, an int32 field 60 // default values will be omitted in JSON joutput. For example, an int32 field
53 // set to 0 will be omitted. Set this flag to true will override the default 61 // set to 0 will be omitted. Set this flag to true will override the default
54 // behavior and print primitive fields regardless of their values. 62 // behavior and print primitive fields regardless of their values.
55 bool always_print_primitive_fields; 63 bool always_print_primitive_fields;
56 64
57 JsonOptions() : add_whitespace(false), 65 JsonPrintOptions() : add_whitespace(false),
58 always_print_primitive_fields(false) { 66 always_print_primitive_fields(false) {
59 } 67 }
60 }; 68 };
61 69
70 // DEPRECATED. Use JsonPrintOptions instead.
71 typedef JsonPrintOptions JsonOptions;
72
73 // Converts from protobuf message to JSON. This is a simple wrapper of
74 // BinaryToJsonString(). It will use the DescriptorPool of the passed-in
75 // message to resolve Any types.
76 LIBPROTOBUF_EXPORT util::Status MessageToJsonString(const Message& message,
77 string* output,
78 const JsonOptions& options);
79
80 inline util::Status MessageToJsonString(const Message& message,
81 string* output) {
82 return MessageToJsonString(message, output, JsonOptions());
83 }
84
85 // Converts from JSON to protobuf message. This is a simple wrapper of
86 // JsonStringToBinary(). It will use the DescriptorPool of the passed-in
87 // message to resolve Any types.
88 LIBPROTOBUF_EXPORT util::Status JsonStringToMessage(const string& input,
89 Message* message,
90 const JsonParseOptions& options);
91
92 inline util::Status JsonStringToMessage(const string& input,
93 Message* message) {
94 return JsonStringToMessage(input, message, JsonParseOptions());
95 }
96
62 // Converts protobuf binary data to JSON. 97 // Converts protobuf binary data to JSON.
63 // The conversion will fail if: 98 // The conversion will fail if:
64 // 1. TypeResolver fails to resolve a type. 99 // 1. TypeResolver fails to resolve a type.
65 // 2. input is not valid protobuf wire format, or conflicts with the type 100 // 2. input is not valid protobuf wire format, or conflicts with the type
66 // information returned by TypeResolver. 101 // information returned by TypeResolver.
67 // Note that unknown fields will be discarded silently. 102 // Note that unknown fields will be discarded silently.
68 util::Status BinaryToJsonStream( 103 LIBPROTOBUF_EXPORT util::Status BinaryToJsonStream(
69 TypeResolver* resolver, 104 TypeResolver* resolver,
70 const string& type_url, 105 const string& type_url,
71 io::ZeroCopyInputStream* binary_input, 106 io::ZeroCopyInputStream* binary_input,
72 io::ZeroCopyOutputStream* json_output, 107 io::ZeroCopyOutputStream* json_output,
73 const JsonOptions& options); 108 const JsonPrintOptions& options);
74 109
75 inline util::Status BinaryToJsonStream( 110 inline util::Status BinaryToJsonStream(
76 TypeResolver* resolver, const string& type_url, 111 TypeResolver* resolver, const string& type_url,
77 io::ZeroCopyInputStream* binary_input, 112 io::ZeroCopyInputStream* binary_input,
78 io::ZeroCopyOutputStream* json_output) { 113 io::ZeroCopyOutputStream* json_output) {
79 return BinaryToJsonStream(resolver, type_url, binary_input, json_output, 114 return BinaryToJsonStream(resolver, type_url, binary_input, json_output,
80 JsonOptions()); 115 JsonPrintOptions());
81 } 116 }
82 117
83 LIBPROTOBUF_EXPORT util::Status BinaryToJsonString( 118 LIBPROTOBUF_EXPORT util::Status BinaryToJsonString(
84 TypeResolver* resolver, 119 TypeResolver* resolver,
85 const string& type_url, 120 const string& type_url,
86 const string& binary_input, 121 const string& binary_input,
87 string* json_output, 122 string* json_output,
88 const JsonOptions& options); 123 const JsonPrintOptions& options);
89 124
90 inline util::Status BinaryToJsonString(TypeResolver* resolver, 125 inline util::Status BinaryToJsonString(TypeResolver* resolver,
91 const string& type_url, 126 const string& type_url,
92 const string& binary_input, 127 const string& binary_input,
93 string* json_output) { 128 string* json_output) {
94 return BinaryToJsonString(resolver, type_url, binary_input, json_output, 129 return BinaryToJsonString(resolver, type_url, binary_input, json_output,
95 JsonOptions()); 130 JsonPrintOptions());
96 } 131 }
97 132
98 // Converts JSON data to protobuf binary format. 133 // Converts JSON data to protobuf binary format.
99 // The conversion will fail if: 134 // The conversion will fail if:
100 // 1. TypeResolver fails to resolve a type. 135 // 1. TypeResolver fails to resolve a type.
101 // 2. input is not valid JSON format, or conflicts with the type 136 // 2. input is not valid JSON format, or conflicts with the type
102 // information returned by TypeResolver. 137 // information returned by TypeResolver.
103 // 3. input has unknown fields. 138 LIBPROTOBUF_EXPORT util::Status JsonToBinaryStream(
104 util::Status JsonToBinaryStream(
105 TypeResolver* resolver, 139 TypeResolver* resolver,
106 const string& type_url, 140 const string& type_url,
107 io::ZeroCopyInputStream* json_input, 141 io::ZeroCopyInputStream* json_input,
108 io::ZeroCopyOutputStream* binary_output); 142 io::ZeroCopyOutputStream* binary_output,
143 const JsonParseOptions& options);
144
145 inline util::Status JsonToBinaryStream(
146 TypeResolver* resolver,
147 const string& type_url,
148 io::ZeroCopyInputStream* json_input,
149 io::ZeroCopyOutputStream* binary_output) {
150 return JsonToBinaryStream(resolver, type_url, json_input, binary_output,
151 JsonParseOptions());
152 }
109 153
110 LIBPROTOBUF_EXPORT util::Status JsonToBinaryString( 154 LIBPROTOBUF_EXPORT util::Status JsonToBinaryString(
111 TypeResolver* resolver, 155 TypeResolver* resolver,
112 const string& type_url, 156 const string& type_url,
113 const string& json_input, 157 const string& json_input,
114 string* binary_output); 158 string* binary_output,
159 const JsonParseOptions& options);
160
161 inline util::Status JsonToBinaryString(
162 TypeResolver* resolver,
163 const string& type_url,
164 const string& json_input,
165 string* binary_output) {
166 return JsonToBinaryString(resolver, type_url, json_input, binary_output,
167 JsonParseOptions());
168 }
115 169
116 namespace internal { 170 namespace internal {
117 // Internal helper class. Put in the header so we can write unit-tests for it. 171 // Internal helper class. Put in the header so we can write unit-tests for it.
118 class LIBPROTOBUF_EXPORT ZeroCopyStreamByteSink : public strings::ByteSink { 172 class LIBPROTOBUF_EXPORT ZeroCopyStreamByteSink : public strings::ByteSink {
119 public: 173 public:
120 explicit ZeroCopyStreamByteSink(io::ZeroCopyOutputStream* stream) 174 explicit ZeroCopyStreamByteSink(io::ZeroCopyOutputStream* stream)
121 : stream_(stream) {} 175 : stream_(stream) {}
122 176
123 virtual void Append(const char* bytes, size_t len); 177 virtual void Append(const char* bytes, size_t len);
124 178
125 private: 179 private:
126 io::ZeroCopyOutputStream* stream_; 180 io::ZeroCopyOutputStream* stream_;
127 181
128 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ZeroCopyStreamByteSink); 182 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ZeroCopyStreamByteSink);
129 }; 183 };
130 } // namespace internal 184 } // namespace internal
131 185
132 } // namespace util 186 } // namespace util
133 } // namespace protobuf 187 } // namespace protobuf
134 188
135 } // namespace google 189 } // namespace google
136 #endif // GOOGLE_PROTOBUF_UTIL_JSON_UTIL_H__ 190 #endif // GOOGLE_PROTOBUF_UTIL_JSON_UTIL_H__
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698