コード例 #1
0
        /// <summary>
        /// 创建属性
        /// </summary>
        /// <param name="member"></param>
        /// <param name="memberSerialization"></param>
        /// <returns></returns>
        protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
        {
            var property   = base.CreateProperty(member, memberSerialization);
            var descriptor = new PropertyDescriptor(this.formatScope, member);

            property.PropertyName = descriptor.AliasName;
            property.Ignored      = descriptor.IgnoreSerialized;

            if (this.useCamelCase == true)
            {
                property.PropertyName = FormatOptions.CamelCase(property.PropertyName);
            }

            if (property.Converter == null && descriptor.DateTimeFormat != null)
            {
                property.Converter = new IsoDateTimeConverter {
                    DateTimeFormat = descriptor.DateTimeFormat
                };
            }

            if (descriptor.IgnoreWhenNull == true)
            {
                property.NullValueHandling = NullValueHandling.Ignore;
            }
            return(property);
        }
コード例 #2
0
 public void Test_CamelCase()
 {
     Assert.True(FormatOptions.CamelCase("WebApiClient") == "webApiClient");
     Assert.True(FormatOptions.CamelCase("AWebApiClient") == "aWebApiClient");
     Assert.True(FormatOptions.CamelCase("aWebApiClient") == "aWebApiClient");
     Assert.True(FormatOptions.CamelCase("ABWebApiClient") == "abWebApiClient");
 }
コード例 #3
0
        /// <summary>
        /// 转换为键值对
        /// </summary>
        /// <returns></returns>
        public KeyValuePair <string, string> ToKeyValuePair()
        {
            var key = this.Name;

            if (this.Options.UseCamelCase == true)
            {
                key = FormatOptions.CamelCase(key);
            }

            if (this.Data == null)
            {
                return(new KeyValuePair <string, string>(key, null));
            }

            var dataType = Nullable.GetUnderlyingType(this.DataType);

            if (dataType == null)
            {
                dataType = this.DataType;
            }

            if (dataType == typeof(DateTime))
            {
                var dateTimeString = this.Options.FormatDateTime((DateTime)this.Data);
                return(new KeyValuePair <string, string>(key, dateTimeString));
            }

            if (dataType.IsEnum == true)
            {
                var enumValueString = ((int)this.Data).ToString();
                return(new KeyValuePair <string, string>(key, enumValueString));
            }

            return(new KeyValuePair <string, string>(key, this.Data.ToString()));
        }
コード例 #4
0
        /// <summary>
        /// 创建Json属性
        /// </summary>
        /// <param name="member">属性</param>
        /// <param name="memberSerialization"></param>
        /// <returns></returns>
        protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
        {
            var property    = base.CreateProperty(member, memberSerialization);
            var annotations = Annotations.GetAnnotations(member, this.formatScope);

            if (string.IsNullOrEmpty(annotations.AliasName) == false)
            {
                property.PropertyName = annotations.AliasName;
            }
            else if (this.useCamelCase == true)
            {
                property.PropertyName = FormatOptions.CamelCase(property.PropertyName);
            }

            if (string.IsNullOrEmpty(annotations.DateTimeFormat) == false)
            {
                property.Converter = new IsoDateTimeConverter {
                    DateTimeFormat = annotations.DateTimeFormat
                };
            }

            if (annotations.IgnoreWhenNull == true)
            {
                property.NullValueHandling = NullValueHandling.Ignore;
            }

            property.Ignored = annotations.IgnoreSerialized;
            return(property);
        }
