Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add message type resolver #42428

Merged
merged 5 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,27 @@ public override void Write(Utf8JsonWriter writer, TMessage value, JsonSerializer
}
else
{
MessageConverter<Any>.WriteMessageFields(writer, valueMessage, Context.Settings, options);
WriteMessageFields(writer, valueMessage, Context.Settings, options);
}

writer.WriteEndObject();
}

internal static void WriteMessageFields(Utf8JsonWriter writer, IMessage message, GrpcJsonSettings settings, JsonSerializerOptions options)
{
var fields = message.Descriptor.Fields;

foreach (var field in fields.InFieldNumberOrder())
{
var accessor = field.Accessor;
var value = accessor.GetValue(message);
if (!JsonConverterHelper.ShouldFormatFieldValue(message, field, value, !settings.IgnoreDefaultValues))
{
continue;
}

writer.WritePropertyName(accessor.Descriptor.JsonName);
JsonSerializer.Serialize(writer, value, value.GetType(), options);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using Google.Protobuf;
using Google.Protobuf.WellKnownTypes;
using Type = System.Type;

namespace Microsoft.AspNetCore.Grpc.JsonTranscoding.Internal.Json;
Expand All @@ -32,14 +31,14 @@ public override bool CanConvert(Type typeToConvert)
return false;
}

return WellKnownTypeNames.ContainsKey(descriptor.FullName);
return JsonConverterHelper.WellKnownTypeNames.ContainsKey(descriptor.FullName);
}

