Is there any convention or built in concept how to inject a Json serializer?

This isn't JSON serialization specific. As with every static class, you can't apply an interface to it (let alone a third-party class). See for example Dependency injection with a static logger, static helper class.

So you'll need to write your own wrapper class and inject that:

public interface IJsonSerializer
{
    string SerializeObject(object o);
    T DeserializeObject<T>(string json);
}

public class NewtonsoftJsonSerializer : IJsonSerializer
{
    public string SerializeObject(object o)
    {
        return Newtonsoft.Json.JsonConvert.SerializeObject(o);
    }

    public T DeserializeObject<T>(string json)
    {
        return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json);
    }
}

If it helps, ASP.NET Core is abstracting the JSON serializer itself in some places. For example, within Razor views (both views and pages), you can use Json.Serialize() to serialize stuff into the body. This uses the IJsonHelper to provide an abstracted access to the serialization, while using all the pre-configured settings from the serializer (ensuring a consistent output).

In 2.2, the underlying JsonHelper uses the JsonOutputFormatter to actually provide access to the serializer. Both the IJsonHelper and the JsonOutputFormatter are available through dependency injection, so you can inject those anywhere if you need them.

In 3.0, the team is removing the direct dependency on Newtonsoft.Json and introduces a proper abstraction themselves. The IJsonHelper is still around though. By default, Newtonsoft.Json will not around though, so if you depend on that (e.g. because you are using it statically), then you will have to add a dependency yourself (and maybe switch ASP.NET Core back to use it too).

When you want to deserialize, the IJsonHelper will not help you, and there is no component around that will give you direct access to the deserializer. In those cases, you can always create a JsonSerializer yourself. You can get the serializer settings from DI:

IOptions<MvcJsonOptions> mvcJsonOptions // get through DI

var serializer = JsonSerializer.Create(mvcJsonOptions.Options.SerializerSettings);

That uses the globally configured serialization settings from the framework.

Usually, just using the JsonConvert static would be fine too. In general though, you sould try to avoid having to serialize and deserialize stuff yourself. The framework will already take care of that for you in various places where data gets in or out. So depending on your use case, there might already be an automatism to convert between JSON.


Personally, I wouldn't really bother about abstracting a service such as a JSON serializer. Not all statics are wrong and not everything has to be abstracted in a service, except if it's highly expected you want to swap out implementations or create mocks for testing. Otherwise I'd suggest to focus on the business logic of your application and create abstractions only if they're necessary and provide value for the application's design and testability.