Last active
September 20, 2023 01:26
-
-
Save Arham4/429ee1216e0d7224cb1522ac5a2a0fe2 to your computer and use it in GitHub Desktop.
Easy Jackson YAML Parsing with Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
username: "Test" | |
password: "123456" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you! this is really useful!