Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 <T: Any> parseDto(fileName: String, dto: KClass<T>): 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)
Add Comment
Please, Sign In to add comment