Fix attribute serializer
This commit is contained in:
parent
50ccfeab70
commit
2343f37655
@ -6,9 +6,7 @@ import kotlinx.serialization.KSerializer
|
|||||||
import kotlinx.serialization.descriptors.SerialDescriptor
|
import kotlinx.serialization.descriptors.SerialDescriptor
|
||||||
import kotlinx.serialization.encoding.Decoder
|
import kotlinx.serialization.encoding.Decoder
|
||||||
import kotlinx.serialization.encoding.Encoder
|
import kotlinx.serialization.encoding.Encoder
|
||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.*
|
||||||
import kotlinx.serialization.json.JsonObject
|
|
||||||
import kotlinx.serialization.json.buildJsonObject
|
|
||||||
|
|
||||||
public class AttributesSerializer(
|
public class AttributesSerializer(
|
||||||
private val serializableAttributes: Set<SerializableAttribute<*>>,
|
private val serializableAttributes: Set<SerializableAttribute<*>>,
|
||||||
@ -21,7 +19,13 @@ public class AttributesSerializer(
|
|||||||
val attributeMap: Map<SerializableAttribute<*>, Any> = jsonElement.entries.associate { (key, element) ->
|
val attributeMap: Map<SerializableAttribute<*>, Any> = jsonElement.entries.associate { (key, element) ->
|
||||||
val attr = serializableAttributes.find { it.serialId == key }
|
val attr = serializableAttributes.find { it.serialId == key }
|
||||||
?: error("Attribute serializer for key $key not found")
|
?: error("Attribute serializer for key $key not found")
|
||||||
val value = Json.decodeFromJsonElement(attr.serializer, element) ?: error("Null values are not allowed")
|
|
||||||
|
val json = if (decoder is JsonDecoder) {
|
||||||
|
decoder.json
|
||||||
|
} else {
|
||||||
|
Json { serializersModule = decoder.serializersModule }
|
||||||
|
}
|
||||||
|
val value = json.decodeFromJsonElement(attr.serializer, element) ?: error("Null values are not allowed")
|
||||||
|
|
||||||
attr to value
|
attr to value
|
||||||
}
|
}
|
||||||
@ -33,9 +37,16 @@ public class AttributesSerializer(
|
|||||||
value.content.forEach { (key, value) ->
|
value.content.forEach { (key, value) ->
|
||||||
if (key !in serializableAttributes) error("An attribute key $key is not in the list of allowed attributes for this serializer")
|
if (key !in serializableAttributes) error("An attribute key $key is not in the list of allowed attributes for this serializer")
|
||||||
val serializableKey = key as SerializableAttribute
|
val serializableKey = key as SerializableAttribute
|
||||||
|
|
||||||
|
val json = if (encoder is JsonEncoder) {
|
||||||
|
encoder.json
|
||||||
|
} else {
|
||||||
|
Json { serializersModule = encoder.serializersModule }
|
||||||
|
}
|
||||||
|
|
||||||
put(
|
put(
|
||||||
serializableKey.serialId,
|
serializableKey.serialId,
|
||||||
Json.encodeToJsonElement(serializableKey.serializer as KSerializer<Any>, value)
|
json.encodeToJsonElement(serializableKey.serializer as KSerializer<Any>, value)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,10 +7,18 @@ import kotlin.test.assertEquals
|
|||||||
|
|
||||||
internal class AttributesSerializationTest {
|
internal class AttributesSerializationTest {
|
||||||
|
|
||||||
|
internal class TestAttributeContainer(val attributes: Attributes)
|
||||||
|
|
||||||
|
// internal object TestContainerAttribute: SerializableAttribute<TestAttributeContainer>("container", se)
|
||||||
|
|
||||||
internal object TestAttribute : SerializableAttribute<Map<String, String>>("test", serializer())
|
internal object TestAttribute : SerializableAttribute<Map<String, String>>("test", serializer())
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun restore() {
|
fun restore() {
|
||||||
|
//
|
||||||
|
// val serializersModule = SerializersModule {
|
||||||
|
// contextual(AttributesSerializer(setOf()))
|
||||||
|
// }
|
||||||
val serializer = AttributesSerializer(setOf(NameAttribute, TestAttribute))
|
val serializer = AttributesSerializer(setOf(NameAttribute, TestAttribute))
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user