Search code examples
javapojo

How to convert POJO to map with types retained?


I need to convert a POJO to a map. I tried using Object mapper but types like timestamp are coming either as String or as Long in the final map. Is there some tool that would do a simple conversion that would result in a map having exactly the same objects as in the POJO ? (I know I can use reflection but want to see if there is an easier way.)


Solution

  • This can be done directly with BeanUtils describe or BeanMap:

    Map<Object, Object> map = BeanUtils.describe(object); 
    
    Map<Object, Object> map = new BeanMap(object);