Encodes an object to a byte array.

@param obj an object to be encoded
496925
@Override
  public byte[] encode(final T obj) {
    final Encoder<T> encoder = (Encoder<T>) clazzToEncoderMap.get(obj.getClass());
    if (encoder == null) {
      throw new RemoteRuntimeException("Encoder for " + obj.getClass() + " not known.");
    }

    final WakeTuplePBuf.Builder tupleBuilder = WakeTuplePBuf.newBuilder();
    tupleBuilder.setClassName(obj.getClass().getName());
    tupleBuilder.setData(ByteString.copyFrom(encoder.encode(obj)));
    return tupleBuilder.build().toByteArray();
  }
550015
function toByteArray(obj) {
  var uint = new Uint8Array(obj.length);
  for (var i = 0, l = obj.length; i < l; i++){
    uint[i] = obj.charCodeAt(i);
  }

  return new Uint8Array(uint);
}
368959
public static byte[] getBytes(Object obj) throws IOException {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		ObjectOutputStream oos = new ObjectOutputStream(bos);
		oos.writeObject(obj);
		oos.flush();
		oos.close();
		bos.close();
		byte[] data = bos.toByteArray();
		return data;
	}
562638
public static <T extends Serializable> void serializeToStream(T obj, OutputStream out) throws IOException
	{
		ObjectOutputStream objOut;

		if(out instanceof ObjectOutputStream)
			objOut = (ObjectOutputStream)out;
		else
			objOut = new ObjectOutputStream(out);
		objOut.writeObject(obj);
		objOut.close();
	}
907037
public static byte[] serialize(Serializable obj) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
        serialize(obj, baos);
        return baos.toByteArray();
    }
870275
public static byte[] toByteArrayFst(final Object obj, final ClassLoader classLoader) {
        if (obj == null) {
            return null;
        }
        ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
        if (classLoader != null) {
            Thread.currentThread().setContextClassLoader(classLoader);
        }
        try {
            FSTConfiguration conf = fstConf.get();
            conf.setClassLoader(classLoader != null ? classLoader : oldClassLoader);
            return conf.asByteArray(obj);
        } catch (Exception e) {
            throw e instanceof SerializationException ? (SerializationException) e
                    : new SerializationException(e);
        } finally {
            Thread.currentThread().setContextClassLoader(oldClassLoader);
        }
    }
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;
  }
863440
@SuppressWarnings({ "rawtypes", "unchecked"})
  public static byte[] encode(final String attributeName, final Object obj) {
    TypeConverter converter = DataConverter.attributeConverters
        .get(attributeName);
    if (converter == null) {
      log.warn(
          "Unable to find a suitable data converter for attribute {}.",
          attributeName);
      throw new IllegalArgumentException(
          "Unable to find a suitable data converter for attribute \""
              + attributeName + "\".");
    }

    if (obj instanceof String) {
      return converter.encode((String) obj);
    }
    return converter.encode(obj);
  }
1148710
def default_encoder(obj):
    
    if isinstance(obj, bytes):
        return json.dumps(bytes_to_str(obj),
                          ensure_ascii=False).encode("utf-8")
    else:
        return json.dumps(obj, ensure_ascii=False).encode("utf-8")
63953
def writeObject(self, obj):
        
        log_debug("Writing object of type {0}".format(type(obj).__name__))
        if isinstance(obj, JavaArray):
            # Deserialized Java array
            self.write_array(obj)
        elif isinstance(obj, JavaEnum):
            # Deserialized Java Enum
            self.write_enum(obj)
        elif isinstance(obj, JavaObject):
            # Deserialized Java object
            self.write_object(obj)
        elif isinstance(obj, JavaString):
            # Deserialized String
            self.write_string(obj)
        elif isinstance(obj, JavaClass):
            # Java class
            self.write_class(obj)
        elif obj is None:
            # Null
            self.write_null()
        elif type(obj) is str:
            # String value
            self.write_blockdata(obj)
        else:
            # Unhandled type
            raise RuntimeError(
                "Object serialization of type {0} is not "
                "supported.".format(type(obj))
            )
759904
@Override
  public byte[] encode(final NamingLookupResponse obj) {
    final List<AvroNamingAssignment> assignments = new ArrayList<>(obj.getNameAssignments().size());
    for (final NameAssignment nameAssignment : obj.getNameAssignments()) {
      assignments.add(AvroNamingAssignment.newBuilder()
          .setId(nameAssignment.getIdentifier().toString())
          .setHost(nameAssignment.getAddress().getHostName())
          .setPort(nameAssignment.getAddress().getPort())
          .build());
    }
    return AvroUtils.toBytes(
        AvroNamingLookupResponse.newBuilder().setTuples(assignments).build(), AvroNamingLookupResponse.class
    );
  }
735323
public static byte[] getBytesFor(String name, Object obj)
	{
		if(obj == null)
			throw new NullPointerException("obj is null");
		return getBytesFor(name,obj.getClass());
	}
1306600
private void writeObject(Writable obj) throws IOException {
    // For Text and BytesWritable, encode them directly, so that they end up
    // in C++ as the natural translations.
    if (obj instanceof Text) {
      Text t = (Text) obj;
      int len = t.getLength();
      WritableUtils.writeVInt(stream, len);
      stream.write(t.getBytes(), 0, len);
    } else if (obj instanceof BytesWritable) {
      BytesWritable b = (BytesWritable) obj;
      int len = b.getLength();
      WritableUtils.writeVInt(stream, len);
      stream.write(b.getBytes(), 0, len);
    } else {
      buffer.reset();
      obj.write(buffer);
      int length = buffer.getLength();
      WritableUtils.writeVInt(stream, length);
      stream.write(buffer.getData(), 0, length);
    }
  }
154023
public void serialize(final Object obj, final ByteBuffer buffer) {
    ByteBufferOutput out = new ByteBufferOutput(buffer);
    Kryo kryo = borrow();
    try {
      kryo.writeClassAndObject(out, obj);
      out.flush();
    } finally {
      release(kryo);
    }
  }
453913
@Override
  public byte[] encode(final NamingUnregisterRequest obj) {
    final AvroNamingUnRegisterRequest result = AvroNamingUnRegisterRequest.newBuilder()
        .setId(obj.getIdentifier().toString())
        .build();
    return AvroUtils.toBytes(result, AvroNamingUnRegisterRequest.class);
  }
1304693
public Object encode(final Object o) {
        if (o == null) {
            return null;
        }
        return encode(o.getClass(), o);
    }
1098637
private int getSerializationSize(final Object obj) {
		try {
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			ObjectOutputStream oos = new ObjectOutputStream(bos);
			oos.writeObject(obj);
			oos.close();

			byte[] bytes = bos.toByteArray();
			return bytes.length;
		} catch (IOException ex) {
			// Unable to serialize so cannot determine size.
			return -1;
		}
	}