0

I've a JSON in the following format:

{
    "FileStatuses": {
        "FileStatus": [{
                "accessTime": 1479784299020,
                "type": "FILE"
            },
            {
                "accessTime": 1475421868510,
                "type": "FILE"
            }
        ]
    }
}

I'm trying to deserialize it using the following class:

@Value.Immutable
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonSerialize(as = ImmutableFileListResponse.class)
@JsonDeserialize(as = ImmutableFileListResponse.class)
public interface FileListResponse {

    @JsonProperty("FileStatuses")
    JSONObject fileStatuses();
}

But it throws the following error: unrecognized field "FileStatus", not marked as ignorable (0 known properties )

But if I read the Json as String and then use JSONObject obj = new JSONObject(source); It works perfectly.

Where am I going wrong?

1 Answers1

0

I got it working using the following 2 classes:

@Value.Immutable
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonSerialize(as = ImmutableFileListResponse.class)
@JsonDeserialize(as = ImmutableFileListResponse.class)
public interface FileListResponse {

    @JsonProperty("FileStatuses")
    FileListInnerResponse fileStatuses();

    @Value.Immutable
    @JsonIgnoreProperties(ignoreUnknown = true)
    @JsonSerialize(as = ImmutableFileListInnerResponse.class)
    @JsonDeserialize(as = ImmutableFileListInnerResponse.class)
    interface FileListInnerResponse {

        @JsonProperty("FileStatus")
        List<FileData> fileStatus();
    }
}

And FileData.java:

@Value.Immutable
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonSerialize(as = ImmutableFileData.class)
@JsonDeserialize(as = ImmutableFileData.class)
public interface FileData {

    @JsonProperty("accessTime")
    long accessTime();

    @JsonProperty("type")
    String type();
}

Please do let me know if there's a better way than using an inner class or 3 different classes.