mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-03-15 08:06:06 +09:00
Former-commit-id: 99c51499131a7cbae1b7345c15d804bd5340e7b6 Former-commit-id: 1326b69cb920d3590fe2cbe33013c85c9eeb1191
53 lines
1.4 KiB
Kotlin
53 lines
1.4 KiB
Kotlin
package net.torvald
|
|
|
|
import com.google.gson.JsonObject
|
|
import com.google.gson.JsonParser
|
|
import java.io.File
|
|
|
|
import java.io.IOException
|
|
import java.nio.file.FileSystems
|
|
import java.nio.file.Files
|
|
import java.util.ArrayList
|
|
import java.util.function.Consumer
|
|
|
|
/**
|
|
* Created by minjaesong on 16-02-15.
|
|
*/
|
|
object JsonFetcher {
|
|
|
|
private var jsonString: StringBuffer? = null
|
|
|
|
@Throws(IOException::class)
|
|
operator fun invoke(jsonFilePath: String): JsonObject {
|
|
jsonString = StringBuffer() // reset buffer every time it called
|
|
readJsonFileAsString(jsonFilePath)
|
|
|
|
println("Reading JSON $jsonFilePath")
|
|
|
|
val jsonParser = JsonParser()
|
|
val jsonObj = jsonParser.parse(jsonString!!.toString()).asJsonObject
|
|
|
|
return jsonObj
|
|
}
|
|
|
|
@Throws(IOException::class)
|
|
operator fun invoke(jsonFile: File): JsonObject {
|
|
jsonString = StringBuffer() // reset buffer every time it called
|
|
readJsonFileAsString(jsonFile.canonicalPath)
|
|
|
|
println("Reading JSON ${jsonFile.path}")
|
|
|
|
val jsonParser = JsonParser()
|
|
val jsonObj = jsonParser.parse(jsonString!!.toString()).asJsonObject
|
|
|
|
return jsonObj
|
|
}
|
|
|
|
@Throws(IOException::class)
|
|
private fun readJsonFileAsString(path: String) {
|
|
Files.lines(FileSystems.getDefault().getPath(path)).forEach(
|
|
{ jsonString!!.append(it) }
|
|
) // JSON does not require line break
|
|
}
|
|
}
|