示例#1
0
        public static PropertyValueConverterAttribute GetPropertyValueConverter(
            Type parentClassType,
            PropertyInfo propertyInfo,
            Type propertyType,
            JsonSerializerOptions options)
        {
            if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                propertyType = Nullable.GetUnderlyingType(propertyType);
            }

            PropertyValueConverterAttribute 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);
        }
示例#2
0
        private static PropertyValueConverterAttribute GetPropertyValueConverterInternal(
            Type parentClassType,
            PropertyInfo propertyInfo,
            JsonSerializerOptions options,
            Type propertyType)
        {
            if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                propertyType = Nullable.GetUnderlyingType(propertyType);
            }

            PropertyValueConverterAttribute attr = null;

            if (propertyInfo != null)
            {
                // Use Property first
                attr = options.GetAttributes <PropertyValueConverterAttribute>(propertyInfo).Where(a => a.PropertyType == propertyType).FirstOrDefault();

                if (attr == null)
                {
                    // Then class type
                    attr = options.GetAttributes <PropertyValueConverterAttribute>(parentClassType, inherit: true).Where(a => a.PropertyType == propertyType).FirstOrDefault();
                    if (attr == null)
                    {
                        // Then declaring assembly
                        attr = options.GetAttributes <PropertyValueConverterAttribute>(parentClassType.Assembly).Where(a => a.PropertyType == propertyType).FirstOrDefault();

                        if (attr == null)
                        {
                            // Then global
                            attr = options.GetAttributes <PropertyValueConverterAttribute>(JsonSerializerOptions.GlobalAttributesProvider).Where(a => a.PropertyType == propertyType).FirstOrDefault();

                            if (attr == null)
                            {
                                // Then default
                                if (propertyType == typeof(DateTime))
                                {
                                    attr = DefaultConverters.s_dateConverterAttibute;
                                }
                            }
                        }
                    }
                }
            }

            return(attr);
        }
示例#3
0
        protected PropertyValueConverterAttribute GetPropertyValueConverter(JsonSerializerOptions options)
        {
            PropertyValueConverterAttribute attr = DefaultConverters.GetPropertyValueConverter(ParentClassType, PropertyInfo, PropertyType, options);

            return(attr);
        }