Skip to content

Commit

Permalink
JSON string output funtions should clear() the destination first
Browse files Browse the repository at this point in the history
Make the JSON util string output functions match the Message::SerializeToString() behavior.  This is helpful when packing the output into string fields of reused protobuf messages.
  • Loading branch information
boscosiu authored and acozzette committed Jun 10, 2019
1 parent 6153f80 commit 3cbe45a
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/google/protobuf/util/json_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ util::Status BinaryToJsonString(TypeResolver* resolver,
const std::string& binary_input,
std::string* json_output,
const JsonPrintOptions& options) {
json_output->clear();
io::ArrayInputStream input_stream(binary_input.data(), binary_input.size());
io::StringOutputStream output_stream(json_output);
return BinaryToJsonStream(resolver, type_url, &input_stream, &output_stream,
Expand Down Expand Up @@ -209,6 +210,7 @@ util::Status JsonToBinaryString(TypeResolver* resolver,
StringPiece json_input,
std::string* binary_output,
const JsonParseOptions& options) {
binary_output->clear();
io::ArrayInputStream input_stream(json_input.data(), json_input.size());
io::StringOutputStream output_stream(binary_output);
return JsonToBinaryStream(resolver, type_url, &input_stream, &output_stream,
Expand Down

0 comments on commit 3cbe45a

Please sign in to comment.