json_util.h 8.4 KB

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