Polymorphism deserialization

50 views
Skip to first unread message

Mattia Amato

unread,
Aug 20, 2014, 5:51:18 AM8/20/14
to jackso...@googlegroups.com
Hi
I have a problem with polymorphism and deserialization.
Here the situation: I have an abstract Message class:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({ @JsonSubTypes.Type(value = MacAddrRequest.class, name = "MacAddrRequest") })
public abstract class Message {

private String messageId;

public Message(String messageId) {
setMessageId(messageId);
}

public String getMessageId() {
return messageId;
}

public void setMessageId(String messageId) {
this.messageId = messageId;
}
}

which is extended by the MacAddrRequest class:
@JsonTypeName(value = "MacAddrRequest")
public class MacAddrRequest extends Message {
public MacAddrRequest(String messageId) {
super(messageId);
}
}

I serialize the MacAddrRequest class like this:
private ObjectMapper mapper = new ObjectMapper();
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
String m1 = mapper.writeValueAsString(new MacAddrRequest(UUID.randomUUID().toString()));



The question is: How do I deserialize the Message class ?
private ObjectMapper objectMapper = new ObjectMapper();
Message m = objectMapper.readValue(request, Message.class);
    if (m instanceof MacAddrRequest){
        response = "Ok";
    }

Thanks for the help

Tatu Saloranta

unread,
Aug 20, 2014, 6:20:38 PM8/20/14
to jackso...@googlegroups.com
The code should work as you suggested, since Message is marked as polymorphic, and has @JsonSubTypes to point to known implementing classes.

Are you having an issue with this?

-+ Tatu +-


--
You received this message because you are subscribed to the Google Groups "jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.
To post to this group, send email to jackso...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mattia Amato

unread,
Aug 21, 2014, 4:10:18 AM8/21/14
to jackso...@googlegroups.com
I found the problem!
To let the whole thing work I needed to add:

@JsonProperty("messageId")

for each argument in the different constructors. 

I created a Packet class that stores the abstract Message (a "container" for the message).

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, visible = true, property = "@class")
public class Packet {
private Message message;
public Packet(@JsonProperty("message") Message message) {
setMessage(message);
}
public Message getMessage() {
return message;
}
public void setMessage(Message message) {
this.message = message;
}
}

Since the class Message is abstract I think that this is the only possible solution.
To deserialize it I did the following:

private ObjectMapper objectMapper = new ObjectMapper();
Packet p = objectMapper.readValue(request, Packet.class);
    if (p.getMessage() instanceof MacAddrRequest){
        response = "Ok";
    }

- Mattia
Reply all
Reply to author
Forward
0 new messages