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 hidden or 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
| data class UserDto(val username: String, val password: String) | |
| val user: UserDto = parseDto("user.yaml", UserDto::class) |
This file contains hidden or 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 # don't need "" because of configuration | |
| password: 123456 |
This file contains hidden or 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
| 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(Paths.get(fileName)).use { mapper.readValue(it, dto.java) } | |
| } |
Author
Hello, I'm on mobile, but I believe https://GitHub.com/Arham4/RuneBot uses
it if you go to the Kotlin branch and https://GitHub.com/Arham4/SilentBot
does too @Chydee
…On Fri, Jul 10, 2020, 03:59 Desmond Ngwuta Chidiebere < ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
Hi, I have been searching over the internet for a solution on how to Parse
YML with Kotlin. Then came across your gist. But I've got some question as
regards how to join or connect all these together to make it work. Can you
direct to probably a project that shows how it was all used
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<https://gist.github.com/429ee1216e0d7224cb1522ac5a2a0fe2#gistcomment-3370817>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAWAUSPFZCBWW4QYMO7XODTR23J5HANCNFSM4OWMQP4A>
.
Okay, that's great. Thank you!
thank you! this is really useful!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, I have been searching over the internet for a solution on how to Parse YML with Kotlin. Then came across your gist. But I've got some question as regards how to join or connect all these together to make it work. Can you direct to probably a project that shows how it was all used? Thank you 😊