6
6
using System . Collections . Generic ;
7
7
using System . Reflection ;
8
8
using System . Threading . Tasks ;
9
- using Newtonsoft . Json ;
10
9
using Windows . Storage ;
11
10
12
11
namespace Microsoft . Toolkit . Uwp . Helpers
@@ -16,6 +15,19 @@ namespace Microsoft.Toolkit.Uwp.Helpers
16
15
/// </summary>
17
16
public abstract class BaseObjectStorageHelper : IObjectStorageHelper
18
17
{
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
+
19
31
/// <summary>
20
32
/// Gets or sets the settings container.
21
33
/// </summary>
@@ -78,7 +90,7 @@ public bool KeyExists(string compositeKey, string key)
78
90
return ( T ) Convert . ChangeType ( value , type ) ;
79
91
}
80
92
81
- return JsonConvert . DeserializeObject < T > ( ( string ) value ) ;
93
+ return serializer . Deserialize < T > ( ( string ) value ) ;
82
94
}
83
95
84
96
/// <summary>
@@ -97,7 +109,7 @@ public bool KeyExists(string compositeKey, string key)
97
109
string value = ( string ) composite [ key ] ;
98
110
if ( value != null )
99
111
{
100
- return JsonConvert . DeserializeObject < T > ( value ) ;
112
+ return serializer . Deserialize < T > ( value ) ;
101
113
}
102
114
}
103
115
@@ -123,7 +135,7 @@ public void Save<T>(string key, T value)
123
135
}
124
136
else
125
137
{
126
- Settings . Values [ key ] = JsonConvert . SerializeObject ( value ) ;
138
+ Settings . Values [ key ] = serializer . Serialize ( value ) ;
127
139
}
128
140
}
129
141
@@ -146,11 +158,11 @@ public void Save<T>(string compositeKey, IDictionary<string, T> values)
146
158
{
147
159
if ( composite . ContainsKey ( setting . Key ) )
148
160
{
149
- composite [ setting . Key ] = JsonConvert . SerializeObject ( setting . Value ) ;
161
+ composite [ setting . Key ] = serializer . Serialize ( setting . Value ) ;
150
162
}
151
163
else
152
164
{
153
- composite . Add ( setting . Key , JsonConvert . SerializeObject ( setting . Value ) ) ;
165
+ composite . Add ( setting . Key , serializer . Serialize ( setting . Value ) ) ;
154
166
}
155
167
}
156
168
}
@@ -159,7 +171,7 @@ public void Save<T>(string compositeKey, IDictionary<string, T> values)
159
171
ApplicationDataCompositeValue composite = new ApplicationDataCompositeValue ( ) ;
160
172
foreach ( KeyValuePair < string , T > setting in values )
161
173
{
162
- composite . Add ( setting . Key , JsonConvert . SerializeObject ( setting . Value ) ) ;
174
+ composite . Add ( setting . Key , serializer . Serialize ( setting . Value ) ) ;
163
175
}
164
176
165
177
Settings . Values [ compositeKey ] = composite ;
@@ -186,7 +198,7 @@ public Task<bool> FileExistsAsync(string filePath)
186
198
public async Task < T > ReadFileAsync < T > ( string filePath , T @default = default ( T ) )
187
199
{
188
200
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 ;
190
202
}
191
203
192
204
/// <summary>
@@ -199,7 +211,7 @@ public Task<bool> FileExistsAsync(string filePath)
199
211
/// <returns>The <see cref="StorageFile"/> where the object was saved</returns>
200
212
public Task < StorageFile > SaveFileAsync < T > ( string filePath , T value )
201
213
{
202
- return StorageFileHelper . WriteTextToFileAsync ( Folder , JsonConvert . SerializeObject ( value ) , filePath , CreationCollisionOption . ReplaceExisting ) ;
214
+ return StorageFileHelper . WriteTextToFileAsync ( Folder , serializer . Serialize ( value ) , filePath , CreationCollisionOption . ReplaceExisting ) ;
203
215
}
204
216
}
205
217
}
0 commit comments