mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-11 11:04:05 +09:00
more tests with gson (de)serialiser
This commit is contained in:
@@ -7,7 +7,9 @@ import com.google.gson.JsonObject
|
|||||||
*/
|
*/
|
||||||
interface GsonSerialisable {
|
interface GsonSerialisable {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Will modify itself according to the input gson. Not sure it's even necessary so please test.
|
||||||
|
*/
|
||||||
fun read(gson: JsonObject)
|
fun read(gson: JsonObject)
|
||||||
fun write(targetGson: JsonObject)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
package net.torvald.terrarum
|
package net.torvald.terrarum
|
||||||
|
|
||||||
import com.google.gson.Gson
|
|
||||||
import com.google.gson.JsonObject
|
import com.google.gson.JsonObject
|
||||||
import com.google.gson.JsonPrimitive
|
import com.google.gson.JsonPrimitive
|
||||||
|
|
||||||
@@ -115,12 +114,4 @@ open class KVHashMap : GsonSerialisable {
|
|||||||
TODO()
|
TODO()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun write(targetGson: JsonObject) {
|
|
||||||
hashMap.forEach { t, u ->
|
|
||||||
if (u is JsonPrimitive)
|
|
||||||
targetGson.add(t, u)
|
|
||||||
else
|
|
||||||
targetGson.add(t, Gson().toJsonTree(u))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -138,11 +138,15 @@ object SavegameWriter {
|
|||||||
.setPrettyPrinting()
|
.setPrettyPrinting()
|
||||||
|
|
||||||
.serializeNulls()
|
.serializeNulls()
|
||||||
|
.disableHtmlEscaping()
|
||||||
|
.enableComplexMapKeySerialization()
|
||||||
.create()
|
.create()
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
GsonBuilder()
|
GsonBuilder()
|
||||||
.serializeNulls()
|
.serializeNulls()
|
||||||
|
.disableHtmlEscaping()
|
||||||
|
.enableComplexMapKeySerialization()
|
||||||
.create()
|
.create()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import com.badlogic.gdx.graphics.Color
|
|
||||||
import com.google.gson.GsonBuilder
|
import net.torvald.terrarum.serialise.SavegameWriter
|
||||||
import net.torvald.terrarum.gameactors.Actor
|
|
||||||
import org.dyn4j.geometry.Vector2
|
import org.dyn4j.geometry.Vector2
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -10,54 +9,42 @@ import org.dyn4j.geometry.Vector2
|
|||||||
*/
|
*/
|
||||||
object GsonTest {
|
object GsonTest {
|
||||||
|
|
||||||
operator fun invoke() {
|
private val testClass = GsonTestClass()
|
||||||
val jsonString = GsonBuilder()
|
|
||||||
.setPrettyPrinting()
|
|
||||||
.serializeNulls()
|
|
||||||
.create()
|
|
||||||
.toJson(GsonTestActor())
|
|
||||||
|
|
||||||
|
|
||||||
println(jsonString)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
class GsonTestActor : Actor(Actor.RenderOrder.MIDDLE) {
|
|
||||||
override fun update(delta: Float) {
|
|
||||||
TODO("not implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onActorValueChange(key: String, value: Any?) {
|
|
||||||
TODO("not implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun dispose() {
|
|
||||||
TODO("not implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun run() {
|
|
||||||
TODO("not implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
init {
|
init {
|
||||||
referenceID = 42
|
testClass.foo = 42
|
||||||
|
testClass.bar = 1f/42f
|
||||||
|
testClass.baz = "According to all known laws of aviation, there is no way a bee should be able to fly."
|
||||||
|
testClass.fov = Vector2(1.23432, -0.4)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
operator fun invoke() {
|
||||||
|
|
||||||
|
val gson = SavegameWriter.getJsonBuilder()
|
||||||
|
val jsonString = gson.toJson(testClass)
|
||||||
|
|
||||||
|
println(jsonString)
|
||||||
|
|
||||||
|
|
||||||
|
val deserialised = gson.fromJson(jsonString, GsonTestSuper::class.java)
|
||||||
|
println(deserialised)
|
||||||
|
println(deserialised as GsonTestClass) // ClassCastException
|
||||||
}
|
}
|
||||||
|
|
||||||
private val mysecretnote = "note"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class GsonTestClass : GsonTestInterface {
|
open class GsonTestSuper(var foo: Int) {
|
||||||
var foo = 1.567f
|
override fun toString() = "GsonTestSuper"
|
||||||
val bar = "stingy"
|
}
|
||||||
val baz = Color(0f, 0.2f, 0.4f, 1f)
|
|
||||||
val fov = Vector2(1.324324, -0.4321)
|
|
||||||
|
|
||||||
val bazget: Color
|
class GsonTestClass(
|
||||||
get() = Color.CHARTREUSE
|
foo: Int = 0,
|
||||||
|
var bar: Float = 0f,
|
||||||
@Transient override var superfoo = 42
|
var baz: String = "",
|
||||||
@Transient val tbar = "i'm invisible"
|
var fov: Vector2 = Vector2(0.0,0.0)
|
||||||
|
) : GsonTestSuper(foo) {
|
||||||
|
override fun toString() = "GsonTestClass(foo=$foo, bar=$bar, baz=$baz, fov=$fov)"
|
||||||
}
|
}
|
||||||
|
|
||||||
interface GsonTestInterface {
|
interface GsonTestInterface {
|
||||||
|
|||||||
Reference in New Issue
Block a user