diff options
Diffstat (limited to 'csharp/src/Google.Protobuf/JsonFormatter.cs')
-rw-r--r-- | csharp/src/Google.Protobuf/JsonFormatter.cs | 47 |
1 files changed, 22 insertions, 25 deletions
diff --git a/csharp/src/Google.Protobuf/JsonFormatter.cs b/csharp/src/Google.Protobuf/JsonFormatter.cs index 83772473..a894ffa1 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) + + /// <summary> + /// 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 <see cref="Format(IMessage)"/> + /// or <see cref="Format(IMessage, TextWriter)"/>. + /// </summary> + /// <param name="writer">The writer to write the value to. Must not be null.</param> + /// <param name="value">The value to write. May be null.</param> + 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 { @@ -724,20 +724,6 @@ namespace Google.Protobuf } /// <summary> - /// 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. - /// </summary> - private bool CanWriteSingleValue(object value) - { - if (value is System.Enum) - { - return System.Enum.IsDefined(value.GetType(), value); - } - return true; - } - - /// <summary> /// Writes a string (including leading and trailing double quotes) to a builder, escaping as required. /// </summary> /// <remarks> @@ -899,6 +885,16 @@ namespace Google.Protobuf return originalName; } +#if DOTNET35 + // TODO: Consider adding functionality to TypeExtensions to avoid this difference. + private static Dictionary<object, string> GetNameMapping(System.Type enumType) => + enumType.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static) + .ToDictionary(f => f.GetValue(null), + f => (f.GetCustomAttributes(typeof(OriginalNameAttribute), false) + .FirstOrDefault() as OriginalNameAttribute) + // If the attribute hasn't been applied, fall back to the name of the field. + ?.Name ?? f.Name); +#else private static Dictionary<object, string> GetNameMapping(System.Type enumType) => enumType.GetTypeInfo().DeclaredFields .Where(f => f.IsStatic) @@ -907,6 +903,7 @@ namespace Google.Protobuf .FirstOrDefault() // If the attribute hasn't been applied, fall back to the name of the field. ?.Name ?? f.Name); +#endif } } } |