David Blevins created JOHNZON-418:
-------------------------------------
Summary: Inconsistent Precedence with JsonbTypeAdapter
Key: JOHNZON-418
URL: https://issues.apache.org/jira/browse/JOHNZON-418
Project: Johnzon
Issue Type: Bug
Components: JSON-B
Affects Versions: 2.0.1
Reporter: David Blevins
In the scenario where there are competing {{JsonbTypeAdapter}} specified in 1)
the {{JsonbConfig}} and 2) on the class itself, the behavior is not consistent
in all scenarios.
We have an adapter specified on Email as follows and will attempt to override
this via {{JsonbConfig}}
{code:java}
@JsonbTypeAdapter(Adapter.EmailClass.class)
public static class Email {
final String user;
final String domain;
//... {code}
h2. Writing Email instance
When Email is written as a json document, the {{Adapter.Config}} specified via
{{JsonbConfig}} will win. The {{Adapter.EmailClass}} on {{Email}} will lose.
{code:java}
final Jsonb jsonb = JsonbBuilder.create(new JsonbConfig().withAdapters(new
Adapter.Config()));
final Email email = new Email("test", "domain.com");
final String json = jsonb.toJson(email);
assertEquals("\"[email protected]:Config.adaptToJson\"", json);
assertEquals("Config.adaptToJson", calls());{code}
This behavior is consistent with how {{JsonbTypeSerializer}} and
{{JsonbTypeDeserializer}} are handled.
h2. Writing an Email field
When writing an object where Email is a field, the {{Adapter.EmailClass}} on
{{Email}} will win. The {{Adapter.Config}} specified via {{JsonbConfig}} will
lose.
{code:java}
final Jsonb jsonb = JsonbBuilder.create(new JsonbConfig().withAdapters(new
Adapter.Config()));
final Email email = new Email("test", "domain.com");
final Contact contact = new Contact();
contact.setEmail(email);
reset();
final String json = jsonb.toJson(contact);
assertEquals("{\"email\":\"[email protected]:EmailClass.adaptToJson\"}", json);
assertEquals("Contact.getEmail\n" +
"EmailClass.adaptToJson", calls());{code}
Where {{Email}} is a field or property of {{Contact}}
{code:java}
public static class Contact {
private Email email;
// normal non-annotated getter/setter ...
}
{code}
This behavior is inconsistent with the write behavior.
This behavior is inconsistent with read and write behavior of
{{JsonbTypeSerializer}} and {{{}JsonbTypeDeserializer{}}}.
This behavior also makes it impossible to override behavior via configuration
in any scenario.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)