Exemplo n.º 1
0
            /// <summary>
            /// 属性的描述
            /// </summary>
            /// <param name="scope"></param>
            /// <param name="member"></param>
            public PropertyDescriptor(FormatScope scope, MemberInfo member)
            {
                /*
                 * var aliasAsAttribute = member.GetCustomAttribute<AliasAsAttribute>(true);
                 * if (aliasAsAttribute != null && aliasAsAttribute.IsDefinedScope(scope))
                 * {
                 *  this.AliasName = aliasAsAttribute.Name;
                 * }
                 * else
                 * {
                 *  this.AliasName = member.Name;
                 * }
                 *
                 * var datetimeFormatAttribute = member.GetCustomAttribute<DateTimeFormatAttribute>(true);
                 * if (datetimeFormatAttribute != null && datetimeFormatAttribute.IsDefinedScope(scope))
                 * {
                 *  this.DateTimeFormat = datetimeFormatAttribute.Format;
                 * }
                 *
                 * this.IgnoreSerialized = member.IsDefinedFormatScope<IgnoreSerializedAttribute>(scope);
                 * this.IgnoreWhenNull = member.IsDefinedFormatScope<IgnoreWhenNullAttribute>(scope);
                 */

                this.AliasName      = member.Name;
                this.IgnoreWhenNull = true;
            }
 /// <summary>
 /// 返回是否声明指定的scope
 /// </summary>
 /// <param name="scope">序列化范围</param>
 /// <returns></returns>
 public bool IsDefinedScope(FormatScope scope)
 {
     if (this.Scope == FormatScope.All)
     {
         return(true);
     }
     return(this.Scope.HasFlag(scope));
 }
        /// <summary>
        /// 返回属性解析约定
        /// </summary>
        /// <param name="scope">序列化范围</param>
        /// <param name="camelCase">是否使用CamelCase</param>
        /// <returns></returns>
        public static AnnotationsContractResolver GetResolver(FormatScope scope, bool camelCase)
        {
            switch (scope)
            {
            case FormatScope.JsonFormat:
                return(camelCase ? jsonCamelCaseResolver : jsonNoCamelCaseResolver);

            case FormatScope.KeyValueFormat:
                return(camelCase ? keyValueCamelCaseResolver : keyValueNoCamelCaseResolver);

            default:
                throw new NotImplementedException();
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 返回成员的注解信息
        /// </summary>
        /// <param name="member">成员信息</param>
        /// <param name="scope">序列化范围</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <returns></returns>
        public static Annotations GetAnnotations(MemberInfo member, FormatScope scope)
        {
            if (member == null)
            {
                throw new ArgumentNullException(nameof(member));
            }

            var annotations = new Annotations();
            var attributes  = member.GetCustomAttributes <DataAnnotationAttribute>(true);

            foreach (var item in attributes)
            {
                if (item.IsDefinedScope(scope) == true)
                {
                    item.Invoke(member, annotations);
                }
            }
            return(annotations);
        }
Exemplo n.º 5
0
            /// <summary>
            /// 属性的描述
            /// </summary>
            /// <param name="scope"></param>
            /// <param name="member"></param>
            public PropertyDescriptor(FormatScope scope, MemberInfo member)
            {
                var aliasAsAttribute = member.GetAttribute <AliasAsAttribute>(true);

                if (aliasAsAttribute != null && aliasAsAttribute.IsDefinedScope(scope))
                {
                    this.AliasName = aliasAsAttribute.Name;
                }
                else
                {
                    this.AliasName = member.Name;
                }

                var datetimeFormatAttribute = member.GetAttribute <DateTimeFormatAttribute>(true);

                if (datetimeFormatAttribute != null && datetimeFormatAttribute.IsDefinedScope(scope))
                {
                    this.DateTimeFormat = datetimeFormatAttribute.Format;
                }

                this.IgnoreSerialized = member.IsDefinedFormatScope <IgnoreSerializedAttribute>(scope);
                this.IgnoreWhenNull   = member.IsDefinedFormatScope <IgnoreWhenNullAttribute>(scope);
            }
Exemplo n.º 6
0
 /// <summary>
 /// 属性解析器
 /// </summary>
 /// <param name="camelCase">是否camel命名</param>
 /// <param name="scope">序列化范围</param>
 public PropertyContractResolver(bool camelCase, FormatScope scope)
 {
     this.useCamelCase = camelCase;
     this.formatScope  = scope;
 }
Exemplo n.º 7
0
        /// <summary>
        /// 返回特性是否声明指定的FormatScope
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="element"></param>
        /// <param name="scope"></param>
        /// <returns></returns>
        public static bool IsDefinedFormatScope <T>(this MemberInfo element, FormatScope scope) where T : DataAnnotationAttribute
        {
            var attribute = element.GetCustomAttribute <T>();

            return(attribute != null && attribute.IsDefinedScope(scope));
        }
Exemplo n.º 8
0
 /// <summary>
 /// 返回是否声明指定的scope
 /// </summary>
 /// <param name="scope">序列化范围</param>
 /// <returns></returns>
 public bool IsDefinedScope(FormatScope scope)
 {
     return(scope == (scope & this.Scope));
 }
Exemplo n.º 9
0
 /// <summary>
 /// 数据注解特性抽象
 /// </summary>
 public DataAnnotationAttribute()
 {
     this.Scope = FormatScope.All;
 }
 /// <summary>
 /// 属性解析器
 /// </summary>
 /// <param name="camelCase">是否camel命名</param>
 /// <param name="scope">序列化范围</param>
 private AnnotationsContractResolver(bool camelCase, FormatScope scope)
 {
     this.useCamelCase = camelCase;
     this.formatScope  = scope;
 }
Exemplo n.º 11
0
        /// <summary>
        /// 转换为序列化配置项
        /// </summary>
        /// <param name="options">格式化选项</param>
        /// <param name="formatScope">序列化范围</param>
        /// <returns></returns>
        public static JsonSerializerSettings ToSerializerSettings(this FormatOptions options, FormatScope formatScope)
        {
            var useCamelCase     = options == null ? false : options.UseCamelCase;
            var contractResolver = AnnotationsContractResolver.GetResolver(formatScope, useCamelCase);
            var settings         = new JsonSerializerSettings {
                ContractResolver = contractResolver
            };

            if (options != null)
            {
                settings.DateFormatString  = options.DateTimeFormat;
                settings.NullValueHandling = options.IgnoreNullProperty ? NullValueHandling.Ignore : NullValueHandling.Include;
            }
            return(settings);
        }
Exemplo n.º 12
0
        /// <summary>
        /// 返回属性解析约定
        /// </summary>
        /// <param name="scope">序列化范围</param>
        /// <param name="camelCase">是否使用CamelCase</param>
        /// <exception cref="NotImplementedException"></exception>
        /// <returns></returns>
        public static AnnotationsContractResolver GetResolver(FormatScope scope, bool camelCase)
        {
            var key = new ContractKey(camelCase, scope);

            return(cache.GetOrAdd(key, k => new AnnotationsContractResolver(k)));
        }
Exemplo n.º 13
0
 /// <summary>
 /// ContractResolver缓存的键
 /// </summary>
 /// <param name="camelCase"></param>
 /// <param name="formatScope"></param>
 public ContractKey(bool camelCase, FormatScope formatScope)
 {
     this.CamelCase   = camelCase;
     this.FormatScope = formatScope;
 }