Skip to content

Instantly share code, notes, and snippets.

@kldavis4
Created January 8, 2013 22:08
Show Gist options
  • Select an option

  • Save kldavis4/4488448 to your computer and use it in GitHub Desktop.

Select an option

Save kldavis4/4488448 to your computer and use it in GitHub Desktop.
Test case to replicate issue with handling of polymorphic enum classes
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;
public class EnumPolymorphicSerializationTest {
@Test
public void testEnumPolymorphicSerializationTest() throws IOException {
ObjectMapper mapper = new ObjectMapper();
List<ITestType> testTypesList = new ArrayList<ITestType>();
testTypesList.add(ConcreteType.ONE);
testTypesList.add(ConcreteType.TWO);
ListContainer listContainer = new ListContainer();
listContainer.setTestTypes(testTypesList);
listContainer = mapper.readValue(mapper.writeValueAsString(listContainer),ListContainer.class);
EnumMapContainer enumMapContainer = new EnumMapContainer();
EnumMap<KeyType,ITestType> testTypesMap = new EnumMap<KeyType,ITestType>(KeyType.class);
testTypesMap.put(KeyType.A, ConcreteType.ONE);
testTypesMap.put(KeyType.B, ConcreteType.TWO);
enumMapContainer.setTestTypes(testTypesMap);
enumMapContainer = mapper.readValue(mapper.writeValueAsString(enumMapContainer), EnumMapContainer.class);
}
public static class EnumMapContainer {
private EnumMap<KeyType,ITestType> testTypes;
@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="@class")
public EnumMap<KeyType,ITestType> getTestTypes() {
return testTypes;
}
public void setTestTypes(EnumMap<KeyType,ITestType> testTypes) {
this.testTypes = testTypes;
}
}
public static class ListContainer {
private List<ITestType> testTypes;
public List<ITestType> getTestTypes() { return testTypes; }
public void setTestTypes(List<ITestType> testTypes) { this.testTypes = testTypes; }
}
@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="@class")
public static interface ITestType extends Serializable {
}
public static enum KeyType {
A, B
}
public static enum ConcreteType implements ITestType {
ONE, TWO;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment