Add JsonStreamToMessage method

PiperOrigin-RevId: 673445444
pull/18081/head
Protobuf Team Bot 3 months ago committed by Copybara-Service
parent c4668f341f
commit 0259cc3980
  1. 11
      src/google/protobuf/json/internal/parser.cc
  2. 6
      src/google/protobuf/json/internal/parser.h
  3. 11
      src/google/protobuf/json/json.cc
  4. 16
      src/google/protobuf/json/json.h

@ -1271,14 +1271,11 @@ absl::Status ParseMessage(JsonLexer& lex, const Desc<Traits>& desc,
} }
} // namespace } // namespace
absl::Status JsonStringToMessage(absl::string_view input, Message* message, absl::Status JsonStreamToMessage(io::ZeroCopyInputStream* input,
Message* message,
json_internal::ParseOptions options) { json_internal::ParseOptions options) {
MessagePath path(message->GetDescriptor()->full_name()); MessagePath path(message->GetDescriptor()->full_name());
if (PROTOBUF_DEBUG) { JsonLexer lex(input, options, &path);
ABSL_DLOG(INFO) << "json2/input: " << absl::CHexEscape(input);
}
io::ArrayInputStream in(input.data(), input.size());
JsonLexer lex(&in, options, &path);
ParseProto2Descriptor::Msg msg(message); ParseProto2Descriptor::Msg msg(message);
absl::Status s = absl::Status s =
@ -1360,3 +1357,5 @@ absl::Status JsonToBinaryStream(google::protobuf::util::TypeResolver* resolver,
} // namespace json_internal } // namespace json_internal
} // namespace protobuf } // namespace protobuf
} // namespace google } // namespace google
#include "google/protobuf/port_undef.inc"

@ -10,7 +10,6 @@
#include <string> #include <string>
#include "absl/strings/string_view.h"
#include "google/protobuf/json/internal/lexer.h" #include "google/protobuf/json/internal/lexer.h"
#include "google/protobuf/message.h" #include "google/protobuf/message.h"
#include "google/protobuf/util/type_resolver.h" #include "google/protobuf/util/type_resolver.h"
@ -18,9 +17,10 @@
namespace google { namespace google {
namespace protobuf { namespace protobuf {
namespace json_internal { 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. // details.
absl::Status JsonStringToMessage(absl::string_view input, Message* message, absl::Status JsonStreamToMessage(io::ZeroCopyInputStream* input,
Message* message,
json_internal::ParseOptions options); json_internal::ParseOptions options);
// Internal version of google::protobuf::util::JsonToBinaryStream; see json_util.h for // Internal version of google::protobuf::util::JsonToBinaryStream; see json_util.h for
// details. // details.

@ -100,6 +100,13 @@ absl::Status MessageToJsonString(const Message& message, std::string* output,
absl::Status JsonStringToMessage(absl::string_view input, Message* message, absl::Status JsonStringToMessage(absl::string_view input, Message* message,
const ParseOptions& options) { 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; google::protobuf::json_internal::ParseOptions opts;
opts.ignore_unknown_fields = options.ignore_unknown_fields; opts.ignore_unknown_fields = options.ignore_unknown_fields;
opts.case_insensitive_enum_parsing = options.case_insensitive_enum_parsing; 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. // TODO: Drop this setting.
opts.allow_legacy_syntax = true; 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 json
} // namespace protobuf } // namespace protobuf
} // namespace google } // namespace google
#include "google/protobuf/port_undef.inc"

@ -69,7 +69,7 @@ inline absl::Status MessageToJsonString(const Message& message,
return MessageToJsonString(message, output, PrintOptions()); 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 // JsonToBinaryStream(). It will use the DescriptorPool of the passed-in
// message to resolve Any types. // message to resolve Any types.
// //
@ -84,6 +84,20 @@ inline absl::Status JsonStringToMessage(absl::string_view input,
return JsonStringToMessage(input, message, ParseOptions()); 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. // Converts protobuf binary data to JSON.
// The conversion will fail if: // The conversion will fail if:
// 1. TypeResolver fails to resolve a type. // 1. TypeResolver fails to resolve a type.

Loading…
Cancel
Save