Jankson icon indicating copy to clipboard operation
Jankson copied to clipboard

Support Java 14 Records

Open UpcraftLP opened this issue 4 years ago • 2 comments

Would it be possible to support java 14 record classes without needing to manually create type adapters?

Currently (as of Jankson 1.2.0), when trying to deserialize a record class via Jankson#fromJson(String, Class), it simply returns null.

Serialization however works just fine.

UpcraftLP avatar Apr 28 '21 04:04 UpcraftLP

Small, self-contained test case:

public record MyRecord(String value) {

	public static void main(String[] args) throws Exception {
		Jankson jankson = Jankson.builder().build();
		MyRecord testValue = new MyRecord("test");
		String serialized = jankson.toJson(testValue).toJson();
		System.out.println("Original: " + testValue); // MyRecord[value=test]
		System.out.println("Serialized: " + serialized); // { "value": "test" }
		System.out.println();
		
		MyRecord deserialized = jankson.fromJson(serialized, MyRecord.class);
		System.out.println("Original: " + testValue); // MyRecord[value=test]
		System.out.println("Deserialized: " + deserialized); // null
	}

}

UpcraftLP avatar Apr 28 '21 04:04 UpcraftLP

Thank you for the test case!

Records have definitely been on my mind lately.

I think we're going to have to have another breaking-changes version, because support requires some methods not available in java <14. Either that or some kind of live-detection mechanism that loads an extra class to handle it, which is a mess. I'll keep it in mind.

falkreon avatar May 02 '21 20:05 falkreon

I'm happy to report that this is finally addressed in 2.0 alpha 3! Records will automatically be created using their canonical constructors, and json types constrained to the record component types. I'm leaving #50 up till I have Comment and SerializedName fully dialed in

falkreon avatar Jun 27 '24 23:06 falkreon