I'm trying to implement a Moshi ContentConverter, ...
# ktor
d
I'm trying to implement a Moshi ContentConverter, but when I run the tests in
JsonContentNegotiationTest
for it, it fails testBadlyFormattedJson, that it expects a BAD REQUEST... what am I doing wrong? (code in the thread)
Copy code
class MoshiSerializer(block: Moshi.Builder.() -> Moshi.Builder = { this }) : ContentConverter {
    private val backend: Moshi = Moshi.Builder().apply { block() }.build()

    override suspend fun deserialize(charset: Charset, typeInfo: TypeInfo, content: ByteReadChannel): Any? {
        try {
            return backend.adapter<Any>(typeInfo.reifiedType).fromJson(content.toInputStream().source().buffer())
        } catch (e: JsonDataException) {
            throw JsonConvertException(e.message ?: "", e)
        }
    }

    override suspend fun serializeNullable(
        contentType: ContentType,
        charset: Charset,
        typeInfo: TypeInfo,
        value: Any?
    ): OutgoingContent? {
        if (value == null) return TextContent("null", ContentType.Application.Json)

        return TextContent(backend.adapter<Any>(typeInfo.reifiedType).toJson(value), contentType)
    }
}

fun Configuration.moshi(
    contentType: ContentType = ContentType.Application.Json,
    block: Moshi.Builder.() -> Unit = {}
) {
    val builder = Moshi.Builder()
    builder.apply(block)
    val converter = MoshiSerializer()
    register(contentType, converter)
}
a
As I can see you catch only the
JsonDataException
in the
deserialize
method but the
EOFException
is thrown during an execution of the test.
d
So what should I do?
Oh catch the eof exception too...