json_util.h
Go to the documentation of this file.
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 
31 // Utility functions to convert between protobuf binary format and proto3 JSON
32 // format.
33 #ifndef GOOGLE_PROTOBUF_UTIL_JSON_UTIL_H__
34 #define GOOGLE_PROTOBUF_UTIL_JSON_UTIL_H__
35 
40 
41 #include <google/protobuf/port_def.inc>
42 
43 namespace google {
44 namespace protobuf {
45 namespace io {
46 class ZeroCopyInputStream;
47 class ZeroCopyOutputStream;
48 } // namespace io
49 namespace util {
50 
52  // Whether to ignore unknown JSON fields during parsing
54 
55  // If true, when a lowercase enum value fails to parse, try convert it to
56  // UPPER_CASE and see if it matches a valid enum.
57  // WARNING: This option exists only to preserve legacy behavior. Avoid using
58  // this option. If your enum needs to support different casing, consider using
59  // allow_alias instead.
61 
65 };
66 
68  // Whether to add spaces, line breaks and indentation to make the JSON output
69  // easy to read.
71  // Whether to always print primitive fields. By default proto3 primitive
72  // fields with default values will be omitted in JSON output. For example, an
73  // int32 field set to 0 will be omitted. Set this flag to true will override
74  // the default behavior and print primitive fields regardless of their values.
76  // Whether to always print enums as ints. By default they are rendered as
77  // strings.
79  // Whether to preserve proto field names
81 
87 };
88 
89 // DEPRECATED. Use JsonPrintOptions instead.
91 
92 // Converts from protobuf message to JSON and appends it to |output|. This is a
93 // simple wrapper of BinaryToJsonString(). It will use the DescriptorPool of the
94 // passed-in message to resolve Any types.
95 PROTOBUF_EXPORT util::Status MessageToJsonString(const Message& message,
97  const JsonOptions& options);
98 
100  std::string* output) {
102 }
103 
104 // Converts from JSON to protobuf message. This is a simple wrapper of
105 // JsonStringToBinary(). It will use the DescriptorPool of the passed-in
106 // message to resolve Any types.
107 PROTOBUF_EXPORT util::Status JsonStringToMessage(
108  StringPiece input, Message* message, const JsonParseOptions& options);
109 
111  Message* message) {
113 }
114 
115 // Converts protobuf binary data to JSON.
116 // The conversion will fail if:
117 // 1. TypeResolver fails to resolve a type.
118 // 2. input is not valid protobuf wire format, or conflicts with the type
119 // information returned by TypeResolver.
120 // Note that unknown fields will be discarded silently.
121 PROTOBUF_EXPORT util::Status BinaryToJsonStream(
122  TypeResolver* resolver, const std::string& type_url,
123  io::ZeroCopyInputStream* binary_input,
124  io::ZeroCopyOutputStream* json_output, const JsonPrintOptions& options);
125 
127  TypeResolver* resolver, const std::string& type_url,
128  io::ZeroCopyInputStream* binary_input,
129  io::ZeroCopyOutputStream* json_output) {
130  return BinaryToJsonStream(resolver, type_url, binary_input, json_output,
131  JsonPrintOptions());
132 }
133 
134 PROTOBUF_EXPORT util::Status BinaryToJsonString(
135  TypeResolver* resolver, const std::string& type_url,
136  const std::string& binary_input, std::string* json_output,
137  const JsonPrintOptions& options);
138 
140  const std::string& type_url,
141  const std::string& binary_input,
142  std::string* json_output) {
143  return BinaryToJsonString(resolver, type_url, binary_input, json_output,
144  JsonPrintOptions());
145 }
146 
147 // Converts JSON data to protobuf binary format.
148 // The conversion will fail if:
149 // 1. TypeResolver fails to resolve a type.
150 // 2. input is not valid JSON format, or conflicts with the type
151 // information returned by TypeResolver.
152 PROTOBUF_EXPORT util::Status JsonToBinaryStream(
153  TypeResolver* resolver, const std::string& type_url,
154  io::ZeroCopyInputStream* json_input,
155  io::ZeroCopyOutputStream* binary_output, const JsonParseOptions& options);
156 
158  TypeResolver* resolver, const std::string& type_url,
159  io::ZeroCopyInputStream* json_input,
160  io::ZeroCopyOutputStream* binary_output) {
161  return JsonToBinaryStream(resolver, type_url, json_input, binary_output,
162  JsonParseOptions());
163 }
164 
165 PROTOBUF_EXPORT util::Status JsonToBinaryString(
166  TypeResolver* resolver, const std::string& type_url,
167  StringPiece json_input, std::string* binary_output,
168  const JsonParseOptions& options);
169 
171  const std::string& type_url,
172  StringPiece json_input,
173  std::string* binary_output) {
174  return JsonToBinaryString(resolver, type_url, json_input, binary_output,
175  JsonParseOptions());
176 }
177 
178 namespace internal {
179 // Internal helper class. Put in the header so we can write unit-tests for it.
180 class PROTOBUF_EXPORT ZeroCopyStreamByteSink : public strings::ByteSink {
181  public:
183  : stream_(stream), buffer_(NULL), buffer_size_(0) {}
185 
186  void Append(const char* bytes, size_t len) override;
187 
188  private:
190  void* buffer_;
192 
194 };
195 } // namespace internal
196 
197 } // namespace util
198 } // namespace protobuf
199 } // namespace google
200 
201 #include <google/protobuf/port_undef.inc>
202 
203 #endif // GOOGLE_PROTOBUF_UTIL_JSON_UTIL_H__
google::protobuf::util::JsonOptions
JsonPrintOptions JsonOptions
Definition: json_util.h:90
google::protobuf::util::BinaryToJsonString
util::Status BinaryToJsonString(TypeResolver *resolver, const std::string &type_url, const std::string &binary_input, std::string *json_output, const JsonPrintOptions &options)
Definition: json_util.cc:113
google::protobuf::util::JsonPrintOptions::always_print_primitive_fields
bool always_print_primitive_fields
Definition: json_util.h:75
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS
#define GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TypeName)
Definition: macros.h:40
stream
GLuint GLuint stream
Definition: glcorearb.h:3946
NULL
NULL
Definition: test_security_zap.cpp:405
google::protobuf::util::internal::ZeroCopyStreamByteSink::ZeroCopyStreamByteSink
ZeroCopyStreamByteSink(io::ZeroCopyOutputStream *stream)
Definition: json_util.h:182
options
Message * options
Definition: src/google/protobuf/descriptor.cc:3119
input
std::string input
Definition: tokenizer_unittest.cc:197
google::protobuf::util::JsonToBinaryStream
util::Status JsonToBinaryStream(TypeResolver *resolver, const std::string &type_url, io::ZeroCopyInputStream *json_input, io::ZeroCopyOutputStream *binary_output, const JsonParseOptions &options)
Definition: json_util.cc:176
Append
static void Append(State *state, const char *const str, const int length)
Definition: demangle.cc:272
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
google::protobuf::util::JsonParseOptions
Definition: json_util.h:51
type_resolver.h
bytes
uint8 bytes[10]
Definition: coded_stream_unittest.cc:153
google::protobuf::util::JsonToBinaryString
util::Status JsonToBinaryString(TypeResolver *resolver, const std::string &type_url, StringPiece json_input, std::string *binary_output, const JsonParseOptions &options)
Definition: json_util.cc:207
google::protobuf::util::JsonPrintOptions::preserve_proto_field_names
bool preserve_proto_field_names
Definition: json_util.h:80
strutil.h
google::protobuf::util::TypeResolver
Definition: type_resolver.h:52
google::protobuf::StringPiece
Definition: stringpiece.h:180
google::protobuf::util::JsonPrintOptions::JsonPrintOptions
JsonPrintOptions()
Definition: json_util.h:82
google::protobuf::util::internal::ZeroCopyStreamByteSink
Definition: json_util.h:180
message.h
google::protobuf::util::BinaryToJsonStream
util::Status BinaryToJsonStream(TypeResolver *resolver, const std::string &type_url, io::ZeroCopyInputStream *binary_input, io::ZeroCopyOutputStream *json_output, const JsonPrintOptions &options)
Definition: json_util.cc:85
google::protobuf::util::JsonPrintOptions::always_print_enums_as_ints
bool always_print_enums_as_ints
Definition: json_util.h:78
google::protobuf::util::internal::ZeroCopyStreamByteSink::buffer_size_
int buffer_size_
Definition: json_util.h:191
google::protobuf::util::internal::ZeroCopyStreamByteSink::stream_
io::ZeroCopyOutputStream * stream_
Definition: json_util.h:189
google::protobuf::io::ZeroCopyInputStream
Definition: zero_copy_stream.h:126
google::protobuf::Message
Definition: src/google/protobuf/message.h:205
len
int len
Definition: php/ext/google/protobuf/map.c:206
google::protobuf::util::JsonPrintOptions::add_whitespace
bool add_whitespace
Definition: json_util.h:70
google::protobuf::util::JsonPrintOptions
Definition: json_util.h:67
google::protobuf::io::ZeroCopyOutputStream
Definition: zero_copy_stream.h:183
google::protobuf::util::internal::ZeroCopyStreamByteSink::buffer_
void * buffer_
Definition: json_util.h:190
google::protobuf::util::Status
Definition: status.h:67
google::protobuf::util::JsonParseOptions::case_insensitive_enum_parsing
bool case_insensitive_enum_parsing
Definition: json_util.h:60
internal
Definition: any.pb.h:40
buffer_
static uint8 buffer_[kBufferSize]
Definition: coded_stream_unittest.cc:136
google::protobuf::util::JsonParseOptions::ignore_unknown_fields
bool ignore_unknown_fields
Definition: json_util.h:53
type_url
string * type_url
Definition: conformance_cpp.cc:98
google::protobuf::util::MessageToJsonString
util::Status MessageToJsonString(const Message &message, std::string *output, const JsonOptions &options)
Definition: json_util.cc:243
google::protobuf::util::JsonStringToMessage
util::Status JsonStringToMessage(StringPiece input, Message *message, const JsonParseOptions &options)
Definition: json_util.cc:259
output
const upb_json_parsermethod const upb_symtab upb_sink * output
Definition: ruby/ext/google/protobuf_c/upb.h:10503
google::protobuf::util::JsonParseOptions::JsonParseOptions
JsonParseOptions()
Definition: json_util.h:62
bytestream.h
false
#define false
Definition: cJSON.c:70
google
Definition: data_proto2_to_proto3_util.h:11
message
GLenum GLuint GLenum GLsizei const GLchar * message
Definition: glcorearb.h:2695


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:55