On Sun, Apr 15, 2018 at 9:53 AM, Marcel Overdijk <mar...@overdijk.me> wrote: > Hi, > > Is it possible to provide (request-based) context to a custom > `JsonSerializer`? Like a `fieldsets` request param to remove some fields > from the generated json? > > Or is the best option to bind the needed context to a `ThreadLocal`? > > > Basically when calling objectMapper.writeValueAsString(document) in a custom > serializer I want to change the json data based on a particular context > which is not part of the provided document.
Yes, there are "context attributes" that were added to reduce/remove need for passing things using ThreadLocals. You can specify baseline attributes via ObjectMapper, but usually they are set on ObjectReader and ObjectWriter which are light-weight reconfigurable entities constructed from ObjectMapper (configuration of which may not be changed on per-call basis). So, something like: ObjectWriter w = mapper.writer() // or, `writerFor(type)` to cache lookup for serializer .withAttribute("key", value); w.writeValue(new File("foo.json"), thingToSerialize); Serialize can then call `provider.getAttribute("key")` to locate active value. I hope this helps, -+ Tatu +- -- You received this message because you are subscribed to the Google Groups "jackson-user" group. To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user+unsubscr...@googlegroups.com. To post to this group, send email to jackson-user@googlegroups.com. For more options, visit https://groups.google.com/d/optout.