-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Extended enum values are not handled as enums when used as Map keys #2457
Comments
To me that seems to work as expected: by default, But if you do want to use |
Can confirm issue. Test case. import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
Map<Pojo, String> testData = new HashMap<>();
testData.put(Pojo.FIRST, "firstVal");
testData.put(Pojo.SECOND, "secondVal");
// uses #toString() instead of #name()
// so json is {"FIRST":"firstVal","toString()":"secondVal"}
// instead of {"FIRST":"firstVal","SECOND":"secondVal"}
String json = mapper.writeValueAsString(testData);
System.out.println(json);
// crash here
Map<Pojo, String> res = mapper.readValue(json, new TypeReference<Map<Pojo, String>>() {});
// Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidFormatException:
// Cannot deserialize Map key of type `dk.xakeps.jacksontest.Test$Pojo`from String "toString()":
// not a valid representation,
// problem: (com.fasterxml.jackson.databind.exc.InvalidFormatException)
// Cannot deserialize Map key of type `dk.xakeps.jacksontest.Test$Pojo` from String "toString()":
// not one of values excepted for Enum class: [SECOND, FIRST]
// at [Source: (String)"{"FIRST":"firstVal","toString()":"secondVal"}"; line: 1, column: 21]
}
public enum Pojo {
FIRST, SECOND() {
@Override
public String toString() {
return "toString()";
}
}
}
} |
Ah. Enums as Map Keys are behaving in unexpected way... that makes more sense. One thing I would suggest, which should work (and may be required) is to force serialization type, so use I will keep this open for investigation since handling of Enums as keys is not as well developed as that for values. |
Root cause was that
returns -- surprisingly! -- Enum.class.isAssignableFrom(EnumSubType.class) check. |
Hello, guys!
I've faced with a problem of inconsistent enum serialization:
Expected result:
{"A":"1", "B":"2"}
Actial result:
{"A":"1", "B as string":"2"}
Research:
Actually
B.getClass()
is notMyEnum
butMyEnum$1
which is not enum anymore!So, my proposal is to add this check to
com.fasterxml.jackson.databind.JavaType
:The text was updated successfully, but these errors were encountered: