Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@JsonCreator and companion object #110

Closed
crumbpicker opened this issue Nov 17, 2013 · 6 comments
Closed

@JsonCreator and companion object #110

crumbpicker opened this issue Nov 17, 2013 · 6 comments

Comments

@crumbpicker
Copy link

I have a question related to the @JsonCreator annotation. The Jacks library allows to use this annotation in combination with the companion object of the case class to be deserialized. It has the benefit to be less obstrusive than defining a secondary constructor in the case class. My problem is that it is sometimes hard to deal with multiple constructors having default values.

So is it possible with the scala module to use the @JsonCreator annotation in the companion object ?

@christophercurrie
Copy link
Member

It's not currently possible with the @JsonCreator annotation, but it's a use case I'm looking at supporting.

In the interim, you might be able implement support directly for your own types by creating your own custom module. The outline of this approach looks like:

  • Create a ValueInstantiator, by deriving from the supplied base class StdValueInstantiator and implementing createFromObjectWith to call the companion object method.
  • Create a Module by deriving from the supplied base class SimpleModule and calling addValueInstantiator in the constructor to register your instantiator for your case class type.
  • Register your module using ObjectMapper.registerModule.

@crumbpicker
Copy link
Author

Thank you Christopher for your quick answer.
Your procedure should be effectively helpful for a custom implementation.

Currently, as a quick and dirty workaround, I'm using the scala module for serialization and Jacks for deserialization.
I have wrapped a scala module object mapper and the JacksMapper (with the same configuration) in a scala object and it works like a charm.
Do you see any conflict that could occur doing this way ?

@christophercurrie
Copy link
Member

If you're using separate ObjectMapper instances, they will not conflict, but I would have some concerns:

  • There's no guarantee the Scala module and the JacksMapper will interpret a given object or JSON text in a consistent way. Property naming rules, null handling, and minor details are likely to not be applied consistently.
  • ObjectMapper does a lot of caching to improve performance. Using two will slow the warm-up process of the mapper and duplicate the memory footprint over time, and potentially create other unknown inconsistencies between the cached versions of things.

If you're not in a position to implement a custom ValueInstantiator, my own recommendation would be to simply use Jacks for both serialization and deserialization until the Scala module has an equivalent feature. This assumes that Jacks is not missing any serialization functionality that you would need.

@crumbpicker
Copy link
Author

I'm applying the same configuration (PropertyNamingStrategy,Visibility,SerializationInclusion) to the both object mappers. Thus I expect that the first point won't be an issue.
However the cache aspect may be effectively a problem. Is the cache shared for between serialization and deserialization procedures ?

I'll try to implement my own ValueInstantiator if I encounter any bug in my Unit tests.

@cowtowncoder
Copy link
Member

Caches used are separate between serialization and deserialization; I can not think of any shared caching at databind level, or even at core streaming API level.

@christophercurrie christophercurrie modified the milestones: 2.4, 2.6 Nov 26, 2014
@nbauernfeind nbauernfeind modified the milestones: 2.8.0, 2.6 Apr 26, 2016
@pjfanning
Copy link
Member

There are test cases in latest versions of jackson-module-scala that use @JsonCreator annotations on companion object functions. Please reopen if you have a specific test case that does not work.

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

No branches or pull requests

5 participants