Getting Started
Architecture
NServiceBus
Transports
Persistence
ServiceInsight
ServicePulse
ServiceControl
Monitoring
Samples

System.Text.Json serializer

NuGet Package: NServiceBus (9-pre)
This page targets a pre-release version. Pre-releases are subject to change and samples are not guaranteed to be fully functional.

The System.Text.Json message serializer uses the JSON serialization built in to .NET to serialize and deserialize messages. This serializer should be the default choice for serialization for new projects.

Usage

endpointConfiguration.UseSerialization<SystemJsonSerializer>();

Specifying content type

The default content type used is application/json but can be changed using:

endpointConfiguration.UseSerialization<SystemJsonSerializer>()
    .ContentType("application/json; systemjson");
Adding a suffix like ; systemjson requires all endpoint involved to use this case-sensitive full key. See NServiceBus.ContentType documentation for more information

Customizing serialization options

To control how the serialization and deserialzation is performed JsonSerializerOptions can be passed in:

var jsonSerializerOptions = new JsonSerializerOptions
{
    WriteIndented = true
};

endpointConfiguration.UseSerialization<SystemJsonSerializer>()
    .Options(jsonSerializerOptions);

Compatibility with Newtonsoft.Json

The System.Text.Json serializer is more limited compared to Newtonsoft.Json, see the upgrade guide for more details.

If needed, both serializers can be used side-by-side during a transition period by using multiple deserializers. For this to work, a custom content type needs to be specified as shown above.

Migration from the community version

Thanks to Simon Cropp who built the community version of the serializer and donated it to Particular Software.

The serializer is mostly compatible with the community version, see the upgrade guide for more details.

Samples


Last modified