diff --git a/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/BaseObjectStorageHelper.cs b/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/BaseObjectStorageHelper.cs
index c3abcc49fbc..73311acfc88 100644
--- a/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/BaseObjectStorageHelper.cs
+++ b/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/BaseObjectStorageHelper.cs
@@ -6,7 +6,6 @@
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
-using Newtonsoft.Json;
using Windows.Storage;
namespace Microsoft.Toolkit.Uwp.Helpers
@@ -16,6 +15,19 @@ namespace Microsoft.Toolkit.Uwp.Helpers
///
public abstract class BaseObjectStorageHelper : IObjectStorageHelper
{
+ private readonly IObjectSerializer serializer;
+
+ ///
+ /// Initializes a new instance of the class,
+ /// which can read and write data using the provided ;
+ /// if none is provided, a default Json serializer will be used.
+ ///
+ /// The serializer to use.
+ public BaseObjectStorageHelper(IObjectSerializer objectSerializer = null)
+ {
+ serializer = objectSerializer ?? new JsonObjectSerializer();
+ }
+
///
/// Gets or sets the settings container.
///
@@ -78,7 +90,7 @@ public bool KeyExists(string compositeKey, string key)
return (T)Convert.ChangeType(value, type);
}
- return JsonConvert.DeserializeObject((string)value);
+ return serializer.Deserialize((string)value);
}
///
@@ -97,7 +109,7 @@ public bool KeyExists(string compositeKey, string key)
string value = (string)composite[key];
if (value != null)
{
- return JsonConvert.DeserializeObject(value);
+ return serializer.Deserialize(value);
}
}
@@ -123,7 +135,7 @@ public void Save(string key, T value)
}
else
{
- Settings.Values[key] = JsonConvert.SerializeObject(value);
+ Settings.Values[key] = serializer.Serialize(value);
}
}
@@ -146,11 +158,11 @@ public void Save(string compositeKey, IDictionary values)
{
if (composite.ContainsKey(setting.Key))
{
- composite[setting.Key] = JsonConvert.SerializeObject(setting.Value);
+ composite[setting.Key] = serializer.Serialize(setting.Value);
}
else
{
- composite.Add(setting.Key, JsonConvert.SerializeObject(setting.Value));
+ composite.Add(setting.Key, serializer.Serialize(setting.Value));
}
}
}
@@ -159,7 +171,7 @@ public void Save(string compositeKey, IDictionary values)
ApplicationDataCompositeValue composite = new ApplicationDataCompositeValue();
foreach (KeyValuePair setting in values)
{
- composite.Add(setting.Key, JsonConvert.SerializeObject(setting.Value));
+ composite.Add(setting.Key, serializer.Serialize(setting.Value));
}
Settings.Values[compositeKey] = composite;
@@ -186,7 +198,7 @@ public Task FileExistsAsync(string filePath)
public async Task ReadFileAsync(string filePath, T @default = default(T))
{
string value = await StorageFileHelper.ReadTextFromFileAsync(Folder, filePath);
- return (value != null) ? JsonConvert.DeserializeObject(value) : @default;
+ return (value != null) ? serializer.Deserialize(value) : @default;
}
///
@@ -199,7 +211,7 @@ public Task FileExistsAsync(string filePath)
/// The where the object was saved
public Task SaveFileAsync(string filePath, T value)
{
- return StorageFileHelper.WriteTextToFileAsync(Folder, JsonConvert.SerializeObject(value), filePath, CreationCollisionOption.ReplaceExisting);
+ return StorageFileHelper.WriteTextToFileAsync(Folder, serializer.Serialize(value), filePath, CreationCollisionOption.ReplaceExisting);
}
}
}
diff --git a/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/IObjectSerializer.cs b/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/IObjectSerializer.cs
new file mode 100644
index 00000000000..3dbad93431e
--- /dev/null
+++ b/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/IObjectSerializer.cs
@@ -0,0 +1,28 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+namespace Microsoft.Toolkit.Uwp.Helpers
+{
+ ///
+ /// A basic serialization service.
+ ///
+ public interface IObjectSerializer
+ {
+ ///
+ /// Serialize an object into a string.
+ ///
+ /// The type of the object to serialize.
+ /// The object to serialize.
+ /// The serialized object.
+ string Serialize(T value);
+
+ ///
+ /// Deserialize a string into an object.
+ ///
+ /// The type of the deserialized object.
+ /// The string to deserialize.
+ /// The deserialized object.
+ T Deserialize(string value);
+ }
+}
diff --git a/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/JsonObjectSerializer.cs b/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/JsonObjectSerializer.cs
new file mode 100644
index 00000000000..8ce982ddfc0
--- /dev/null
+++ b/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/JsonObjectSerializer.cs
@@ -0,0 +1,15 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using Newtonsoft.Json;
+
+namespace Microsoft.Toolkit.Uwp.Helpers
+{
+ internal class JsonObjectSerializer : IObjectSerializer
+ {
+ public string Serialize(T value) => JsonConvert.SerializeObject(value);
+
+ public T Deserialize(string value) => JsonConvert.DeserializeObject(value);
+ }
+}
diff --git a/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/LocalObjectStorageHelper.cs b/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/LocalObjectStorageHelper.cs
index 9be235382c5..186f4ffe019 100644
--- a/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/LocalObjectStorageHelper.cs
+++ b/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/LocalObjectStorageHelper.cs
@@ -12,9 +12,13 @@ namespace Microsoft.Toolkit.Uwp.Helpers
public class LocalObjectStorageHelper : BaseObjectStorageHelper
{
///
- /// Initializes a new instance of the class.
+ /// Initializes a new instance of the class,
+ /// which can read and write data using the provided ;
+ /// if none is provided, a default Json serializer will be used.
///
- public LocalObjectStorageHelper()
+ /// The serializer to use.
+ public LocalObjectStorageHelper(IObjectSerializer objectSerializer = null)
+ : base(objectSerializer)
{
Settings = ApplicationData.Current.LocalSettings;
Folder = ApplicationData.Current.LocalFolder;
diff --git a/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/RoamingObjectStorageHelper.cs b/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/RoamingObjectStorageHelper.cs
index 62c9e5e832b..18bd7d244ec 100644
--- a/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/RoamingObjectStorageHelper.cs
+++ b/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/RoamingObjectStorageHelper.cs
@@ -12,9 +12,13 @@ namespace Microsoft.Toolkit.Uwp.Helpers
public class RoamingObjectStorageHelper : BaseObjectStorageHelper
{
///
- /// Initializes a new instance of the class.
+ /// Initializes a new instance of the class,
+ /// which can read and write data using the provided ;
+ /// if none is provided, a default Json serializer will be used.
///
- public RoamingObjectStorageHelper()
+ /// The serializer to use.
+ public RoamingObjectStorageHelper(IObjectSerializer objectSerializer = null)
+ : base(objectSerializer)
{
Settings = ApplicationData.Current.RoamingSettings;
Folder = ApplicationData.Current.RoamingFolder;