private JsonConverter GetConverterFromAttribute(JsonConverterAttribute converterAttribute, Type typeToConvert, Type classTypeAttributeIsOn, PropertyInfo?propertyInfo) { JsonConverter?converter; Type?type = converterAttribute.ConverterType; if (type == null) { // Allow the attribute to create the converter. converter = converterAttribute.CreateConverter(typeToConvert); if (converter == null) { ThrowHelper.ThrowInvalidOperationException_SerializationConverterOnAttributeNotCompatible(classTypeAttributeIsOn, propertyInfo, typeToConvert); } } else { ConstructorInfo?ctor = type.GetConstructor(Type.EmptyTypes); if (!typeof(JsonConverter).IsAssignableFrom(type) || ctor == null || !ctor.IsPublic) { ThrowHelper.ThrowInvalidOperationException_SerializationConverterOnAttributeInvalid(classTypeAttributeIsOn, propertyInfo); } converter = (JsonConverter)Activator.CreateInstance(type) !; } Debug.Assert(converter != null); if (!converter.CanConvert(typeToConvert)) { ThrowHelper.ThrowInvalidOperationException_SerializationConverterOnAttributeNotCompatible(classTypeAttributeIsOn, propertyInfo, typeToConvert); } return(converter); }
private JsonConverter GetConverterFromAttribute(JsonConverterAttribute converterAttribute, Type typeToConvert, Type classTypeAttributeIsOn, MemberInfo?memberInfo) { JsonConverter?converter; Type?type = converterAttribute.ConverterType; if (type == null) { // Allow the attribute to create the converter. converter = converterAttribute.CreateConverter(typeToConvert); if (converter == null) { ThrowHelper.ThrowInvalidOperationException_SerializationConverterOnAttributeNotCompatible(classTypeAttributeIsOn, memberInfo, typeToConvert); } } else { ConstructorInfo?ctor = type.GetConstructor(Type.EmptyTypes); if (!typeof(JsonConverter).IsAssignableFrom(type) || ctor == null || !ctor.IsPublic) { ThrowHelper.ThrowInvalidOperationException_SerializationConverterOnAttributeInvalid(classTypeAttributeIsOn, memberInfo); } converter = (JsonConverter)Activator.CreateInstance(type) !; } Debug.Assert(converter != null); if (!converter.CanConvert(typeToConvert)) { Type?underlyingType = Nullable.GetUnderlyingType(typeToConvert); if (underlyingType != null && converter.CanConvert(underlyingType)) { if (converter is JsonConverterFactory converterFactory) { converter = converterFactory.GetConverterInternal(underlyingType, this); } // Allow nullable handling to forward to the underlying type's converter. return(NullableConverterFactory.CreateValueConverter(underlyingType, converter)); } ThrowHelper.ThrowInvalidOperationException_SerializationConverterOnAttributeNotCompatible(classTypeAttributeIsOn, memberInfo, typeToConvert); } return(converter); }