How to pass settings to Nservicebus JsonSerializer in C#?

554 views Asked by At

Based on https://docs.particular.net/nservicebus/serialization/json, I would like to pass some settings to the JsonSerializer, such as IgnoreNullValues for example. But so far I didn't find out any example of how to pass settings to the serializer.

var serialization = endpointConfiguration.UseSerialization<JsonSerializer>();

//I would like to use something like below, 
//but I don't find any examples of how to pass settings to the JsonSerializer
serialization.Settings.IgnoreNUllValues = true;

What I cannot find out is the correct syntax for passing the settings or a list of available settings for the default JsonSerializer.

Thanks.

2

There are 2 answers

0
znn On BEST ANSWER

From David Boike comment, this is exactly the answer to my question:

By default JSON.NET is internalized into NServiceBus, which means you can't set any options on it or really access its APIs at all. That's why the NewtonsoftSerializer exists, to allow you to use an external JSON.NET package that's in your project with whatever settings you'd like.

2
Yannick Meeus On

As per their documentation:

var settings = new JsonSerializerSettings
{
    TypeNameHandling = TypeNameHandling.Auto,
    Converters =
    {
        new IsoDateTimeConverter
        {
            DateTimeStyles = DateTimeStyles.RoundtripKind
        }
    }
};
var serialization = endpointConfiguration.UseSerialization<NewtonsoftSerializer>();
serialization.Settings(settings);

This does use the NewtonsoftSerializer, instead of the JsonSerializer so your mileage may vary.