On Sun, Sep 12, 2021 at 1:30 AM <[email protected]> wrote: > > On 2021-09-11T21:19:15 -0700 > Tatu Saloranta <[email protected]> wrote: > > > > The main (or initial?) problem here is just that type LocationID is > > taken to be a POJO with properties (since Records by definition are), > > instead of something behaving like String value. The issue is that > > POJOs cannot be serialized as attributes but only as elements. > > In theory it is possible to annotate Records to work like Strings, > > too, but it might be easier to first try to create equivalent "simple" > > pojo with > > something like: > > > > static class LocationId { > > private UUID id; > > > > @JsonCreator(mode = JsonCreator.Mode.DELEGATING) > > public LocationId(UUID id) { this.id = id; } > > > > @JsonValue // method name can be anything > > protected UUID id() { return id; } > > } > > > > which should then serialize as a basic String value, and deserialized > > similarly from a String value. > > It works! Thank you!
Great! I am happy this is the case & thank you for verifying and helping others possibly find it too. > Will records get some kind of special handling post JDK 17? I often use > these kind of wrappers for extra type safety, and it's a shame that > they can't be expressed as records in this case. Jackson 2.12 does support Record types, actually, but the challenge is in how to annotate them with `@JsonValue` and `@JsonCreator`. Adding `@JsonCreator` is possible if you explicitly add the 1-argument constructor, but that is obviously kind of code Records are meant to help eliminate. Similarly `@JsonValue` would be possible to add if you add an explicit accessor. In a way... Hmmh. I am almost tempted to consider specific kind of new annotation that would essentially allow marking this style of 1-field Record as "wrapper" record. I think that'd be perfectly doable. Could I ask you to file an issue for jackson-databind, to request such annotation (annotation goes in jackson-annotations, but handling code is all databind), if that makes sense to you? Perhaps I could even think of how to extend handling to POJOs... but start with Records for sure. -+ Tatu +- -- You received this message because you are subscribed to the Google Groups "jackson-user" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/jackson-user/CAL4a10jPiObLy4Mjsc7ud3-HWtHVdoKv%2BVix%2Bp56EpG_CuyBEw%40mail.gmail.com.
