Last active
October 13, 2017 14:09
-
-
Save milovtim/b2c0936aa163290759bf1ca4667ca131 to your computer and use it in GitHub Desktop.
Sample snipped showing how to setup virtual property/attribute in jackson objectmapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.fasterxml.jackson.core.JsonGenerator | |
import com.fasterxml.jackson.databind.JavaType | |
import com.fasterxml.jackson.databind.ObjectMapper | |
import com.fasterxml.jackson.databind.SequenceWriter | |
import com.fasterxml.jackson.databind.SerializationFeature | |
import com.fasterxml.jackson.databind.SerializerProvider | |
import com.fasterxml.jackson.databind.annotation.JsonAppend | |
import com.fasterxml.jackson.databind.annotation.JsonSerialize | |
import com.fasterxml.jackson.databind.cfg.MapperConfig | |
import com.fasterxml.jackson.databind.introspect.AnnotatedClass | |
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition | |
import com.fasterxml.jackson.databind.ser.VirtualBeanPropertyWriter | |
import com.fasterxml.jackson.databind.util.Annotations | |
import com.fasterxml.jackson.databind.util.StdConverter | |
class Globals { | |
public static final String HOSTNAME_ATTR = 'hostname' | |
} | |
class ReverseStringConverter extends StdConverter<String, String> { | |
@Override | |
String convert(String s) { | |
return s.reverse() | |
} | |
} | |
@JsonAppend( | |
prepend = true | |
,attrs = @JsonAppend.Attr(value = Globals.HOSTNAME_ATTR) | |
,props = @JsonAppend.Prop(value = CustomVirtualBeanPropertyWriter, name = "keySum", type = Integer) | |
) | |
class A { | |
Map<Integer, String> vals | |
@JsonSerialize(contentConverter = ReverseStringConverter) | |
Map<Integer, String> getVals() { | |
return vals | |
} | |
} | |
class CustomVirtualBeanPropertyWriter extends VirtualBeanPropertyWriter { | |
CustomVirtualBeanPropertyWriter() {} | |
CustomVirtualBeanPropertyWriter(BeanPropertyDefinition propDef, Annotations contextAnnotations, JavaType declaredType) { | |
super(propDef, contextAnnotations, declaredType) | |
} | |
@Override | |
protected Object value(Object bean, JsonGenerator gen, SerializerProvider prov) throws Exception { | |
return (bean as A).vals?.keySet()?.sum() | |
} | |
@Override | |
VirtualBeanPropertyWriter withConfig(MapperConfig<?> config, AnnotatedClass declaringClass, BeanPropertyDefinition propDef, JavaType type) { | |
//here we can inspect current configuration of jackson writer to tweak customizations for generated virtual field | |
return new CustomVirtualBeanPropertyWriter(propDef, declaringClass.annotations, type) | |
} | |
} | |
new ObjectMapper() | |
.writerFor(A) | |
.with(SerializationFeature.INDENT_OUTPUT) | |
.withAttribute(Globals.HOSTNAME_ATTR, InetAddress.getLocalHost().hostName) | |
.writeValues(System.out)//write result to console | |
.with { SequenceWriter seqWriter -> | |
3.times {//write objects three times | |
seqWriter.write( | |
new A( | |
vals: (0..it).collectEntries { [(it), "hello$it" as String] }//just generate simple hashmap for field | |
) | |
) | |
} | |
null | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: