使用Json.Net从UI隐藏类型,但改为公开其属性之一

时间:2018-11-09 17:56:02

标签: json.net

假设我们有一些业务类型,我们希望以一种Restful的方式向UI公开:

class ToExpose {
    public ToHide SomeBusinessProperty { get; set; }
    public string SomeOtherBusinessProperty { get; set; }
}

然后,我们有一种类型可以用作包装器,并通过Content属性公开业务类型:

class Wrapper
{
    public ToExpose Content { get; set; }
}

我们希望我们的UI与Wrapper类型无关,因此Wrapper的实例需要通过直接公开其Content来进行序列化,并需要通过重新初始化a来反序列化Wrapper用于该类型的每个属性,并填充Wrapper.Content

ToExpose类型的标准序列化可以提供:

{
  "$type": "ToExpose",
  "someBusinessProperty": {
    "$type": "Wrapper",
    "content": {
      "$type": "ToExpose",
      "SomeOtherBusinessProperty": "abc"
    }
  }
}

我们致力于实现这一目标:

{
  "$type": "ToExpose",
  "SomeBusinessProperty": {
    "$type": "ToExpose",
    "SomeOtherBusinessProperty": "abc"
  }
}

我们使用IContractResolverIValueProviderJsonConverterISerializationBinder进行了一些挖掘,并在互联网上寻找答案,但是我们不确定哪种良好的设计模式会是。

0 个答案:

没有答案