Hi Experts,
I would like to open discussion about Generic Type Support in JSON Binding.
The initial proposal is to add two methods to Jsonb interface:
public <T> T fromJson(String str, java.lang.reflect.Type runtimeType)
throws JsonbException;
public String toJson(Object object, java.lang.reflect.Type runtimeType)
throws JsonbException;
(and analogically other methods with different input/output)
Based on reviewing existing options, it does not appear there is a single
optimal option.
Reasoning behind java.lang.reflect.Type.
There are several ways how to deal with generics:
1. To use java.lang.reflect.Type
2. Use Type Literal (e.g. GenericType ...)
3. Use Generic Type Builder
It would be a lot of unnecessary code to build Generic Type Builder. And I
think this should be not part of JSON Binding.
Type Literal may work in some cases, but generally it doesn't work (in
infinite cases). It is also not backed by Java Language Specification and
may not work in the future. For example, Type Literal doesn't work when
used with lambdas or with some JVM languages different than Java. I don't
want to introduce yet another GenericType class and to duplicate code with
some other specifications.
Use of java.lang.reflect.Type seems like the best choice from bad choices
available. Integration with JAX-RS will be flawless, it is upon the user to
build java.lang.reflect.Type (hopefully with the help of JSON Binding
implementation). User can use already available Type Literal classes
available in the Java ecosystem.
Examples and pdf proposal are available at
https://java.net/projects/jsonb-spec/sources/git/content/spec/spec.pdf?rev=da7db533076856699cec49a4eebd300b9f4a7230
https://github.com/json-binding/spec/blob/default_mapping/examples/runtime/src/main/java/examples/mapping/DefaultMappingGenerics.java
Looking forward to your feedback.
MartinV