aboutsummaryrefslogtreecommitdiff
path: root/src/ProtocolBuffers/TextFormat.cs
diff options
context:
space:
mode:
authorJon Skeet <skeet@pobox.com>2014-04-07 17:34:03 +0100
committerJon Skeet <skeet@pobox.com>2014-04-07 17:34:03 +0100
commit8bb0d7288e333e76eee4a04d5d6ed7089f0fa0b0 (patch)
tree52166873ee5959564cad1904934522fa3f304d5b /src/ProtocolBuffers/TextFormat.cs
parent8e04d10daba5bfd1213cc9b6c8554a017250e199 (diff)
downloadprotobuf-8bb0d7288e333e76eee4a04d5d6ed7089f0fa0b0.tar.gz
protobuf-8bb0d7288e333e76eee4a04d5d6ed7089f0fa0b0.tar.bz2
protobuf-8bb0d7288e333e76eee4a04d5d6ed7089f0fa0b0.zip
Add the ability to print a builder (not just a message),
and override ToString in AbstractBuilder. This fixes issue 73.
Diffstat (limited to 'src/ProtocolBuffers/TextFormat.cs')
-rw-r--r--src/ProtocolBuffers/TextFormat.cs26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/ProtocolBuffers/TextFormat.cs b/src/ProtocolBuffers/TextFormat.cs
index 7ea25053..747dce4e 100644
--- a/src/ProtocolBuffers/TextFormat.cs
+++ b/src/ProtocolBuffers/TextFormat.cs
@@ -62,6 +62,16 @@ namespace Google.ProtocolBuffers
}
/// <summary>
+ /// Outputs a textual representation of the Protocol Message builder supplied into
+ /// the parameter output.
+ /// </summary>
+ public static void Print(IBuilder builder, TextWriter output)
+ {
+ TextGenerator generator = new TextGenerator(output, "\n");
+ Print(builder, generator);
+ }
+
+ /// <summary>
/// Outputs a textual representation of <paramref name="fields" /> to <paramref name="output"/>.
/// </summary>
public static void Print(UnknownFieldSet fields, TextWriter output)
@@ -77,6 +87,13 @@ namespace Google.ProtocolBuffers
return text.ToString();
}
+ public static string PrintToString(IBuilder builder)
+ {
+ StringWriter text = new StringWriter();
+ Print(builder, text);
+ return text.ToString();
+ }
+
public static string PrintToString(UnknownFieldSet fields)
{
StringWriter text = new StringWriter();
@@ -93,6 +110,15 @@ namespace Google.ProtocolBuffers
PrintUnknownFields(message.UnknownFields, generator);
}
+ private static void Print(IBuilder message, TextGenerator generator)
+ {
+ foreach (KeyValuePair<FieldDescriptor, object> entry in message.AllFields)
+ {
+ PrintField(entry.Key, entry.Value, generator);
+ }
+ PrintUnknownFields(message.UnknownFields, generator);
+ }
+
internal static void PrintField(FieldDescriptor field, object value, TextGenerator generator)
{
if (field.IsRepeated)