jsr367-experts@jsonb-spec.java.net

[jsr367-experts] Re: TypeInfo for wrapping generic types

From: Roman Grigoriadi <roman.grigoriadi_at_oracle.com>
Date: Wed, 27 Apr 2016 15:18:07 +0200

Another preference to use such token is elimination of instance creation
or extension. I thought that GenericType user may wish to deserialize
into may be havy for instantiation in contrast to such token.

For example what will you pass in method receiving Type for
deserialization in a result of Map<String, MyPojo> ?

Roman

On 04/27/2016 03:08 PM, Romain Manni-Bucau wrote:
> Hi Roman,
>
> think we spoke about it and decided to not do it cause it was already
> everywhere with different API (JAXRS -
> http://docs.oracle.com/javaee/7/api/javax/ws/rs/core/GenericType.html,
> CDI -
> http://docs.oracle.com/javaee/7/api/javax/enterprise/util/TypeLiteral.html,
> ...) and using Type is enough. Issue is you loose the type safety with
> Type (guess that's why you ask).
>
> I know JSR 250 will get another release, would be awesome if we could
> get in touch with this spec (and maybe EE/CDI/JAXRS) to get a common
> annotation for that.
>
> If we can I'm clearly +1 otherwise I think implementations can provide
> this on top of Type until we get such an API.
>
> Does it make sense? I can ping CDI list about that if you want.
>
>
>
>
> Romain Manni-Bucau
> @rmannibucau
> http://www.tomitribe.com
> http://rmannibucau.wordpress.com
> https://github.com/rmannibucau
>
> 2016-04-27 14:57 GMT+02:00 Roman Grigoriadi
> <roman.grigoriadi_at_oracle.com <mailto:roman.grigoriadi_at_oracle.com>>:
>
> 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
>
>