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

Transitioning to System.Text.Json from Newtonsoft.Json #7021

Open
volkanceylan opened this issue Nov 18, 2023 · 1 comment
Open

Transitioning to System.Text.Json from Newtonsoft.Json #7021

volkanceylan opened this issue Nov 18, 2023 · 1 comment

Comments

@volkanceylan
Copy link
Member

volkanceylan commented Nov 18, 2023

Transitioning to System.Text.Json from Newtonsoft.Json in .NET

We are migrating to System.Text.Json from Newtonsoft.Json, as it is the recommended and more performant library in .NET.

Please note that the transition occured in 8.0.1 version.

Compatibility and Workarounds

There exist several compatibility issues between Newtonsoft and System.Text.Json. Check the following link for detailed insights: Migrate from Newtonsoft to System.Text.Json.

To ensure compatibility, we've implemented various workarounds such as using custom converters, specific serializer settings, etc. In versions before .NET 8, handling missing members by raising an exception was not feasible.

Update Requirements for Users

After the transition, users must add attributes specific to System.Text.Json to properties and classes that previously had Newtonsoft-specific attributes. For instance:

  • Replace Newtonsoft.Json.JsonProperty with System.Text.Json.Serialization.JsonPropertyName.
  • Modify converters and usage of attributes like JsonIgnore, among others.

An example includes the UserListRequest DataProtector property, which previously had a JsonIgnore attribute specific to Newtonsoft.

You may choose to keep Newtonsoft specific attributes while adding their System.Text.Json counterparts to keep compatibility and still use Newtonsoft.Json in some parts of your application.

Transition Guidelines

The converter classes unique to System.Text.Json reside in the Serenity.JsonConverters namespace and end with JsonConverter. For example:

  • The former converter specific to Newtonsoft.Json for rows was JsonRowConverter.
  • The new one for System.Text.Json is Serenity.JsonConverters.RowJsonConverter.

When using [JsonConverter(typeof(...))] attribute, users should exercise caution to specify the correct type, as the attribute name remains the same for both libraries.

To transition, users need to:

  • Remove the .AddNewtonsoftJson block in Startup.cs.
  • Replace it with the following line to configure serialization settings similar to the previous Serenity Newtonsoft.Json-based settings:
    services.Configure<JsonOptions>(options => JSON.Defaults.Populate(options.JsonSerializerOptions));
  • Additionally, remove this package reference from the project file:
    <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.0" />

Behavior Changes

Unlike Newtonsoft.Json, IncludeNulls is replaced with WriteNulls as we will only ignore nulls during serialization. The parameter names are also changed to writeNulls to align with this behavior.

System.Text.Json will be used for JSON.Stringify methods, and there won't be any means to use Newtonsoft other than manually calling Newtonsoft.Json.JsonConvert, etc.

Retaining Newtonsoft.Json Reference

We have not yet removed the Newtonsoft.Json reference as it is necessary for referencing serialization attributes, ensuring compatibility. While we may consider its removal in the future, it won't happen immediately.

@volkanceylan volkanceylan pinned this issue Nov 18, 2023
@volkanceylan volkanceylan changed the title Switching to System.Text.JSON from Newtonsoft.JSON Transitioning to System.Text.Json from Newtonsoft.Json Nov 18, 2023
@serenity-is serenity-is locked as off-topic and limited conversation to collaborators Nov 21, 2023
@volkanceylan
Copy link
Member Author

Also note that System.Text.Json does not support field members, e.g. ones without get set. If you have any non property in your custom types convert them to properties.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant