|
| 1 | +package com.fasterxml.jackson.module.afterburner.ser; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.core.*; |
| 6 | + |
| 7 | +import com.fasterxml.jackson.databind.*; |
| 8 | +import com.fasterxml.jackson.databind.annotation.JsonSerialize; |
| 9 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 10 | +import com.fasterxml.jackson.databind.ser.DefaultSerializerProvider; |
| 11 | +import com.fasterxml.jackson.databind.ser.SerializerFactory; |
| 12 | +import com.fasterxml.jackson.module.afterburner.AfterburnerTestBase; |
| 13 | + |
| 14 | +// Copied from [com.fasterxml.jackson.databind.ser.filter] |
| 15 | +public class NullSerializationTest extends AfterburnerTestBase |
| 16 | +{ |
| 17 | + static class NullSerializer extends JsonSerializer<Object> |
| 18 | + { |
| 19 | + @Override |
| 20 | + public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) |
| 21 | + throws IOException |
| 22 | + { |
| 23 | + gen.writeString("foobar"); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + static class Bean1 { |
| 28 | + public String name = null; |
| 29 | + } |
| 30 | + |
| 31 | + static class Bean2 { |
| 32 | + public String type = null; |
| 33 | + } |
| 34 | + |
| 35 | + @SuppressWarnings("serial") |
| 36 | + static class MyNullProvider extends DefaultSerializerProvider |
| 37 | + { |
| 38 | + public MyNullProvider() { super(); } |
| 39 | + public MyNullProvider(MyNullProvider base, SerializationConfig config, SerializerFactory jsf) { |
| 40 | + super(base, config, jsf); |
| 41 | + } |
| 42 | + |
| 43 | + // not really a proper impl, but has to do |
| 44 | + @Override |
| 45 | + public DefaultSerializerProvider copy() { |
| 46 | + return this; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public DefaultSerializerProvider createInstance(SerializationConfig config, SerializerFactory jsf) { |
| 51 | + return new MyNullProvider(this, config, jsf); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public JsonSerializer<Object> findNullValueSerializer(BeanProperty property) |
| 56 | + throws JsonMappingException |
| 57 | + { |
| 58 | + if ("name".equals(property.getName())) { |
| 59 | + return new NullSerializer(); |
| 60 | + } |
| 61 | + return super.findNullValueSerializer(property); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + static class BeanWithNullProps |
| 66 | + { |
| 67 | + @JsonSerialize(nullsUsing=NullSerializer.class) |
| 68 | + public String a = null; |
| 69 | + } |
| 70 | + |
| 71 | + /* |
| 72 | + /********************************************************** |
| 73 | + /* Test methods |
| 74 | + /********************************************************** |
| 75 | + */ |
| 76 | + |
| 77 | + private final ObjectMapper MAPPER = mapperWithModule(); |
| 78 | + |
| 79 | + public void testSimple() throws Exception |
| 80 | + { |
| 81 | + assertEquals("null", MAPPER.writeValueAsString(null)); |
| 82 | + } |
| 83 | + |
| 84 | + public void testOverriddenDefaultNulls() throws Exception |
| 85 | + { |
| 86 | + DefaultSerializerProvider sp = new DefaultSerializerProvider.Impl(); |
| 87 | + sp.setNullValueSerializer(new NullSerializer()); |
| 88 | + ObjectMapper m = new ObjectMapper(); |
| 89 | + m.setSerializerProvider(sp); |
| 90 | + assertEquals("\"foobar\"", m.writeValueAsString(null)); |
| 91 | + } |
| 92 | + |
| 93 | + public void testCustomPOJONullsViaProvider() throws Exception |
| 94 | + { |
| 95 | + ObjectMapper m = new ObjectMapper(); |
| 96 | + m.setSerializerProvider(new MyNullProvider()); |
| 97 | + assertEquals("{\"name\":\"foobar\"}", m.writeValueAsString(new Bean1())); |
| 98 | + assertEquals("{\"type\":null}", m.writeValueAsString(new Bean2())); |
| 99 | + } |
| 100 | + |
| 101 | + public void testCustomTreeNullsViaProvider() throws Exception |
| 102 | + { |
| 103 | + ObjectNode root = MAPPER.createObjectNode(); |
| 104 | + root.putNull("a"); |
| 105 | + |
| 106 | + // by default, null is... well, null |
| 107 | + assertEquals("{\"a\":null}", MAPPER.writeValueAsString(root)); |
| 108 | + |
| 109 | + // but then we can customize it: |
| 110 | + DefaultSerializerProvider prov = new MyNullProvider(); |
| 111 | + prov.setNullValueSerializer(new NullSerializer()); |
| 112 | + ObjectMapper m = new ObjectMapper(); |
| 113 | + m.setSerializerProvider(prov); |
| 114 | + assertEquals("{\"a\":\"foobar\"}", m.writeValueAsString(root)); |
| 115 | + } |
| 116 | + |
| 117 | + public void testNullSerializeViaPropertyAnnotation() throws Exception |
| 118 | + { |
| 119 | + assertEquals("{\"a\":\"foobar\"}", MAPPER.writeValueAsString(new BeanWithNullProps())); |
| 120 | + } |
| 121 | +} |
0 commit comments