@@ -16,7 +16,26 @@ namespace Microsoft.Toolkit.Uwp.Helpers
1616 /// </summary>
1717 public abstract class BaseObjectStorageHelper : IObjectStorageHelper
1818 {
19- private JsonSerializer serializer = new JsonSerializer ( ) ;
19+ private readonly JsonSerializerSettings settings ;
20+
21+ /// <summary>
22+ /// Initializes a new instance of the <see cref="BaseObjectStorageHelper"/> class,
23+ /// which can read and write serialized data using the provided <see cref="JsonSerializerSettings"/>.
24+ /// </summary>
25+ /// <param name="serializerSettings">The serializer settings to use.</param>
26+ public BaseObjectStorageHelper ( JsonSerializerSettings serializerSettings )
27+ {
28+ settings = serializerSettings ;
29+ }
30+
31+ /// <summary>
32+ /// Initializes a new instance of the <see cref="BaseObjectStorageHelper"/> class,
33+ /// which can read and write serialized data using the default <see cref="JsonSerializerSettings"/>.
34+ /// </summary>
35+ public BaseObjectStorageHelper ( )
36+ : this ( new JsonSerializerSettings ( ) )
37+ {
38+ }
2039
2140 /// <summary>
2241 /// Gets or sets the settings container.
@@ -87,7 +106,7 @@ public bool KeyExists(string compositeKey, string key)
87106 return ( T ) Convert . ChangeType ( value , type ) ;
88107 }
89108
90- return JsonConvert . DeserializeObject < T > ( ( string ) value ) ;
109+ return Deserialize < T > ( ( string ) value ) ;
91110 }
92111
93112 /// <summary>
@@ -106,7 +125,7 @@ public bool KeyExists(string compositeKey, string key)
106125 string value = ( string ) composite [ key ] ;
107126 if ( value != null )
108127 {
109- return JsonConvert . DeserializeObject < T > ( value ) ;
128+ return Deserialize < T > ( value ) ;
110129 }
111130 }
112131
@@ -132,7 +151,7 @@ public void Save<T>(string key, T value)
132151 }
133152 else
134153 {
135- Settings . Values [ key ] = JsonConvert . SerializeObject ( value ) ;
154+ Settings . Values [ key ] = Serialize ( value ) ;
136155 }
137156 }
138157
@@ -155,11 +174,11 @@ public void Save<T>(string compositeKey, IDictionary<string, T> values)
155174 {
156175 if ( composite . ContainsKey ( setting . Key ) )
157176 {
158- composite [ setting . Key ] = JsonConvert . SerializeObject ( setting . Value ) ;
177+ composite [ setting . Key ] = Serialize ( setting . Value ) ;
159178 }
160179 else
161180 {
162- composite . Add ( setting . Key , JsonConvert . SerializeObject ( setting . Value ) ) ;
181+ composite . Add ( setting . Key , Serialize ( setting . Value ) ) ;
163182 }
164183 }
165184 }
@@ -168,7 +187,7 @@ public void Save<T>(string compositeKey, IDictionary<string, T> values)
168187 ApplicationDataCompositeValue composite = new ApplicationDataCompositeValue ( ) ;
169188 foreach ( KeyValuePair < string , T > setting in values )
170189 {
171- composite . Add ( setting . Key , JsonConvert . SerializeObject ( setting . Value ) ) ;
190+ composite . Add ( setting . Key , Serialize ( setting . Value ) ) ;
172191 }
173192
174193 Settings . Values [ compositeKey ] = composite ;
@@ -195,7 +214,7 @@ public Task<bool> FileExistsAsync(string filePath)
195214 public async Task < T > ReadFileAsync < T > ( string filePath , T @default = default ( T ) )
196215 {
197216 string value = await StorageFileHelper . ReadTextFromFileAsync ( Folder , filePath ) ;
198- return ( value != null ) ? JsonConvert . DeserializeObject < T > ( value ) : @default ;
217+ return ( value != null ) ? Deserialize < T > ( value ) : @default ;
199218 }
200219
201220 /// <summary>
@@ -208,7 +227,11 @@ public Task<bool> FileExistsAsync(string filePath)
208227 /// <returns>The <see cref="StorageFile"/> where the object was saved</returns>
209228 public Task < StorageFile > SaveFileAsync < T > ( string filePath , T value )
210229 {
211- return StorageFileHelper . WriteTextToFileAsync ( Folder , JsonConvert . SerializeObject ( value ) , filePath , CreationCollisionOption . ReplaceExisting ) ;
230+ return StorageFileHelper . WriteTextToFileAsync ( Folder , Serialize ( value ) , filePath , CreationCollisionOption . ReplaceExisting ) ;
212231 }
232+
233+ private string Serialize < T > ( T value ) => JsonConvert . SerializeObject ( value , typeof ( T ) , settings ) ;
234+
235+ private T Deserialize < T > ( string value ) => JsonConvert . DeserializeObject < T > ( value , settings ) ;
213236 }
214237}
0 commit comments