From 0421238cc1ab0f671249d967ad5f14e73feccf5e Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Thu, 23 Jun 2016 10:46:21 +0100 Subject: Expose JsonFormatter.WriteValue. This isn't useful to most users, but can be handy in advanced use cases, as requested in #1465. --- csharp/src/Google.Protobuf/JsonFormatter.cs | 36 +++++++++-------------------- 1 file changed, 11 insertions(+), 25 deletions(-) (limited to 'csharp/src/Google.Protobuf') diff --git a/csharp/src/Google.Protobuf/JsonFormatter.cs b/csharp/src/Google.Protobuf/JsonFormatter.cs index 83772473..2aa98cd1 100644 --- a/csharp/src/Google.Protobuf/JsonFormatter.cs +++ b/csharp/src/Google.Protobuf/JsonFormatter.cs @@ -377,8 +377,16 @@ namespace Google.Protobuf throw new ArgumentException("Invalid field type"); } } - - private void WriteValue(TextWriter writer, object value) + + /// + /// Writes a single value to the given writer as JSON. Only types understood by + /// Protocol Buffers can be written in this way. This method is only exposed for + /// advanced use cases; most users should be using + /// or . + /// + /// The writer to write the value to. Must not be null. + /// The value to write. May be null. + public void WriteValue(TextWriter writer, object value) { if (value == null) { @@ -447,15 +455,7 @@ namespace Google.Protobuf } else if (value is IMessage) { - IMessage message = (IMessage) value; - if (message.Descriptor.IsWellKnownType) - { - WriteWellKnownTypeValue(writer, message.Descriptor, value); - } - else - { - WriteMessage(writer, (IMessage)value); - } + Format((IMessage)value, writer); } else { @@ -723,20 +723,6 @@ namespace Google.Protobuf writer.Write(first ? "}" : " }"); } - /// - /// Returns whether or not a singular value can be represented in JSON. - /// Currently only relevant for enums, where unknown values can't be represented. - /// For repeated/map fields, this always returns true. - /// - private bool CanWriteSingleValue(object value) - { - if (value is System.Enum) - { - return System.Enum.IsDefined(value.GetType(), value); - } - return true; - } - /// /// Writes a string (including leading and trailing double quotes) to a builder, escaping as required. /// -- cgit v1.2.3