Skip to content

Commit b4f1b2f

Browse files
author
msftbot[bot]
authored
Replace JsonSerializer with DataContractJsonSerializer in Microsoft.Toolkit.Uwp. (#3637)
<!-- 🚨 Please Do Not skip any instructions and information mentioned below as they are all required and essential to evaluate and test the PR. By fulfilling all the required information you will be able to reduce the volume of questions and most likely help merge the PR faster 🚨 --> <!-- 📝 It is preferred if you keep the "☑️ Allow edits by maintainers" checked in the Pull Request Template as it increases collaboration with the Toolkit maintainers by permitting commits to your PR branch (only) created from your fork. This can let us quickly make fixes for minor typos or forgotten StyleCop issues during review without needing to wait on you doing extra work. Let us help you help us! 🎉 --> ## Fixes #3636 <!-- Add the relevant issue number after the "#" mentioned above (for ex: Fixes #1234) which will automatically close the issue once the PR is merged. --> <!-- Add a brief overview here of the feature/bug & fix. --> Change implementation of JsonObjectSerializer to use DataContractJsonSerializer ## PR Type What kind of change does this PR introduce? <!-- Please uncomment one or more that apply to this PR. --> <!-- - Bugfix --> <!-- - Feature --> <!-- - Code style update (formatting) --> <!-- - Refactoring (no functional changes, no api changes) --> <!-- - Build or CI related changes --> <!-- - Documentation content changes --> <!-- - Sample app changes --> <!-- - Other... Please describe: --> ## What is the current behavior? <!-- Please describe the current behavior that you are modifying, or link to a relevant issue. --> Microsoft.Toolkit.Uwp uses JsonSerializer ## What is the new behavior? <!-- Describe how was this issue resolved or changed? --> Microsoft.Toolkit.Uwp uses DataContractJsonSerializer ## PR Checklist Please check if your PR fulfills the following requirements: - [ ] Tested code with current [supported SDKs](../readme.md#supported) - [ ] Pull Request has been submitted to the documentation repository [instructions](..\contributing.md#docs). Link: <!-- docs PR link --> - [ ] Sample in sample app has been added / updated (for bug fixes / features) - [ ] Icon has been created (if new sample) following the [Thumbnail Style Guide and templates](https://github.com/windows-toolkit/WindowsCommunityToolkit-design-assets) - [ ] Tests for the changes have been added (for bug fixes / features) (if applicable) - [ ] Header has been added to all new source files (run *build/UpdateHeaders.bat*) - [ ] Contains **NO** breaking changes <!-- If this PR contains a breaking change, please describe the impact and migration path for existing applications below. Please note that breaking changes are likely to be rejected within minor release cycles or held until major versions. --> ## Other information
2 parents 8e2b3af + bf475e9 commit b4f1b2f

File tree

10 files changed

+62
-22
lines changed

10 files changed

+62
-22
lines changed

Microsoft.Toolkit.Uwp.SampleApp/Microsoft.Toolkit.Uwp.SampleApp.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,6 @@
124124
<PackageReference Include="Microsoft.Xaml.Behaviors.Uwp.Managed">
125125
<Version>2.0.1</Version>
126126
</PackageReference>
127-
<PackageReference Include="System.Text.Json">
128-
<Version>4.7.2</Version>
129-
</PackageReference>
130127
<PackageReference Include="NotificationsVisualizerLibrary">
131128
<Version>1.0.5</Version>
132129
</PackageReference>

Microsoft.Toolkit.Uwp.UI.Controls/Microsoft.Toolkit.Uwp.UI.Controls.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747

4848
<ItemGroup>
4949
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
50+
<PackageReference Include="System.Text.Json" Version="4.7.2" />
5051
</ItemGroup>
5152

5253
<ItemGroup>

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.Collections.Generic;
77
using System.Reflection;
8+
using System.Runtime.Serialization;
89
using System.Threading.Tasks;
910
using Windows.Storage;
1011

@@ -20,7 +21,9 @@ public abstract class BaseObjectStorageHelper : IObjectStorageHelper
2021
/// <summary>
2122
/// Initializes a new instance of the <see cref="BaseObjectStorageHelper"/> class,
2223
/// 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+
/// if none is provided, a default Json serializer will be used (based on <see cref="DataContractSerializer"/>).
25+
/// In 6.1 and older the default Serializer was based on Newtonsoft.Json and the new default Serializer may behave differently.
26+
/// To implement a <see cref="IObjectSerializer"/> based on Newtonsoft.Json or System.Text.Json see https://aka.ms/wct/storagehelper-migration
2427
/// </summary>
2528
/// <param name="objectSerializer">The serializer to use.</param>
2629
public BaseObjectStorageHelper(IObjectSerializer objectSerializer = null)

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,28 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using System.Text.Json;
5+
using System.IO;
6+
using System.Runtime.Serialization.Json;
7+
using System.Text;
68

79
namespace Microsoft.Toolkit.Uwp.Helpers
810
{
911
internal class JsonObjectSerializer : IObjectSerializer
1012
{
11-
public string Serialize<T>(T value) => JsonSerializer.Serialize(value);
13+
public string Serialize<T>(T value)
14+
{
15+
using var sr = new MemoryStream();
1216

13-
public T Deserialize<T>(string value) => JsonSerializer.Deserialize<T>(value);
17+
new DataContractJsonSerializer(typeof(T)).WriteObject(sr, value);
18+
var json = sr.ToArray();
19+
return Encoding.UTF8.GetString(json, 0, json.Length);
20+
}
21+
22+
public T Deserialize<T>(string value)
23+
{
24+
using var ms = new MemoryStream(Encoding.UTF8.GetBytes(value));
25+
26+
return (T)new DataContractJsonSerializer(typeof(T)).ReadObject(ms);
27+
}
1428
}
1529
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System.Runtime.Serialization;
56
using Windows.Storage;
67

78
namespace Microsoft.Toolkit.Uwp.Helpers
@@ -14,7 +15,9 @@ public class LocalObjectStorageHelper : BaseObjectStorageHelper
1415
/// <summary>
1516
/// Initializes a new instance of the <see cref="LocalObjectStorageHelper"/> class,
1617
/// which can read and write data using the provided <see cref="IObjectSerializer"/>;
17-
/// if none is provided, a default Json serializer will be used.
18+
/// if none is provided, a default Json serializer will be used (based on <see cref="DataContractSerializer"/>).
19+
/// In 6.1 and older the default Serializer was based on Newtonsoft.Json and the new default Serializer may behave differently.
20+
/// To implement a <see cref="IObjectSerializer"/> based on Newtonsoft.Json or System.Text.Json see https://aka.ms/wct/storagehelper-migration
1821
/// </summary>
1922
/// <param name="objectSerializer">The serializer to use.</param>
2023
public LocalObjectStorageHelper(IObjectSerializer objectSerializer = null)

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System.Runtime.Serialization;
56
using Windows.Storage;
67

78
namespace Microsoft.Toolkit.Uwp.Helpers
@@ -14,7 +15,9 @@ public class RoamingObjectStorageHelper : BaseObjectStorageHelper
1415
/// <summary>
1516
/// Initializes a new instance of the <see cref="RoamingObjectStorageHelper"/> class,
1617
/// which can read and write data using the provided <see cref="IObjectSerializer"/>;
17-
/// if none is provided, a default Json serializer will be used.
18+
/// if none is provided, a default Json serializer will be used (based on <see cref="DataContractSerializer"/>).
19+
/// In 6.1 and older the default Serializer was based on Newtonsoft.Json and the new default Serializer may behave differently.
20+
/// To implement a <see cref="IObjectSerializer"/> based on Newtonsoft.Json or System.Text.Json see https://aka.ms/wct/storagehelper-migration
1821
/// </summary>
1922
/// <param name="objectSerializer">The serializer to use.</param>
2023
public RoamingObjectStorageHelper(IObjectSerializer objectSerializer = null)

Microsoft.Toolkit.Uwp/Microsoft.Toolkit.Uwp.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
13-
<PackageReference Include="System.Text.Json" Version="4.7.2" />
1412

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

Microsoft.Toolkit.Uwp/Properties/Microsoft.Toolkit.Uwp.rd.xml

Lines changed: 0 additions & 6 deletions
This file was deleted.

UnitTests/UnitTests.UWP/Helpers/Test_StorageHelper.cs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public void Test_StorageHelper_LegacyIntTest()
3434
Assert.AreEqual(input, output);
3535
}
3636

37+
[Ignore]
3738
[TestCategory("Helpers")]
3839
[TestMethod]
3940
public void Test_StorageHelper_LegacyDateTest()
@@ -53,19 +54,41 @@ public void Test_StorageHelper_LegacyDateTest()
5354
Assert.AreEqual(input, output);
5455
}
5556

