Skip to content

Instantly share code, notes, and snippets.

@cowtowncoder
Created February 5, 2016 05:24
Show Gist options
  • Save cowtowncoder/197e137e2a91c4837d27 to your computer and use it in GitHub Desktop.
Save cowtowncoder/197e137e2a91c4837d27 to your computer and use it in GitHub Desktop.
Example of how IAP/ION codec for jvm-serializers could look like
package serializers;
import java.util.Arrays;
import com.jenkov.iap.ion.read.IonObjectReader;
import com.jenkov.iap.ion.write.IonObjectWriter;
import data.media.MediaContent;
/**
* Codec for ION/IAP.
*/
public class IapDatabind
{
public static void register(TestGroups groups) {
groups.media.add(JavaBuiltIn.mediaTransformer, new IapSerializer<MediaContent>(MediaContent.class),
new SerFeatures(
SerFormat.BIN_CROSSLANG,
SerGraph.FLAT_TREE,
SerClass.ZERO_KNOWLEDGE,
""
)
);
}
public final static class IapSerializer<T> extends Serializer<T>
{
private final IonObjectWriter writer;
private final IonObjectReader reader;
protected IapSerializer(Class<T> t)
{
reader = new IonObjectReader(t);
writer = new IonObjectWriter(t);
}
@Override
public String getName() { return "iap/databind"; }
@SuppressWarnings("unchecked")
@Override
public T deserialize(byte[] array) throws Exception {
return (T) reader.read(array, 0);
}
@Override
public byte[] serialize(T content) throws Exception {
// really should not assume known output length but...
byte[] buffer = new byte[512]; // just because we know it'll fit, for now
int end = writer.writeObject(content, 2, buffer, 0);
return Arrays.copyOf(buffer, end);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment