feature/root #69
@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
26920
cern-root-loader/src/commonTest/resources/BM@N.root.json
Normal file
26920
cern-root-loader/src/commonTest/resources/BM@N.root.json
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user