From 6b901d8309b44ab809c3dd07a5ed5676c47d3cef Mon Sep 17 00:00:00 2001 From: Rosario Pulella Date: Tue, 26 Jan 2021 16:53:00 -0500 Subject: [PATCH 1/5] Remove JsonObjectSerilizer and have storage helpers requiere a serilizer --- .../ObjectStorage/BaseObjectStorageHelper.cs | 7 ++--- .../ObjectStorage/JsonObjectSerializer.cs | 29 ------------------- .../ObjectStorage/LocalObjectStorageHelper.cs | 5 ++-- .../RoamingObjectStorageHelper.cs | 5 ++-- 4 files changed, 7 insertions(+), 39 deletions(-) delete mode 100644 Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/JsonObjectSerializer.cs diff --git a/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/BaseObjectStorageHelper.cs b/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/BaseObjectStorageHelper.cs index bf724ac796e..d273c8b3d39 100644 --- a/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/BaseObjectStorageHelper.cs +++ b/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/BaseObjectStorageHelper.cs @@ -21,14 +21,13 @@ public abstract class BaseObjectStorageHelper : IObjectStorageHelper /// /// 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 (based on ). - /// In 6.1 and older the default Serializer was based on Newtonsoft.Json and the new default Serializer may behave differently. + /// In 6.1 and older the default Serializer was based on Newtonsoft.Json. /// To implement a based on Newtonsoft.Json or System.Text.Json see https://aka.ms/wct/storagehelper-migration /// /// The serializer to use. - public BaseObjectStorageHelper(IObjectSerializer objectSerializer = null) + public BaseObjectStorageHelper(IObjectSerializer objectSerializer) { - serializer = objectSerializer ?? new JsonObjectSerializer(); + serializer = objectSerializer ?? throw new ArgumentNullException(nameof(objectSerializer)); } /// diff --git a/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/JsonObjectSerializer.cs b/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/JsonObjectSerializer.cs deleted file mode 100644 index cb962714b2d..00000000000 --- a/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/JsonObjectSerializer.cs +++ /dev/null @@ -1,29 +0,0 @@ -// 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 System.IO; -using System.Runtime.Serialization.Json; -using System.Text; - -namespace Microsoft.Toolkit.Uwp.Helpers -{ - internal class JsonObjectSerializer : IObjectSerializer - { - public string Serialize(T value) - { - using var sr = new MemoryStream(); - - new DataContractJsonSerializer(typeof(T)).WriteObject(sr, value); - var json = sr.ToArray(); - return Encoding.UTF8.GetString(json, 0, json.Length); - } - - public T Deserialize(string value) - { - using var ms = new MemoryStream(Encoding.UTF8.GetBytes(value)); - - return (T)new DataContractJsonSerializer(typeof(T)).ReadObject(ms); - } - } -} diff --git a/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/LocalObjectStorageHelper.cs b/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/LocalObjectStorageHelper.cs index 0b717fb2d83..f56aada41da 100644 --- a/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/LocalObjectStorageHelper.cs +++ b/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/LocalObjectStorageHelper.cs @@ -15,12 +15,11 @@ public class LocalObjectStorageHelper : BaseObjectStorageHelper /// /// 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 (based on ). - /// In 6.1 and older the default Serializer was based on Newtonsoft.Json and the new default Serializer may behave differently. + /// In 6.1 and older the default Serializer was based on Newtonsoft.Json. /// To implement a based on Newtonsoft.Json or System.Text.Json see https://aka.ms/wct/storagehelper-migration /// /// The serializer to use. - public LocalObjectStorageHelper(IObjectSerializer objectSerializer = null) + public LocalObjectStorageHelper(IObjectSerializer objectSerializer) : base(objectSerializer) { Settings = ApplicationData.Current.LocalSettings; diff --git a/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/RoamingObjectStorageHelper.cs b/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/RoamingObjectStorageHelper.cs index e2d9ae9b2b6..d4580e4f34a 100644 --- a/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/RoamingObjectStorageHelper.cs +++ b/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/RoamingObjectStorageHelper.cs @@ -15,12 +15,11 @@ public class RoamingObjectStorageHelper : BaseObjectStorageHelper /// /// 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 (based on ). - /// In 6.1 and older the default Serializer was based on Newtonsoft.Json and the new default Serializer may behave differently. + /// In 6.1 and older the default Serializer was based on Newtonsoft.Json. /// To implement a based on Newtonsoft.Json or System.Text.Json see https://aka.ms/wct/storagehelper-migration /// /// The serializer to use. - public RoamingObjectStorageHelper(IObjectSerializer objectSerializer = null) + public RoamingObjectStorageHelper(IObjectSerializer objectSerializer) : base(objectSerializer) { Settings = ApplicationData.Current.RoamingSettings; From b664b23a83a36f2f80e3174417d495edb53d889b Mon Sep 17 00:00:00 2001 From: michael-hawker <24302614+michael-hawker@users.noreply.github.com> Date: Wed, 27 Jan 2021 16:24:04 -0800 Subject: [PATCH 2/5] Created a SystemSerializer to handle our primitive types within SystemInformation Tested copying data from 6.1 Sample App and was able to read/resume file writes with this new serializer. TODO: Update tests. --- .../Controls/SampleAppMarkdownRenderer.cs | 2 +- .../Models/Sample.cs | 2 +- .../Models/Samples.cs | 6 +-- .../Object Storage/ObjectStoragePage.xaml | 43 ++++++++++------- .../Object Storage/ObjectStoragePage.xaml.cs | 32 +++---------- .../ObjectStorage/BaseObjectStorageHelper.cs | 21 ++------ .../ObjectStorage/IObjectSerializer.cs | 8 ++-- .../ObjectStorage/LocalObjectStorageHelper.cs | 2 +- .../RoamingObjectStorageHelper.cs | 4 +- .../Helpers/ObjectStorage/SystemSerializer.cs | 48 +++++++++++++++++++ .../Helpers/SystemInformation.cs | 2 +- 11 files changed, 96 insertions(+), 74 deletions(-) create mode 100644 Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/SystemSerializer.cs diff --git a/Microsoft.Toolkit.Uwp.SampleApp/Controls/SampleAppMarkdownRenderer.cs b/Microsoft.Toolkit.Uwp.SampleApp/Controls/SampleAppMarkdownRenderer.cs index dc671f7c5a6..ecc9cb6cc2a 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/Controls/SampleAppMarkdownRenderer.cs +++ b/Microsoft.Toolkit.Uwp.SampleApp/Controls/SampleAppMarkdownRenderer.cs @@ -416,7 +416,7 @@ public string DesiredLang /// /// The Local Storage Helper. /// - private LocalObjectStorageHelper storage = new LocalObjectStorageHelper(); + private LocalObjectStorageHelper storage = new LocalObjectStorageHelper(new SystemSerializer()); /// /// DocFX note types and styling info, keyed by identifier. diff --git a/Microsoft.Toolkit.Uwp.SampleApp/Models/Sample.cs b/Microsoft.Toolkit.Uwp.SampleApp/Models/Sample.cs index 1db3fbd9e7e..157caa2114b 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/Models/Sample.cs +++ b/Microsoft.Toolkit.Uwp.SampleApp/Models/Sample.cs @@ -39,7 +39,7 @@ public class Sample public static async void EnsureCacheLatest() { - var settingsStorage = new LocalObjectStorageHelper(); + var settingsStorage = new LocalObjectStorageHelper(new SystemSerializer()); var onlineDocsSHA = await GetDocsSHA(); var cacheSHA = settingsStorage.Read(_cacheSHAKey); diff --git a/Microsoft.Toolkit.Uwp.SampleApp/Models/Samples.cs b/Microsoft.Toolkit.Uwp.SampleApp/Models/Samples.cs index 901bea050e8..0aa18896ad4 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/Models/Samples.cs +++ b/Microsoft.Toolkit.Uwp.SampleApp/Models/Samples.cs @@ -21,7 +21,7 @@ public static class Samples private static SemaphoreSlim _semaphore = new SemaphoreSlim(1); private static LinkedList _recentSamples; - private static RoamingObjectStorageHelper _roamingObjectStorageHelper = new RoamingObjectStorageHelper(); + private static LocalObjectStorageHelper _localObjectStorageHelper = new LocalObjectStorageHelper(new SystemSerializer()); public static async Task GetCategoryBySample(Sample sample) { @@ -98,7 +98,7 @@ public static async Task> GetRecentSamples() if (_recentSamples == null) { _recentSamples = new LinkedList(); - var savedSamples = _roamingObjectStorageHelper.Read(_recentSamplesStorageKey); + var savedSamples = _localObjectStorageHelper.Read(_recentSamplesStorageKey); if (savedSamples != null) { @@ -144,7 +144,7 @@ private static void SaveRecentSamples() } var str = string.Join(";", _recentSamples.Take(10).Select(s => s.Name).ToArray()); - _roamingObjectStorageHelper.Save(_recentSamplesStorageKey, str); + _localObjectStorageHelper.Save(_recentSamplesStorageKey, str); } } } diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Object Storage/ObjectStoragePage.xaml b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Object Storage/ObjectStoragePage.xaml index e782345850d..3fc3f747d91 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Object Storage/ObjectStoragePage.xaml +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Object Storage/ObjectStoragePage.xaml @@ -1,26 +1,33 @@ - + - + - + - - - -