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

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

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

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.util.Map;

import jakarta.json.bind.Jsonb;

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

/**
 * A {@link Deserializer} that deserializes JSON using JSON-B.
 */
public class JsonbDeserializer implements Deserializer {

    private final Jsonb jsonb;
    private final Type type;
    private final boolean jsonbNeedsClosing;

    public JsonbDeserializer(Class clazz) {
        this(clazz, JsonbProducer.get(), true);
    }

    public JsonbDeserializer(Class clazz, Jsonb jsonb) {
        this(clazz, jsonb, false);
    }

    private JsonbDeserializer(Class clazz, Jsonb jsonb, boolean jsonbNeedsClosing) {
        this.type = clazz;
        this.jsonb = jsonb;
        this.jsonbNeedsClosing = jsonbNeedsClosing;
    }

    public JsonbDeserializer(Type type) {
        this(type, JsonbProducer.get(), true);
    }

    public JsonbDeserializer(Type type, Jsonb jsonb) {
        this(type, jsonb, false);
    }

    private JsonbDeserializer(Type type, Jsonb jsonb, boolean jsonbNeedsClosing) {
        this.type = type;
        this.jsonb = jsonb;
        this.jsonbNeedsClosing = jsonbNeedsClosing;
    }

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

    @Override
    public T deserialize(String topic, byte[] data) {
        if (data == null) {
            return null;
        }

        try (InputStream is = new ByteArrayInputStream(data)) {
            return jsonb.fromJson(is, type);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void close() {
        if (!jsonbNeedsClosing) {
            return;
        }

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy