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.

Revisions

  1. Arham4 revised this gist Feb 9, 2021. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions example.kt
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,9 @@
    import com.fasterxml.jackson.databind.ObjectMapper
    import com.fasterxml.jackson.module.kotlin.readValue
    import com.fasterxml.jackson.module.kotlin.registerKotlinModule
    import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
    import java.nio.file.Path

    val mapper = ObjectMapper()
    .registerKotlinModule()
    val mapper = jacksonObjectMapper()

    data class UserDto(val username: String, val password: String)

  2. Arham4 revised this gist Jan 31, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions extensions.kt
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    import com.fasterxml.jackson.databind.ObjectMapper
    import com.fasterxml.jackson.module.kotlin.jacksonTypeRef
    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(), jacksonTypeRef<T>())
    inline fun <reified T> ObjectMapper.readValue(src: Path): T = readValue(src.toFile())
  3. Arham4 revised this gist Jan 31, 2021. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions extensions.kt
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,7 @@
    import com.fasterxml.jackson.databind.ObjectMapper
    import com.fasterxml.jackson.module.kotlin.jacksonTypeRef
    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(), jacksonTypeRef<T>())
  4. Arham4 revised this gist Jan 31, 2021. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions example.kt
    Original file line number Diff line number Diff line change
    @@ -7,6 +7,7 @@ val mapper = ObjectMapper()
    .registerKotlinModule()

    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"))
  5. Arham4 revised this gist Jan 31, 2021. 2 changed files with 6 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion example.kt
    Original file line number Diff line number Diff line change
    @@ -7,4 +7,6 @@ val mapper = ObjectMapper()
    .registerKotlinModule()

    data class UserDto(val username: String, val password: String)
    val user: UserDto = mapper.readValue(Path.of("data.yml").toFile())
    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"))
    3 changes: 3 additions & 0 deletions extensions.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    // Extension function taking in Path as a parameter for ease of use

    inline fun <reified T> ObjectMapper.readValue(src: Path): T = readValue(src.toFile(), jacksonTypeRef<T>())
  6. Arham4 revised this gist Jan 31, 2021. 3 changed files with 3 additions and 10 deletions.
    2 changes: 1 addition & 1 deletion data.yaml
    Original file line number Diff line number Diff line change
    @@ -1,2 +1,2 @@
    username: "Test"
    password: 123456
    password: "123456"
    9 changes: 2 additions & 7 deletions parsing.kt → example.kt
    Original file line number Diff line number Diff line change
    @@ -6,10 +6,5 @@ import java.nio.file.Path
    val mapper = ObjectMapper()
    .registerKotlinModule()

    /**
    * Takes in a data class via generics and parses it by the fileName provided, returning the class
    * provided with parsed data.
    */
    inline fun <reified T> parseDto(fileName: String): T {
    return mapper.readValue(Path.of(fileName).toFile())
    }
    data class UserDto(val username: String, val password: String)
    val user: UserDto = mapper.readValue(Path.of("data.yml").toFile())
    2 changes: 0 additions & 2 deletions usage.kt
    Original file line number Diff line number Diff line change
    @@ -1,2 +0,0 @@
    data class UserDto(val username: String, val password: String)
    val user: UserDto = parseDto<UserDto>("data.yaml")
  7. Arham4 revised this gist Jan 31, 2021. 2 changed files with 1 addition and 1 deletion.
    File renamed without changes.
    2 changes: 1 addition & 1 deletion example_usage.kt → usage.kt
    Original file line number Diff line number Diff line change
    @@ -1,2 +1,2 @@
    data class UserDto(val username: String, val password: String)
    val user: UserDto = parseDto<UserDto>("user.yaml")
    val user: UserDto = parseDto<UserDto>("data.yaml")
  8. Arham4 renamed this gist Jan 31, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  9. Arham4 revised this gist Jan 31, 2021. 3 changed files with 13 additions and 9 deletions.
    18 changes: 11 additions & 7 deletions YAMLParse.kt
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,15 @@
    private val mapper = ObjectMapper(YAMLFactory())
    .registerModule(KotlinModule())
    .configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true)
    import com.fasterxml.jackson.databind.ObjectMapper
    import com.fasterxml.jackson.module.kotlin.readValue
    import com.fasterxml.jackson.module.kotlin.registerKotlinModule
    import java.nio.file.Path

    val mapper = ObjectMapper()
    .registerKotlinModule()

    /**
    * Takes in a data class (with ::class) and parses it by the fileName provided, returning the appropriate class
    * originally provided with parsed data.
    * Takes in a data class via generics and parses it by the fileName provided, returning the class
    * provided with parsed data.
    */
    fun <T: Any> parseDto(fileName: String, dto: KClass<T>): T {
    return Files.newBufferedReader(Paths.get(fileName)).use { mapper.readValue(it, dto.java) }
    inline fun <reified T> parseDto(fileName: String): T {
    return mapper.readValue(Path.of(fileName).toFile())
    }
    2 changes: 1 addition & 1 deletion example_usage.kt
    Original file line number Diff line number Diff line change
    @@ -1,2 +1,2 @@
    data class UserDto(val username: String, val password: String)
    val user: UserDto = parseDto("user.yaml", UserDto::class)
    val user: UserDto = parseDto<UserDto>("user.yaml")
    2 changes: 1 addition & 1 deletion user.yaml
    Original file line number Diff line number Diff line change
    @@ -1,2 +1,2 @@
    username: Test # don't need "" because of configuration
    username: "Test"
    password: 123456
  10. Arham4 revised this gist Jan 10, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion YAMLParse.kt
    Original file line number Diff line number Diff line change
    @@ -7,5 +7,5 @@ private val mapper = ObjectMapper(YAMLFactory())
    * 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) }
    return Files.newBufferedReader(Paths.get(fileName)).use { mapper.readValue(it, dto.java) }
    }
  11. Arham4 revised this gist Jan 10, 2021. 3 changed files with 11 additions and 16 deletions.
    23 changes: 9 additions & 14 deletions YAMLParse.kt
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,11 @@
    object YAMLParse {
    private val mapper = let {
    val mapper = ObjectMapper(YAMLFactory())
    mapper.registerModule(KotlinModule())
    mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true)
    mapper
    }
    private val mapper = ObjectMapper(YAMLFactory())
    .registerModule(KotlinModule())
    .configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true)

    /**
    * 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) }
    }
    /**
    * 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) }
    }
    2 changes: 2 additions & 0 deletions example_usage.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    data class UserDto(val username: String, val password: String)
    val user: UserDto = parseDto("user.yaml", UserDto::class)
    2 changes: 0 additions & 2 deletions usage.kt
    Original file line number Diff line number Diff line change
    @@ -1,2 +0,0 @@
    data class UserDto(val username: String, val password: String)
    val user: UserDto = YAMLParse.parseDto("user.yaml", UserDto::class)
  12. Arham4 revised this gist Apr 26, 2020. 2 changed files with 3 additions and 7 deletions.
    8 changes: 1 addition & 7 deletions YAMLParse.kt
    Original file line number Diff line number Diff line change
    @@ -13,10 +13,4 @@ object YAMLParse {
    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)
    }
    2 changes: 2 additions & 0 deletions usage.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    data class UserDto(val username: String, val password: String)
    val user: UserDto = YAMLParse.parseDto("user.yaml", UserDto::class)
  13. Arham4 revised this gist Feb 4, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions user.yaml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    username: Test # don't need "" because of configuration
    password: 123456
  14. Arham4 revised this gist Feb 4, 2018. No changes.
  15. Arham4 created this gist Feb 4, 2018.
    22 changes: 22 additions & 0 deletions YAMLParse.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    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)