@@ -23,36 +23,64 @@ public PropertyCSScriptSerializer(IReadOnlyCollection<Func<T, object>> argumentG
2323 }
2424
2525 public PropertyCSScriptSerializer ( IEnumerable < PropertyInfo > properties )
26- : this ( properties , parameterGetters : null )
26+ : this ( properties , constructorParameterGetters : null )
2727 {
2828 }
2929
3030 public PropertyCSScriptSerializer ( IReadOnlyDictionary < string , Func < T , object > > propertyValueGetters )
31- : this ( propertyValueGetters , parameterGetters : null )
31+ : this ( propertyValueGetters , constructorParameterGetters : null )
3232 {
3333 }
3434
3535 public PropertyCSScriptSerializer ( IEnumerable < PropertyInfo > properties ,
36- IReadOnlyCollection < Func < T , object > > parameterGetters )
36+ 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 )
3755 : this (
38- properties . Select ( p => new PropertyData ( p . Name , p . PropertyType , CreatePropertyInitializer ( p ) ) ) . ToArray ( ) ,
39- parameterGetters )
56+ propertyValueGetters ,
57+ constructorParameterGetters ,
58+ propertyDefaults . ToDictionary < KeyValuePair < string , object > , string , Func < T , object > > (
59+ p => p . Key ,
60+ p => o => p . Value ) )
4061 {
4162 }
4263
4364 public PropertyCSScriptSerializer ( IReadOnlyDictionary < string , Func < T , object > > propertyValueGetters ,
44- IReadOnlyCollection < Func < T , object > > parameterGetters )
45- : base ( parameterGetters )
65+ IReadOnlyCollection < Func < T , object > > constructorParameterGetters ,
66+ IReadOnlyDictionary < string , Func < T , object > > propertyDefaultGetters )
67+ : this ( constructorParameterGetters )
4668 {
4769 var properties = typeof ( T ) . GetTypeInfo ( )
48- . GetProperties ( BindingFlags . Instance | BindingFlags . Public | BindingFlags . NonPublic )
49- . ToDictionary ( p => p . Name ) ;
50- _propertyData = propertyValueGetters . Select ( p => new PropertyData ( p . Key , properties [ p . Key ] . PropertyType , p . Value ) ) . ToArray ( ) ;
70+ . GetProperties ( BindingFlags . Instance | BindingFlags . Public | BindingFlags . NonPublic )
71+ . ToDictionary ( p => p . Name ) ;
72+ _propertyData = propertyValueGetters . Select (
73+ p => new PropertyData (
74+ p . Key ,
75+ properties [ p . Key ] . PropertyType ,
76+ p . Value ,
77+ propertyDefaultGetters . GetValueOrDefault ( p . Key , o => GetDefault ( properties [ p . Key ] . PropertyType ) ) ) )
78+ . ToArray ( ) ;
5179 }
5280
5381 protected PropertyCSScriptSerializer ( IReadOnlyCollection < PropertyData > propertyData ,
54- IReadOnlyCollection < Func < T , object > > parameterGetters )
55- : base ( parameterGetters )
82+ IReadOnlyCollection < Func < T , object > > constructorParameterGetters )
83+ : base ( constructorParameterGetters )
5684 {
5785 _propertyData = propertyData ;
5886 }
@@ -62,21 +90,25 @@ protected PropertyCSScriptSerializer(IReadOnlyCollection<PropertyData> propertyD
6290 public override ExpressionSyntax GetCreation ( object obj )
6391 => GetObjectCreationExpression ( ( T ) obj ) ;
6492
65- // TODO: custom defaults
6693 protected override ObjectCreationExpressionSyntax GetObjectCreationExpression ( T obj )
6794 => base . GetObjectCreationExpression ( obj )
6895 . WithInitializer ( AddNewLine (
6996 SyntaxFactory . InitializerExpression (
7097 SyntaxKind . ObjectInitializerExpression ,
7198 SyntaxFactory . SeparatedList < ExpressionSyntax > (
7299 ToCommaSeparatedList ( _propertyData
73- . Select ( p => new { p . PropertyName , p . PropertyType , PropertyValue = p . PropertyInitializer ( obj ) } )
74- . Where ( p => ! IsDefault ( p . PropertyValue , p . PropertyType ) )
75- . Select ( p =>
76- SyntaxFactory . AssignmentExpression (
77- SyntaxKind . SimpleAssignmentExpression ,
78- SyntaxFactory . IdentifierName ( p . PropertyName ) ,
79- GetCreationExpression ( p . PropertyValue ) ) ) ) ) ) ) ) ;
100+ . Select ( p => new
101+ {
102+ p . PropertyName ,
103+ p . PropertyType ,
104+ PropertyValue = p . PropertyValueGetter ( obj ) ,
105+ PropertyDefault = p . PropertyDefaultGetter ( obj )
106+ } )
107+ . Where ( p => ! Equals ( p . PropertyValue , p . PropertyDefault ) )
108+ . Select ( p => SyntaxFactory . AssignmentExpression (
109+ SyntaxKind . SimpleAssignmentExpression ,
110+ SyntaxFactory . IdentifierName ( p . PropertyName ) ,
111+ GetCreationExpression ( p . PropertyValue ) ) ) ) ) ) ) ) ;
80112
81113 protected static Func < T , object > CreatePropertyInitializer ( PropertyInfo property )
82114 {
@@ -91,16 +123,22 @@ protected static Func<T, object> CreatePropertyInitializer(PropertyInfo property
91123
92124 protected class PropertyData
93125 {
94- public PropertyData ( string propertyName , Type propertyType , Func < T , object > propertyInitializer )
126+ public PropertyData (
127+ string propertyName ,
128+ Type propertyType ,
129+ Func < T , object > propertyValueGetter ,
130+ Func < T , object > propertyDefaultGetter )
95131 {
96132 PropertyName = propertyName ;
97133 PropertyType = propertyType ;
98- PropertyInitializer = propertyInitializer ;
134+ PropertyValueGetter = propertyValueGetter ;
135+ PropertyDefaultGetter = propertyDefaultGetter ;
99136 }
100137
101138 public string PropertyName { get ; }
102139 public Type PropertyType { get ; }
103- public Func < T , object > PropertyInitializer { get ; }
140+ public Func < T , object > PropertyValueGetter { get ; }
141+ public Func < T , object > PropertyDefaultGetter { get ; }
104142 }
105143 }
106144}
0 commit comments