Java Jackson-如何编写自定义反序列化程序
我想使用Jackson类将我的HashMaps列表转换为一个对象列表(我已经读过了,可以这样做) 我要序列化的对象如下所示Java Jackson-如何编写自定义反序列化程序,java,jackson,Java,Jackson,我想使用Jackson类将我的HashMaps列表转换为一个对象列表(我已经读过了,可以这样做) 我要序列化的对象如下所示 public class FinancialTransaction { private Date tranDate; private String tranDescription; private Double debit; private Double credit; ...getters and setters... A
public class FinancialTransaction {
private Date tranDate;
private String tranDescription;
private Double debit;
private Double credit;
...getters and setters...
ArrayList<FinancialTransaction> transactions =
new ArrayList<FinancialTransaction>();
HashMap<String, String> records = new HashMap<String, String>();
records.put("tranDate", "08/08/2014");
records.put("tranDescription", "08/08/2014");
records.put("debit", "1.50");
records.put("credit", null);true);
for (HashMap<String, String> record : records) {
ObjectMapper m = new ObjectMapper();
FinancialTransaction ft = m.convertValue(record, FinancialTransaction.class);
transactions.add(ft);
}
return transactions;
我想做一些像这样的事情
public class FinancialTransaction {
private Date tranDate;
private String tranDescription;
private Double debit;
private Double credit;
...getters and setters...
ArrayList<FinancialTransaction> transactions =
new ArrayList<FinancialTransaction>();
HashMap<String, String> records = new HashMap<String, String>();
records.put("tranDate", "08/08/2014");
records.put("tranDescription", "08/08/2014");
records.put("debit", "1.50");
records.put("credit", null);true);
for (HashMap<String, String> record : records) {
ObjectMapper m = new ObjectMapper();
FinancialTransaction ft = m.convertValue(record, FinancialTransaction.class);
transactions.add(ft);
}
return transactions;
ArrayList事务=
新的ArrayList();
HashMap记录=新的HashMap();
记录。出售(“交易日期”,“2014年8月8日”);
记录。提交(“交易说明”、“2014年8月8日”);
记录。卖出(“借方”,“1.50”);
记录。put(“贷方”,空);正确的);
for(HashMap记录:记录){
ObjectMapper m=新的ObjectMapper();
FinancialTransaction ft=m.convertValue(记录,FinancialTransaction.class);
交易。添加(ft);
}
退货交易;
其中HashMap的键值等于我的FinancialTransaction类中属性的名称,但这些值都是字符串
因为hashmap的所有值都是字符串,所以在尝试从映射转换到对象时出错。这让我觉得我需要编写一个自定义反序列化程序?有人能帮我看看自定义反序列化器类的外观吗。或者如果我不需要,那就更好了
谢谢您不需要编写自定义序列化程序。只要映射条目类型对应于类字段的类型,Jackson就可以将映射转换为您的类型 以下是一个例子:
public class JacksonConversion {
public static class FinancialTransaction {
public Date tranDate;
public String tranDescription;
public Double debit;
public Double credit;
@Override
public String toString() {
return "FinancialTransaction{" +
"tranDate=" + tranDate +
", tranDescription='" + tranDescription + '\'' +
", debit=" + debit +
", credit=" + credit +
'}';
}
}
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> map1 = new HashMap<>();
map1.put("tranDate", new Date().getTime());
map1.put("tranDescription", "descr");
map1.put("debit", 123.45);
Map<String, Object> map2 = new HashMap<>();
map2.put("tranDate", new Date().getTime());
map2.put("tranDescription", "descr2");
map2.put("credit", 678.9);
System.out.println(mapper.convertValue(
Arrays.asList(map1, map2),
new TypeReference<List<FinancialTransaction>>() {}));
}
}
如果字段类型匹配,转换在技术上是可能的,但是为什么不在hashmap和类之间编写一个简单的转换(映射器)?为什么是杰克逊?我只是觉得使用这些课程会很好。好吧,很公平。请注意,Jackson将映射序列化为JSON,然后将其反序列化为您的类型,这会带来一些性能开销。感谢您告知我有关性能开销的部分。我现在可能不会采用这种方法。但这些信息绝对有用,因为我正在使用jackson进行实际的json反序列化。