Пример #1
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);
            var val    = reader.GetValue(value);

            writer.WritePropertyName(key.ToString());
            writer.WriteValue(val);
        }
Пример #2
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);
        }
Пример #3
0
        /// <summary>
        /// 执行转换
        /// </summary>
        /// <param name="context">转换上下文</param>
        /// <returns></returns>
        public override IEnumerable <KeyValuePair <string, string> > Invoke(ConvertContext context)
        {
            var type = context.Type;

            if (type.IsGenericType && type.GetGenericTypeDefinition() == keyValuePairType)
            {
                var reader = KeyValuePairReader.GetReader(type);
                var key    = reader.GetKey(context.Value).ToString();
                var value  = reader.GetValue(context.Value);

                return(new[] { this.GetKeyValuePair(key, value, context.Options) });
            }
            return(this.Next.Invoke(context));
        }
        /// <summary>
        /// 执行转换
        /// </summary>
        /// <param name="context">转换上下文</param>
        /// <returns></returns>
        public override IEnumerable <KeyValuePair <string, string> > Invoke(ConvertContext context)
        {
            var reader = KeyValuePairReader.GetReader(context.DataType);

            if (reader == null)
            {
                return(this.Next.Invoke(context));
            }

            var key   = reader.GetKey(context.Data).ToString();
            var value = reader.GetValue(context.Data);

            var ctx = new ConvertContext(key, value, context.Depths, context.Options);

            return(ctx.ToKeyValuePairs());
        }