Hello all,
I'm hoping someone can give me some insight into what I'm doing wrong here.
I have an enum type, and I want to serialize it including the class information using the @JsonTypeInfo annotation, so that it can be deserialized into the correct class automatically:
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property="@class")
public enum SimpleFakeRecordType {
FAKE1("Fake1"),
FAKE2("Fake2"),
FAKE3("Fake3");
@JsonProperty("name")
private String name;
SimpleFakeRecordType(final String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
When I do this, it seems to correctly serialize an object, for example:
SimpleFakeRecordType.FAKE1 becomes {"@class":"test.SimpleFakeRecordType","name":"Fake1"}
However, deserializing this same value causes an exception:
com.fasterxml.jackson.databind.JsonMappingException:
Unexpected token (START_OBJECT), expected START_ARRAY: need JSON Array to contain As.WRAPPER_ARRAY type information for class test.SimpleFakeRecordType
at [Source: {"@class":"test.SimpleFakeRecordType","name":"Fake1"}; line: 1, column: 1]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
at com.fasterxml.jackson.databind.DeserializationContext.wrongTokenException(DeserializationContext.java:946)
at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer._locateTypeId(AsArrayTypeDeserializer.java:127)
at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer._deserialize(AsArrayTypeDeserializer.java:93)
at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer.deserializeTypedFromScalar(AsArrayTypeDeserializer.java:63)
at com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer.deserializeWithType(StdScalarDeserializer.java:26)
at com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer.deserialize(TypeWrappedDeserializer.java:42)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3736) at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2726)
at test.SimpleFakeRecordTypeSerializationTest$anonfun$1.apply$mcV$sp(SimpleFakeRecordTypeSerializationTest.scala:20)
Test case is available
here, and a simple maven project that runs the test is
here.
Any help or insights into what I am doing wrong to get the roundtrip serialization working correctly would be much appreciated.
Thanks,