-
Notifications
You must be signed in to change notification settings - Fork 325
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 System.Text.Json #2488
Comments
We would love to do this, but we need to keep our dependencies compatible with net452 and System.Text.Json is not compatible with that. |
It's actually much worse, the dependency on I was trying to replace It turns out it was referenced through But then… this happened at runtime:
This is because Microsoft.TestPlatform.TranslationLayer is lying about its dependencies. Its JsonDataSerializer class depends on Q.E.D. |
Instead, use System.Text.Json everywhere. ### JSON Configuration File Since there's [no equivalent][1] to `MissingMemberHandling` with `System.Text.Json`, the error messages in case of erroneous keys in the JSON condiguration file have been improved. For example, if you have a typo in the `enabled` key you would get this error: > The allowed keys for the "stryker-config.since" object are { "enabled", "ignore-changes-in", "target" } but "enable" was found in the config file at "[…]/tests/stryker-config.json" Those better error messages also required to have a `JsonPropertyName` attribute on _all_ the properties. This is probably a good thing anyway since it makes the code more _greppable_. ### Newtonsoft.Json transitive dependency Unfortunately, `Newtonsoft.Json` is still there transitively (through Buildalyzer/3.2.2 → Microsoft.Extensions.DependencyModel/2.1.0 → Newtonsoft.Json/9.0.1) but there's nothing we can do about it for now, see microsoft/vstest#2488 (comment) for a full explanation. [1]: https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to#missingmemberhandling
Instead, use System.Text.Json everywhere. ### JSON Configuration File Since there's [no equivalent][1] to `MissingMemberHandling` with `System.Text.Json`, the error messages in case of erroneous keys in the JSON configuration file have been improved. For example, if you have a typo in the `enabled` key you would get this error: > The allowed keys for the "stryker-config.since" object are { "enabled", "ignore-changes-in", "target" } but "enable" was found in the config file at "[…]/tests/stryker-config.json" Those better error messages also required to have a `JsonPropertyName` attribute on _all_ the properties. This is probably a good thing anyway since it makes the code more _greppable_. ### Newtonsoft.Json transitive dependency Unfortunately, `Newtonsoft.Json` is still there transitively (through Buildalyzer/3.2.2 → Microsoft.Extensions.DependencyModel/2.1.0 → Newtonsoft.Json/9.0.1) but there's nothing we can do about it for now, see microsoft/vstest#2488 (comment) for a full explanation. [1]: https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to#missingmemberhandling
…ig keys (#1707) Instead, use System.Text.Json everywhere. ### JSON Configuration File Since there's [no equivalent][1] to `MissingMemberHandling` with `System.Text.Json`, the error messages in case of erroneous keys in the JSON configuration file have been improved. For example, if you have a typo in the `enabled` key you would get this error: > The allowed keys for the "stryker-config.since" object are { "enabled", "ignore-changes-in", "target" } but "enable" was found in the config file at "[…]/tests/stryker-config.json" Those better error messages also required to have a `JsonPropertyName` attribute on _all_ the properties. This is probably a good thing anyway since it makes the code more _greppable_. ### Newtonsoft.Json transitive dependency Unfortunately, `Newtonsoft.Json` is still there transitively (through Buildalyzer/3.2.2 → Microsoft.Extensions.DependencyModel/2.1.0 → Newtonsoft.Json/9.0.1) but there's nothing we can do about it for now, see microsoft/vstest#2488 (comment) for a full explanation. [1]: https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to#missingmemberhandling Co-authored-by: Rouke Broersma <mobrockers@gmail.com>
almost 2 years later. perhaps it is time to drop support for net452? support is ending in 2 months anyway |
I was just doing some refactoring of the base file new UWP Unit Test project to coordinate the multiple copies we have across our project to a single set of code for our custom test harness. Doing this suddenly broke the UWP app from connecting to the vstest.console or VS runner. Took me a day, but finally saw a Added a reference to Not sure exactly why what I did broke the dependency look up, but it seems we're using the |
#2316 (comment) we are still researching how to get rid of that dependency altogether, unfortunately System.Text.Json does not support the data annotations we use, and we don't want to add additional dependency to object model for .NET Framework. |
@nohwnd can u point to where/how u use data annotations? |
@SimonCropp Sure, for example here in ObjectModel which has no dependency on the serializer we have the TestCase object, and it is annotated with DataMember attribute. Theoretically we could work around this on the serializer / deserializer side by explicitly listing the properties to use for every object. Another issue with System.Text.Json is that it is not built-in in .NET Framework, and serializers are missing for common types that we use. |
|
@michael-hawker One of the reasons is that we get complaints about newtonsoft.json being required to test code that is not relying on it. We keep using very old version of the library to avoid upgrading the tested code that needs it. But if the tested code does not need it, we would ideally not bring it in. Or any other dependency that is not present in the targetted runtime. |
Any progress on this? |
Wouldn't using |
perhaps u could alias newtonsoft https://github.com/getsentry/dotnet-assembly-alias/ and use the internal option |
|
Any status update? |
There is no update, the reasons in #2488 (comment) are still true, and why we don't work on replacing newtonsoft.json. |
Data annotation is a bad pattern because the tight coupling. can be solved in other ways, like fluent mapping. If you want to keep supporting newtonsoft for full framework you could use compile flags that let you compile newtonsoft specific code when building for .NET Fullframework 4.8 |
Can you be more specific why this is a bad practice? From what I see in our code we use a in-built attribute that is general purpose for data annotation, and newtonsoft.json is picking that up. So to me that is the opposite of coupling. While if we use fluent mapping we either need to let a "serializer" library know about all the shapes of all our messages. Or we need to add references to serializer to the dll that has our messages.
Yes that is a way forward, but at that point we can also just move to STJ fully and maintain it in a single way. But we don't because the newtonsoft signature is part of our public api and we don't have good opportunity to break when it works. |
Datannotation are bad practice because it favours high coupling. |
that wont help. nunit still requires Microsoft.NET.Test.Sdk to work. and that refs Microsoft.TestPlatform.TestHost which refs Newtonsoft.Json |
This is what code reviews are for. |
How is the debate about attributes & tight coupling helping to move us closer to a solution? If it's not, we should limit discussion to on-topic updates. I'd prefer to remain subscribed to replies so that I can keep up with meaningful conversation on the topic. Asking for status updates and debating attributes are not what I consider meaningful. The issue is in open status and repository maintainers are aware of it. Let's patiently wait on a solution, or if you're impatient, I'm sure they would appreciate a pull request. |
Would an approach based on STJ contract customization be considered acceptable? (see dotnet/runtime#29975 (comment) ) |
I know this will be unpopular but we would have to break the public API to do this, and we still would have to ship STJ for .NET Framework, so this would require significant changes to vstest. For this reasion it won't be implemented, we are focusing on adding new features to Testing.Platform instead, where we avoided this problem since the beginning. https://aka.ms/testingplatform |
(This was discussed in #2316 but deserves to be its own issue, as the work necessary to close this issue would be different to the work to close that one.)
I'm tired of typing
JsonSerializer
in my test projects and getting it autocompleted toNewtonsoft.Json.JsonSerializer
becauseMicrosoft.Net.Test.Sdk
(viaMicrosoft.TestPlatform.TestHost
) references (a very old version) of the now-essentially-deprecatedNewtonsoft.Json
, especially when my greenfields .NET Core app under test has zero references toNewtonsoft.Json
.This would also help people who do have explicit dependencies on
Newtonsoft.Json
and are being bitten by #2279.The text was updated successfully, but these errors were encountered: