[英]Is this a bug in the Jackson JsonParser, or am I doing something wrong?
[英]Jackson's mixin class doesn't do the trick: bug or am I doing something wrong?
這是傑克遜2.3.3。 我最近在Jackson上了解了關於mixin注釋的信息 ,我正在嘗試將其應用於課程。 到目前為止沒有成功。 有問題的類是另一個類的靜態類:
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
}
並且沒有其他構造函數。 因此,我這樣寫了mixin類:
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時它失敗:
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類的構造函數, @JsonCreator
,它對於“常規”反序列化始終有效(這是我第一次嘗試mixin)。
這是Jackson的錯誤,還是我做錯了什么?
編輯: main
:
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
:
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);
}
}
確保您的mixin類上的ctor簽名與目標類上的ctor簽名匹配。 您的目標類ctor具有totalNanoTime作為長整數,但是您的mixin ctor具有totalNanoTime作為整數。
░░░░░░░░░▄░░░░░░░░░░░░░░▄░░░░
░░░░░░░░▌▒█░░░░░░░░░░░▄▀▒▌░░░
░░░░░░░░▌▒▒█░░░░░░░░▄▀▒▒▒▐░░░
░░░░░░░▐▄▀▒▒▀▀▀▀▄▄▄▀▒▒▒▒▒▐░░░
░░░░░▄▄▀▒░▒▒▒▒▒▒▒▒▒█▒▒▄█▒▐░░░
░░░▄▀▒▒▒░░░▒▒▒░░░▒▒▒▀██▀▒▌░░░
░░▐▒▒▒▄▄▒▒▒▒░░░▒▒▒▒▒▒▒▀▄▒▒▌░░
░░▌░░▌█▀▒▒▒▒▒▄▀█▄▒▒▒▒▒▒▒█▒▐░░
░▐░░░▒▒▒▒▒▒▒▒▌██▀▒▒░░░▒▒▒▀▄▌░
░▌░▒▄██▄▒▒▒▒▒▒▒▒▒░░░░░░▒▒▒▒▌░
▀▒▀▐▄█▄█▌▄░▀▒▒░░░░░░░░░░▒▒▒▐░
▐▒▒▐▀▐▀▒░▄▄▒▄▒▒▒▒▒▒░▒░▒░▒▒▒▒▌
▐▒▒▒▀▀▄▄▒▒▒▄▒▒▒▒▒▒▒▒░▒░▒░▒▒▐░
░▌▒▒▒▒▒▒▀▀▀▒▒▒▒▒▒░▒░▒░▒░▒▒▒▌░
░▐▒▒▒▒▒▒▒▒▒▒▒▒▒▒░▒░▒░▒▒▄▒▒▐░░
░░▀▄▒▒▒▒▒▒▒▒▒▒▒░▒░▒░▒▄▒▒▒▒▌░░
░░░░▀▄▒▒▒▒▒▒▒▒▒▒▄▄▄▀▒▒▒▒▄▀░░░
░░░░░░▀▄▄▄▄▄▄▀▀▀▒▒▒▒▒▄▄▀░░░░░
░░░░░░░░░▒▒▒▒▒▒▒▒▒▒▀▀░░░░░░░░
這樣的驚奇。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.