users@jsonb-spec.java.net

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

From: Romain Manni-Bucau <rmannibucau_at_tomitribe.com>
Date: Wed, 27 Apr 2016 15:08:22 +0200

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>:

> Hi experts,
>
> how about to include a class for holding info about generic types in API:
>
> public abstract class TypeInfo<T> {
>
> private final Type type; 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
>