1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
30
31 #include <google/protobuf/util/json_util.h>
32
33 #include <google/protobuf/stubs/common.h>
34 #include <google/protobuf/stubs/once.h>
35 #include <google/protobuf/stubs/status.h>
36 #include <google/protobuf/stubs/bytestream.h>
37 #include <google/protobuf/stubs/strutil.h>
38 #include <google/protobuf/io/coded_stream.h>
39 #include <google/protobuf/io/zero_copy_stream.h>
40 #include <google/protobuf/util/internal/default_value_objectwriter.h>
41 #include <google/protobuf/util/internal/error_listener.h>
42 #include <google/protobuf/util/internal/json_objectwriter.h>
43 #include <google/protobuf/util/internal/json_stream_parser.h>
44 #include <google/protobuf/util/internal/protostream_objectsource.h>
45 #include <google/protobuf/util/internal/protostream_objectwriter.h>
46 #include <google/protobuf/util/type_resolver.h>
47 #include <google/protobuf/util/type_resolver_util.h>
48 #include <google/protobuf/stubs/status_macros.h>
49
50 // clang-format off
51 #include <google/protobuf/port_def.inc>
52 // clang-format on
53
54 namespace google {
55 namespace protobuf {
56 namespace util {
57
58 namespace internal {
~ZeroCopyStreamByteSink()59 ZeroCopyStreamByteSink::~ZeroCopyStreamByteSink() {
60 if (buffer_size_ > 0) {
61 stream_->BackUp(buffer_size_);
62 }
63 }
64
Append(const char * bytes,size_t len)65 void ZeroCopyStreamByteSink::Append(const char* bytes, size_t len) {
66 while (true) {
67 if (len <= buffer_size_) { // NOLINT
68 memcpy(buffer_, bytes, len);
69 buffer_ = static_cast<char*>(buffer_) + len;
70 buffer_size_ -= len;
71 return;
72 }
73 if (buffer_size_ > 0) {
74 memcpy(buffer_, bytes, buffer_size_);
75 bytes += buffer_size_;
76 len -= buffer_size_;
77 }
78 if (!stream_->Next(&buffer_, &buffer_size_)) {
79 // There isn't a way for ByteSink to report errors.
80 buffer_size_ = 0;
81 return;
82 }
83 }
84 }
85 } // namespace internal
86
BinaryToJsonStream(TypeResolver * resolver,const std::string & type_url,io::ZeroCopyInputStream * binary_input,io::ZeroCopyOutputStream * json_output,const JsonPrintOptions & options)87 util::Status BinaryToJsonStream(TypeResolver* resolver,
88 const std::string& type_url,
89 io::ZeroCopyInputStream* binary_input,
90 io::ZeroCopyOutputStream* json_output,
91 const JsonPrintOptions& options) {
92 io::CodedInputStream in_stream(binary_input);
93 google::protobuf::Type type;
94 RETURN_IF_ERROR(resolver->ResolveMessageType(type_url, &type));
95 converter::ProtoStreamObjectSource::RenderOptions render_options;
96 render_options.use_ints_for_enums = options.always_print_enums_as_ints;
97 render_options.preserve_proto_field_names =
98 options.preserve_proto_field_names;
99 converter::ProtoStreamObjectSource proto_source(&in_stream, resolver, type,
100 render_options);
101 io::CodedOutputStream out_stream(json_output);
102 converter::JsonObjectWriter json_writer(options.add_whitespace ? " " : "",
103 &out_stream);
104 if (options.always_print_primitive_fields) {
105 converter::DefaultValueObjectWriter default_value_writer(resolver, type,
106 &json_writer);
107 default_value_writer.set_preserve_proto_field_names(
108 options.preserve_proto_field_names);
109 default_value_writer.set_print_enums_as_ints(
110 options.always_print_enums_as_ints);
111 return proto_source.WriteTo(&default_value_writer);
112 } else {
113 return proto_source.WriteTo(&json_writer);
114 }
115 }
116
BinaryToJsonString(TypeResolver * resolver,const std::string & type_url,const std::string & binary_input,std::string * json_output,const JsonPrintOptions & options)117 util::Status BinaryToJsonString(TypeResolver* resolver,
118 const std::string& type_url,
119 const std::string& binary_input,
120 std::string* json_output,
121 const JsonPrintOptions& options) {
122 io::ArrayInputStream input_stream(binary_input.data(), binary_input.size());
123 io::StringOutputStream output_stream(json_output);
124 return BinaryToJsonStream(resolver, type_url, &input_stream, &output_stream,
125 options);
126 }
127
128 namespace {
129 class StatusErrorListener : public converter::ErrorListener {
130 public:
StatusErrorListener()131 StatusErrorListener() {}
~StatusErrorListener()132 ~StatusErrorListener() override {}
133
GetStatus()134 util::Status GetStatus() { return status_; }
135
InvalidName(const converter::LocationTrackerInterface & loc,StringPiece unknown_name,StringPiece message)136 void InvalidName(const converter::LocationTrackerInterface& loc,
137 StringPiece unknown_name,
138 StringPiece message) override {
139 std::string loc_string = GetLocString(loc);
140 if (!loc_string.empty()) {
141 loc_string.append(" ");
142 }
143 status_ = util::InvalidArgumentError(
144 StrCat(loc_string, unknown_name, ": ", message));
145 }
146
InvalidValue(const converter::LocationTrackerInterface & loc,StringPiece type_name,StringPiece value)147 void InvalidValue(const converter::LocationTrackerInterface& loc,
148 StringPiece type_name,
149 StringPiece value) override {
150 status_ = util::InvalidArgumentError(
151 StrCat(GetLocString(loc), ": invalid value ", std::string(value),
152 " for type ", std::string(type_name)));
153 }
154
MissingField(const converter::LocationTrackerInterface & loc,StringPiece missing_name)155 void MissingField(const converter::LocationTrackerInterface& loc,
156 StringPiece missing_name) override {
157 status_ = util::InvalidArgumentError(StrCat(
158 GetLocString(loc), ": missing field ", std::string(missing_name)));
159 }
160
161 private:
162 util::Status status_;
163
GetLocString(const converter::LocationTrackerInterface & loc)164 std::string GetLocString(const converter::LocationTrackerInterface& loc) {
165 std::string loc_string = loc.ToString();
166 StripWhitespace(&loc_string);
167 if (!loc_string.empty()) {
168 loc_string = StrCat("(", loc_string, ")");
169 }
170 return loc_string;
171 }
172
173 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(StatusErrorListener);
174 };
175 } // namespace
176
JsonToBinaryStream(TypeResolver * resolver,const std::string & type_url,io::ZeroCopyInputStream * json_input,io::ZeroCopyOutputStream * binary_output,const JsonParseOptions & options)177 util::Status JsonToBinaryStream(TypeResolver* resolver,
178 const std::string& type_url,
179 io::ZeroCopyInputStream* json_input,
180 io::ZeroCopyOutputStream* binary_output,
181 const JsonParseOptions& options) {
182 google::protobuf::Type type;
183 RETURN_IF_ERROR(resolver->ResolveMessageType(type_url, &type));
184 internal::ZeroCopyStreamByteSink sink(binary_output);
185 StatusErrorListener listener;
186 converter::ProtoStreamObjectWriter::Options proto_writer_options;
187 proto_writer_options.ignore_unknown_fields = options.ignore_unknown_fields;
188 proto_writer_options.ignore_unknown_enum_values =
189 options.ignore_unknown_fields;
190 proto_writer_options.case_insensitive_enum_parsing =
191 options.case_insensitive_enum_parsing;
192 converter::ProtoStreamObjectWriter proto_writer(
193 resolver, type, &sink, &listener, proto_writer_options);
194
195 converter::JsonStreamParser parser(&proto_writer);
196 const void* buffer;
197 int length;
198 while (json_input->Next(&buffer, &length)) {
199 if (length == 0) continue;
200 RETURN_IF_ERROR(parser.Parse(
201 StringPiece(static_cast<const char*>(buffer), length)));
202 }
203 RETURN_IF_ERROR(parser.FinishParse());
204
205 return listener.GetStatus();
206 }
207
JsonToBinaryString(TypeResolver * resolver,const std::string & type_url,StringPiece json_input,std::string * binary_output,const JsonParseOptions & options)208 util::Status JsonToBinaryString(TypeResolver* resolver,
209 const std::string& type_url,
210 StringPiece json_input,
211 std::string* binary_output,
212 const JsonParseOptions& options) {
213 io::ArrayInputStream input_stream(json_input.data(), json_input.size());
214 io::StringOutputStream output_stream(binary_output);
215 return JsonToBinaryStream(resolver, type_url, &input_stream, &output_stream,
216 options);
217 }
218
219 namespace {
220 const char* kTypeUrlPrefix = "type.googleapis.com";
221 TypeResolver* generated_type_resolver_ = nullptr;
222 PROTOBUF_NAMESPACE_ID::internal::once_flag generated_type_resolver_init_;
223
GetTypeUrl(const Message & message)224 std::string GetTypeUrl(const Message& message) {
225 return std::string(kTypeUrlPrefix) + "/" +
226 message.GetDescriptor()->full_name();
227 }
228
DeleteGeneratedTypeResolver()229 void DeleteGeneratedTypeResolver() { // NOLINT
230 delete generated_type_resolver_;
231 }
232
InitGeneratedTypeResolver()233 void InitGeneratedTypeResolver() {
234 generated_type_resolver_ = NewTypeResolverForDescriptorPool(
235 kTypeUrlPrefix, DescriptorPool::generated_pool());
236 ::google::protobuf::internal::OnShutdown(&DeleteGeneratedTypeResolver);
237 }
238
GetGeneratedTypeResolver()239 TypeResolver* GetGeneratedTypeResolver() {
240 PROTOBUF_NAMESPACE_ID::internal::call_once(generated_type_resolver_init_,
241 InitGeneratedTypeResolver);
242 return generated_type_resolver_;
243 }
244 } // namespace
245
MessageToJsonString(const Message & message,std::string * output,const JsonOptions & options)246 util::Status MessageToJsonString(const Message& message, std::string* output,
247 const JsonOptions& options) {
248 const DescriptorPool* pool = message.GetDescriptor()->file()->pool();
249 TypeResolver* resolver =
250 pool == DescriptorPool::generated_pool()
251 ? GetGeneratedTypeResolver()
252 : NewTypeResolverForDescriptorPool(kTypeUrlPrefix, pool);
253 util::Status result =
254 BinaryToJsonString(resolver, GetTypeUrl(message),
255 message.SerializeAsString(), output, options);
256 if (pool != DescriptorPool::generated_pool()) {
257 delete resolver;
258 }
259 return result;
260 }
261
JsonStringToMessage(StringPiece input,Message * message,const JsonParseOptions & options)262 util::Status JsonStringToMessage(StringPiece input, Message* message,
263 const JsonParseOptions& options) {
264 const DescriptorPool* pool = message->GetDescriptor()->file()->pool();
265 TypeResolver* resolver =
266 pool == DescriptorPool::generated_pool()
267 ? GetGeneratedTypeResolver()
268 : NewTypeResolverForDescriptorPool(kTypeUrlPrefix, pool);
269 std::string binary;
270 util::Status result = JsonToBinaryString(resolver, GetTypeUrl(*message),
271 input, &binary, options);
272 if (result.ok() && !message->ParseFromString(binary)) {
273 result = util::InvalidArgumentError(
274 "JSON transcoder produced invalid protobuf output.");
275 }
276 if (pool != DescriptorPool::generated_pool()) {
277 delete resolver;
278 }
279 return result;
280 }
281
282 } // namespace util
283 } // namespace protobuf
284 } // namespace google
285