@@ -27,51 +27,30 @@ public JsonDeserializer()
2727
2828 public T Deserialize < T > ( IRestResponse response )
2929 {
30- T target = Activator . CreateInstance < T > ( ) ;
30+ object json = this . FindRoot ( response . Content ) ;
3131
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 ) ;
6333 }
6434
6535 private object FindRoot ( string content )
6636 {
67- IDictionary < string , object > data = ( IDictionary < string , object > ) SimpleJson . DeserializeObject ( content ) ;
37+ object json = SimpleJson . DeserializeObject ( content ) ;
6838
69- if ( this . RootElement . HasValue ( ) && data . ContainsKey ( this . RootElement ) )
39+ if ( this . RootElement . HasValue ( ) )
7040 {
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+ }
7251 }
7352
74- return data ;
53+ return json ;
7554 }
7655
7756 private object Map ( object target , IDictionary < string , object > data )
@@ -318,19 +297,11 @@ private object ConvertValue(Type type, object value)
318297
319298 if ( genericTypeDef == typeof ( Dictionary < , > ) )
320299 {
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 ) ;
333301 }
302+
303+ // nested property classes
304+ return this . CreateAndMap ( type , value ) ;
334305 }
335306 else if ( type . IsSubclassOfRawGeneric ( typeof ( List < > ) ) )
336307 {
0 commit comments