57+
[Ignore]
5658
[TestCategory("Helpers")]
5759
[TestMethod]
58-
public void Test_StorageHelper_LegacyPersonTest()
60+
public void Test_StorageHelper_LegacyInternalClassTest()
5961
{
6062
string key = "Contact";
6163

62-
Person input = new Person() { Name = "Joe Bloggs", Age = 42 };
64+
UI.Person input = new UI.Person() { Name = "Joe Bloggs", Age = 42 };
6365

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

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

71+
// now read it as int to valid that the change works
72+
UI.Person output = storageHelper.Read<UI.Person>(key, null);
73+
74+
Assert.IsNotNull(output);
75+
Assert.AreEqual(input.Name, output.Name);
76+
Assert.AreEqual(input.Age, output.Age);
77+
}
78+
79+
[TestCategory("Helpers")]
80+
[TestMethod]
81+
public void Test_StorageHelper_LegacyPublicClassTest()
82+
{
83+
string key = "Contact";
84+
85+
UI.Person input = new UI.Person() { Name = "Joe Bloggs", Age = 42 };
86+
87+
// simulate previous version by generating json and manually inserting it as string
88+
string jsonInput = JsonSerializer.Serialize(input);
89+
90+
storageHelper.Save(key, jsonInput);
91+
6992
// now read it as int to valid that the change works
7093
Person output = storageHelper.Read<Person>(key, null);
7194

@@ -123,5 +146,12 @@ public void Test_StorageHelper_NewPersonTest()
123146
Assert.AreEqual(input.Name, output.Name);
124147
Assert.AreEqual(input.Age, output.Age);
125148
}
149+
150+
public class Person
151+
{
152+
public string Name { get; set; }
153+
154+
public int Age { get; set; }
155+
}
126156
}
127157
}

UnitTests/UnitTests.UWP/UnitTests.UWP.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,6 @@
122122
<PackageReference Include="System.Xml.XPath.XmlDocument">
123123
<Version>4.3.0</Version>
124124
</PackageReference>
125-
<PackageReference Include="System.Text.Json">
126-
<Version>4.7.2</Version>
127-
</PackageReference>
128125
</ItemGroup>
129126
<ItemGroup>
130127
<Compile Include="Converters\Test_AdaptiveHeightValueConverter.cs" />

0 commit comments

Comments
 (0)