Schema Evolution
Apache Fory™ C# supports schema evolution in compatible mode. Compatible mode is enabled by default.
Compatible Mode
Fory fory = Fory.Builder()
.Build();
Compatible mode writes type metadata that allows readers and writers with different struct definitions to interoperate.
For a standalone
external structural serializer, field names, field IDs,
schema descriptors, and the Evolving setting come from the local serializer
declaration. Register the third-party target with the same stable wire identity
on every version. Abstract ordinary classes and external BaseOnly
declarations cannot set Evolving; each concrete descendant owns that setting.
Inherited Schemas
A concrete C# class has one flattened schema containing its own wire members and those selected by every annotated base. Adding, removing, renaming, or changing a base wire member therefore changes the schema of every concrete descendant. Rebuild and redeploy descendant assemblies when a base wire declaration changes.
Physical fields that are not wire members do not affect field ordering,
schema hashes, or TypeMeta. Rebuild the assembly that owns the annotated base
or external hierarchy declaration when those fields change. Rebuild dependent
descendant assemblies when the referenced package's assembly identity changes.
Compatible readers also tolerate selected scalar field type changes when the value is lossless. A
matched field can read between bool, string, numeric scalars, and decimal when the converted
value has the same logical value. Boolean strings must be exactly "0", "1", "true", or
"false". Numeric strings use finite ASCII decimal syntax without whitespace, a leading plus sign,
Unicode digits, underscores, hexadecimal notation, NaN, or infinities. Numbers and decimals read as
strings use canonical plain decimal text. Nullable fields still compose with these conversions, but
reference-tracked scalar type changes are incompatible. Invalid strings, out-of-range values, and lossy
numeric conversions fail during deserialization.
Example: Add a Field
using Apache.Fory;
[ForyStruct]
public sealed class OneStringField
{
public string? F1 { get; set; }
}
[ForyStruct]
public sealed class TwoStringField
{
public string F1 { get; set; } = string.Empty;
public string F2 { get; set; } = string.Empty;
}
Fory fory1 = Fory.Builder().Build();
fory1.Register<OneStringField>(200);
Fory fory2 = Fory.Builder().Build();
fory2.Register<TwoStringField>(200);
byte[] payload = fory1.Serialize(new OneStringField { F1 = "hello" });
TwoStringField evolved = fory2.Deserialize<TwoStringField>(payload);
// F2 falls back to default value on reader side.
System.Diagnostics.Debug.Assert(evolved.F1 == "hello");
System.Diagnostics.Debug.Assert(evolved.F2 == string.Empty);
Same-Schema Optimization
Use this only when every reader and writer always uses the same schema and you want faster serialization and smaller size:
Fory sameSchema = Fory.Builder()
.Compatible(false)
.CheckStructVersion(true)
.Build();
Because C# uses the xlang wire format only, use Compatible(false) only after verifying that every peer uses the same schema, or when native types are generated from Fory schema IDL. This mode throws on schema hash mismatches.
Best Practices
- Keep compatible mode enabled for independently deployed services.
- Keep stable type IDs across versions.
- Add new fields with safe defaults.
- Use
CheckStructVersion(true)withCompatible(false)for intentional same-schema payloads.