jansupol / jsonbapi

0 stars 0 forks source link

Unexpected behaviour from @JsonbTypeAdapter when marking a field #64

Closed jansupol closed 6 years ago

jansupol commented 6 years ago

Changing the order of the fields in a Java Bean changes the behaviour of JsonbAdapter.

Given the JsonbAdapter :

public class FirstNameAdapter implements JsonbAdapter<String, JsonValue> {
    @Override
    public JsonValue adaptToJson(String firstName) {
        return Json.createValue(firstName.subSequence(0,1).toString());
    }
    @Override
    public String adaptFromJson(JsonValue json) {
        return json.toString();
    }
}

And the Java Bean:

public class Author {
    @JsonbTypeAdapter(FirstNameAdapter.class)
    private String firstName;
    private String lastName;
    // plumbing code omitted
}

The adaptToJson() method is called twice, once for the firstName field and again for the lastName field. Change the order of the fields as follows:

public class Author {
    private String lastName;   
    @JsonbTypeAdapter(FirstNameAdapter.class)
    private String firstName;
    // plumbing code omitted
}

and only the firstName field is passed to the adaptToJson() method.

Code is here: https://github.com/readlearncode/Java-EE-8-Sampler/tree/master/json-b-1-0/src/main/java/com/readlearncode/devWorks/part2/adaptors

Unit tests here: https://github.com/readlearncode/Java-EE-8-Sampler/tree/master/json-b-1-0/src/test/java/com/readlearncode/devWorks/part2/adapter

jansupol commented 6 years ago
jansupol commented 6 years ago

@m0mus Commented Looks like a bug in Yasson. Can you please move it to Yasson issues tracker here https://github.com/eclipse/yasson/issues and we will start working on it.

jansupol commented 6 years ago

@readlearncode Commented opps sorry moved to https://github.com/eclipse/yasson/issues/57