Replies: 9 comments 2 replies
-
please provide the version of Jackson - there were big changes in the code for Record support in Jackson 2.15.0 |
Beta Was this translation helpful? Give feedback.
-
I tested on Jackson 2.15.0 |
Beta Was this translation helpful? Give feedback.
-
possibly down to this - https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getDeclaredFields-- says 'not sorted' |
Beta Was this translation helpful? Give feedback.
-
That possibly answers it. Thanks! |
Beta Was this translation helpful? Give feedback.
-
I tested this and it does seem to be an issue - the order does appear to be based on the record definition when no JsonProperty annotations are used. I'll turn this into an issue. |
Beta Was this translation helpful? Give feedback.
-
Serialization uses either field or method, the order of the entries returned when calling You may get the same order as declared in the source code when running using JRE/JDK A, it can be different when using JRE/JDK B. |
Beta Was this translation helpful? Give feedback.
-
Phew, I thought this was caused by #3724, but that's not the case. The JSON produced is different because the setup weren't actually equivalent:
...and the output (ie field ordering) would be the same as Record's: {
"all is fine!": {
"id": "2",
"passport": "111110"
},
"id": "1",
"name": "[email protected]"
} |
Beta Was this translation helpful? Give feedback.
-
Also, the Record's equivalent of: public class NestedClassOne {
private String id;
private String name;
@JsonProperty(value = "all is fine!")
private NestedClassTwo nestedClassTwo;
// All args constructor + getters + setters
} ...is: public record NestedRecordOne(
String id,
String email,
NestedRecordTwo nestedRecordTwo) {
@JsonProperty(value = "yikes!") // Even though it's on getter, this name will also be used during deserialization
@Override
public NestedRecordTwo nestedRecordTwo() {
return nestedRecordTwo;
}
} ...which will produce: {
"id": "1",
"email": "[email protected]",
"yikes!": {
"id": "2",
"passport": "111110"
}
} |
Beta Was this translation helpful? Give feedback.
-
Quick note: due to major property introspection rewrite (#4515) it would be possible to revisit this issue too, if that was necessary. |
Beta Was this translation helpful? Give feedback.
-
Hi, this is a trivial case that I came across. Using nested records changes the order of the properties in the serialized output. Here's a simple example:
The output is:
Performing the same test on classes seems to work fine:
Produces:
This could be easily fixed by using
@JsonPropertyOrder
. I wonder why it behaves this way for records. I didn't think this warranted creating an issue.Beta Was this translation helpful? Give feedback.
All reactions