@@ -11,7 +11,7 @@ namespace TableStorage.Abstractions.TableEntityConverters
1111 public static class EntityConvert
1212 {
1313 private static JsonSerializerSettings _defaultJsonSerializerSettings = new JsonSerializerSettings ( ) ;
14-
14+
1515 /// <summary>
1616 /// Json fields will use be serialized/deserialized with these provided settings when jsonSerializerSettings are
1717 /// not explicitly passed into ToTableEntity/FromTableEntity
@@ -25,25 +25,31 @@ public static void SetDefaultJsonSerializerSettings (JsonSerializerSettings json
2525 public static DynamicTableEntity ToTableEntity < T > ( this T o , string partitionKey , string rowKey ,
2626 params Expression < Func < T , object > > [ ] ignoredProperties )
2727 {
28- return ToTableEntity ( o , partitionKey , rowKey , _defaultJsonSerializerSettings , ignoredProperties ) ;
28+ return ToTableEntity ( o , partitionKey , rowKey , _defaultJsonSerializerSettings , default , ignoredProperties ) ;
2929 }
30- public static DynamicTableEntity ToTableEntity < T > ( this T o , string partitionKey , string rowKey , JsonSerializerSettings jsonSerializerSettings , params Expression < Func < T , object > > [ ] ignoredProperties )
30+ public static DynamicTableEntity ToTableEntity < T > ( this T o , string partitionKey , string rowKey ,
31+ JsonSerializerSettings jsonSerializerSettings ,
32+ PropertyConverters < T > propertyConverters ,
33+ params Expression < Func < T , object > > [ ] ignoredProperties )
3134 {
3235 _ = jsonSerializerSettings ?? throw new ArgumentNullException ( nameof ( jsonSerializerSettings ) ) ;
3336 var type = typeof ( T ) ;
3437 var properties = GetProperties ( type ) ;
3538 RemoveIgnoredProperties ( properties , ignoredProperties ) ;
36- return CreateTableEntity ( o , properties , partitionKey , rowKey , jsonSerializerSettings ) ;
39+ return CreateTableEntity ( o , properties , partitionKey , rowKey , jsonSerializerSettings , propertyConverters ) ;
3740 }
3841
3942 public static DynamicTableEntity ToTableEntity < T > ( this T o , Expression < Func < T , object > > partitionProperty ,
40- Expression < Func < T , object > > rowProperty , params Expression < Func < T , object > > [ ] ignoredProperties )
43+ Expression < Func < T , object > > rowProperty ,
44+ params Expression < Func < T , object > > [ ] ignoredProperties )
4145 {
42- return ToTableEntity ( o , partitionProperty , rowProperty , _defaultJsonSerializerSettings , ignoredProperties ) ;
46+ return ToTableEntity ( o , partitionProperty , rowProperty , _defaultJsonSerializerSettings , null , ignoredProperties ) ;
4347 }
4448
4549 public static DynamicTableEntity ToTableEntity < T > ( this T o , Expression < Func < T , object > > partitionProperty ,
46- Expression < Func < T , object > > rowProperty , JsonSerializerSettings jsonSerializerSettings , params Expression < Func < T , object > > [ ] ignoredProperties )
50+ Expression < Func < T , object > > rowProperty , JsonSerializerSettings jsonSerializerSettings ,
51+ PropertyConverters < T > propertyConverters = null ,
52+ params Expression < Func < T , object > > [ ] ignoredProperties )
4753 {
4854 _ = jsonSerializerSettings ?? throw new ArgumentNullException ( nameof ( jsonSerializerSettings ) ) ;
4955 var type = typeof ( T ) ;
@@ -64,11 +70,10 @@ public static DynamicTableEntity ToTableEntity<T>(this T o, Expression<Func<T, o
6470 properties . Remove ( partitionProp ) ;
6571 properties . Remove ( rowProp ) ;
6672 RemoveIgnoredProperties ( properties , ignoredProperties ) ;
67-
6873 var partitionKey = partitionProp . GetValue ( o ) . ToString ( ) ;
6974 var rowKey = rowProp . GetValue ( o ) . ToString ( ) ;
7075
71- return CreateTableEntity ( o , properties , partitionKey , rowKey , jsonSerializerSettings ) ;
76+ return CreateTableEntity ( o , properties , partitionKey , rowKey , jsonSerializerSettings , propertyConverters ) ;
7277 }
7378
7479 public static T FromTableEntity < T , TP , TR > ( this DynamicTableEntity entity ,
@@ -80,7 +85,7 @@ public static T FromTableEntity<T, TP, TR>(this DynamicTableEntity entity,
8085
8186 public static T FromTableEntity < T , TP , TR > ( this DynamicTableEntity entity ,
8287 Expression < Func < T , object > > partitionProperty ,
83- Expression < Func < T , object > > rowProperty , JsonSerializerSettings jsonSerializerSettings ) where T : new ( )
88+ Expression < Func < T , object > > rowProperty , JsonSerializerSettings jsonSerializerSettings , PropertyConverters < T > propertyConverters = null ) where T : new ( )
8489 {
8590 _ = jsonSerializerSettings ?? throw new ArgumentNullException ( nameof ( jsonSerializerSettings ) ) ;
8691
@@ -96,7 +101,7 @@ public static T FromTableEntity<T, TP, TR>(this DynamicTableEntity entity,
96101 convertRow = r => ( TR ) ( object ) Guid . Parse ( r ) ;
97102 }
98103 return FromTableEntity ( entity , partitionProperty , convertPartition ,
99- rowProperty , convertRow , jsonSerializerSettings ) ;
104+ rowProperty , convertRow , jsonSerializerSettings , propertyConverters ) ;
100105 }
101106
102107 public static T FromTableEntity < T , TP , TR > ( this DynamicTableEntity entity ,
@@ -111,7 +116,7 @@ public static T FromTableEntity<T, TP, TR>(this DynamicTableEntity entity,
111116 public static T FromTableEntity < T , TP , TR > ( this DynamicTableEntity entity ,
112117 Expression < Func < T , object > > partitionProperty ,
113118 Func < string , TP > convertPartitionKey , Expression < Func < T , object > > rowProperty ,
114- Func < string , TR > convertRowKey , JsonSerializerSettings jsonSerializerSettings ) where T : new ( )
119+ Func < string , TR > convertRowKey , JsonSerializerSettings jsonSerializerSettings , PropertyConverters < T > propertyConverters = null ) where T : new ( )
115120 {
116121 _ = jsonSerializerSettings ?? throw new ArgumentNullException ( nameof ( jsonSerializerSettings ) ) ;
117122
@@ -144,7 +149,7 @@ public static T FromTableEntity<T, TP, TR>(this DynamicTableEntity entity,
144149 }
145150
146151 SetTimestamp ( entity , o , properties ) ;
147- FillProperties ( entity , o , properties , jsonSerializerSettings ) ;
152+ FillProperties ( entity , o , properties , jsonSerializerSettings , propertyConverters ) ;
148153 return o ;
149154 }
150155
@@ -153,11 +158,11 @@ public static T FromTableEntity<T, TP, TR>(this DynamicTableEntity entity,
153158 return FromTableEntity < T > ( entity , _defaultJsonSerializerSettings ) ;
154159 }
155160
156- public static T FromTableEntity < T > ( this DynamicTableEntity entity , JsonSerializerSettings jsonSerializerSettings ) where T : new ( )
161+ public static T FromTableEntity < T > ( this DynamicTableEntity entity , JsonSerializerSettings jsonSerializerSettings , PropertyConverters < T > propertyConverters = null ) where T : new ( )
157162 {
158163 _ = jsonSerializerSettings ?? throw new ArgumentNullException ( nameof ( jsonSerializerSettings ) ) ;
159164
160- return entity . FromTableEntity < T , object , object > ( null , null , null , null , jsonSerializerSettings ) ;
165+ return entity . FromTableEntity < T , object , object > ( null , null , null , null , jsonSerializerSettings , propertyConverters ) ;
161166 }
162167
163168 internal static string GetPropertyNameFromExpression < T > ( Expression < Func < T , object > > exp )
@@ -203,11 +208,15 @@ internal static string GetPropertyNameFromExpression<T>(Expression<Func<T, objec
203208 }
204209 }
205210
206- private static void FillProperties < T > ( DynamicTableEntity entity , T o , List < PropertyInfo > properties , JsonSerializerSettings jsonSerializerSettings ) where T : new ( )
211+ private static void FillProperties < T > ( DynamicTableEntity entity , T o , List < PropertyInfo > properties , JsonSerializerSettings jsonSerializerSettings , PropertyConverters < T > propertyConverters ) where T : new ( )
207212 {
208213 foreach ( var propertyInfo in properties )
209214 {
210- if ( entity . Properties . ContainsKey ( propertyInfo . Name ) && propertyInfo . Name != nameof ( DynamicTableEntity . Timestamp ) )
215+ if ( propertyConverters != null && entity . Properties . ContainsKey ( propertyInfo . Name ) && propertyConverters . ContainsKey ( propertyInfo . Name ) )
216+ {
217+ propertyConverters [ propertyInfo . Name ] . SetObjectProperty ( o , entity . Properties [ propertyInfo . Name ] ) ;
218+ }
219+ else if ( entity . Properties . ContainsKey ( propertyInfo . Name ) && propertyInfo . Name != nameof ( DynamicTableEntity . Timestamp ) )
211220 {
212221 var val = entity . Properties [ propertyInfo . Name ] . PropertyAsObject ;
213222
@@ -250,58 +259,67 @@ internal static string GetPropertyNameFromExpression<T>(Expression<Func<T, objec
250259 }
251260 }
252261
253- private static DynamicTableEntity CreateTableEntity ( object o , List < PropertyInfo > properties ,
254- string partitionKey , string rowKey , JsonSerializerSettings jsonSerializerSettings )
262+ private static DynamicTableEntity CreateTableEntity < T > ( object o , List < PropertyInfo > properties ,
263+ string partitionKey , string rowKey , JsonSerializerSettings jsonSerializerSettings , PropertyConverters < T > propertyConverters )
255264 {
256265 var entity = new DynamicTableEntity ( partitionKey , rowKey ) ;
257266 foreach ( var propertyInfo in properties )
258267 {
259268 var name = propertyInfo . Name ;
260269 var val = propertyInfo . GetValue ( o ) ;
261270 EntityProperty entityProperty ;
262- switch ( val )
271+ if ( propertyConverters != null && propertyConverters . ContainsKey ( name ) )
263272 {
264- case int x :
265- entityProperty = new EntityProperty ( x ) ;
266- break ;
267- case short x :
268- entityProperty = new EntityProperty ( x ) ;
269- break ;
270- case byte x :
271- entityProperty = new EntityProperty ( x ) ;
272- break ;
273- case string x :
274- entityProperty = new EntityProperty ( x ) ;
275- break ;
276- case double x :
277- entityProperty = new EntityProperty ( x ) ;
278- break ;
279- case DateTime x :
280- entityProperty = new EntityProperty ( x ) ;
281- break ;
282- case DateTimeOffset x :
283- entityProperty = new EntityProperty ( x ) ;
284- break ;
285- case bool x :
286- entityProperty = new EntityProperty ( x ) ;
287- break ;
288- case byte [ ] x :
289- entityProperty = new EntityProperty ( x ) ;
290- break ;
291- case long x :
292- entityProperty = new EntityProperty ( x ) ;
293- break ;
294- case Guid x :
295- entityProperty = new EntityProperty ( x ) ;
296- break ;
297- case null :
298- entityProperty = new EntityProperty ( ( int ? ) null ) ;
299- break ;
300- default :
301- name += "Json" ;
302- entityProperty = new EntityProperty ( JsonConvert . SerializeObject ( val , jsonSerializerSettings ?? _defaultJsonSerializerSettings ) ) ;
303- break ;
273+ entityProperty = propertyConverters [ name ] . ToTableEntityProperty ( ( T ) o ) ;
304274 }
275+ else
276+ {
277+ switch ( val )
278+ {
279+ case int x :
280+ entityProperty = new EntityProperty ( x ) ;
281+ break ;
282+ case short x :
283+ entityProperty = new EntityProperty ( x ) ;
284+ break ;
285+ case byte x :
286+ entityProperty = new EntityProperty ( x ) ;
287+ break ;
288+ case string x :
289+ entityProperty = new EntityProperty ( x ) ;
290+ break ;
291+ case double x :
292+ entityProperty = new EntityProperty ( x ) ;
293+ break ;
294+ case DateTime x :
295+ entityProperty = new EntityProperty ( x ) ;
296+ break ;
297+ case DateTimeOffset x :
298+ entityProperty = new EntityProperty ( x ) ;
299+ break ;
300+ case bool x :
301+ entityProperty = new EntityProperty ( x ) ;
302+ break ;
303+ case byte [ ] x :
304+ entityProperty = new EntityProperty ( x ) ;
305+ break ;
306+ case long x :
307+ entityProperty = new EntityProperty ( x ) ;
308+ break ;
309+ case Guid x :
310+ entityProperty = new EntityProperty ( x ) ;
311+ break ;
312+ case null :
313+ entityProperty = new EntityProperty ( ( int ? ) null ) ;
314+ break ;
315+ default :
316+ name += "Json" ;
317+ entityProperty = new EntityProperty ( JsonConvert . SerializeObject ( val ,
318+ jsonSerializerSettings ?? _defaultJsonSerializerSettings ) ) ;
319+ break ;
320+ }
321+ }
322+
305323 entity . Properties [ name ] = entityProperty ;
306324 }
307325 return entity ;
0 commit comments