-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Immutable object deserialization should be possible without @JsonCreator when the creator is unambiguous. #2852
Comments
I am currently working on #1498, for inclusion in 2.12, and it might do what you want. Note that one way you can probably implement this at this point for your own use case is by sub-classing |
@cowtowncoder |
Yes, so, #1498 tests are under: src/test/java/com/fasterxml/jackson/databind/deser/creators/ConstructorDetector1498Test.java and configuration setting to look for is Setting is done using something like: ObjectMapper mapper = JsonMapper.builder()
.setConstructorDetector(ConstructorDetector.PROPERTIES)
.build(); If you can verify this locally with Closing this for now assuming implementation works. |
Hi @cowtowncoder
? PROPERTIES seems to be SingleArgConstructor type, not ConstructorDetector I am using version 2.12.0-rc1 released yesterday in my project, and I have added a following:
When I run main() it fails with |
@lukaszpierog yes, correct wrt usage, I did not doublecheck my example. On your example: you must register One challenge so far has been testing, as |
@cowtowncoder
|
@lukaszpierog Hmmh. Ok, thank you, I'll see if I can reproduce. |
@lukaszpierog Unfortunately I can not reproduce your findings: your example works fine for me. How does the example fail for you? |
@cowtowncoder
|
@lukaszpierog I can reproduce the issue with that project. But as per my earlier suspicion, using this: that is, adding lines:
makes test pass. I don't know why javac by default does not appear to add parameter names in bytecode. |
@cowtowncoder |
@lukaszpierog Yeah, it is bit odd that default is such that information is left out and not the other way around. But at least the mystery is solved. It would be nice to have some mechanism of indicating likely issue, but this may be tricky. Problem would be reported if constructor was marked with just Will file an issue for that idea. |
Hello, where I work we tend to only use immutable objects with all-args constructor. In order to deserialize such object, we must either add annotations like @JsonCreator to the actual class or when it comes from a library, we must create mix-ins.
Consider the following example class:
Such example will deserialize nicely, but it would be great if there was no need to add anything to the class itself, because:
Actually there already seems to be a deserialization feature that gets me halfway where I want and we can skip the @JsonProperty annotations by configuring ObjectMapper:
However if the @JsonCreator annotation is removed, it will fail, even though there is only a single constructor. I would assume it should be given a try if there is no default nor @JsonCreator annotated method.
In my opinion such scenario should be possible to handle without any additions to the class itself. If not by default, then by configuring a proper deserialization features on ObjectMapper.
The text was updated successfully, but these errors were encountered: