Skip to content

Commit

Permalink
Able to read JSON from path
Browse files Browse the repository at this point in the history
  • Loading branch information
Unthrottled committed Sep 5, 2022
1 parent 13251fb commit b2a0be7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group = "io.unthrottled.doki.build.jvm"
version = "88.0.2"
version = "88.0.3"

java {
withSourcesJar()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package io.unthrottled.doki.build.jvm.tools

import com.google.gson.GsonBuilder
import io.unthrottled.doki.build.jvm.models.AssetTemplateDefinition
import java.io.InputStreamReader
import java.nio.charset.StandardCharsets
import io.unthrottled.doki.build.jvm.tools.PathTools.readJSONFromFile
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
Expand Down Expand Up @@ -36,8 +34,6 @@ class ConstructableAsset(

object ConstructableAssetSupplierFactory {

private val gson = GsonBuilder().setPrettyPrinting().create()

fun createCommonAssetsTemplate(
buildSourceAssetDirectory: Path,
masterThemesDirectory: Path
Expand All @@ -49,13 +45,7 @@ object ConstructableAssetSupplierFactory {
)
.filter { !Files.isDirectory(it) }
.filter { it.fileName.toString().endsWith(".template.json") }
.map { Files.newInputStream(it) }
.map {
gson.fromJson(
InputStreamReader(it, StandardCharsets.UTF_8),
AssetTemplateDefinition::class.java
)
}
.map { readJSONFromFile(it, AssetTemplateDefinition::class.java) }
.collect(
Collectors.groupingBy {
it.type ?: throw IllegalArgumentException("Expected template ${it.name} to have a type")
Expand Down
25 changes: 25 additions & 0 deletions src/main/kotlin/io/unthrottled/doki/build/jvm/tools/PathTools.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.unthrottled.doki.build.jvm.tools

import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
import java.io.InputStreamReader
import java.nio.file.Files
import java.nio.file.Files.createDirectories
import java.nio.file.Files.exists
Expand All @@ -24,4 +27,26 @@ object PathTools {
.forEach(Files::delete)
}
}

private val gson = GsonBuilder()
.create()

fun <T> readJSONFromFile(mappingFile: Path, typeToken: TypeToken<T>): T =
gson.fromJson(
InputStreamReader(
Files.newInputStream(
mappingFile
)
),
typeToken.type
)
fun <T> readJSONFromFile(mappingFile: Path, clazz: Class<T>): T =
gson.fromJson(
InputStreamReader(
Files.newInputStream(
mappingFile
)
),
clazz
)
}

0 comments on commit b2a0be7

Please sign in to comment.