首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >杰克逊的混音课并没有奏效:错误还是我做错了什么?

杰克逊的混音课并没有奏效:错误还是我做错了什么?
EN

Stack Overflow用户
提问于 2014-05-07 11:54:23
回答 1查看 1.3K关注 0票数 1

这是杰克逊2.3.3。我最近在杰克逊中学到了混合注释,我正在尝试把它应用到一个类中。然而,到目前为止,还没有成功。所讨论的类是另一个类中的静态类:

代码语言:javascript
复制
public static class Report
{
    // Some non relevant public static final fields, then:

    public final int totalRuns;
    public final int totalInvocations;
    public final int totalMatches;
    public final int totalMismatches;
    public final double matchShare;
    public final int reinvocations;
    public final int rematches;
    public final int remismatches;
    public final double reinvocationShare;
    public final long totalNanoTime;
    public final List<RuleReport> ruleReports;

    public Report(final int totalRuns, final int totalMatches,
        final int totalMismatches, final int rematches,
        final int remismatches, final long totalNanoTime,
        final List<RuleReport> ruleReports)
    {
        this.totalRuns = totalRuns;
        this.totalInvocations = totalMatches + totalMismatches;
        this.totalMatches = totalMatches;
        this.totalMismatches = totalMismatches;
        this.matchShare = (double) totalMatches / (double) totalInvocations;
        this.reinvocations = rematches + remismatches;
        this.rematches = rematches;
        this.remismatches = remismatches;
        this.reinvocationShare = (double) reinvocations
            / (double) totalInvocations;
        this.totalNanoTime = totalNanoTime;
        this.ruleReports = ruleReports;
    }
    // Other, non relevant methods
}

也没有其他的建设者。因此,我以这样的方式编写了我的混音类:

代码语言:javascript
复制
public abstract class ProfilingReportMixin
{
    @JsonProperty("runs")
    private int totalRuns;
    @JsonIgnore
    private int totalInvocations;
    @JsonProperty("matches")
    private int totalMatches;
    @JsonProperty("mismatches")
    private int totalMismatches;
    @JsonIgnore
    private double matchShare;
    @JsonIgnore
    private int reinvocations;
    @JsonProperty("rematches")
    private int rematches;
    @JsonProperty("remismatches")
    private int remismatches;
    @JsonIgnore
    private double reinvocationShare;
    @JsonIgnore
    private long totalNanoTime;
    @JsonProperty("ruleReports")
    private List<RuleReport> ruleReports;

    @JsonCreator
    protected ProfilingReportMixin(
        @JsonProperty("runs") final int totalRuns,
        @JsonProperty("matches") final int totalMatches,
        @JsonProperty("mismatches") final int totalMismatches,
        @JsonProperty("rematches") final int rematches,
        @JsonProperty("remismatches") final int remismatches,
        @JsonProperty("totalNanoTime") final int totalNanoTime,
        @JsonProperty("ruleReports") final List<RuleReport> ruleReports
    )
    {
    }
}

显然,由于没有"bean构造器“,所以我决定采用@JsonCreator方式。

我创建Module,注册它,没问题.但是,当试图反序列化一个示例JSON时,它失败了:

代码语言:javascript
复制
Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class org.parboiled.parserunners.ProfilingParseRunner$Report]: can not instantiate from JSON object (need to add/enable type information?)
 at [Source: java.io.BufferedInputStream@b41b571; line: 2, column: 5]
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1078)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:268)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:124)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2993)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2144)
    at com.github.parboiled1.grappa.assertions.mixins.ProfilingReportMixin.main(ProfilingReportMixin.java:70)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

我真的不明白;我用@JsonCreator注释了mixin类的构造函数,到目前为止,它一直适用于“正常”反序列化(这是我第一次尝试混合)。

这是杰克逊的错误还是我做错了什么?

编辑:main

代码语言:javascript
复制
public static void main(final String... args)
    throws IOException
{
    final ObjectMapper mapper = new ObjectMapper()
        .configure(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, true);
    mapper.registerModule(GrappaModule.INSTANCE);

    final Closer closer = Closer.create();
    final InputStream in;

    try {
        in = closer.register(ProfilingReportMixin.class.
            getResourceAsStream("/profilingReports/test.json"));
        if (in == null)
            throw new IOException("resource not found");
        final ProfilingParseRunner.Report report
            = mapper.readValue(in, ProfilingParseRunner.Report.class);
        mapper.writerWithDefaultPrettyPrinter()
            .writeValue(System.out, report);
    } finally {
        closer.close();
    }
}

Module

代码语言:javascript
复制
public final class GrappaModule
    extends SimpleModule
{
    private static final Version VERSION = new Version(1, 0, 0,
        "beta.5-SNAPSHOT", "com.github.parboiled1", "grappa");

    public static final Module INSTANCE = new GrappaModule();

    private GrappaModule()
    {
        super("grappa", VERSION);
    }

    @Override
    public void setupModule(final SetupContext context)
    {
        context.setMixInAnnotations(ProfilingParseRunner.RuleReport.class,
            RuleReportMixin.class);
        context.setMixInAnnotations(ProfilingParseRunner.Report.class,
            ProfilingReportMixin.class);
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-07 12:32:37

确保mixin类上的ctor签名与目标类上的ctor签名匹配。您的目标类ctor以totalNanoTime作为长,而mixin将totalNanoTime作为int。

代码语言:javascript
复制
░░░░░░░░░▄░░░░░░░░░░░░░░▄░░░░
░░░░░░░░▌▒█░░░░░░░░░░░▄▀▒▌░░░
░░░░░░░░▌▒▒█░░░░░░░░▄▀▒▒▒▐░░░
░░░░░░░▐▄▀▒▒▀▀▀▀▄▄▄▀▒▒▒▒▒▐░░░
░░░░░▄▄▀▒░▒▒▒▒▒▒▒▒▒█▒▒▄█▒▐░░░
░░░▄▀▒▒▒░░░▒▒▒░░░▒▒▒▀██▀▒▌░░░ 
░░▐▒▒▒▄▄▒▒▒▒░░░▒▒▒▒▒▒▒▀▄▒▒▌░░
░░▌░░▌█▀▒▒▒▒▒▄▀█▄▒▒▒▒▒▒▒█▒▐░░
░▐░░░▒▒▒▒▒▒▒▒▌██▀▒▒░░░▒▒▒▀▄▌░
░▌░▒▄██▄▒▒▒▒▒▒▒▒▒░░░░░░▒▒▒▒▌░
▀▒▀▐▄█▄█▌▄░▀▒▒░░░░░░░░░░▒▒▒▐░
▐▒▒▐▀▐▀▒░▄▄▒▄▒▒▒▒▒▒░▒░▒░▒▒▒▒▌
▐▒▒▒▀▀▄▄▒▒▒▄▒▒▒▒▒▒▒▒░▒░▒░▒▒▐░
░▌▒▒▒▒▒▒▀▀▀▒▒▒▒▒▒░▒░▒░▒░▒▒▒▌░
░▐▒▒▒▒▒▒▒▒▒▒▒▒▒▒░▒░▒░▒▒▄▒▒▐░░
░░▀▄▒▒▒▒▒▒▒▒▒▒▒░▒░▒░▒▄▒▒▒▒▌░░
░░░░▀▄▒▒▒▒▒▒▒▒▒▒▄▄▄▀▒▒▒▒▄▀░░░
░░░░░░▀▄▄▄▄▄▄▀▀▀▒▒▒▒▒▄▄▀░░░░░
░░░░░░░░░▒▒▒▒▒▒▒▒▒▒▀▀░░░░░░░░

真让人吃惊。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23517120

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档