forked from kscience/visionforge
WIP Root object model
This commit is contained in:
parent
b79265e8c2
commit
6ff3a04278
@ -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() {
|
||||||
@ -12,12 +13,7 @@ public class TGeoManager : TNamed() {
|
|||||||
public val fVolumes: TObjArray = TObjArray.empty
|
public val fVolumes: TObjArray = TObjArray.empty
|
||||||
|
|
||||||
|
|
||||||
companion object {
|
public companion object {
|
||||||
public val rootJson: Json = Json {
|
|
||||||
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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
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