From 0259cc3980a29945af19bf80225611ff84b7eece Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Wed, 11 Sep 2024 10:27:52 -0700 Subject: [PATCH] Add JsonStreamToMessage method PiperOrigin-RevId: 673445444 --- src/google/protobuf/json/internal/parser.cc | 11 +++++------ src/google/protobuf/json/internal/parser.h | 6 +++--- src/google/protobuf/json/json.cc | 11 ++++++++++- src/google/protobuf/json/json.h | 16 +++++++++++++++- 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/google/protobuf/json/internal/parser.cc b/src/google/protobuf/json/internal/parser.cc index ae4954d75e75..b7631f356a9d 100644 --- a/src/google/protobuf/json/internal/parser.cc +++ b/src/google/protobuf/json/internal/parser.cc @@ -1271,14 +1271,11 @@ absl::Status ParseMessage(JsonLexer& lex, const Desc& desc, } } // namespace -absl::Status JsonStringToMessage(absl::string_view input, Message* message, +absl::Status JsonStreamToMessage(io::ZeroCopyInputStream* input, + Message* message, json_internal::ParseOptions options) { MessagePath path(message->GetDescriptor()->full_name()); - if (PROTOBUF_DEBUG) { - ABSL_DLOG(INFO) << "json2/input: " << absl::CHexEscape(input); - } - io::ArrayInputStream in(input.data(), input.size()); - JsonLexer lex(&in, options, &path); + JsonLexer lex(input, options, &path); ParseProto2Descriptor::Msg msg(message); absl::Status s = @@ -1360,3 +1357,5 @@ absl::Status JsonToBinaryStream(google::protobuf::util::TypeResolver* resolver, } // namespace json_internal } // namespace protobuf } // namespace google + +#include "google/protobuf/port_undef.inc" diff --git a/src/google/protobuf/json/internal/parser.h b/src/google/protobuf/json/internal/parser.h index a8a43b28bfda..3ec4b3c939db 100644 --- a/src/google/protobuf/json/internal/parser.h +++ b/src/google/protobuf/json/internal/parser.h @@ -10,7 +10,6 @@ #include -#include "absl/strings/string_view.h" #include "google/protobuf/json/internal/lexer.h" #include "google/protobuf/message.h" #include "google/protobuf/util/type_resolver.h" @@ -18,9 +17,10 @@ namespace google { namespace protobuf { namespace json_internal { -// Internal version of google::protobuf::util::JsonStringToMessage; see json_util.h for +// Internal version of google::protobuf::util::JsonStreamToMessage; see json_util.h for // details. -absl::Status JsonStringToMessage(absl::string_view input, Message* message, +absl::Status JsonStreamToMessage(io::ZeroCopyInputStream* input, + Message* message, json_internal::ParseOptions options); // Internal version of google::protobuf::util::JsonToBinaryStream; see json_util.h for // details. diff --git a/src/google/protobuf/json/json.cc b/src/google/protobuf/json/json.cc index cd8743d151be..f3397c2dff63 100644 --- a/src/google/protobuf/json/json.cc +++ b/src/google/protobuf/json/json.cc @@ -100,6 +100,13 @@ absl::Status MessageToJsonString(const Message& message, std::string* output, absl::Status JsonStringToMessage(absl::string_view input, Message* message, const ParseOptions& options) { + io::ArrayInputStream input_stream(input.data(), input.size()); + return JsonStreamToMessage(&input_stream, message, options); +} + +absl::Status JsonStreamToMessage(io::ZeroCopyInputStream* input, + Message* message, + const ParseOptions& options) { google::protobuf::json_internal::ParseOptions opts; opts.ignore_unknown_fields = options.ignore_unknown_fields; opts.case_insensitive_enum_parsing = options.case_insensitive_enum_parsing; @@ -107,8 +114,10 @@ absl::Status JsonStringToMessage(absl::string_view input, Message* message, // TODO: Drop this setting. opts.allow_legacy_syntax = true; - return google::protobuf::json_internal::JsonStringToMessage(input, message, opts); + return google::protobuf::json_internal::JsonStreamToMessage(input, message, opts); } } // namespace json } // namespace protobuf } // namespace google + +#include "google/protobuf/port_undef.inc" diff --git a/src/google/protobuf/json/json.h b/src/google/protobuf/json/json.h index c025a2a5a836..69b1278ed555 100644 --- a/src/google/protobuf/json/json.h +++ b/src/google/protobuf/json/json.h @@ -69,7 +69,7 @@ inline absl::Status MessageToJsonString(const Message& message, return MessageToJsonString(message, output, PrintOptions()); } -// Converts from JSON to protobuf message. This works equivalently to +// Converts from JSON string to protobuf message. This works equivalently to // JsonToBinaryStream(). It will use the DescriptorPool of the passed-in // message to resolve Any types. // @@ -84,6 +84,20 @@ inline absl::Status JsonStringToMessage(absl::string_view input, return JsonStringToMessage(input, message, ParseOptions()); } +// Converts from JSON stream to protobuf message. Similar to JsonStringToMessage +// but with input stream. +// +// Please note that non-OK statuses are not a stable output of this API and +// subject to change without notice. +PROTOBUF_EXPORT absl::Status JsonStreamToMessage(io::ZeroCopyInputStream* input, + Message* message, + const ParseOptions& options); + +inline absl::Status JsonStreamToMessage(io::ZeroCopyInputStream* input, + Message* message) { + return JsonStreamToMessage(input, message, ParseOptions()); +} + // Converts protobuf binary data to JSON. // The conversion will fail if: // 1. TypeResolver fails to resolve a type.