【问题标题】:Jackson - how to write custom deserializerJackson - 如何编写自定义反序列化器
【发布时间】:2014-08-08 08:08:30
【问题描述】:

我想使用 Jackson 类将我的 HashMap 列表转换为对象列表(我已经阅读过可以这样做)。

我要序列化的对象如下所示...

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;

HashMap 的键值与我的 FinancialTransaction 类中的属性名称相同,但值都是字符串。

因为哈希映射的所有值都是字符串,所以在尝试从映射转换为对象时出现错误。这让我觉得我需要编写一个自定义反序列化器?有人可以帮助我了解我的自定义反序列化器类的外观。或者如果我不需要一个会更好。

谢谢

【问题讨论】:

  • 如果字段类型匹配,转换在技术上是可能的,但你为什么不直接在哈希图和你的类之间编写一个简单的转换(映射器)?为什么选择杰克逊?
  • 我只是觉得使用这些类会很好。
  • 好的,很公平。请注意,Jackson 将映射序列化为 JSON,然后将其反序列化为您的类型,这会产生一些性能开销。

标签: java jackson


【解决方案1】:

您不需要编写自定义序列化程序。只要映射条目类型对应于类字段的类型,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>>() {}));
    }
}

输出:

[FinancialTransaction{tranDate=Fri Aug 08 12:24:51 CEST 2014, tranDescription='descr', debit=123.45, credit=null}, FinancialTransaction{tranDate=Fri Aug 08 12:24:51 CEST 2014, tranDescription='descr2', debit=null, credit=678.9}]

【讨论】:

  • 感谢您告知我有关性能开销的部分。我现在可能不会继续使用这种方法。但是这些信息绝对有用,因为我使用 jackson 进行实际的 json 反序列化。
猜你喜欢
  • 2011-12-16
  • 1970-01-01
  • 2016-02-13
  • 2011-04-12
  • 2018-08-30
  • 1970-01-01
  • 2016-01-29
  • 1970-01-01
  • 2019-08-10
相关资源
最近更新 更多