object YAMLParse { private val mapper = let { val mapper = ObjectMapper(YAMLFactory()) mapper.registerModule(KotlinModule()) mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true) mapper } /** * Takes in a data class (with ::class) and parses it by the fileName provided, returning the appropriate class * originally provided with parsed data. */ fun parseDto(fileName: String, dto: KClass): T { return Files.newBufferedReader(FileSystems.getDefault().getPath(fileName)).use { mapper.readValue(it, dto.java) } } } /** * Usage: */ data class UserDto(val username: String, val password: String) val user: UserDto = YAMLParse.parseDto("user.yaml", UserDto::class)