java - fasterxml serialize using toString and deserialize using String constructor -


i have pojo looks this:

public class thing {    private final int x;    private final int y;    private final int z;     public thing(string strthing)    {        // parse strthing in arbitrary format set x, y , z    }     @override    public string tostring()    {        // return string representation of thing        // (same format parsed constructor)    }     @override    public boolean equals(object obj) ...     @override    public int hashcode() ...  } 

and want use key map (e.g. hashmap<thing, someotherpojo>) which, when serializing json, uses tostring() representation of thing key, , when deserializing, uses string constructor. possible using simple jackson databind annotations? best way tackle this?

through experimentation (i think documentation have been little clearer on this) have discovered can use jsoncreator annotation on string constructor , jsonvalue on tostring() method achieve want:

public class thing {    private final int x;    private final int y;    private final int z;     @jsoncreator    public thing(string strthing)    {        // parse strthing in arbitrary format set x, y , z    }     @override    @jsonvalue    public string tostring()    {        // return string representation of thing        // (same format parsed constructor)    }     @override    public boolean equals(object obj) ...     @override    public int hashcode() ...  } 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -