コード例 #1
0
        public static JsonEnumerableConverter GetEnumerableConverter(
            Type parentClassType,
            PropertyInfo propertyInfo,
            Type propertyType,
            JsonSerializerOptions options)
        {
            Type enumerableType;

            if (propertyType.IsGenericType)
            {
                enumerableType = propertyType.GetGenericTypeDefinition();
            }
            else if (propertyType.IsArray)
            {
                enumerableType = typeof(Array);
            }
            else
            {
                enumerableType = propertyType;
            }

            JsonEnumerableConverterAttribute attr = null;

            if (propertyInfo != null)
            {
                // Use Property first
                attr = options.GetAttributes <JsonEnumerableConverterAttribute>(propertyInfo).Where(a => a.EnumerableType == enumerableType).FirstOrDefault();
            }

            if (attr == null)
            {
                // Then class type
                attr = options.GetAttributes <JsonEnumerableConverterAttribute>(parentClassType, inherit: true).Where(a => a.EnumerableType == enumerableType).FirstOrDefault();

                if (attr == null)
                {
                    // Then declaring assembly
                    attr = options.GetAttributes <JsonEnumerableConverterAttribute>(parentClassType.Assembly).Where(a => a.EnumerableType == enumerableType).FirstOrDefault();

                    if (attr == null)
                    {
                        // Then global
                        attr = options.GetAttributes <JsonEnumerableConverterAttribute>(JsonSerializerOptions.GlobalAttributesProvider).Where(a => a.EnumerableType == enumerableType).FirstOrDefault();

                        // Then default
                        if (enumerableType == typeof(Array))
                        {
                            attr = s_arrayConverterAttibute;
                        }
                    }
                }
            }

            if (attr != null)
            {
                return(attr.CreateConverter());
            }

            return(null);
        }
コード例 #2
0
        public static TAttribute GetPolicy <TAttribute>(
            Type parentClassType,
            PropertyInfo propertyInfo,
            JsonSerializerOptions options) where TAttribute : Attribute
        {
            TAttribute attr = null;

            if (propertyInfo != null)
            {
                // Use Property first
                attr = options.GetAttributes <TAttribute>(propertyInfo).FirstOrDefault();
            }

            if (attr == null)
            {
                // Then class type
                attr = options.GetAttributes <TAttribute>(parentClassType, inherit: true).FirstOrDefault();

                if (attr == null)
                {
                    // Then declaring assembly
                    attr = options.GetAttributes <TAttribute>(parentClassType.Assembly).FirstOrDefault();

                    if (attr == null)
                    {
                        // Then global
                        attr = options.GetAttributes <TAttribute>(JsonSerializerOptions.GlobalAttributesProvider).FirstOrDefault();
                    }
                }
            }

            return(attr);
        }
コード例 #3
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);
        }
コード例 #4
0
        public static TValue GetPropertyClassAssemblyPolicy <TValue>(
            Type parentClassType,
            PropertyInfo propertyInfo,
            JsonSerializerOptions options,
            Func <JsonPropertyValueAttribute, TValue> selector)
        {
            TValue value;

            JsonPropertyValueAttribute attr = null;

            if (propertyInfo != null)
            {
                // Use Property first
                attr = options.GetAttributes <JsonPropertyValueAttribute>(propertyInfo).FirstOrDefault();
            }

            if (attr == null || (value = selector(attr)) == default)
            {
                // Then class type
                attr = options.GetAttributes <JsonPropertyValueAttribute>(parentClassType, inherit: true).FirstOrDefault();
                if (attr == null || (value = selector(attr)) == default)
                {
                    // Then declaring assembly
                    attr = options.GetAttributes <JsonPropertyValueAttribute>(parentClassType.Assembly).FirstOrDefault();

                    if (attr == null || (value = selector(attr)) == default)
                    {
                        // Then global
                        attr = options.GetAttributes <JsonPropertyValueAttribute>(JsonSerializerOptions.GlobalAttributesProvider).FirstOrDefault();

                        if (attr == null)
                        {
                            value = default;
                        }
                        else
                        {
                            value = selector(attr);
                        }
                    }
                }
            }

            return(value);
        }