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
+55-1Lines changed: 55 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -44,7 +44,7 @@ public sealed class Holiday
44
44
45
45
# Supported types
46
46
47
-
Both serialiser and deserialiser support the core built-in types of `byte`, `sbyte`, `short`, `ushort`, `int`, `uint`, `long`, `ulong`, `float`, `double`, `decimal`, `string`, as well as `DateTime`, `DateTimeOffset`, `TimeSpan`, `Guid`, `IntPtr`, `Version`, `Nullable<T>`, `IReadOnlyList<T>`, `IReadOnlyDictionary<TKey, TValue>`, `Tuple<...>` and enum types.
47
+
Both serialiser and deserialiser support the core built-in types of `byte`, `sbyte`, `short`, `ushort`, `int`, `uint`, `long`, `ulong`, `float`, `double`, `decimal`, `string`, as well as `DateTime`, `DateTimeOffset`, `TimeSpan`, `Guid`, `IntPtr`, `Version`, `Nullable<T>`, `IReadOnlyList<T>`, `IReadOnlyDictionary<TKey, TValue>`, `Tuple<...>` and enum types. Additionally, a `Union<...>` type exists which allows a given field to take on one of a fixed number of known types.
48
48
49
49
Types may contain fields of further complex types, which are nested.
50
50
@@ -180,6 +180,60 @@ new Deserialiser<Reindeer>(UnexpectedFieldBehaviour.Ignore).Deserialise(...);
180
180
181
181
---
182
182
183
+
# Union Types
184
+
185
+
Dasher is strict about the types it deals with. This allows great control over message schema versions, but sometime you don't know exactly which type you will need. Union types allow flexibility, but in a controlled fashion.
186
+
187
+
Dasher provides a `Union<T1,T2,...>` type. This allows a given field's type to be one of a set of known types.
188
+
189
+
Let's look at some examples. Firstly, construction of a union instance:
190
+
191
+
```csharp
192
+
// implicit conversion
193
+
Union<int, string>union1=1;
194
+
Union<int, string>union2="Hello";
195
+
196
+
// alternatively, explicit construction
197
+
varunion1=Union<int, string>.Create(1);
198
+
varunion2=Union<int, string>.Create("Hello");
199
+
```
200
+
201
+
There are a few ways to consume unions:
202
+
203
+
```csharp
204
+
// consuming a union
205
+
union1.Type// int
206
+
union1.Value// 1 (boxed as object)
207
+
208
+
// perform an action based on type
209
+
union1.Match(
210
+
i=>Console.WriteLine($"Union holds an int: {i}"),
211
+
s=>Console.WriteLine($"Union holds a string: {s}"));
212
+
213
+
// mapping based on type
214
+
varnum=union1.Match(
215
+
i=>i*2,
216
+
s=>s.Length);
217
+
```
218
+
219
+
The `Match` methods offer great performance as they won't box value types and don't involve any lookup based on type (beyond a single vtable lookup).
220
+
221
+
A class could have a property of type `Union<int, string>` and be successfully serialised and deserialised by Dasher. That property can contain either of these types.
222
+
223
+
This allows a controlled form of polymorphism (with base type requirement), and allows for heterogeneous lists/dictionaries. For example:
0 commit comments