Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Context

In some of my class in an ASP.NET project serialize/deserialize JSON I suppose using the static JsonConvert... methods are not the best option, neither using new with a hardcoded class

Question

Would I inject something in the constructor? Do I have to define my custom simple interface for this purpose, or is there any convention/built in concept how to not hardcode types?

like image 849
g.pickardou Avatar asked May 20 '19 07:05

g.pickardou


People also ask

What does JSON serializer do?

The JSON Serialization feature converts objects to and from JSON format. This can be useful when interacting with web services, or just for packing and unpacking data to a text-based format easily.

Which one is correct class for JSON serializer?

The JsonSerializer is a static class in the System. Text. Json namespace. It provides functionality for serializing objects to a JSON string and deserializing from a JSON string to objects.

How do I deserialize text JSON?

A common way to deserialize JSON is to first create a class with properties and fields that represent one or more of the JSON properties. Then, to deserialize from a string or a file, call the JsonSerializer. Deserialize method.


2 Answers

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.

like image 78
poke Avatar answered Nov 15 '22 11:11

poke


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);
    }
}
like image 45
CodeCaster Avatar answered Nov 15 '22 12:11

CodeCaster