Skip to content

Instantly share code, notes, and snippets.

@milovtim
Last active October 13, 2017 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milovtim/b2c0936aa163290759bf1ca4667ca131 to your computer and use it in GitHub Desktop.
Save milovtim/b2c0936aa163290759bf1ca4667ca131 to your computer and use it in GitHub Desktop.
Sample snipped showing how to setup virtual property/attribute in jackson objectmapper
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
}
@milovtim
Copy link
Author

Output:

{
  "keySum" : 0,
  "hostname" : "SOME-HOSTNAME",
  "vals" : {
    "0" : "0olleh"
  }
} {
  "keySum" : 1,
  "hostname" : "SOME-HOSTNAME",
  "vals" : {
    "0" : "0olleh",
    "1" : "1olleh"
  }
} {
  "keySum" : 3,
  "hostname" : "SOME-HOSTNAME",
  "vals" : {
    "0" : "0olleh",
    "1" : "1olleh",
    "2" : "2olleh"
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment