title: Xlang Serialization sidebar_position: 3 id: xlang_serialization license: | Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Apache Fory™ C# supports xlang serialization with other Fory implementations.
C# always writes and reads the xlang frame header. There is no mode switch, so interoperability code only needs to configure the remaining settings such as compatibility mode and reference tracking.
Fory fory = Fory.Builder() .Build();
[ForyStruct] public sealed class Person { public string Name { get; set; } = string.Empty; public int Age { get; set; } } Fory fory = Fory.Builder() .Build(); fory.Register<Person>(100);
Use the same ID mapping on all languages.
fory.Register<Person>("com.example.Person");
Person person = new() { Name = "Alice", Age = 30 }; byte[] payload = fory.Serialize(person);
Fory fory = Fory.builder() .withXlang(true) .withRefTracking(true) .build(); fory.register(Person.class, 100); Person value = (Person) fory.deserialize(payloadFromCSharp);
import pyfory fory = pyfory.Fory(xlang=True, ref=True) fory.register_type(Person, type_id=100) value = fory.deserialize(payload_from_csharp)
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.
C# List<T> maps to Fory list<T>. Use the schema marker Apache.Fory.Schema.Types.Array<T> when a field is dense array<T>.
| Fory schema | C# schema marker sketch |
|---|---|
list<int32> | S.List<S.Int32> |
array<bool> | S.Array<S.Bool> |
array<int8> | S.Array<S.Int8> |
array<int16> | S.Array<S.Int16> |
array<int32> | S.Array<S.Int32> |
array<int64> | S.Array<S.Int64> |
array<uint8> | S.Array<S.UInt8> |
array<uint16> | S.Array<S.UInt16> |
array<uint32> | S.Array<S.UInt32> |
array<uint64> | S.Array<S.UInt64> |
array<float16> | S.Array<S.Float16> |
array<bfloat16> | S.Array<S.BFloat16> |
array<float32> | S.Array<S.Float32> |
array<float64> | S.Array<S.Float64> |