1- using System ;
1+ using RestSharp . Extensions ;
2+ using System ;
23using System . Collections ;
34using System . Collections . Generic ;
45using System . Globalization ;
56using System . Linq ;
67using System . Reflection ;
7- using RestSharp . Extensions ;
88
99namespace RestSharp . Deserializers
1010{
@@ -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 )
@@ -89,7 +68,7 @@ private object Map(object target, IDictionary<string, object> data)
8968
9069 if ( attributes . Length > 0 )
9170 {
92- DeserializeAsAttribute attribute = ( DeserializeAsAttribute ) attributes [ 0 ] ;
71+ DeserializeAsAttribute attribute = ( DeserializeAsAttribute ) attributes [ 0 ] ;
9372 name = attribute . Name ;
9473 }
9574 else
@@ -117,7 +96,7 @@ private object Map(object target, IDictionary<string, object> data)
11796 }
11897 else
11998 {
120- currentData = ( IDictionary < string , object > ) currentData [ actualName ] ;
99+ currentData = ( IDictionary < string , object > ) currentData [ actualName ] ;
121100 }
122101 }
123102
@@ -132,11 +111,11 @@ private object Map(object target, IDictionary<string, object> data)
132111
133112 private IDictionary BuildDictionary ( Type type , object parent )
134113 {
135- IDictionary dict = ( IDictionary ) Activator . CreateInstance ( type ) ;
114+ IDictionary dict = ( IDictionary ) Activator . CreateInstance ( type ) ;
136115 Type keyType = type . GetGenericArguments ( ) [ 0 ] ;
137116 Type valueType = type . GetGenericArguments ( ) [ 1 ] ;
138117
139- foreach ( KeyValuePair < string , object > child in ( IDictionary < string , object > ) parent )
118+ foreach ( KeyValuePair < string , object > child in ( IDictionary < string , object > ) parent )
140119 {
141120 object key = keyType != typeof ( string )
142121 ? Convert . ChangeType ( child . Key , keyType , CultureInfo . InvariantCulture )
@@ -161,15 +140,15 @@ private IDictionary BuildDictionary(Type type, object parent)
161140
162141 private IList BuildList ( Type type , object parent )
163142 {
164- IList list = ( IList ) Activator . CreateInstance ( type ) ;
143+ IList list = ( IList ) Activator . CreateInstance ( type ) ;
165144 Type listType = type . GetInterfaces ( )
166145 . First
167146 ( x => x . IsGenericType && x . GetGenericTypeDefinition ( ) == typeof ( IList < > ) ) ;
168147 Type itemType = listType . GetGenericArguments ( ) [ 0 ] ;
169148
170149 if ( parent is IList )
171150 {
172- foreach ( object element in ( IList ) parent )
151+ foreach ( object element in ( IList ) parent )
173152 {
174153 if ( itemType . IsPrimitive )
175154 {
@@ -272,14 +251,14 @@ private object ConvertValue(Type type, object value)
272251
273252 if ( type == typeof ( DateTimeOffset ) )
274253 {
275- return ( DateTimeOffset ) dt ;
254+ return ( DateTimeOffset ) dt ;
276255 }
277256 }
278257 else if ( type == typeof ( decimal ) )
279258 {
280259 if ( value is double )
281260 {
282- return ( decimal ) ( ( double ) value ) ;
261+ return ( decimal ) ( ( double ) value ) ;
283262 }
284263
285264 if ( stringValue . Contains ( "e" ) )
@@ -339,7 +318,7 @@ private object ConvertValue(Type type, object value)
339318 }
340319 else if ( type == typeof ( JsonObject ) )
341320 {
342- // simplify JsonObject into a Dictionary<string, object>
321+ // simplify JsonObject into a Dictionary<string, object>
343322 return this . BuildDictionary ( typeof ( Dictionary < string , object > ) , value ) ;
344323 }
345324 else
@@ -355,9 +334,9 @@ private object CreateAndMap(Type type, object element)
355334 {
356335 object instance = Activator . CreateInstance ( type ) ;
357336
358- this . Map ( instance , ( IDictionary < string , object > ) element ) ;
337+ this . Map ( instance , ( IDictionary < string , object > ) element ) ;
359338
360339 return instance ;
361340 }
362341 }
363- }
342+ }
0 commit comments