Disable type containing dictionary property using ServiceStack JsonSerializer

The below code snippet shows two ways to achieve this. The first one uses MsgPack and the second test uses the ServiceStack JSONSerializer . The second one is more favorable because the ServiceStack.Text JSONSerializer is used throughout the project in which I work.

Why the second test below doesn't work when using dictionary <Street, HashSet <int β†’> <//

[TestFixture]
public class NeighbourhoodTests
{
    private Neighbourhood _myNeighbourhood;
    private Street _street;

    [SetUp]
    public void SetupOnEachTest()
    {
        _street = new Street() { Name = "Storgate" };
        _myNeighbourhood = SetupMyNeighbourhood(_street);
    }

    private static Neighbourhood SetupMyNeighbourhood(Street street)
    {
        var myNeighbourhood = new Neighbourhood();
        myNeighbourhood.Addresses = new Dictionary<Street, HashSet<int>>();

        myNeighbourhood.Addresses.Add(street, new HashSet<int>(new[] { 1, 2, 3, 4 }));
        myNeighbourhood.LocalCouncilName = "Stavanger";
        myNeighbourhood.RegionName = "Rogaland";
        return myNeighbourhood;
    }

    [Test]
    public void TestNeighbourhoodClass_OnMsgPackDeserialization_AddressesShouldEqualOriginalInput()
    {
        ObjectPacker packer = new ObjectPacker();
        var packedMessageBytes = packer.Pack(_myNeighbourhood);
        var unpackedMessage = packer.Unpack(packedMessageBytes);

        Assert.That(unpackedMessage.RegionName, Is.EqualTo("Rogaland"));
        Assert.That(unpackedMessage.Addresses, Is.Not.Empty);
        Assert.That(unpackedMessage.Addresses.Keys.Any(key => key.Name.Equals(_street.Name)));
    }

    [Test]
    public void TestNeighbourhoodClass_OnServiceStackJsonTextDeserialization_AddressesShouldEqualOriginalInput()
    {
        string serialisedMessage = JsonSerializer.SerializeToString(_myNeighbourhood);
        var deserializedMessage = JsonSerializer.DeserializeFromString(serialisedMessage);

        Assert.That(deserializedMessage.RegionName, Is.EqualTo("Rogaland"));
        Assert.That(deserializedMessage.Addresses, Is.Not.Empty);
        Assert.That(deserializedMessage.Addresses.Keys.Any(key => key.Name.Equals(_street.Name)));
    }
}

public class Neighbourhood
{
    public Dictionary<Street, HashSet<int>> Addresses { get; set; }
    public string LocalCouncilName { get; set; }
    public string RegionName { get; set; }
}

      

+3


source to share


1 answer


Aaah thanks to @ mortenrΓΈgenes - it turns out that I have to wrap the Addresses property of the Neighborhood class in its own type as such:

public class Addresses
{
    public Street Street{ get; set; }
    public HashSet<int> HouseNumbers { get; set; }
}
public class Neighbourhood
{
    public Addresses { get; set; }
    public string LocalCouncilName { get; set; }
    public string RegionName { get; set; }
}

      



This way the ServiceStack JSONSerializer will work and the test will pass.

+1


source







All Articles