users@jsonb-spec.java.net

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

From: <dmitry.kornilov_at_oracle.com>
Date: Fri, 6 Nov 2015 15:28:34 +0000 (UTC)

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