简体   繁体   中英

Jackson - how to write custom deserializer

I would like to use the Jackson classes to convert my list of HashMaps to a list of objects (which i have read can be done).

My object that I want to serialize looks like this...

public class FinancialTransaction {
    private Date tranDate;
    private String tranDescription;
    private Double debit;
    private Double credit;
    ...getters and setters...

And I would like to do something like this...

    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;

Where the HashMap's key values are equal to the names of the properties in my FinancialTransaction class but the values are all strings.

Because all the values of the hashmap are strings I get an error when trying to convert from a map to my object. Which makes me think I need to write a custom deserializer? Can someone please help me with how my custom deserializer class should look. Or if I don't need one that would be better.

thanks

You do not need to write a custom serializer. Jackson can convert from a map to your type as long as the map entry types correspond to the types of the class fields.

Here is an example:

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>>() {}));
    }
}

Output:

[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}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM