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
Expand converter documentation with registration methods and priority (#137)
Rewrite site/docs/serialization/converters.md to document:
- Three registration methods (member-level, options-level, type-level)
- Priority order for converter resolution
- Converter factory pattern with example
- CurrentKey context access for custom converters
- Precedence summary table including IParsable<T> fallback
Converters allow custom serialization/deserialization for specific CLR types.
6
6
7
-
## Register a converter
7
+
## Registration methods
8
8
9
-
You can register converters globally via options:
9
+
SharpYaml supports three ways to register converters, evaluated in this priority order:
10
+
11
+
1.**Member-level attribute** — highest priority, applies to a single property or field
12
+
2.**Options-level** — applies globally to all types the converter can handle
13
+
3.**Type-level attribute** — lowest priority of the three explicit registrations
14
+
15
+
If none of these match, SharpYaml falls through to built-in converters (primitives, collections, enums, etc.) and finally to the reflection-based object converter.
16
+
17
+
### Options-level registration
18
+
19
+
Register converters globally via [`YamlSerializerOptions.Converters`](xref:SharpYaml.YamlSerializerOptions.Converters). Converters are evaluated in order and take precedence over built-in converters:
10
20
11
21
```csharp
12
22
varoptions=newYamlSerializerOptions
13
23
{
14
24
Converters=
15
25
[
16
-
newMyConverter(),
26
+
newIPAddressConverter(),
27
+
newTemperatureConverterFactory(),
17
28
],
18
29
};
19
30
```
20
31
21
-
##Attribute-based converters
32
+
### Type-level attribute
22
33
23
-
Use[`YamlConverterAttribute`](xref:SharpYaml.Serialization.YamlConverterAttribute)on a type or member:
34
+
Apply[`YamlConverterAttribute`](xref:SharpYaml.Serialization.YamlConverterAttribute)to a type to associate a converter with all instances of that type:
24
35
25
36
```csharp
26
-
usingSharpYaml.Serialization;
37
+
[YamlConverter(typeof(TemperatureConverter))]
38
+
publicreadonlystructTemperature
39
+
{
40
+
publicdoubleValue { get; init; }
41
+
publicstringUnit { get; init; }
42
+
}
43
+
```
44
+
45
+
### Member-level attribute
46
+
47
+
Apply [`YamlConverterAttribute`](xref:SharpYaml.Serialization.YamlConverterAttribute) to a property or field to override the converter for that specific member:
27
48
28
-
[YamlConverter(typeof(MyTypeConverter))]
29
-
publicsealedclassMyType
49
+
```csharp
50
+
publicsealedclassConfig
30
51
{
52
+
[YamlConverter(typeof(HexIntConverter))]
53
+
publicintColor { get; set; }
54
+
55
+
// Uses the default int converter
56
+
publicintCount { get; set; }
31
57
}
32
58
```
33
59
34
60
## Converter shape
35
61
36
-
Converters operate on [`YamlReader`](xref:SharpYaml.Serialization.YamlReader) and [`YamlWriter`](xref:SharpYaml.Serialization.YamlWriter):
62
+
Converters extend [`YamlConverter<T>`](xref:SharpYaml.Serialization.YamlConverter`1) and operate on [`YamlReader`](xref:SharpYaml.Serialization.YamlReader) and [`YamlWriter`](xref:SharpYaml.Serialization.YamlWriter):
Register factories the same way as regular converters:
102
+
103
+
```csharp
104
+
varoptions=newYamlSerializerOptions
105
+
{
106
+
Converters= [newNullableConverterFactory()],
107
+
};
108
+
```
109
+
53
110
## Reader and writer basics
54
111
55
112
-[`YamlReader`](xref:SharpYaml.Serialization.YamlReader) is positioned on a token; converters must consume the current value and advance the reader.
56
113
-[`YamlWriter`](xref:SharpYaml.Serialization.YamlWriter) writes YAML in a streaming manner; converters should write a complete value (scalar/sequence/mapping).
57
114
58
115
For most custom scenarios, prefer writing scalars (`writer.WriteScalar(...)`) unless you need to emit complex YAML structures.
116
+
117
+
## Accessing the current mapping key
118
+
119
+
[`YamlReader.CurrentKey`](xref:SharpYaml.Serialization.YamlReader.CurrentKey) exposes the most recent mapping key set by built-in dictionary and object converters. Custom converters can use this for context-dependent logic:
0 commit comments