[
https://issues.apache.org/jira/browse/JOHNZON-417?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
David Blevins updated JOHNZON-417:
----------------------------------
Description:
Attempting to read class Email as follows
{code:java}
final Jsonb jsonb = JsonbBuilder.create(new JsonbConfig().withAdapters(new
Adapter.Config()));
final String json = "\{\"user\":\"test\",\"domain\":\"domain.com\"}";
final Email email = jsonb.fromJson(json, Email.class);{code}
and adapter code
{code:java}
public static class Config implements JsonbAdapter<Email, Map<String, Object>> {
@Override
public Map<String, Object> adaptToJson(final Email obj) {
final LinkedHashMap<String, Object> map = new LinkedHashMap<>();
map.put("user", obj.user);
map.put("domain", obj.domain);
return map;
}
@Override
public Email adaptFromJson(final Map<String, Object> map) {
return new Email(map.get("user").toString(),
map.get("domain").toString());
}{code}
fails and the JsonbAdapter specified in the JsonbConfig will not be invoked
{code:java}
public static class Email {
private final String user;
private final String domain;
public Email(final String user, final String domain) {
//..
}
//... {code}
will fail with exception
{code:java}
jakarta.json.bind.JsonbException: Email has no suitable constructor or factory.
Cannot deserialize json object value: {"user":"test","domain":"domain.com"}
Use Johnzon @ConstructorProperties or @JsonbCreator if constructor arguments
are needed
class org.apache.johnzon.jsonb.symmetry.adapter.map.MapAdapterOnClassTest$Email
not instantiable{code}
This will fail for write operations for either List or Map, however, this will
work for write operations.
This will work for read and write if the {{JsonbAdapter}} adapts a {{String}}.
was:
Attempting to serialize or write class Email as follows
{code:java}
final Jsonb jsonb = JsonbBuilder.create(new JsonbConfig().withAdapters(new
Adapter.Config()));
final String json = "\{\"user\":\"test\",\"domain\":\"domain.com\"}";
final Email email = jsonb.fromJson(json, Email.class);{code}
and adapter code
{code:java}
public static class Config implements JsonbAdapter<Email, Map<String, Object>> {
@Override
public Map<String, Object> adaptToJson(final Email obj) {
final LinkedHashMap<String, Object> map = new LinkedHashMap<>();
map.put("user", obj.user);
map.put("domain", obj.domain);
return map;
}
@Override
public Email adaptFromJson(final Map<String, Object> map) {
return new Email(map.get("user").toString(),
map.get("domain").toString());
}{code}
fails and the JsonbAdapter specified in the JsonbConfig will not be invoked
{code:java}
public static class Email {
private final String user;
private final String domain;
public Email(final String user, final String domain) {
//..
}
//... {code}
will fail with exception
{code:java}
jakarta.json.bind.JsonbException: Email has no suitable constructor or factory.
Cannot deserialize json object value: {"user":"test","domain":"domain.com"}
Use Johnzon @ConstructorProperties or @JsonbCreator if constructor arguments
are needed
class org.apache.johnzon.jsonb.symmetry.adapter.map.MapAdapterOnClassTest$Email
not instantiable{code}
if the {{Email}} class is annotated with {{@JsonbTypeAdapter}} the error will
be as indicated in JOHNZON-416
This this will work for a write if the adapter operates on a String or List and
the {{Adapter.Config}} specified in the configuration will be used.
> Read fail with Map or List JsonbTypeAdapter specified via JsonbConfig
> ---------------------------------------------------------------------
>
> Key: JOHNZON-417
> URL: https://issues.apache.org/jira/browse/JOHNZON-417
> Project: Johnzon
> Issue Type: Bug
> Components: JSON-B
> Affects Versions: 2.0.1
> Reporter: David Blevins
> Priority: Major
>
> Attempting to read class Email as follows
>
> {code:java}
> final Jsonb jsonb = JsonbBuilder.create(new JsonbConfig().withAdapters(new
> Adapter.Config()));
> final String json = "\{\"user\":\"test\",\"domain\":\"domain.com\"}";
> final Email email = jsonb.fromJson(json, Email.class);{code}
> and adapter code
> {code:java}
> public static class Config implements JsonbAdapter<Email, Map<String,
> Object>> {
> @Override
> public Map<String, Object> adaptToJson(final Email obj) {
> final LinkedHashMap<String, Object> map = new LinkedHashMap<>();
> map.put("user", obj.user);
> map.put("domain", obj.domain);
> return map;
> }
> @Override
> public Email adaptFromJson(final Map<String, Object> map) {
> return new Email(map.get("user").toString(),
> map.get("domain").toString());
> }{code}
> fails and the JsonbAdapter specified in the JsonbConfig will not be invoked
> {code:java}
> public static class Email {
> private final String user;
> private final String domain;
> public Email(final String user, final String domain) {
> //..
> }
> //... {code}
> will fail with exception
> {code:java}
> jakarta.json.bind.JsonbException: Email has no suitable constructor or
> factory. Cannot deserialize json object value:
> {"user":"test","domain":"domain.com"}
> Use Johnzon @ConstructorProperties or @JsonbCreator if constructor arguments
> are needed
> class
> org.apache.johnzon.jsonb.symmetry.adapter.map.MapAdapterOnClassTest$Email not
> instantiable{code}
> This will fail for write operations for either List or Map, however, this
> will work for write operations.
> This will work for read and write if the {{JsonbAdapter}} adapts a
> {{String}}.
>
--
This message was sent by Atlassian Jira
(v8.20.10#820010)