1 回答
TA贡献1818条经验 获得超3个赞
如果一组属性(我的意思是输入数据的结构)是不可修改的,我们可以将它表示为一个 POJO 类。让它成为DetailsData:
public class DetailsData {
private String data;
private BigDecimal amount;
private List<MoneyValue> amountCurrencyList;
/*getters, setters, constructors*/
public DetailsData() {
}
@JsonProperty("amountCurrencyList")
private void deserializeMoneyValue(List<Map<String, Object>> data) {
if (Objects.isNull(amountCurrencyList)) {
amountCurrencyList = new ArrayList<>();
} else {
amountCurrencyList.clear();
}
MoneyValue moneyValue;
for (Map<String, Object> item : data) {
moneyValue = new MoneyValue(getValue(item.get("value")),
(String) item.get("currency"));
amountCurrencyList.add(moneyValue);
}
}
private BigDecimal getValue(Object value) {
BigDecimal result = null;
if (value != null) {
if (value instanceof BigDecimal) {
result = (BigDecimal) value;
} else if (value instanceof BigInteger) {
result = new BigDecimal((BigInteger) value);
} else if (value instanceof Number) {
result = new BigDecimal(((Number) value).doubleValue());
} else {
throw new ClassCastException("Invalid value");
}
}
return result;
}
}
如您所见,这是一个deserializeMoneyValue注释为 JSON 属性的方法。因此,对于 ObjectMapper,它将用作自定义反序列化器。getValue方法是必要的,因为 Jackson 以这种方式工作,因此值将在满足值大小的类中反序列化。例如,如果 value = "100" 它将被反序列化为Integer,如果 value = "100.0" -Double等等。其实这个方法你可以制作static并移动到一些util类中。总结一下,您可以buildDetailsFromJson通过以下方式进行更改:
public void buildDetailsFromJson(String details) {
if (details != null) {
ObjectMapper mapper = new ObjectMapper();
try {
mapper.enable(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
mapper.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);
// assuming the eventDetails is an instance of DetailsData
eventDetails = mapper.readValue(details, DetailsData.class);
} catch (IOException e) {
log.error("Exception during JSON {} parsing! {}", details, e.getMessage());
}
}
}
和 EventDetail 类:
public class SomeEventDetails extends BaseEventDetails implements EventDetails {
private List<MoneyValue> amountCurrencyList;
@Override
public void setUpFieldsFromMap() {
this.amountCurrencyList = super.eventDetails.getAmountCurrencyList();
if (isNull(amountCurrencyList)) {
throw new InvalidOrMissingParametersException("Exception during JSON parsing! Critical data is missing in DetailsMap - " + super.eventDetails.toString());
}
}
}
添加回答
举报