如何在另一个自定义JsonSerializer Jackson中调用JsonSerializer

时间:2015-08-26 13:55:47

标签: java json jackson

我有两节课。首先是二等领域。

class A {
  @JsonSerializer(using = CustomBSerializer.class)
  private B b;
}

class B {
  ...
}

我有两个自定义序列化器:

class CustomBSerializer extends JsonSerializer<B> {
  ...
}

class CustomASerializer extends JsonSerializer<A> {
    @Override
    public void serialize(A a, JsonGenerator jgen, SerializerProvider provider)
            throws IOException, JsonProcessingException {
        jgen.writeStartObject();
        //write here
        jgen.writeEndObject();
    }

}

我应该在CustomASerializer中添加哪种方法而不是评论,以便使用b来编写字段CustomBSerializer的序列化值?

2 个答案:

答案 0 :(得分:3)

您可以使用writeObjectField方法。如果存在,Jackson应使用默认或自定义序列化程序。

以下示例:

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import java.io.IOException;
import java.util.Date;

public class Main {

    public static void main(String[] args) throws Exception {
        B b = new B();
        b.setProperty("Value");

        A a = new A();
        a.setB(b);

        ObjectMapper mapper = new ObjectMapper();
        System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(a));
    }
}

@JsonSerialize(using = ASerializer.class)
class A {

    private B b;

    public B getB() {
        return b;
    }

    public void setB(B b) {
        this.b = b;
    }
}

class ASerializer extends JsonSerializer<A> {

    @Override
    public void serialize(A value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
        gen.writeStartObject();
        gen.writeStringField("now", new Date().toString());
        gen.writeObjectField("b", value.getB());
        gen.writeEndObject();
    }
}

@JsonSerialize(using = BSerializer.class)
class B {

    private String property;

    public String getProperty() {
        return property;
    }

    public void setProperty(String property) {
        this.property = property;
    }
}

class BSerializer extends JsonSerializer<B> {

    @Override
    public void serialize(B value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
        gen.writeStartObject();
        gen.writeStringField("p", value.getProperty());
        gen.writeEndObject();
    }
}

打印:

{
  "now" : "Wed Aug 26 22:27:08 CEST 2015",
  "b" : {
    "p" : "Value"
  }
}

答案 1 :(得分:0)

您并非总是有权访问类代码,也不会总是有可能修改A或B类以放置批注@JsonSerialize(使用= BSerializer.class)。在这些情况下,您将需要在CustomASerializer中调用CustomBSerializer,这也是问题首先提出的问题。这是这样做的方法:

class CustomASerializer extends JsonSerializer<A> {

    @Override
    public void serialize(A a, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {

        final ObjectMapper mapper = (ObjectMapper)jgen.getCodec();
        SimpleModule module =  new SimpleModule("CustomBSerializer");
        module.addSerializer(B.class, new CustomBSerializer());
        mapper.registerModule(module);

        jgen.writeStartObject();
        mapper.writeValue(jgen, a.getB());
        jgen.writeEndObject();
    }

}