io.apicurio.registry.serde.AbstractSerializer Maven / Gradle / Ivy
package io.apicurio.registry.serde;
import io.apicurio.registry.resolver.ParsedSchema;
import io.apicurio.registry.resolver.SchemaLookupResult;
import io.apicurio.registry.resolver.SchemaResolver;
import io.apicurio.registry.resolver.strategy.ArtifactReferenceResolverStrategy;
import io.apicurio.registry.rest.client.RegistryClient;
import io.apicurio.registry.serde.data.SerdeMetadata;
import io.apicurio.registry.serde.data.SerdeRecord;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UncheckedIOException;
public abstract class AbstractSerializer extends AbstractSerDe {
public AbstractSerializer() {
super();
}
public AbstractSerializer(RegistryClient client) {
super(client);
}
public AbstractSerializer(SchemaResolver schemaResolver) {
super(schemaResolver);
}
public AbstractSerializer(RegistryClient client, SchemaResolver schemaResolver) {
super(client, schemaResolver);
}
public AbstractSerializer(RegistryClient client, ArtifactReferenceResolverStrategy strategy,
SchemaResolver schemaResolver) {
super(client, strategy, schemaResolver);
}
protected abstract void serializeData(ParsedSchema schema, U data, OutputStream out)
throws IOException;
public byte[] serializeData(String topic, U data) {
// just return null
if (data == null) {
return null;
}
try {
SerdeMetadata resolverMetadata = new SerdeMetadata(topic, isKey());
SchemaLookupResult schema = getSchemaResolver()
.resolveSchema(new SerdeRecord<>(resolverMetadata, data));
ByteArrayOutputStream out = new ByteArrayOutputStream();
out.write(MAGIC_BYTE);
getIdHandler().writeId(schema.toArtifactReference(), out);
serializeData(schema.getParsedSchema(), data, out);
return out.toByteArray();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}