0.2.0 #71

Merged
altavir merged 139 commits from dev into master 2022-01-24 09:44:18 +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
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() {
@ -12,12 +13,7 @@ public class TGeoManager : TNamed() {
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