json_util.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. namespace google {
  38. namespace protobuf {
  39. namespace io {
  40. class ZeroCopyInputStream;
  41. class ZeroCopyOutputStream;
  42. } // namespace io
  43. namespace util {
  44. struct JsonParseOptions {
  45. // Whether to ignore unknown JSON fields during parsing
  46. bool ignore_unknown_fields;
  47. JsonParseOptions() : ignore_unknown_fields(false) {}
  48. };
  49. struct JsonPrintOptions {
  50. // Whether to add spaces, line breaks and indentation to make the JSON output
  51. // easy to read.
  52. bool add_whitespace;
  53. // Whether to always print primitive fields. By default proto3 primitive
  54. // fields with default values will be omitted in JSON output. For example, an
  55. // int32 field set to 0 will be omitted. Set this flag to true will override
  56. // the default behavior and print primitive fields regardless of their values.
  57. bool always_print_primitive_fields;
  58. // Whether to always print enums as ints. By default they are rendered as
  59. // strings.
  60. bool always_print_enums_as_ints;
  61. // Whether to preserve proto field names
  62. bool preserve_proto_field_names;
  63. JsonPrintOptions()
  64. : add_whitespace(false),
  65. always_print_primitive_fields(false),
  66. always_print_enums_as_ints(false),
  67. preserve_proto_field_names(false) {}
  68. };
  69. // DEPRECATED. Use JsonPrintOptions instead.
  70. typedef JsonPrintOptions JsonOptions;
  71. // Converts from protobuf message to JSON. This is a simple wrapper of
  72. // BinaryToJsonString(). It will use the DescriptorPool of the passed-in
  73. // message to resolve Any types.
  74. LIBPROTOBUF_EXPORT util::Status MessageToJsonString(const Message& message,
  75. string* output,
  76. const JsonOptions& options);
  77. inline util::Status MessageToJsonString(const Message& message,
  78. string* output) {
  79. return MessageToJsonString(message, output, JsonOptions());
  80. }
  81. // Converts from JSON to protobuf message. This is a simple wrapper of
  82. // JsonStringToBinary(). It will use the DescriptorPool of the passed-in
  83. // message to resolve Any types.
  84. LIBPROTOBUF_EXPORT util::Status JsonStringToMessage(const string& input,
  85. Message* message,
  86. const JsonParseOptions& options);
  87. inline util::Status JsonStringToMessage(const string& input,
  88. Message* message) {
  89. return JsonStringToMessage(input, message, JsonParseOptions());
  90. }
  91. // Converts protobuf binary data to JSON.
  92. // The conversion will fail if:
  93. // 1. TypeResolver fails to resolve a type.
  94. // 2. input is not valid protobuf wire format, or conflicts with the type
  95. // information returned by TypeResolver.
  96. // Note that unknown fields will be discarded silently.
  97. LIBPROTOBUF_EXPORT util::Status BinaryToJsonStream(
  98. TypeResolver* resolver,
  99. const string& type_url,
  100. io::ZeroCopyInputStream* binary_input,
  101. io::ZeroCopyOutputStream* json_output,
  102. const JsonPrintOptions& options);
  103. inline util::Status BinaryToJsonStream(
  104. TypeResolver* resolver, const string& type_url,
  105. io::ZeroCopyInputStream* binary_input,
  106. io::ZeroCopyOutputStream* json_output) {
  107. return BinaryToJsonStream(resolver, type_url, binary_input, json_output,
  108. JsonPrintOptions());
  109. }
  110. LIBPROTOBUF_EXPORT util::Status BinaryToJsonString(
  111. TypeResolver* resolver,
  112. const string& type_url,
  113. const string& binary_input,
  114. string* json_output,
  115. const JsonPrintOptions& options);
  116. inline util::Status BinaryToJsonString(TypeResolver* resolver,
  117. const string& type_url,
  118. const string& binary_input,
  119. string* json_output) {
  120. return BinaryToJsonString(resolver, type_url, binary_input, json_output,
  121. JsonPrintOptions());
  122. }
  123. // Converts JSON data to protobuf binary format.
  124. // The conversion will fail if:
  125. // 1. TypeResolver fails to resolve a type.
  126. // 2. input is not valid JSON format, or conflicts with the type
  127. // information returned by TypeResolver.
  128. LIBPROTOBUF_EXPORT util::Status JsonToBinaryStream(
  129. TypeResolver* resolver,
  130. const string& type_url,
  131. io::ZeroCopyInputStream* json_input,
  132. io::ZeroCopyOutputStream* binary_output,
  133. const JsonParseOptions& options);
  134. inline util::Status JsonToBinaryStream(
  135. TypeResolver* resolver,
  136. const string& type_url,
  137. io::ZeroCopyInputStream* json_input,
  138. io::ZeroCopyOutputStream* binary_output) {
  139. return JsonToBinaryStream(resolver, type_url, json_input, binary_output,
  140. JsonParseOptions());
  141. }
  142. LIBPROTOBUF_EXPORT util::Status JsonToBinaryString(
  143. TypeResolver* resolver,
  144. const string& type_url,
  145. const string& json_input,
  146. string* binary_output,
  147. const JsonParseOptions& options);
  148. inline util::Status JsonToBinaryString(
  149. TypeResolver* resolver,
  150. const string& type_url,
  151. const string& json_input,
  152. 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 LIBPROTOBUF_EXPORT ZeroCopyStreamByteSink : public strings::ByteSink {
  159. public:
  160. explicit ZeroCopyStreamByteSink(io::ZeroCopyOutputStream* stream)
  161. : stream_(stream), buffer_(NULL), buffer_size_(0) {}
  162. ~ZeroCopyStreamByteSink();
  163. virtual void Append(const char* bytes, size_t len);
  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. #endif // GOOGLE_PROTOBUF_UTIL_JSON_UTIL_H__