All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fasterxml.jackson.module.kotlin.Extensions.kt Maven / Gradle / Ivy

Go to download

Add-on module for Jackson (https://github.com/FasterXML/jackson/) to support Kotlin language, specifically introspection of method/constructor parameter names, without having to add explicit property name annotation.

There is a newer version: 2.9.8
Show newest version
package com.fasterxml.jackson.module.kotlin

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.core.TreeNode
import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.MappingIterator
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.ObjectReader
import java.io.File
import java.io.InputStream
import java.io.Reader
import java.net.URL

fun jacksonObjectMapper(): ObjectMapper = ObjectMapper().registerKotlinModule()
fun ObjectMapper.registerKotlinModule(): ObjectMapper = this.registerModule(KotlinModule())

inline fun  ObjectMapper.readValue(jp: JsonParser): T = readValue(jp, object: TypeReference() {})
inline fun  ObjectMapper.readValues(jp: JsonParser): MappingIterator = readValues(jp, object: TypeReference() {})

inline fun  ObjectMapper.readValue(src: File): T = readValue(src, object: TypeReference() {})
inline fun  ObjectMapper.readValue(src: URL): T = readValue(src, object: TypeReference() {})
inline fun  ObjectMapper.readValue(content: String): T = readValue(content, object: TypeReference() {})
inline fun  ObjectMapper.readValue(src: Reader): T = readValue(src, object: TypeReference() {})
inline fun  ObjectMapper.readValue(src: InputStream): T = readValue(src, object: TypeReference() {})
inline fun  ObjectMapper.readValue(src: ByteArray): T = readValue(src, object: TypeReference() {})

inline fun  ObjectMapper.treeToValue(n: TreeNode): T = treeToValue(n, T::class.java)
inline fun  ObjectMapper.convertValue(from: Any): T = convertValue(from, object: TypeReference() {})

inline fun  ObjectReader.readValue(jp: JsonParser): T = readValue(jp, object: TypeReference() {})
inline fun  ObjectReader.readValues(jp: JsonParser): Iterator = readValues(jp, object: TypeReference() {})
inline fun  ObjectReader.treeToValue(n: TreeNode): T = treeToValue(n, T::class.java)




© 2015 - 2025 Weber Informatics LLC | Privacy Policy