File tree Expand file tree Collapse file tree 2 files changed +41
-15
lines changed
Expand file tree Collapse file tree 2 files changed +41
-15
lines changed Original file line number Diff line number Diff line change 1919using System . Diagnostics ;
2020using System . Globalization ;
2121using System . IO ;
22+ using System . Linq ;
2223using Newtonsoft . Json ;
2324using Newtonsoft . Json . Converters ;
2425using Newtonsoft . Json . Linq ;
@@ -70,6 +71,28 @@ public void Can_Deserialize_Simple_Generic_List_of_Simple_Types()
7071 Assert . NotEmpty ( output ) ;
7172 }
7273
74+ [ Fact ]
75+ public void Can_Deserialize_Simple_Generic_List_Given_Item_Without_Array ( )
76+ {
77+ const string content = "{\" users\" :\" johnsheehan\" }" ;
78+ var json = new JsonDeserializer { RootElement = "users" } ;
79+
80+ var output = json . Deserialize < List < string > > ( new RestResponse { Content = content } ) ;
81+
82+ Assert . True ( output . SequenceEqual ( new [ ] { "johnsheehan" } ) ) ;
83+ }
84+
85+ [ Fact ]
86+ public void Can_Deserialize_Simple_Generic_List_Given_Toplevel_Item_Without_Array ( )
87+ {
88+ const string content = "\" johnsheehan\" " ;
89+ var json = new JsonDeserializer ( ) ;
90+
91+ var output = json . Deserialize < List < string > > ( new RestResponse { Content = content } ) ;
92+
93+ Assert . True ( output . SequenceEqual ( new [ ] { "johnsheehan" } ) ) ;
94+ }
95+
7396 [ Fact ]
7497 public void Can_Deserialize_From_Root_Element ( )
7598 {
Original file line number Diff line number Diff line change @@ -200,22 +200,25 @@ private IList BuildList(Type type, object parent)
200200 var list = ( IList ) Activator . CreateInstance ( type ) ;
201201 var itemType = type . GetGenericArguments ( ) [ 0 ] ;
202202
203- foreach ( var element in ( IList ) parent )
204- {
205- if ( itemType . IsPrimitive )
206- {
207- var value = element . ToString ( ) ;
208- list . Add ( value . ChangeType ( itemType , Culture ) ) ;
209- }
210- else if ( itemType == typeof ( string ) )
211- {
212- list . Add ( element . ToString ( ) ) ;
213- }
214- else
215- {
216- var item = CreateAndMap ( itemType , element ) ;
217- list . Add ( item ) ;
203+ if ( parent is IList ) {
204+ foreach ( var element in ( IList ) parent ) {
205+ if ( itemType . IsPrimitive )
206+ {
207+ var value = element . ToString ( ) ;
208+ list . Add ( value . ChangeType ( itemType , Culture ) ) ;
209+ }
210+ else if ( itemType == typeof ( string ) )
211+ {
212+ list . Add ( element . ToString ( ) ) ;
213+ }
214+ else
215+ {
216+ var item = CreateAndMap ( itemType , element ) ;
217+ list . Add ( item ) ;
218+ }
218219 }
220+ } else {
221+ list . Add ( CreateAndMap ( itemType , parent ) ) ;
219222 }
220223 return list ;
221224 }
You can’t perform that action at this time.
0 commit comments