Skip to content

Commit

Permalink
Add JsonStreamToMessage method
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 670707551
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Sep 4, 2024
1 parent 5d34147 commit 311609b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
11 changes: 5 additions & 6 deletions src/google/protobuf/json/internal/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1271,14 +1271,11 @@ absl::Status ParseMessage(JsonLexer& lex, const Desc<Traits>& 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 =
Expand Down Expand Up @@ -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"
6 changes: 3 additions & 3 deletions src/google/protobuf/json/internal/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@

#include <string>

#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"

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.
Expand Down
11 changes: 10 additions & 1 deletion src/google/protobuf/json/json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,24 @@ 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;

// 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"
16 changes: 15 additions & 1 deletion src/google/protobuf/json/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand All @@ -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.
Expand Down

0 comments on commit 311609b

Please sign in to comment.