You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: readme.md
+42Lines changed: 42 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,6 +13,7 @@ Tomlyn is a high-performance .NET [TOML](https://toml.io/en/) 1.1 parser, round-
13
13
-**`System.Text.Json`-style API**: familiar surface with `TomlSerializer`, `TomlSerializerOptions`, `TomlTypeInfo<T>`
14
14
-**TOML 1.1.0 only**: Tomlyn v1 targets [TOML 1.1.0](https://toml.io/en/v1.1.0) and does **not** support TOML 1.0
15
15
-**Source generation**: NativeAOT / trimming friendly via `TomlSerializerContext` and `[TomlSerializable]` roots
16
+
-**Cross-project polymorphism**: register derived types at runtime or on a source-generated context when base and derived types live in different assemblies
When a base type lives in one project and derived types live in another, you can register derived types without putting `[TomlDerivedType]` on the base type.
Use `TomlPolymorphismOptions.DerivedTypeMappings` and `[TomlDerivedTypeMapping]` additively with existing base-type attributes. Base-type registrations still take precedence when the same discriminator or derived type is registered more than once.
Copy file name to clipboardExpand all lines: site/docs/serialization.md
+53-1Lines changed: 53 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -457,7 +457,7 @@ The extension data member should be a dictionary type with string keys (e.g. `ID
457
457
458
458
## Polymorphism
459
459
460
-
Tomlyn supports discriminator-based polymorphism via attributes or options.
460
+
Tomlyn supports discriminator-based polymorphism via base-type attributes, reflection-time runtime mappings, and source-generated context mappings.
461
461
462
462
### Attribute-based
463
463
@@ -509,6 +509,51 @@ public abstract class Animal { /* ... */ }
509
509
> [!NOTE]
510
510
> When both Toml and Json polymorphic attributes are present on the same type, the Toml-specific attributes take precedence.
511
511
512
+
### Cross-project runtime mappings
513
+
514
+
Use [`TomlPolymorphismOptions.DerivedTypeMappings`](xref:Tomlyn.TomlPolymorphismOptions.DerivedTypeMappings) when the base type and derived types live in different projects and you're using the reflection resolver:
This is especially useful for clean architecture and plugin-style applications where the base type cannot reference every concrete implementation.
537
+
538
+
### Cross-project source generation mappings
539
+
540
+
Use [`TomlDerivedTypeMappingAttribute`](xref:Tomlyn.Serialization.TomlDerivedTypeMappingAttribute) on a [`TomlSerializerContext`](xref:Tomlyn.Serialization.TomlSerializerContext) when you want the same pattern in NativeAOT / trimming-safe source-generated code:
Mapped derived types are discovered automatically by the generator, so they do not also need their own `[TomlSerializable]` roots.
554
+
555
+
If the base type doesn't declare [`TomlPolymorphicAttribute`](xref:Tomlyn.Serialization.TomlPolymorphicAttribute) or a JSON equivalent, Tomlyn still supports the mapping and falls back to [`TomlPolymorphismOptions`](xref:Tomlyn.TomlPolymorphismOptions) for the discriminator property name and unknown-derived-type handling.
556
+
512
557
### Default derived type
513
558
514
559
Register one derived type **without a discriminator** to act as the default. When a TOML table has no discriminator key (or an unknown discriminator), it deserializes as the default type. When serializing the default type, no discriminator is emitted.
@@ -593,6 +638,13 @@ The priority chain is:
593
638
2.`JsonPolymorphicAttribute.UnknownDerivedTypeHandling` (mapped from `JsonUnknownDerivedTypeHandling`)
Derived type registrations are merged additively with this precedence:
642
+
643
+
1.[`TomlDerivedTypeAttribute`](xref:Tomlyn.Serialization.TomlDerivedTypeAttribute) on the base type
644
+
2.[`JsonDerivedTypeAttribute`](xref:System.Text.Json.Serialization.JsonDerivedTypeAttribute) on the base type
645
+
3.[`TomlDerivedTypeMappingAttribute`](xref:Tomlyn.Serialization.TomlDerivedTypeMappingAttribute) on a source-generated context
646
+
4.[`TomlPolymorphismOptions.DerivedTypeMappings`](xref:Tomlyn.TomlPolymorphismOptions.DerivedTypeMappings) at runtime
647
+
596
648
> [!NOTE]
597
649
> [`TomlUnknownDerivedTypeHandling.Unspecified`](xref:Tomlyn.TomlUnknownDerivedTypeHandling.Unspecified) is a sentinel value for attribute properties and **cannot** be used on [`TomlPolymorphismOptions`](xref:Tomlyn.TomlPolymorphismOptions) - doing so throws `ArgumentOutOfRangeException`.
Copy file name to clipboardExpand all lines: site/docs/source-generation.md
+19Lines changed: 19 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -38,6 +38,24 @@ Nested types referenced by the root are discovered transitively - you only need
38
38
Set [`TomlSerializableAttribute.TypeInfoPropertyName`](xref:Tomlyn.Serialization.TomlSerializableAttribute.TypeInfoPropertyName) when you want to customize the generated property name exposed by the context.
39
39
Source-generated deserialization also supports C# `init` and `required` members.
40
40
41
+
## Cross-project polymorphism
42
+
43
+
When a polymorphic base type cannot reference all of its derived types, register the derived types on the context instead of on the base type:
The generator automatically includes the mapped derived types, so they don't need separate `[TomlSerializable]` roots.
57
+
If the base type already has [`TomlDerivedTypeAttribute`](xref:Tomlyn.Serialization.TomlDerivedTypeAttribute) or [`JsonDerivedTypeAttribute`](xref:System.Text.Json.Serialization.JsonDerivedTypeAttribute) registrations, those take precedence over context-level mappings.
58
+
41
59
## Use generated metadata
42
60
43
61
Use the generated [`TomlTypeInfo<T>`](xref:Tomlyn.TomlTypeInfo`1) property directly (recommended):
@@ -108,6 +126,7 @@ The source generator supports these attributes at compile time:
0 commit comments