Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,6 @@
<PackageReference Include="Microsoft.Xaml.Behaviors.Uwp.Managed">
<Version>2.0.1</Version>
</PackageReference>
<PackageReference Include="System.Text.Json">
<Version>4.7.2</Version>
</PackageReference>
<PackageReference Include="NotificationsVisualizerLibrary">
<Version>1.0.5</Version>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

<ItemGroup>
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
<PackageReference Include="System.Text.Json" Version="4.7.2" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<PackageReference Include="System.Text.Json" Version="4.7.2" />
<PackageReference Include="System.Text.Json" Version="4.7.2" />

</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using Windows.Storage;

Expand All @@ -20,7 +21,9 @@ public abstract class BaseObjectStorageHelper : IObjectStorageHelper
/// <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.
/// if none is provided, a default Json serializer will be used (based on <see cref="DataContractSerializer"/>).
/// In 6.1 and older the defualt Serizlizer was based on Newtownsoft Json and the new default Serizlizer may behave diffrenly.
/// To implement a <see cref="IObjectSerializer"/> based on Newtonsoft Json or System.Text.Json see https://aka.ms/wct/storagehelper-migration
/// </summary>
/// <param name="objectSerializer">The serializer to use.</param>
public BaseObjectStorageHelper(IObjectSerializer objectSerializer = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,28 @@
// 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.Text.Json;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;

namespace Microsoft.Toolkit.Uwp.Helpers
{
internal class JsonObjectSerializer : IObjectSerializer
{
public string Serialize<T>(T value) => JsonSerializer.Serialize(value);
public string Serialize<T>(T value)
{
using var sr = new MemoryStream();

public T Deserialize<T>(string value) => JsonSerializer.Deserialize<T>(value);
new DataContractJsonSerializer(typeof(T)).WriteObject(sr, value);
var json = sr.ToArray();
return Encoding.UTF8.GetString(json, 0, json.Length);
}

public T Deserialize<T>(string value)
{
using var ms = new MemoryStream(Encoding.UTF8.GetBytes(value));

return (T)new DataContractJsonSerializer(typeof(T)).ReadObject(ms);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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.Runtime.Serialization;
using Windows.Storage;

namespace Microsoft.Toolkit.Uwp.Helpers
Expand All @@ -14,7 +15,9 @@ public class LocalObjectStorageHelper : BaseObjectStorageHelper
/// <summary>
/// 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.
/// if none is provided, a default Json serializer will be used (based on <see cref="DataContractSerializer"/>).
/// In 6.1 and older the defualt Serizlizer was based on Newtownsoft Json and the new default Serizlizer may behave diffrenly.
/// To implement a <see cref="IObjectSerializer"/> based on Newtonsoft Json or System.Text.Json see https://aka.ms/wct/storagehelper-migration
/// </summary>
/// <param name="objectSerializer">The serializer to use.</param>
public LocalObjectStorageHelper(IObjectSerializer objectSerializer = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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.Runtime.Serialization;
using Windows.Storage;

namespace Microsoft.Toolkit.Uwp.Helpers
Expand All @@ -14,7 +15,9 @@ public class RoamingObjectStorageHelper : BaseObjectStorageHelper
/// <summary>
/// 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.
/// if none is provided, a default Json serializer will be used (based on <see cref="DataContractSerializer"/>).
/// In 6.1 and older the defualt Serizlizer was based on Newtownsoft Json and the new default Serizlizer may behave diffrenly.
/// To implement a <see cref="IObjectSerializer"/> based on Newtonsoft Json or System.Text.Json see https://aka.ms/wct/storagehelper-migration
/// </summary>
/// <param name="objectSerializer">The serializer to use.</param>
public RoamingObjectStorageHelper(IObjectSerializer objectSerializer = null)
Expand Down
2 changes: 0 additions & 2 deletions Microsoft.Toolkit.Uwp/Microsoft.Toolkit.Uwp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
</PropertyGroup>

<ItemGroup>

<PackageReference Include="System.Text.Json" Version="4.7.2" />

<ProjectReference Include="..\Microsoft.Toolkit\Microsoft.Toolkit.csproj" />

Expand Down
6 changes: 0 additions & 6 deletions Microsoft.Toolkit.Uwp/Properties/Microsoft.Toolkit.Uwp.rd.xml

This file was deleted.

34 changes: 32 additions & 2 deletions UnitTests/UnitTests.UWP/Helpers/Test_StorageHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public void Test_StorageHelper_LegacyIntTest()
Assert.AreEqual(input, output);
}

[Ignore]
[TestCategory("Helpers")]
[TestMethod]
public void Test_StorageHelper_LegacyDateTest()
Expand All @@ -53,19 +54,41 @@ public void Test_StorageHelper_LegacyDateTest()
Assert.AreEqual(input, output);
}

[Ignore]
[TestCategory("Helpers")]
[TestMethod]
public void Test_StorageHelper_LegacyPersonTest()
public void Test_StorageHelper_LegacyInternalClassTest()
{
string key = "Contact";

Person input = new Person() { Name = "Joe Bloggs", Age = 42 };
UI.Person input = new UI.Person() { Name = "Joe Bloggs", Age = 42 };

// simulate previous version by generating json and manually inserting it as string
string jsonInput = JsonSerializer.Serialize(input);

storageHelper.Save<string>(key, jsonInput);

// now read it as int to valid that the change works
UI.Person output = storageHelper.Read<UI.Person>(key, null);

Assert.IsNotNull(output);
Assert.AreEqual(input.Name, output.Name);
Assert.AreEqual(input.Age, output.Age);
}

[TestCategory("Helpers")]
[TestMethod]
public void Test_StorageHelper_LegacyPublicClassTest()
{
string key = "Contact";

UI.Person input = new UI.Person() { Name = "Joe Bloggs", Age = 42 };

// simulate previous version by generating json and manually inserting it as string
string jsonInput = JsonSerializer.Serialize(input);

storageHelper.Save(key, jsonInput);

// now read it as int to valid that the change works
Person output = storageHelper.Read<Person>(key, null);

Expand Down Expand Up @@ -123,5 +146,12 @@ public void Test_StorageHelper_NewPersonTest()
Assert.AreEqual(input.Name, output.Name);
Assert.AreEqual(input.Age, output.Age);
}

public class Person
{
public string Name { get; set; }

public int Age { get; set; }
}
}
}
3 changes: 0 additions & 3 deletions UnitTests/UnitTests.UWP/UnitTests.UWP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,6 @@
<PackageReference Include="System.Xml.XPath.XmlDocument">
<Version>4.3.0</Version>
</PackageReference>
<PackageReference Include="System.Text.Json">
<Version>4.7.2</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
<Compile Include="Converters\Test_AdaptiveHeightValueConverter.cs" />
Expand Down