2

我想制作一个自定义 List 序列化程序,可以安全地解析无效的 json 数组。示例: Int 列表[1, "invalid_int", 2]应解析为[1, 2]. 我已经制作了一个序列化程序并将其添加到 Json 提供程序,但是在第一个元素之后序列化一直失败并且无法继续,所以我得到了 1 个元素的列表[1],如何正确处理无效元素以便解码器继续解析其他元素?


class SafeListSerializerStack<E>(val elementSerializer: KSerializer<E>) : KSerializer<List<E>> {

    override val descriptor: SerialDescriptor = ListSerializer(elementSerializer).descriptor

    override fun serialize(encoder: Encoder, value: List<E>) {
        val size = value.size
        val composite = encoder.beginCollection(descriptor, size)
        val iterator = value.iterator()
        for (index in 0 until size) {
            composite.encodeSerializableElement(descriptor, index, elementSerializer, iterator.next())
        }
        composite.endStructure(descriptor)
    }

    override fun deserialize(decoder: Decoder): List<E> {
        val arrayList = arrayListOf<E>()
        try {
            val startIndex = arrayList.size
            val messageBuilder = StringBuilder()
            val compositeDecoder = decoder.beginStructure(descriptor)
            while (true) {
                val index = compositeDecoder.decodeElementIndex(descriptor) // fails here on number 2
                if (index == CompositeDecoder.DECODE_DONE) {
                    break
                }
                try {
                    arrayList.add(index, compositeDecoder.decodeSerializableElement(descriptor, startIndex + index, elementSerializer))
                } catch (exception: Exception) {
                    exception.printStackTrace() // falls here when "invalid_int" is parsed, it's ok
                }
            }
            compositeDecoder.endStructure(descriptor)
            if (messageBuilder.isNotBlank()) {
                println(messageBuilder.toString())
            }
        } catch (exception: Exception) {
            exception.printStackTrace() // falls here on number 2
        }
        return arrayList
    }
}

解析无效元素并引发异常后发生错误compositeDecoder.decodeElementIndex(descriptor)

kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 4: Expected end of the array or comma
JSON input: [1, "invalid_int", 2]

我有一种感觉,它应该“吞下”无效元素并继续移动,但它却卡住了,无法继续解析,这对我来说没有意义。

4

2 回答 2

1

这可以在没有自定义序列化程序的情况下完成。只需将所有内容解析为String(指定isLenient = true允许不带引号的字符串),然后转换为Int所有有效整数:

fun main() {
    val input = "[1, \"invalid_int\", 2]"
    val result: List<Int> = Json { isLenient = true }
        .decodeFromString<List<String>>(input)
        .mapNotNull { it.toIntOrNull() }
    println(result) // [1, 2]
}

在更一般的情况下(当列表是一个字段和/或其元素不是 simple 时Int),您将需要一个自定义序列化程序:

class SafeListSerializerStack<E>(private val elementSerializer: KSerializer<E>) : KSerializer<List<E>> {
    private val listSerializer = ListSerializer(elementSerializer)
    override val descriptor: SerialDescriptor = listSerializer.descriptor

    override fun serialize(encoder: Encoder, value: List<E>) {
        listSerializer.serialize(encoder, value)
    }

    override fun deserialize(decoder: Decoder): List<E> = with(decoder as JsonDecoder) {
        decodeJsonElement().jsonArray.mapNotNull {
            try {
                json.decodeFromJsonElement(elementSerializer, it)
            } catch (e: SerializationException) {
                e.printStackTrace()
                null
            }
        }
    }
}

请注意,此解决方案仅适用于Json格式的反序列化,并且需要kotlinx.serialization1.2.0+

于 2021-06-24T20:53:10.543 回答
0

找到了一种方法,我们可以从解码器中提取 json 数组,因为我们正在使用 Json 来解析它

    override fun deserialize(decoder: Decoder): List<E> {
        val jsonInput = decoder as? JsonDecoder
            ?: error("Can be deserialized only by JSON")
        val rawJson = jsonInput.decodeJsonElement()
        if (rawJson !is JsonArray) {
            return arrayListOf()
        }

        val jsonArray = rawJson.jsonArray
        val jsonParser = jsonInput.json
        val arrayList = ArrayList<E>(jsonArray.size)

        jsonArray.forEach { jsonElement ->
            val result = readElement(jsonParser, jsonElement)
            when {
                result.isSuccess -> arrayList.add(result.getOrThrow())
                result.isFailure -> Log.d("ERROR", "error parsing array")
            }
        }
        arrayList.trimToSize()
        return arrayList
    }

    private fun readElement(json: Json, jsonElement: JsonElement): Result<E> {
        return try {
            Result.success(json.decodeFromJsonElement(elementSerializer, jsonElement))
        } catch (exception: Exception) {
            Result.failure(exception)
        }
    }
于 2021-06-25T13:06:14.803 回答