Skip to content

Commit 54a2080

Browse files
committed
Let BaseObjectStorageHelper use a custom serializer
1 parent 9fc3a62 commit 54a2080

File tree

5 files changed

+76
-13
lines changed

5 files changed

+76
-13
lines changed

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Collections.Generic;
77
using System.Reflection;
88
using System.Threading.Tasks;
9-
using Newtonsoft.Json;
109
using Windows.Storage;
1110

1211
namespace 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
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
namespace Microsoft.Toolkit.Uwp.Helpers
6+
{
7+
/// <summary>
8+
/// A basic serialization service.
9+
/// </summary>
10+
public interface IObjectSerializer
11+
{
12+
/// <summary>
13+
/// Serialize an object into a string.
14+
/// </summary>
15+
/// <typeparam name="T">The type of the object to serialize.</typeparam>
16+
/// <param name="value">The object to serialize.</param>
17+
/// <returns>The serialized object.</returns>
18+
string Serialize<T>(T value);
19+
20+
/// <summary>
21+
/// Deserialize a string into an object.
22+
/// </summary>
23+
/// <typeparam name="T">The type of the deserialized object.</typeparam>
24+
/// <param name="value">The string to deserialize.</param>
25+
/// <returns>The deserialized object.</returns>
26+
T Deserialize<T>(string value);
27+
}
28+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using Newtonsoft.Json;
6+
7+
namespace Microsoft.Toolkit.Uwp.Helpers
8+
{
9+
internal class JsonObjectSerializer : IObjectSerializer
10+
{
11+
public string Serialize<T>(T value) => JsonConvert.SerializeObject(value);
12+
13+
public T Deserialize<T>(string value) => JsonConvert.DeserializeObject<T>(value);
14+
}
15+
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ namespace Microsoft.Toolkit.Uwp.Helpers
1212
public class LocalObjectStorageHelper : BaseObjectStorageHelper
1313
{
1414
/// <summary>
15-
/// Initializes a new instance of the <see cref="LocalObjectStorageHelper"/> class.
15+
/// Initializes a new instance of the <see cref="LocalObjectStorageHelper"/> class,
16+
/// which can read and write data using the provided <see cref="IObjectSerializer"/>;
17+
/// if none is provided, a default Json serializer will be used.
1618
/// </summary>
17-
public LocalObjectStorageHelper()
19+
/// <param name="objectSerializer">The serializer to use.</param>
20+
public LocalObjectStorageHelper(IObjectSerializer objectSerializer = null)
21+
: base(objectSerializer)
1822
{
1923
Settings = ApplicationData.Current.LocalSettings;
2024
Folder = ApplicationData.Current.LocalFolder;

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ namespace Microsoft.Toolkit.Uwp.Helpers
1212
public class RoamingObjectStorageHelper : BaseObjectStorageHelper
1313
{
1414
/// <summary>
15-
/// Initializes a new instance of the <see cref="RoamingObjectStorageHelper"/> class.
15+
/// Initializes a new instance of the <see cref="RoamingObjectStorageHelper"/> class,
16+
/// which can read and write data using the provided <see cref="IObjectSerializer"/>;
17+
/// if none is provided, a default Json serializer will be used.
1618
/// </summary>
17-
public RoamingObjectStorageHelper()
19+
/// <param name="objectSerializer">The serializer to use.</param>
20+
public RoamingObjectStorageHelper(IObjectSerializer objectSerializer = null)
21+
: base(objectSerializer)
1822
{
1923
Settings = ApplicationData.Current.RoamingSettings;
2024
Folder = ApplicationData.Current.RoamingFolder;

0 commit comments

Comments
 (0)