コード例 #5
0
        /// <summary>
        /// 结合options约定
        /// 将value值转换为键值对
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="value">值</param>
        /// <param name="options">选项</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <returns></returns>
        protected KeyValuePair <string, string> GetKeyValuePair(string name, object value, FormatOptions options)
        {
            if (string.IsNullOrEmpty(name) == true)
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (options == null)
            {
                options = new FormatOptions();
            }

            if (options.UseCamelCase == true)
            {
                name = FormatOptions.CamelCase(name);
            }

            if (value == null)
            {
                return(new KeyValuePair <string, string>(name, null));
            }

            var isDateTime = value is DateTime;

            if (isDateTime == true)
            {
                var dateTimeString = ((DateTime)value).ToString(options.DateTimeFormat, DateTimeFormatInfo.InvariantInfo);
                return(new KeyValuePair <string, string>(name, dateTimeString));
            }

            return(new KeyValuePair <string, string>(name, value.ToString()));
        }
コード例 #6
0
        /// <summary>
        /// 简单类型转换为键值对
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="value">值</param>
        /// <param name="options">选项</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <returns></returns>
        protected KeyValuePair <string, string> SerializeAsSimple(
            string name,
            object value,
            FormatOptions options)
        {
            if (string.IsNullOrEmpty(name) == true)
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (options.UseCamelCase == true)
            {
                name = FormatOptions.CamelCase(name);
            }

            if (value == null)
            {
                return(new KeyValuePair <string, string>(name, null));
            }

            var isDateTime = value is DateTime;

            if (isDateTime == false)
            {
                return(new KeyValuePair <string, string>(name, value.ToString()));
            }

            // 时间格式转换
            var dateTime = ((DateTime)value).ToString(options.DateTimeFormat, DateTimeFormatInfo.InvariantInfo);

            return(new KeyValuePair <string, string>(name, dateTime));
        }
コード例 #7
0
            /// <summary>
            /// 访问成员时
            /// </summary>
            /// <param name="node"></param>
            /// <returns></returns>
            protected override Expression VisitMember(MemberExpression node)
            {
                var name = nameCache.GetOrAdd(node.Member, m => this.nameFunc(m));

                if (this.camelCase == true)
                {
                    name = FormatOptions.CamelCase(name);
                }

                this.path.Insert(0, $"/{name}");
                return(base.VisitMember(node));
            }
コード例 #8
0
        /// <summary>
        /// 解析为json
        /// 实际解析为KeyValuePair类型
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="value"></param>
        /// <param name="serializer"></param>
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            var reader = KeyValuePairReader.GetReader(value.GetType());
            var key    = reader.GetKey(value).ToString();
            var val    = reader.GetValue(value);

            if (this.useCamelCase == true)
            {
                key = FormatOptions.CamelCase(key);
            }

            writer.WritePropertyName(key);
            writer.WriteValue(val);
        }
コード例 #9
0
            /// <summary>
            /// <summary>
            /// 创建属性
            /// </summary>
            /// <param name="member"></param>
            /// <param name="memberSerialization"></param>
            /// <returns></returns>
            protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
            {
                var property   = base.CreateProperty(member, memberSerialization);
                var descriptor = PropertyDescriptor.GetDescriptor(member);

                property.PropertyName = descriptor.AliasName;
                if (this.useCamelCase == true)
                {
                    property.PropertyName = FormatOptions.CamelCase(property.PropertyName);
                }

                if (property.Converter != null)
                {
                    property.Converter = descriptor.DateTimeConverter;
                }

                property.Ignored = descriptor.IsIgnoreSerialized;
                return(property);
            }
コード例 #10
0
        /// <summary>
        /// 字典Key的CamelCase
        /// </summary>
        /// <param name="dictionaryKey"></param>
        /// <returns></returns>
        protected override string ResolveDictionaryKey(string dictionaryKey)
        {
            var name = base.ResolveDictionaryKey(dictionaryKey);

            return(this.useCamelCase ? FormatOptions.CamelCase(name) : name);
        }
コード例 #11
0
        /// <summary>
        /// 类型属性的的CamelCase
        /// </summary>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        protected override string ResolvePropertyName(string propertyName)
        {
            var name = base.ResolvePropertyName(propertyName);

            return(this.useCamelCase ? FormatOptions.CamelCase(name) : name);
        }