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

Unexpected behaviour from @JsonbTypeAdapter when marking a field #57

Closed
readlearncode opened this Issue Oct 16, 2017 · 0 comments

Comments

Projects
None yet
3 participants
@readlearncode

readlearncode commented Oct 16, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment