扩展jackson自定义序列化程序

时间:2015-11-18 04:16:04

标签: java json jackson

鉴于MyClass2扩展了MyClass1并且只向MyClass1添加了两个属性,我为这两个类编写了两个Jackson自定义序列化器,如下所示:

public class MyClass1Serializer extends JsonSerializer<MyClass1> {

    @Override
    public void serialize(MyClass1 myClass1, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {

        jsonGenerator.writeStartObject();
        jsonGenerator.writeStringField("ApplicationName", myClass1.getApplicationName());
        jsonGenerator.writeStringField("UserName", myClass1.getUserName());
    }       
}

public class MyClass2Serializer extends JsonSerializer<MyClass2> {

    @Override
    public void serialize(MyClass2 myClass2, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {

        jsonGenerator.writeStartObject();
        jsonGenerator.writeStringField("ApplicationName", myClass2.getApplicationName());       
        jsonGenerator.writeStringField("ErrorMessage", myClass2.getErrorMessage());
        jsonGenerator.writeStringField("ResultCode", myClass2.getResultCode());
        jsonGenerator.writeStringField("UserName", myClass2.getUserName());
    }       
}

哪个工作正常,并给我以下输出:

{&#34; ApplicationName&#34;:&#34; FakeApp&#34;,&#34; UserName&#34;:&#34; Joe the Schmoe&#34;}

{&#34; ApplicationName&#34;:&#34; AnotherApp&#34;,&#34; ErrorMessage&#34;:&#34;呃哦!&#34;,&#34; ResultCode&#34 ;:&#34; Errrrm,不好......&#34;,&#34; UserName&#34;:&#34; John Doe&#34;}

嗯,对我来说,看起来两个序列化方法中存在代码重复,我可以从第一个继承第二个序列化器吗? Hmmmm ...

嗯,好吧,我试过了:

public class MyClass1Serializer<T extends MyClass1> extends JsonSerializer<MyClass1> {

    @Override
    public void serialize(MyClass1 myClass1, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {

        jsonGenerator.writeStartObject();
        jsonGenerator.writeStringField("ApplicationName", myClass1.getApplicationName());
        jsonGenerator.writeStringField("UserName", myClass1.getUserName());
    }       
}

public class MyClass2Serializer extends MyClass1Serializer<MyClass2> {

    public void serialize(MyClass2 myClass2, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {

        super.serialize(myClass2, jsonGenerator, serializerProvider);
        jsonGenerator.writeStringField("ErrorMessage", myClass2.getErrorMessage());
        jsonGenerator.writeStringField("ResultCode", myClass2.getResultCode());
    }       
}

编译并运行,但给我这个输出:

{&#34; ApplicationName&#34;:&#34; FakeApp&#34;,&#34; UserName&#34;:&#34; Joe the Schmoe&#34;}

{&#34; ApplicationName&#34;:&#34; AnotherApp&#34;,&#34; UserName&#34;:&#34; John Doe&#34;}

我很清楚MyClass2Serializer现在完全被忽略了,Jackson发现了MyClass1Serializer并将其用于MyClass2。 (因为它不直接将JsonSerializer子类化?)

对于这种简单的情况,没有什么大不了的,但是我工作中真实世界的阶级结构可以真正受益于#34;链接&#34;自定义序列化器,而不是从头开始每个。

为了防止重要,我告诉Jackson哪个序列化程序在类中使用注释用于哪个类:

@JsonSerialize(using=MyClass1Serializer.class)
public class MyClass1 {

可能的未来问题:假设甚至可以继承自定义序列化程序,可以继承自定义反序列化程序的工作吗?指向一些示例代码或教程的指针非常棒!

1 个答案:

答案 0 :(得分:1)

有趣的是:我试图复制这个问题,但似乎有效:

import java.io.*;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.*;

public class Test
{
    public static void main(String[] args) {
        MyClass1 myc1 = new MyClass1("app1", "user1");
        MyClass1 myc2 = new MyClass2("app2", "user2", "err2", "rc2");

        try {
            System.out.println(new ObjectMapper().writeValueAsString(myc1));
            System.out.println(new ObjectMapper().writeValueAsString(myc2));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @JsonSerialize(using = MyClass1Serializer.class)
    public static class MyClass1 {
        protected String applicationName;
        protected String userName;

        public MyClass1() {}

        public MyClass1(String applicationName, String userName) {
            this.applicationName = applicationName;
            this.userName = userName;
        }

        public String getApplicationName() { return applicationName; }
        public String getUserName()        { return userName; }
    }

    @JsonSerialize(using = MyClass2Serializer.class)
    public static class MyClass2 extends MyClass1 {
        protected String errorMessage;
        protected String resultCode;

        public MyClass2() {}

        public MyClass2(String applicationName, String userName, String errorMessage, String resultCode) {
            super(applicationName, userName);
            this.errorMessage = errorMessage;
            this.resultCode = resultCode;
        }

        public String getErrorMessage() { return errorMessage; }
        public String getResultCode()   { return resultCode; }
    }

    public static class MyClass1Serializer<T extends MyClass1> extends JsonSerializer<T> {
        @Override
        public void serialize(MyClass1 myClass1, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
                throws IOException
        {
            jsonGenerator.writeStartObject();
            jsonGenerator.writeStringField("ApplicationName", myClass1.getApplicationName());
            jsonGenerator.writeStringField("UserName", myClass1.getUserName());
            //jsonGenerator.writeEndObject();
        }
    }

    public static class MyClass2Serializer extends MyClass1Serializer<MyClass2> {
        @Override
        public void serialize(MyClass2 myClass2, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
                throws IOException
        {
            super.serialize(myClass2, jsonGenerator, serializerProvider);
            jsonGenerator.writeStringField("ErrorMessage", myClass2.getErrorMessage());
            jsonGenerator.writeStringField("ResultCode", myClass2.getResultCode());
            //jsonGenerator.writeEndObject();
        }
    }
}

输出:

{"ApplicationName":"app1","UserName":"user1"}
{"ApplicationName":"app2","UserName":"user2","ErrorMessage":"err2","ResultCode":"rc2"}