users@jsonb-spec.java.net

[jsonb-spec users] [jsr367-experts] TypeInfo for wrapping generic types

From: Roman Grigoriadi <roman.grigoriadi_at_oracle.com>
Date: Wed, 27 Apr 2016 14:57:49 +0200

Hi experts,

how about to include a class for holding info about generic types in API:

public abstract class TypeInfo<T> {

     private final Typetype;
protected TypeInfo() {
       //type extracted from <T>
     }
public Type getType() {
         return type;
     }
}


Such helper is present in Gson as TypeToken and in Jackson as TypeReference.

Jsonb methods with "Type runtimeType" argument will than be interchanged
with "TypeInfo runtimeTypeInfo" arg.
For example:

<T>T fromJson(Reader reader, Type runtimeType)

<T>T fromJson(Reader reader, TypeInfo<T> runtimeType)

Returning <T> instance in second case is type safe, and eliminates need
of creation anonymous class instance (or class extension) of
(de)serialized a Java type.
For example:

List<MyPojo> result1 =jsonb.fromJson(jsonString,new TypeToken<List<MyPojo>>(){});

instead of

ArrayList<MyPojo>> result =jsonb.fromJson(jsonString,new ArrayList<MyPojo>>(){});

WDYT? Thanks, Roman