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
54namespace google {
55namespace protobuf {
56namespace util {
57
58namespace internal {
59ZeroCopyStreamByteSink::~ZeroCopyStreamByteSink() {
60 if (buffer_size_ > 0) {
61 stream_->BackUp(count: buffer_size_);
62 }
63}
64
65void ZeroCopyStreamByteSink::Append(const char* bytes, size_t len) {
66 while (true) {
67 if (len <= buffer_size_) { // NOLINT
68 memcpy(dest: buffer_, src: bytes, n: len);
69 buffer_ = static_cast<char*>(buffer_) + len;
70 buffer_size_ -= len;
71 return;
72 }
73 if (buffer_size_ > 0) {
74 memcpy(dest: buffer_, src: bytes, n: buffer_size_);
75 bytes += buffer_size_;
76 len -= buffer_size_;
77 }
78 if (!stream_->Next(data: &buffer_, size: &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
87util::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(ow: &default_value_writer);
112 } else {
113 return proto_source.WriteTo(ow: &json_writer);
114 }
115}
116
117util::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, binary_input: &input_stream, json_output: &output_stream,
125 options);
126}
127
128namespace {
129class StatusErrorListener : public converter::ErrorListener {
130 public:
131 StatusErrorListener() {}
132 ~StatusErrorListener() override {}
133
134 util::Status GetStatus() { return status_; }
135
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(s: " ");
142 }
143 status_ = util::InvalidArgumentError(
144 message: StrCat(a: loc_string, b: unknown_name, c: ": ", d: message));
145 }
146
147 void InvalidValue(const converter::LocationTrackerInterface& loc,
148 StringPiece type_name,
149 StringPiece value) override {
150 status_ = util::InvalidArgumentError(
151 message: StrCat(a: GetLocString(loc), b: ": invalid value ", c: std::string(value),
152 d: " for type ", e: std::string(type_name)));
153 }
154
155 void MissingField(const converter::LocationTrackerInterface& loc,
156 StringPiece missing_name) override {
157 status_ = util::InvalidArgumentError(message: StrCat(
158 a: GetLocString(loc), b: ": missing field ", c: std::string(missing_name)));
159 }
160
161 private:
162 util::Status status_;
163
164 std::string GetLocString(const converter::LocationTrackerInterface& loc) {
165 std::string loc_string = loc.ToString();
166 StripWhitespace(s: &loc_string);
167 if (!loc_string.empty()) {
168 loc_string = StrCat(a: "(", b: loc_string, c: ")");
169 }
170 return loc_string;
171 }
172
173 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(StatusErrorListener);
174};
175} // namespace
176
177util::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(data: &buffer, size: &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
208util::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, json_input: &input_stream, binary_output: &output_stream,
216 options);
217}
218
219namespace {
220const char* kTypeUrlPrefix = "type.googleapis.com";
221TypeResolver* generated_type_resolver_ = nullptr;
222PROTOBUF_NAMESPACE_ID::internal::once_flag generated_type_resolver_init_;
223
224std::string GetTypeUrl(const Message& message) {
225 return std::string(kTypeUrlPrefix) + "/" +
226 message.GetDescriptor()->full_name();
227}
228
229void DeleteGeneratedTypeResolver() { // NOLINT
230 delete generated_type_resolver_;
231}
232
233void InitGeneratedTypeResolver() {
234 generated_type_resolver_ = NewTypeResolverForDescriptorPool(
235 url_prefix: kTypeUrlPrefix, pool: DescriptorPool::generated_pool());
236 ::google::protobuf::internal::OnShutdown(func: &DeleteGeneratedTypeResolver);
237}
238
239TypeResolver* GetGeneratedTypeResolver() {
240 PROTOBUF_NAMESPACE_ID::internal::call_once(args&: generated_type_resolver_init_,
241 args&: InitGeneratedTypeResolver);
242 return generated_type_resolver_;
243}
244} // namespace
245
246util::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(url_prefix: kTypeUrlPrefix, pool);
253 util::Status result =
254 BinaryToJsonString(resolver, type_url: GetTypeUrl(message),
255 binary_input: message.SerializeAsString(), json_output: output, options);
256 if (pool != DescriptorPool::generated_pool()) {
257 delete resolver;
258 }
259 return result;
260}
261
262util::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(url_prefix: kTypeUrlPrefix, pool);
269 std::string binary;
270 util::Status result = JsonToBinaryString(resolver, type_url: GetTypeUrl(message: *message),
271 json_input: input, binary_output: &binary, options);
272 if (result.ok() && !message->ParseFromString(data: binary)) {
273 result = util::InvalidArgumentError(
274 message: "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