Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove System.Text.Json reference in Microsoft.Toolkit.Uwp package #3636

Closed
michael-hawker opened this issue Dec 17, 2020 · 2 comments · Fixed by #3637
Closed

Remove System.Text.Json reference in Microsoft.Toolkit.Uwp package #3636

michael-hawker opened this issue Dec 17, 2020 · 2 comments · Fixed by #3637

Comments

@michael-hawker
Copy link
Member

michael-hawker commented Dec 17, 2020

Describe the bug

We're only using this dependency (which is large) for a single helper in the package. We should streamline this with some docs instead of having a hard dependency. This also makes it easier for folks to use either Newtonsoft.Json or System.Text.Json depending on what their app is already using.

Add docs for:

using System.Text.Json;

namespace Microsoft.Toolkit.Uwp.Helpers
{
    public class JsonObjectSerializer : IObjectSerializer
    {
        public string Serialize<T>(T value) => JsonSerializer.Serialize(value);

        public T Deserialize<T>(string value) => JsonSerializer.Deserialize<T>(value);
    }
}
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);
    }
}

Instead of including explicitly. This will help with #3145 as most other higher-level toolkit libs are including the UI package which also includes this package.

FYI @vgromfeld @simop-msft

@michael-hawker
Copy link
Member Author

michael-hawker commented Dec 17, 2020

Snafu... the SystemInformation helpers use the LocalObjectStorageHelper which uses this...

Instead of completely requiring the developer to specify the serialization option, do we want to provide a base implementation which uses DataContractJsonSerializer instead? This would basically complete the original #889 issue.

At least for the SystemInformation class, I believe that would be simple enough in terms of data structure to be able to directly read-in any existing uses from apps.

⚠ Since 6.1 shipped with the Newtonsoft Serializer, for testing of the final change, we should make sure we try having a contract serializer with Newtonsoft initialize the SystemInformation storage on the app. Then change to the new serializer and ensure it can read-in all the data to continue from for app migrations.

For more complex app scenarios where devs have done more complex storage, the recommendation is that they provide their own storage helper (as noted above in the issue) for either Newtonsoft or System.Text.Json as they desire.

@ghost ghost added the In-PR 🚀 label Dec 17, 2020
@ghost ghost closed this as completed in #3637 Jan 6, 2021
@ghost ghost removed the In-PR 🚀 label Jan 6, 2021
ghost pushed a commit that referenced this issue Jan 6, 2021
…oolkit.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
@ghost ghost added the Completed 🔥 label Jan 6, 2021
@michael-hawker
Copy link
Member Author

Before:

Microsoft.Toolkit.Uwp Additional Footprint: 635,580 bytes
  App Diff: (.exe) = 4,096
  App Diff: (.xml) = 264
  App Diff: (.appxsym) = 40,009
  Size Diff: AppxBundleManifest.xml = 11
  Size Diff: AppxBlockMap.xml = 1,190
  Size Diff: AppxManifest.xml = 10
  Size Diff: resources.pri = 104
  Size Diff: System.Runtime.CompilerServices.Unsafe.dll = 1,744
  Additional: Microsoft.Bcl.AsyncInterfaces.dll = 21,064
  Additional: Microsoft.Toolkit.dll = 103,456
  Lib (self): Microsoft.Toolkit.Uwp.dll = 112,672
  Additional: System.Text.Encodings.Web.dll = 57,720
  Additional: System.Text.Json.dll = 293,240
-----------------------------------------

And After (Projected due to #3657):

Microsoft.Toolkit.Uwp Additional Footprint: 263,556 bytes
  App Diff: (.exe) = 4,096
  App Diff: (.xml) = 264
  App Diff: (.appxsym) = 40,009
  Size Diff: AppxBundleManifest.xml = 11
  Size Diff: AppxBlockMap.xml = 1,190
  Size Diff: AppxManifest.xml = 10
  Size Diff: resources.pri = 104
  Size Diff: System.Runtime.CompilerServices.Unsafe.dll = 1,744
  Additional: Microsoft.Toolkit.dll = 103,456
  Lib (self): Microsoft.Toolkit.Uwp.dll = 112,672
-----------------------------------------

Savings of: 372,024 🎉🎉🎉

And since most of our other higher-level packages reference this base package, we'll save this amount on those too!

@ghost ghost locked as resolved and limited conversation to collaborators Mar 7, 2021
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants