|
| 1 | +package com.fasterxml.jackson.module.afterburner.ser; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonGenerator; |
| 4 | +import com.fasterxml.jackson.databind.*; |
| 5 | +import com.fasterxml.jackson.databind.annotation.JsonAppend; |
| 6 | +import com.fasterxml.jackson.databind.cfg.MapperConfig; |
| 7 | +import com.fasterxml.jackson.databind.introspect.AnnotatedClass; |
| 8 | +import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition; |
| 9 | +import com.fasterxml.jackson.databind.ser.VirtualBeanPropertyWriter; |
| 10 | +import com.fasterxml.jackson.databind.util.Annotations; |
| 11 | +import com.fasterxml.jackson.module.afterburner.AfterburnerTestBase; |
| 12 | + |
| 13 | +// most for [module-afterburner#57] |
| 14 | +public class JsonAppendTest extends AfterburnerTestBase |
| 15 | +{ |
| 16 | + @JsonAppend(props = @JsonAppend.Prop(name = "virtual", value = MyVirtualPropertyWriter.class)) |
| 17 | + public static class Pojo { |
| 18 | + public final String name; |
| 19 | + public Pojo(String name) { |
| 20 | + this.name = name; |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + public static class MyVirtualPropertyWriter extends VirtualBeanPropertyWriter { |
| 25 | + protected MyVirtualPropertyWriter() { } |
| 26 | + |
| 27 | + protected MyVirtualPropertyWriter(BeanPropertyDefinition propDef, Annotations contextAnnotations, |
| 28 | + JavaType declaredType) { |
| 29 | + super(propDef, contextAnnotations, declaredType); |
| 30 | + } |
| 31 | + @Override |
| 32 | + protected Object value(Object bean, JsonGenerator g, SerializerProvider prov) throws Exception { |
| 33 | + return "bar"; |
| 34 | + } |
| 35 | + @Override |
| 36 | + public VirtualBeanPropertyWriter withConfig(MapperConfig<?> config, AnnotatedClass declaringClass, |
| 37 | + BeanPropertyDefinition propDef, JavaType type) { |
| 38 | + return new MyVirtualPropertyWriter(propDef, declaringClass.getAnnotations(), type); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + public void testSimpleAppend() throws Exception |
| 43 | + { |
| 44 | + ObjectMapper mapper = mapperWithModule(); |
| 45 | +// ObjectMapper mapper = new ObjectMapper(); |
| 46 | + String json = mapper.writeValueAsString(new Pojo("foo")); |
| 47 | + assertEquals("{\"name\":\"foo\",\"virtual\":\"bar\"}", json); |
| 48 | + } |
| 49 | +} |
0 commit comments