Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,6 +15,19 @@ namespace Microsoft.Toolkit.Uwp.Helpers
/// </summary>
public abstract class BaseObjectStorageHelper : IObjectStorageHelper
{
private readonly IObjectSerializer serializer;

/// <summary>
/// Initializes a new instance of the <see cref="BaseObjectStorageHelper"/> class,
/// which can read and write data using the provided <see cref="IObjectSerializer"/>;
/// if none is provided, a default Json serializer will be used.
/// </summary>
/// <param name="objectSerializer">The serializer to use.</param>
public BaseObjectStorageHelper(IObjectSerializer objectSerializer = null)
{
serializer = objectSerializer ?? new JsonObjectSerializer();
}

/// <summary>
/// Gets or sets the settings container.
/// </summary>
Expand Down Expand Up @@ -78,7 +90,7 @@ public bool KeyExists(string compositeKey, string key)
return (T)Convert.ChangeType(value, type);
}

return JsonConvert.DeserializeObject<T>((string)value);
return serializer.Deserialize<T>((string)value);
}

/// <summary>
Expand All @@ -97,7 +109,7 @@ public bool KeyExists(string compositeKey, string key)
string value = (string)composite[key];
if (value != null)
{
return JsonConvert.DeserializeObject<T>(value);
return serializer.Deserialize<T>(value);
}
}

Expand All @@ -123,7 +135,7 @@ public void Save<T>(string key, T value)
}
else
{
Settings.Values[key] = JsonConvert.SerializeObject(value);
Settings.Values[key] = serializer.Serialize(value);
}
}

Expand All @@ -146,11 +158,11 @@ public void Save<T>(string compositeKey, IDictionary<string, T> 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));
}
}
}
Expand All @@ -159,7 +171,7 @@ public void Save<T>(string compositeKey, IDictionary<string, T> values)
ApplicationDataCompositeValue composite = new ApplicationDataCompositeValue();
foreach (KeyValuePair<string, T> setting in values)
{
composite.Add(setting.Key, JsonConvert.SerializeObject(setting.Value));
composite.Add(setting.Key, serializer.Serialize(setting.Value));
}

Settings.Values[compositeKey] = composite;
Expand All @@ -186,7 +198,7 @@ public Task<bool> FileExistsAsync(string filePath)
public async Task<T> ReadFileAsync<T>(string filePath, T @default = default(T))
{
string value = await StorageFileHelper.ReadTextFromFileAsync(Folder, filePath);
return (value != null) ? JsonConvert.DeserializeObject<T>(value) : @default;
return (value != null) ? serializer.Deserialize<T>(value) : @default;
}

/// <summary>
Expand All @@ -199,7 +211,7 @@ public Task<bool> FileExistsAsync(string filePath)
/// <returns>The <see cref="StorageFile"/> where the object was saved</returns>
public Task<StorageFile> SaveFileAsync<T>(string filePath, T value)
{
return StorageFileHelper.WriteTextToFileAsync(Folder, JsonConvert.SerializeObject(value), filePath, CreationCollisionOption.ReplaceExisting);
return StorageFileHelper.WriteTextToFileAsync(Folder, serializer.Serialize(value), filePath, CreationCollisionOption.ReplaceExisting);
}
}
}
28 changes: 28 additions & 0 deletions Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/IObjectSerializer.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// A basic serialization service.
/// </summary>
public interface IObjectSerializer
{
/// <summary>
/// Serialize an object into a string.
/// </summary>
/// <typeparam name="T">The type of the object to serialize.</typeparam>
/// <param name="value">The object to serialize.</param>
/// <returns>The serialized object.</returns>
string Serialize<T>(T value);

/// <summary>
/// Deserialize a string into an object.
/// </summary>
/// <typeparam name="T">The type of the deserialized object.</typeparam>
/// <param name="value">The string to deserialize.</param>
/// <returns>The deserialized object.</returns>
T Deserialize<T>(string value);
}
}
Original file line number Diff line number Diff line change
@@ -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>(T value) => JsonConvert.SerializeObject(value);

public T Deserialize<T>(string value) => JsonConvert.DeserializeObject<T>(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ namespace Microsoft.Toolkit.Uwp.Helpers
public class LocalObjectStorageHelper : BaseObjectStorageHelper
{
/// <summary>
/// Initializes a new instance of the <see cref="LocalObjectStorageHelper"/> class.
/// Initializes a new instance of the <see cref="LocalObjectStorageHelper"/> class,
/// which can read and write data using the provided <see cref="IObjectSerializer"/>;
/// if none is provided, a default Json serializer will be used.
/// </summary>
public LocalObjectStorageHelper()
/// <param name="objectSerializer">The serializer to use.</param>
public LocalObjectStorageHelper(IObjectSerializer objectSerializer = null)
: base(objectSerializer)
{
Settings = ApplicationData.Current.LocalSettings;
Folder = ApplicationData.Current.LocalFolder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ namespace Microsoft.Toolkit.Uwp.Helpers
public class RoamingObjectStorageHelper : BaseObjectStorageHelper
{
/// <summary>
/// Initializes a new instance of the <see cref="RoamingObjectStorageHelper"/> class.
/// Initializes a new instance of the <see cref="RoamingObjectStorageHelper"/> class,
/// which can read and write data using the provided <see cref="IObjectSerializer"/>;
/// if none is provided, a default Json serializer will be used.
/// </summary>
public RoamingObjectStorageHelper()
/// <param name="objectSerializer">The serializer to use.</param>
public RoamingObjectStorageHelper(IObjectSerializer objectSerializer = null)
: base(objectSerializer)
{
Settings = ApplicationData.Current.RoamingSettings;
Folder = ApplicationData.Current.RoamingFolder;
Expand Down