谦虚好学的鸵鸟 · Oops!!! - 简书· 1 年前 · |
潇洒的领结 · c++ - std::map::clear ...· 1 年前 · |
小胡子的熊猫 · OAuth2AuthenticationMa ...· 1 年前 · |
我有一个需要序列化的树对象结构,我希望每次都能够使用jackson (或任何其他库--我是开放的)基于参数来控制序列化的深度。
我的类是这样的:
class Node {
private String id;
private Node child;
}
这里有两个序列化的json示例,我希望基于深度级别获得它们。
深度级别设置为3
{
"id": "A",
"child": {
"id": "B",
"child": {
"id": "C",
"child": {}
}
深度级别设置为2
{
"id": "A",
"child": {
"id": "B",
"child": {}
}
有没有办法控制递归对象中序列化的深度?
谢谢
发布于 2020-01-09 05:55:35
您需要实现自定义序列化程序,在其中您需要计算已经处理的
Node
对象的数量。对于每个序列化过程,我们需要提供
max depth
值,并在每次发现
Node
类时递减该值。示例实现:
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.BeanSerializer;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
import com.fasterxml.jackson.databind.ser.std.BeanSerializerBase;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
public class JsonApp {
public static void main(String[] args) throws IOException {
Node nodeD = new Node("D", null);
Node nodeC = new Node("C", nodeD);
Node nodeB = new Node("B", nodeC);
Node nodeA = new Node("A", nodeB);
for (int i = 0; i < 4; i++) {
System.out.println("Depth: " + i);
System.out.println(serialiseWithDepth(nodeA, i));
private static ObjectMapper mapper = JsonMapper.builder()
.enable(SerializationFeature.INDENT_OUTPUT)
.addModule(createNodeModule())
.build();
private static String serialiseWithDepth(Node node, int maxDepth) throws JsonProcessingException {
ObjectWriter writer = mapper.writerFor(Node.class)
.withAttribute(NodeDepthBeanSerializer.DEPTH_KEY, new AtomicInteger(maxDepth));
return writer.writeValueAsString(node);
private static SimpleModule createNodeModule() {
SimpleModule nodeModule = new SimpleModule("NodeModule");
nodeModule.setSerializerModifier(new BeanSerializerModifier() {
@Override
public JsonSerializer<?> modifySerializer(SerializationConfig config, BeanDescription beanDesc, JsonSerializer<?> serializer) {
if (beanDesc.getBeanClass() == Node.class) {
return new NodeDepthBeanSerializer((BeanSerializerBase) serializer);
return super.modifySerializer(config, beanDesc, serializer);
return nodeModule;
class NodeDepthBeanSerializer extends BeanSerializer {
public static final String DEPTH_KEY = "maxDepthSize";
public NodeDepthBeanSerializer(BeanSerializerBase src) {
super(src);
@Override
protected void serializeFields(Object bean, JsonGenerator gen, SerializerProvider provider) throws IOException {
AtomicInteger depth = (AtomicInteger) provider.getAttribute(DEPTH_KEY);
if (depth.decrementAndGet() >= 0) {
super.serializeFields(bean, gen, provider);
}
上面的代码打印:
Depth: 0
Node{id='A', child=Node{id='B', child=Node{id='C', child=Node{id='D', child=null}}}} => 0
Depth: 1
Node{id='A', child=Node{id='B', child=Node{id='C', child=Node{id='D', child=null}}}} => 1
Node{id='B', child=Node{id='C', child=Node{id='D', child=null}}} => 0
"id" : "A",
"child" : { }
Depth: 2
Node{id='A', child=Node{id='B', child=Node{id='C', child=Node{id='D', child=null}}}} => 2
Node{id='B', child=Node{id='C', child=Node{id='D', child=null}}} => 1
Node{id='C', child=Node{id='D', child=null}} => 0
"id" : "A",
"child" : {
"id" : "B",
"child" : { }
Depth: 3
Node{id='A', child=Node{id='B', child=Node{id='C', child=Node{id='D', child=null}}}} => 3
Node{id='B', child=Node{id='C', child=Node{id='D', child=null}}} => 2
Node{id='C', child=Node{id='D', child=null}} => 1
Node{id='D', child=null} => 0
"id" : "A",
"child" : {
"id" : "B",
谦虚好学的鸵鸟 · Oops!!! - 简书 1 年前 |