1 package com.fasterxml.jackson.databind.module; 2 3 import java.util.Map; 4 5 import com.fasterxml.jackson.core.Version; 6 import com.fasterxml.jackson.core.type.TypeReference; 7 import com.fasterxml.jackson.databind.*; 8 9 public class TestKeyDeserializers extends BaseMapTest 10 { 11 static class FooKeyDeserializer extends KeyDeserializer 12 { 13 @Override deserializeKey(String key, DeserializationContext ctxt)14 public Foo deserializeKey(String key, DeserializationContext ctxt) 15 { 16 return new Foo(key); 17 } 18 } 19 20 static class Foo { 21 public String value; 22 Foo(String v)23 public Foo(String v) { value = v; } 24 } 25 26 /* 27 /********************************************************** 28 /* Unit tests 29 /********************************************************** 30 */ 31 testKeyDeserializers()32 public void testKeyDeserializers() throws Exception 33 { 34 ObjectMapper mapper = new ObjectMapper(); 35 SimpleModule mod = new SimpleModule("test", Version.unknownVersion()); 36 mod.addKeyDeserializer(Foo.class, new FooKeyDeserializer()); 37 mapper.registerModule(mod); 38 Map<Foo,Integer> map = mapper.readValue("{\"a\":3}", 39 new TypeReference<Map<Foo,Integer>>() {} ); 40 assertNotNull(map); 41 assertEquals(1, map.size()); 42 Foo foo = map.keySet().iterator().next(); 43 assertEquals("a", foo.value); 44 } 45 } 46