json_util.cc 10 KB

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