How to add a constant* during JSON deserialization using Jackson

Solution 1:

The problem stands in the fact that you are trying to register your context object in your mapper's config in the wrong way: you can use the DeserializationConfig withAttribute method that returns a new configuration including your context object and then set your mapper with the new configuration:

MyContext myContext = new MyContext();
myContext.setFoo("foo");
myContext.setBar(0);

ObjectMapper mapper = new ObjectMapper();
mapper.setConfig(mapper.getDeserializationConfig()
      .withAttribute("context", myContext));

After that the context object is available in the your JsonDeserializer class like you wrote:

MyContext myContext = (MyContext) deserializationContext.getConfig()
                                 .getAttributes()
                                 .getAttribute("context");