club.pizzalord.shire.serializer.protostuff.ProtostuffSerializer Maven / Gradle / Ivy
package club.pizzalord.shire.serializer.protostuff;
import club.pizzalord.shire.sdk.exceptions.ShireException;
import club.pizzalord.shire.serializer.core.Serializer;
import io.protostuff.LinkedBuffer;
import io.protostuff.ProtostuffIOUtil;
import io.protostuff.Schema;
/**
* Protostuff serializer
*
* @author Xpizza
* @since shire1.0
*/
public class ProtostuffSerializer implements Serializer {
@Override
public byte[] serializeToBytes(T object) {
LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
try {
Schema schema = SchemaManager.getSchema((Class) object.getClass());
return ProtostuffIOUtil.toByteArray(object, schema, buffer);
} catch (Exception e) {
throw new ShireException("Failure to serialize with protostuff", e);
}
}
@Override
public String serializeToString(T object) {
byte[] bytes = serializeToBytes(object);
return new String(bytes);
}
}