Repository navigation
Description
Is your feature request related to a problem? Please describe.
Now that the new System.Text.Json APIs have been around for a while, and they're also available as a NuGet package, I was thinking it'd be nice to start looking into a way to possibly transition away from Newtonsoft.Json in refit, remove that dependency entirely and switch to the new System.Text.Json namespace. Given that refit currently targets .NET Standard 1.4, this would be a breaking change, since the new APIs require a minimum target of .NET Standard 2.0 instead.
This would make that a candidate for a new major version of refit, where breaking changes would be allowed. Removing the Newtonsoft.Json dependency and switching to System.Text.Json would make refit much more speed and memory efficient. It would also be an occasion for simplifying the project structure, by dropping some targets that are no longer compatible with .NET Standard 2.0. Users on older platforms could still keep using the previous major version of refit anyway.
Describe suggestions on how to achieve the feature
- Drop
Newtonsoft.Jsonand switch toSystem.Text.Json - Target .NET Standard 2.0 as minimum framework, drop older frameworks
Additional context
Also, regarding the library as a whole, the codebase is generally very unorganized and with a serious lack of documentation (compared to other .NET Foundation projects like ImageSharp). I'm not criticizing anyone here and I'm sure there were plenty of different reasons why the codebase ended up in this state. I'm thinking that this would be a good occasion to also do a major refactoring of the project, organizing classes with a proper hierarchy, adding missing XML docs, etc.
I'd be happy to contribute of course, but I figured it'd be a good idea to first open this issue to do some preliminary discussion on this feature as a whole, to see if it's actually feasible, what constraints there are, whether it'd be ok to drop all those older frameworks with a new major version, etc.
Paging @onovotny since I believe he's the main maintainer of the repo at the moment.
NOTE: the current
refitpackages wouldn't go anywhere and they would still work. Older packages targeting older frameworks could also still keep receiving automatic updates when a dependency is updated, if that was really necessary. This is why we have semantic versioning in the first place, and one of the reasons why it exists at all.
Activity
clairernovotny commentedon Feb 7, 2020
There are two things here:
code organization/docs
json library
The challenge with changing the json library is that Newtonsoft.Json is extremely flexible. While System.Text.Json may be faster/built-in, it's very likely that switching would inevitably break people. I'm fairly confident in saying that we can never move away from Newtonsoft.Json due to this. Might as well be a new library at that point.
For code organization/docs, we'd certainly be open to it if you have some concrete suggestions?
Sergio0694 commentedon Feb 7, 2020
Hey @onovotny - thanks for chiming in! 😊
System.Text.Json, yeah you make a very good point there, I was expecting something like that. I wonder how much interest would there be for a performance oriented version ofrefitbased off ofSystem.Text.Json(at the cost of some flexibility) and targeted at .NET Standard 2.0. I would definitely like to help if we were to decide to investigate such a thing. Or, since you've mentioned how an actual switch toSystem.Text.Jsonfor the entirerefitlibrary would probably be impossible anyway, and given than from your comment I take it that most of the users are probably fine with the current library anyway, I could just give it a try in a separate repo when I can fine the time (I see the license is MIT so that should be ok) and see how it goes from there.System.Text.Json, it would've been a good occasion to also put in the time to reorganize the code, add the missing XML docs, etc. If instead the plan is to leave the current library onNewtonsoft.Json, then it probably makes sense to just leave the codebase as is and keep doing maintenance as usual when necessary.Thank you for your feedbacks, I appreciate it! 😄
anaisbetts commentedon Feb 7, 2020
Please do, that sounds great! Though if you do end up publishing it please make a ID / Name / Description that doesn't look like an extension or plugin (i.e. "Refit.System.Json" would be a Bad example), having separate projects that appear to be owned by the original causes a lot of confusion
clairernovotny commentedon Feb 7, 2020
One option here could be to make the json serializer pluggable...it kinda already is. The default is Newtonsoft.Json, but people can opt-in to using System.Text.Json instead. That would enable people to use the higher-perf code paths and is completely within scope. I have no issue bumping Refit to netstandard 2.0 for this; there's nothing supported below that anyway.
Sergio0694 commentedon Feb 7, 2020
@onovotny Ah, that'd be great! 😄
Would you imagine keeping both references and just let the users choose which serialization backend to use, or would it be possible to make the library more modular, eg. with a core
refitpackage with all the main logic and interfaces, and then arefit.Newtonsoft.Jsonandrefit.System.Text.Json(example names) pair of packages the user could install, so that it would be possible not to install the unnecessary dependencies at all? That'd be even better.As I said, I'd be happy to contribute to this update once we decide if/when to move forward 😊
From what I'm seeing, these are the main files that would need to be changed:
RefitSettings, which contains theIContentSerializerproperty among othersProblemDetailsand in particular theExtensionspropertyJsonContentSerializerwhich would need anotherIContentSerializerimplementation, backed bySystem.Text.JsonFormValueMultimap.GetFieldNameForProperty, which has a hard dependency on theJsonPropertyAttributeReopening this just in case, since adding this feature to
refitseems more of a possibility now 🚀clairernovotny commentedon Feb 8, 2020
That would seem to be the simplest solution. I don't think it's worth breaking things apart.
Sergio0694 commentedon Feb 8, 2020
Alright, sounds good!
I'll start working on a new
IContentSerializerbacked bySystem.Text.Jsonthen, I'll open a draft PR when I have a prototype and we can pick it up from there.Thanks! 😊
Dreamescaper commentedon Feb 10, 2020
I'd like to add that ability to drop Newtonsoft.Json dependency is not only about performance concerns, but also size.
That matters when you use Refit with client-side Blazor, for example.
Sergio0694 commentedon Feb 10, 2020
@Dreamescaper Yeah that'd be nice, though in order to do that, we'd need to remove both references from
refit, only leave the genericIContentSerializerinterface, and then provide two additionalrefitpackages, each with a differentIContentSerializerimplementation users could use. They'd both reference the baserefitpackage, so you'd still only need to install a single package from your point of view. We'd also need some more minor tweaks to remove the remainingNewtonsoft.Jsonstrong references in the code (eg. for attributes), though that's already planned if we want to properly supportSystem.Text.Jsonanyway.I'd be all for it, though @onovotny seemed to have expressed a strong preference for having a single
refitpackage including both references, to keep things simpler, so you'd need to convince him 😄[-]feature: Switch from Newtonsoft.Json to System.Text.Json[/-][+]feature: Enable choice between Newtonsoft.Json and System.Text.Json[/+]clairernovotny commentedon Mar 23, 2020
Out of scope for this one but we could probably do some MSBuild magic and keep things in one package. Maybe open a different issue to track splitting out the serializer implementations?
Basically it could be an MSBuild property that gets set and all three dll's are packaged in the NuPkg. At build-time, the right one(s) are referenced by the project so that it all works.