用龙目岛和杰克逊预处理json

我一起使用龙目岛和杰克逊。我需要使用此结构预处理json。顺便说一句,我不能更改json结构。

[
    {
        "value1": "INC12345",
        "value2": "12345",
        "task": [
            {
                "status": "A",
                "typeOfClass": "Membership"
            },
            {
                "status": "D",
                "reverseEnum": 1,
                "typeOfClass": "Reverse"
            }
        ]
    }
]

What's the problem? Well, I need to process task's list when the request is received to define the type of object of every task with the attribute typeOfClass because there I’’m using different type of class, could receive a Membership’s type, a Reverse’s type, etc, and this classes don’t have anything common. This is my java class.

@Data
@JsonDeserialize(builder = Incident.IncidentBuilder.class)
@Builder(builderClassName = "IncidentBuilder", toBuilder = true)
public class Incident {

  @NotNull
  private String value1;

  @NotNull
  private BigInteger value2;

  @NotNull
  private List<Object> task;

  @JsonPOJOBuilder(withPrefix = "")
  public static class IncidentBuilder {
  }

}

I don't have problems with others attributes but when I'm debugging I can see that task list is a LinkedList, so I need to pre-process that list to create a list with the specific type of objects. I know how to create a custom deserializer with jackson and I'm looking for a similar solution using lombok to avoid lot of code.

我以为建筑商可以帮助我,但我迷路了,无法为我的问题写出好的解决方案。

评论
  • Moll
    Moll 回复

    In Java, the List is a collection of objects which belong to the same class type.

    As your Type element contains which object it belongs to, I assume, there is a top-level parent class that is the parent to all the type objects.

    So, instead of declaring it as the List<Object> task, I recommend you use List<ParentClass> task.