1 package com.fasterxml.jackson.databind.convert; 2 3 import java.io.IOException; 4 import java.util.*; 5 6 import com.fasterxml.jackson.core.JsonGenerator; 7 8 import com.fasterxml.jackson.databind.JsonSerializer; 9 import com.fasterxml.jackson.databind.SerializerProvider; 10 import com.fasterxml.jackson.databind.annotation.JsonSerialize; 11 import com.fasterxml.jackson.databind.util.StdConverter; 12 13 public class TestConvertingSerializer 14 extends com.fasterxml.jackson.databind.BaseMapTest 15 { 16 @JsonSerialize(converter=ConvertingBeanConverter.class) 17 static class ConvertingBean 18 { 19 public int x, y; 20 ConvertingBean(int v1, int v2)21 public ConvertingBean(int v1, int v2) { 22 x = v1; 23 y = v2; 24 } 25 } 26 27 static class Point 28 { 29 public int x, y; 30 Point(int v1, int v2)31 public Point(int v1, int v2) { 32 x = v1; 33 y = v2; 34 } 35 } 36 37 static class ConvertingBeanContainer 38 { 39 public List<ConvertingBean> values; 40 ConvertingBeanContainer(ConvertingBean... beans)41 public ConvertingBeanContainer(ConvertingBean... beans) { 42 values = Arrays.asList(beans); 43 } 44 } 45 46 static class ConvertingBeanConverter extends StdConverter<ConvertingBean, int[]> 47 { 48 @Override convert(ConvertingBean value)49 public int[] convert(ConvertingBean value) { 50 return new int[] { value.x, value.y }; 51 } 52 } 53 54 static class PointConverter extends StdConverter<Point, int[]> 55 { convert(Point value)56 @Override public int[] convert(Point value) { 57 return new int[] { value.x, value.y }; 58 } 59 } 60 61 static class PointWrapper { 62 @JsonSerialize(converter=PointConverter.class) 63 public Point value; 64 PointWrapper(int x, int y)65 public PointWrapper(int x, int y) { 66 value = new Point(x, y); 67 } 68 } 69 70 static class PointListWrapperArray { 71 @JsonSerialize(contentConverter=PointConverter.class) 72 public Point[] values; 73 PointListWrapperArray(int x, int y)74 public PointListWrapperArray(int x, int y) { 75 values = new Point[] { new Point(x, y), new Point(y, x) }; 76 } 77 } 78 79 static class PointListWrapperList { 80 @JsonSerialize(contentConverter=PointConverter.class) 81 public List<Point> values; 82 PointListWrapperList(int x, int y)83 public PointListWrapperList(int x, int y) { 84 values = Arrays.asList(new Point[] { new Point(x, y), new Point(y, x) }); 85 } 86 } 87 88 static class PointListWrapperMap { 89 @JsonSerialize(contentConverter=PointConverter.class) 90 public Map<String,Point> values; 91 PointListWrapperMap(String key, int x, int y)92 public PointListWrapperMap(String key, int x, int y) { 93 values = new HashMap<String,Point>(); 94 values.put(key, new Point(x, y)); 95 } 96 } 97 98 // [databind#357] 99 static class Value { } 100 101 static class ListWrapper { 102 @JsonSerialize(contentConverter = ValueToStringListConverter.class) 103 public List<Value> list = Arrays.asList(new Value()); 104 } 105 106 static class ValueToStringListConverter extends StdConverter<Value, List<String>> { 107 @Override convert(Value value)108 public List<String> convert(Value value) { 109 return Arrays.asList("Hello world!"); 110 } 111 } 112 113 // [databind#359] 114 static class Bean359 { 115 @JsonSerialize(as = List.class, contentAs = Source.class) 116 public List<Source> stuff = Arrays.asList(new Source()); 117 } 118 119 @JsonSerialize(using = TargetSerializer.class) 120 static class Target { 121 public String unexpected = "Bye."; 122 } 123 124 @JsonSerialize(converter = SourceToTargetConverter.class) 125 static class Source { } 126 127 static class SourceToTargetConverter extends StdConverter<Source, Target> { 128 @Override convert(Source value)129 public Target convert(Source value) { 130 return new Target(); 131 } 132 } 133 134 static class TargetSerializer extends JsonSerializer<Target> 135 { 136 @Override serialize(Target a, JsonGenerator jsonGenerator, SerializerProvider provider)137 public void serialize(Target a, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException { 138 jsonGenerator.writeString("Target"); 139 } 140 } 141 142 // [databind#731] 143 public static class DummyBean { 144 public final int a, b; DummyBean(int v1, int v2)145 public DummyBean(int v1, int v2) { 146 a = v1 * 2; 147 b = v2 * 2; 148 } 149 } 150 151 @JsonSerialize(converter = UntypedConvertingBeanConverter.class) 152 static class ConvertingBeanWithUntypedConverter { 153 public int x, y; ConvertingBeanWithUntypedConverter(int v1, int v2)154 public ConvertingBeanWithUntypedConverter(int v1, int v2) { 155 x = v1; 156 y = v2; 157 } 158 } 159 160 static class UntypedConvertingBeanConverter extends StdConverter<ConvertingBeanWithUntypedConverter, Object> 161 { 162 @Override convert(ConvertingBeanWithUntypedConverter cb)163 public Object convert(ConvertingBeanWithUntypedConverter cb) { 164 return new DummyBean(cb.x, cb.y); 165 } 166 } 167 168 /* 169 /********************************************************** 170 /* Test methods 171 /********************************************************** 172 */ 173 testClassAnnotationSimple()174 public void testClassAnnotationSimple() throws Exception 175 { 176 String json = objectWriter().writeValueAsString(new ConvertingBean(1, 2)); 177 assertEquals("[1,2]", json); 178 } 179 testClassAnnotationForLists()180 public void testClassAnnotationForLists() throws Exception 181 { 182 String json = objectWriter().writeValueAsString(new ConvertingBeanContainer( 183 new ConvertingBean(1, 2), new ConvertingBean(3, 4))); 184 assertEquals("{\"values\":[[1,2],[3,4]]}", json); 185 } 186 testPropertyAnnotationSimple()187 public void testPropertyAnnotationSimple() throws Exception 188 { 189 String json = objectWriter().writeValueAsString(new PointWrapper(3, 4)); 190 assertEquals("{\"value\":[3,4]}", json); 191 } 192 testPropertyAnnotationForArrays()193 public void testPropertyAnnotationForArrays() throws Exception { 194 String json = objectWriter().writeValueAsString(new PointListWrapperArray(4, 5)); 195 assertEquals("{\"values\":[[4,5],[5,4]]}", json); 196 } 197 testPropertyAnnotationForLists()198 public void testPropertyAnnotationForLists() throws Exception { 199 String json = objectWriter().writeValueAsString(new PointListWrapperList(7, 8)); 200 assertEquals("{\"values\":[[7,8],[8,7]]}", json); 201 } 202 testPropertyAnnotationForMaps()203 public void testPropertyAnnotationForMaps() throws Exception { 204 String json = objectWriter().writeValueAsString(new PointListWrapperMap("a", 1, 2)); 205 assertEquals("{\"values\":{\"a\":[1,2]}}", json); 206 } 207 208 // [databind#357] testConverterForList357()209 public void testConverterForList357() throws Exception { 210 String json = objectWriter().writeValueAsString(new ListWrapper()); 211 assertEquals("{\"list\":[[\"Hello world!\"]]}", json); 212 } 213 214 // [databind#359] testIssue359()215 public void testIssue359() throws Exception { 216 String json = objectWriter().writeValueAsString(new Bean359()); 217 assertEquals("{\"stuff\":[\"Target\"]}", json); 218 } 219 220 // [databind#731]: Problems converting from java.lang.Object ("unknown") testIssue731()221 public void testIssue731() throws Exception 222 { 223 String json = objectWriter().writeValueAsString(new ConvertingBeanWithUntypedConverter(1, 2)); 224 // must be {"a":2,"b":4} 225 assertEquals("{\"a\":2,\"b\":4}", json); 226 } 227 } 228