public static JsonValueConverterAttribute GetPropertyValueConverter( Type parentClassType, PropertyInfo propertyInfo, Type propertyType, JsonSerializerOptions options) { if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable <>)) { propertyType = Nullable.GetUnderlyingType(propertyType); } JsonValueConverterAttribute attr = GetPropertyValueConverterInternal(parentClassType, propertyInfo, options, propertyType); // For Enums, support both the type Enum plus strongly-typed Enums. if (attr == null && (propertyType.IsEnum || propertyType == typeof(Enum))) { attr = DefaultConverters.GetPropertyValueConverterInternal(parentClassType, propertyInfo, options, typeof(Enum)); if (attr == null) { attr = DefaultConverters.s_enumConverterAttibute; } } return(attr); }
protected JsonValueConverter <TValue> GetPropertyValueConverter(JsonSerializerOptions options) { JsonValueConverterAttribute attr = DefaultConverters.GetPropertyValueConverter(ParentClassType, PropertyInfo, PropertyType, options); if (attr != null) { return(attr.GetConverter <TValue>()); } return(null); }
private static JsonValueConverterAttribute GetPropertyValueConverterInternal( Type parentClassType, PropertyInfo propertyInfo, JsonSerializerOptions options, Type propertyType) { if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable <>)) { propertyType = Nullable.GetUnderlyingType(propertyType); } JsonValueConverterAttribute attr = null; if (propertyInfo != null) { // Use Property first attr = options.GetAttributes <JsonValueConverterAttribute>(propertyInfo).Where(a => a.PropertyType == propertyType).FirstOrDefault(); if (attr == null) { // Then class type attr = options.GetAttributes <JsonValueConverterAttribute>(parentClassType, inherit: true).Where(a => a.PropertyType == propertyType).FirstOrDefault(); if (attr == null) { // Then declaring assembly attr = options.GetAttributes <JsonValueConverterAttribute>(parentClassType.Assembly).Where(a => a.PropertyType == propertyType).FirstOrDefault(); if (attr == null) { // Then global attr = options.GetAttributes <JsonValueConverterAttribute>(JsonSerializerOptions.GlobalAttributesProvider).Where(a => a.PropertyType == propertyType).FirstOrDefault(); } } } } return(attr); }