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{
@@ -37,34 +37,34 @@ public T Deserialize<T>(IRestResponse response)
3737 {
3838 object root = this . FindRoot ( response . Content ) ;
3939
40- target = ( T ) this . BuildList ( objType , root ) ;
40+ target = ( T ) this . BuildList ( objType , root ) ;
4141 }
4242 else
4343 {
4444 object data = SimpleJson . DeserializeObject ( response . Content ) ;
4545
46- target = ( T ) this . BuildList ( objType , data ) ;
46+ target = ( T ) this . BuildList ( objType , data ) ;
4747 }
4848 }
4949 else if ( target is IDictionary )
5050 {
5151 object root = this . FindRoot ( response . Content ) ;
5252
53- target = ( T ) this . BuildDictionary ( target . GetType ( ) , root ) ;
53+ target = ( T ) this . BuildDictionary ( target . GetType ( ) , root ) ;
5454 }
5555 else
5656 {
5757 object root = this . FindRoot ( response . Content ) ;
5858
59- target = ( T ) this . Map ( target , ( IDictionary < string , object > ) root ) ;
59+ target = ( T ) this . Map ( target , ( IDictionary < string , object > ) root ) ;
6060 }
6161
6262 return target ;
6363 }
6464
6565 private object FindRoot ( string content )
6666 {
67- IDictionary < string , object > data = ( IDictionary < string , object > ) SimpleJson . DeserializeObject ( content ) ;
67+ IDictionary < string , object > data = ( IDictionary < string , object > ) SimpleJson . DeserializeObject ( content ) ;
6868
6969 if ( this . RootElement . HasValue ( ) && data . ContainsKey ( this . RootElement ) )
7070 {
@@ -89,7 +89,7 @@ private object Map(object target, IDictionary<string, object> data)
8989
9090 if ( attributes . Length > 0 )
9191 {
92- DeserializeAsAttribute attribute = ( DeserializeAsAttribute ) attributes [ 0 ] ;
92+ DeserializeAsAttribute attribute = ( DeserializeAsAttribute ) attributes [ 0 ] ;
9393 name = attribute . Name ;
9494 }
9595 else
@@ -117,7 +117,7 @@ private object Map(object target, IDictionary<string, object> data)
117117 }
118118 else
119119 {
120- currentData = ( IDictionary < string , object > ) currentData [ actualName ] ;
120+ currentData = ( IDictionary < string , object > ) currentData [ actualName ] ;
121121 }
122122 }
123123
@@ -132,11 +132,11 @@ private object Map(object target, IDictionary<string, object> data)
132132
133133 private IDictionary BuildDictionary ( Type type , object parent )
134134 {
135- IDictionary dict = ( IDictionary ) Activator . CreateInstance ( type ) ;
135+ IDictionary dict = ( IDictionary ) Activator . CreateInstance ( type ) ;
136136 Type keyType = type . GetGenericArguments ( ) [ 0 ] ;
137137 Type valueType = type . GetGenericArguments ( ) [ 1 ] ;
138138
139- foreach ( KeyValuePair < string , object > child in ( IDictionary < string , object > ) parent )
139+ foreach ( KeyValuePair < string , object > child in ( IDictionary < string , object > ) parent )
140140 {
141141 object key = keyType != typeof ( string )
142142 ? Convert . ChangeType ( child . Key , keyType , CultureInfo . InvariantCulture )
@@ -161,15 +161,15 @@ private IDictionary BuildDictionary(Type type, object parent)
161161
162162 private IList BuildList ( Type type , object parent )
163163 {
164- IList list = ( IList ) Activator . CreateInstance ( type ) ;
164+ IList list = ( IList ) Activator . CreateInstance ( type ) ;
165165 Type listType = type . GetInterfaces ( )
166166 . First
167167 ( x => x . IsGenericType && x . GetGenericTypeDefinition ( ) == typeof ( IList < > ) ) ;
168168 Type itemType = listType . GetGenericArguments ( ) [ 0 ] ;
169169
170170 if ( parent is IList )
171171 {
172- foreach ( object element in ( IList ) parent )
172+ foreach ( object element in ( IList ) parent )
173173 {
174174 if ( itemType . IsPrimitive )
175175 {
@@ -225,8 +225,12 @@ private object ConvertValue(Type type, object value)
225225 type = type . GetGenericArguments ( ) [ 0 ] ;
226226 }
227227
228- if ( type == typeof ( object ) && value != null )
228+ if ( type == typeof ( object ) )
229229 {
230+ if ( value == null )
231+ {
232+ return null ;
233+ }
230234 type = value . GetType ( ) ;
231235 }
232236
@@ -272,14 +276,14 @@ private object ConvertValue(Type type, object value)
272276
273277 if ( type == typeof ( DateTimeOffset ) )
274278 {
275- return ( DateTimeOffset ) dt ;
279+ return ( DateTimeOffset ) dt ;
276280 }
277281 }
278282 else if ( type == typeof ( decimal ) )
279283 {
280284 if ( value is double )
281285 {
282- return ( decimal ) ( ( double ) value ) ;
286+ return ( decimal ) ( ( double ) value ) ;
283287 }
284288
285289 if ( stringValue . Contains ( "e" ) )
@@ -339,7 +343,7 @@ private object ConvertValue(Type type, object value)
339343 }
340344 else if ( type == typeof ( JsonObject ) )
341345 {
342- // simplify JsonObject into a Dictionary<string, object>
346+ // simplify JsonObject into a Dictionary<string, object>
343347 return this . BuildDictionary ( typeof ( Dictionary < string , object > ) , value ) ;
344348 }
345349 else
@@ -355,9 +359,9 @@ private object CreateAndMap(Type type, object element)
355359 {
356360 object instance = Activator . CreateInstance ( type ) ;
357361
358- this . Map ( instance , ( IDictionary < string , object > ) element ) ;
362+ this . Map ( instance , ( IDictionary < string , object > ) element ) ;
359363
360364 return instance ;
361365 }
362366 }
363- }
367+ }
0 commit comments