powerdesigner 转为java实体对象时怎么序列化,@JsonSerialize-如何在运行时创建包装器并为对象字段使用默认序列化?...

I want to add a wrapper which named is determined at runtime, because it depends of the class name (I could use @JsonRootName but I don't want to because I would have to use it on every sub class, which is not efficient).

I suppose I should use @JsonSerialize to override the default serializer, but I want that just to create the wrapper; I don't want to serialize the object fields myself (also I am in an abstract class, so I don't even know the fields of the sub class!). I don't care about them, I just care about the wrapper! So I would like the default serializer to handle those fields for me, or something like that.

@JsonSerialize(using = CustomSerializer.class)

public abstract class Request {

public static class CustomSerializer extends JsonSerializer {

@Override

public void serialize(Request request, JsonGenerator jgen, SerializerProvider provider) throws IOException {

// Doing my stuff to determine the wrapper name based on request.class.getSimpleName()

// Then what should I wright to serialize the fields?

// Basically I just want a function to generate the same json that the default serializer would generate!

// I tried the following, but obviously it gives a com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion

jgen.writeObject(value);

// Same error for the function below

provider.defaultSerializeValue(value, jgen);

}

}

解决方案

To create wrapper serialiser you need to use com.fasterxml.jackson.databind.ser.BeanSerializerModifier class. You can register it using com.fasterxml.jackson.databind.module.SimpleModule. Below example shows end-to-end solution how to do that:

import com.fasterxml.jackson.core.JsonGenerator;

import com.fasterxml.jackson.databind.BeanDescription;

import com.fasterxml.jackson.databind.JsonSerializer;

import com.fasterxml.jackson.databind.ObjectMapper;

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.BeanSerializerModifier;

import com.fasterxml.jackson.databind.util.NameTransformer;

import java.io.IOException;

import java.util.UUID;

public class JsonPathApp {

public static void main(String[] args) throws Exception {

SimpleModule wrappersModule = new SimpleModule("requestWrapper");

wrappersModule.setSerializerModifier(new BeanSerializerModifier() {

@Override

public JsonSerializer> modifySerializer(SerializationConfig config, BeanDescription beanDesc, JsonSerializer> serializer) {

if (Request.class.isAssignableFrom(beanDesc.getBeanClass())) {

return new RequestWrapperJsonSerializer(serializer);

}

return serializer;

}

});

ObjectMapper mapper = JsonMapper.builder()

.enable(SerializationFeature.INDENT_OUTPUT)

.addModule(wrappersModule)

.build();

System.out.println(mapper.writeValueAsString(new Request1(1, "POST")));

System.out.println(mapper.writeValueAsString(new Request2(2, UUID.randomUUID())));

}

}

class RequestWrapperJsonSerializer extends JsonSerializer {

private final JsonSerializer baseSerializer;

public RequestWrapperJsonSerializer(JsonSerializer baseSerializer) {

this.baseSerializer = baseSerializer;

}

@Override

public void serialize(Request value, JsonGenerator gen, SerializerProvider serializers) throws IOException {

gen.writeStartObject();

gen.writeFieldName(value.getClass().getSimpleName() + "Wrapper");

gen.writeStartObject();

baseSerializer.unwrappingSerializer(NameTransformer.NOP).serialize(value, gen, serializers);

gen.writeEndObject();

gen.writeEndObject();

}

}

abstract class Request {

private int id;

//constructor, getters, setters, toString

}

class Request1 extends Request {

private String body;

//constructor, getters, setters, toString

}

class Request2 extends Request {

private UUID uuid;

//constructor, getters, setters, toString

}

Above code prints:

{

"Request1Wrapper" : {

"id" : 1,

"body" : "POST"

}

}

{

"Request2Wrapper" : {

"id" : 2,

"uuid" : "dd4cccb5-1cf5-4dd4-8bc7-97cb101e5d7d"

}

}

Instead unwrappingSerializer method you can use serialize method and remove extra wrapping invocations.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值