You might want to have a look at the examples here (first hit on google BTW):
Define a custom Deserializer:
public class SomeClassDeserializer extends StdDeserializer<SomeClass> {
public SomeClassDeserializer() {
this(null);
}
public SomeClassDeserializer(Class<?> vc) {
super(vc);
}
@Override
public SomeClass deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
JsonNode node = jp.getCodec().readTree(jp);
// read values from node and use the SomeClassBuilder
// to create an instance of SomeClass
}
}
Make Jackson aware of the deserializer by registering it with an ObjectMapper
:
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addDeserializer(SomeClass.class, new SomeClassDeserializer());
mapper.registerModule(module);
SomeClass readValue = mapper.readValue(json, SomeClass.class);
Depending on the framework you're using there might be other more elegant ways to register the deserializer.