1
- using System ;
1
+ using RestSharp . Extensions ;
2
+ using System ;
2
3
using System . Collections ;
3
4
using System . Collections . Generic ;
4
5
using System . Globalization ;
5
6
using System . Linq ;
6
7
using System . Reflection ;
7
- using RestSharp . Extensions ;
8
8
9
9
namespace RestSharp . Deserializers
10
10
{
@@ -37,34 +37,34 @@ public T Deserialize<T>(IRestResponse response)
37
37
{
38
38
object root = this . FindRoot ( response . Content ) ;
39
39
40
- target = ( T ) this . BuildList ( objType , root ) ;
40
+ target = ( T ) this . BuildList ( objType , root ) ;
41
41
}
42
42
else
43
43
{
44
44
object data = SimpleJson . DeserializeObject ( response . Content ) ;
45
45
46
- target = ( T ) this . BuildList ( objType , data ) ;
46
+ target = ( T ) this . BuildList ( objType , data ) ;
47
47
}
48
48
}
49
49
else if ( target is IDictionary )
50
50
{
51
51
object root = this . FindRoot ( response . Content ) ;
52
52
53
- target = ( T ) this . BuildDictionary ( target . GetType ( ) , root ) ;
53
+ target = ( T ) this . BuildDictionary ( target . GetType ( ) , root ) ;
54
54
}
55
55
else
56
56
{
57
57
object root = this . FindRoot ( response . Content ) ;
58
58
59
- target = ( T ) this . Map ( target , ( IDictionary < string , object > ) root ) ;
59
+ target = ( T ) this . Map ( target , ( IDictionary < string , object > ) root ) ;
60
60
}
61
61
62
62
return target ;
63
63
}
64
64
65
65
private object FindRoot ( string content )
66
66
{
67
- IDictionary < string , object > data = ( IDictionary < string , object > ) SimpleJson . DeserializeObject ( content ) ;
67
+ IDictionary < string , object > data = ( IDictionary < string , object > ) SimpleJson . DeserializeObject ( content ) ;
68
68
69
69
if ( this . RootElement . HasValue ( ) && data . ContainsKey ( this . RootElement ) )
70
70
{
@@ -89,7 +89,7 @@ private object Map(object target, IDictionary<string, object> data)
89
89
90
90
if ( attributes . Length > 0 )
91
91
{
92
- DeserializeAsAttribute attribute = ( DeserializeAsAttribute ) attributes [ 0 ] ;
92
+ DeserializeAsAttribute attribute = ( DeserializeAsAttribute ) attributes [ 0 ] ;
93
93
name = attribute . Name ;
94
94
}
95
95
else
@@ -117,7 +117,7 @@ private object Map(object target, IDictionary<string, object> data)
117
117
}
118
118
else
119
119
{
120
- currentData = ( IDictionary < string , object > ) currentData [ actualName ] ;
120
+ currentData = ( IDictionary < string , object > ) currentData [ actualName ] ;
121
121
}
122
122
}
123
123
@@ -132,11 +132,11 @@ private object Map(object target, IDictionary<string, object> data)
132
132
133
133
private IDictionary BuildDictionary ( Type type , object parent )
134
134
{
135
- IDictionary dict = ( IDictionary ) Activator . CreateInstance ( type ) ;
135
+ IDictionary dict = ( IDictionary ) Activator . CreateInstance ( type ) ;
136
136
Type keyType = type . GetGenericArguments ( ) [ 0 ] ;
137
137
Type valueType = type . GetGenericArguments ( ) [ 1 ] ;
138
138
139
- foreach ( KeyValuePair < string , object > child in ( IDictionary < string , object > ) parent )
139
+ foreach ( KeyValuePair < string , object > child in ( IDictionary < string , object > ) parent )
140
140
{
141
141
object key = keyType != typeof ( string )
142
142
? Convert . ChangeType ( child . Key , keyType , CultureInfo . InvariantCulture )
@@ -161,15 +161,15 @@ private IDictionary BuildDictionary(Type type, object parent)
161
161
162
162
private IList BuildList ( Type type , object parent )
163
163
{
164
- IList list = ( IList ) Activator . CreateInstance ( type ) ;
164
+ IList list = ( IList ) Activator . CreateInstance ( type ) ;
165
165
Type listType = type . GetInterfaces ( )
166
166
. First
167
167
( x => x . IsGenericType && x . GetGenericTypeDefinition ( ) == typeof ( IList < > ) ) ;
168
168
Type itemType = listType . GetGenericArguments ( ) [ 0 ] ;
169
169
170
170
if ( parent is IList )
171
171
{
172
- foreach ( object element in ( IList ) parent )
172
+ foreach ( object element in ( IList ) parent )
173
173
{
174
174
if ( itemType . IsPrimitive )
175
175
{
@@ -225,8 +225,12 @@ private object ConvertValue(Type type, object value)
225
225
type = type . GetGenericArguments ( ) [ 0 ] ;
226
226
}
227
227
228
- if ( type == typeof ( object ) && value != null )
228
+ if ( type == typeof ( object ) )
229
229
{
230
+ if ( value == null )
231
+ {
232
+ return null ;
233
+ }
230
234
type = value . GetType ( ) ;
231
235
}
232
236
@@ -272,14 +276,14 @@ private object ConvertValue(Type type, object value)
272
276
273
277
if ( type == typeof ( DateTimeOffset ) )
274
278
{
275
- return ( DateTimeOffset ) dt ;
279
+ return ( DateTimeOffset ) dt ;
276
280
}
277
281
}
278
282
else if ( type == typeof ( decimal ) )
279
283
{
280
284
if ( value is double )
281
285
{
282
- return ( decimal ) ( ( double ) value ) ;
286
+ return ( decimal ) ( ( double ) value ) ;
283
287
}
284
288
285
289
if ( stringValue . Contains ( "e" ) )
@@ -339,7 +343,7 @@ private object ConvertValue(Type type, object value)
339
343
}
340
344
else if ( type == typeof ( JsonObject ) )
341
345
{
342
- // simplify JsonObject into a Dictionary<string, object>
346
+ // simplify JsonObject into a Dictionary<string, object>
343
347
return this . BuildDictionary ( typeof ( Dictionary < string , object > ) , value ) ;
344
348
}
345
349
else
@@ -355,9 +359,9 @@ private object CreateAndMap(Type type, object element)
355
359
{
356
360
object instance = Activator . CreateInstance ( type ) ;
357
361
358
- this . Map ( instance , ( IDictionary < string , object > ) element ) ;
362
+ this . Map ( instance , ( IDictionary < string , object > ) element ) ;
359
363
360
364
return instance ;
361
365
}
362
366
}
363
- }
367
+ }
0 commit comments