completely abolishing GSON; new save format impl wip

This commit is contained in:
minjaesong
2021-08-23 16:55:51 +09:00
parent df1ebdf93d
commit e15d5c9b05
58 changed files with 421 additions and 2101 deletions

View File

@@ -1,5 +1,7 @@
package net.torvald.terrarum.utils
import com.badlogic.gdx.utils.JsonReader
import com.badlogic.gdx.utils.JsonValue
import net.torvald.terrarum.AppLoader.printdbg
/**
@@ -10,7 +12,7 @@ object JsonFetcher {
private var jsonString: StringBuffer? = null
@Throws(java.nio.file.NoSuchFileException::class)
operator fun invoke(jsonFilePath: String): com.google.gson.JsonObject {
operator fun invoke(jsonFilePath: String): JsonValue {
jsonString = StringBuffer() // reset buffer every time it called
readJsonFileAsString(jsonFilePath)
@@ -20,14 +22,11 @@ object JsonFetcher {
throw Error("[JsonFetcher] jsonString is null!")
}
val jsonParser = com.google.gson.JsonParser()
val jsonObj = jsonParser.parse(jsonString.toString()).asJsonObject
return jsonObj
return JsonReader().parse(jsonString.toString())
}
@Throws(java.nio.file.NoSuchFileException::class)
operator fun invoke(jsonFile: java.io.File): com.google.gson.JsonObject {
operator fun invoke(jsonFile: java.io.File): JsonValue {
jsonString = StringBuffer() // reset buffer every time it called
readJsonFileAsString(jsonFile.canonicalPath)
@@ -37,10 +36,7 @@ object JsonFetcher {
throw Error("[JsonFetcher] jsonString is null!")
}
val jsonParser = com.google.gson.JsonParser()
val jsonObj = jsonParser.parse(jsonString.toString()).asJsonObject
return jsonObj
return JsonReader().parse(jsonString.toString())
}
@Throws(java.nio.file.NoSuchFileException::class)
@@ -50,4 +46,14 @@ object JsonFetcher {
) // JSON does not require line break
}
fun forEach(map: JsonValue, action: (String, JsonValue) -> Unit) {
var counter = 0
var entry = map.child
while (entry != null) {
action(entry.name ?: "(arrayindex $counter)", entry)
entry = entry.next
counter += 1
}
}
}

View File

@@ -1,7 +1,7 @@
package net.torvald.terrarum.utils
import com.google.gson.GsonBuilder
import net.torvald.terrarum.AppLoader
import com.badlogic.gdx.utils.Json
import com.badlogic.gdx.utils.JsonWriter
/**
* Created by minjaesong on 2016-03-04.
@@ -10,25 +10,6 @@ object JsonWriter {
private val formattingRegex = Regex("""(?<=[\{,\[])|(?=[\]}])""")
fun getJsonBuilder() = if (AppLoader.IS_DEVELOPMENT_BUILD) {
getPrettyBuilder()
}
else {
GsonBuilder()
.serializeNulls()
.disableHtmlEscaping()
.enableComplexMapKeySerialization()
.create()
}
fun getPrettyBuilder() = GsonBuilder()
.setPrettyPrinting()
.serializeNulls()
.disableHtmlEscaping()
.enableComplexMapKeySerialization()
.create()
/**
* serialise a class to the file as JSON, using Google GSON.
*
@@ -37,10 +18,8 @@ object JsonWriter {
*/
@Throws(java.io.IOException::class)
fun writeToFile(c: Any, path: String) {
val jsonString = getJsonBuilder().toJson(c)
val writer = java.io.FileWriter(path, false)
writer.write(jsonString.replace(formattingRegex, "\n"))
writer.write(Json(JsonWriter.OutputType.json).toJson(c).replace(formattingRegex, "\n"))
writer.close()
}
@@ -50,13 +29,13 @@ object JsonWriter {
* @param jsonObject
* @param path: path to write a file
*/
@Throws(java.io.IOException::class)
fun writeToFile(jsonObject: com.google.gson.JsonObject, path: String) {
/*@Throws(java.io.IOException::class)
fun writeToFile(jsonObject: Json, path: String) {
val writer = java.io.FileWriter(path, false)
writer.write(getPrettyBuilder().toJson(jsonObject))
writer.write(jsonObject.)
//writer.write(jsonObject.toString().replace(formattingRegex, "\n"))
writer.close()
}
}*/
}