Alan2
Alan2

Reputation: 24562

Is there a way I can set the settings of a json serializer when it is constructed?

In my app every time the JsonSerializer is used this always appears first.

JsonSerializerSettings jsonSerializerSettings = Helpers.JsonSerializer.EscapeHtmlSerializer();

for example:

    public void UpdateD(D1 d)
    {
        JsonSerializerSettings jsonSerializerSettings = Helpers.JsonSerializer.EscapeHtmlSerializer();
        ...
        var serializeJson = JsonConvert.SerializeObject(dconf, jsonSerializerSettings); ;
        ...
    }

where:

public static partial class JsonSerializer
{
    public static JsonSerializerSettings EscapeHtmlSerializer()
    {
        JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings
        {
            StringEscapeHandling = StringEscapeHandling.EscapeHtml
        };
        return jsonSerializerSettings;
    }
}

Is there a way this could be simplified? For example a way that I could make a new JsonConvert that already had the settings?

Upvotes: 0

Views: 492

Answers (1)

Zohar Peled
Zohar Peled

Reputation: 82474

You can't "make a new JsonConvert", because JsonConvert.SerializeObject is a static method, and static members are not polymorphic.

What you can do, however, quite easily - is simply to write a method to bind these two steps together:

// Inside the Helpers namespace, in JsonConverter class...
public static string SerializeEscapeHtml(object content)
    => JsonConvert.SerializeObject(content, JsonSerializer.EscapeHtmlSerializer());

and then, in your update method - simply do this:

var serializeJson = Helpers.JsonConverter.SerializeEscapeHtml(dconf);

If you're using the EscapeHtmlSerializer() often, you might consider creating a static property and initialize it once instead of generating new JsonSerializerSettings every single time:

public static JsonSerializerSettings EscapeHtmlSerializer {get;} = new JsonSerializerSettings
    {
        StringEscapeHandling = StringEscapeHandling.EscapeHtml
    };

and then change the method to

public static string SerializeEscapeHtml(object content)
    => JsonConvert.SerializeObject(content, JsonSerializer.EscapeHtmlSerializer);

This doesn't look like much of a change, but it should slightly improve memory allocation since now you only use a single instance of the JsonSerializerSettings throughout the lifetime of the program instead of generating an instance of it every time you serialize something to json.

Update:

The JsonConverter class

namspace Helpers
{
    public static class JsonConverter 
    {
        public static string SerializeEscapeHtml(object content)
            => JsonConvert.SerializeObject(content, JsonSerializer.EscapeHtmlSerializer);
    }
    // Whatever more code you need here
}

The JsonSerializer class

namspace Helpers
{
    public static class JsonSerializer 
    {
        public static JsonSerializerSettings EscapeHtmlSerializer {get;} = new JsonSerializerSettings
        {
            StringEscapeHandling = StringEscapeHandling.EscapeHtml
        };
    }
    // Whatever more code you need here
}

Upvotes: 1

Related Questions