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

Unexpected behaviour from @JsonbTypeAdapter when marking a field #64

Closed
jsonbrobot opened this issue Oct 15, 2017 · 3 comments
Closed

Comments

@jsonbrobot
Copy link

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

@jsonbrobot
Copy link
Author

@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.

@jsonbrobot
Copy link
Author

@readlearncode Commented
opps sorry moved to eclipse-ee4j/yasson#57

@jsonbrobot
Copy link
Author

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

1 participant