jsr367-experts@jsonb-spec.java.net

[jsr367-experts] Re: java 8 Optionals

From: Otávio Gonçalves de Santana <otaviopolianasantana_at_gmail.com>
Date: Mon, 8 Feb 2016 05:55:49 -0200

I agree with Romain, another problem is Optional is a reference, so
Optional can be null and the Optional in Java 8 does not implement
Serializable.

On Sat, Jan 30, 2016 at 3:22 PM, Romain Manni-Bucau <
rmannibucau_at_tomitribe.com> wrote:

> Hi Roman
>
> Since we dont know in which context it is used I'd say null is a safer
> default since Optional is poorly ontegrated with most frameworks but having
> a global and local flag to switch would be nice.
> Le 30 janv. 2016 18:11, "Roman Grigoriadi" <roman.grigoriadi_at_oracle.com>
> a écrit :
>
>> Hi experts.
>>
>> I have a question about marshalling / unmarshalling java 8 Optionals.
>> Dmitry already made some suggestion about optionals, but lets elaborate a
>> bit on them. They are a bit of special case. Logically Optional fields are
>> not intended to be null, but technically it is possible. It makes some
>> impact on marshalling / unmarshalling considerations.
>>
>> Basic question is: Should empty Optionals be skipped during serialization
>> or serialized as keys with null values? Should optional properties honor
>> @JsonbNillable configuration?
>>
>> Given a class:
>>
>> @JsonbNillable(true|false)
>> class Pojo {
>> Optional<Component> pojoComponent;
>> String strField;
>> }
>>
>> 1. Consider empty Optionals are serialized as missed in a JSON document
>> and we also honor @JsonbNillable setting.
>>
>> If @JsonbNillable is set to true all is nice and easy:
>>
>> Pojo pojo = new Pojo();
>> pojo.pojoComponent = Optional.empty(); //this have actually no effect on
>> JSON document result
>> jsonb.toJson(pojo);
>> //Will produce {"pojoComponent":null,"strField":null} JSON.
>> During unmarshalling of such JSON document we will set pojoComponent to
>> Optional.empty()
>>
>>
>> If @JsonbNillable is not set or set to false we will result with JSON:
>>
>> Pojo pojo = new Pojo();
>> jsonb.toJson(pojo);
>> //Will produce "{}" JSON.
>>
>> pojo.pojoComponent = Optional.empty();//no effect
>> jsonb.toJson(pojo);
>> //Will produce "{}" JSON again.
>>
>> Now when we will unmarshall such JSON document with missing keys for
>> Optionals, we don't know if they were actually Optional empty or null.
>> Furthermore, our JSONB Unmarshaller is driven by JSONP parser events, and I
>> will get no event for any keys that are missing in document. So if I would
>> like to set all Optional fields with null value to Optional.empty() (which
>> makes most sense) it would have to be done by some kind of postprocessing.
>>
>> Things get even more complicated considering Arrays of Optionals. Array
>> of Optionals of size 3 with either: 3 null values or 3 empty Optionals
>> always have to be serialized as [null,null,null] in JSON document. It is
>> inconsistent according to serializing Optional properties.
>>
>> Another option is not to honor @JsonbNillable setting and serialize null
>> properties of Optionals as missing in JSON document and Optional.empty()
>> values as JSON keys with null values. This will be consistent with Optional
>> Arrays and we will explicitly know when and where to set Optional.emtpy()
>> during unmarshalling, but it will be inconsistent with marshalling non
>> Optional object properties set to null with @JsonbNillable set to false
>> (default).
>>
>> Do you have any considerations about this?
>>
>> Thank you,
>> Roman
>>
>>
>>
>>
>>
>>
>>


-- 
Otávio Gonçalves de Santana
twitter: http://twitter.com/otaviojava
site:     *http://about.me/otaviojava <http://about.me/otaviojava>*