7
7
using System . Text . Json . Serialization . Metadata ;
8
8
using Google . Protobuf ;
9
9
using Google . Protobuf . Reflection ;
10
- using Google . Protobuf . WellKnownTypes ;
11
10
using Grpc . Shared ;
12
11
using Type = System . Type ;
13
12
14
13
namespace Microsoft . AspNetCore . Grpc . JsonTranscoding . Internal . Json ;
15
14
16
- internal sealed class MessageTypeInfoResolver : DefaultJsonTypeInfoResolver
15
+ internal sealed class MessageTypeInfoResolver : IJsonTypeInfoResolver
17
16
{
18
17
private readonly JsonContext _context ;
19
18
@@ -22,33 +21,34 @@ public MessageTypeInfoResolver(JsonContext context)
22
21
_context = context ;
23
22
}
24
23
25
- public override JsonTypeInfo GetTypeInfo ( Type type , JsonSerializerOptions options )
24
+ public JsonTypeInfo ? GetTypeInfo ( Type type , JsonSerializerOptions options )
26
25
{
27
- var typeInfo = base . GetTypeInfo ( type , options ) ;
28
-
29
- if ( IsStandardMessage ( type , out var messageDescriptor ) )
26
+ if ( ! IsStandardMessage ( type , out var messageDescriptor ) )
30
27
{
31
- typeInfo . Properties . Clear ( ) ;
28
+ return null ;
29
+ }
32
30
33
- var fields = messageDescriptor . Fields . InFieldNumberOrder ( ) ;
34
- var mappings = CreateJsonFieldMap ( fields ) ;
31
+ var typeInfo = JsonTypeInfo . CreateJsonTypeInfo ( type , options ) ;
32
+ typeInfo . CreateObject = ( ) => Activator . CreateInstance ( type ) ! ;
35
33
36
- foreach ( var field in fields )
37
- {
38
- mappings . Remove ( field . JsonName ) ;
34
+ var fields = messageDescriptor . Fields . InFieldNumberOrder ( ) ;
35
+ var mappings = CreateJsonFieldMap ( fields ) ;
39
36
40
- var propertyInfo = CreatePropertyInfo ( typeInfo , field . JsonName , field , isWritable : true ) ;
41
- typeInfo . Properties . Add ( propertyInfo ) ;
42
- }
37
+ foreach ( var field in fields )
38
+ {
39
+ mappings . Remove ( field . JsonName ) ;
43
40
44
- // Fields have two mappings: the original field name and the camelcased JSON name.
45
- // The JSON name can also be customized in proto with json_name option.
46
- // Add extra setter only properties for mappings that haven't already been added.
47
- foreach ( var mapping in mappings )
48
- {
49
- var propertyInfo = CreatePropertyInfo ( typeInfo , mapping . Key , mapping . Value , isWritable : false ) ;
50
- typeInfo . Properties . Add ( propertyInfo ) ;
51
- }
41
+ var propertyInfo = CreatePropertyInfo ( typeInfo , field . JsonName , field , isWritable : true ) ;
42
+ typeInfo . Properties . Add ( propertyInfo ) ;
43
+ }
44
+
45
+ // Fields have two mappings: the original field name and the camelcased JSON name.
46
+ // The JSON name can also be customized in proto with json_name option.
47
+ // Add extra setter only properties for mappings that haven't already been added.
48
+ foreach ( var mapping in mappings )
49
+ {
50
+ var propertyInfo = CreatePropertyInfo ( typeInfo , mapping . Key , mapping . Value , isWritable : false ) ;
51
+ typeInfo . Properties . Add ( propertyInfo ) ;
52
52
}
53
53
54
54
return typeInfo ;
0 commit comments