-
-
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
Issue Serializing an Immutable Class with @JsonCreator #1529
Comments
I suspect may not be aware of one limitation regarding JVM and bytecode: that of method (including constructor) parameters not retaining names in bytecode (at least before Java 8, in which they may be included, depending on compiler settings). Because of this, code: public BrokenDTO(
@JsonProperty("first_name")
final String firstName,
@JsonProperty("last_name")
final String lastName) {
} does not rename property from "firstName" into "first_name", but rather assigns a name for sort of unnamed parameter. With Java 8 things change, and this is why |
Thanks for that comment, I am actually compiling the parameter names in as this is a Java 8 project, but I was unaware of the |
That fixes my problem! Thank you so much for your help @cowtowncoder! |
@haskovec Glad to hear it works. I agree in that it is unfortunate this kink is there; JDK extension API that is required to get parameter names was only added in Java 8. And since for now (Jackson 2.x) JDK baseline is JDK 6 (for runtime; for compilation JDK 7 is needed) it is not possible to include functionality directly. But with Jackson 3.x we should be able to integrate this small part of functionality into core I wonder if there's a way to improve javadocs; maybe mention this for annotations or something. Anyway: thank you for reporting this; it might help others. |
If you create a Immutable class that you want to serialize out with Jackson and the output needs a different fieldName than the class has it will serialize it with the fieldName and not the name you passed in
@JsonProperty("")
meanwhile if you have the property annotated with@JsonProperty
it outputs the correct field name. Consider the following class:The class will be output as:
On the otherhand the following class will be serialized correctly:
With the output of:
I noticed in Debugging the code in
BeanSerializerFactory
that inremoveIgnorableTypes
it shows the values of the fields in the Constructor, however property.getAccessor() returns null which results in those @JsonProperty's being removed.I created a test program to quickly demonstrate the issue (I did it in Spring Boot since that is where we found the issue in production). You can just run
mvn test
to demonstrate the output of the classes:https://github.com/haskovec/jackson-serialization-bug
The text was updated successfully, but these errors were encountered: