users@jsonb-spec.java.net

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

From: Eugen Cepoi <cepoi.eugen_at_gmail.com>
Date: Wed, 27 Apr 2016 10:24:40 -0700

Yeah I know but having a type token pop in every jsr/component of java ee
wouldn't be serious...


2016-04-27 10:17 GMT-07:00 Romain Manni-Bucau <rmannibucau_at_tomitribe.com>:

> Such an API is great cause you keep the return type typed vs Type usage
> which goes back on object cause of type erasure but if EE can provide a
> common annotation I'm to rely on it. If not Type is good enough for jsonb 1.
> Le 27 avr. 2016 19:06, "Eugen Cepoi" <cepoi.eugen_at_gmail.com> a écrit :
>
>> Yeah we already discussed it. If the user needs access to an instance of
>> java.lang.reflect.Type it can get it from elsewhere.
>>
>> Romain, are you saying that you want to provide an API with auto
>> discovery to create generic types?
>>
>> 2016-04-27 6:33 GMT-07:00 Romain Manni-Bucau <rmannibucau_at_tomitribe.com>:
>>
>>> Not sure I fully got it but the idea was for vendors to provide an
>>> extension api/module with an implementation of ParameterizedType: new
>>> VendorParameterizedType<Map<String, MyPojo>>(Map.class, new Type[] {
>>> String.class, Mypojo.class }) // or same using reflection to get the types
>>> but it is slower and has some pitfalls as seen in CDI API.
>>>
>>> If we can get such API (with auto or not discovery) in JSR 250 we are
>>> very good IMO
>>>
>>>
>>> Romain Manni-Bucau
>>> @rmannibucau
>>> http://www.tomitribe.com
>>> http://rmannibucau.wordpress.com
>>> https://github.com/rmannibucau
>>>
>>> 2016-04-27 15:18 GMT+02:00 Roman Grigoriadi <roman.grigoriadi_at_oracle.com
>>> >:
>>>
>>>> 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>
>>>> 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
>>>>>
>>>>
>>>>
>>>>
>>>
>>