use of org.apache.avro.reflect.ReflectDatumWriter in project flink by apache.
the class EncoderDecoderTest method testObjectSerialization.
private static <X> void testObjectSerialization(X obj) {
try {
// serialize
ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
{
DataOutputStream dataOut = new DataOutputStream(baos);
DataOutputEncoder encoder = new DataOutputEncoder();
encoder.setOut(dataOut);
@SuppressWarnings("unchecked") Class<X> clazz = (Class<X>) obj.getClass();
ReflectDatumWriter<X> writer = new ReflectDatumWriter<X>(clazz);
writer.write(obj, encoder);
dataOut.flush();
dataOut.close();
}
byte[] data = baos.toByteArray();
X result = null;
// deserialize
{
ByteArrayInputStream bais = new ByteArrayInputStream(data);
DataInputStream dataIn = new DataInputStream(bais);
DataInputDecoder decoder = new DataInputDecoder();
decoder.setIn(dataIn);
@SuppressWarnings("unchecked") Class<X> clazz = (Class<X>) obj.getClass();
ReflectDatumReader<X> reader = new ReflectDatumReader<X>(clazz);
// create a reuse object if possible, otherwise we have no reuse object
X reuse = null;
try {
@SuppressWarnings("unchecked") X test = (X) obj.getClass().newInstance();
reuse = test;
} catch (Throwable t) {
}
result = reader.read(reuse, decoder);
}
// check
final String message = "Deserialized object is not the same as the original";
if (obj.getClass().isArray()) {
Class<?> clazz = obj.getClass();
if (clazz == byte[].class) {
assertArrayEquals(message, (byte[]) obj, (byte[]) result);
} else if (clazz == short[].class) {
assertArrayEquals(message, (short[]) obj, (short[]) result);
} else if (clazz == int[].class) {
assertArrayEquals(message, (int[]) obj, (int[]) result);
} else if (clazz == long[].class) {
assertArrayEquals(message, (long[]) obj, (long[]) result);
} else if (clazz == char[].class) {
assertArrayEquals(message, (char[]) obj, (char[]) result);
} else if (clazz == float[].class) {
assertArrayEquals(message, (float[]) obj, (float[]) result, 0.0f);
} else if (clazz == double[].class) {
assertArrayEquals(message, (double[]) obj, (double[]) result, 0.0);
} else {
assertArrayEquals(message, (Object[]) obj, (Object[]) result);
}
} else {
assertEquals(message, obj, result);
}
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
fail("Test failed due to an exception: " + e.getMessage());
}
}
use of org.apache.avro.reflect.ReflectDatumWriter in project Gaffer by gchq.
the class AvroSerialiser method serialise.
public byte[] serialise(final Object object) throws SerialisationException {
Schema schema = ReflectData.get().getSchema(object.getClass());
DatumWriter<Object> datumWriter = new ReflectDatumWriter<>(schema);
DataFileWriter<Object> dataFileWriter = new DataFileWriter<>(datumWriter);
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
try {
dataFileWriter.create(schema, byteOut);
dataFileWriter.append(object);
dataFileWriter.flush();
} catch (final IOException e) {
throw new SerialisationException("Unable to serialise given object of class: " + object.getClass().getName(), e);
} finally {
close(dataFileWriter);
}
return byteOut.toByteArray();
}
use of org.apache.avro.reflect.ReflectDatumWriter in project hadoop by apache.
the class AvroTestUtil method testReflect.
public static void testReflect(Object value, Type type, String schema) throws Exception {
// check that schema matches expected
Schema s = ReflectData.get().getSchema(type);
assertEquals(Schema.parse(schema), s);
// check that value is serialized correctly
ReflectDatumWriter<Object> writer = new ReflectDatumWriter<Object>(s);
ByteArrayOutputStream out = new ByteArrayOutputStream();
writer.write(value, EncoderFactory.get().directBinaryEncoder(out, null));
ReflectDatumReader<Object> reader = new ReflectDatumReader<Object>(s);
Object after = reader.read(null, DecoderFactory.get().binaryDecoder(out.toByteArray(), null));
assertEquals(value, after);
}
Aggregations