2

How can I globally configure json serializer for http4k? For example, snake case field names or formatting DateTime as ISO8601.

alisabzevari
  • 8,008
  • 6
  • 43
  • 67
  • The solution is likely similar to the answer on this issue reported to http4k: https://github.com/http4k/http4k/issues/183 where you have to create your own subtype of `ConfigurableJackson` ... not the easiest way to have extensibility, but appears to be the option. – Jayson Minard Nov 18 '18 at 16:01
  • Yeah, I did the same. Creating another object subclassing from `ConfigurableJackson`. Would you mind creating an answer to my question? – alisabzevari Nov 18 '18 at 18:33

2 Answers2

1

Since the ObjectMapper instance is private within ConfigurableJackson you cannot get at it after construction to do any configuration.

So you either need to construct your own direct instance of ConfigurableJackson and pass in a customized ObjectMapper or you need to subclass ConfigurableJackson with your own class. And then during the constructor, create an ObjectMapper (see example below) or intercept one being passed into your constructor and change its settings.

Whatever you do, be sure you do not break the http4k framework or anything else that might be using the same instance. You can see the defaults used by http4k declared in their source code:

object Jackson : ConfigurableJackson(ObjectMapper()
    .registerModule(defaultKotlinModuleWithHttp4kSerialisers)
    .disableDefaultTyping()
    .configure(FAIL_ON_UNKNOWN_PROPERTIES, false)
    .configure(FAIL_ON_IGNORED_PROPERTIES, false)
    .configure(USE_BIG_DECIMAL_FOR_FLOATS, true)
    .configure(USE_BIG_INTEGER_FOR_INTS, true)
)

You can use code similar to above to create your own instance.

See this thread for some conversation about this topic: https://github.com/http4k/http4k/issues/183

Jayson Minard
  • 84,842
  • 38
  • 184
  • 227
  • 1
    In version 3.108.0 they have [changed](https://github.com/http4k/http4k/commit/829db5d7035150a0028c3bc929439439a0cc42f2#diff-6259924eca39a61827c8587aed046d95) the way to configure Jackson. You may want to change the answer to match the new changes. – alisabzevari Feb 23 '19 at 14:11
-1

You don't necessarily need to extend ConfigurableJackson - it's just that extending it is the most convenient way to do this (in our experience).

All configuration is done by tweaking the ObjectMapper instance which is injected into the ConfigurableJackson constructor - the ConfigurableJackson itself just provides the wrapper API around that mapper. The question is to do with standard configuration of Jackson, so you should seek answers to your specific questions (snake case etc) from the Jackson docs directly as http4k doesn't own that API.

David D
  • 239
  • 2
  • 4