Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not cope with Optional.empty() arguments in @JsonbCreator method/constructor #276

Open
misl opened this issue Jun 24, 2019 · 1 comment

Comments

@misl
Copy link

misl commented Jun 24, 2019

Just like in #237, I also like to stick to immutable pojos. However I tried a different approach using Optional<> fields. In comparisons like this on with other Json serialization frameworks (Jackson and/or Gson) it is stated that JSON-B has improved handling of Optional<> fields.

However the examples mostly show only serialization works. How about deserialization optional fields. Especially when using immutable pojos. In the following example I used a contructor with Optional<> arguments:

import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.json.bind.annotation.JsonbCreator;
import javax.json.bind.annotation.JsonbProperty;
import java.util.Optional;

public class OptionalInConstructor {

  public static class MyModel {
    private final String mandatory;
    private final Optional<String> optional;

    @JsonbCreator
    public MyModel( @JsonbProperty("mandatory") String mandatory, @JsonbProperty("optional") Optional<String> optional ) {
      this.mandatory = mandatory;
      this.optional = optional;
    }

    @JsonbProperty("mandatory")
    public String getMandatory() {
      return mandatory;
    }

    @JsonbProperty("optional")
    public Optional<String> getOptional() {
      return optional;
    }
  }

  public static void main( String[] args ) {
    MyModel model = new MyModel( "mandatory", Optional.empty() );

    // From Object -->Json
    Jsonb jsonb = JsonbBuilder.create();
    String serializedModel = jsonb.toJson( model );
    System.out.println( "Json: " + serializedModel );

    // From Json --> Object
    MyModel deserialized = jsonb.fromJson( serializedModel, MyModel.class );
  }
}

Unfortunately this lead to the following error:

Json: {"mandatory":"mandatory"}
Jun 24, 2019 11:29:14 PM org.eclipse.yasson.internal.Unmarshaller deserializeItem
SEVERE: JsonbCreator parameter optional is missing in json document.
Exception in thread "main" javax.json.bind.JsonbException: JsonbCreator parameter optional is missing in json document.
	at org.eclipse.yasson.internal.serializer.ObjectDeserializer.createInstance(ObjectDeserializer.java:120)
	at org.eclipse.yasson.internal.serializer.ObjectDeserializer.getInstance(ObjectDeserializer.java:93)
	at org.eclipse.yasson.internal.serializer.AbstractContainerDeserializer.deserialize(AbstractContainerDeserializer.java:61)
	at org.eclipse.yasson.internal.Unmarshaller.deserializeItem(Unmarshaller.java:70)
	at org.eclipse.yasson.internal.Unmarshaller.deserialize(Unmarshaller.java:56)
	at org.eclipse.yasson.internal.JsonBinding.deserialize(JsonBinding.java:53)
	at org.eclipse.yasson.internal.JsonBinding.fromJson(JsonBinding.java:60)
	at OptionalInConstructor.main(OptionalInConstructor.java:39)

When the Optional<> argument is present it works correctly.

Should this be possible? Or is there another way (without making my pojo mutable) to achieve this?

@misl misl changed the title Can not use Optional<> arguments in @JsonbCreator method/constructor Can not copy with Optional.empty() arguments in @JsonbCreator method/constructor Jun 25, 2019
@misl misl changed the title Can not copy with Optional.empty() arguments in @JsonbCreator method/constructor Can not cope with Optional.empty() arguments in @JsonbCreator method/constructor Jun 25, 2019
@Degubi
Copy link
Contributor

Degubi commented Jul 10, 2023

It seems the given example code is working on master, printing out deserialized.getOptional() correctly prints Optional.empty

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants