@@ -10,17 +10,36 @@ namespace TableStorage.Abstractions.TableEntityConverters
1010{
1111 public static class EntityConvert
1212 {
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 )
1425 {
26+ _ = jsonSerializerSettings ?? throw new ArgumentNullException ( nameof ( jsonSerializerSettings ) ) ;
1527 var type = typeof ( T ) ;
1628 var properties = GetProperties ( type ) ;
1729 RemoveIgnoredProperties ( properties , ignoredProperties ) ;
18- return CreateTableEntity ( o , properties , partitionKey , rowKey ) ;
30+ return CreateTableEntity ( o , properties , partitionKey , rowKey , jsonSerializerSettings ) ;
1931 }
2032
2133 public static DynamicTableEntity ToTableEntity < T > ( this T o , Expression < Func < T , object > > partitionProperty ,
2234 Expression < Func < T , object > > rowProperty , params Expression < Func < T , object > > [ ] ignoredProperties )
2335 {
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 ) ) ;
2443 var type = typeof ( T ) ;
2544 var properties = GetProperties ( type ) ;
2645 var partitionProp =
@@ -43,13 +62,22 @@ public static DynamicTableEntity ToTableEntity<T>(this T o, Expression<Func<T, o
4362 var partitionKey = partitionProp . GetValue ( o ) . ToString ( ) ;
4463 var rowKey = rowProp . GetValue ( o ) . ToString ( ) ;
4564
46- return CreateTableEntity ( o , properties , partitionKey , rowKey ) ;
65+ return CreateTableEntity ( o , properties , partitionKey , rowKey , jsonSerializerSettings ) ;
4766 }
4867
4968 public static T FromTableEntity < T , TP , TR > ( this DynamicTableEntity entity ,
5069 Expression < Func < T , object > > partitionProperty ,
5170 Expression < Func < T , object > > rowProperty ) where T : new ( )
5271 {
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+
5381 var convertPartition = new Func < string , TP > ( p => ( TP ) Convert . ChangeType ( p , typeof ( TP ) ) ) ;
5482 var convertRow = new Func < string , TR > ( r => ( TR ) Convert . ChangeType ( r , typeof ( TR ) ) ) ;
5583
@@ -62,14 +90,25 @@ public static T FromTableEntity<T, TP, TR>(this DynamicTableEntity entity,
6290 convertRow = r => ( TR ) ( object ) Guid . Parse ( r ) ;
6391 }
6492 return FromTableEntity ( entity , partitionProperty , convertPartition ,
65- rowProperty , convertRow ) ;
93+ rowProperty , convertRow , jsonSerializerSettings ) ;
6694 }
6795
6896 public static T FromTableEntity < T , TP , TR > ( this DynamicTableEntity entity ,
6997 Expression < Func < T , object > > partitionProperty ,
7098 Func < string , TP > convertPartitionKey , Expression < Func < T , object > > rowProperty ,
7199 Func < string , TR > convertRowKey ) where T : new ( )
72100 {
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+
73112 var o = new T ( ) ;
74113 var type = typeof ( T ) ;
75114 var properties = GetProperties ( type ) ;
@@ -99,13 +138,20 @@ public static T FromTableEntity<T, TP, TR>(this DynamicTableEntity entity,
99138 }
100139
101140 SetTimestamp ( entity , o , properties ) ;
102- FillProperties ( entity , o , properties ) ;
141+ FillProperties ( entity , o , properties , jsonSerializerSettings ) ;
103142 return o ;
104143 }
105144
106145 public static T FromTableEntity < T > ( this DynamicTableEntity entity ) where T : new ( )
107146 {
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 ) ;
109155 }
110156
111157 internal static string GetPropertyNameFromExpression < T > ( Expression < Func < T , object > > exp )
@@ -151,7 +197,7 @@ internal static string GetPropertyNameFromExpression<T>(Expression<Func<T, objec
151197 }
152198 }
153199
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 ( )
155201 {
156202 foreach ( var propertyInfo in properties )
157203 {
@@ -191,15 +237,15 @@ internal static string GetPropertyNameFromExpression<T>(Expression<Func<T, objec
191237 var val = entity . Properties [ $ "{ propertyInfo . Name } Json"] . StringValue ;
192238 if ( val != null )
193239 {
194- var propVal = JsonConvert . DeserializeObject ( val , propertyInfo . PropertyType ) ;
240+ var propVal = JsonConvert . DeserializeObject ( val , propertyInfo . PropertyType , jsonSerializerSettings ?? _defaultJsonSerializerSettings ) ;
195241 propertyInfo . SetValue ( o , propVal ) ;
196242 }
197243 }
198244 }
199245 }
200246
201247 private static DynamicTableEntity CreateTableEntity ( object o , List < PropertyInfo > properties ,
202- string partitionKey , string rowKey )
248+ string partitionKey , string rowKey , JsonSerializerSettings jsonSerializerSettings )
203249 {
204250 var entity = new DynamicTableEntity ( partitionKey , rowKey ) ;
205251 foreach ( var propertyInfo in properties )
@@ -247,7 +293,7 @@ private static DynamicTableEntity CreateTableEntity(object o, List<PropertyInfo>
247293 break ;
248294 default :
249295 name += "Json" ;
250- entityProperty = new EntityProperty ( JsonConvert . SerializeObject ( val ) ) ;
296+ entityProperty = new EntityProperty ( JsonConvert . SerializeObject ( val , jsonSerializerSettings ?? _defaultJsonSerializerSettings ) ) ;
251297 break ;
252298 }
253299 entity . Properties [ name ] = entityProperty ;
0 commit comments