io.quarkus.kafka.client.serialization.JsonbSerde Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of quarkus-kafka-client Show documentation
Show all versions of quarkus-kafka-client Show documentation
Connect to Apache Kafka with its native API
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