含反序列化使用ServiceStack JsonSerializer字典物业类型 [英] Deserialize a type containing a Dictionary property using ServiceStack JsonSerializer

查看:162
本文介绍了含反序列化使用ServiceStack JsonSerializer字典物业类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在code段列出了两种方法,我可以做到这一点。首先是使用 MsgPack 并第二次测试使用的 ServiceStack的JSONSerializer 。二是更有利的,因为ServiceStack.Text JSONSerializer用于整个项目中,我的工作。

为什么要低于使用字典和LT时未能通过第二次试验,街,HashSet的< INT>>

  [的TestFixture]
公共类NeighbourhoodTests
{
    私人邻里_myNeighbourhood;
    私家街道_street;

    [建立]
    公共无效SetupOnEachTest()
    {
        _street =新街(){名称=Storgate};
        _myNeighbourhood = SetupMyNeighbourhood(_street);
    }

    私有静态邻里SetupMyNeighbourhood(陌陌)
    {
        VAR myNeighbourhood =新邻里();
        myNeighbourhood.Addresses =新字典<街道,HashSet的< INT>>();

        myNeighbourhood.Addresses.Add(街道,新的HashSet&其中; INT>(新[] {1,2,3,4}));
        myNeighbourhood.LocalCouncilName =斯塔万格;
        myNeighbourhood.RegionName =罗加兰;
        返回myNeighbourhood;
    }

    [测试]
    公共无效TestNeighbourhoodClass_OnMsgPackDeserialization_AddressesShouldEqualOriginalInput()
    {
        ObjectPacker打包机=新ObjectPacker();
        VAR packedMessageBytes = packer.Pack(_myNeighbourhood);
        VAR unpackedMessage = packer.Unpack(packedMessageBytes);

        Assert.That(unpackedMessage.RegionName,Is.EqualTo(罗加兰));
        Assert.That(unpackedMessage.Addresses,Is.Not.Empty);
        Assert.That(unpackedMessage.Addresses.Keys.Any(键=> key.Name.Equals(_street.Name)));
    }

    [测试]
    公共无效TestNeighbourhoodClass_OnServiceStackJsonTextDeserialization_AddressesShouldEqualOriginalInput()
    {
        字符串serialisedMessage = JsonSerializer.SerializeToString(_myNeighbourhood);
        VAR deserializedMessage = JsonSerializer.DeserializeFromString(serialisedMessage);

        Assert.That(deserializedMessage.RegionName,Is.EqualTo(罗加兰));
        Assert.That(deserializedMessage.Addresses,Is.Not.Empty);
        Assert.That(deserializedMessage.Addresses.Keys.Any(键=> key.Name.Equals(_street.Name)));
    }
}

公共类邻里
{
    公共字典<街道,HashSet的< INT>>地址{获得;组; }
    公共字符串LocalCouncilName {获得;组; }
    公共字符串RegionName {获得;组; }
}
 

解决方案

啊哈感谢@mortenrøgenes-原来,我不得不将邻里类的地址都归到它自己的类型,例如:

 公共类地址
{
    大众街街道{获得;组; }
    公众的HashSet< INT> HouseNumbers {获得;组; }
}
公共类邻里
{
    公网地址{获得;组; }
    公共字符串LocalCouncilName {获得;组; }
    公共字符串RegionName {获得;组; }
}
 

这样的ServiceStack JSONSerializer将工作和测试也就过去了。

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

解决方案

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.

这篇关于含反序列化使用ServiceStack JsonSerializer字典物业类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