JSON Serializer

Source
NuGet Package NServiceBus (4.x)
Standard support for version 4.x of NServiceBus has expired. For more information see our Support Policy.
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

Configure.Serialization.Json();

Json.NET versions

Over time, the version of ILMerged Json.NET has changed.

NServiceBus VersionJson.NET Version
4.04.5.11
4.1 - 5.X5.0.6

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"}

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

Using BSON via the same ILMerged copy of Json.NET as above.

Usage

Configure.Serialization.Bson();

Samples


Last modified