You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
Similar to issue #587, which has been fixed in v3.0.3, custom serializers/deserializers and adapters are also ignored for map keys and values. Consider a map with a custom key and an adapter that translates this custom key to a string, then the map should be serialized into a JSON object
{
"key":"value"
}
rather than an array of key/value objects.
[
{
"key":{"value":"key"},
"value":"value",
}
]
To Reproduce
The unit test below allows reproducing the issue.
package maps;
import static jakarta.json.bind.JsonbBuilder.create;
import static org.junit.Assert.assertEquals;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import jakarta.json.bind.Jsonb;
import jakarta.json.bind.adapter.JsonbAdapter;
import jakarta.json.bind.annotation.JsonbTypeAdapter;
public class MapTest {
public static class CustomKeyAdapter implements JsonbAdapter<CustomKey, String> {
@Override
public String adaptToJson(CustomKey key) throws Exception {
return key != null ? key.toString() : null;
}
@Override
public CustomKey adaptFromJson(String s) throws Exception {
return s != null && s.length() > 0 ? new CustomKey(s) : null;
}
}
// A simple custom key that can be translated into a string.
@JsonbTypeAdapter(CustomKeyAdapter.class)
public static class CustomKey {
private String value;
public CustomKey(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
@Test
public void marshalMap() {
Map<CustomKey,String> map = new HashMap<>();
map.put(new CustomKey("key"),"value");
Jsonb jsonb = create();
String json = jsonb.toJson(map);
assertEquals("{\"key\":\"value\"}", json);
}
}
The unit test fails with the following error message:
org.junit.ComparisonFailure: expected:<[{"key":"value"}]> but was:<[[{"key":{"value":"key"},"value":"value"}]]>
Expected behavior
Serialize maps into a JSON object if the custom key type can be adapted to a string.
System information:
OS: [Mac, Ubuntu]
Java Version: [19]
Yasson Version: [3.0.3]
Additional context
In a comment in issue 587 it was already pointed out that the fix should be applied to maps and arrays too.
The text was updated successfully, but these errors were encountered:
martinsteger
changed the title
Custom serializers/deserializers and adapters ignored for Map keys (and values.
Custom serializers/deserializers and adapters ignored for Map keys (and values).
Aug 28, 2024
Describe the bug
Similar to issue #587, which has been fixed in v3.0.3, custom serializers/deserializers and adapters are also ignored for map keys and values. Consider a map with a custom key and an adapter that translates this custom key to a string, then the map should be serialized into a JSON object
rather than an array of key/value objects.
To Reproduce
The unit test below allows reproducing the issue.
The unit test fails with the following error message:
Expected behavior
Serialize maps into a JSON object if the custom key type can be adapted to a string.
System information:
Additional context
In a comment in issue 587 it was already pointed out that the fix should be applied to maps and arrays too.
The text was updated successfully, but these errors were encountered: