Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ protected void write(Schema schema, Object datum, Encoder out)
}
}

private <T> Object convert(Schema schema, LogicalType logicalType,
Conversion<T> conversion, Object datum) {
protected <T> Object convert(Schema schema, LogicalType logicalType,
Conversion<T> conversion, Object datum) {
if (conversion == null) {
return datum;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
*/
package org.apache.avro.specific;

import org.apache.avro.Conversion;
import org.apache.avro.Schema;
import org.apache.avro.AvroRuntimeException;
import org.apache.avro.generic.GenericDatumReader;
import org.apache.avro.io.ResolvingDecoder;
import org.apache.avro.util.ClassUtils;
import java.io.IOException;

/** {@link org.apache.avro.io.DatumReader DatumReader} for generated Java classes. */
public class SpecificDatumReader<T> extends GenericDatumReader<T> {
Expand Down Expand Up @@ -98,5 +101,26 @@ private Class getPropAsClass(Schema schema, String prop) {
}
}

@Override
protected void readField(Object r, Schema.Field f, Object oldDatum,
ResolvingDecoder in, Object state)
throws IOException {
if (r instanceof SpecificRecordBase) {
Conversion<?> conversion = ((SpecificRecordBase) r).getConversion(f.pos());

Object datum;
if (conversion != null) {
datum = readWithConversion(
oldDatum, f.schema(), f.schema().getLogicalType(), conversion, in);
} else {
datum = readWithoutConversion(oldDatum, f.schema(), in);
}

getData().setField(r, f.name(), f.pos(), datum);

} else {
super.readField(r, f, oldDatum, in, state);
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import java.io.IOException;

import org.apache.avro.Conversion;
import org.apache.avro.LogicalType;
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericDatumWriter;
import org.apache.avro.io.Encoder;
Expand Down Expand Up @@ -69,5 +71,24 @@ && getSpecificData().isStringable(datum.getClass())) {
writeString(datum, out);
}

@Override
protected void writeField(Object datum, Schema.Field f, Encoder out,
Object state) throws IOException {
if (datum instanceof SpecificRecordBase) {
Conversion<?> conversion = ((SpecificRecordBase) datum).getConversion(f.pos());
Schema fieldSchema = f.schema();
LogicalType logicalType = fieldSchema.getLogicalType();

Object value = getData().getField(datum, f.name(), f.pos());
if (conversion != null && logicalType != null) {
value = convert(fieldSchema, logicalType, conversion, value);
}

writeWithoutConversion(fieldSchema, value, out);

} else {
super.writeField(datum, f, out, state);
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.ObjectInput;
import java.io.IOException;

import org.apache.avro.Conversion;
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericRecord;

Expand All @@ -34,6 +35,11 @@ public abstract class SpecificRecordBase
public abstract Object get(int field);
public abstract void put(int field, Object value);

public Conversion<?> getConversion(int field) {
// for backward-compatibility. no older specific classes have conversions.
return null;
}

@Override
public void put(String fieldName, Object value) {
put(getSchema().getField(fieldName).pos(), value);
Expand All @@ -44,6 +50,10 @@ public Object get(String fieldName) {
return get(getSchema().getField(fieldName).pos());
}

public Conversion<?> getConverion(String fieldName) {
return getConversion(getSchema().getField(fieldName).pos());
}

@Override
public boolean equals(Object that) {
if (that == this) return true; // identical object
Expand Down
Loading