arrow-kt / arrow-integrations

Λrrow Integrations is part of Λrrow, a functional companion to Kotlin's Standard Library
http://arrow-kt.io
Other
27 stars 6 forks source link

update readme on how to use jackson integration #55

Closed myuwono closed 3 years ago

myuwono commented 3 years ago

Currently there is no documentation about how to register arrow's jackson integration. e.g. users should be made aware that they need to register the arrow module using mapper.registerArrowModule().

Might be useful to include this code in the top readme to make it immediately obvious.

val mapper = ObjectMapper()
    .registerKotlinModule()
    .registerArrowModule()
    .setSerializationInclusion(JsonInclude.Include.NON_ABSENT) // enable this to omit serialization of Option.None

data class Foo(val value: Option<String>)
data class Bar(val value: Nel<String>)

mapper.writeValueAsString(Foo(none())) // {}
mapper.readValue("{}", Foo::class.java) // Foo(value=Option.None)

mapper.writeValueAsString(Foo("foo".some())) // {"value":"foo"}
mapper.readValue("""{"value":"foo"}""", Foo::class.java) // Foo(value=Option.Some(foo))

mapper.writeValueAsString(Bar("bar".nel())) // {"value":["bar"]}
mapper.readValue("""{"value":["bar"]}""", Bar::class.java) // Bar(value=NonEmptyList(bar))
mapper.readValue("""{"value":[]}""", Bar::class.java) // IllegalArgumentException: NonEmptyList cannot be empty