jsonb-api icon indicating copy to clipboard operation
jsonb-api copied to clipboard

Add support for Java Record

Open rdebusscher opened this issue 3 years ago • 45 comments

Currently Records need to have specific names for the attributes before they are picked up.

public record Person (String name, Integer age)

results in an empty JSON construct due to the rules around what properties are picked up.

You can workaround the issue by defining

public record Person (String getName, Integer getAge)

The specification could be improved by adding a section that describes how Record attributes are treated.

rdebusscher avatar Jun 16 '21 07:06 rdebusscher

This all depends on the Java SE versions that are supported by the spec.

rdebusscher avatar Jun 16 '21 07:06 rdebusscher

+1, it is already supported by Apache Johnzon and even if JSON-B only targets Java 8/11, we can mention how records would be handled (constructor with most parameters and accessor methods for ex).

rmannibucau avatar Jun 16 '21 07:06 rmannibucau

most annotations can now be put on parameter (so inline record constructor) and jsonbcreators don't fail when null is passed so looks like records are supported if we relax the getter naming for serialization for this type of class.

rmannibucau avatar Nov 16 '21 10:11 rmannibucau

I would love to see records support in JSON-B that just works out-of-the-box (i. e. without any tricks). This includes not only serialization (= uses non-getter-syntax for accessor methods), but also for deserialization (= uses constructor to inject values). I assume this is what most users of records would expect from JSON-B.

Are there any actual plans to mandate that in the spec?

mkarg avatar Apr 27 '22 12:04 mkarg

Yes. When Jakarta Platform officially supports Java 14 where records were introduced.

m0mus avatar Apr 28 '22 08:04 m0mus

So we all should vote for a JRE 14+ minimum level for EE 11 at the Jakarta EE Steering Committee.

mkarg avatar May 16 '22 09:05 mkarg

So it means java 17 and it would probably be bad for JakartaEE since 1 or 2 more versions will need to keep Java 11 compatibility. @m0mus what does prevent to define record compatibility as an extension of the spec (often done as appendix in spec)? Behavior is quite straight forward to define and saying it is only supported if java version supports records is more than fine for impls as shown by the current implementations IMHO and it would enable users to use it before the spec baseline upgrades to java 17. Is it an option?

rmannibucau avatar May 16 '22 10:05 rmannibucau

@mkarg Yes, I think we should move Jakarta 11 to Java 17+. I very much doubt that it can be released before next September, so Java 21 is also an option. @rmannibucau Yes, it's another option. I would go this way if Jakarta 11 won't adopt Java 14+.

m0mus avatar May 16 '22 10:05 m0mus

Another interesting aspect would be, how to place annotations to modify property names for serialization...? An idea could be to re-interpret the annotations found at the constructor's arguments, hence use those not just for deserialization but also for serialization.

mkarg avatar May 20 '22 22:05 mkarg

It is what was done at johnzon. Indeed ypu could use setters for deserialization too - note it is not only property name but adapter/deserializer, format, ... too - but it is not convenient at all so constructor params is the best option IMHO.

rmannibucau avatar May 21 '22 07:05 rmannibucau

It seems that Yasson supports records already, though I have not tested it yet. https://github.com/eclipse-ee4j/yasson/issues/495 https://github.com/eclipse-ee4j/yasson/pull/499 https://github.com/eclipse-ee4j/yasson/issues/528

nlisker avatar Jun 27 '22 12:06 nlisker

Link looks like a partial not ootb support, if true can be better to not need a custom visibility strat. Anyway records model is as defined as pojo ones so look straight forward and since all impl support it, it is just a matter of doing the pr, there is no issue in the spec to support a more recent version of java while baseline is still there.

rmannibucau avatar Jun 29 '22 12:06 rmannibucau

What is the current status of this issue?

mkarg avatar Apr 29 '23 20:04 mkarg

Again: What is the current status of this issue?

mkarg avatar Mar 25 '24 08:03 mkarg