Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/dotnet/aspnetcore.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Newton-King <james@newtonking.com>2022-06-27 16:24:24 +0300
committerJames Newton-King <james@newtonking.com>2022-06-27 16:24:24 +0300
commit739f60f8b492b01bfbc6e5da2816171085b0a72f (patch)
tree861e6a3be3ec90fc42959e8b259547708ef28937
parent5f7b9f7eff8d03552014dbcddf499072c625835d (diff)
Refactor type info resolverjamesnk/contractresolver
-rw-r--r--src/Grpc/JsonTranscoding/src/Microsoft.AspNetCore.Grpc.JsonTranscoding/Internal/Json/JsonConverterHelper.cs7
-rw-r--r--src/Grpc/JsonTranscoding/src/Microsoft.AspNetCore.Grpc.JsonTranscoding/Internal/Json/MessageTypeInfoResolver.cs46
2 files changed, 29 insertions, 24 deletions
diff --git a/src/Grpc/JsonTranscoding/src/Microsoft.AspNetCore.Grpc.JsonTranscoding/Internal/Json/JsonConverterHelper.cs b/src/Grpc/JsonTranscoding/src/Microsoft.AspNetCore.Grpc.JsonTranscoding/Internal/Json/JsonConverterHelper.cs
index 3af0d23b79..2feb6fe063 100644
--- a/src/Grpc/JsonTranscoding/src/Microsoft.AspNetCore.Grpc.JsonTranscoding/Internal/Json/JsonConverterHelper.cs
+++ b/src/Grpc/JsonTranscoding/src/Microsoft.AspNetCore.Grpc.JsonTranscoding/Internal/Json/JsonConverterHelper.cs
@@ -6,6 +6,7 @@ using System.Reflection;
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;
@@ -36,12 +37,16 @@ internal static class JsonConverterHelper
// For streaming to work, indenting must be disabled when streaming.
var writeIndented = !isStreamingOptions ? context.Settings.WriteIndented : false;
+ var typeInfoResolver = JsonTypeInfoResolver.Combine(
+ new MessageTypeInfoResolver(context),
+ new DefaultJsonTypeInfoResolver());
+
var options = new JsonSerializerOptions
{
WriteIndented = writeIndented,
NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
- TypeInfoResolver = new MessageTypeInfoResolver(context)
+ TypeInfoResolver = typeInfoResolver
};
options.Converters.Add(new NullValueConverter());
options.Converters.Add(new ByteStringConverter());
diff --git a/src/Grpc/JsonTranscoding/src/Microsoft.AspNetCore.Grpc.JsonTranscoding/Internal/Json/MessageTypeInfoResolver.cs b/src/Grpc/JsonTranscoding/src/Microsoft.AspNetCore.Grpc.JsonTranscoding/Internal/Json/MessageTypeInfoResolver.cs
index e788d1e0b3..e9caacc8d2 100644
--- a/src/Grpc/JsonTranscoding/src/Microsoft.AspNetCore.Grpc.JsonTranscoding/Internal/Json/MessageTypeInfoResolver.cs
+++ b/src/Grpc/JsonTranscoding/src/Microsoft.AspNetCore.Grpc.JsonTranscoding/Internal/Json/MessageTypeInfoResolver.cs
@@ -7,13 +7,12 @@ using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
using Google.Protobuf;
using Google.Protobuf.Reflection;
-using Google.Protobuf.WellKnownTypes;
using Grpc.Shared;
using Type = System.Type;
namespace Microsoft.AspNetCore.Grpc.JsonTranscoding.Internal.Json;
-internal sealed class MessageTypeInfoResolver : DefaultJsonTypeInfoResolver
+internal sealed class MessageTypeInfoResolver : IJsonTypeInfoResolver
{
private readonly JsonContext _context;
@@ -22,33 +21,34 @@ internal sealed class MessageTypeInfoResolver : DefaultJsonTypeInfoResolver
_context = context;
}
- public override JsonTypeInfo GetTypeInfo(Type type, JsonSerializerOptions options)
+ public JsonTypeInfo? GetTypeInfo(Type type, JsonSerializerOptions options)
{
- var typeInfo = base.GetTypeInfo(type, options);
-
- if (IsStandardMessage(type, out var messageDescriptor))
+ if (!IsStandardMessage(type, out var messageDescriptor))
{
- typeInfo.Properties.Clear();
+ return null;
+ }
- var fields = messageDescriptor.Fields.InFieldNumberOrder();
- var mappings = CreateJsonFieldMap(fields);
+ var typeInfo = JsonTypeInfo.CreateJsonTypeInfo(type, options);
+ typeInfo.CreateObject = () => Activator.CreateInstance(type)!;
- foreach (var field in fields)
- {
- mappings.Remove(field.JsonName);
+ var fields = messageDescriptor.Fields.InFieldNumberOrder();
+ var mappings = CreateJsonFieldMap(fields);
- var propertyInfo = CreatePropertyInfo(typeInfo, field.JsonName, field, isWritable: true);
- typeInfo.Properties.Add(propertyInfo);
- }
+ foreach (var field in fields)
+ {
+ mappings.Remove(field.JsonName);
- // Fields have two mappings: the original field name and the camelcased JSON name.
- // The JSON name can also be customized in proto with json_name option.
- // Add extra setter only properties for mappings that haven't already been added.
- foreach (var mapping in mappings)
- {
- var propertyInfo = CreatePropertyInfo(typeInfo, mapping.Key, mapping.Value, isWritable: false);
- typeInfo.Properties.Add(propertyInfo);
- }
+ var propertyInfo = CreatePropertyInfo(typeInfo, field.JsonName, field, isWritable: true);
+ typeInfo.Properties.Add(propertyInfo);
+ }
+
+ // Fields have two mappings: the original field name and the camelcased JSON name.
+ // The JSON name can also be customized in proto with json_name option.
+ // Add extra setter only properties for mappings that haven't already been added.
+ foreach (var mapping in mappings)
+ {
+ var propertyInfo = CreatePropertyInfo(typeInfo, mapping.Key, mapping.Value, isWritable: false);
+ typeInfo.Properties.Add(propertyInfo);
}
return typeInfo;