org.apache.avro.SchemaResolver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spf4j-avro-lib-ext Show documentation
Show all versions of spf4j-avro-lib-ext Show documentation
Avro adaptation (abstraction layer betwen zolyfarkas/avro and apache/avro) utils.
package org.apache.avro;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonNode;
import java.io.IOException;
import java.util.function.Function;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* @author Zoltan Farkas
*/
public interface SchemaResolver {
/**
* Lower level resolver implementation, that will write a schema in a "custom way"
* @param schema
* @param gen
* @return
* @throws IOException
*/
default boolean customWrite(Schema schema, JsonGenerator gen) throws IOException {
String ref = getId(schema);
if (ref != null) {
gen.writeStartObject();
gen.writeFieldName(getJsonAttrName());
gen.writeString(ref);
gen.writeEndObject();
return true;
} else {
return false;
}
}
/**
* Lowere level resolver implementation that will read a schema from a "custom way"
* @param object
* @return
*/
@Nullable
default Schema customRead(Function object) {
JsonNode refVal = object.apply(getJsonAttrName());
if (refVal != null) {
return resolveSchema(refVal.asText());
} else {
return null;
}
}
default String getJsonAttrName() {
return "$ref";
}
@Nonnull
Schema resolveSchema(String id);
@Nullable
String getId(Schema schema);
SchemaResolver NONE = new SchemaResolver() {
@Override
public Schema resolveSchema(final String id) {
throw new UnsupportedOperationException();
}
@Override
public String getId(final Schema schema) {
return null;
}
};
default void registerAsDefault() {
SchemaResolvers.registerDefault(this);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy