JSON Serializer

In NServiceBus version 7 and above, the JsonSerializer has been removed. Use the external Json.NET Serializer instead. See also Compatibility between the core and external Json.NET serializer.

This serializer uses JSON via an ILMerged copy of Json.NET.

Usage

endpointConfiguration.UseSerialization<JsonSerializer>();

Json.NET version

Version 8.0.2 of Json.NET is ILMerged.

Customization

Since Json.NET is ILMerged, the Json.NET customization attributes are not supported. However, certain customizations are still supported via standard .NET attributes.

Excluding members

Members can be exclude using the IgnoreDataMemberAttribute.

The attribute can be used as follows:

public class Person
{
    public string FamilyName { get; set; }
    public string GivenNames { get; set; }

    [IgnoreDataMember]
    public string FullName { get; set; }
}

The serialized output for following instance:

Person person = new Person
{
    GivenNames = "John",
    FamilyName = "Smith",
    FullName = "John Smith"
};

will be:

{"FamilyName":"Smith","GivenNames":"John"}

Customizing encoding

By default, the JSON serializer uses UTF8 encoding. To customize the encoding, use the following API. In the example below, the custom encoding omits the Byte Order Mark (BOM).

var noBomEncoding = new UTF8Encoding(false);

endpointConfiguration.UseSerialization<JsonSerializer>()
    .Encoding(noBomEncoding);

Inferring message type from $type

For integration scenarios where the sender is unable to add message headers, the serializer is able to infer the message type from the $type property supported by Json.NET. By default Json.NET requires the property to contain the assembly-qualified name of the message type but to make integration easier, the serializer also registers a custom binder that allows only the full type name to be passed.

See the native integration with SqlTransport sample for more details.

Bson

The built in BSON serializer has been deprecated. The external Newtonsoft serializer can be used as a replacement.

Samples


Last modified