jakartaee / jsonb-api

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

@JsonbTypeDeserializer and @JsonbTypeSerializer don't work in the fields #235

Closed lusabo closed 4 years ago

lusabo commented 4 years ago

I'm in an activity of exchanging Jackson for Jsob-B and I am having a problem on use @JsonbTypeDeserializer and @JsonbTypeSerializer as told below.

I have an entity like this:

 public class User implements Serializable {

  private static final long serialVersionUID = 1L;

  private String login;

  @JsonbTypeDeserializer(Decrypting.class)
  @JsonbTypeSerializer(Encrypting.class)
  private String password;

  // getters and setter
}

And one test like this:

  @Test
  public void whenDeserializingUsingJsonbTypeDeserializer() throws IOException {

    String json = "{\"login\":\"admin\", \"password\":\"yfv_ntl3_Nbrv0139tDwRQ\"}";

    Jsonb jsonb = JsonbBuilder.create();

    User user = jsonb.fromJson(json, User.class);

    Assert.assertEquals(user.getPassword(), "test-string");
  }

When running the test, the Decryption class is trying to deserialize the login field, but, as you can see, only the password field has the annotations.

Any way to fix it?

aguibert commented 4 years ago

hi @lusabo, thanks for raising this issue. Can you please include the code for the Encrypting and Decrypting classes referenced in the annotations?

lusabo commented 4 years ago

Hi @aguibert , follow the codes:

public class Decrypting implements JsonbDeserializer<String> {

  @Override
  public String deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext, final Type type) {
    String encryptedValue = jsonParser.getString();
    try {
      return EncryptDecryptUtil.decrypt(encryptedValue);
    } catch (GeneralSecurityException e) {
      throw new IllegalArgumentException("Could not decrypt value", e);
    }
  }
}
public class Encrypting implements JsonbSerializer<String> {

    @Override
    public void serialize(final String decryptedValue, final JsonGenerator jsonGenerator, final SerializationContext serializationContext) {
        try {
            jsonGenerator.write(EncryptDecryptUtil.encrypt(decryptedValue));
        } catch (GeneralSecurityException e) {
            throw new IllegalArgumentException("Could not encrypt value", e);
        }
    }
}
aguibert commented 4 years ago

ok, everything appears to be written correctly. I suspect it is an implementation bug -- which JSON-B impl are you using? (probably Yasson or Johnzon)

lusabo commented 4 years ago

Yasson. After update for new versions, I had a problem with JsonbConfigProperties.FAIL_ON_UNKNOWN_PROPERTIES that I switched for YassonConfig.FAIL_ON_UNKNOWN_PROPERTIES and now everything works fine.

<dependency>
        <groupId>jakarta.json.bind</groupId>
        <artifactId>jakarta.json.bind-api</artifactId>
        <version>1.0.2</version>
</dependency>
<dependency>
        <groupId>org.eclipse</groupId>
        <artifactId>yasson</artifactId>
        <version>1.0.7</version>
</dependency>