jakartaee / jsonb-api

Jakarta JSON Binding
https://eclipse-ee4j.github.io/jsonb-api/
Other
79 stars 39 forks source link

Allow Jsonb.toJson() to take a JsonGenerator #280

Closed kalgon closed 3 years ago

kalgon commented 3 years ago

I was writing an implementation of JsonGenerator which prevents writing empty objects (like a non-null bean whose properties are all null and ends up being written as {}) and noticed that I could not give my JsonGenerator to Jsonb.toJson() which only takes Writer or OutputStream. So could it be possible to add void toJson(Object, JsonGenerator) and void toJson(Object, Type, JsonGenerator) to Jsonb? For the sake of symmetry, consider adding <T> T fromJson(JsonParser, Class<T>) and <T> T fromJson(JsonParser, Type) too...

rmannibucau commented 3 years ago

Hi,

I get where you are coming from but have to admit that I'm mixed about this one because current API enables to use JsonReader/JsonWriter and does not require JsonGenerator/JsonParser to be used - which is very good and enables/simplifies the adoption as a portable API whereas otherwise the entry cost will increase a lot and the API will become quickly closer to an implementation (or be as rigid as an impl to be fair).

Think a workaround, as of today, is to inject the JsonProvider in JsonbBuilder (https://github.com/eclipse-ee4j/jsonb-api/blob/master/api/src/main/java/jakarta/json/bind/JsonbBuilder.java#L58). You will not get any guarantee about the usage of the API but it will enable you to wrap them all and reach your goal more portably.

kalgon commented 3 years ago

OK, I'll wrap the JsonProvider instead. Thanks!