Implementation:Ray project Ray MessagePackSerializer Encode
| Knowledge Sources | |
|---|---|
| Domains | Serialization, Cross_Language_Interop |
| Last Updated | 2026-02-13 17:00 GMT |
Overview
Concrete tool for encoding Java objects into MessagePack binary format for cross-language transport provided by the Ray Java runtime.
Description
MessagePackSerializer.encode() converts a Java object to a byte array using MessagePack format. The first 9 bytes are reserved for a length header. The method returns a Pair<byte[], Boolean> where the boolean indicates cross-language compatibility. Supported types produce standard MessagePack values (isCrossLanguage=true). Unsupported types fall back to FST serialization with extension type 101 (isCrossLanguage=false).
Usage
Used internally by the Ray runtime during task argument serialization. Not typically called directly.
Code Reference
Source Location
- Repository: ray-project/ray
- File: java/runtime/src/main/java/io/ray/runtime/serializer/MessagePackSerializer.java (L248-278)
Signature
public static Pair<byte[], Boolean> encode(Object obj)
Import
import io.ray.runtime.serializer.MessagePackSerializer;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| obj | Object | Yes | The Java object to serialize. Supported cross-language types: Boolean, Byte, Short, Integer, Long, BigInteger, Float, Double, String, byte[], arrays, null. |
Outputs
| Name | Type | Description |
|---|---|---|
| result | Pair<byte[], Boolean> | Serialized bytes and isCrossLanguage flag |
Usage Examples
Automatic Serialization During Cross-Language Calls
import io.ray.api.Ray;
import io.ray.api.function.PyFunction;
// Arguments are automatically serialized via MessagePack
// String, int, and other primitives are cross-language compatible
ObjectRef<Integer> result = Ray.task(
PyFunction.of("math_module", "add", Integer.class),
3, // auto-serialized via MessagePack
4 // auto-serialized via MessagePack
).remote();