@@ -11,7 +11,7 @@ namespace TableStorage.Abstractions.TableEntityConverters
11
11
public static class EntityConvert
12
12
{
13
13
private static JsonSerializerSettings _defaultJsonSerializerSettings = new JsonSerializerSettings ( ) ;
14
-
14
+
15
15
/// <summary>
16
16
/// Json fields will use be serialized/deserialized with these provided settings when jsonSerializerSettings are
17
17
/// not explicitly passed into ToTableEntity/FromTableEntity
@@ -25,25 +25,31 @@ public static void SetDefaultJsonSerializerSettings (JsonSerializerSettings json
25
25
public static DynamicTableEntity ToTableEntity < T > ( this T o , string partitionKey , string rowKey ,
26
26
params Expression < Func < T , object > > [ ] ignoredProperties )
27
27
{
28
- return ToTableEntity ( o , partitionKey , rowKey , _defaultJsonSerializerSettings , ignoredProperties ) ;
28
+ return ToTableEntity ( o , partitionKey , rowKey , _defaultJsonSerializerSettings , default , ignoredProperties ) ;
29
29
}
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 )
31
34
{
32
35
_ = jsonSerializerSettings ?? throw new ArgumentNullException ( nameof ( jsonSerializerSettings ) ) ;
33
36
var type = typeof ( T ) ;
34
37
var properties = GetProperties ( type ) ;
35
38
RemoveIgnoredProperties ( properties , ignoredProperties ) ;
36
- return CreateTableEntity ( o , properties , partitionKey , rowKey , jsonSerializerSettings ) ;
39
+ return CreateTableEntity ( o , properties , partitionKey , rowKey , jsonSerializerSettings , propertyConverters ) ;
37
40
}
38
41
39
42
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 )
41
45
{
42
- return ToTableEntity ( o , partitionProperty , rowProperty , _defaultJsonSerializerSettings , ignoredProperties ) ;
46
+ return ToTableEntity ( o , partitionProperty , rowProperty , _defaultJsonSerializerSettings , null , ignoredProperties ) ;
43
47
}
44
48
45
49
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 )
47
53
{
48
54
_ = jsonSerializerSettings ?? throw new ArgumentNullException ( nameof ( jsonSerializerSettings ) ) ;
49
55
var type = typeof ( T ) ;
@@ -64,11 +70,10 @@ public static DynamicTableEntity ToTableEntity<T>(this T o, Expression<Func<T, o
64
70
properties . Remove ( partitionProp ) ;
65
71
properties . Remove ( rowProp ) ;
66
72
RemoveIgnoredProperties ( properties , ignoredProperties ) ;
67
-
68
73
var partitionKey = partitionProp . GetValue ( o ) . ToString ( ) ;
69
74
var rowKey = rowProp . GetValue ( o ) . ToString ( ) ;
70
75
71
- return CreateTableEntity ( o , properties , partitionKey , rowKey , jsonSerializerSettings ) ;
76
+ return CreateTableEntity ( o , properties , partitionKey , rowKey , jsonSerializerSettings , propertyConverters ) ;
72
77
}
73
78
74
79
public static T FromTableEntity < T , TP , TR > ( this DynamicTableEntity entity ,
@@ -80,7 +85,7 @@ public static T FromTableEntity<T, TP, TR>(this DynamicTableEntity entity,
80
85
81
86
public static T FromTableEntity < T , TP , TR > ( this DynamicTableEntity entity ,
82
87
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 ( )
84
89
{
85
90
_ = jsonSerializerSettings ?? throw new ArgumentNullException ( nameof ( jsonSerializerSettings ) ) ;
86
91
@@ -96,7 +101,7 @@ public static T FromTableEntity<T, TP, TR>(this DynamicTableEntity entity,
96
101
convertRow = r => ( TR ) ( object ) Guid . Parse ( r ) ;
97
102
}
98
103
return FromTableEntity ( entity , partitionProperty , convertPartition ,
99
- rowProperty , convertRow , jsonSerializerSettings ) ;
104
+ rowProperty , convertRow , jsonSerializerSettings , propertyConverters ) ;
100
105
}
101
106
102
107
public static T FromTableEntity < T , TP , TR > ( this DynamicTableEntity entity ,
@@ -111,7 +116,7 @@ public static T FromTableEntity<T, TP, TR>(this DynamicTableEntity entity,
111
116
public static T FromTableEntity < T , TP , TR > ( this DynamicTableEntity entity ,
112
117
Expression < Func < T , object > > partitionProperty ,
113
118
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 ( )
115
120
{
116
121
_ = jsonSerializerSettings ?? throw new ArgumentNullException ( nameof ( jsonSerializerSettings ) ) ;
117
122
@@ -144,7 +149,7 @@ public static T FromTableEntity<T, TP, TR>(this DynamicTableEntity entity,
144
149
}
145
150
146
151
SetTimestamp ( entity , o , properties ) ;
147
- FillProperties ( entity , o , properties , jsonSerializerSettings ) ;
152
+ FillProperties ( entity , o , properties , jsonSerializerSettings , propertyConverters ) ;
148
153
return o ;
149
154
}
150
155
@@ -153,11 +158,11 @@ public static T FromTableEntity<T, TP, TR>(this DynamicTableEntity entity,
153
158
return FromTableEntity < T > ( entity , _defaultJsonSerializerSettings ) ;
154
159
}
155
160
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 ( )
157
162
{
158
163
_ = jsonSerializerSettings ?? throw new ArgumentNullException ( nameof ( jsonSerializerSettings ) ) ;
159
164
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 ) ;
161
166
}
162
167
163
168
internal static string GetPropertyNameFromExpression < T > ( Expression < Func < T , object > > exp )
@@ -203,11 +208,15 @@ internal static string GetPropertyNameFromExpression<T>(Expression<Func<T, objec
203
208
}
204
209
}
205
210
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 ( )
207
212
{
208
213
foreach ( var propertyInfo in properties )
209
214
{
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 ) )
211
220
{
212
221
var val = entity . Properties [ propertyInfo . Name ] . PropertyAsObject ;
213
222
@@ -250,58 +259,67 @@ internal static string GetPropertyNameFromExpression<T>(Expression<Func<T, objec
250
259
}
251
260
}
252
261
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 )
255
264
{
256
265
var entity = new DynamicTableEntity ( partitionKey , rowKey ) ;
257
266
foreach ( var propertyInfo in properties )
258
267
{
259
268
var name = propertyInfo . Name ;
260
269
var val = propertyInfo . GetValue ( o ) ;
261
270
EntityProperty entityProperty ;
262
- switch ( val )
271
+ if ( propertyConverters != null && propertyConverters . ContainsKey ( name ) )
263
272
{
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 ) ;
304
274
}
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
+
305
323
entity . Properties [ name ] = entityProperty ;
306
324
}
307
325
return entity ;
0 commit comments