0

I have a usecase where the request payload contains a payload like given below

"list_of_conditions": [
    {
      "field_id": "QTrg9vq4S5uUs0yMnCpSVQ",
      "value": "monitor",
      "show": [
          {
              "field_id": "7Q41FQvWSR2u4E5PiT39DQ",
              "required": true
          },
          {
              "field_id": "FCqIf70iQJi48H-dC6xMEg"
          },
          {
              "field_id": "vaTvFctJQgmhZq4F_liUBg"
          }
      ],
      "modify": [
          {
              "field_id": "7Q41FQvWSR2u4E5PiT39DQ",
              "required": true,
              "newValue": "hello"
          }
      ]
  }, 

  {
      "field_id": "QTrg9vq4S5uUs0yMnCpSVQ",
      "value": "monitor",
      "show": [
          {
              "field_id": "7Q41FQvWSR2u4E5PiT39DQ",
              "required": true
          },
          {
              "field_id": "FCqIf70iQJi48H-dC6xMEg"
          },
          {
              "field_id": "vaTvFctJQgmhZq4F_liUBg"
          }
      ]
  }


]

In the above snippet, every object inside list_of_conditions has some operations like "show", "modify" and this list can grow in future as functionality increases. My objective here is to ingest this payload from the request payload in the controller and persist in DB. I tried writing below DTO but it doesn't work for all the use cases. Ideally a request payload can contain other operation like "modify" and DTO should be able to deserialise it

@Data
@Builder
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@NoArgsConstructor
@AllArgsConstructor
public class ListOfCondition {
    @JsonProperty("id")
    private String id;

    @JsonProperty("field_id")
    private String fieldId;

    @JsonProperty("value")
    private String value;

    @JsonProperty("show")
    private List<ActionDTO> childFields;

}

What I really want here is, my DTO should be able to deserialise payload given above. Currently, it is only doing it for a payload containing "show" as key because of below snippet. How can I make it generic and accept other keys as well

  @JsonProperty("show")
    private List<ConditionActionDTO> childFields;

1 Answers1

0

Most likely a duplicate of this. JSON Jackson parse different keys into same field

I guess that should satisfy your requirement if you are not going to have multiple keys for a single field_id.

Edit:

@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class TestPojo {

    private Map<String, List<ActionDto>> unknownFields = new HashMap<>();

    @JsonAnyGetter
    public Map<String, List<ActionDto>> getUnknownFields() {
        return unknownFields;
    }

    @JsonAnySetter
    public void setUnknownFields(String name, List<ActionDto> actionDtos) {
        this.unknownFields.put(name, actionDtos);
    }
}

Possible solution for dynamic key value pojos for you.

cant-code
  • 66
  • 3
  • Thanks @cant-code. This solution looks very simple and probably easiest way to achieve the above requirement. I'm looking for something in which I don't have to provide json alias. As the operation list is dynamic, I'm afraid, I'll end up modifying it every now and then – Abhinaya Pandey Aug 18 '23 at 12:56
  • Added another possible solution as an edit. You can try something like that for unknown fields where the key is the name and the value pojo which is known. I would still not recommend this but its an option. – cant-code Aug 18 '23 at 14:24