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

Custom serializers/deserializers and adapters ignored for Map keys (and values). #649

Open
martinsteger opened this issue Aug 28, 2024 · 0 comments
Labels
bug Something isn't working right

Comments

@martinsteger
Copy link

martinsteger commented 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

{
  "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.

@martinsteger martinsteger added the bug Something isn't working right label Aug 28, 2024
@martinsteger 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working right
Projects
None yet
Development

No branches or pull requests

1 participant