Skip to content

Instantly share code, notes, and snippets.

@christophercurrie
Created February 11, 2014 17:20
Show Gist options
  • Save christophercurrie/8939489 to your computer and use it in GitHub Desktop.
Save christophercurrie/8939489 to your computer and use it in GitHub Desktop.
Example of Polymorphic Deserialization using Jackson
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value=AudioAttachment.class, name="audio"),
@JsonSubTypes.Type(value=LinkAttachment.class, name="link")
})
public class Attachment {
}
@JsonNaming(PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy.class)
public class Audio {
public int aid;
public int ownerId;
}
public class AudioAttachment extends Attachment {
public Audio audio;
}
public class Link {
public String url;
public String title;
}
public class LinkAttachment extends Attachment {
public Link link;
}
@jloisel
Copy link

jloisel commented Feb 14, 2018

Here is a good article describing how to deal with Polymorphism and Jackson Deserialization: https://octoperf.com/blog/2018/02/01/polymorphism-with-jackson/

@juli0mendes
Copy link

Excellent tutorial!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment