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