@Override
    protected void writeValue(final T value, final Buffer buffer, final GraphBinaryWriter context) throws IOException {
        // the predicate name is either a static method of P or an instance method when a type ConnectiveP
        final boolean isConnectedP = value instanceof ConnectiveP;
        final String predicateName = value.getPredicateName();
        final Object args = isConnectedP ? ((ConnectiveP<?>) value).getPredicates() : value.getValue();

        final List<Object> argsAsList = args instanceof Collection ? new ArrayList<>((Collection) args) : Collections.singletonList(args);
        final int length = argsAsList.size();

        context.writeValue(predicateName, buffer, false);
        context.writeValue(length, buffer, false);

        for (Object o : argsAsList) {
            context.write(o, buffer);
        }
    }
@author Stephen Mallette (http://stephen.genoprime.com)
gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/binary/types/PSerializer.java