eclipse-ee4j / eclipse-ee4j/yasson

Deserializing Map with enum keys results in runtime string keys

Open
#283 16 comments 0 reactions 1 assignee Claimed by @aguibert View on GitHub
enhancement
Dominant language
Java
Stars
218
Forks
109
Avg merge
1d 5h
Merged PRs (30d)
9

Description

Attempting to deserialize json into a Map with enum keys will result in the instance having String keys at runtime. Using an enum as the value side of a Map works as expected. We should support Map keys with natural string representations in JSON (immediately, enums and UUIDs come to mind, although I'm sure some other types would benefit from this). This is somewhat related to #110, #177 and #253, although those issues have slightly ambiguous behavior since there is no natural representation, other than the JSON-in-JSON string as the key

A simple example:
```java
public class EnumKeyTest {

public static class Container {
public Map letterToOrdinal;
public Map nameToLetter;
}

@Test
public void shouldHaveEnumKeys() {
Jsonb jsonb = JsonbBuilder.create();
Container container = jsonb.fromJson("{\"letterToOrdinal\":{\"B\": 1}, \"nameToLetter\":{\"a\":\"A\"}}", Container.class);
assert container.nameToLetter.values().iterator().next() instanceof Letter : "value is Letter";
assert container.nameToLetter.values().iterator().next() == Letter.A : "value == A";
assert container.letterToOrdinal.keySet().iterator().next() instanceof Letter : "key is letter";
assert container.letterToOrdinal.keySet().iterator().next() == Letter.B : "key == B";
}

enum Letter {
A, B, C
}
}
```
This test will fail on key is letter since the key value is actually a String.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.