add custom json encoder to serializerable meta converter

This commit is contained in:
Alexander Nozik 2024-03-14 17:07:32 +03:00
parent 0e72b4b63c
commit db938e1ad8

View File

@ -135,16 +135,17 @@ public interface MetaConverter<T>: MetaSpec<T> {
@DFExperimental @DFExperimental
public inline fun <reified T> serializable( public inline fun <reified T> serializable(
descriptor: MetaDescriptor? = null, descriptor: MetaDescriptor? = null,
jsonEncoder: Json = Json { ignoreUnknownKeys = true },
): MetaConverter<T> = object : MetaConverter<T> { ): MetaConverter<T> = object : MetaConverter<T> {
private val serializer: KSerializer<T> = serializer() private val serializer: KSerializer<T> = serializer()
override fun readOrNull(source: Meta): T? { override fun readOrNull(source: Meta): T? {
val json = source.toJson(descriptor) val json = source.toJson(descriptor)
return Json.decodeFromJsonElement(serializer, json) return jsonEncoder.decodeFromJsonElement(serializer, json)
} }
override fun convert(obj: T): Meta { override fun convert(obj: T): Meta {
val json = Json.encodeToJsonElement(obj) val json = jsonEncoder.encodeToJsonElement(obj)
return json.toMeta(descriptor) return json.toMeta(descriptor)
} }