Search This Blog

Friday, May 31, 2019

How Object Mapper works internally!!


Usually when we work with Json or XML based data objects ,we use some convertors or mappers with it. Object Mapper is one of the data format converter which converts data or objects into different different format.

I was working with one of my project and there we had used lombok for Data objects , when I use it with ObjectMapper , I got to know that ,its not able to convert into type 'Map'. I have just removed lombok annotation and put getters and setters into that Data Object . After these steps that object easily got converted into Map type.

I am writing few points which happens when mapper gets executed. 


Without Annotations:

Without any annotations, it does what is called POJO mapping, it just uses reflection on the instance members and uses some rules about how to map the keys in the json to the names of the instance members. *note: it works on private members as well as public or package protected as well
If it doesn't match the names of the instance members, then it starts trying to match the getXXX and setXXX methods, if it doesn't match anything then it gives up.

With Annotations:

It uses the metadata supplied by the annotations to do the mapping and conversions.
It is always better to explicitly use the annotations when you have the source to add them to, then there is no guess work on what gets mapped to what.
Remember explicit is always better than implicit!

No comments :

Post a Comment