Apache.Fory
1.7.8
dotnet add package Apache.Fory --version 1.7.8
NuGet\Install-Package Apache.Fory -Version 1.7.8
<PackageReference Include="Apache.Fory" Version="1.7.8" />
<PackageVersion Include="Apache.Fory" Version="1.7.8" />
<PackageReference Include="Apache.Fory" />
paket add Apache.Fory --version 1.7.8
#r "nuget: Apache.Fory, 1.7.8"
#:package Apache.Fory@1.7.8
#addin nuget:?package=Apache.Fory&version=1.7.8
#tool nuget:?package=Apache.Fory&version=1.7.8
Apache Fory™ C#
Apache Fory™ is a blazing fast multi-language serialization framework powered by JIT compilation and zero-copy techniques.
The C# implementation provides high-performance object graph serialization for .NET with source-generated serializers, optional reference tracking, schema evolution support, and cross-language compatibility.
Why Apache Fory™ C#?
- High-performance binary serialization for .NET 8+
- Cross-language compatibility with Java, Python, C++, Go, Rust, and JavaScript
- Source-generator-based serializers for
[ForyStruct]types, plus[ForyEnum]and[ForyUnion]registration - Flattened class inheritance schemas, including explicitly selected private base state
- Field-level schema descriptors with
[ForyField(Type = typeof(...))] - Optional shared/circular reference tracking (
TrackRef(true)) - Compatible mode for schema evolution
- Reduced-precision carriers for
Half/BFloat16scalars andHalf[]/List<Half>/BFloat16[]/List<BFloat16>array payloads - Thread-safe wrapper (
ThreadSafeFory) for concurrent workloads - Dynamic object serialization APIs for heterogeneous payloads
Quick Start
Requirements
- .NET SDK 8.0+
- C# 12+
Add Apache Fory™ C#
From NuGet, reference the single Apache.Fory package. It includes the Fory library plus the source generator for [ForyStruct], [ForyEnum], and [ForyUnion] types.
<ItemGroup>
<PackageReference Include="Apache.Fory" Version="1.7.8" />
</ItemGroup>
For local development against this repository, reference the Fory project and generator project directly:
<ItemGroup>
<ProjectReference Include="../fory/csharp/src/Fory/Fory.csproj" />
<ProjectReference
Include="../fory/csharp/src/Fory.Generator/Fory.Generator.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false" />
</ItemGroup>
Basic Example
using Apache.Fory;
[ForyStruct]
public sealed class User
{
public long Id { get; set; }
public string Name { get; set; } = string.Empty;
public string? Email { get; set; }
}
Fory fory = Fory.Builder().Build();
fory.Register<User>(1);
User user = new()
{
Id = 1,
Name = "Alice",
Email = "alice@example.com",
};
byte[] payload = fory.Serialize(user);
User decoded = fory.Deserialize<User>(payload);
Core Features
1. Object Graph Serialization
[ForyStruct] types are serialized with generated serializers.
[ForyStruct]
public sealed class Address
{
public string Street { get; set; } = string.Empty;
public int Zip { get; set; }
}
[ForyStruct]
public sealed class Person
{
public long Id { get; set; }
public string Name { get; set; } = string.Empty;
public List<Address> Addresses { get; set; } = [];
}
Fory fory = Fory.Builder().Build();
fory.Register<Address>(100);
fory.Register<Person>(101);
2. Class Inheritance
Annotate every first-party class in a serializable hierarchy directly. The concrete serializer flattens inherited fields and properties into one schema:
[ForyStruct]
public abstract class Entity
{
[ForyField(1)]
private long _id;
public long Id => _id;
}
[ForyStruct]
public sealed class User : Entity
{
[ForyField(2)]
public string Name { get; set; } = string.Empty;
}
An inaccessible base member is serialized only when it has [ForyField].
Unmodifiable third-party bases use an external BaseOnly declaration. See the
class inheritance guide.
3. Shared and Circular References
Enable reference tracking to preserve object identity.
[ForyStruct]
public sealed class Node
{
public int Value { get; set; }
public Node? Next { get; set; }
}
Fory fory = Fory.Builder().TrackRef(true).Build();
fory.Register<Node>(200);
Node node = new() { Value = 7 };
node.Next = node;
Node decoded = fory.Deserialize<Node>(fory.Serialize(node));
System.Diagnostics.Debug.Assert(object.ReferenceEquals(decoded, decoded.Next));
4. Schema Evolution
Compatible mode allows schema changes between writer and reader.
[ForyStruct]
public sealed class OneField
{
public string? F1 { get; set; }
}
[ForyStruct]
public sealed class TwoFields
{
public string F1 { get; set; } = string.Empty;
public string F2 { get; set; } = string.Empty;
}
Fory fory1 = Fory.Builder().Build();
fory1.Register<OneField>(300);
Fory fory2 = Fory.Builder().Build();
fory2.Register<TwoFields>(300);
TwoFields decoded = fory2.Deserialize<TwoFields>(fory1.Serialize(new OneField { F1 = "hello" }));
5. Dynamic Object Serialization
Use dynamic APIs for unknown/heterogeneous payloads.
Fory fory = Fory.Builder().Build();
Dictionary<object, object?> map = new()
{
["k1"] = 7,
[2] = "v2",
[true] = null,
};
byte[] payload = fory.Serialize<object?>(map);
object? decoded = fory.Deserialize<object?>(payload);
6. Thread-Safe Fory
Fory is single-thread optimized. Use ThreadSafeFory for concurrent access.
using ThreadSafeFory fory = Fory.Builder().BuildThreadSafe();
fory.Register<User>(1);
Parallel.For(0, 128, i =>
{
byte[] payload = fory.Serialize(i);
int decoded = fory.Deserialize<int>(payload);
});
7. External Types
Generate serializers for mutable third-party classes, structs, and enums with a local serializer declaration:
[ForyStruct(Target = typeof(ThirdParty.User))]
internal abstract class UserSerializer
{
[ForyField(
1,
TargetDeclaringType = typeof(ThirdParty.User),
TargetMemberName = "<Name>k__BackingField")]
public abstract string Name { get; }
}
Fory fory = Fory.Builder().Build();
fory.Register<ThirdParty.User>(300);
ThirdParty.User user = new() { Name = "Alice" };
byte[] payload = fory.Serialize(user);
ThirdParty.User decoded = fory.Deserialize<ThirdParty.User>(payload);
The target is the runtime and registration type. See the external-types guide.
8. Custom Serializers
Provide specialized encoding logic with Serializer<T>.
public sealed class PointSerializer : Serializer<Point>
{
public override Point DefaultValue => new();
public override void WriteData(WriteContext context, in Point value, bool hasGenerics)
{
context.Writer.WriteVarInt32(value.X);
context.Writer.WriteVarInt32(value.Y);
}
public override Point ReadData(ReadContext context)
{
return new Point
{
X = context.Reader.ReadVarInt32(),
Y = context.Reader.ReadVarInt32(),
};
}
}
Fory fory = Fory.Builder().Build();
fory.Register<Point, PointSerializer>(400);
Cross-Language Serialization
Use consistent registration mappings across languages.
Fory fory = Fory.Builder()
.Build();
fory.Register<Person>(100); // same ID on other language peers
See xlang guide for mapping details.
Documentation
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.7.8 | 108 | 11/13/2026 |
| 1.7.8-rc4 | 92 | 11/9/2026 |
| 1.7.8-rc3 | 95 | 11/9/2026 |
| 1.7.8-rc2 | 88 | 11/7/2026 |
| 1.7.8-rc1 | 87 | 11/7/2026 |
| 1.7.7 | 378 | 10/12/2026 |
| 1.7.7-rc3 | 647 | 10/8/2026 |
| 1.7.7-rc2 | 98 | 10/7/2026 |
| 1.7.7-rc1 | 108 | 10/5/2026 |
| 1.7.6 | 202 | 9/30/2026 |
| 1.7.6-rc1 | 99 | 9/26/2026 |
| 1.7.5 | 437 | 9/25/2026 |
| 1.7.5-rc1 | 106 | 9/22/2026 |
| 1.7.4 | 159 | 9/20/2026 |
| 1.7.4-rc1 | 125 | 9/17/2026 |
| 1.7.3 | 151 | 9/15/2026 |
| 1.7.3-rc2 | 115 | 9/12/2026 |
| 1.7.3-rc1 | 120 | 9/12/2026 |
| 1.7.2 | 171 | 9/11/2026 |
| 1.7.2-rc2 | 135 | 9/8/2026 |