public override JsonConverter CreateConverter(
Type typeToConvert, JsonSerializerOptions options)
{
var descriptor = JsonConverterHelper.GetMessageDescriptor(typeToConvert)!;
var converterType = WellKnownTypeNames[descriptor.FullName];
var converterType = JsonConverterHelper.WellKnownTypeNames[descriptor.FullName];

var converter = (JsonConverter)Activator.CreateInstance(
converterType.MakeGenericType(new Type[] { typeToConvert }),
Expand All @@ -50,15 +49,4 @@ public override JsonConverter CreateConverter(

return converter;
}

private static readonly Dictionary<string, Type> WellKnownTypeNames = new Dictionary<string, Type>
{
[Any.Descriptor.FullName] = typeof(AnyConverter<>),
[Duration.Descriptor.FullName] = typeof(DurationConverter<>),
[Timestamp.Descriptor.FullName] = typeof(TimestampConverter<>),
[FieldMask.Descriptor.FullName] = typeof(FieldMaskConverter<>),
[Struct.Descriptor.FullName] = typeof(StructConverter<>),
[ListValue.Descriptor.FullName] = typeof(ListValueConverter<>),
[Value.Descriptor.FullName] = typeof(ValueConverter<>),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
using Google.Protobuf;
using Google.Protobuf.Collections;
using Google.Protobuf.Reflection;
using Google.Protobuf.WellKnownTypes;
using Grpc.Shared;
Expand All @@ -18,17 +20,33 @@ internal static class JsonConverterHelper
{
internal const int WrapperValueFieldNumber = Int32Value.ValueFieldNumber;

internal static readonly Dictionary<string, Type> WellKnownTypeNames = new Dictionary<string, Type>
{
[Any.Descriptor.FullName] = typeof(AnyConverter<>),
[Duration.Descriptor.FullName] = typeof(DurationConverter<>),
[Timestamp.Descriptor.FullName] = typeof(TimestampConverter<>),
[FieldMask.Descriptor.FullName] = typeof(FieldMaskConverter<>),
[Struct.Descriptor.FullName] = typeof(StructConverter<>),
[ListValue.Descriptor.FullName] = typeof(ListValueConverter<>),
[Value.Descriptor.FullName] = typeof(ValueConverter<>),
};

internal static JsonSerializerOptions CreateSerializerOptions(JsonContext context, bool isStreamingOptions = false)
{
// Streaming is line delimited between messages. That means JSON can't be indented as it adds new lines.
// For streaming to work, indenting must be disabled when streaming.
var writeIndented = !isStreamingOptions ? context.Settings.WriteIndented : false;

var typeInfoResolver = JsonTypeInfoResolver.Combine(
JamesNK marked this conversation as resolved.
Show resolved Hide resolved
new MessageTypeInfoResolver(context),
new DefaultJsonTypeInfoResolver());

var options = new JsonSerializerOptions
{
WriteIndented = writeIndented,
NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
TypeInfoResolver = typeInfoResolver
};
options.Converters.Add(new NullValueConverter());
options.Converters.Add(new ByteStringConverter());
Expand All @@ -38,12 +56,33 @@ internal static JsonSerializerOptions CreateSerializerOptions(JsonContext contex
options.Converters.Add(new JsonConverterFactoryForEnum(context));
options.Converters.Add(new JsonConverterFactoryForWrappers(context));
options.Converters.Add(new JsonConverterFactoryForWellKnownTypes(context));
options.Converters.Add(new JsonConverterFactoryForMessage(context));

return options;
}

internal static Type GetFieldType(FieldDescriptor descriptor)
{
if (descriptor.IsMap)
{
var mapFields = descriptor.MessageType.Fields.InFieldNumberOrder();
var keyField = mapFields[0];
var valueField = mapFields[1];

return typeof(MapField<,>).MakeGenericType(GetFieldTypeCore(keyField), GetFieldTypeCore(valueField));
}
else if (descriptor.IsRepeated)
{
var itemType = GetFieldTypeCore(descriptor);

return typeof(RepeatedField<>).MakeGenericType(itemType);
}
else
{
return GetFieldTypeCore(descriptor);
}
}

private static Type GetFieldTypeCore(FieldDescriptor descriptor)
{
switch (descriptor.FieldType)
{
Expand Down Expand Up @@ -85,6 +124,7 @@ internal static Type GetFieldType(FieldDescriptor descriptor)

return t;
}

return descriptor.MessageType.ClrType;
default:
throw new ArgumentException("Invalid field type");
Expand Down Expand Up @@ -124,7 +164,8 @@ public static void PopulateMap(ref Utf8JsonReader reader, JsonSerializerOptions
public static void PopulateList(ref Utf8JsonReader reader, JsonSerializerOptions options, IMessage message, FieldDescriptor fieldDescriptor)
{
var fieldType = GetFieldType(fieldDescriptor);
var repeatedFieldType = typeof(List<>).MakeGenericType(fieldType);
var itemType = fieldType.GetGenericArguments()[0];
var repeatedFieldType = typeof(List<>).MakeGenericType(itemType);
var newValues = (IList)JsonSerializer.Deserialize(ref reader, repeatedFieldType, options)!;

var existingValue = (IList)fieldDescriptor.Accessor.GetValue(message);
Expand All @@ -133,4 +174,67 @@ public static void PopulateList(ref Utf8JsonReader reader, JsonSerializerOptions
existingValue.Add(item);
}
}

/// <summary>
/// Determines whether or not a field value should be serialized according to the field,
/// its value in the message, and the settings of this formatter.
/// </summary>
public static bool ShouldFormatFieldValue(IMessage message, FieldDescriptor field, object? value, bool formatDefaultValues) =>
field.HasPresence
// Fields that support presence *just* use that
? field.Accessor.HasValue(message)
// Otherwise, format if either we've been asked to format default values, or if it's
// not a default value anyway.
: formatDefaultValues || !IsDefaultValue(field, value);

private static bool IsDefaultValue(FieldDescriptor descriptor, object? value)
{
if (value == null)
{
return true;
}
if (descriptor.IsMap)
{
var dictionary = (IDictionary)value;
return dictionary.Count == 0;
}
if (descriptor.IsRepeated)
{
var list = (IList)value;
return list.Count == 0;
}
switch (descriptor.FieldType)
{
case FieldType.Bool:
return (bool)value == false;
case FieldType.Bytes:
return (ByteString)value == ByteString.Empty;
case FieldType.String:
return (string)value == string.Empty;
case FieldType.Double:
return (double)value == 0.0;
case FieldType.SInt32:
case FieldType.Int32:
case FieldType.SFixed32:
case FieldType.Enum:
return (int)value == 0;
case FieldType.Fixed32:
case FieldType.UInt32:
return (uint)value == 0;
case FieldType.Fixed64:
case FieldType.UInt64:
return (ulong)value == 0;
case FieldType.SFixed64:
case FieldType.Int64:
case FieldType.SInt64:
return (long)value == 0;
case FieldType.Float:
return (float)value == 0f;
case FieldType.Message:
case FieldType.Group: // Never expect to get this, but...
return value == null;
default:
throw new ArgumentException("Invalid field type");
}
}
}
Loading