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
{
@@ -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 )
@@ -89,7 +68,7 @@ private object Map(object target, IDictionary<string, object> data)
89
68
90
69
if ( attributes . Length > 0 )
91
70
{
92
- DeserializeAsAttribute attribute = ( DeserializeAsAttribute ) attributes [ 0 ] ;
71
+ DeserializeAsAttribute attribute = ( DeserializeAsAttribute ) attributes [ 0 ] ;
93
72
name = attribute . Name ;
94
73
}
95
74
else
@@ -117,7 +96,7 @@ private object Map(object target, IDictionary<string, object> data)
117
96
}
118
97
else
119
98
{
120
- currentData = ( IDictionary < string , object > ) currentData [ actualName ] ;
99
+ currentData = ( IDictionary < string , object > ) currentData [ actualName ] ;
121
100
}
122
101
}
123
102
@@ -132,11 +111,11 @@ private object Map(object target, IDictionary<string, object> data)
132
111
133
112
private IDictionary BuildDictionary ( Type type , object parent )
134
113
{
135
- IDictionary dict = ( IDictionary ) Activator . CreateInstance ( type ) ;
114
+ IDictionary dict = ( IDictionary ) Activator . CreateInstance ( type ) ;
136
115
Type keyType = type . GetGenericArguments ( ) [ 0 ] ;
137
116
Type valueType = type . GetGenericArguments ( ) [ 1 ] ;
138
117
139
- foreach ( KeyValuePair < string , object > child in ( IDictionary < string , object > ) parent )
118
+ foreach ( KeyValuePair < string , object > child in ( IDictionary < string , object > ) parent )
140
119
{
141
120
object key = keyType != typeof ( string )
142
121
? Convert . ChangeType ( child . Key , keyType , CultureInfo . InvariantCulture )
@@ -161,15 +140,15 @@ private IDictionary BuildDictionary(Type type, object parent)
161
140
162
141
private IList BuildList ( Type type , object parent )
163
142
{
164
- IList list = ( IList ) Activator . CreateInstance ( type ) ;
143
+ IList list = ( IList ) Activator . CreateInstance ( type ) ;
165
144
Type listType = type . GetInterfaces ( )
166
145
. First
167
146
( x => x . IsGenericType && x . GetGenericTypeDefinition ( ) == typeof ( IList < > ) ) ;
168
147
Type itemType = listType . GetGenericArguments ( ) [ 0 ] ;
169
148
170
149
if ( parent is IList )
171
150
{
172
- foreach ( object element in ( IList ) parent )
151
+ foreach ( object element in ( IList ) parent )
173
152
{
174
153
if ( itemType . IsPrimitive )
175
154
{
@@ -272,14 +251,14 @@ private object ConvertValue(Type type, object value)
272
251
273
252
if ( type == typeof ( DateTimeOffset ) )
274
253
{
275
- return ( DateTimeOffset ) dt ;
254
+ return ( DateTimeOffset ) dt ;
276
255
}
277
256
}
278
257
else if ( type == typeof ( decimal ) )
279
258
{
280
259
if ( value is double )
281
260
{
282
- return ( decimal ) ( ( double ) value ) ;
261
+ return ( decimal ) ( ( double ) value ) ;
283
262
}
284
263
285
264
if ( stringValue . Contains ( "e" ) )
@@ -339,7 +318,7 @@ private object ConvertValue(Type type, object value)
339
318
}
340
319
else if ( type == typeof ( JsonObject ) )
341
320
{
342
- // simplify JsonObject into a Dictionary<string, object>
321
+ // simplify JsonObject into a Dictionary<string, object>
343
322
return this . BuildDictionary ( typeof ( Dictionary < string , object > ) , value ) ;
344
323
}
345
324
else
@@ -355,9 +334,9 @@ private object CreateAndMap(Type type, object element)
355
334
{
356
335
object instance = Activator . CreateInstance ( type ) ;
357
336
358
- this . Map ( instance , ( IDictionary < string , object > ) element ) ;
337
+ this . Map ( instance , ( IDictionary < string , object > ) element ) ;
359
338
360
339
return instance ;
361
340
}
362
341
}
363
- }
342
+ }
0 commit comments