1414using System . Collections . Generic ;
1515using System . Reflection ;
1616using System . Threading ;
17+ #if WINDOWS_PHONE
18+ using ServiceStack . Text . WP ;
19+ #endif
1720
1821namespace ServiceStack . Text . Common
1922{
20- internal static class DeserializeCollection < TSerializer >
21- where TSerializer : ITypeSerializer
22- {
23- private static readonly ITypeSerializer Serializer = JsWriter . GetTypeSerializer < TSerializer > ( ) ;
24-
25- public static ParseStringDelegate GetParseMethod ( Type type )
26- {
27- var collectionInterface = type . GetTypeWithGenericInterfaceOf ( typeof ( ICollection < > ) ) ;
28- if ( collectionInterface == null )
29- throw new ArgumentException ( string . Format ( "Type {0} is not of type ICollection<>" , type . FullName ) ) ;
30-
31- //optimized access for regularly used types
32- if ( type . HasInterface ( typeof ( ICollection < string > ) ) )
33- return value => ParseStringCollection ( value , type ) ;
34-
35- if ( type . HasInterface ( typeof ( ICollection < int > ) ) )
36- return value => ParseIntCollection ( value , type ) ;
37-
38- var elementType = collectionInterface . GetGenericArguments ( ) [ 0 ] ;
39-
40- var supportedTypeParseMethod = Serializer . GetParseFn ( elementType ) ;
41- if ( supportedTypeParseMethod != null )
42- {
43- var createCollectionType = type . HasAnyTypeDefinitionsOf ( typeof ( ICollection < > ) )
23+ internal static class DeserializeCollection < TSerializer >
24+ where TSerializer : ITypeSerializer
25+ {
26+ private static readonly ITypeSerializer Serializer = JsWriter . GetTypeSerializer < TSerializer > ( ) ;
27+
28+ public static ParseStringDelegate GetParseMethod ( Type type )
29+ {
30+ var collectionInterface = type . GetTypeWithGenericInterfaceOf ( typeof ( ICollection < > ) ) ;
31+ if ( collectionInterface == null )
32+ throw new ArgumentException ( string . Format ( "Type {0} is not of type ICollection<>" , type . FullName ) ) ;
33+
34+ //optimized access for regularly used types
35+ if ( type . HasInterface ( typeof ( ICollection < string > ) ) )
36+ return value => ParseStringCollection ( value , type ) ;
37+
38+ if ( type . HasInterface ( typeof ( ICollection < int > ) ) )
39+ return value => ParseIntCollection ( value , type ) ;
40+
41+ var elementType = collectionInterface . GetGenericArguments ( ) [ 0 ] ;
42+
43+ var supportedTypeParseMethod = Serializer . GetParseFn ( elementType ) ;
44+ if ( supportedTypeParseMethod != null )
45+ {
46+ var createCollectionType = type . HasAnyTypeDefinitionsOf ( typeof ( ICollection < > ) )
4447 ? null : type ;
4548
46- return value => ParseCollectionType ( value , createCollectionType , elementType , supportedTypeParseMethod ) ;
47- }
49+ return value => ParseCollectionType ( value , createCollectionType , elementType , supportedTypeParseMethod ) ;
50+ }
4851
49- return null ;
50- }
52+ return null ;
53+ }
5154
52- public static ICollection < string > ParseStringCollection ( string value , Type createType )
53- {
54- var items = DeserializeArrayWithElements < string , TSerializer > . ParseGenericArray ( value , Serializer . ParseString ) ;
55- return CreateAndPopulate ( createType , items ) ;
56- }
55+ public static ICollection < string > ParseStringCollection ( string value , Type createType )
56+ {
57+ var items = DeserializeArrayWithElements < string , TSerializer > . ParseGenericArray ( value , Serializer . ParseString ) ;
58+ return CreateAndPopulate ( createType , items ) ;
59+ }
5760
58- public static ICollection < int > ParseIntCollection ( string value , Type createType )
59- {
60- var items = DeserializeArrayWithElements < int , TSerializer > . ParseGenericArray ( value , x => int . Parse ( x ) ) ;
61- return CreateAndPopulate ( createType , items ) ;
62- }
61+ public static ICollection < int > ParseIntCollection ( string value , Type createType )
62+ {
63+ var items = DeserializeArrayWithElements < int , TSerializer > . ParseGenericArray ( value , x => int . Parse ( x ) ) ;
64+ return CreateAndPopulate ( createType , items ) ;
65+ }
6366
64- public static ICollection < T > ParseCollection < T > ( string value , Type createType , ParseStringDelegate parseFn )
65- {
66- if ( value == null ) return null ;
67+ public static ICollection < T > ParseCollection < T > ( string value , Type createType , ParseStringDelegate parseFn )
68+ {
69+ if ( value == null ) return null ;
6770
68- var items = DeserializeArrayWithElements < T , TSerializer > . ParseGenericArray ( value , parseFn ) ;
69- return CreateAndPopulate ( createType , items ) ;
70- }
71+ var items = DeserializeArrayWithElements < T , TSerializer > . ParseGenericArray ( value , parseFn ) ;
72+ return CreateAndPopulate ( createType , items ) ;
73+ }
7174
72- private static ICollection < T > CreateAndPopulate < T > ( Type ofCollectionType , T [ ] withItems )
73- {
74- if ( ofCollectionType == null ) return new List < T > ( withItems ) ;
75+ private static ICollection < T > CreateAndPopulate < T > ( Type ofCollectionType , T [ ] withItems )
76+ {
77+ if ( ofCollectionType == null ) return new List < T > ( withItems ) ;
7578
76- var genericTypeDefinition = ofCollectionType . IsGenericType ( )
79+ var genericTypeDefinition = ofCollectionType . IsGenericType ( )
7780 ? ofCollectionType . GetGenericTypeDefinition ( )
7881 : null ;
7982#if ! XBOX
80- if ( genericTypeDefinition == typeof ( HashSet < T > ) )
81- return new HashSet < T > ( withItems ) ;
83+ if ( genericTypeDefinition == typeof ( HashSet < T > ) )
84+ return new HashSet < T > ( withItems ) ;
8285#endif
83- if ( genericTypeDefinition == typeof ( LinkedList < T > ) )
84- return new LinkedList < T > ( withItems ) ;
85-
86- var collection = ( ICollection < T > ) ofCollectionType . CreateInstance ( ) ;
87- foreach ( var item in withItems )
88- {
89- collection . Add ( item ) ;
90- }
91- return collection ;
92- }
93-
94- private static Dictionary < Type , ParseCollectionDelegate > ParseDelegateCache
86+ if ( genericTypeDefinition == typeof ( LinkedList < T > ) )
87+ return new LinkedList < T > ( withItems ) ;
88+
89+ var collection = ( ICollection < T > ) ofCollectionType . CreateInstance ( ) ;
90+ foreach ( var item in withItems )
91+ {
92+ collection . Add ( item ) ;
93+ }
94+ return collection ;
95+ }
96+
97+ private static Dictionary < Type , ParseCollectionDelegate > ParseDelegateCache
9598 = new Dictionary < Type , ParseCollectionDelegate > ( ) ;
9699
97- private delegate object ParseCollectionDelegate ( string value , Type createType , ParseStringDelegate parseFn ) ;
100+ private delegate object ParseCollectionDelegate ( string value , Type createType , ParseStringDelegate parseFn ) ;
98101
99- public static object ParseCollectionType ( string value , Type createType , Type elementType , ParseStringDelegate parseFn )
100- {
101- ParseCollectionDelegate parseDelegate ;
102- if ( ParseDelegateCache . TryGetValue ( elementType , out parseDelegate ) )
102+ public static object ParseCollectionType ( string value , Type createType , Type elementType , ParseStringDelegate parseFn )
103+ {
104+ ParseCollectionDelegate parseDelegate ;
105+ if ( ParseDelegateCache . TryGetValue ( elementType , out parseDelegate ) )
103106 return parseDelegate ( value , createType , parseFn ) ;
104107
105108 var mi = typeof ( DeserializeCollection < TSerializer > ) . GetMethod ( "ParseCollection" , BindingFlags . Static | BindingFlags . Public ) ;
@@ -115,8 +118,8 @@ public static object ParseCollectionType(string value, Type createType, Type ele
115118
116119 } while ( ! ReferenceEquals (
117120 Interlocked . CompareExchange ( ref ParseDelegateCache , newCache , snapshot ) , snapshot ) ) ;
118-
121+
119122 return parseDelegate ( value , createType , parseFn ) ;
120- }
121- }
123+ }
124+ }
122125}
0 commit comments