package com.fasterxml.jackson.databind.ser;

import java.io.IOException;
import java.util.*;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

public class TestKeySerializers extends BaseMapTest
{
    public static class KarlSerializer extends JsonSerializer<String>
    {
        @Override
        public void serialize(String value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
            jgen.writeFieldName("Karl");
        }
    }

    public static class NotKarlBean
    {
        public Map<String,Integer> map = new HashMap<String,Integer>();
        {
            map.put("Not Karl", 1);
        }
    }

    public static class KarlBean
    {
        @JsonSerialize(keyUsing = KarlSerializer.class)
        public Map<String,Integer> map = new HashMap<String,Integer>();
        {
            map.put("Not Karl", 1);
        }
    }

    /*
    /**********************************************************
    /* Unit tests
    /**********************************************************
     */
    
    public void testNotKarl() throws IOException {
        final ObjectMapper mapper = new ObjectMapper();
        final String serialized = mapper.writeValueAsString(new NotKarlBean());
        assertEquals("{\"map\":{\"Not Karl\":1}}", serialized);
    }

    public void testKarl() throws IOException {
        final ObjectMapper mapper = new ObjectMapper();
        final String serialized = mapper.writeValueAsString(new KarlBean());
        assertEquals("{\"map\":{\"Karl\":1}}", serialized);
    }

    // [Issue#75]: caching of KeySerializers
    public void testBoth() throws IOException
    {
        final ObjectMapper mapper = new ObjectMapper();
        final String value1 = mapper.writeValueAsString(new NotKarlBean());
        assertEquals("{\"map\":{\"Not Karl\":1}}", value1);
        final String value2 = mapper.writeValueAsString(new KarlBean());
        assertEquals("{\"map\":{\"Karl\":1}}", value2);
    }
}

@@ -136,16 +136,17 @@
 erator;%0A
+%0A
 import c
@@ -241,16 +241,75 @@
 rialize;
+%0Aimport com.fasterxml.jackson.databind.module.SimpleModule;
 %0A%0Apublic
@@ -1025,24 +1025,517 @@
    %7D%0A    %7D%0A%0A
+    enum ABC %7B%0A        A, B, C%0A    %7D%0A%0A    static class ABCSerializer extends JsonSerializer%3CABC%3E %7B%0A        @Override%0A        public void serialize(ABC value, JsonGenerator jgen,%0A                SerializerProvider provider) throws IOException %7B%0A            jgen.writeFieldName(%22xxx%22+value);%0A        %7D%0A    %7D%0A%0A    static class ABCMapWrapper %7B%0A        public Map%3CABC,String%3E stuff = new HashMap%3CABC,String%3E();%0A        public ABCMapWrapper() %7B%0A            stuff.put(ABC.B, %22bar%22);%0A        %7D%0A    %7D%0A%0A
     /*%0A    /
@@ -2548,32 +2548,32 @@
 ew KarlBean());%0A
-
         assertEq
@@ -2617,11 +2617,462 @@
 );%0A    %7D
+%0A%0A    // Test custom key serializer for enum%0A    public void testCustomForEnum() throws IOException%0A    %7B%0A        final ObjectMapper mapper = new ObjectMapper();%0A        SimpleModule mod = new SimpleModule(%22test%22);%0A        mod.addKeySerializer(ABC.class, new ABCSerializer());%0A        mapper.registerModule(mod);%0A%0A        String json = mapper.writeValueAsString(new ABCMapWrapper());%0A        assertEquals(%22%7B%5C%22stuff%5C%22:%7B%5C%22xxxB%5C%22:%5C%22bar%5C%22%7D%7D%22, json);%0A    %7D
 %0A%7D%0A

add a test for key serializers