Search code examples
javajsonjacksonjackson-modules

What is Class<?> vc or JavaType valueType used for in the constructor of Jackson's StdDeserializer


I am making a custom deserializer (extending StdDeserializer) using the Jackson JSON library and I can't figure out what the purpose of the constructor parameters are. What is Class<?> or JavaType valueType used for by Jackson? Is it just because Java doesn't have reified generics and Jackson needs more information about the generic type you're making an instance of?


Solution

  • Jackson matches JSON structures with java classes. Just like JAXB maps java classes to XSD-s.

    So, this Class<?> (or JavaType) defines which java class belongs to this deserializer. These java classes can be anything that have the right annotations, like @JsonProperty.

    Another question: why is this class generic? This is so because [java.lang.Class][1] is generic. If you want to do it correctly, then you specify it like this:

    StdDeserializer serializer = new StdDeserializer(YourClass.class);
    

    The point is that the StdSerializer does not bound the classes you can specify.