Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event bus codec doesn't work for a list of objet #452

Open
ZouhairBear opened this issue Aug 16, 2023 · 1 comment
Open

Event bus codec doesn't work for a list of objet #452

ZouhairBear opened this issue Aug 16, 2023 · 1 comment
Labels

Comments

@ZouhairBear
Copy link

Context

I have used this exemple CustomMessageCodec to make a codec for a simple object. It works for a simple object so I wanted to use for a list of objects. This didn't work.

Exemple of used Codec class

import fr.convergence.proddoc.common.model.surcharge.SurchargeDocument
import fr.convergence.proddoc.common.util.json
import io.vertx.core.buffer.Buffer
import io.vertx.core.eventbus.MessageCodec
import io.vertx.mutiny.core.Vertx
import kotlinx.serialization.builtins.ListSerializer
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class SurchargeDocumentListCodecRegistering {
    @Inject
    constructor(vertx: Vertx) {
        vertx.eventBus().registerCodec(SurchargeDocumentListCodec())
    }
}

class SurchargeDocumentListCodec : MessageCodec<List<SurchargeDocument>, List<SurchargeDocument>> {


    override fun encodeToWire(buffer: Buffer, list: List<SurchargeDocument>) {


        val encodedList = json.encodeToString(ListSerializer(SurchargeDocument.serializer()), list)
        val length = encodedList.toByteArray().size
        buffer.appendInt(length)
        buffer.appendString(encodedList)
    }

    override fun decodeFromWire(position: Int, buffer: Buffer): List<SurchargeDocument> {
        var _pos = position
        val length = buffer.getInt(_pos)
        val jsonContent = buffer.getString(4.let { _pos += it; _pos }, length.let { _pos += it; _pos })
        return json.decodeFromString(ListSerializer(SurchargeDocument.serializer()), jsonContent)
    }

    override fun transform(list: List<SurchargeDocument>): List<SurchargeDocument> {
        return list.toList() // Transform as needed
    }

    override fun name(): String {
        return this.javaClass.simpleName
    }

    override fun systemCodecID(): Byte {
        return -1
    }
}

Extra

  • OS : ubuntu 22.04
  • JAVA version : 17
@tsegismont
Copy link
Contributor

This will not work because Vert.x has no way to know if the list object was templated.

If your objects are all annotated with Jackson databind annotations, you could use a default codec selector: https://vertx.io/blog/whats-new-in-vert-x-4-3/#dynamic-codec-lookup

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants