Skip to content

Instantly share code, notes, and snippets.

@wmira
Created December 22, 2018 03:22
Show Gist options
  • Save wmira/b3f0711d45727c743809f8d5362ee48c to your computer and use it in GitHub Desktop.
Save wmira/b3f0711d45727c743809f8d5362ee48c to your computer and use it in GitHub Desktop.
Kotlin SpringBoot Json
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
@Configuration
class Json {
@Bean
fun objectMapper(): ObjectMapper = defaultMapper()
companion object {
fun defaultMapper(): ObjectMapper = jacksonObjectMapper()
.findAndRegisterModules()
// toJson()
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.disable(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS)
// fromJson()
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.disable(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT)
.disable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)
.disable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
.enable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
.enable(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment