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

Replace Newtonsoft.Json with the new .Net JsonSerializer. #3414

Merged
merged 16 commits into from
Sep 16, 2020

Conversation

azchohfi
Copy link
Contributor

@azchohfi azchohfi commented Aug 7, 2020

Fixes #3060, #889, #2957

This completely removes Newtonsoft.Json in favor of the new DotNet JsonSerializer.
We need to test this thoroughly. If something was serialized with Newtonsoft.Json and deserialized with this, it needs to work.

PR Type

What kind of change does this PR introduce?

  • Feature
  • Refactoring (no functional changes, no api changes)

What is the current behavior?

We rely on Newtonsoft.Json for any Json serialization or deserialization.

What is the new behavior?

We depend on the new System.Text.Json package.

PR Checklist

Please check if your PR fulfills the following requirements:

  • Tested code with current supported SDKs
  • Pull Request has been submitted to the documentation repository instructions. Link:
  • Sample in sample app has been added / updated (for bug fixes / features)
  • 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 (lets test thoroughly, but I believe it should be fine).

@ghost
Copy link

ghost commented Aug 7, 2020

Thanks azchohfi for opening a Pull Request! The reviewers will test the PR and highlight if there is any conflict or changes required. If the PR is approved we will proceed to merge the pull request 🙌

@ghost ghost requested review from michael-hawker and Kyaa-dost August 7, 2020 01:16
@ghost ghost added the feature request 📬 A request for new changes to improve functionality label Aug 7, 2020
@azchohfi
Copy link
Contributor Author

azchohfi commented Aug 7, 2020

Since $type in JSON is not recommended, maybe its time we bump the version of the json to version 2, and create a new format that supports this better. @michael-hawker ? We could also support the same file version and just create a custom converter.

@azchohfi
Copy link
Contributor Author

azchohfi commented Aug 8, 2020

I fixed it with a few custom json converters.

@azchohfi azchohfi marked this pull request as ready for review August 8, 2020 00:28
Copy link
Member

@Sergio0694 Sergio0694 left a comment

Choose a reason for hiding this comment

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

Looks great, glad to see the toolkit switching to System.Text.Json 🚀🚀🚀
Just left a couple suggestions and questions, but overall I like this a lot!

@ghost
Copy link

ghost commented Aug 13, 2020

This PR has been marked as "needs attention 👋" and awaiting a response from the team.

@ghost
Copy link

ghost commented Aug 13, 2020

This PR has been marked as "needs attention 👋" and awaiting a response from the team.

Copy link
Member

@Sergio0694 Sergio0694 left a comment

Choose a reason for hiding this comment

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

Looks great! 🎉

Co-authored-by: Sergio Pedri <[email protected]>
@michael-hawker michael-hawker self-assigned this Sep 1, 2020
@michael-hawker michael-hawker added reviewers wanted ✋ for-review 📖 To evaluate and validate the Issues or PR labels Sep 1, 2020
Copy link
Contributor

@Rosuavio Rosuavio left a comment

Choose a reason for hiding this comment

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

I looks good, but I encountered an issue. An exception was thrown when I tried to "Import and Load" json generated from saving the canvas while on 1b08bd6.

I think we should try to support json generated from old version of the InfanteCanvas, can we fix this?

Attached is the json, the max view image, and the exception.
Infinite Canvas.zip

@azchohfi
Copy link
Contributor Author

Nice catch! I'll look it up today!

@azchohfi
Copy link
Contributor Author

Very simple fix. I'll just create a regression test to keep this stable.

@ghost
Copy link

ghost commented Sep 15, 2020

This PR has been marked as "needs attention 👋" and awaiting a response from the team.

Copy link
Contributor

@Rosuavio Rosuavio left a comment

Choose a reason for hiding this comment

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

Can't get it to crash with any old json (no matter how hard I scribble). Good work!

@michael-hawker
Copy link
Member

@azchohfi looks like the last bit is that the unit test needs some rd.xml or other fix to load:

Test method UnitTests.UI.Controls.Test_InfiniteCanvas_Regression.Test_InfiniteCanvas_LoadsV1File threw exception: 
System.TypeInitializationException: A type initializer threw an exception. To determine which type, inspect the InnerException's StackTrace property. ---> EETypeRva:0x000430B0(System.Reflection.MissingRuntimeArtifactException): Cannot retrieve a MethodInfo for this delegate because the method it targeted (System.Text.Json.ReflectionMemberAccessor.CreateStructPropertyGetter[TClass, TProperty](ReflectionMemberAccessor.GetPropertyByRef`2)) was not enabled for metadata using the Dynamic attribute. For more information, please visit http://go.microsoft.com/fwlink/?LinkID=616868

@ghost ghost removed the needs attention 👋 label Sep 15, 2020
Copy link
Member

@michael-hawker michael-hawker left a comment

Choose a reason for hiding this comment

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

Approving, as I think we're good once the CI is fixed.

@azchohfi I think this means the only thing we're using Json serialization for in the UWP library is the StorageHelper, eh? And then for Controls it's the Infinite Canvas?

@azchohfi
Copy link
Contributor Author

I think that is it, yes.

@Rosuavio Rosuavio merged commit 0e2d49f into master Sep 16, 2020
@delete-merged-branch delete-merged-branch bot deleted the removeNewtonsoftJson branch September 16, 2020 17:42
@michael-hawker michael-hawker added this to the 7.0 milestone Sep 16, 2020
ghost pushed a commit that referenced this pull request Jan 28, 2021
## Fixes #3692
This introduces a new light-weight serializer which we can use within the toolkit for the `SystemInformation` class still. And then developers with more complex type needs can specify their own serializer for the `BaseObjectStorageHelper` classes. See the related issue for more details.

Follow-on to #3637 and #3414

## 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?
Using `DataContractJsonSerializer`, though we had used `Newtonsoft.Json` in 6.1.

## What is the new behavior?
Use new `SystemSerializer` which passes values to/from the Windows `ApplicationDataContainer` APIs directly. Updates our internal usages of this to the new system. Requires developer using the `BaseObjectStorageHelper` to provide a serializer implementation.

This of course does provide a 'default'/system information within the toolkit now compared to our original thought of providing none at all before the `SystemInformation` problem was identified.

## PR Checklist

Please check if your PR fulfills the following requirements:

- [x] 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)
- [ ] New major technical changes in the toolkit have or will be added to the [Wiki](https://github.com/windows-toolkit/WindowsCommunityToolkit/wiki) e.g. build changes, source generators, testing infrastructure, sample creation changes, etc...
- [ ] 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
I tested copying the system.dat file from the Settings folder of our 6.1 sample app to the development version with the changes, no issues with `SystemInformation` reading the old data.

Updated/added unit tests to test the old json layer vs. reading from the new SystemSerializer as well as just testing a new `System.Text.Json` complex scenario. Added tests for exceptions.

TODO:
- [ ] Add more tests?
- [x] Run XAML Islands SystemInformation test
- [ ] Do we want `SystemSerializer` to handle a few more 'basic' (but non-primitive) types? E.g. @Sergio0694 has a [similar implementation for a different related system](https://github.com/Sergio0694/Brainf_ckSharp/blob/master/src/Brainf_ckSharp.Services.Uwp/SettingsService.cs)
- [ ] ???
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request 📬 A request for new changes to improve functionality for-review 📖 To evaluate and validate the Issues or PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature] Future - Use JsonSerializer with .NET 5
4 participants