简体   繁体   中英

Preprocess json with lombok and jackson

I'm using lombok and jackson together. I need to pre-process a json with this structure. By the way, I can't change json structure.

[
    {
        "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.

I thought that a builder could help me, but I've got lost and couldn't write a good solution for my problem.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM