WIP Root object model

This commit is contained in:
Alexander Nozik 2021-08-19 09:56:20 +03:00
parent b79265e8c2
commit 6ff3a04278
2 changed files with 26961 additions and 8 deletions

View File

@ -1,8 +1,9 @@
package ru.mipt.npm.root
import kotlinx.serialization.Serializable
import kotlinx.serialization.*
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.modules.SerializersModule
@Serializable
public class TGeoManager : TNamed() {
@ -10,14 +11,9 @@ public class TGeoManager : TNamed() {
public val fMatrices: TObjArray = TObjArray.empty
public val fShapes: TObjArray = TObjArray.empty
public val fVolumes: TObjArray = TObjArray.empty
companion object {
public val rootJson: Json = Json {
encodeDefaults = true
ignoreUnknownKeys = true
classDiscriminator = "_typename"
}
public companion object {
/**
@ -27,4 +23,41 @@ public class TGeoManager : TNamed() {
}
}
@OptIn(ExperimentalSerializationApi::class)
private class RootJsonSerialFormat : StringFormat {
override val serializersModule: SerializersModule get() = json.serializersModule
private val refCache: HashMap<UInt, TObject> = HashMap()
override fun <T> decodeFromString(deserializer: DeserializationStrategy<T>, string: String): T {
val match = refRegex.matchEntire(string)
return if (match != null) {
//Do unref
val ref = match.value.toUIntOrNull() ?: error("Ref value is not a number")
val refValue = refCache[ref] ?: error("Reference $ref unresolved")
refValue as T //TODO research means to make it safe
} else {
val res = json.decodeFromString(deserializer, string)
val uid = (res as? TObject)?.fUniqueID
if (uid != null && refCache[uid] == null) {
refCache[uid] = res
}
res
}
}
override fun <T> encodeToString(serializer: SerializationStrategy<T>, value: T): String =
json.encodeToString(serializer, value)
companion object {
val refRegex = """\{\s*"${"\\$"}ref"\s*:\s*(\d*)}""".toRegex()
val json: Json = Json {
encodeDefaults = true
ignoreUnknownKeys = true
classDiscriminator = "_typename"
}
}
}

File diff suppressed because it is too large Load Diff