Files
Terrarum/src/net/torvald/JsonFetcher.kt
Song Minjae fb11d5c9c2 FIX: quickbar opacity bug, Notification won't display, remaned majuscule to fullwidth, keyboard layouts for control helper
Former-commit-id: 99c51499131a7cbae1b7345c15d804bd5340e7b6
Former-commit-id: 1326b69cb920d3590fe2cbe33013c85c9eeb1191
2016-07-29 21:02:24 +09:00

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
}
}