诸暨麻将添加redis
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

204 lines
8.4 KiB

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