@@ -27,51 +27,30 @@ public JsonDeserializer()
27
27
28
28
public T Deserialize < T > ( IRestResponse response )
29
29
{
30
- T target = Activator . CreateInstance < T > ( ) ;
30
+ object json = this . FindRoot ( response . Content ) ;
31
31
32
- if ( target is IList )
33
- {
34
- Type objType = target . GetType ( ) ;
35
-
36
- if ( this . RootElement . HasValue ( ) )
37
- {
38
- object root = this . FindRoot ( response . Content ) ;
39
-
40
- target = ( T ) this . BuildList ( objType , root ) ;
41
- }
42
- else
43
- {
44
- object data = SimpleJson . DeserializeObject ( response . Content ) ;
45
-
46
- target = ( T ) this . BuildList ( objType , data ) ;
47
- }
48
- }
49
- else if ( target is IDictionary )
50
- {
51
- object root = this . FindRoot ( response . Content ) ;
52
-
53
- target = ( T ) this . BuildDictionary ( target . GetType ( ) , root ) ;
54
- }
55
- else
56
- {
57
- object root = this . FindRoot ( response . Content ) ;
58
-
59
- target = ( T ) this . Map ( target , ( IDictionary < string , object > ) root ) ;
60
- }
61
-
62
- return target ;
32
+ return ( T ) this . ConvertValue ( typeof ( T ) , json ) ;
63
33
}
64
34
65
35
private object FindRoot ( string content )
66
36
{
67
- IDictionary < string , object > data = ( IDictionary < string , object > ) SimpleJson . DeserializeObject ( content ) ;
37
+ object json = SimpleJson . DeserializeObject ( content ) ;
68
38
69
- if ( this . RootElement . HasValue ( ) && data . ContainsKey ( this . RootElement ) )
39
+ if ( this . RootElement . HasValue ( ) )
70
40
{
71
- return data [ this . RootElement ] ;
41
+ IDictionary < string , object > dictionary = json as IDictionary < string , object > ;
42
+
43
+ if ( dictionary != null )
44
+ {
45
+ object result ;
46
+ if ( dictionary . TryGetValue ( this . RootElement , out result ) )
47
+ {
48
+ return result ;
49
+ }
50
+ }
72
51
}
73
52
74
- return data ;
53
+ return json ;
75
54
}
76
55
77
56
private object Map ( object target , IDictionary < string , object > data )
@@ -318,19 +297,11 @@ private object ConvertValue(Type type, object value)
318
297
319
298
if ( genericTypeDef == typeof ( Dictionary < , > ) )
320
299
{
321
- Type keyType = type . GetGenericArguments ( ) [ 0 ] ;
322
-
323
- // only supports Dict<string, T>()
324
- if ( keyType == typeof ( string ) )
325
- {
326
- return this . BuildDictionary ( type , value ) ;
327
- }
328
- }
329
- else
330
- {
331
- // nested property classes
332
- return this . CreateAndMap ( type , value ) ;
300
+ return this . BuildDictionary ( type , value ) ;
333
301
}
302
+
303
+ // nested property classes
304
+ return this . CreateAndMap ( type , value ) ;
334
305
}
335
306
else if ( type . IsSubclassOfRawGeneric ( typeof ( List < > ) ) )
336
307
{
0 commit comments