mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-03-10 13:51:53 +09:00
Former-commit-id: 375604da8a20a6ba7cd0a8d05a44add02b2d04f4 Former-commit-id: 287287c5920b07618174d7a7573f049d350ded66
44 lines
1.0 KiB
Kotlin
44 lines
1.0 KiB
Kotlin
package net.torvald
|
|
|
|
import com.google.gson.Gson
|
|
import com.google.gson.JsonElement
|
|
import com.google.gson.JsonObject
|
|
|
|
import java.io.FileWriter
|
|
import java.io.IOException
|
|
|
|
/**
|
|
* Created by minjaesong on 16-03-04.
|
|
*/
|
|
object JsonWriter {
|
|
|
|
/**
|
|
* serialise a class to the file as JSON, using Google GSON.
|
|
*
|
|
* @param c: a class
|
|
* @param path: path to write a file
|
|
*/
|
|
@Throws(IOException::class)
|
|
fun writeToFile(c: Any, path: String) {
|
|
val classElem = Gson().toJsonTree(c)
|
|
val jsonString = classElem.toString()
|
|
val writer = FileWriter(path)
|
|
writer.write(jsonString)
|
|
writer.close()
|
|
}
|
|
|
|
/**
|
|
* serialise JsonObject to the file as JSON, using Google GSON.
|
|
*
|
|
* @param jsonObject
|
|
* @param path: path to write a file
|
|
*/
|
|
@Throws(IOException::class)
|
|
fun writeToFile(jsonObject: JsonObject, path: String) {
|
|
val writer = FileWriter(path)
|
|
writer.write(jsonObject.toString())
|
|
writer.close()
|
|
}
|
|
|
|
}
|