1 package com.fasterxml.jackson.databind.ser; 2 3 import java.io.IOException; 4 import java.util.*; 5 6 import com.fasterxml.jackson.core.*; 7 8 import com.fasterxml.jackson.databind.*; 9 import com.fasterxml.jackson.databind.annotation.JsonSerialize; 10 11 public class TestJsonSerialize3 extends BaseMapTest 12 { 13 // [JACKSON-829] 14 static class FooToBarSerializer extends JsonSerializer<String> { 15 @Override serialize(String value, JsonGenerator jgen, SerializerProvider provider)16 public void serialize(String value, JsonGenerator jgen, SerializerProvider provider) 17 throws IOException { 18 if ("foo".equals(value)) { 19 jgen.writeString("bar"); 20 } else { 21 jgen.writeString(value); 22 } 23 } 24 } 25 26 static class MyObject { 27 @JsonSerialize(contentUsing = FooToBarSerializer.class) 28 List<String> list; 29 } 30 /* 31 /********************************************************** 32 /* Test methods 33 /********************************************************** 34 */ 35 testCustomContentSerializer()36 public void testCustomContentSerializer() throws Exception 37 { 38 ObjectMapper m = new ObjectMapper(); 39 MyObject object = new MyObject(); 40 object.list = Arrays.asList("foo"); 41 String json = m.writeValueAsString(object); 42 assertEquals("{\"list\":[\"bar\"]}", json); 43 } 44 } 45