The System.Text.Json message serializer uses the built-in JSON serialization in .NET to serialize and deserialize messages. This serializer should be the default choice for serialization in new projects.
Usage
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
Specifying content type
The default content type is application/. It can be changed using:
endpointConfiguration.UseSerialization<SystemJsonSerializer>()
.ContentType("application/json; systemjson");
Adding a suffix like ; systemjson requires all endpoints involved to use this case-sensitive full key. See NServiceBus.ContentType documentation for more information.
Customizing serialization options
To control how serialization and deserialization are performed, pass in JsonSerializerOptions:
var jsonSerializerOptions = new JsonSerializerOptions
{
WriteIndented = true
};
endpointConfiguration.UseSerialization<SystemJsonSerializer>()
.Options(jsonSerializerOptions);
Trimming and Native AOT
Registering message types with a source-generated JsonSerializerContext as the TypeInfoResolver of the serializer options avoids reflection-based metadata and makes the serializer trimming and Native AOT friendly. See reflection vs source generation for how source generation works in System.Text.Json.