Skip to content

Commit 0259cc3

Browse files
Add JsonStreamToMessage method
PiperOrigin-RevId: 673445444
1 parent c4668f3 commit 0259cc3

File tree

4 files changed

+33
-11
lines changed

4 files changed

+33
-11
lines changed

src/google/protobuf/json/internal/parser.cc

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,14 +1271,11 @@ absl::Status ParseMessage(JsonLexer& lex, const Desc<Traits>& desc,
12711271
}
12721272
} // namespace
12731273

1274-
absl::Status JsonStringToMessage(absl::string_view input, Message* message,
1274+
absl::Status JsonStreamToMessage(io::ZeroCopyInputStream* input,
1275+
Message* message,
12751276
json_internal::ParseOptions options) {
12761277
MessagePath path(message->GetDescriptor()->full_name());
1277-
if (PROTOBUF_DEBUG) {
1278-
ABSL_DLOG(INFO) << "json2/input: " << absl::CHexEscape(input);
1279-
}
1280-
io::ArrayInputStream in(input.data(), input.size());
1281-
JsonLexer lex(&in, options, &path);
1278+
JsonLexer lex(input, options, &path);
12821279

12831280
ParseProto2Descriptor::Msg msg(message);
12841281
absl::Status s =
@@ -1360,3 +1357,5 @@ absl::Status JsonToBinaryStream(google::protobuf::util::TypeResolver* resolver,
13601357
} // namespace json_internal
13611358
} // namespace protobuf
13621359
} // namespace google
1360+
1361+
#include "google/protobuf/port_undef.inc"

src/google/protobuf/json/internal/parser.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@
1010

1111
#include <string>
1212

13-
#include "absl/strings/string_view.h"
1413
#include "google/protobuf/json/internal/lexer.h"
1514
#include "google/protobuf/message.h"
1615
#include "google/protobuf/util/type_resolver.h"
1716

1817
namespace google {
1918
namespace protobuf {
2019
namespace json_internal {
21-
// Internal version of google::protobuf::util::JsonStringToMessage; see json_util.h for
20+
// Internal version of google::protobuf::util::JsonStreamToMessage; see json_util.h for
2221
// details.
23-
absl::Status JsonStringToMessage(absl::string_view input, Message* message,
22+
absl::Status JsonStreamToMessage(io::ZeroCopyInputStream* input,
23+
Message* message,
2424
json_internal::ParseOptions options);
2525
// Internal version of google::protobuf::util::JsonToBinaryStream; see json_util.h for
2626
// details.

src/google/protobuf/json/json.cc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,24 @@ absl::Status MessageToJsonString(const Message& message, std::string* output,
100100

101101
absl::Status JsonStringToMessage(absl::string_view input, Message* message,
102102
const ParseOptions& options) {
103+
io::ArrayInputStream input_stream(input.data(), input.size());
104+
return JsonStreamToMessage(&input_stream, message, options);
105+
}
106+
107+
absl::Status JsonStreamToMessage(io::ZeroCopyInputStream* input,
108+
Message* message,
109+
const ParseOptions& options) {
103110
google::protobuf::json_internal::ParseOptions opts;
104111
opts.ignore_unknown_fields = options.ignore_unknown_fields;
105112
opts.case_insensitive_enum_parsing = options.case_insensitive_enum_parsing;
106113

107114
// TODO: Drop this setting.
108115
opts.allow_legacy_syntax = true;
109116

110-
return google::protobuf::json_internal::JsonStringToMessage(input, message, opts);
117+
return google::protobuf::json_internal::JsonStreamToMessage(input, message, opts);
111118
}
112119
} // namespace json
113120
} // namespace protobuf
114121
} // namespace google
122+
123+
#include "google/protobuf/port_undef.inc"

src/google/protobuf/json/json.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ inline absl::Status MessageToJsonString(const Message& message,
6969
return MessageToJsonString(message, output, PrintOptions());
7070
}
7171

72-
// Converts from JSON to protobuf message. This works equivalently to
72+
// Converts from JSON string to protobuf message. This works equivalently to
7373
// JsonToBinaryStream(). It will use the DescriptorPool of the passed-in
7474
// message to resolve Any types.
7575
//
@@ -84,6 +84,20 @@ inline absl::Status JsonStringToMessage(absl::string_view input,
8484
return JsonStringToMessage(input, message, ParseOptions());
8585
}
8686

87+
// Converts from JSON stream to protobuf message. Similar to JsonStringToMessage
88+
// but with input stream.
89+
//
90+
// Please note that non-OK statuses are not a stable output of this API and
91+
// subject to change without notice.
92+
PROTOBUF_EXPORT absl::Status JsonStreamToMessage(io::ZeroCopyInputStream* input,
93+
Message* message,
94+
const ParseOptions& options);
95+
96+
inline absl::Status JsonStreamToMessage(io::ZeroCopyInputStream* input,
97+
Message* message) {
98+
return JsonStreamToMessage(input, message, ParseOptions());
99+
}
100+
87101
// Converts protobuf binary data to JSON.
88102
// The conversion will fail if:
89103
// 1. TypeResolver fails to resolve a type.

0 commit comments

Comments
 (0)