Guest User

Untitled

a guest
Feb 17th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. object YAMLParse {
  2. private val mapper = let {
  3. val mapper = ObjectMapper(YAMLFactory())
  4. mapper.registerModule(KotlinModule())
  5. mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true)
  6. mapper
  7. }
  8.  
  9. /**
  10. * Takes in a data class (with ::class) and parses it by the fileName provided, returning the appropriate class
  11. * originally provided with parsed data.
  12. */
  13. fun <T: Any> parseDto(fileName: String, dto: KClass<T>): T {
  14. return Files.newBufferedReader(FileSystems.getDefault().getPath(fileName)).use { mapper.readValue(it, dto.java) }
  15. }
  16. }
  17.  
  18. /**
  19. * Usage:
  20. */
  21. data class UserDto(val username: String, val password: String)
  22. val user: UserDto = YAMLParse.parseDto("user.yaml", UserDto::class)
Add Comment
Please, Sign In to add comment