diff options
author | Linnea Gräf <nea@nea.moe> | 2024-03-14 13:12:46 +0100 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-03-14 13:37:30 +0100 |
commit | f5862b8edb1fe025eaf1c529764f1d9aab838cbe (patch) | |
tree | e780afdd82ace17f6c612f5ead1f2796f8ac03c4 | |
parent | cfa252f218a640c78935b3cb064f68e1a38ffced (diff) | |
download | SkyHanniChangelogBuilder-f5862b8edb1fe025eaf1c529764f1d9aab838cbe.tar.gz SkyHanniChangelogBuilder-f5862b8edb1fe025eaf1c529764f1d9aab838cbe.tar.bz2 SkyHanniChangelogBuilder-f5862b8edb1fe025eaf1c529764f1d9aab838cbe.zip |
Add json util
-rw-r--r-- | src/main/kotlin/at/hannibal2/skyhanni/changelog/GsonUtils.kt | 14 | ||||
-rw-r--r-- | src/main/kotlin/at/hannibal2/skyhanni/changelog/Main.kt | 21 |
2 files changed, 19 insertions, 16 deletions
diff --git a/src/main/kotlin/at/hannibal2/skyhanni/changelog/GsonUtils.kt b/src/main/kotlin/at/hannibal2/skyhanni/changelog/GsonUtils.kt new file mode 100644 index 0000000..0520c21 --- /dev/null +++ b/src/main/kotlin/at/hannibal2/skyhanni/changelog/GsonUtils.kt @@ -0,0 +1,14 @@ +package at.hannibal2.skyhanni.changelog + +import com.google.gson.Gson +import com.google.gson.reflect.TypeToken + +object GsonUtils { + + val gson = Gson() + + inline fun <reified T> readObject(json: String): T { + return gson.fromJson(json, object : TypeToken<T>() {}) + } + +}
\ No newline at end of file diff --git a/src/main/kotlin/at/hannibal2/skyhanni/changelog/Main.kt b/src/main/kotlin/at/hannibal2/skyhanni/changelog/Main.kt index 8f62f34..40f7b72 100644 --- a/src/main/kotlin/at/hannibal2/skyhanni/changelog/Main.kt +++ b/src/main/kotlin/at/hannibal2/skyhanni/changelog/Main.kt @@ -1,5 +1,6 @@ package org.example +import at.hannibal2.skyhanni.changelog.GsonUtils import com.google.gson.GsonBuilder import com.google.gson.JsonArray @@ -25,19 +26,9 @@ val changePattern = "\\+ (?<text>.*) - (?<author>.*)".toPattern() val extraInfoPattern = " {4}\\* (?<text>.*)".toPattern() val illegalStartPattern = "^[-=*+ ].*".toPattern() -fun getTextFromUrl(urlString: String): List<String> { +fun getTextFromUrl(urlString: String): String { val url = URL(urlString) - val connection = url.openConnection() - val inputStream = connection.getInputStream() - val text = mutableListOf<String>() - - inputStream.bufferedReader().useLines { lines -> - lines.forEach { - text.add(it) - } - } - - return text + return url.openStream().use { it.bufferedReader().readText() } } enum class WhatToDo { @@ -59,10 +50,8 @@ fun main() { WhatToDo.OPEN_PRS -> "https://api.github.com/repos/hannibal002/SkyHanni/pulls?state=open&sort=updated&direction=desc&per_page=30" } - val data = getTextFromUrl(url).joinToString("") - val gson = GsonBuilder().create() - val fromJson = gson.fromJson(data, JsonArray::class.java) - val prs = fromJson.map { gson.fromJson(it, PullRequest::class.java) } + val data = getTextFromUrl(url) + val prs = GsonUtils.readObject<List<PullRequest>>(data) readPrs(prs, firstPr, hideWhenError, whatToDo, fullVersion, beta) } |