All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.quarkus.kafka.client.serialization.JsonbSerde Maven / Gradle / Ivy

There is a newer version: 3.17.5
Show newest version
package io.quarkus.kafka.client.serialization;

import java.util.Map;

import jakarta.json.bind.Jsonb;

import org.apache.kafka.common.serialization.Deserializer;
import org.apache.kafka.common.serialization.Serde;
import org.apache.kafka.common.serialization.Serializer;

/**
 * A {@link Serde} that (de-)serializes JSON using JSON-B.
 */
public class JsonbSerde implements Serde {

    private final Jsonb jsonb;
    private final boolean jsonbNeedsClosing;

    private final JsonbSerializer serializer;
    private final JsonbDeserializer deserializer;

    public JsonbSerde(Class type) {
        this(type, JsonbProducer.get(), true);
    }

    public JsonbSerde(Class type, Jsonb jsonb) {
        this(type, jsonb, false);
    }

    private JsonbSerde(Class type, Jsonb jsonb, boolean jsonbNeedsClosing) {
        this.jsonb = jsonb;
        this.jsonbNeedsClosing = jsonbNeedsClosing;

        this.serializer = new JsonbSerializer(jsonb);
        this.deserializer = new JsonbDeserializer(type, jsonb);
    }

    @Override
    public void configure(Map configs, boolean isKey) {
    }

    @Override
    public void close() {
        serializer.close();
        deserializer.close();

        if (jsonbNeedsClosing) {
            try {
                jsonb.close();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public Serializer serializer() {
        return serializer;
    }

    @Override
    public Deserializer deserializer() {
        return deserializer;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy