This serializes an object into a byte array.

@param serializable The object to be serialized.
@return The serialized object.
554329
public static final byte[] serialize(Serializable serializable) throws IOException {
	
		if (serializable == null) {
			return null;
		}
		
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		ObjectOutputStream objectOutputStream = null;
		byte[] result = null;

		if (null != _instance ){
			SerializationService ss = _instance.serializationServiceRef.getService();
			if (null != ss){
				objectOutputStream = ss.createObjectOutputStream(byteArrayOutputStream);
			}
		} 
		
		if (null == objectOutputStream){
			objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
		}
		
		try {
			objectOutputStream.writeObject(serializable);
			result = byteArrayOutputStream.toByteArray();
		} finally {

			if (byteArrayOutputStream != null) {
				byteArrayOutputStream.close();
			}
			if (objectOutputStream != null) {
				objectOutputStream.close();
			}
		}

		return result;
	}
1174827
public byte[] toBytes(T object) {
        try {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            ObjectOutputStream out = new ObjectOutputStream(stream);
            out.writeObject(object);
            return stream.toByteArray();
        } catch(IOException e) {
            throw new SerializationException(e);
        }
    }
611258
public static byte[] toByteArray(Serializable obj) {
        byte[] bytes = null;
        ObjectOutputStream oos = null;
        ByteArrayOutputStream baos = null;
        try {
            baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
            oos.writeObject(obj);
            oos.flush();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                    bytes = baos.toByteArray();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return bytes;
    }
102019
protected byte[] jsonSerialize(Object o) {
        byte[] res = null;
        try {
            res = JsonUtils.toJson(o).getBytes("utf-8");
        } catch (UnsupportedEncodingException e) {
            logger.error("JsonSerialize object fail ", e);
        }
        return res;
    }
293413
public static byte[] toByteArray( Serializable object )
	{
	    try
	    {
            ByteArrayOutputStream buf = new ByteArrayOutputStream(1024);
            ObjectOutputStream objOut = new ObjectOutputStream(buf);
            objOut.writeObject(object);
            objOut.close();
            return buf.toByteArray();
	    }
	    catch (IOException e)
	    {
	        throw new IllegalArgumentException("Cannot serialize object : "+e.toString());
	    }
	}
33809
private Serializable deserializeObject(byte[] buffer) throws IOException, ClassNotFoundException {

		Serializable theObject = null;
		ObjectInputStream objectIn = null;

		if (buffer != null) {
			objectIn = new ObjectInputStream(new ByteArrayInputStream(buffer));
			theObject = (Serializable)objectIn.readObject();
			objectIn.close();
		}
		return theObject;
	}
366043
public static byte[] serialize(Serializable s) throws IOException {

        HsqlByteArrayOutputStream bo = new HsqlByteArrayOutputStream();
        ObjectOutputStream        os = new ObjectOutputStream(bo);

        os.writeObject(s);

        return bo.toByteArray();
    }
1330589
public byte[] toByteArray() {
    try {
      final byte[] result = new byte[getSerializedSize()];
      final CodedOutputStream output = CodedOutputStream.newInstance(result);
      writeTo(output);
      output.checkNoSpaceLeft();
      return result;
    } catch (final IOException e) {
      throw new RuntimeException(
        "Serializing to a byte array threw an IOException " +
        "(should never happen).", e);
    }
  }
722212
def encode_json(self, serializable):
        
        return json.dumps(serializable.serialize(), cls=self.encoder)
1261585
public static byte[] serialize(Object obj) {
        try(ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos)) {
            oos.writeObject(obj);
            return bos.toByteArray();
        } 
        catch (IOException ex) {
            throw new UncheckedIOException(ex);
        }
    }
457833
public static final byte[] serializeToBytes(final Object object) {
    if (object == null)
      throw new IllegalArgumentException("Object was null");
    try {
      return jsonMapper.writeValueAsBytes(object);
    } catch (JsonProcessingException e) {
      throw new JSONException(e);
    }
  }
