【发布时间】:2021-12-08 17:03:29
【问题描述】:
假设我正在尝试用 Jackson 序列化以下内容:
public class Investment implements Serializable{
private String shortName;
private MutualFund mutualFund;
public String getShortName() {
return shortName;
}
public void setShortName(String shortName) {
this.shortName = shortName;
}
public MutualFund getMutualFund() {
return mutualFund;
}
public void setMutualFund(MutualFund mutualFund) {
this.mutualFund = mutualFund;
}
}
那又是指:
public class MutualFund implements Serializable{
private BigDecimal expenseRatio;
private Map<Investment, BigDecimal> underlyingInvestments;
public BigDecimal getExpenseRatio() {
return BigDecimalHelper.guard(expenseRatio);
}
public void setExpenseRatio(BigDecimal expenseRatio) {
this.expenseRatio = expenseRatio;
}
public Map<Investment, BigDecimal> getUnderlyingInvestments() {
return underlyingInvestments;
}
public void setUnderlyingInvestments(Map<Investment, BigDecimal>
underlyingFunds) {
this.underlyingInvestments = underlyingFunds;
}
}
当我尝试用 Jackson 序列化它时,其他一切都很好,除了我最终得到一个 Investment 对象引用,而不是像我期望的那样具有属性的字符串:
"underlyingInvestments":{"com.financial.detail.Investment@5d465e4b":1}}
我尝试设计一些自定义序列化程序,但没有成功,因为我总是获得 nested Investment(s) 的对象引用。所以,我有一个两部分的问题:
- 可以简单地使用 Jackson 注释解决此问题吗?
- 如果我必须构建一个自定义序列化程序,有人能指出我正确的方向吗? 鉴于此问题的嵌套性质(例如,一项投资可能包含一个共同基金,而该共同基金又具有一项与共同基金的投资......)
【问题讨论】:
标签: java json serialization jackson