feature/root #69

Merged
altavir merged 23 commits from feature/root into dev 2021-09-11 19:14:21 +03:00
2 changed files with 26961 additions and 8 deletions
Showing only changes of commit 6ff3a04278 - Show all commits

View File

@ -1,8 +1,9 @@
package ru.mipt.npm.root package ru.mipt.npm.root
import kotlinx.serialization.Serializable import kotlinx.serialization.*
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.modules.SerializersModule
@Serializable @Serializable
public class TGeoManager : TNamed() { public class TGeoManager : TNamed() {
@ -10,14 +11,9 @@ public class TGeoManager : TNamed() {
public val fMatrices: TObjArray = TObjArray.empty public val fMatrices: TObjArray = TObjArray.empty
public val fShapes: TObjArray = TObjArray.empty public val fShapes: TObjArray = TObjArray.empty
public val fVolumes: TObjArray = TObjArray.empty public val fVolumes: TObjArray = TObjArray.empty
companion object {
public val rootJson: Json = Json { public companion object {
encodeDefaults = true
ignoreUnknownKeys = true
classDiscriminator = "_typename"
}
/** /**
@ -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