1

Is it better to use one static instance of an ObjectMapper or create an instance of ObjectMapper for every serialization or deserialization?

Anand
  • 1,791
  • 5
  • 23
  • 41
  • 3
    Please, see this question: http://stackoverflow.com/questions/3907929/should-i-make-jacksons-objectmapper-as-static-final – Michał Ziober Jul 30 '13 at 07:04
  • Thanks, Michal Ziober. The reason I asked for is that my application has to parse json strings with different date formats. If my ObjectMapper is a static instance, I then need to change the configuration each time and reset it back to default every time I want to parse different date formats. Any pointers there? – Anand Jul 30 '13 at 07:44
  • If I understand you correctly, every time when you want to parse JSON, you are doing something like that: 1. objectMapper.setDateFormat(NEW_DATE_FORMAT), 2. Deserialize JSON, 3.objectMapper.setDateFormat(DEFAULT_FORMAT), don't you? How many different date formats do you have? Could you show a simple source code how are you doing it? – Michał Ziober Jul 30 '13 at 09:34
  • Michael - It is exactly as in the steps you had mentioned above. Since, the mapper is a static instance, the configuration is visible application wide. So, any time I set a new date format and parse a JSON, I will have to reset it to the default. As of now, I might have a couple of formats. – Anand Jul 31 '13 at 08:50
  • 1
    If you use Jackson 2.x, you should be using single `ObjectMapper`, but multiple `ObjectReader` and/or `ObjectWriter` instances: latter are safe to configure on per-call basis. Same is NOT true for `ObjectMapper`. – StaxMan Aug 05 '13 at 23:40

0 Answers0