1 package com.fasterxml.jackson.databind.ser; 2 3 import java.io.IOException; 4 import java.util.*; 5 6 import com.fasterxml.jackson.annotation.*; 7 import com.fasterxml.jackson.core.JsonGenerator; 8 import com.fasterxml.jackson.core.JsonProcessingException; 9 10 import com.fasterxml.jackson.databind.*; 11 import com.fasterxml.jackson.databind.annotation.JsonSerialize; 12 import com.fasterxml.jackson.databind.ser.std.NullSerializer; 13 14 @SuppressWarnings("serial") 15 public class TestJsonSerialize2 16 extends BaseMapTest 17 { 18 static class SimpleKey { 19 protected final String key; 20 SimpleKey(String str)21 public SimpleKey(String str) { key = str; } 22 toString()23 @Override public String toString() { return "toString:"+key; } 24 } 25 26 static class SimpleValue { 27 public final String value; 28 SimpleValue(String str)29 public SimpleValue(String str) { value = str; } 30 } 31 32 @JsonPropertyOrder({"value", "value2"}) 33 static class ActualValue extends SimpleValue 34 { 35 public final String other = "123"; 36 ActualValue(String str)37 public ActualValue(String str) { super(str); } 38 } 39 40 static class SimpleKeySerializer extends JsonSerializer<SimpleKey> { 41 @Override serialize(SimpleKey key, JsonGenerator jgen, SerializerProvider provider)42 public void serialize(SimpleKey key, JsonGenerator jgen, SerializerProvider provider) 43 throws IOException, JsonProcessingException { 44 jgen.writeFieldName("key "+key.key); 45 } 46 } 47 48 static class SimpleValueSerializer extends JsonSerializer<SimpleValue> { 49 @Override serialize(SimpleValue value, JsonGenerator jgen, SerializerProvider provider)50 public void serialize(SimpleValue value, JsonGenerator jgen, SerializerProvider provider) 51 throws IOException, JsonProcessingException { 52 jgen.writeString("value "+value.value); 53 } 54 } 55 56 @JsonSerialize(contentAs=SimpleValue.class) 57 static class SimpleValueList extends ArrayList<ActualValue> { } 58 59 @JsonSerialize(contentAs=SimpleValue.class) 60 static class SimpleValueMap extends HashMap<SimpleKey, ActualValue> { } 61 62 @JsonSerialize(contentUsing=SimpleValueSerializer.class) 63 static class SimpleValueListWithSerializer extends ArrayList<ActualValue> { } 64 65 @JsonSerialize(keyUsing=SimpleKeySerializer.class, contentUsing=SimpleValueSerializer.class) 66 static class SimpleValueMapWithSerializer extends HashMap<SimpleKey, ActualValue> { } 67 68 static class ListWrapperSimple 69 { 70 @JsonSerialize(contentAs=SimpleValue.class) 71 public final ArrayList<ActualValue> values = new ArrayList<ActualValue>(); 72 ListWrapperSimple(String value)73 public ListWrapperSimple(String value) { 74 values.add(new ActualValue(value)); 75 } 76 } 77 78 static class ListWrapperWithSerializer 79 { 80 @JsonSerialize(contentUsing=SimpleValueSerializer.class) 81 public final ArrayList<ActualValue> values = new ArrayList<ActualValue>(); 82 ListWrapperWithSerializer(String value)83 public ListWrapperWithSerializer(String value) { 84 values.add(new ActualValue(value)); 85 } 86 } 87 88 static class MapWrapperSimple 89 { 90 @JsonSerialize(contentAs=SimpleValue.class) 91 public final HashMap<SimpleKey, ActualValue> values = new HashMap<SimpleKey, ActualValue>(); 92 MapWrapperSimple(String key, String value)93 public MapWrapperSimple(String key, String value) { 94 values.put(new SimpleKey(key), new ActualValue(value)); 95 } 96 } 97 98 static class MapWrapperWithSerializer 99 { 100 @JsonSerialize(keyUsing=SimpleKeySerializer.class, contentUsing=SimpleValueSerializer.class) 101 public final HashMap<SimpleKey, ActualValue> values = new HashMap<SimpleKey, ActualValue>(); 102 MapWrapperWithSerializer(String key, String value)103 public MapWrapperWithSerializer(String key, String value) { 104 values.put(new SimpleKey(key), new ActualValue(value)); 105 } 106 } 107 108 static class NullBean 109 { 110 @JsonSerialize(using=NullSerializer.class) 111 public String value = "abc"; 112 } 113 114 /* 115 /********************************************************** 116 /* Test methods 117 /********************************************************** 118 */ 119 120 private final ObjectMapper MAPPER = new ObjectMapper(); 121 122 // test value annotation applied to List value class testSerializedAsListWithClassAnnotations()123 public void testSerializedAsListWithClassAnnotations() throws IOException 124 { 125 SimpleValueList list = new SimpleValueList(); 126 list.add(new ActualValue("foo")); 127 assertEquals("[{\"value\":\"foo\"}]", MAPPER.writeValueAsString(list)); 128 } 129 130 // test value annotation applied to Map value class testSerializedAsMapWithClassAnnotations()131 public void testSerializedAsMapWithClassAnnotations() throws IOException 132 { 133 SimpleValueMap map = new SimpleValueMap(); 134 map.put(new SimpleKey("x"), new ActualValue("y")); 135 assertEquals("{\"toString:x\":{\"value\":\"y\"}}", MAPPER.writeValueAsString(map)); 136 } 137 138 // test Serialization annotation with List testSerializedAsListWithClassSerializer()139 public void testSerializedAsListWithClassSerializer() throws IOException 140 { 141 ObjectMapper m = new ObjectMapper(); 142 SimpleValueListWithSerializer list = new SimpleValueListWithSerializer(); 143 list.add(new ActualValue("foo")); 144 assertEquals("[\"value foo\"]", m.writeValueAsString(list)); 145 } 146 testSerializedAsListWithPropertyAnnotations()147 public void testSerializedAsListWithPropertyAnnotations() throws IOException 148 { 149 ListWrapperSimple input = new ListWrapperSimple("bar"); 150 assertEquals("{\"values\":[{\"value\":\"bar\"}]}", MAPPER.writeValueAsString(input)); 151 } 152 testSerializedAsMapWithClassSerializer()153 public void testSerializedAsMapWithClassSerializer() throws IOException 154 { 155 SimpleValueMapWithSerializer map = new SimpleValueMapWithSerializer(); 156 map.put(new SimpleKey("abc"), new ActualValue("123")); 157 assertEquals("{\"key abc\":\"value 123\"}", MAPPER.writeValueAsString(map)); 158 } 159 testSerializedAsMapWithPropertyAnnotations()160 public void testSerializedAsMapWithPropertyAnnotations() throws IOException 161 { 162 MapWrapperSimple input = new MapWrapperSimple("a", "b"); 163 assertEquals("{\"values\":{\"toString:a\":{\"value\":\"b\"}}}", 164 MAPPER.writeValueAsString(input)); 165 } 166 testSerializedAsListWithPropertyAnnotations2()167 public void testSerializedAsListWithPropertyAnnotations2() throws IOException 168 { 169 ListWrapperWithSerializer input = new ListWrapperWithSerializer("abc"); 170 assertEquals("{\"values\":[\"value abc\"]}", MAPPER.writeValueAsString(input)); 171 } 172 testSerializedAsMapWithPropertyAnnotations2()173 public void testSerializedAsMapWithPropertyAnnotations2() throws IOException 174 { 175 MapWrapperWithSerializer input = new MapWrapperWithSerializer("foo", "b"); 176 assertEquals("{\"values\":{\"key foo\":\"value b\"}}", MAPPER.writeValueAsString(input)); 177 } 178 testEmptyInclusionContainers()179 public void testEmptyInclusionContainers() throws IOException 180 { 181 ObjectMapper defMapper = MAPPER; 182 ObjectMapper inclMapper = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_EMPTY); 183 184 ListWrapper<String> list = new ListWrapper<String>(); 185 assertEquals("{\"list\":[]}", defMapper.writeValueAsString(list)); 186 assertEquals("{}", inclMapper.writeValueAsString(list)); 187 assertEquals("{}", inclMapper.writeValueAsString(new ListWrapper<String>())); 188 189 MapWrapper<String,Integer> map = new MapWrapper<String,Integer>(new HashMap<String,Integer>()); 190 assertEquals("{\"map\":{}}", defMapper.writeValueAsString(map)); 191 assertEquals("{}", inclMapper.writeValueAsString(map)); 192 assertEquals("{}", inclMapper.writeValueAsString(new MapWrapper<String,Integer>(null))); 193 194 ArrayWrapper<Integer> array = new ArrayWrapper<Integer>(new Integer[0]); 195 assertEquals("{\"array\":[]}", defMapper.writeValueAsString(array)); 196 assertEquals("{}", inclMapper.writeValueAsString(array)); 197 assertEquals("{}", inclMapper.writeValueAsString(new ArrayWrapper<Integer>(null))); 198 } 199 testNullSerializer()200 public void testNullSerializer() throws Exception 201 { 202 String json = MAPPER.writeValueAsString(new NullBean()); 203 assertEquals("{\"value\":null}", json); 204 } 205 } 206