use of org.corfudb.runtime.object.ICorfuSMR in project CorfuDB by CorfuDB.
the class JSONSerializer method serialize.
/**
* Serialize an object into a given byte buffer.
*
* @param o The object to serialize.
* @param b The bytebuf to serialize it into.
*/
@Override
public void serialize(Object o, ByteBuf b) {
String className = o == null ? "null" : o.getClass().getName();
if (className.endsWith(ICorfuSMR.CORFUSMR_SUFFIX)) {
String SMRClass = className.split("\\$")[0];
className = "CorfuObject";
byte[] classNameBytes = className.getBytes();
b.writeShort(classNameBytes.length);
b.writeBytes(classNameBytes);
byte[] SMRClassNameBytes = SMRClass.getBytes();
b.writeShort(SMRClassNameBytes.length);
b.writeBytes(SMRClassNameBytes);
UUID id = ((ICorfuSMR) o).getCorfuStreamID();
log.trace("Serializing a CorfuObject of type {} as a stream pointer to {}", SMRClass, id);
b.writeLong(id.getMostSignificantBits());
b.writeLong(id.getLeastSignificantBits());
} else {
byte[] classNameBytes = className.getBytes();
b.writeShort(classNameBytes.length);
b.writeBytes(classNameBytes);
if (o == null) {
return;
}
try (ByteBufOutputStream bbos = new ByteBufOutputStream(b)) {
try (OutputStreamWriter osw = new OutputStreamWriter(bbos)) {
gson.toJson(o, o.getClass(), osw);
}
} catch (IOException ie) {
log.error("Exception during serialization!", ie);
}
}
}
use of org.corfudb.runtime.object.ICorfuSMR in project CorfuDB by CorfuDB.
the class SMRMapTest method canGetID.
@Test
@SuppressWarnings("unchecked")
public void canGetID() throws Exception {
UUID id = UUID.randomUUID();
ICorfuSMR testMap = (ICorfuSMR) getRuntime().getObjectsView().build().setStreamID(id).setTypeToken(new TypeToken<SMRMap<String, String>>() {
}).open();
assertThat(id).isEqualTo(testMap.getCorfuStreamID());
}
Aggregations