users@jsonb-spec.java.net

[jsonb-spec users] [jsr367-experts] Re: Generics support on serialization

From: Romain Manni-Bucau <rmannibucau_at_tomitribe.com>
Date: Fri, 6 Nov 2015 08:30:46 -0800

Hi Dmitry,

I see few points keeping it this way even if it doesn't bring much yet:

- this is symmetric with deserialisation and doesn't prevent us to
provide this value when we will need a context for converters -
otherwise we would break the compatibility. This will likely be needed
for complex json object graphes
- this allows a "view" oriented serialization: toJson(listOfDogs,
List<Animal>) if desired without having to implement any particular
API
- this is compatible with proxy oriented frameworks (CDI, Spring,
AspectJ, Hibernate, OpenJPA, ....) otherwise you would serialize the
proxy fields

Interesting point is we could provide methods without this type *as
well* but I would keep them to let the user select the model to use.




Romain Manni-Bucau
@rmannibucau
http://www.tomitribe.com
http://rmannibucau.wordpress.com
https://github.com/rmannibucau


2015-11-06 7:28 GMT-08:00 <dmitry.kornilov_at_oracle.com>:
> Hi Experts,
>
> I am working on JSON-B reference implementation at the moment and want
> to clarify one moment. We have three Jsonb::toJson(...) methods
> accepting Type parameter:
>
> String toJson(Object object, Type runtimeType) throws JsonbException;
> void toJson(Object object, Type runtimeType, Appendable appendable)
> throws JsonbException;
> void toJson(Object object, Type runtimeType, OutputStream stream)
> throws JsonbException;
>
> I am not convinced why these methods are needed. Raw type is enough for
> serialization. For deserialization this parameter is obviously
> required.
>
> There is an example in DefaultMappingGenerics class which covers this
> case (see below). It can be (and is) perfectly processed without
> knowing a generic type. So this is a bad sample.
>
> List<java.util.Optional<String>> expected =
> Arrays.asList(Optional.empty(), Optional.ofNullable("first"),
> Optional.of("second"));
> String json = jsonb.toJson(expected,
> DefaultMappingGenerics.class.getField("listOfOptionalStringField").getG
> enericType());
> assertEquals("[null,\"first\",\"second\"]",json);
>
> Is there any real world sample showing that toJson(Object object) is
> not enough and toJson(Object object, Type runtimeType) has to be used.
>
> Thanks,
> Dmitry Kornilov