diff options
author | temporal <temporal@630680e5-0e50-0410-840e-4b1c322b438d> | 2008-08-06 01:12:21 +0000 |
---|---|---|
committer | temporal <temporal@630680e5-0e50-0410-840e-4b1c322b438d> | 2008-08-06 01:12:21 +0000 |
commit | a0f27fcd96c5bf2509ca88cca54f00b78f7b8bc5 (patch) | |
tree | 6d44ab5739d6fc9b956e17eb13a6d34f71094109 /src/google/protobuf/text_format.cc | |
parent | 8ccb79057ee477c36d155f2c507c859934f858dd (diff) | |
download | protobuf-a0f27fcd96c5bf2509ca88cca54f00b78f7b8bc5.tar.gz protobuf-a0f27fcd96c5bf2509ca88cca54f00b78f7b8bc5.tar.bz2 protobuf-a0f27fcd96c5bf2509ca88cca54f00b78f7b8bc5.zip |
Heuristically detect sub-messages when printing unknown fields.
Patch mostly written by Dilip Joseph <dilip.antony.joseph@gmail.com>.
Diffstat (limited to 'src/google/protobuf/text_format.cc')
-rw-r--r-- | src/google/protobuf/text_format.cc | 41 |
1 files changed, 38 insertions, 3 deletions
diff --git a/src/google/protobuf/text_format.cc b/src/google/protobuf/text_format.cc index 63a64db1..d698681c 100644 --- a/src/google/protobuf/text_format.cc +++ b/src/google/protobuf/text_format.cc @@ -728,6 +728,16 @@ bool TextFormat::Parser::MergeFromString(const string& input, return result; } +/* static */ bool TextFormat::PrintUnknownFieldsToString( + const UnknownFieldSet& unknown_fields, + string* output) { + GOOGLE_DCHECK(output) << "output specified is NULL"; + + output->clear(); + io::StringOutputStream output_stream(output); + return PrintUnknownFields(unknown_fields, &output_stream); +} + /* static */ bool TextFormat::Print(const Message& message, io::ZeroCopyOutputStream* output) { TextGenerator generator(output); @@ -738,6 +748,17 @@ bool TextFormat::Parser::MergeFromString(const string& input, return !generator.failed(); } +/* static */ bool TextFormat::PrintUnknownFields( + const UnknownFieldSet& unknown_fields, + io::ZeroCopyOutputStream* output) { + TextGenerator generator(output); + + PrintUnknownFields(unknown_fields, generator); + + // Output false if the generator failed internally. + return !generator.failed(); +} + /* static */ void TextFormat::Print(const Descriptor* descriptor, const Message::Reflection* message, TextGenerator& generator) { @@ -922,9 +943,23 @@ static string PaddedHex(IntType value) { } for (int j = 0; j < field.length_delimited_size(); j++) { generator.Print(field_number); - generator.Print(": \""); - generator.Print(CEscape(field.length_delimited(j))); - generator.Print("\"\n"); + const string& value = field.length_delimited(j); + UnknownFieldSet embedded_unknown_fields; + if (!value.empty() && embedded_unknown_fields.ParseFromString(value)) { + // This field is parseable as a Message. + // So it is probably an embedded message. + generator.Print(" {\n"); + generator.Indent(); + PrintUnknownFields(embedded_unknown_fields, generator); + generator.Outdent(); + generator.Print("}\n"); + } else { + // This field is not parseable as a Message. + // So it is probably just a plain string. + generator.Print(": \""); + generator.Print(CEscape(value)); + generator.Print("\"\n"); + } } for (int j = 0; j < field.group_size(); j++) { generator.Print(field_number); |