Cross-Language Serialization
Apache Fory™ C# supports cross-language serialization with other Fory runtimes.
Cross-Language Runtime
C# always writes and reads the xlang frame header. There is no separate Xlang(...) builder
option, so interoperability code only needs to configure the remaining runtime behavior such as
compatibility mode and reference tracking.
Fory fory = Fory.Builder()
.Compatible(true)
.Build();
Register with Stable IDs
[ForyObject]
public sealed class Person
{
public string Name { get; set; } = string.Empty;
public int Age { get; set; }
}
Fory fory = Fory.Builder()
.Compatible(true)
.Build();
fory.Register<Person>(100);
Use the same ID mapping on all languages.
Register by Namespace/Type Name
fory.Register<Person>("com.example", "Person");
Cross-Language Example
C# (Serializer)
Person person = new() { Name = "Alice", Age = 30 };
byte[] payload = fory.Serialize(person);
Java (Deserializer)
Fory fory = Fory.builder()
.withXlang(true)
.withRefTracking(true)
.build();
fory.register(Person.class, 100);
Person value = (Person) fory.deserialize(payloadFromCSharp);
Python (Deserializer)
import pyfory
fory = pyfory.Fory(xlang=True, ref=True)
fory.register_type(Person, type_id=100)
value = fory.deserialize(payload_from_csharp)
Type Mapping Reference
See xlang guide for complete mapping.
For reduced-precision numeric payloads, use Half / Half[] or List<Half> for xlang float16, and BFloat16 / BFloat16[] or List<BFloat16> for xlang bfloat16.
Best Practices
- Keep type IDs stable and documented.
- Enable
Compatible(true)for rolling upgrades. - Register all user types on both read/write peers.
- Validate integration with real payload round trips.