893731
public static byte[] toByteArray(Object obj) {
    int len = (int) sizeOf(obj);
    byte[] bytes = new byte[len];
    unsafe.copyMemory(obj, 0, bytes, Unsafe.ARRAY_BYTE_BASE_OFFSET, bytes.length);
    return bytes;
  }
92159
@Override
    public @Sensitive byte[] serialize() throws IOException {
        // Captured thread context
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        int size = threadContext.size();
        byte[][] contextBytes = new byte[size][];
        for (int i = 0; i < size; i++) {
            bout.reset();
            ObjectOutputStream oout = new ObjectOutputStream(bout);
            oout.writeObject(threadContext.get(i));
            oout.flush();
            contextBytes[i] = bout.toByteArray();
            oout.close();
        }

        bout.reset();
        ObjectOutputStream oout = new ObjectOutputStream(bout);

        // Number of serialized fields (list of thread context counts as one)
        oout.writeShort(3);

        // 1) Bytes for captured thread context
        oout.writeObject(contextBytes);

        // 2) Metadata identifier
        oout.writeObject(metaDataIdentifier);

        // 3) Thread context provider names
        oout.writeShort(providerNames.size());
        for (String providerName : providerNames)
            if (providerName.startsWith("com.ibm.ws.") && providerName.endsWith(".context.provider"))
                oout.writeObject(providerName.substring(10, providerName.length() - 16));
            else
                oout.writeObject(providerName);

        // Only add primitive or compatible Java types in the future. If necessary, pre-serialize to byte[]

        oout.flush();
        oout.close();

        return bout.toByteArray();
    }
329968
public static byte[] serialize(FastSerializable object) throws IOException {
        FastSerializer out = new FastSerializer();
        object.writeExternal(out);
        return out.getBBContainer().b().array();
    }
403960
protected static Serializable deserialize(String serialized) throws JMSException {
        if (serialized == null) {
            return null;
        }
        Serializable deserializedObject;
        ObjectInputStream objectInputStream = null;
        try {
            byte[] bytes = Base64.decode(serialized);
            objectInputStream = new ObjectInputStream(new ByteArrayInputStream(bytes));
            deserializedObject = (Serializable) objectInputStream.readObject();
        } catch (IOException e) {
            LOG.error("IOException: Message cannot be written", e);
            throw convertExceptionToMessageFormatException(e);
        } catch (Exception e) {
            LOG.error("Unexpected exception: ", e);
            throw convertExceptionToMessageFormatException(e);
        } finally {
            if (objectInputStream != null) {
                try {
                    objectInputStream.close();
                } catch (IOException e) {
                    LOG.warn(e.getMessage());
                }
            }
        }
        return deserializedObject;
    }
921964
def serialize(self, serializable: Optional[Union[SerializableType, List[SerializableType]]]) \
            -> PrimitiveJsonType:
        
        if serializable is None:
            # Implements #17
            return None
        elif isinstance(serializable, List):
            return [self.serialize(item) for item in serializable]
        else:
            serialized = self._create_serialized_container()

            for mapping in self._property_mappings:
                if mapping.object_property_getter is not None and mapping.serialized_property_setter is not None:
                    value = mapping.object_property_getter(serializable)
                    if not (mapping.optional and value is None):
                        if isinstance(value, type(mapping.collection_factory([]))):
                            value = list(mapping.collection_iter(value))
                        encoded_value = self._serialize_property_value(value, mapping.serializer_cls)
                        mapping.serialized_property_setter(serialized, encoded_value)

            return serialized
102535
public void serializeToBuffer(ByteBuffer b) {
        assert(getSerializedSize() <= b.remaining());
        b.putInt(getSerializedSize() - 4);
        b.put((byte)getExceptionType().ordinal());
        if (m_message != null) {
            final byte messageBytes[] = m_message.getBytes();
            b.putInt(messageBytes.length);
            b.put(messageBytes);
        } else {
            b.putInt(0);
        }
        p_serializeToBuffer(b);
    }