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/io/coded_stream.h>
35 #include <google/protobuf/io/zero_copy_stream.h>
36 #include <google/protobuf/stubs/once.h>
37 #include <google/protobuf/util/internal/default_value_objectwriter.h>
38 #include <google/protobuf/util/internal/error_listener.h>
39 #include <google/protobuf/util/internal/json_objectwriter.h>
40 #include <google/protobuf/util/internal/json_stream_parser.h>
41 #include <google/protobuf/util/internal/protostream_objectsource.h>
42 #include <google/protobuf/util/internal/protostream_objectwriter.h>
43 #include <google/protobuf/util/type_resolver.h>
44 #include <google/protobuf/util/type_resolver_util.h>
45 #include <google/protobuf/stubs/bytestream.h>
46 #include <google/protobuf/stubs/strutil.h>
47 #include <google/protobuf/stubs/status_macros.h>
48
49 #include <google/protobuf/port_def.inc>
50
51 namespace google {
52 namespace protobuf {
53 namespace util {
54
55 namespace internal {
~ZeroCopyStreamByteSink()56 ZeroCopyStreamByteSink::~ZeroCopyStreamByteSink() {
57 if (buffer_size_ > 0) {
58 stream_->BackUp(buffer_size_);
59 }
60 }
61
Append(const char * bytes,size_t len)62 void ZeroCopyStreamByteSink::Append(const char* bytes, size_t len) {
63 while (true) {
64 if (len <= buffer_size_) {
65 memcpy(buffer_, bytes, len);
66 buffer_ = static_cast<char*>(buffer_) + len;
67 buffer_size_ -= len;
68 return;
69 }
70 if (buffer_size_ > 0) {
71 memcpy(buffer_, bytes, buffer_size_);
72 bytes += buffer_size_;
73 len -= buffer_size_;
74 }
75 if (!stream_->Next(&buffer_, &buffer_size_)) {
76 // There isn't a way for ByteSink to report errors.
77 buffer_size_ = 0;
78 return;
79 }
80 }
81 }
82 } // namespace internal
83
BinaryToJsonStream(TypeResolver * resolver,const std::string & type_url,io::ZeroCopyInputStream * binary_input,io::ZeroCopyOutputStream * json_output,const JsonPrintOptions & options)84 util::Status BinaryToJsonStream(TypeResolver* resolver,
85 const std::string& type_url,
86 io::ZeroCopyInputStream* binary_input,
87 io::ZeroCopyOutputStream* json_output,
88 const JsonPrintOptions& options) {
89 io::CodedInputStream in_stream(binary_input);
90 google::protobuf::Type type;
91 RETURN_IF_ERROR(resolver->ResolveMessageType(type_url, &type));
92 converter::ProtoStreamObjectSource proto_source(&in_stream, resolver, type);
93 proto_source.set_use_ints_for_enums(options.always_print_enums_as_ints);
94 proto_source.set_preserve_proto_field_names(
95 options.preserve_proto_field_names);
96 io::CodedOutputStream out_stream(json_output);
97 converter::JsonObjectWriter json_writer(options.add_whitespace ? " " : "",
98 &out_stream);
99 if (options.always_print_primitive_fields) {
100 converter::DefaultValueObjectWriter default_value_writer(resolver, type,
101 &json_writer);
102 default_value_writer.set_preserve_proto_field_names(
103 options.preserve_proto_field_names);
104 default_value_writer.set_print_enums_as_ints(
105 options.always_print_enums_as_ints);
106 return proto_source.WriteTo(&default_value_writer);
107 } else {
108 return proto_source.WriteTo(&json_writer);
109 }
110 }
111
BinaryToJsonString(TypeResolver * resolver,const std::string & type_url,const std::string & binary_input,std::string * json_output,const JsonPrintOptions & options)112 util::Status BinaryToJsonString(TypeResolver* resolver,
113 const std::string& type_url,
114 const std::string& binary_input,
115 std::string* json_output,
116 const JsonPrintOptions& options) {
117 io::ArrayInputStream input_stream(binary_input.data(), binary_input.size());
118 io::StringOutputStream output_stream(json_output);
119 return BinaryToJsonStream(resolver, type_url, &input_stream, &output_stream,
120 options);
121 }
122
123 namespace {
124 class StatusErrorListener : public converter::ErrorListener {
125 public:
StatusErrorListener()126 StatusErrorListener() {}
~StatusErrorListener()127 ~StatusErrorListener() override {}
128
GetStatus()129 util::Status GetStatus() { return status_; }
130
InvalidName(const converter::LocationTrackerInterface & loc,StringPiece unknown_name,StringPiece message)131 void InvalidName(const converter::LocationTrackerInterface& loc,
132 StringPiece unknown_name,
133 StringPiece message) override {
134 std::string loc_string = GetLocString(loc);
135 if (!loc_string.empty()) {
136 loc_string.append(" ");
137 }
138 status_ =
139 util::Status(util::error::INVALID_ARGUMENT,
140 StrCat(loc_string, unknown_name, ": ", message));
141 }
142
InvalidValue(const converter::LocationTrackerInterface & loc,StringPiece type_name,StringPiece value)143 void InvalidValue(const converter::LocationTrackerInterface& loc,
144 StringPiece type_name,
145 StringPiece value) override {
146 status_ = util::Status(
147 util::error::INVALID_ARGUMENT,
148 StrCat(GetLocString(loc), ": invalid value ", std::string(value),
149 " for type ", std::string(type_name)));
150 }
151
MissingField(const converter::LocationTrackerInterface & loc,StringPiece missing_name)152 void MissingField(const converter::LocationTrackerInterface& loc,
153 StringPiece missing_name) override {
154 status_ = util::Status(util::error::INVALID_ARGUMENT,
155 StrCat(GetLocString(loc), ": missing field ",
156 std::string(missing_name)));
157 }
158
159 private:
160 util::Status status_;
161
GetLocString(const converter::LocationTrackerInterface & loc)162 std::string GetLocString(const converter::LocationTrackerInterface& loc) {
163 std::string loc_string = loc.ToString();
164 StripWhitespace(&loc_string);
165 if (!loc_string.empty()) {
166 loc_string = StrCat("(", loc_string, ")");
167 }
168 return loc_string;
169 }
170
171 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(StatusErrorListener);
172 };
173 } // namespace
174
JsonToBinaryStream(TypeResolver * resolver,const std::string & type_url,io::ZeroCopyInputStream * json_input,io::ZeroCopyOutputStream * binary_output,const JsonParseOptions & options)175 util::Status JsonToBinaryStream(TypeResolver* resolver,
176 const std::string& type_url,
177 io::ZeroCopyInputStream* json_input,
178 io::ZeroCopyOutputStream* binary_output,
179 const JsonParseOptions& options) {
180 google::protobuf::Type type;
181 RETURN_IF_ERROR(resolver->ResolveMessageType(type_url, &type));
182 internal::ZeroCopyStreamByteSink sink(binary_output);
183 StatusErrorListener listener;
184 converter::ProtoStreamObjectWriter::Options proto_writer_options;
185 proto_writer_options.ignore_unknown_fields = options.ignore_unknown_fields;
186 proto_writer_options.ignore_unknown_enum_values =
187 options.ignore_unknown_fields;
188 proto_writer_options.case_insensitive_enum_parsing =
189 options.case_insensitive_enum_parsing;
190 converter::ProtoStreamObjectWriter proto_writer(
191 resolver, type, &sink, &listener, proto_writer_options);
192
193 converter::JsonStreamParser parser(&proto_writer);
194 const void* buffer;
195 int length;
196 while (json_input->Next(&buffer, &length)) {
197 if (length == 0) continue;
198 RETURN_IF_ERROR(parser.Parse(
199 StringPiece(static_cast<const char*>(buffer), length)));
200 }
201 RETURN_IF_ERROR(parser.FinishParse());
202
203 return listener.GetStatus();
204 }
205
JsonToBinaryString(TypeResolver * resolver,const std::string & type_url,StringPiece json_input,std::string * binary_output,const JsonParseOptions & options)206 util::Status JsonToBinaryString(TypeResolver* resolver,
207 const std::string& type_url,
208 StringPiece json_input,
209 std::string* binary_output,
210 const JsonParseOptions& options) {
211 io::ArrayInputStream input_stream(json_input.data(), json_input.size());
212 io::StringOutputStream output_stream(binary_output);
213 return JsonToBinaryStream(resolver, type_url, &input_stream, &output_stream,
214 options);
215 }
216
217 namespace {
218 const char* kTypeUrlPrefix = "type.googleapis.com";
219 TypeResolver* generated_type_resolver_ = NULL;
220 PROTOBUF_NAMESPACE_ID::internal::once_flag generated_type_resolver_init_;
221
GetTypeUrl(const Message & message)222 std::string GetTypeUrl(const Message& message) {
223 return std::string(kTypeUrlPrefix) + "/" +
224 message.GetDescriptor()->full_name();
225 }
226
DeleteGeneratedTypeResolver()227 void DeleteGeneratedTypeResolver() { delete generated_type_resolver_; }
228
InitGeneratedTypeResolver()229 void InitGeneratedTypeResolver() {
230 generated_type_resolver_ = NewTypeResolverForDescriptorPool(
231 kTypeUrlPrefix, DescriptorPool::generated_pool());
232 ::google::protobuf::internal::OnShutdown(&DeleteGeneratedTypeResolver);
233 }
234
GetGeneratedTypeResolver()235 TypeResolver* GetGeneratedTypeResolver() {
236 PROTOBUF_NAMESPACE_ID::internal::call_once(generated_type_resolver_init_,
237 InitGeneratedTypeResolver);
238 return generated_type_resolver_;
239 }
240 } // namespace
241
MessageToJsonString(const Message & message,std::string * output,const JsonOptions & options)242 util::Status MessageToJsonString(const Message& message, std::string* output,
243 const JsonOptions& options) {
244 const DescriptorPool* pool = message.GetDescriptor()->file()->pool();
245 TypeResolver* resolver =
246 pool == DescriptorPool::generated_pool()
247 ? GetGeneratedTypeResolver()
248 : NewTypeResolverForDescriptorPool(kTypeUrlPrefix, pool);
249 util::Status result =
250 BinaryToJsonString(resolver, GetTypeUrl(message),
251 message.SerializeAsString(), output, options);
252 if (pool != DescriptorPool::generated_pool()) {
253 delete resolver;
254 }
255 return result;
256 }
257
JsonStringToMessage(StringPiece input,Message * message,const JsonParseOptions & options)258 util::Status JsonStringToMessage(StringPiece input, Message* message,
259 const JsonParseOptions& options) {
260 const DescriptorPool* pool = message->GetDescriptor()->file()->pool();
261 TypeResolver* resolver =
262 pool == DescriptorPool::generated_pool()
263 ? GetGeneratedTypeResolver()
264 : NewTypeResolverForDescriptorPool(kTypeUrlPrefix, pool);
265 std::string binary;
266 util::Status result = JsonToBinaryString(resolver, GetTypeUrl(*message),
267 input, &binary, options);
268 if (result.ok() && !message->ParseFromString(binary)) {
269 result =
270 util::Status(util::error::INVALID_ARGUMENT,
271 "JSON transcoder produced invalid protobuf output.");
272 }
273 if (pool != DescriptorPool::generated_pool()) {
274 delete resolver;
275 }
276 return result;
277 }
278
279 } // namespace util
280 } // namespace protobuf
281 } // namespace google
282