Skip to content

Commit a1e2126

Browse files
committed
Let BaseObjectStorageHelper use custom JsonSerializerOptions
1 parent abf77ed commit a1e2126

File tree

3 files changed

+62
-13
lines changed

3 files changed

+62
-13
lines changed

Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/BaseObjectStorageHelper.cs

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/LocalObjectStorageHelper.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using Newtonsoft.Json;
56
using Windows.Storage;
67

78
namespace Microsoft.Toolkit.Uwp.Helpers
@@ -12,12 +13,24 @@ namespace Microsoft.Toolkit.Uwp.Helpers
1213
public class LocalObjectStorageHelper : BaseObjectStorageHelper
1314
{
1415
/// <summary>
15-
/// Initializes a new instance of the <see cref="LocalObjectStorageHelper"/> class.
16+
/// Initializes a new instance of the <see cref="LocalObjectStorageHelper"/> class,
17+
/// which can read and write serialized data using the provided <see cref="JsonSerializerSettings"/>.
1618
/// </summary>
17-
public LocalObjectStorageHelper()
19+
/// <param name="serializerSettings">The serializer settings to use.</param>
20+
public LocalObjectStorageHelper(JsonSerializerSettings serializerSettings)
21+
: base(serializerSettings)
1822
{
1923
Settings = ApplicationData.Current.LocalSettings;
2024
Folder = ApplicationData.Current.LocalFolder;
2125
}
26+
27+
/// <summary>
28+
/// Initializes a new instance of the <see cref="LocalObjectStorageHelper"/> class,
29+
/// which can read and write serialized data using the default <see cref="JsonSerializerSettings"/>.
30+
/// </summary>
31+
public LocalObjectStorageHelper()
32+
: this(new JsonSerializerSettings())
33+
{
34+
}
2235
}
2336
}

Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/RoamingObjectStorageHelper.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using Newtonsoft.Json;
56
using Windows.Storage;
67

78
namespace Microsoft.Toolkit.Uwp.Helpers
@@ -12,12 +13,24 @@ namespace Microsoft.Toolkit.Uwp.Helpers
1213
public class RoamingObjectStorageHelper : BaseObjectStorageHelper
1314
{
1415
/// <summary>
15-
/// Initializes a new instance of the <see cref="RoamingObjectStorageHelper"/> class.
16+
/// Initializes a new instance of the <see cref="RoamingObjectStorageHelper"/> class,
17+
/// which can read and write serialized data using the provided <see cref="JsonSerializerSettings"/>.
1618
/// </summary>
17-
public RoamingObjectStorageHelper()
19+
/// <param name="serializerSettings">The serializer settings to use.</param>
20+
public RoamingObjectStorageHelper(JsonSerializerSettings serializerSettings)
21+
: base(serializerSettings)
1822
{
1923
Settings = ApplicationData.Current.RoamingSettings;
2024
Folder = ApplicationData.Current.RoamingFolder;
2125
}
26+
27+
/// <summary>
28+
/// Initializes a new instance of the <see cref="RoamingObjectStorageHelper"/> class,
29+
/// which can read and write serialized data using the default <see cref="JsonSerializerSettings"/>.
30+
/// </summary>
31+
public RoamingObjectStorageHelper()
32+
: this(new JsonSerializerSettings())
33+
{
34+
}
2235
}
2336
}

0 commit comments

Comments
 (0)