json_util.cc
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 
32 
46 
49 
50 #include <google/protobuf/port_def.inc>
51 
52 namespace google {
53 namespace protobuf {
54 namespace util {
55 
56 namespace internal {
58  if (buffer_size_ > 0) {
60  }
61 }
62 
63 void ZeroCopyStreamByteSink::Append(const char* bytes, size_t len) {
64  while (true) {
65  if (len <= buffer_size_) {
66  memcpy(buffer_, bytes, len);
67  buffer_ = static_cast<char*>(buffer_) + len;
68  buffer_size_ -= len;
69  return;
70  }
71  if (buffer_size_ > 0) {
72  memcpy(buffer_, bytes, buffer_size_);
74  len -= buffer_size_;
75  }
76  if (!stream_->Next(&buffer_, &buffer_size_)) {
77  // There isn't a way for ByteSink to report errors.
78  buffer_size_ = 0;
79  return;
80  }
81  }
82 }
83 } // namespace internal
84 
86  const std::string& type_url,
87  io::ZeroCopyInputStream* binary_input,
88  io::ZeroCopyOutputStream* json_output,
89  const JsonPrintOptions& options) {
90  io::CodedInputStream in_stream(binary_input);
93  converter::ProtoStreamObjectSource proto_source(&in_stream, resolver, type);
94  proto_source.set_use_ints_for_enums(options.always_print_enums_as_ints);
95  proto_source.set_preserve_proto_field_names(
96  options.preserve_proto_field_names);
97  io::CodedOutputStream out_stream(json_output);
98  converter::JsonObjectWriter json_writer(options.add_whitespace ? " " : "",
99  &out_stream);
100  if (options.always_print_primitive_fields) {
101  converter::DefaultValueObjectWriter default_value_writer(resolver, type,
102  &json_writer);
103  default_value_writer.set_preserve_proto_field_names(
104  options.preserve_proto_field_names);
105  default_value_writer.set_print_enums_as_ints(
106  options.always_print_enums_as_ints);
107  return proto_source.WriteTo(&default_value_writer);
108  } else {
109  return proto_source.WriteTo(&json_writer);
110  }
111 }
112 
114  const std::string& type_url,
115  const std::string& binary_input,
116  std::string* json_output,
117  const JsonPrintOptions& options) {
118  io::ArrayInputStream input_stream(binary_input.data(), binary_input.size());
119  io::StringOutputStream output_stream(json_output);
120  return BinaryToJsonStream(resolver, type_url, &input_stream, &output_stream,
121  options);
122 }
123 
124 namespace {
125 class StatusErrorListener : public converter::ErrorListener {
126  public:
127  StatusErrorListener() {}
128  ~StatusErrorListener() override {}
129 
130  util::Status GetStatus() { return status_; }
131 
132  void InvalidName(const converter::LocationTrackerInterface& loc,
133  StringPiece unknown_name,
134  StringPiece message) override {
135  std::string loc_string = GetLocString(loc);
136  if (!loc_string.empty()) {
137  loc_string.append(" ");
138  }
139  status_ =
141  StrCat(loc_string, unknown_name, ": ", message));
142  }
143 
144  void InvalidValue(const converter::LocationTrackerInterface& loc,
145  StringPiece type_name,
146  StringPiece value) override {
149  StrCat(GetLocString(loc), ": invalid value ", string(value),
150  " for type ", string(type_name)));
151  }
152 
153  void MissingField(const converter::LocationTrackerInterface& loc,
154  StringPiece missing_name) override {
156  StrCat(GetLocString(loc), ": missing field ",
157  std::string(missing_name)));
158  }
159 
160  private:
162 
163  std::string GetLocString(const converter::LocationTrackerInterface& loc) {
164  std::string loc_string = loc.ToString();
165  StripWhitespace(&loc_string);
166  if (!loc_string.empty()) {
167  loc_string = StrCat("(", loc_string, ")");
168  }
169  return loc_string;
170  }
171 
172  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(StatusErrorListener);
173 };
174 } // namespace
175 
177  const std::string& type_url,
178  io::ZeroCopyInputStream* json_input,
179  io::ZeroCopyOutputStream* binary_output,
180  const JsonParseOptions& options) {
183  internal::ZeroCopyStreamByteSink sink(binary_output);
184  StatusErrorListener listener;
185  converter::ProtoStreamObjectWriter::Options proto_writer_options;
186  proto_writer_options.ignore_unknown_fields = options.ignore_unknown_fields;
187  proto_writer_options.ignore_unknown_enum_values =
188  options.ignore_unknown_fields;
189  proto_writer_options.case_insensitive_enum_parsing =
190  options.case_insensitive_enum_parsing;
192  resolver, type, &sink, &listener, proto_writer_options);
193 
194  converter::JsonStreamParser parser(&proto_writer);
195  const void* buffer;
196  int length;
197  while (json_input->Next(&buffer, &length)) {
198  if (length == 0) continue;
199  RETURN_IF_ERROR(parser.Parse(
200  StringPiece(static_cast<const char*>(buffer), length)));
201  }
202  RETURN_IF_ERROR(parser.FinishParse());
203 
204  return listener.GetStatus();
205 }
206 
208  const std::string& type_url,
209  StringPiece json_input,
210  std::string* binary_output,
211  const JsonParseOptions& options) {
212  io::ArrayInputStream input_stream(json_input.data(), json_input.size());
213  io::StringOutputStream output_stream(binary_output);
214  return JsonToBinaryStream(resolver, type_url, &input_stream, &output_stream,
215  options);
216 }
217 
218 namespace {
219 const char* kTypeUrlPrefix = "type.googleapis.com";
220 TypeResolver* generated_type_resolver_ = NULL;
221 PROTOBUF_NAMESPACE_ID::internal::once_flag generated_type_resolver_init_;
222 
224  return std::string(kTypeUrlPrefix) + "/" +
225  message.GetDescriptor()->full_name();
226 }
227 
228 void DeleteGeneratedTypeResolver() { delete generated_type_resolver_; }
229 
230 void InitGeneratedTypeResolver() {
231  generated_type_resolver_ = NewTypeResolverForDescriptorPool(
233  ::google::protobuf::internal::OnShutdown(&DeleteGeneratedTypeResolver);
234 }
235 
236 TypeResolver* GetGeneratedTypeResolver() {
237  PROTOBUF_NAMESPACE_ID::internal::call_once(generated_type_resolver_init_,
238  InitGeneratedTypeResolver);
239  return generated_type_resolver_;
240 }
241 } // namespace
242 
244  const JsonOptions& options) {
245  const DescriptorPool* pool = message.GetDescriptor()->file()->pool();
246  TypeResolver* resolver =
248  ? GetGeneratedTypeResolver()
250  util::Status result =
252  message.SerializeAsString(), output, options);
254  delete resolver;
255  }
256  return result;
257 }
258 
260  const JsonParseOptions& options) {
261  const DescriptorPool* pool = message->GetDescriptor()->file()->pool();
262  TypeResolver* resolver =
264  ? GetGeneratedTypeResolver()
267  util::Status result = JsonToBinaryString(resolver, GetTypeUrl(*message),
268  input, &binary, options);
269  if (result.ok() && !message->ParseFromString(binary)) {
270  result =
272  "JSON transcoder produced invalid protobuf output.");
273  }
275  delete resolver;
276  }
277  return result;
278 }
279 
280 } // namespace util
281 } // namespace protobuf
282 } // namespace google
zero_copy_stream.h
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::converter::DefaultValueObjectWriter
Definition: default_value_objectwriter.h:60
google::protobuf::util::internal::ZeroCopyStreamByteSink::Append
void Append(const char *bytes, size_t len) override
Definition: json_util.cc:63
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS
#define GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TypeName)
Definition: macros.h:40
NULL
NULL
Definition: test_security_zap.cpp:405
google::protobuf::StrCat
string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: strutil.cc:1480
google::protobuf::StringPiece::data
const char * data() const
Definition: stringpiece.h:247
length
GLenum GLuint GLenum GLsizei length
Definition: glcorearb.h:2695
options
Message * options
Definition: src/google/protobuf/descriptor.cc:3119
input
std::string input
Definition: tokenizer_unittest.cc:197
json_stream_parser.h
GetTypeUrl
static string GetTypeUrl(const Descriptor *message)
Definition: conformance_cpp.cc:91
error_listener.h
google::protobuf::util::converter::ProtoStreamObjectSource::set_preserve_proto_field_names
void set_preserve_proto_field_names(bool value)
Definition: protostream_objectsource.h:116
google::protobuf.internal::call_once
void call_once(Args &&... args)
Definition: once.h:45
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
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
google::protobuf::util::NewTypeResolverForDescriptorPool
TypeResolver * NewTypeResolverForDescriptorPool(const std::string &url_prefix, const DescriptorPool *pool)
Definition: type_resolver_util.cc:366
google::protobuf::util::JsonParseOptions
Definition: json_util.h:51
google::protobuf::util::converter::DefaultValueObjectWriter::set_print_enums_as_ints
void set_print_enums_as_ints(bool value)
Definition: default_value_objectwriter.h:139
protostream_objectwriter.h
google::protobuf::util::converter::ProtoStreamObjectSource::set_use_ints_for_enums
void set_use_ints_for_enums(bool value)
Definition: protostream_objectsource.h:113
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.internal::OnShutdown
void OnShutdown(void(*func)())
Definition: common.cc:344
protostream_objectsource.h
google::protobuf::StripWhitespace
void StripWhitespace(string *str)
Definition: strutil.cc:113
strutil.h
google::protobuf.internal::once_flag
std::once_flag once_flag
Definition: once.h:43
google::protobuf::util::TypeResolver
Definition: type_resolver.h:52
coded_stream.h
buffer
GLuint buffer
Definition: glcorearb.h:2939
google::protobuf::StringPiece
Definition: stringpiece.h:180
type_resolver_util.h
google::protobuf::util::internal::ZeroCopyStreamByteSink
Definition: json_util.h:180
google::protobuf::DescriptorPool
Definition: src/google/protobuf/descriptor.h:1539
google::protobuf::util::converter::JsonStreamParser
Definition: json_stream_parser.h:70
google::protobuf::util::TypeResolver::ResolveMessageType
virtual util::Status ResolveMessageType(const std::string &type_url, google::protobuf::Type *message_type)=0
google::protobuf::io::ArrayInputStream
Definition: zero_copy_stream_impl_lite.h:65
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::converter::ProtoStreamObjectSource
Definition: protostream_objectsource.h:72
buffer
Definition: buffer_processor.h:43
default_value_objectwriter.h
status_macros.h
google::protobuf::util::converter::JsonObjectWriter
Definition: json_objectwriter.h:88
google::protobuf::util::internal::ZeroCopyStreamByteSink::buffer_size_
int buffer_size_
Definition: json_util.h:191
google::protobuf::util::error::INVALID_ARGUMENT
@ INVALID_ARGUMENT
Definition: status.h:50
google::protobuf::util::internal::ZeroCopyStreamByteSink::stream_
io::ZeroCopyOutputStream * stream_
Definition: json_util.h:189
google::protobuf::util::converter::ProtoStreamObjectWriter::Options
Definition: protostream_objectwriter.h:70
pool
InternalDescriptorPool * pool
Definition: php/ext/google/protobuf/protobuf.h:798
google::protobuf::io::CodedOutputStream
Definition: coded_stream.h:693
aditof::Status
Status
Status of any operation that the TOF sdk performs.
Definition: status_definitions.h:48
google::protobuf::io::ZeroCopyInputStream
Definition: zero_copy_stream.h:126
status_
util::Status status_
Definition: json_util.cc:161
google::protobuf::util::converter::ObjectSource::WriteTo
virtual util::Status WriteTo(ObjectWriter *ow) const
Definition: object_source.h:58
google::protobuf::util::Status::ok
bool ok() const
Definition: status.h:87
google::protobuf::util::converter::ProtoStreamObjectWriter::Options::ignore_unknown_fields
bool ignore_unknown_fields
Definition: protostream_objectwriter.h:84
type
GLenum type
Definition: glcorearb.h:2695
google::protobuf::Message
Definition: src/google/protobuf/message.h:205
google::protobuf::io::StringOutputStream
Definition: zero_copy_stream_impl_lite.h:131
len
int len
Definition: php/ext/google/protobuf/map.c:206
common.h
google::protobuf::util::converter::ProtoStreamObjectWriter::Options::ignore_unknown_enum_values
bool ignore_unknown_enum_values
Definition: protostream_objectwriter.h:87
google::protobuf::util::JsonPrintOptions
Definition: json_util.h:67
google::protobuf::DescriptorPool::generated_pool
static const DescriptorPool * generated_pool()
Definition: src/google/protobuf/descriptor.cc:1346
google::protobuf::io::ZeroCopyOutputStream
Definition: zero_copy_stream.h:183
google::protobuf::io::CodedInputStream
Definition: coded_stream.h:173
google::protobuf::util::converter::ProtoStreamObjectWriter
Definition: protostream_objectwriter.h:67
once.h
google::protobuf::util::internal::ZeroCopyStreamByteSink::buffer_
void * buffer_
Definition: json_util.h:190
kTypeUrlPrefix
static const char kTypeUrlPrefix[]
Definition: conformance_cpp.cc:60
google::protobuf::util::Status
Definition: status.h:67
Type
struct Type Type
Definition: php/ext/google/protobuf/protobuf.h:664
internal
Definition: any.pb.h:40
google::protobuf::io::ZeroCopyOutputStream::Next
virtual bool Next(void **data, int *size)=0
type_url
string * type_url
Definition: conformance_cpp.cc:98
json_objectwriter.h
google::protobuf::util::converter::ProtoStreamObjectWriter::Options::case_insensitive_enum_parsing
bool case_insensitive_enum_parsing
Definition: protostream_objectwriter.h:94
value
GLsizei const GLfloat * value
Definition: glcorearb.h:3093
google::protobuf::util::MessageToJsonString
util::Status MessageToJsonString(const Message &message, std::string *output, const JsonOptions &options)
Definition: json_util.cc:243
binary
const GLuint GLenum const GLvoid * binary
Definition: glcorearb.h:3962
google::protobuf::io::ZeroCopyInputStream::Next
virtual bool Next(const void **data, int *size)=0
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
benchmarks.python.py_benchmark.parser
parser
Definition: py_benchmark.py:10
RETURN_IF_ERROR
#define RETURN_IF_ERROR(expr)
Definition: status_macros.h:49
json_util.h
bytestream.h
google::protobuf::StringPiece::size
stringpiece_ssize_type size() const
Definition: stringpiece.h:248
google::protobuf::util::internal::ZeroCopyStreamByteSink::~ZeroCopyStreamByteSink
~ZeroCopyStreamByteSink()
Definition: json_util.cc:57
google
Definition: data_proto2_to_proto3_util.h:11
message
GLenum GLuint GLenum GLsizei const GLchar * message
Definition: glcorearb.h:2695
google::protobuf::util::converter::DefaultValueObjectWriter::set_preserve_proto_field_names
void set_preserve_proto_field_names(bool value)
Definition: default_value_objectwriter.h:133
google::protobuf::io::ZeroCopyOutputStream::BackUp
virtual void BackUp(int count)=0


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