具有单参数的Jackson单参数构造函数因ParameterNameModule失败 [英] Jackson single argument constructor with single argument fails with ParameterNameModule

查看:844
本文介绍了具有单参数的Jackson单参数构造函数因ParameterNameModule失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有ParameterNamesModule的Jackson 2.8.5 for Java 8( https://github. com/FasterXML/jackson-modules-java8 ).

I am using Jackson 2.8.5 with ParameterNamesModule for Java 8 (https://github.com/FasterXML/jackson-modules-java8).

当我想使用单个构造函数使用单个构造函数反序列化一个类时,我的问题对于一个用例而言非常具体.这是一个重现该行为的测试:

My problem is very specific for one use case, when I want to de-serialize a class with a single constructor using a single argument. Here is a test to reproduces the behavior:

public class JacksonTest {

    @Test
    public void TestReadValue() throws IOException {
        ObjectMapper objectMapper = new ObjectMapper()
                .setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
                .setVisibility(PropertyAccessor.CREATOR, JsonAutoDetect.Visibility.PUBLIC_ONLY)
                .registerModule(new ParameterNamesModule(JsonCreator.Mode.PROPERTIES));

        ImmutableIdentity identity = objectMapper.readValue("{\"id\":\"ABCDEF\"}", ImmutableIdentity.class);

        assertEquals("ABCDEF", identity.id);
    }

    private static final class ImmutableIdentity {

        private final String id;

        public ImmutableIdentity(final String id) {
            Objects.requireNonNull(id, "The id must not be null.");

            this.id = id;
        }
    }

}

测试失败的原因如下:

com.fasterxml.jackson.databind.JsonMappingException:无法构造 JacksonTest $ ImmutableIdentity的实例,问题:该ID不得为 空值.在[来源:{"id":"ABCDEF"};行:1,列:15]

com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of JacksonTest$ImmutableIdentity, problem: The id must not be null. at [Source: {"id":"ABCDEF"}; line: 1, column: 15]

有趣的是,如果我向构造函数添加另一个参数,则测试通过OK.

The funny thing is that if I add another argument to the constructor, the test passes OK.

public class JacksonTest {

    @Test
    public void TestReadValue() throws IOException {
        ObjectMapper objectMapper = new ObjectMapper()
                .setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
                .setVisibility(PropertyAccessor.CREATOR, JsonAutoDetect.Visibility.PUBLIC_ONLY)
                .registerModule(new ParameterNamesModule(JsonCreator.Mode.PROPERTIES));

        ImmutableIdentity identity = objectMapper.readValue("{\"id\":\"ABCDEF\"}", ImmutableIdentity.class);

        assertEquals("ABCDEF", identity.id);
    }

    private static final class ImmutableIdentity {

        private final String id;

        public ImmutableIdentity(final String id, **final String unused**) {
            Objects.requireNonNull(id, "The id must not be null.");

            this.id = id;
        }
    }

}

我真的不喜欢在这里的构造函数中使用无用参数来减少歧义的想法,因为它在我的业务对象中没有任何价值,尤其是例如ProjectId或定义我的业务对象的某些抽象ID实体,我也需要手动构建它们.因此,我想找到一种支持Jackson的配置来支持此操作,但是我无法.

I really don't like the idea to use a useless argument in the constructor here to make it less ambiguous, because it has no value in my business objects, especially they are for instance ProjectId, or some abstract Id that defines my entities and I need to construct them manually as well. So I would like to find a configuration of Jackson to support this but I could not.

我也在这里为维护者交叉张贴了文章: https://github.com/FasterXML/jackson-modules-java8/issues/8

I also crossposted here for the maintainers: https://github.com/FasterXML/jackson-modules-java8/issues/8

推荐答案

您是否有机会使用-parameters选项编译JacksonTest?如果是这样,这是预期的行为. 过去,单参数构造函数曾被用作委托创建者. 即使在创建模块时,我也曾与@staxman讨论过此问题.各种用户多次弹出该问题,请参见此问题以获得详细信息. 展望未来,有望在3.0中进行更改,请参见本主题以获得详细信息.

Are you by any chance compiling JacksonTest with -parameters option? If so, this is expected behavior. Single argument constructors were historically used as delegating creators. I've had discussions about this matter with @staxman even when we created the module. The issue popped up a number of times by various users, see this issue for details. Looking to the future, this will hopefully be changed in 3.0, see this topic for details.

更新:关于3.0的更改,请参见此问题.如果您希望更改此行为,请添加+1或评论.目前尚不清楚哪种方法更好,因为有些用户需要旧的行为(有关更多详细信息,请参阅问题).

Update: regarding 3.0 change, see this issue. If you want this behavior to change please add +1 or comment. Right now it isn't clear if either approach is better as there are users that need the old behavior (see the issue for more details).

这篇关于具有单参数的Jackson单参数构造函数因ParameterNameModule失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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