dk.nversion.copybook.CopyBookSerializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of copybook4java Show documentation
Show all versions of copybook4java Show documentation
CopyBook serializer and deserializer for Java where CopyBook lines are used to annotate a normal Java class
The newest version!
/*
* Copyright (c) 2015. Troels Liebe Bentsen
* Licensed under the MIT license (LICENSE.txt)
*/
package dk.nversion.copybook;
import dk.nversion.ByteUtils;
import dk.nversion.copybook.exceptions.CopyBookException;
import dk.nversion.copybook.serializers.CopyBookMapper;
import dk.nversion.copybook.serializers.CopyBookParser;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
public class CopyBookSerializer {
private CopyBookMapper serializer;
public CopyBookSerializer(Class> type) {
this(type, false);
}
public CopyBookSerializer(Class> type, boolean debug) {
this(type, null, debug);
}
public CopyBookSerializer(Class> type, Class mapper, boolean debug) {
CopyBookParser parser = new CopyBookParser(type, debug);
try {
if(mapper != null) {
serializer = mapper.getDeclaredConstructor().newInstance();
} else {
serializer = parser.getSerializerClass().getDeclaredConstructor().newInstance();
}
serializer.initialize(parser.getConfig());
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new CopyBookException("Failed to load Serialization class("+ parser.getSerializerClass().getSimpleName() +")", e);
}
}
public byte[] serialize(T obj) {
return serializer.serialize(obj);
}
public T deserialize(byte[] bytes, Class type) {
return serializer.deserialize(bytes, type);
}
public T deserialize(InputStream inputStream, Class type) throws IOException {
return deserialize(ByteUtils.toByteArray(inputStream), type);
}
public List getErrors() {
return null; // TODO: Implement depending on errorValue
}
public int getMinRecordSize() {
return serializer.getMinRecordSize();
}
public int getMaxRecordSize() {
return serializer.getMaxRecordSize();
}
}