使用 Jackson 以两种不同的方式序列化一个类 [英] Serialize one class in two different ways with Jackson

查看:18
本文介绍了使用 Jackson 以两种不同的方式序列化一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的一个项目中,我们使用 java webapp 与 MongoDB 实例通信.在数据库中,我们使用DBRefs 来跟踪一些对象关系.我们使用 jackson(使用 mongodb-jackson-mapper)对 POJO 对象进行(反)序列化.

In one of our projects we use a java webapp talking to a MongoDB instance. In the database, we use DBRefs to keep track of some object relations. We (de)serialize with POJO objects using jackson (using mongodb-jackson-mapper).

但是,我们使用相同的 POJO 然后(反)序列化到外部世界,我们的前端处理呈现 JSON.

However, we use the same POJOs to then (de)serialize to the outside world, where our front end deals with presenting the JSON.

现在,我们需要一种外部世界的序列化方法来包含来自 DBRef 的引用对象(以便 UI 可以呈现完整的对象),而我们显然希望拥有DBRef 写入数据库,而不是整个对象.

Now, we need a way for the serialization for the outside world to contain the referenced object from a DBRef (so that the UI can present the full object), while we obviously want to have the DBRef written to the database, and not the whole object.

现在我写了一些未经测试的静态嵌套类代码:

Right now I wrote some untested static nested class code:

public static class FooReference {
    public DBRef<Foo> foo;

    // FIXME how to ensure that this doesn't go into the database?
    public Foo getFoo() {
        return foo.fetch();
    }
}

理想情况下,我想要一种对此进行注释的方法,以便我可以在有或没有 getFoo() 结果的情况下(反)序列化它,这可能取决于某些配置对象.这可能吗?你认为有更好的方法来做这件事吗?

Ideally I would like a way to annotate this so that I could (de)serialize it either with or without the getFoo() result, probably depending on some configuration object. Is this possible? Do you see a better way of going about doing this?

推荐答案

从选项来看,您似乎可以注释属性,以便仅在将给定的 View 传递给 时才显示该属性ObjectMapper 用于序列化.您可以因此编辑类:

From looking at options, it seems you can annotate properties to only be shown if a given View is passed to the ObjectMapper used for serialization. You could thus edit the class:

public static class FooReference {
    public DBRef<Foo> foo;

    @JsonView(Views.WebView.class)
    public Foo getFoo() {
        return foo.fetch();
    }
}

并提供:

class Views {
    static class WebView { }
}

然后在使用正确视图创建配置后进行序列化:

and then serialize after creating a configuration with the correct view:

SerializationConfig conf = objectMapper.getSerializationConfig().withView(Views.WebView.class);
objectMapper.setSerializationConfig(conf);

然后将其序列化.在使用 MongoDB 包装器序列化时不指定视图将意味着该方法将被忽略.没有 JsonView 注释的属性默认是序列化的,你可以通过指定来改变这种行为:

Which would then serialize it. Not specifying the view when serializing with the MongoDB wrapper would mean the method would be ignored. Properties without a JsonView annotation are serialized by default, a behaviour you can change by specifying:

objectMapper.configure(SerializationConfig.Feature.DEFAULT_VIEW_INCLUSION, false);

在杰克逊维基上提供了更多信息.

事实证明,还有其他选择:有 Jackson MixIns 可以让您在不修改类本身的情况下覆盖(反)类部分的序列化行为,从 Jackson 2.0(最新版本)开始,有 过滤器,也是.

There are still other alternatives, too, it turns out: there are Jackson MixIns which would let you override (de)serialization behaviour of parts of a class without modifying the class itself, and as of Jackson 2.0 (very recent release) there are filters, too.

这篇关于使用 Jackson 以两种不同的方式序列化一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