@@ -10,17 +10,36 @@ namespace TableStorage.Abstractions.TableEntityConverters
10
10
{
11
11
public static class EntityConvert
12
12
{
13
- public static DynamicTableEntity ToTableEntity < T > ( this T o , string partitionKey , string rowKey , params Expression < Func < T , object > > [ ] ignoredProperties )
13
+ private static JsonSerializerSettings _defaultJsonSerializerSettings = new JsonSerializerSettings ( ) ;
14
+
15
+ public static JsonSerializerSettings DefaultJsonSerializerSettings {
16
+ set => _defaultJsonSerializerSettings = value ?? new JsonSerializerSettings ( ) ;
17
+ }
18
+
19
+ public static DynamicTableEntity ToTableEntity < T > ( this T o , string partitionKey , string rowKey ,
20
+ params Expression < Func < T , object > > [ ] ignoredProperties )
21
+ {
22
+ return ToTableEntity ( o , partitionKey , rowKey , _defaultJsonSerializerSettings , ignoredProperties ) ;
23
+ }
24
+ public static DynamicTableEntity ToTableEntity < T > ( this T o , string partitionKey , string rowKey , JsonSerializerSettings jsonSerializerSettings , params Expression < Func < T , object > > [ ] ignoredProperties )
14
25
{
26
+ _ = jsonSerializerSettings ?? throw new ArgumentNullException ( nameof ( jsonSerializerSettings ) ) ;
15
27
var type = typeof ( T ) ;
16
28
var properties = GetProperties ( type ) ;
17
29
RemoveIgnoredProperties ( properties , ignoredProperties ) ;
18
- return CreateTableEntity ( o , properties , partitionKey , rowKey ) ;
30
+ return CreateTableEntity ( o , properties , partitionKey , rowKey , jsonSerializerSettings ) ;
19
31
}
20
32
21
33
public static DynamicTableEntity ToTableEntity < T > ( this T o , Expression < Func < T , object > > partitionProperty ,
22
34
Expression < Func < T , object > > rowProperty , params Expression < Func < T , object > > [ ] ignoredProperties )
23
35
{
36
+ return ToTableEntity ( o , partitionProperty , rowProperty , _defaultJsonSerializerSettings , ignoredProperties ) ;
37
+ }
38
+
39
+ public static DynamicTableEntity ToTableEntity < T > ( this T o , Expression < Func < T , object > > partitionProperty ,
40
+ Expression < Func < T , object > > rowProperty , JsonSerializerSettings jsonSerializerSettings , params Expression < Func < T , object > > [ ] ignoredProperties )
41
+ {
42
+ _ = jsonSerializerSettings ?? throw new ArgumentNullException ( nameof ( jsonSerializerSettings ) ) ;
24
43
var type = typeof ( T ) ;
25
44
var properties = GetProperties ( type ) ;
26
45
var partitionProp =
@@ -43,13 +62,22 @@ public static DynamicTableEntity ToTableEntity<T>(this T o, Expression<Func<T, o
43
62
var partitionKey = partitionProp . GetValue ( o ) . ToString ( ) ;
44
63
var rowKey = rowProp . GetValue ( o ) . ToString ( ) ;
45
64
46
- return CreateTableEntity ( o , properties , partitionKey , rowKey ) ;
65
+ return CreateTableEntity ( o , properties , partitionKey , rowKey , jsonSerializerSettings ) ;
47
66
}
48
67
49
68
public static T FromTableEntity < T , TP , TR > ( this DynamicTableEntity entity ,
50
69
Expression < Func < T , object > > partitionProperty ,
51
70
Expression < Func < T , object > > rowProperty ) where T : new ( )
52
71
{
72
+ return FromTableEntity < T , TP , TR > ( entity , partitionProperty , rowProperty , _defaultJsonSerializerSettings ) ;
73
+ }
74
+
75
+ public static T FromTableEntity < T , TP , TR > ( this DynamicTableEntity entity ,
76
+ Expression < Func < T , object > > partitionProperty ,
77
+ Expression < Func < T , object > > rowProperty , JsonSerializerSettings jsonSerializerSettings ) where T : new ( )
78
+ {
79
+ _ = jsonSerializerSettings ?? throw new ArgumentNullException ( nameof ( jsonSerializerSettings ) ) ;
80
+
53
81
var convertPartition = new Func < string , TP > ( p => ( TP ) Convert . ChangeType ( p , typeof ( TP ) ) ) ;
54
82
var convertRow = new Func < string , TR > ( r => ( TR ) Convert . ChangeType ( r , typeof ( TR ) ) ) ;
55
83
@@ -62,14 +90,25 @@ public static T FromTableEntity<T, TP, TR>(this DynamicTableEntity entity,
62
90
convertRow = r => ( TR ) ( object ) Guid . Parse ( r ) ;
63
91
}
64
92
return FromTableEntity ( entity , partitionProperty , convertPartition ,
65
- rowProperty , convertRow ) ;
93
+ rowProperty , convertRow , jsonSerializerSettings ) ;
66
94
}
67
95
68
96
public static T FromTableEntity < T , TP , TR > ( this DynamicTableEntity entity ,
69
97
Expression < Func < T , object > > partitionProperty ,
70
98
Func < string , TP > convertPartitionKey , Expression < Func < T , object > > rowProperty ,
71
99
Func < string , TR > convertRowKey ) where T : new ( )
72
100
{
101
+ return FromTableEntity ( entity , partitionProperty , convertPartitionKey , rowProperty ,
102
+ convertRowKey , _defaultJsonSerializerSettings ) ;
103
+ }
104
+
105
+ public static T FromTableEntity < T , TP , TR > ( this DynamicTableEntity entity ,
106
+ Expression < Func < T , object > > partitionProperty ,
107
+ Func < string , TP > convertPartitionKey , Expression < Func < T , object > > rowProperty ,
108
+ Func < string , TR > convertRowKey , JsonSerializerSettings jsonSerializerSettings ) where T : new ( )
109
+ {
110
+ _ = jsonSerializerSettings ?? throw new ArgumentNullException ( nameof ( jsonSerializerSettings ) ) ;
111
+
73
112
var o = new T ( ) ;
74
113
var type = typeof ( T ) ;
75
114
var properties = GetProperties ( type ) ;
@@ -99,13 +138,20 @@ public static T FromTableEntity<T, TP, TR>(this DynamicTableEntity entity,
99
138
}
100
139
101
140
SetTimestamp ( entity , o , properties ) ;
102
- FillProperties ( entity , o , properties ) ;
141
+ FillProperties ( entity , o , properties , jsonSerializerSettings ) ;
103
142
return o ;
104
143
}
105
144
106
145
public static T FromTableEntity < T > ( this DynamicTableEntity entity ) where T : new ( )
107
146
{
108
- return entity . FromTableEntity < T , object , object > ( null , null , null , null ) ;
147
+ return FromTableEntity < T > ( entity , _defaultJsonSerializerSettings ) ;
148
+ }
149
+
150
+ public static T FromTableEntity < T > ( this DynamicTableEntity entity , JsonSerializerSettings jsonSerializerSettings ) where T : new ( )
151
+ {
152
+ _ = jsonSerializerSettings ?? throw new ArgumentNullException ( nameof ( jsonSerializerSettings ) ) ;
153
+
154
+ return entity . FromTableEntity < T , object , object > ( null , null , null , null , jsonSerializerSettings ) ;
109
155
}
110
156
111
157
internal static string GetPropertyNameFromExpression < T > ( Expression < Func < T , object > > exp )
@@ -151,7 +197,7 @@ internal static string GetPropertyNameFromExpression<T>(Expression<Func<T, objec
151
197
}
152
198
}
153
199
154
- private static void FillProperties < T > ( DynamicTableEntity entity , T o , List < PropertyInfo > properties ) where T : new ( )
200
+ private static void FillProperties < T > ( DynamicTableEntity entity , T o , List < PropertyInfo > properties , JsonSerializerSettings jsonSerializerSettings ) where T : new ( )
155
201
{
156
202
foreach ( var propertyInfo in properties )
157
203
{
@@ -191,15 +237,15 @@ internal static string GetPropertyNameFromExpression<T>(Expression<Func<T, objec
191
237
var val = entity . Properties [ $ "{ propertyInfo . Name } Json"] . StringValue ;
192
238
if ( val != null )
193
239
{
194
- var propVal = JsonConvert . DeserializeObject ( val , propertyInfo . PropertyType ) ;
240
+ var propVal = JsonConvert . DeserializeObject ( val , propertyInfo . PropertyType , jsonSerializerSettings ?? _defaultJsonSerializerSettings ) ;
195
241
propertyInfo . SetValue ( o , propVal ) ;
196
242
}
197
243
}
198
244
}
199
245
}
200
246
201
247
private static DynamicTableEntity CreateTableEntity ( object o , List < PropertyInfo > properties ,
202
- string partitionKey , string rowKey )
248
+ string partitionKey , string rowKey , JsonSerializerSettings jsonSerializerSettings )
203
249
{
204
250
var entity = new DynamicTableEntity ( partitionKey , rowKey ) ;
205
251
foreach ( var propertyInfo in properties )
@@ -247,7 +293,7 @@ private static DynamicTableEntity CreateTableEntity(object o, List<PropertyInfo>
247
293
break ;
248
294
default :
249
295
name += "Json" ;
250
- entityProperty = new EntityProperty ( JsonConvert . SerializeObject ( val ) ) ;
296
+ entityProperty = new EntityProperty ( JsonConvert . SerializeObject ( val , jsonSerializerSettings ?? _defaultJsonSerializerSettings ) ) ;
251
297
break ;
252
298
}
253
299
entity . Properties [ name ] = entityProperty ;
0 commit comments