@@ -13,84 +13,54 @@ public class PropertyCSScriptSerializer<T> : ConstructorCSScriptSerializer<T>
1313 private readonly IReadOnlyCollection < PropertyData > _propertyData ;
1414
1515 public PropertyCSScriptSerializer ( )
16- : this ( ( Func < T , object > [ ] ) null )
16+ : this ( ( Func < T , object > [ ] ) null )
1717 {
1818 }
1919
20- public PropertyCSScriptSerializer ( IReadOnlyCollection < Func < T , object > > argumentGetters )
21- : this ( typeof ( T ) . GetRuntimeProperties ( ) . Where ( IsCandidateProperty ) , argumentGetters )
20+ public PropertyCSScriptSerializer ( IReadOnlyCollection < Func < T , object > > constructorParameterGetters )
21+ : this ( propertyConditions : null , constructorParameterGetters : constructorParameterGetters )
2222 {
2323 }
2424
25- public PropertyCSScriptSerializer ( IEnumerable < PropertyInfo > properties )
26- : this ( properties , constructorParameterGetters : null )
25+ public PropertyCSScriptSerializer ( IReadOnlyDictionary < string , Func < T , object , bool > > propertyConditions )
26+ : this ( propertyConditions , constructorParameterGetters : null )
2727 {
2828 }
2929
30- public PropertyCSScriptSerializer ( IReadOnlyDictionary < string , Func < T , object > > propertyValueGetters )
31- : this ( propertyValueGetters , constructorParameterGetters : null )
32- {
33- }
34-
35- public PropertyCSScriptSerializer ( IEnumerable < PropertyInfo > properties ,
30+ public PropertyCSScriptSerializer ( IReadOnlyDictionary < string , Func < T , object , bool > > propertyConditions ,
3631 IReadOnlyCollection < Func < T , object > > constructorParameterGetters )
37- : this ( properties . Select ( p => new PropertyData (
38- p . Name ,
39- p . PropertyType ,
40- CreatePropertyInitializer ( p ) ,
41- o => GetDefault ( p . PropertyType ) ) ) . ToArray ( ) ,
42- constructorParameterGetters )
43- {
44- }
45-
46- public PropertyCSScriptSerializer ( IReadOnlyDictionary < string , Func < T , object > > propertyValueGetters ,
47- IReadOnlyCollection < Func < T , object > > constructorParameterGetters )
48- : this ( propertyValueGetters , constructorParameterGetters , new Dictionary < string , object > ( ) )
49- {
50- }
51-
52- public PropertyCSScriptSerializer ( IReadOnlyDictionary < string , Func < T , object > > propertyValueGetters ,
53- IReadOnlyCollection < Func < T , object > > constructorParameterGetters ,
54- IReadOnlyDictionary < string , object > propertyDefaults )
55- : this (
56- propertyValueGetters ,
57- constructorParameterGetters ,
58- propertyDefaults . ToDictionary < KeyValuePair < string , object > , string , Func < T , object > > (
59- p => p . Key ,
60- p => o => p . Value ) )
32+ : this ( propertyConditions , constructorParameterGetters , propertyValueGetters : null )
6133 {
6234 }
6335
64- // To not serialize properties give default that's always equal to the property value
65- public PropertyCSScriptSerializer ( IReadOnlyDictionary < string , Func < T , object > > propertyValueGetters ,
36+ public PropertyCSScriptSerializer ( IReadOnlyDictionary < string , Func < T , object , bool > > propertyConditions ,
6637 IReadOnlyCollection < Func < T , object > > constructorParameterGetters ,
67- IReadOnlyDictionary < string , Func < T , object > > propertyDefaultGetters )
68- : this ( constructorParameterGetters )
38+ IReadOnlyDictionary < string , Func < T , object > > propertyValueGetters )
39+ : base ( constructorParameterGetters )
6940 {
70- var referencedProperties = typeof ( T ) . GetTypeInfo ( )
41+ propertyConditions = propertyConditions ?? new Dictionary < string , Func < T , object , bool > > ( ) ;
42+ propertyValueGetters = propertyValueGetters ?? new Dictionary < string , Func < T , object > > ( ) ;
43+ var referencedPropertyNames = propertyConditions . Keys . Concat ( propertyValueGetters . Keys ) . Distinct ( ) ;
44+ var allProperties = typeof ( T ) . GetTypeInfo ( )
7145 . GetProperties ( BindingFlags . Instance | BindingFlags . Public )
72- . Where ( p => propertyValueGetters . ContainsKey ( p . Name ) ) ;
73- _propertyData = typeof ( T ) . GetRuntimeProperties ( ) . Where ( IsCandidateProperty )
74- . Concat ( referencedProperties ) . Distinct ( ) . Select (
46+ . ToDictionary ( p => p . Name ) ;
47+
48+ _propertyData = referencedPropertyNames . Select ( n => allProperties [ n ] )
49+ . Concat ( allProperties . Values . Where ( IsCandidateProperty ) ) . Distinct ( )
50+ . Select (
7551 p => new PropertyData (
7652 p . Name ,
7753 p . PropertyType ,
7854 propertyValueGetters . GetValueOrDefault ( p . Name , CreatePropertyInitializer ( p ) ) ,
79- propertyDefaultGetters . GetValueOrDefault ( p . Name , o => GetDefault ( p . PropertyType ) ) ) )
55+ propertyConditions . GetValueOrDefault ( p . Name ,
56+ ( o , v ) => ! Equals ( v , GetDefault ( p . PropertyType ) ) ) ) )
8057 . ToArray ( ) ;
8158 }
8259
83- protected PropertyCSScriptSerializer ( IReadOnlyCollection < PropertyData > propertyData ,
84- IReadOnlyCollection < Func < T , object > > constructorParameterGetters )
85- : base ( constructorParameterGetters )
86- {
87- _propertyData = propertyData ;
88- }
89-
9060 protected override bool GenerateEmptyArgumentList => false ;
9161
9262 public override ExpressionSyntax GetCreation ( object obj )
93- => GetObjectCreationExpression ( ( T ) obj ) ;
63+ => GetObjectCreationExpression ( ( T ) obj ) ;
9464
9565 protected override ObjectCreationExpressionSyntax GetObjectCreationExpression ( T obj )
9666 => base . GetObjectCreationExpression ( obj )
@@ -99,18 +69,11 @@ protected override ObjectCreationExpressionSyntax GetObjectCreationExpression(T
9969 SyntaxKind . ObjectInitializerExpression ,
10070 SyntaxFactory . SeparatedList < ExpressionSyntax > (
10171 ToCommaSeparatedList ( _propertyData
102- . Select ( p => new
103- {
104- p . PropertyName ,
105- p . PropertyType ,
106- PropertyValue = p . PropertyValueGetter ( obj ) ,
107- PropertyDefault = p . PropertyDefaultGetter ( obj )
108- } )
109- . Where ( p => ! Equals ( p . PropertyValue , p . PropertyDefault ) )
72+ . Where ( p => p . PropertyCondition ( obj , p . PropertyValueGetter ( obj ) ) )
11073 . Select ( p => SyntaxFactory . AssignmentExpression (
11174 SyntaxKind . SimpleAssignmentExpression ,
11275 SyntaxFactory . IdentifierName ( p . PropertyName ) ,
113- GetCreationExpression ( p . PropertyValue ) ) ) ) ) ) ) ) ;
76+ GetCreationExpression ( p . PropertyValueGetter ( obj ) ) ) ) ) ) ) ) ) ;
11477
11578 protected static Func < T , object > CreatePropertyInitializer ( PropertyInfo property )
11679 {
@@ -129,18 +92,18 @@ public PropertyData(
12992 string propertyName ,
13093 Type propertyType ,
13194 Func < T , object > propertyValueGetter ,
132- Func < T , object > propertyDefaultGetter )
95+ Func < T , object , bool > propertyCondition )
13396 {
13497 PropertyName = propertyName ;
13598 PropertyType = propertyType ;
13699 PropertyValueGetter = propertyValueGetter ;
137- PropertyDefaultGetter = propertyDefaultGetter ;
100+ PropertyCondition = propertyCondition ;
138101 }
139102
140103 public string PropertyName { get ; }
141104 public Type PropertyType { get ; }
142105 public Func < T , object > PropertyValueGetter { get ; }
143- public Func < T , object > PropertyDefaultGetter { get ; }
106+ public Func < T , object , bool > PropertyCondition { get ; }
144107 }
145108 }
146109}
0 commit comments