0

Sample:

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.codehaus.jackson.map.ObjectMapper;

public class Test {

    public static void main(String[] args) throws Exception {       
        ObjectMapper mapper = new ObjectMapper();   
        Map<Foo, Foo> map = new HashMap<Foo, Foo>();
        map.put(new Foo("foo1"), new Foo("foo2"));
        System.out.println(mapper.writeValueAsString(map));
    }

    public static class Foo implements Serializable {
        public String a;
        public Foo(String a) {
            this.a = a;
        }   
    }
}

Expected:

{{"a": "foo1"}:{"a":"foo2"}}

Ouput:

{"Test$Foo@4f4a7090":{"a":"foo2"}}

I used jackson-core-asl-1.9.13.jar. Anyone know why the Key is not serialized and how I can modify it such that it is serialized?

hansi
  • 2,278
  • 6
  • 34
  • 42
  • 1
    Jackson interprets your map as `Map`. When you are using default serializer it is just trying to read your object as a string. Note: Totally forgot to mention that your expected JSON is not a valid JSON. – Coder Mar 09 '17 at 01:32
  • 1
    That would be invalid json. – Sotirios Delimanolis Mar 09 '17 at 01:57
  • That JSON is valid, not useful, but valid. – Bob Kuhar Mar 09 '17 at 03:08
  • Do let me know how it will be a valid JSON? If I am wrong I am looking to learn from it @BobKuhar – Coder Mar 09 '17 at 03:30
  • Thanks, I realize now too that it would be invalid json. Not going to try if I could make it happen with a custom Jackson serializer. – hansi Mar 09 '17 at 04:55
  • @SotiriosDelimanolis my bad, I was looking at the actual output. The expected output is not valid JSON. the actual output is not useful JSON. – Bob Kuhar Mar 09 '17 at 06:05

0 Answers0