code: public static final byte[] getAsByteArray(Object object, ExternalContext ctx)
      {
          ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
          
          // get the Factory that was instantiated @ startup
          SerialFactory serialFactory = (SerialFactory) ctx.getApplicationMap().get(SERIAL_FACTORY);
          
          if(serialFactory == null)
          {
              throw new NullPointerException("serialFactory");
          }
          
          try
          {
              ObjectOutputStream writer = serialFactory.getObjectOutputStream(outputStream);
              writer.writeObject(object);
              byte[] bytes = outputStream.toByteArray();
              writer.close();
              outputStream.close();
              writer = null;
              outputStream = null;
              return bytes;
          }
          catch (IOException e)
          {
              throw new FacesException(e);
          }
      }
docs: Performs serialization with the serialization provider created by the
  SerialFactory.
  
  @param object
  @param ctx
  @return