66using System . Collections . Generic ;
77using System . Reflection ;
88using System . Threading . Tasks ;
9- using Newtonsoft . Json ;
109using Windows . Storage ;
1110
1211namespace Microsoft . Toolkit . Uwp . Helpers
@@ -16,6 +15,19 @@ namespace Microsoft.Toolkit.Uwp.Helpers
1615 /// </summary>
1716 public abstract class BaseObjectStorageHelper : IObjectStorageHelper
1817 {
18+ private readonly IObjectSerializer serializer ;
19+
20+ /// <summary>
21+ /// Initializes a new instance of the <see cref="BaseObjectStorageHelper"/> class,
22+ /// which can read and write data using the provided <see cref="IObjectSerializer"/>;
23+ /// if none is provided, a default Json serializer will be used.
24+ /// </summary>
25+ /// <param name="objectSerializer">The serializer to use.</param>
26+ public BaseObjectStorageHelper ( IObjectSerializer objectSerializer = null )
27+ {
28+ serializer = objectSerializer ?? new JsonObjectSerializer ( ) ;
29+ }
30+
1931 /// <summary>
2032 /// Gets or sets the settings container.
2133 /// </summary>
@@ -78,7 +90,7 @@ public bool KeyExists(string compositeKey, string key)
7890 return ( T ) Convert . ChangeType ( value , type ) ;
7991 }
8092
81- return JsonConvert . DeserializeObject < T > ( ( string ) value ) ;
93+ return serializer . Deserialize < T > ( ( string ) value ) ;
8294 }
8395
8496 /// <summary>
@@ -97,7 +109,7 @@ public bool KeyExists(string compositeKey, string key)
97109 string value = ( string ) composite [ key ] ;
98110 if ( value != null )
99111 {
100- return JsonConvert . DeserializeObject < T > ( value ) ;
112+ return serializer . Deserialize < T > ( value ) ;
101113 }
102114 }
103115
@@ -123,7 +135,7 @@ public void Save<T>(string key, T value)
123135 }
124136 else
125137 {
126- Settings . Values [ key ] = JsonConvert . SerializeObject ( value ) ;
138+ Settings . Values [ key ] = serializer . Serialize ( value ) ;
127139 }
128140 }
129141
@@ -146,11 +158,11 @@ public void Save<T>(string compositeKey, IDictionary<string, T> values)
146158 {
147159 if ( composite . ContainsKey ( setting . Key ) )
148160 {
149- composite [ setting . Key ] = JsonConvert . SerializeObject ( setting . Value ) ;
161+ composite [ setting . Key ] = serializer . Serialize ( setting . Value ) ;
150162 }
151163 else
152164 {
153- composite . Add ( setting . Key , JsonConvert . SerializeObject ( setting . Value ) ) ;
165+ composite . Add ( setting . Key , serializer . Serialize ( setting . Value ) ) ;
154166 }
155167 }
156168 }
@@ -159,7 +171,7 @@ public void Save<T>(string compositeKey, IDictionary<string, T> values)
159171 ApplicationDataCompositeValue composite = new ApplicationDataCompositeValue ( ) ;
160172 foreach ( KeyValuePair < string , T > setting in values )
161173 {
162- composite . Add ( setting . Key , JsonConvert . SerializeObject ( setting . Value ) ) ;
174+ composite . Add ( setting . Key , serializer . Serialize ( setting . Value ) ) ;
163175 }
164176
165177 Settings . Values [ compositeKey ] = composite ;
@@ -186,7 +198,7 @@ public Task<bool> FileExistsAsync(string filePath)
186198 public async Task < T > ReadFileAsync < T > ( string filePath , T @default = default ( T ) )
187199 {
188200 string value = await StorageFileHelper . ReadTextFromFileAsync ( Folder , filePath ) ;
189- return ( value != null ) ? JsonConvert . DeserializeObject < T > ( value ) : @default ;
201+ return ( value != null ) ? serializer . Deserialize < T > ( value ) : @default ;
190202 }
191203
192204 /// <summary>
@@ -199,7 +211,7 @@ public Task<bool> FileExistsAsync(string filePath)
199211 /// <returns>The <see cref="StorageFile"/> where the object was saved</returns>
200212 public Task < StorageFile > SaveFileAsync < T > ( string filePath , T value )
201213 {
202- return StorageFileHelper . WriteTextToFileAsync ( Folder , JsonConvert . SerializeObject ( value ) , filePath , CreationCollisionOption . ReplaceExisting ) ;
214+ return StorageFileHelper . WriteTextToFileAsync ( Folder , serializer . Serialize ( value ) , filePath , CreationCollisionOption . ReplaceExisting ) ;
203215 }
204216 }
205217}
0 commit comments