Deserialize a type containing a Dictionary property using ServiceStack JsonSerializer

孤者浪人 提交于 2019-12-10 19:13:18

问题


The code snippet below shows two ways I could achieve this. The first is using MsgPack and the second test is using ServiceStack's JSONSerializer. The second is more favourable because the ServiceStack.Text JSONSerializer is used throughout a project I'm working in.

Why is the second test below failing when using a 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; }
}

回答1:


Aaah thanks to @mortenrøgenes- turns out that I have to wrap the Addresses property of the Neighbourhood class into it's 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; }
}

That way the ServiceStack JSONSerializer would work and the test would pass.



来源:https://stackoverflow.com/questions/14716979/deserialize-a-type-containing-a-dictionary-property-using-servicestack-jsonseria

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!