Skip to content

Instantly share code, notes, and snippets.

@Arham4
Last active September 20, 2023 01:26
Show Gist options
  • Save Arham4/429ee1216e0d7224cb1522ac5a2a0fe2 to your computer and use it in GitHub Desktop.
Save Arham4/429ee1216e0d7224cb1522ac5a2a0fe2 to your computer and use it in GitHub Desktop.
Easy Jackson YAML Parsing with Kotlin
username: "Test"
password: "123456"
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import java.nio.file.Path
val mapper = jacksonObjectMapper()
data class UserDto(val username: String, val password: String)
val user: UserDto = mapper.readValue(Path.of("data.yml").toFile())
// or, using the below extension function:
val user: UserDto = mapper.readValue(Path.of("data.yml"))
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import java.nio.file.Path
// Extension function taking in Path as a parameter for ease of use
inline fun <reified T> ObjectMapper.readValue(src: Path): T = readValue(src.toFile())
@chydee
Copy link

chydee commented Jul 10, 2020

Hi, I have been searching over the internet for a solution on how to Parse YML with Kotlin. Then came across your gist. But I've got some question as regards how to join or connect all these together to make it work. Can you direct to probably a project that shows how it was all used? Thank you 😊

@Arham4
Copy link
Author

Arham4 commented Jul 10, 2020 via email

@chydee
Copy link

chydee commented Jul 10, 2020

Okay, that's great. Thank you!

@longquanzheng
Copy link

thank you! this is really useful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment