1- using System ;
1+ using Newtonsoft . Json ;
2+ using System ;
23using System . Collections . Concurrent ;
34using System . Collections . Generic ;
45
5- namespace WebApiClient . Defaults . KeyValueFormats . Converters
6+ namespace WebApiClient . Defaults
67{
78 /// <summary>
8- /// 表示KeyValuePair类型转换器
9+ /// 表示KeyValuePair转换器
910 /// </summary>
10- public class KeyValuePairConverter : ConverterBase
11+ class KeyValuePairConverter : JsonConverter
1112 {
1213 /// <summary>
13- /// 执行转换
14+ /// KeyValuePair泛型
1415 /// </summary>
15- /// <param name="context">转换上下文</param>
16+ private static readonly Type keyValuePairType = typeof ( KeyValuePair < , > ) ;
17+
18+ /// <summary>
19+ /// 返回是否支持转换目标类型
20+ /// </summary>
21+ /// <param name="objectType">目标类型</param>
1622 /// <returns></returns>
17- public override IEnumerable < KeyValuePair < string , string > > Invoke ( ConvertContext context )
23+ public override bool CanConvert ( Type objectType )
1824 {
19- var reader = KeyValuePairReader . GetReader ( context . DataType ) ;
20- if ( reader == null )
21- {
22- return this . Next . Invoke ( context ) ;
23- }
25+ return objectType . IsGenericType && objectType . GetGenericTypeDefinition ( ) == keyValuePairType ;
26+ }
27+
28+ /// <summary>
29+ /// 从json解析得到对象
30+ /// </summary>
31+ /// <param name="reader"></param>
32+ /// <param name="objectType"></param>
33+ /// <param name="existingValue"></param>
34+ /// <param name="serializer"></param>
35+ /// <returns></returns>
36+ public override object ReadJson ( JsonReader reader , Type objectType , object existingValue , JsonSerializer serializer )
37+ {
38+ throw new NotImplementedException ( ) ;
39+ }
2440
25- var key = reader . GetKey ( context . Data ) . ToString ( ) ;
26- var value = reader . GetValue ( context . Data ) ;
41+ /// <summary>
42+ /// 解析为json
43+ /// 实际解析为KeyValuePair类型
44+ /// </summary>
45+ /// <param name="writer"></param>
46+ /// <param name="value"></param>
47+ /// <param name="serializer"></param>
48+ public override void WriteJson ( JsonWriter writer , object value , JsonSerializer serializer )
49+ {
50+ var reader = KeyValuePairReader . GetReader ( value . GetType ( ) ) ;
51+ var key = reader . GetKey ( value ) ;
52+ var val = reader . GetValue ( value ) ;
2753
28- var ctx = new ConvertContext ( key , value , context . Depths , context . Options ) ;
29- return ctx . ToKeyValuePairs ( ) ;
54+ writer . WritePropertyName ( key . ToString ( ) ) ;
55+ writer . WriteValue ( val ) ;
3056 }
3157
3258 /// <summary>
@@ -74,11 +100,6 @@ public object GetValue(object instance)
74100 return this . valueGetter . Invoke ( instance ) ;
75101 }
76102
77- /// <summary>
78- /// KeyValuePair泛型
79- /// </summary>
80- private static readonly Type keyValuePairType = typeof ( KeyValuePair < , > ) ;
81-
82103 /// <summary>
83104 /// 类型的KeyValuePairReader缓存
84105 /// </summary>
@@ -91,12 +112,8 @@ public object GetValue(object instance)
91112 /// <returns></returns>
92113 public static KeyValuePairReader GetReader ( Type type )
93114 {
94- if ( type . IsGenericType == false || type . GetGenericTypeDefinition ( ) != keyValuePairType )
95- {
96- return null ;
97- }
98115 return readerCache . GetOrAdd ( type , t => new KeyValuePairReader ( t ) ) ;
99116 }
100117 }
101118 }
102- }
119+ }
0 commit comments