com.fasterxml.jackson.dataformat.avro.AvroMapper Maven / Gradle / Ivy
package com.fasterxml.jackson.dataformat.avro;
import java.io.*;
import org.apache.avro.Schema;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.avro.schema.AvroSchemaGenerator;
/**
* Convenience {@link AvroMapper}, which is mostly similar to simply
* constructing a mapper with {@link AvroFactory}, but also adds little
* bit of convenience around {@link AvroSchema} generation.
*
* @since 2.5
*/
public class AvroMapper extends ObjectMapper
{
private static final long serialVersionUID = 1L;
public AvroMapper() {
this(new AvroFactory());
}
public AvroMapper(AvroFactory f) {
super(f);
registerModule(new AvroModule());
}
protected AvroMapper(ObjectMapper src) {
super(src);
}
@Override
public AvroMapper copy()
{
_checkInvalidCopy(AvroMapper.class);
return new AvroMapper(this);
}
@Override
public Version version() {
return PackageVersion.VERSION;
}
@Override
public AvroFactory getFactory() {
return (AvroFactory) _jsonFactory;
}
/**
* @since 2.5
*/
public AvroSchema schemaFor(Class> type) throws JsonMappingException
{
AvroSchemaGenerator gen = new AvroSchemaGenerator();
acceptJsonFormatVisitor(type, gen);
return gen.getGeneratedSchema();
}
/**
* @since 2.5
*/
public AvroSchema schemaFor(JavaType type) throws JsonMappingException
{
AvroSchemaGenerator gen = new AvroSchemaGenerator();
acceptJsonFormatVisitor(type, gen);
return gen.getGeneratedSchema();
}
/**
* Method for reading an Avro Schema from given {@link InputStream},
* and once done (successfully or not), closing the stream.
*
* @since 2.6
*/
public AvroSchema schemaFrom(InputStream in) throws IOException
{
try {
return new AvroSchema(new Schema.Parser().setValidate(true)
.parse(in));
} finally {
in.close();
}
}
/**
* Convenience method for reading {@link AvroSchema} from given
* encoded JSON representation.
*
* @since 2.6
*/
public AvroSchema schemaFrom(String schemaAsString) throws IOException
{
return new AvroSchema(new Schema.Parser().setValidate(true)
.parse(schemaAsString));
}
/**
* Convenience method for reading {@link AvroSchema} from given
* encoded JSON representation.
*
* @since 2.6
*/
public AvroSchema schemaFrom(File schemaFile) throws IOException
{
return new AvroSchema(new Schema.Parser().setValidate(true)
.parse(schemaFile));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy