在JSON转换期间过滤掉某些子对象

时间:2018-08-08 08:53:53

标签: c# .net serialization json.net deserialization

我不确定如何用语言解释我们的目标,因此我将直接跳至代码。

使用我们当前的json转换器设置。转换其中一个事件时,我们得到以下结果。

{
    "PortfolioId": {
        "Id": "portId"
    },
    "EntityId": {
        "Id": "3cf7582b-3cad-4aeb-a671-0132ba97d60d"
    },
    "EventVersion": 1,
    "OccurredOn": "2018-08-08T09:52:03.7871323+02:00",
    "Id": "71fe3a2e-354a-4b19-abea-655471e96d72",
    "Creator": {
        "Id": "27a1d6b1-1ffa-4071-92ee-31c12bf120f0"
    },
    "CorrelationId": "3138dbe0-3a4d-4559-83e9-d1f3e5684ee8"
}

我们的目标是获得一个看起来像这样的转换后的事件;

{
    "PortfolioId": "portId",
    "EntityId":  "3cf7582b-3cad-4aeb-a671-0132ba97d60d",
    "EventVersion": 1,
    "OccurredOn": "2018-08-08T09:52:03.7871323+02:00",
    "Id": "71fe3a2e-354a-4b19-abea-655471e96d72",
    "Creator": "27a1d6b1-1ffa-4071-92ee-31c12bf120f0",
    "CorrelationId": "3138dbe0-3a4d-4559-83e9-d1f3e5684ee8"
}

在这种情况下,我们有某种类型的对象(即EntityIdPortfolioId),该对象将值保存在属性中。所有这些Id类型都是从具有“ Id”属性的抽象类派生的。

事件类如下。

 public class ExampleEvent : DomainEvent
{
    public PortfolioId PortfolioId { get; }
    public EntityId EntityId { get;}

    public ExampleEvent(
        PortfolioId portfolioId,
        EntityId entityId,
        UserId creator, Guid correlationId) : base(creator, correlationId)
    {
        PortfolioId = portfolioId;
        EntityId = entityId;
    }
}

ExampleEvent

有人知道怎么做吗?我认为使用自定义json转换器可能会实现此功能,但目前还不知道如何在这种情况下实现此功能。

编辑:我应该说,这必须完成许多事件类型。通用可重用解决方案似乎最适合于降低开销。这意味着最好根本不更改事件类本身。 (因此最好没有属性等)

3 个答案:

答案 0 :(得分:1)

此答案中的第二种方法可能有助于操纵序列化。 Making a property deserialize but not serialize with json.net

答案 1 :(得分:0)

您可以将JsonIgnore属性与计算出的属性一起使用:

public class PortfolioId
{
    public string Id { get; set; }
}

public class EntityId
{
    public string Id { get; set; }
}

public class UserId
{
    public string Id { get; set; }
}

public class ExampleEvent 
{
    private ExampleEvent() // for JSON deserializer
    {
        Creator = new UserId();
        Portfolio = new PortfolioId();
        Entity = new EntityId();
    }

    // add your base constructor call
    public ExampleEvent(PortfolioId portfolio, EntityId entity, UserId creator)
    {
        Creator = creator;
        Portfolio = portfolio;
        Entity = entity;
    }

    [JsonIgnore]
    public UserId Creator { get; set; } 

    public string CreatorId
    {
        get => Creator.Id;
        set => Creator.Id = value;
    }

    [JsonIgnore]
    public PortfolioId Portfolio { get; set; } 

    public string PortfolioId
    {
        get => Portfolio.Id;
        set => Portfolio.Id = value;
    }

    [JsonIgnore]
    public EntityId Entity { get; set; } 

    public string EntityId
    {
        get => Entity.Id;
        set => Entity.Id = value;
    }

    public int EventVersion { get; set; }

    public string Id { get; set; }

    public string CorrelationId { get; set; }
}

答案 2 :(得分:0)

如果JsonIgnore不适合您的需要,或者您需要更多的自定义,则您可能还会使用IContractResolver / JsonProperty.ShouldDeserialize寻找JsonProperty.ShouldSerializehere一些示例。