serenity-is / Serenity

Business Apps Made Simple with Asp.Net Core MVC / TypeScript
https://serenity.is
MIT License
2.57k stars 796 forks source link

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

Open volkanceylan opened 7 months ago

volkanceylan commented 7 months ago

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:

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:

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:

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 commented 7 months ago

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.