杰克逊 Serialization/Deserialization 个 java.util.EnumMap 个错误
Jackson Serialization/Deserialization of java.util.EnumMap Errors
当我尝试使用 Jackson v2.5.0 序列化和反序列化 java.util.EnumMap
时,我收到以下异常:
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.EnumMap out of FIELD_NAME token
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:835)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:831)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer._deserializeFromEmpty(StdDeserializer.java:838)
at com.fasterxml.jackson.databind.deser.std.EnumMapDeserializer.deserialize(EnumMapDeserializer.java:128)
at com.fasterxml.jackson.databind.deser.std.EnumMapDeserializer.deserialize(EnumMapDeserializer.java:17)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer._deserializeTypedForId(AsPropertyTypeDeserializer.java:124)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:95)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserializeWithType(MapDeserializer.java:364)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:521)
at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:101)
at com.fasterxml.jackson.databind.deser.impl.BeanPropertyMap.findDeserializeAndSet(BeanPropertyMap.java:285)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:248)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:169)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:144)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer._deserializeTypedForId(AsPropertyTypeDeserializer.java:124)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:95)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeWithType(BeanDeserializerBase.java:957)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:521)
at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:101)
at com.fasterxml.jackson.databind.deser.impl.BeanPropertyMap.findDeserializeAndSet(BeanPropertyMap.java:285)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:335)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeWithObjectId(BeanDeserializerBase.java:1036)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:172)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:144)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer._deserializeTypedForId(AsPropertyTypeDeserializer.java:124)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:95)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromAny(AsPropertyTypeDeserializer.java:167)
at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserializeWithType(UntypedObjectDeserializer.java:494)
at com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer.deserialize(TypeWrappedDeserializer.java:42)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3560)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2679)
at org.redisson.codec.JsonJacksonCodec.decode(JsonJacksonCodec.java:107)
... 47 more
有人知道怎么解决吗?我查看了 Jackson 配置选项文档,但没有找到可以解决此问题的文档。
你能否更具体地说明你的问题、示例代码、你正在尝试做的事情,我只是使用 Jackson 2.5.2 将这个示例代码放在一起并且工作正常。
public class Test {
public enum STATE{
NEW, RUNNING, WAITING, FINISHED
}
public static void main(String[] args) throws JsonProcessingException {
EnumMap<STATE, String> stateMap = new EnumMap<>(STATE.class);
stateMap.put(STATE.RUNNING, "Program is running");
stateMap.put(STATE.WAITING, "Program is waiting");
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writeValueAsString(stateMap));
}
}
输出为:
{"RUNNING":"Program is running","WAITING":"Program is waiting"}
让我们看看有什么不同,很乐意提供帮助。
关于反序列化,因为 EnumMap
需要 Class 类型作为构造函数的一部分,您是否考虑过为您的类型特定枚举扩展它,例如:
public class MyEnumMap extends EnumMap<Test.STATE, String> {
public MyEnumMap() {
super(Test.STATE.class);
}
}
然后你可以扩展反序列化你可以在对象映射器中引用 MyEnumMap
:
EnumMap enumMap = mapper.readValue("{\"RUNNING\":\"Program is running\",\"WAITING\":\"Program is waiting\"}", MyEnumMap.class);
enumMap.keySet().forEach(System.out::println);
解串器的输出是:
RUNNING
WAITING
最佳,
何塞·路易斯
当我尝试使用 Jackson v2.5.0 序列化和反序列化 java.util.EnumMap
时,我收到以下异常:
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.EnumMap out of FIELD_NAME token
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:835)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:831)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer._deserializeFromEmpty(StdDeserializer.java:838)
at com.fasterxml.jackson.databind.deser.std.EnumMapDeserializer.deserialize(EnumMapDeserializer.java:128)
at com.fasterxml.jackson.databind.deser.std.EnumMapDeserializer.deserialize(EnumMapDeserializer.java:17)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer._deserializeTypedForId(AsPropertyTypeDeserializer.java:124)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:95)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserializeWithType(MapDeserializer.java:364)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:521)
at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:101)
at com.fasterxml.jackson.databind.deser.impl.BeanPropertyMap.findDeserializeAndSet(BeanPropertyMap.java:285)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:248)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:169)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:144)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer._deserializeTypedForId(AsPropertyTypeDeserializer.java:124)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:95)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeWithType(BeanDeserializerBase.java:957)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:521)
at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:101)
at com.fasterxml.jackson.databind.deser.impl.BeanPropertyMap.findDeserializeAndSet(BeanPropertyMap.java:285)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:335)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeWithObjectId(BeanDeserializerBase.java:1036)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:172)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:144)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer._deserializeTypedForId(AsPropertyTypeDeserializer.java:124)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:95)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromAny(AsPropertyTypeDeserializer.java:167)
at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserializeWithType(UntypedObjectDeserializer.java:494)
at com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer.deserialize(TypeWrappedDeserializer.java:42)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3560)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2679)
at org.redisson.codec.JsonJacksonCodec.decode(JsonJacksonCodec.java:107)
... 47 more
有人知道怎么解决吗?我查看了 Jackson 配置选项文档,但没有找到可以解决此问题的文档。
你能否更具体地说明你的问题、示例代码、你正在尝试做的事情,我只是使用 Jackson 2.5.2 将这个示例代码放在一起并且工作正常。
public class Test {
public enum STATE{
NEW, RUNNING, WAITING, FINISHED
}
public static void main(String[] args) throws JsonProcessingException {
EnumMap<STATE, String> stateMap = new EnumMap<>(STATE.class);
stateMap.put(STATE.RUNNING, "Program is running");
stateMap.put(STATE.WAITING, "Program is waiting");
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writeValueAsString(stateMap));
}
}
输出为:
{"RUNNING":"Program is running","WAITING":"Program is waiting"}
让我们看看有什么不同,很乐意提供帮助。
关于反序列化,因为 EnumMap
需要 Class 类型作为构造函数的一部分,您是否考虑过为您的类型特定枚举扩展它,例如:
public class MyEnumMap extends EnumMap<Test.STATE, String> {
public MyEnumMap() {
super(Test.STATE.class);
}
}
然后你可以扩展反序列化你可以在对象映射器中引用 MyEnumMap
:
EnumMap enumMap = mapper.readValue("{\"RUNNING\":\"Program is running\",\"WAITING\":\"Program is waiting\"}", MyEnumMap.class);
enumMap.keySet().forEach(System.out::println);
解串器的输出是:
RUNNING
WAITING
最佳,
何塞·路易斯