Jackson is demanding the public
modifier on a no-arg constructor, while jdk8's Optional
provides only static methods. What are the possible workarounds? And in general, if I have to use a a 3rd party library providing classes following this pattern, what should I do?

- 4,811
- 1
- 26
- 46
-
http://stackoverflow.com/questions/11838039/jackson-3rd-party-class-with-no-default-constructor ? – Brian Roach Jan 13 '14 at 18:08
-
@BrianRoach, I still don't get how to make use of that in case of a generic type. – tkroman Jan 13 '14 at 19:33
2 Answers
No, Jackson does NOT require a public no-arg constructor; any access level (even private
) will work. But it does require a no-arg constructor if no custom deserializer is provided.
What is needed for new types, in general are extension modules. There are actually plans to add a "JDK8 module", since core Jackson only requires 1.6 currently, and baseline is unlikely to move in near future. But adding support for new types should be relatively easy: Jackson Guava datatype module (https://github.com/FasterXML/jackson-datatype-guava) for example, adds support for Guava's optional
. You may be able to copy deserializer from there in the meantime, use it via SimpleModule
you define.

- 113,358
- 34
- 211
- 239
-
Thank you, this link is a helpful one. Since there is no significant difference, I'm going to implement something like that while there is no support for this stuff out-of-the-box. – tkroman Jan 13 '14 at 22:12
-
2Here's the issue, for any newcomers https://github.com/FasterXML/jackson-databind/issues/494 – Emerson Farrugia Sep 24 '14 at 13:47
Optional
support has since been added to Jackson, and can be used by adding Jdk8Module
to the ObjectMapper
:
JsonMapper objectMapper =
JsonMapper.builder().addModule(new JavaTimeModule()).build();

- 14,487
- 7
- 91
- 130