Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK committed Jun 26, 2022
1 parent 3ff7677 commit 9ad87f0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 50 deletions.
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 @@ -19,6 +19,17 @@ 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,10 @@ public MessageTypeInfoResolver(JsonContext context)
_context = context;
}

private static bool IsStandardMessage(Type type, [NotNullWhen(true)] out MessageDescriptor? messageDescriptor)
{
if (!typeof(IMessage).IsAssignableFrom(type))
{
messageDescriptor = null;
return false;
}

messageDescriptor = JsonConverterHelper.GetMessageDescriptor(type);
if (messageDescriptor == null)
{
return false;
}
if (ServiceDescriptorHelpers.IsWrapperType(messageDescriptor))
{
return false;
}
if (WellKnownTypeNames.ContainsKey(messageDescriptor.FullName))
{
return false;
}

return true;
}

public override JsonTypeInfo GetTypeInfo(Type type, JsonSerializerOptions options)
{
var typeInfo = base.GetTypeInfo(type, options);

if (IsStandardMessage(type, out var messageDescriptor))
{
typeInfo.Properties.Clear();
Expand All @@ -74,9 +50,37 @@ public override JsonTypeInfo GetTypeInfo(Type type, JsonSerializerOptions option
typeInfo.Properties.Add(propertyInfo);
}
}

return typeInfo;
}

private static bool IsStandardMessage(Type type, [NotNullWhen(true)] out MessageDescriptor? messageDescriptor)
{
if (!typeof(IMessage).IsAssignableFrom(type))
{
messageDescriptor = null;
return false;
}

messageDescriptor = JsonConverterHelper.GetMessageDescriptor(type);
if (messageDescriptor == null)
{
return false;
}

// Wrappers and well known types are handled by converters.
if (ServiceDescriptorHelpers.IsWrapperType(messageDescriptor))
{
return false;
}
if (JsonConverterHelper.WellKnownTypeNames.ContainsKey(messageDescriptor.FullName))
{
return false;
}

return true;
}

private JsonPropertyInfo CreatePropertyInfo(JsonTypeInfo typeInfo, string name, FieldDescriptor field, bool isWritable)
{
var propertyInfo = typeInfo.CreateJsonPropertyInfo(
Expand Down Expand Up @@ -158,15 +162,4 @@ private static Dictionary<string, FieldDescriptor> CreateJsonFieldMap(IList<Fiel
}
return new Dictionary<string, FieldDescriptor>(map);
}

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<>),
};
}

0 comments on commit 9ad87f0

Please sign in to comment.