用Jackson以两种不同的方式序列化一个班级 [英] Serialize one class in two different ways with 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然后(de)序列化到外部世界,我们的前端处理呈现JSON。
However, we use the same POJOs to then (de)serialize to the outside world, where our front end deals with presenting the JSON.
现在,我们需要一种方法让外部世界的序列化包含来自<$ c的引用对象$ c> 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()结果(de)序列化它,可能取决于某些配置对象。这可能吗?你看到了更好的方法吗?
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);
有更多信息。
More info is available on the Jackson Wiki.
还有其他选择,事实证明: Jackson MixIns ,它允许你覆盖(de)类的部分行为而不修改类本身,以及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屋!