Home > Blockchain >  How to deserialize named arrays into objects in Kafka
How to deserialize named arrays into objects in Kafka

Time:06-09

Here is the example of my received kafka message:

{"target":[{"timestamp": "2022-06-20T12:31:46 03:00", "result":"fail", "message":"Client not found."}, {"timestamp": "2022-06-25T12:31:46 03:00", "result":"success"}], "xxi":[{"timestamp": "2022-06-25T12:31:46 03:00", "result":"success"}]}

Here is the class which is for objects to deserialize from the message

public class CommonEventResult {
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    private LocalDateTime timestamp;
    private String result;
    private String message;
}

The main problem that I don't know what the name of a system will come - xxi, target, etc. But I want to deserialize the message in some CommonEventResult[] array or something like that. How can I do it?

CodePudding user response:

try

Map<String,List<CommonEventResult>>

CodePudding user response:

You can model your response class with some generic implementation, something like

@JsonFormat(shape = JsonFormat.Shape.ARRAY)

public class DataResponse {

    public String dataResult;

    public String getDataResult() {
        return dataResult;
    }
}


class GenericResponse implements Response<List<**DataResponse**>> {

    @JsonIgnore
    public List<DataResponse> data;


    @Override
    public List<DataResponse> getData() {
        return data;
    }
  • Related