You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

json_util.h 9.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. // Utility functions to convert between protobuf binary format and proto3 JSON
  31. // format.
  32. #ifndef GOOGLE_PROTOBUF_UTIL_JSON_UTIL_H__
  33. #define GOOGLE_PROTOBUF_UTIL_JSON_UTIL_H__
  34. #include <google/protobuf/stubs/bytestream.h>
  35. #include <google/protobuf/stubs/status.h>
  36. #include <google/protobuf/stubs/strutil.h>
  37. #include <google/protobuf/message.h>
  38. #include <google/protobuf/util/type_resolver.h>
  39. // Must be included last.
  40. #include <google/protobuf/port_def.inc>
  41. namespace google
  42. {
  43. namespace protobuf
  44. {
  45. namespace io
  46. {
  47. class ZeroCopyInputStream;
  48. class ZeroCopyOutputStream;
  49. } // namespace io
  50. namespace util
  51. {
  52. struct JsonParseOptions
  53. {
  54. // Whether to ignore unknown JSON fields during parsing
  55. bool ignore_unknown_fields;
  56. // If true, when a lowercase enum value fails to parse, try convert it to
  57. // UPPER_CASE and see if it matches a valid enum.
  58. // WARNING: This option exists only to preserve legacy behavior. Avoid using
  59. // this option. If your enum needs to support different casing, consider using
  60. // allow_alias instead.
  61. bool case_insensitive_enum_parsing;
  62. JsonParseOptions() :
  63. ignore_unknown_fields(false),
  64. case_insensitive_enum_parsing(false)
  65. {
  66. }
  67. };
  68. struct JsonPrintOptions
  69. {
  70. // Whether to add spaces, line breaks and indentation to make the JSON output
  71. // easy to read.
  72. bool add_whitespace;
  73. // Whether to always print primitive fields. By default proto3 primitive
  74. // fields with default values will be omitted in JSON output. For example, an
  75. // int32 field set to 0 will be omitted. Set this flag to true will override
  76. // the default behavior and print primitive fields regardless of their values.
  77. bool always_print_primitive_fields;
  78. // Whether to always print enums as ints. By default they are rendered as
  79. // strings.
  80. bool always_print_enums_as_ints;
  81. // Whether to preserve proto field names
  82. bool preserve_proto_field_names;
  83. JsonPrintOptions() :
  84. add_whitespace(false),
  85. always_print_primitive_fields(false),
  86. always_print_enums_as_ints(false),
  87. preserve_proto_field_names(false)
  88. {
  89. }
  90. };
  91. // DEPRECATED. Use JsonPrintOptions instead.
  92. typedef JsonPrintOptions JsonOptions;
  93. // Converts from protobuf message to JSON and appends it to |output|. This is a
  94. // simple wrapper of BinaryToJsonString(). It will use the DescriptorPool of the
  95. // passed-in message to resolve Any types.
  96. PROTOBUF_EXPORT util::Status MessageToJsonString(const Message& message, std::string* output, const JsonOptions& options);
  97. inline util::Status MessageToJsonString(const Message& message, std::string* output)
  98. {
  99. return MessageToJsonString(message, output, JsonOptions());
  100. }
  101. // Converts from JSON to protobuf message. This is a simple wrapper of
  102. // JsonStringToBinary(). It will use the DescriptorPool of the passed-in
  103. // message to resolve Any types.
  104. PROTOBUF_EXPORT util::Status JsonStringToMessage(
  105. StringPiece input, Message* message, const JsonParseOptions& options
  106. );
  107. inline util::Status JsonStringToMessage(StringPiece input, Message* message)
  108. {
  109. return JsonStringToMessage(input, message, JsonParseOptions());
  110. }
  111. // Converts protobuf binary data to JSON.
  112. // The conversion will fail if:
  113. // 1. TypeResolver fails to resolve a type.
  114. // 2. input is not valid protobuf wire format, or conflicts with the type
  115. // information returned by TypeResolver.
  116. // Note that unknown fields will be discarded silently.
  117. PROTOBUF_EXPORT util::Status BinaryToJsonStream(
  118. TypeResolver* resolver, const std::string& type_url, io::ZeroCopyInputStream* binary_input, io::ZeroCopyOutputStream* json_output, const JsonPrintOptions& options
  119. );
  120. inline util::Status BinaryToJsonStream(TypeResolver* resolver, const std::string& type_url, io::ZeroCopyInputStream* binary_input, io::ZeroCopyOutputStream* json_output)
  121. {
  122. return BinaryToJsonStream(resolver, type_url, binary_input, json_output, JsonPrintOptions());
  123. }
  124. PROTOBUF_EXPORT util::Status BinaryToJsonString(
  125. TypeResolver* resolver, const std::string& type_url, const std::string& binary_input, std::string* json_output, const JsonPrintOptions& options
  126. );
  127. inline util::Status BinaryToJsonString(TypeResolver* resolver, const std::string& type_url, const std::string& binary_input, std::string* json_output)
  128. {
  129. return BinaryToJsonString(resolver, type_url, binary_input, json_output, JsonPrintOptions());
  130. }
  131. // Converts JSON data to protobuf binary format.
  132. // The conversion will fail if:
  133. // 1. TypeResolver fails to resolve a type.
  134. // 2. input is not valid JSON format, or conflicts with the type
  135. // information returned by TypeResolver.
  136. PROTOBUF_EXPORT util::Status JsonToBinaryStream(
  137. TypeResolver* resolver, const std::string& type_url, io::ZeroCopyInputStream* json_input, io::ZeroCopyOutputStream* binary_output, const JsonParseOptions& options
  138. );
  139. inline util::Status JsonToBinaryStream(
  140. TypeResolver* resolver, const std::string& type_url, io::ZeroCopyInputStream* json_input, io::ZeroCopyOutputStream* binary_output
  141. )
  142. {
  143. return JsonToBinaryStream(resolver, type_url, json_input, binary_output, JsonParseOptions());
  144. }
  145. PROTOBUF_EXPORT util::Status JsonToBinaryString(
  146. TypeResolver* resolver, const std::string& type_url, StringPiece json_input, std::string* binary_output, const JsonParseOptions& options
  147. );
  148. inline util::Status JsonToBinaryString(TypeResolver* resolver, const std::string& type_url, StringPiece json_input, std::string* binary_output)
  149. {
  150. return JsonToBinaryString(resolver, type_url, json_input, binary_output, JsonParseOptions());
  151. }
  152. namespace internal
  153. {
  154. // Internal helper class. Put in the header so we can write unit-tests for it.
  155. class PROTOBUF_EXPORT ZeroCopyStreamByteSink : public strings::ByteSink
  156. {
  157. public:
  158. explicit ZeroCopyStreamByteSink(io::ZeroCopyOutputStream* stream) :
  159. stream_(stream),
  160. buffer_(nullptr),
  161. buffer_size_(0)
  162. {
  163. }
  164. ~ZeroCopyStreamByteSink() override;
  165. void Append(const char* bytes, size_t len) override;
  166. private:
  167. io::ZeroCopyOutputStream* stream_;
  168. void* buffer_;
  169. int buffer_size_;
  170. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ZeroCopyStreamByteSink);
  171. };
  172. } // namespace internal
  173. } // namespace util
  174. } // namespace protobuf
  175. } // namespace google
  176. #include <google/protobuf/port_undef.inc>
  177. #endif // GOOGLE_PROTOBUF_UTIL_JSON_UTIL_H__