Skip to content

Instantly share code, notes, and snippets.

@serkan-ozal
Created January 30, 2020 18:32
Show Gist options
  • Save serkan-ozal/aebcda1fdb64b136f016596a80e9c6a0 to your computer and use it in GitHub Desktop.
Save serkan-ozal/aebcda1fdb64b136f016596a80e9c6a0 to your computer and use it in GitHub Desktop.
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonStreamContext;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
import com.fasterxml.jackson.databind.ser.PropertyWriter;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import com.fasterxml.jackson.databind.type.*;
/**
* @author serkan
*/
public class JacksonMaxDepthExample {
private static final int MAX_OBJECT_DEPTH_ON_SERIALIZATION = 3;
private static class DepthAwarePropertyFilter extends SimpleBeanPropertyFilter {
private static final String FILTER_ID = "DepthAwarePropertyFilter";
private final int maxDepth;
private DepthAwarePropertyFilter(int maxDepth) {
this.maxDepth = maxDepth;
}
private int calculateDepth(PropertyWriter writer, JsonGenerator jgen) {
JsonStreamContext sc = jgen.getOutputContext();
int depth = -1;
while (sc != null) {
sc = sc.getParent();
depth++;
}
return depth;
}
@Override
public void serializeAsField(Object pojo, JsonGenerator gen, SerializerProvider provider, PropertyWriter writer)
throws Exception {
int depth = calculateDepth(writer, gen);
if (depth <= maxDepth) {
try {
writer.serializeAsField(pojo, gen, provider);
} catch (Throwable t) {
writer.serializeAsOmittedField(pojo, gen, provider);
}
} else {
// comment this if you don't want {} placeholders
writer.serializeAsOmittedField(pojo, gen, provider);
}
}
}
private static class DepthAwareBeanSerializerModifier extends BeanSerializerModifier {
@Override
public JsonSerializer<?> modifySerializer(
SerializationConfig config, BeanDescription desc, JsonSerializer<?> serializer) {
return serializer.withFilterId(DepthAwarePropertyFilter.FILTER_ID);
}
@Override
public JsonSerializer<?> modifyArraySerializer(
SerializationConfig config, ArrayType valueType, BeanDescription beanDesc, JsonSerializer<?> serializer) {
return serializer.withFilterId(DepthAwarePropertyFilter.FILTER_ID);
}
@Override
public JsonSerializer<?> modifyCollectionSerializer(
SerializationConfig config, CollectionType valueType, BeanDescription beanDesc, JsonSerializer<?> serializer) {
return serializer.withFilterId(DepthAwarePropertyFilter.FILTER_ID);
}
@Override
public JsonSerializer<?> modifyCollectionLikeSerializer(
SerializationConfig config, CollectionLikeType valueType, BeanDescription beanDesc, JsonSerializer<?> serializer) {
return serializer.withFilterId(DepthAwarePropertyFilter.FILTER_ID);
}
@Override
public JsonSerializer<?> modifyMapSerializer(
SerializationConfig config, MapType valueType, BeanDescription beanDesc, JsonSerializer<?> serializer) {
return serializer.withFilterId(DepthAwarePropertyFilter.FILTER_ID);
}
@Override
public JsonSerializer<?> modifyMapLikeSerializer(
SerializationConfig config, MapLikeType valueType, BeanDescription beanDesc, JsonSerializer<?> serializer) {
return serializer.withFilterId(DepthAwarePropertyFilter.FILTER_ID);
}
@Override
public JsonSerializer<?> modifyEnumSerializer(
SerializationConfig config, JavaType valueType, BeanDescription beanDesc, JsonSerializer<?> serializer) {
return serializer.withFilterId(DepthAwarePropertyFilter.FILTER_ID);
}
@Override
public JsonSerializer<?> modifyKeySerializer(
SerializationConfig config, JavaType valueType, BeanDescription beanDesc, JsonSerializer<?> serializer) {
return serializer.withFilterId(DepthAwarePropertyFilter.FILTER_ID);
}
}
public static void main(String[] args) {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
objectMapper.setFilterProvider(
new SimpleFilterProvider().
addFilter(DepthAwarePropertyFilter.FILTER_ID,
new DepthAwarePropertyFilter(MAX_OBJECT_DEPTH_ON_SERIALIZATION)));
objectMapper.registerModule(new SimpleModule() {
@Override
public void setupModule(SetupContext context) {
super.setupModule(context);
context.addBeanSerializerModifier(new DepthAwareBeanSerializerModifier());
}
});
}
}
@raxetul
Copy link

raxetul commented Jan 31, 2020

Neden maksimum derinlik var?

@raxetul
Copy link

raxetul commented Jan 31, 2020

Neden maksimum derinlik var?

Pardon, amacın kırpmak imiş.
DepthLimittedPropertyFilter gibi bir isim daha güzel olur sanki.

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