Пример #1
0
    public async Task FormatTypeAsExpected_Custom()
    {
        var serializer = new JsonSerializer(new CustomTypeNameConventionBuilder()
                                            .AddWithShortName <SomeType>()
                                            .GetConvention());

        const string expectedTypeName = "SomeType";

        var message          = new Message(new Dictionary <string, string>(), new SomeType());
        var transportMessage = await serializer.Serialize(message);

        var type = transportMessage.Headers.GetValue(Headers.Type);

        Console.WriteLine($@"

Serialized type name: {type}
  Expected type name: {expectedTypeName}

");

        var roundtrippedMessage = await serializer.Deserialize(transportMessage);

        Assert.That(type, Is.EqualTo(expectedTypeName));
        Assert.That(roundtrippedMessage.Body, Is.TypeOf <SomeType>());
    }
Пример #2
0
    public async Task WorksWithoutFullTypeNameHandlingToo()
    {
        var simpleSerializer = new JsonSerializer(new SimpleAssemblyQualifiedMessageTypeNameConvention(), new JsonSerializerSettings {
            TypeNameHandling = TypeNameHandling.None
        });
        var message          = new RandomMessage("hei allihoppa");
        var transportMessage = await simpleSerializer.Serialize(new Message(new Dictionary <string, string>(), message));

        var roundtrippedMessage = (await simpleSerializer.Deserialize(transportMessage)).Body;

        Assert.That(roundtrippedMessage, Is.TypeOf <RandomMessage>());
    }
Пример #3
0
    public async Task CheckEncodingBug()
    {
        var utf32Serializer = new JsonSerializer(new SimpleAssemblyQualifiedMessageTypeNameConvention(), encoding: Encoding.UTF32);
        var utf8Serializer  = new JsonSerializer(new SimpleAssemblyQualifiedMessageTypeNameConvention(), encoding: Encoding.UTF8);

        var transportMessage = await utf8Serializer.Serialize(new Message(new Dictionary <string, string>(), new Something("hej")));

        var roundtripped = await utf32Serializer.Deserialize(transportMessage);

        var something = roundtripped.Body as Something ?? throw new AssertionException($"Message body {roundtripped.Body} was not Something");

        Assert.That(something.Text, Is.EqualTo("hej"));
    }
Пример #4
0
    public async Task FormatTypeAsExpected_Default()
    {
        var expectedTypeName = typeof(SomeType).GetSimpleAssemblyQualifiedName();
        var serializer       = new JsonSerializer(new SimpleAssemblyQualifiedMessageTypeNameConvention());

        var message          = new Message(new Dictionary <string, string>(), new SomeType());
        var transportMessage = await serializer.Serialize(message);

        var type = transportMessage.Headers.GetValue(Headers.Type);

        Console.WriteLine($@"

Serialized type name: {type}
  Expected type name: {expectedTypeName}

");

        var roundtrippedMessage = await serializer.Deserialize(transportMessage);

        Assert.That(type, Is.EqualTo(expectedTypeName));
        Assert.That(roundtrippedMessage.Body, Is.TypeOf <SomeType>());
    }