Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/arrow-kt/arrow-integrations
Λrrow Integrations is part of Λrrow, a functional companion to Kotlin's Standard Library
https://github.com/arrow-kt/arrow-integrations
arrow functional-programming kotlin-library
Last synced: about 2 months ago
JSON representation
Λrrow Integrations is part of Λrrow, a functional companion to Kotlin's Standard Library
- Host: GitHub
- URL: https://github.com/arrow-kt/arrow-integrations
- Owner: arrow-kt
- License: other
- Created: 2020-02-14T17:57:18.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2024-10-24T16:15:59.000Z (2 months ago)
- Last Synced: 2024-10-25T22:36:35.597Z (2 months ago)
- Topics: arrow, functional-programming, kotlin-library
- Language: Kotlin
- Homepage: http://arrow-kt.io
- Size: 297 KB
- Stars: 27
- Watchers: 14
- Forks: 6
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Arrow Integrations
[![Maven Central](https://img.shields.io/maven-central/v/io.arrow-kt/arrow-integrations-jackson-module?color=4caf50&label=latest%20release)](https://maven-badges.herokuapp.com/maven-central/io.arrow-kt/arrow-integrations-jackson-module)
[![Arrow Core logo](https://raw.githubusercontent.com/arrow-kt/arrow-site/master/docs/img/core/arrow-core-brand-sidebar.svg?sanitize=true)](https://arrow-kt.io)
Λrrow Integrations is part of [**Λrrow**](https://arrow-kt.io).
## Jackson Module
Include `arrow-integrations-jackson` in your gradle project:
```groovy
implementation 'io.arrow-kt:arrow-integrations-jackson-module:${version}'
```
or, using gradle kotlin-dsl.
```kotlin
implementation("io.arrow-kt:arrow-integrations-jackson-module:${version}")
```Include `arrow-integrations-jackson` in your maven project:
```xmlio.arrow-kt
arrow-integrations-jackson-module
${version}```
To register support for arrow datatypes, simply call `.registerArrowModule()` on the object mapper as follows:
```kotlin
import arrow.integrations.jackson.module.registerArrowModule
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.registerKotlinModuleval mapper = ObjectMapper()
.registerKotlinModule()
.registerArrowModule()
```currently supported datatypes:
- `Option`
- `NonEmptyList` or `Nel`
- `Either`
- `Validated`
- `Ior`### Example Usage
Serialization and deserialization of data classes that incorporate arrow data types can be
done as follows.```kotlin
val mapper = ObjectMapper()
.registerKotlinModule()
.registerArrowModule()
.setSerializationInclusion(JsonInclude.Include.NON_ABSENT) // will not serialize None as nullsdata class Organization(val name: String, val address: Option, val websiteUrl: Option)
data class ArrowUser(val name: String, val emails: NonEmptyList, val organization: Option)val arrowUser = ArrowUser(
"John Doe",
nonEmptyListOf(
"[email protected]",
"[email protected]"
),
Organization("arrow-kt", none(), URI("https://arrow-kt.io").some()).some()
)mapper.writerWithDefaultPrettyPrinter().writeValueAsString(user)
```
which serializes as follows.
```json
{
"name" : "John Doe",
"emails" : [ "[email protected]", "[email protected]" ],
"organization" : {
"name" : "arrow-kt",
"websiteUrl" : "https://arrow-kt.io"
}
}
```
Notice that the `Option` serializer
is configurable via Jackson's serialization inclusion setting. In this example we have configured the serializer
to not serialize `none()` as null, but instead omit it completely.Various serializers / deserializers within arrow module are configurable.
For instance the field names used for serializing / deserializing `Either`, `Validated` or `Ior` can
be configured within the registration step:```kotlin
val mapper: ObjectMapper = ObjectMapper()
.registerKotlinModule()
.registerArrowModule(
eitherModuleConfig = EitherModuleConfig("left", "right"), // sets the field names for either left / right
validatedModuleConfig = ValidatedModuleConfig("invalid", "valid"), // sets the field names for validated invalid / valid
iorModuleConfig = IorModuleConfig("left", "right") // sets the field names for ior left / right
)
.setSerializationInclusion(JsonInclude.Include.NON_ABSENT) // do not serialize None as nulls```
More example usages can be found in [ExampleTest.kt](arrow-integrations-jackson-module/src/test/kotlin/arrow/integrations/jackson/module/ExampleTest.kt)
### Example Usage for Popular Web Frameworks
In real world scenarios Jackson can be installed as the json serialization/deserialization
engine. These serializations / deserializations are normally done
automatically.For instance we can customize our mapper with `.registerArrowModule()` as follows.
```kotlin
object JsonMapper {
val mapper: ObjectMapper = ObjectMapper()
.registerModule(KotlinModule(singletonSupport = SingletonSupport.CANONICALIZE))
.registerArrowModule()
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.disable(JsonParser.Feature.INCLUDE_SOURCE_IN_LOCATION)
.setSerializationInclusion(JsonInclude.Include.NON_ABSENT)
}
```
This can then be installed accordingly.#### Spring Boot
A way to register of arrow data types JSON serialization / deserialization support in spring boot is as follows:
```kotlin
@Configuration
class JacksonConfiguration {@Bean
@Primary
fun jsonMapper(): ObjectMapper = JsonMapper.mapper
}
```
When this bean is registered, the object mapper will be used to deserialize incoming and outgoing JSON payload.#### Ktor
Jackson support for arrow data type serialization / deserialization can similarly be registered in Ktor as follows:
```kotlin
install(ContentNegotiation) {
register(ContentType.Application.Json, JacksonConverter(JsonMapper.mapper))
}
```