Serlizing JSON polymorphically when type field is in parent JSON using Jackson

Today, I came across a json parsing situation where the sub-type field is outside the type json. Genrally we seen a situation like this

@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="@class")
class Animal{}

class Dog extends Animal{}

class Cat extends Animal{}

and it de-serialize to json like this:

    "@class" : "x.y.z.Cat", ...
}

In my project I need to parse json like this
{
"status": "OK",
"messageName": "x.y.z.Success",
"messagePayload": {
"foo": "cd3a5697",
"bar": 224
}
}
{
"status": "ERROR",
"messageName": "x.y.z.Error",
"messagePayload": {
"errorCode": "APPLICATION_ERROR",
"errorInstanceId": "b292b864",
"errorDetailedMessage": "Invalid UUID string (Length mismatch)"
}
}

Now only thing that differs here is the messagePayload and the property that makes it different is either messageName or status. I googled this kind of json serializing problem but didn't get the expected results. All results gave example shown in above Animal class.

How I solved it - well its very simple trick that strike me next day. I was trying the make @JsonTypeInfo on messagePayload, which is not working as sub-type field is outside the payload json. When I used the @JsonTypeInfo on whole message it works:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = As.EXTERNAL_PROPERTY, property = "messageName")
@JsonSubTypes({
    @JsonSubTypes.Type(value = Success.class, name = "x.y.z.Success"),
    @JsonSubTypes.Type(value = Error.class, name = "x.y.z.Error")})
public abstract class Response {
    private String status;
    public abstract Payload getMessagePayload();
}

public abstract class Payload {}

public class SuccessPayload extends Payload {
    private String foo;
    private Long bar;
}

public class ErrorPayload extends Payload {
    private String errorCode;
    private String errorInstanceId;
    private String errorDetailedMessage;
}

Now using this I get the correct serialized payload depending upon the message class am getting in json. hope this helps.

Comments

Popular Posts