33using System . Text . Json . Serialization ;
44using Google . Protobuf ;
55using Google . Protobuf . Reflection ;
6- using Google . Protobuf . WellKnownTypes ;
76using Protobuf . System . Text . Json . InternalConverters ;
87using Type = System . Type ;
98
@@ -33,7 +32,7 @@ public ProtobufConverter(JsonSerializerOptions jsonSerializerOptions, JsonProtob
3332 Accessor = fieldDescriptor . Accessor ,
3433 IsRepeated = fieldDescriptor . IsRepeated ,
3534 IsMap = fieldDescriptor . IsMap ,
36- FieldType = GetFieldType ( fieldDescriptor , propertyTypeLookup ) ,
35+ FieldType = FieldTypeResolver . ResolverFieldType ( fieldDescriptor , propertyTypeLookup ) ,
3736 JsonName = convertNameFunc ( fieldDescriptor ) ,
3837 IsOneOf = fieldDescriptor . ContainingOneof != null
3938 } ) . ToArray ( ) ;
@@ -142,59 +141,4 @@ public override void Write(Utf8JsonWriter writer, T? value, JsonSerializerOption
142141
143142 writer . WriteEndObject ( ) ;
144143 }
145-
146- private Type GetFieldType ( FieldDescriptor fieldDescriptor , Dictionary < string , Type > propertyTypeLookup )
147- {
148- switch ( fieldDescriptor . FieldType )
149- {
150- case FieldType . Double :
151- return typeof ( double ) ;
152- case FieldType . Float :
153- return typeof ( float ) ;
154- case FieldType . Int32 :
155- case FieldType . SInt32 :
156- case FieldType . SFixed32 :
157- return typeof ( int ) ;
158- case FieldType . Int64 :
159- case FieldType . SInt64 :
160- case FieldType . SFixed64 :
161- return typeof ( long ) ;
162- case FieldType . UInt32 :
163- case FieldType . Fixed32 :
164- return typeof ( uint ) ;
165- case FieldType . UInt64 :
166- case FieldType . Fixed64 :
167- return typeof ( ulong ) ;
168- case FieldType . Bool :
169- return typeof ( bool ) ;
170- case FieldType . String :
171- return typeof ( string ) ;
172- case FieldType . Message when fieldDescriptor . MessageType . ClrType is { } clrType :
173- if ( clrType == typeof ( DoubleValue ) )
174- return typeof ( double ? ) ;
175- if ( clrType == typeof ( FloatValue ) )
176- return typeof ( float ? ) ;
177- if ( clrType == typeof ( Int64Value ) )
178- return typeof ( long ? ) ;
179- if ( clrType == typeof ( UInt64Value ) )
180- return typeof ( ulong ? ) ;
181- if ( clrType == typeof ( Int32Value ) )
182- return typeof ( int ? ) ;
183- if ( clrType == typeof ( UInt32Value ) )
184- return typeof ( uint ? ) ;
185- if ( clrType == typeof ( BoolValue ) )
186- return typeof ( bool ? ) ;
187- if ( clrType == typeof ( StringValue ) )
188- return typeof ( string ) ;
189- return clrType ;
190- case FieldType . Enum when fieldDescriptor . IsRepeated :
191- var fieldType = propertyTypeLookup [ fieldDescriptor . PropertyName ] ;
192- return fieldType . GenericTypeArguments [ 0 ] ;
193- case FieldType . Enum :
194- case FieldType . Message :
195- return propertyTypeLookup [ fieldDescriptor . PropertyName ] ;
196- default :
197- throw new ArgumentOutOfRangeException ( nameof ( fieldDescriptor ) , $ "FieldType: '{ fieldDescriptor } ' is not supported.") ;
198- }
199- }
200144}
0 commit comments