packer.write(ValueFactory.createNilValue());
} else if (object instanceof String || object instanceof Number || object instanceof Boolean) {
packer.write(object);
} else if (object instanceof Element) {
try {
final Element element = (Element) object;
final Set<String> propertyKeys = element.getPropertyKeys();
final int propertySize = propertyKeys.size();
final boolean isVertex = !(element instanceof Edge);
final int elementSize = (isVertex ? 2 : 5) + ((propertySize > 0) ? 1 : 0);
packer.writeMapBegin(elementSize);
packer.write(Tokens._ID);
packer.write(serializeElementId(element));
if (isVertex) {
packer.write(Tokens._TYPE);
packer.write(Tokens.VERTEX);
} else {
final Edge edge = (Edge) element;
packer.write(Tokens._TYPE);
packer.write(Tokens.EDGE);
packer.write(Tokens._IN_V);
packer.write(serializeElementId(edge.getVertex(Direction.IN)));
packer.write(Tokens._OUT_V);
packer.write(serializeElementId(edge.getVertex(Direction.OUT)));
packer.write(Tokens._LABEL);
packer.write(edge.getLabel());
}
if (propertyKeys.size() > 0) {
packer.write(Tokens._PROPERTIES);
packer.writeMapBegin(propertySize);
final Iterator<String> itty = propertyKeys.iterator();
while (itty.hasNext()) {
final String propertyKey = itty.next();
packer.write(propertyKey);
serializeObject(element.getProperty(propertyKey), packer);
}
packer.writeMapEnd(false);
}
packer.writeMapEnd(false);
} catch (Exception e) {
// if a transaction gets closed and the element goes out of scope it may not serialize. in these
// cases the vertex will just be written as null. this can happen during binding serialization.
// specific case is doing this:
// v = g.addVertex()
// g.rollback()
// in some graphs v will go out of scope, yet it is still on the bindings as a Vertex object.
packer.writeNil();
}
} else if (object instanceof Map) {
final Map map = (Map) object;
packer.writeMapBegin(map.size());
for (Object key : map.keySet()) {
if(key instanceof Element) {
// restructure element -> x maps
// this is typical in Tree and Map returns where the key is an object value instead of a
// primitive. MsgPack can't process keys that way so the data needs to be restructured
// so that it doesn't end up simply being toString'd
final Element element = (Element) key;
writeMapKey(element.getId(), packer);
final HashMap<String, Object> m = new HashMap<String, Object>();
m.put(Tokens._KEY, element);
m.put(Tokens._VALUE, map.get(key));
serializeObject(m, packer);
} else {