KGillies
09/27/2024, 10:15 PM{
"UTCTimeKey" : {
   "keyName" : "StartTimes",
   "values" : [ "1970-01-01T00:00:00Z", "2023-04-13T20:23:51.953536907Z" ],
   "units" : "utc"
 }
}, {
 "IntKey" : {
   "keyName" : "Power",
   "values" : [ 70 ],
   "units" : "volts"
 }
}
The typekey tells deserializing how to handle the RHS. These two examples have right hand sides that are the same, but in general the RHS can have varied properties. What I want to do is look at the typekey and dispatch the serialization to a specialized serializer. I’ve asked about this before and a very helpful response led me to the JsonContetPolymorphicSerializer, which works fine. Here is an example:
object ParamDeserializer : JsonContentPolymorphicSerializer<HasKey>(HasKey::class) {
    override fun selectDeserializer(content: JsonElement) = when {
        "DoubleKey" in content.jsonObject -> NumberSerializer
        "LongKey" in content.jsonObject -> IntegerSerializer
        "StringKey" in content.jsonObject -> StringSerializer
        "BooleanKey" in content.jsonObject -> BooleanSerializer
        "ChoiceKey" in content.jsonObject -> ChoiceSerializer
        else -> throw IllegalArgumentException("Unknown type key")
    }
}
This works, but we also need to serialize to CBOR and this solution is JSON-only. Does anyone have suggestions on how to write this code in a way that works for JSON and CBOR?  I’ve banged on this for quite a while so thanks for any suggestions!ephemient
09/28/2024, 4:17 AMKGillies
09/28/2024, 8:54 PMKGillies
09/30/2024, 7:26 PMephemient
09/30/2024, 10:43 PM@SerialName("CoordKey")
    val coord: CoordValue? = null,
) {
    @Serializable
    data class CoordValue(val keyName: String, val ra: Double, val dec: Double)
I just made them a common Value<T> type above for convenience. of course, you'll have to decide how you represent them in your public Param interface, but that's up to you