• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.fasterxml.jackson.databind.convert;
2 
3 import java.util.*;
4 
5 import com.fasterxml.jackson.core.type.TypeReference;
6 import com.fasterxml.jackson.databind.*;
7 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
8 import com.fasterxml.jackson.databind.util.StdConverter;
9 
10 public class TestMapConversions
11     extends com.fasterxml.jackson.databind.BaseMapTest
12 {
13     final ObjectMapper MAPPER = new ObjectMapper();
14 
15     enum AB { A, B; }
16 
17     static class Bean {
18         public Integer A;
19         public String B;
20     }
21 
22     // [Issue#287]
23 
24     @JsonSerialize(converter=RequestConverter.class)
25     static class Request {
x()26         public int x() {
27             return 1;
28         }
29     }
30 
31     static class RequestConverter extends StdConverter<Request, Map<String,Object>> {
32         @Override
convert(final Request value)33         public Map<String,Object> convert(final Request value) {
34             final Map<String, Object> test = new LinkedHashMap<String, Object>();
35             final Map<String, Object> innerTest = new LinkedHashMap<String, Object>();
36             innerTest.put("value", value.x());
37             test.put("hello", innerTest);
38             return test;
39         }
40     }
41 
42     /*
43     /**********************************************************
44     /* Test methods
45     /**********************************************************
46      */
47 
48     /**
49      * Test that verifies that we can go between couple of types of Maps...
50      */
testMapToMap()51     public void testMapToMap()
52     {
53         Map<String,Integer> input = new LinkedHashMap<String,Integer>();
54         input.put("A", Integer.valueOf(3));
55         input.put("B", Integer.valueOf(-4));
56         Map<AB,String> output = MAPPER.convertValue(input,
57                 new TypeReference<Map<AB,String>>() { });
58         assertEquals(2, output.size());
59         assertEquals("3", output.get(AB.A));
60         assertEquals("-4", output.get(AB.B));
61 
62         // Let's try the other way too... and mix up types a bit
63         Map<String,Integer> roundtrip = MAPPER.convertValue(input,
64                 new TypeReference<TreeMap<String,Integer>>() { });
65         assertEquals(2, roundtrip.size());
66         assertEquals(Integer.valueOf(3), roundtrip.get("A"));
67         assertEquals(Integer.valueOf(-4), roundtrip.get("B"));
68     }
69 
testMapToBean()70     public void testMapToBean()
71     {
72         EnumMap<AB,String> map = new EnumMap<AB,String>(AB.class);
73         map.put(AB.A, "17");
74         map.put(AB.B, "-1");
75         Bean bean = MAPPER.convertValue(map, Bean.class);
76         assertEquals(Integer.valueOf(17), bean.A);
77         assertEquals("-1", bean.B);
78     }
79 
testBeanToMap()80     public void testBeanToMap()
81     {
82         Bean bean = new Bean();
83         bean.A = 129;
84         bean.B = "13";
85         EnumMap<AB,String> result = MAPPER.convertValue(bean,
86                 new TypeReference<EnumMap<AB,String>>() { });
87         assertEquals("129", result.get(AB.A));
88         assertEquals("13", result.get(AB.B));
89     }
90 
91     // [Issue#287]: Odd problems with `Object` type, static typing
testIssue287()92     public void testIssue287() throws Exception
93     {
94         // use local instance to ensure no caching affects it:
95         final ObjectMapper mapper = new ObjectMapper();
96         final Request request = new Request();
97         final String retString = mapper.writeValueAsString(request);
98         assertEquals("{\"hello\":{\"value\":1}}",retString);
99     }
100 
101     // [databind#810]
testMapToProperties()102     public void testMapToProperties() throws Exception
103     {
104         Bean bean = new Bean();
105         bean.A = 129;
106         bean.B = "13";
107         Properties props = MAPPER.convertValue(bean, Properties.class);
108 
109         assertEquals(2, props.size());
110 
111         assertEquals("13", props.getProperty("B"));
112         // should coercce non-Strings to Strings
113         assertEquals("129", props.getProperty("A"));
114     }
115 }
116