Use JsonSerializer in AspNet Core API project?

After reading this it got me wondering if it is possible in a Net Core project (< v3) to swap out Json.Net and replace the JSON serializing and deserializing functionality with the ServiceStack JsonSerializer? Unfortunately we could not write the entire API with ServiceStack at the time.

The reasons why I ask are:

  • The way you have built your serializer and deserializers coercion of types is very good and beats every single other serializer I have ever worked with. Makes the developer’s lives a breeze as things “just work”.
  • Performance is better than Json.Net.
  • OrmLite uses the JsonSerializer and this layer works well and is trusted, would be good to see it on the web service layer as well.

For example, Utf8Json provides a library called Utf8Json.AspNetCoreMvcFormatter that can be used to swap out the default Json provider.

Some of my projects are going to be stuck for a while on older net core versions and by swapping in the ServiceStack library here could provide a nice boost.

This previous answer shows you can override the JSON Serializer with a custom implementation:

I’m not familiar with the capabilities/features the new JSON Serializer has, but you’re likely to run into issues if you’re using C# JSON Service Clients as you would be sending/receiving JSON between different implementations which can cause interoperability issues between their opinionated implmentation for sending different data types.

You could create your own JsonServiceClient which inherits from ServiceClientBase that uses their new JSON implementation, although for .NET Core you’re better off using JsonHttpClient but that doesn’t have an easy base class you can subclass and would need a modified copy that replaces all the places it uses JSON serialization.

Sorry Demis I think you misunderstood. I want to replace JSON.Net with your JsonSerializer in a standard aspnet core Web API project, not a ServiceStack project.

I believe you can register a custom MediaTypeFormatter that uses ServiceStack.Text.