-
-
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
Support @JsonCreator
annotation on record classes
#3180
Comments
@markelliot You can do this:
BTW, how can this even work in
Using that, I'm getting:
For it to work, I need to move the
UPDATE: Apparently this one is a known issue: #3063d. |
I think the specific minor version for my example was 2.12.2, though I'm unsure if the minor version made a difference in behavior. My example was extracted from: Where the version of Jackson is specified in: |
@markelliot OK thanks I think I've satisfied my curiousity: a quick scan through the codebase seems to hint that BTW does the solution I suggested in the previous comment satisfies this request, i.e.:
But after looking at Watts.java, maybe you'd want this instead to replace the two factory methods:
|
Thanks -- I'll take your advice on reducing the total boilerplate.
I think it'd be more ergonomic for Jackson to have better default handling of record classes, such as functioning with sane defaults using minimal (or no) annotations: public record Watts(@JsonValue int value) {} |
@markelliot Took me a while to understand intent here but I guess I can see it. So the assumption is that use of This may be tricky to pull off, but would also, I think, apply to equivalent non-record POJO case. I will file a separate issue since it's both more specific and more general than this issue. |
So starting from Option 1
Option 2
Observation: Parameter of type |
…x JsonValue annotation, for FasterXML#3180.
…x JsonValue annotation, for FasterXML#3180.
@yihtserns Right, Come to think of that, the problem with |
Using Jackson 2.12.
One common pattern in code on my teams is to use 'alias' types to decorate otherwise primitive value types in APIs and/or for dimensional units to gain compiler support for detecting otherwise subtle programming errors (such as using the wrong unit or passing parameters in the wrong order).
Today, to do this with Java records, we need to write classes like
Without the
@JsonCreator
annotated static method (or similarly trivial but verbose constructor), Jackson produces the errorIf we could place
@JsonCreator
on record classes and infer use of the single argument default constructor I think it'd cut the boilerplate even more. Alternatively, or in addition, it might be interesting to infer that a record class with a single labeled@JsonValue
parameter should use the default constructor, allowing us to write either:or
The text was updated successfully, but these errors were encountered: