Skip to content

feature: Enable choice between Newtonsoft.Json and System.Text.Json #832

Closed

Description

@Sergio0694

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.Json and switch to System.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 refit packages 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

  1. clairernovotny commented on Feb 7, 2020

    @clairernovotny
    Member

    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?

  2. Sergio0694 commented on Feb 7, 2020

    @Sergio0694
    ContributorAuthor

    Hey @onovotny - thanks for chiming in! 😊

    1. Regarding the move to 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 of refit based off of System.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 to System.Text.Json for the entire refit library 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.
    2. Regarding the organization/docs, I meant to say that eg. right now all the files are in a flat hierarchy in the project, with some files containing a number of different classes/interfaces, XML docs are often missing, and comments are generally sparse. Again, I want to make that clear, I wasn't criticizing anyone and I'm sure there were valid reasons why you and the other contributors didn't spend much time on this particular aspect of the codebase (which by the way, works great!). I was suggesting that if the decision had been made to start working on a refactoring to use 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 on Newtonsoft.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! 😄

  3. anaisbetts commented on Feb 7, 2020

    @anaisbetts
    Member

    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.

    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

  4. clairernovotny commented on Feb 7, 2020

    @clairernovotny
    Member

    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.

  5. Sergio0694 commented on Feb 7, 2020

    @Sergio0694
    ContributorAuthor

    @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 refit package with all the main logic and interfaces, and then a refit.Newtonsoft.Json and refit.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:

    Reopening this just in case, since adding this feature to refit seems more of a possibility now 🚀

  6. clairernovotny commented on Feb 8, 2020

    @clairernovotny
    Member

    Would you imagine keeping both references and just let the users choose which serialization backend to use

    That would seem to be the simplest solution. I don't think it's worth breaking things apart.

  7. Sergio0694 commented on Feb 8, 2020

    @Sergio0694
    ContributorAuthor

    Alright, sounds good!
    I'll start working on a new IContentSerializer backed by System.Text.Json then, I'll open a draft PR when I have a prototype and we can pick it up from there.
    Thanks! 😊

  8. Dreamescaper commented on Feb 10, 2020

    @Dreamescaper
    Contributor

    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.

  9. Sergio0694 commented on Feb 10, 2020

    @Sergio0694
    ContributorAuthor

    @Dreamescaper Yeah that'd be nice, though in order to do that, we'd need to remove both references from refit, only leave the generic IContentSerializer interface, and then provide two additional refit packages, each with a different IContentSerializer implementation users could use. They'd both reference the base refit package, 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 remaining Newtonsoft.Json strong references in the code (eg. for attributes), though that's already planned if we want to properly support System.Text.Json anyway.

    I'd be all for it, though @onovotny seemed to have expressed a strong preference for having a single refit package including both references, to keep things simpler, so you'd need to convince him 😄

  10. changed the title [-]feature: Switch from Newtonsoft.Json to System.Text.Json[/-] [+]feature: Enable choice between Newtonsoft.Json and System.Text.Json[/+] on Mar 23, 2020
  11. clairernovotny commented on Mar 23, 2020

    @clairernovotny
    Member

    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.

  12. locked and limited conversation to collaborators on Jun 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions