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

c# - How can I have the VS Debugger break in the correct location for exceptions thrown in Async methods in a Console App? -

php - Issues outputting data after prepared statement with loops -

javascript - How to pass a property by reference -