package com.fasterxml.jackson.databind.deser.jdk;
import java.util.EnumMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
@SuppressWarnings("serial")
public class EnumMapDeserializationTest extends BaseMapTest
{
enum TestEnum { JACKSON, RULES, OK; }
protected enum LowerCaseEnum {
A, B, C;
private LowerCaseEnum() { }
@Override
public String toString() { return name().toLowerCase(); }
}
static class MySimpleEnumMap extends EnumMap<TestEnum,String> {
public MySimpleEnumMap() {
super(TestEnum.class);
}
}
static class FromStringEnumMap extends EnumMap<TestEnum,String> {
@JsonCreator
public FromStringEnumMap(String value) {
super(TestEnum.class);
put(TestEnum.JACKSON, value);
}
}
static class FromDelegateEnumMap extends EnumMap<TestEnum,String> {
@JsonCreator
public FromDelegateEnumMap(Map<Object,Object> stuff) {
super(TestEnum.class);
put(TestEnum.OK, String.valueOf(stuff));
}
}
static class FromPropertiesEnumMap extends EnumMap<TestEnum,String> {
int a0, b0;
@JsonCreator
public FromPropertiesEnumMap(@JsonProperty("a") int a,
@JsonProperty("b") int b) {
super(TestEnum.class);
a0 = a;
b0 = b;
}
}
/*
/**********************************************************
/* Test methods
/**********************************************************
*/
protected final ObjectMapper MAPPER = new ObjectMapper();
public void testEnumMaps() throws Exception
{
EnumMap<TestEnum,String> value = MAPPER.readValue("{\"OK\":\"value\"}",
new TypeReference<EnumMap<TestEnum,String>>() { });
assertEquals("value", value.get(TestEnum.OK));
}
public void testToStringEnumMaps() throws Exception
{
// can't reuse global one due to reconfig
ObjectReader r = MAPPER.reader()
.with(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
EnumMap<LowerCaseEnum,String> value = r.forType(
new TypeReference<EnumMap<LowerCaseEnum,String>>() { })
.readValue("{\"a\":\"value\"}");
assertEquals("value", value.get(LowerCaseEnum.A));
}
public void testCustomEnumMapWithDefaultCtor() throws Exception
{
MySimpleEnumMap map = MAPPER.readValue(aposToQuotes("{'RULES':'waves'}"),
MySimpleEnumMap.class);
assertEquals(1, map.size());
assertEquals("waves", map.get(TestEnum.RULES));
}
public void testCustomEnumMapFromString() throws Exception
{
FromStringEnumMap map = MAPPER.readValue(quote("kewl"), FromStringEnumMap.class);
assertEquals(1, map.size());
assertEquals("kewl", map.get(TestEnum.JACKSON));
}
public void testCustomEnumMapWithDelegate() throws Exception
{
FromDelegateEnumMap map = MAPPER.readValue(aposToQuotes("{'foo':'bar'}"), FromDelegateEnumMap.class);
assertEquals(1, map.size());
assertEquals("{foo=bar}", map.get(TestEnum.OK));
}
public void testCustomEnumMapFromProps() throws Exception
{
FromPropertiesEnumMap map = MAPPER.readValue(aposToQuotes(
"{'a':13,'RULES':'jackson','b':-731,'OK':'yes'}"),
FromPropertiesEnumMap.class);
assertEquals(13, map.a0);
assertEquals(-731, map.b0);
assertEquals("jackson", map.get(TestEnum.RULES));
assertEquals("yes", map.get(TestEnum.OK));
assertEquals(2, map.size());
}
}