同一类的不同JSON序列化(Different JSON serialization for the same class)

编程入门 行业动态 更新时间:2024-10-27 16:38:45
同一类的不同JSON序列化(Different JSON serialization for the same class)

足够常见的问题:我想以两种不同的方式序列化的类。 在一种情况下,我想包括getItems()方法的输出,在另一种情况下,我不想在输出中看到这一点。

选择使用Jackson Views,因为它给了我最大的灵活性。 创建:

public class Views { public static class WithOrderItems { } }

然后在要序列化的类中:

@JsonView(Views.WithOrderItems.class) public Iterable<OrderItem> getItems() { //Code... }

在进行序列化的方法中:

// Expectation: this *should not* include "items" in JSON output mapper.writeValueAsString(retObj)

返回相同:

// Expectation: this should include "items" in JSON output mapper.writerWithView(Views.WithOrderItems.class).writeValueAsString(retObj)

在这两种情况下,整个对象都被序列化(就好像忽略了View一样)。 为什么会这样?

我在网上找到的大多数文档,教程等都适用于旧版本的Jackson。 我错过了一些设置吗? 据我所知,使用视图注释的方法不应包含在默认mapper 。

我的mapper配置是:

public static ObjectMapper mapper = new ObjectMapper().registerModule(new GuavaModule()) .registerModule(MoneySerializer.getAsModule()); static { mapper.setSerializationInclusion(Include.NON_ABSENT); }

( getAsModule()是临时代码MoneySerializer用于Joda Money对象,在这种情况下不被调用。)

想要避免使用mixins,因为这意味着输出和非输出的静态配置。

使用:

杰克逊:2.6.1 Java 7 GAE SDK 1.9.30 客观化5.1.7

Common enough problem: a class that I want to serialize in two different ways. In one case I want to include the output of getItems() method and in another case I don't want to see this in the output.

Chose to use Jackson Views as it gives me the maximum flexibility. Created:

public class Views { public static class WithOrderItems { } }

Then in the class to be serialized:

@JsonView(Views.WithOrderItems.class) public Iterable<OrderItem> getItems() { //Code... }

In the method doing the serialization:

// Expectation: this *should not* include "items" in JSON output mapper.writeValueAsString(retObj)

returns the same as:

// Expectation: this should include "items" in JSON output mapper.writerWithView(Views.WithOrderItems.class).writeValueAsString(retObj)

In both cases the entire object is being serialized (it's as if the View is being ignored). Why is this happening?

Most of the documentation, tutorials etc. I can find online are for older versions of Jackson. Is there some setting that I am missing out on? As far as I understand, methods annotated with a view should not be included in a default mapper.

My mapper configuration is:

public static ObjectMapper mapper = new ObjectMapper().registerModule(new GuavaModule()) .registerModule(MoneySerializer.getAsModule()); static { mapper.setSerializationInclusion(Include.NON_ABSENT); }

(The getAsModule() is temporary code. MoneySerializer is for Joda Money objects and isn't being invoked in this case.)

Want to avoid mixins as that means static configuration of what is output and what isn't.

Using:

Jackson: 2.6.1 Java 7 GAE SDK 1.9.30 Objectify 5.1.7

最满意答案

据我所知,使用视图注释的方法不应包含在默认映射器中。

我不认为这是真的。 如果您不提供任何视图,则映射器应序列化所有内容。

注意:即使您只想使用“默认”视图 - 也就是说,只排除仅包含在特定“完整”视图中的内容 - 您需要通过指定视图来启用视图处理。 如果您没有明确的“基本”视图设置,只需使用Object.class。

从这里开始

As far as I understand, methods annotated with a view should not be included in a default mapper.

I do not think it is true. If you do not provide any view, mapper should serialize everything.

NOTE: even if you only want to use "default" view -- that is, just exclude things that are only to be included in specific "full" view -- you DO need to enable View processing by specifying a view. If you do not have explicit "basic" view setting, just use Object.class.

Taken from here

更多推荐

本文发布于:2023-07-29 14:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1316720.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:序列化   JSON   class   serialization

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!