Fauna v10 .NET/C# Driver 1.0.1
 
Loading...
Searching...
No Matches
StructSerializers.cs
Go to the documentation of this file.
2using Fauna.Mapping;
3
4namespace Fauna.Serialization;
5
6
7internal class StringSerializer : BaseSerializer<string?>
8{
9 public override string? Deserialize(MappingContext ctx, ref Utf8FaunaReader reader) =>
10 reader.CurrentTokenType switch
11 {
12 TokenType.Null => null,
13 TokenType.String => reader.GetString(),
14 _ => throw new SerializationException(UnexpectedTypeDecodingMessage(reader.CurrentTokenType.GetFaunaType()))
15 };
16
17 public override void Serialize(MappingContext context, Utf8FaunaWriter writer, object? o)
18 {
19 switch (o)
20 {
21 case null:
22 writer.WriteNullValue();
23 break;
24 case string s:
25 writer.WriteStringValue(s);
26 break;
27 default:
29 }
30 }
31
32 public override List<FaunaType> GetSupportedTypes() => new List<FaunaType> { FaunaType.Null, FaunaType.String };
33}
34
35internal class ByteSerializer : BaseSerializer<byte>
36{
37 public override byte Deserialize(MappingContext ctx, ref Utf8FaunaReader reader) =>
38 reader.CurrentTokenType switch
39 {
40 TokenType.Int => reader.GetByte(),
41 _ => throw new SerializationException(UnexpectedTypeDecodingMessage(reader.CurrentTokenType.GetFaunaType()))
42 };
43
44 public override void Serialize(MappingContext context, Utf8FaunaWriter writer, object? o)
45 {
46 switch (o)
47 {
48 case null:
49 writer.WriteNullValue();
50 break;
51 case byte i:
52 writer.WriteIntValue(i);
53 break;
54 default:
56 }
57 }
58
59 public override List<FaunaType> GetSupportedTypes() => new List<FaunaType> { FaunaType.Int, FaunaType.Null };
60}
61
62internal class BytesSerializer : BaseSerializer<byte[]>
63{
64 public override byte[] Deserialize(MappingContext ctx, ref Utf8FaunaReader reader) =>
65 reader.CurrentTokenType switch
66 {
67 TokenType.Bytes => reader.GetBytes(),
68 _ => throw new SerializationException(UnexpectedTypeDecodingMessage(reader.CurrentTokenType.GetFaunaType()))
69 };
70
71 public override void Serialize(MappingContext context, Utf8FaunaWriter writer, object? o)
72 {
73 switch (o)
74 {
75 case null:
76 writer.WriteNullValue();
77 break;
78 case byte[] b:
79 writer.WriteBytesValue(b);
80 break;
81 default:
83 }
84 }
85
86 public override List<FaunaType> GetSupportedTypes() => new List<FaunaType> { FaunaType.Bytes, FaunaType.Null };
87}
88
89internal class SByteSerializer : BaseSerializer<sbyte>
90{
91 public override sbyte Deserialize(MappingContext ctx, ref Utf8FaunaReader reader) =>
92 reader.CurrentTokenType switch
93 {
94 TokenType.Int => reader.GetUnsignedByte(),
95 _ => throw new SerializationException(UnexpectedTypeDecodingMessage(reader.CurrentTokenType.GetFaunaType()))
96 };
97
98 public override void Serialize(MappingContext context, Utf8FaunaWriter writer, object? o)
99 {
100 switch (o)
101 {
102 case null:
103 writer.WriteNullValue();
104 break;
105 case sbyte i:
106 writer.WriteIntValue(i);
107 break;
108 default:
110 }
111 }
112
113 public override List<FaunaType> GetSupportedTypes() => new List<FaunaType> { FaunaType.Int, FaunaType.Null };
114}
115
116
117internal class ShortSerializer : BaseSerializer<short>
118{
119 public override short Deserialize(MappingContext ctx, ref Utf8FaunaReader reader) =>
120 reader.CurrentTokenType switch
121 {
122 TokenType.Int => reader.GetShort(),
123 _ => throw new SerializationException(UnexpectedTypeDecodingMessage(reader.CurrentTokenType.GetFaunaType()))
124 };
125
126 public override void Serialize(MappingContext context, Utf8FaunaWriter writer, object? o)
127 {
128 switch (o)
129 {
130 case null:
131 writer.WriteNullValue();
132 break;
133 case short i:
134 writer.WriteIntValue(i);
135 break;
136 default:
138 }
139 }
140
141 public override List<FaunaType> GetSupportedTypes() => new List<FaunaType> { FaunaType.Int, FaunaType.Null };
142}
143
144internal class UShortSerializer : BaseSerializer<ushort>
145{
146 public override ushort Deserialize(MappingContext ctx, ref Utf8FaunaReader reader) =>
147 reader.CurrentTokenType switch
148 {
149 TokenType.Int => reader.GetUnsignedShort(),
150 _ => throw new SerializationException(UnexpectedTypeDecodingMessage(reader.CurrentTokenType.GetFaunaType()))
151 };
152
153 public override void Serialize(MappingContext context, Utf8FaunaWriter writer, object? o)
154 {
155 switch (o)
156 {
157 case null:
158 writer.WriteNullValue();
159 break;
160 case ushort i:
161 writer.WriteIntValue(i);
162 break;
163 default:
165 }
166 }
167
168 public override List<FaunaType> GetSupportedTypes() => new List<FaunaType> { FaunaType.Int, FaunaType.Null };
169}
170
171internal class IntSerializer : BaseSerializer<int>
172{
173 public override int Deserialize(MappingContext ctx, ref Utf8FaunaReader reader) =>
174 reader.CurrentTokenType switch
175 {
176 TokenType.Int => reader.GetInt(),
177 _ => throw new SerializationException(UnexpectedTypeDecodingMessage(reader.CurrentTokenType.GetFaunaType()))
178 };
179
180 public override void Serialize(MappingContext context, Utf8FaunaWriter writer, object? o)
181 {
182 switch (o)
183 {
184 case null:
185 writer.WriteNullValue();
186 break;
187 case int i:
188 writer.WriteIntValue(i);
189 break;
190 default:
192 }
193 }
194
195 public override List<FaunaType> GetSupportedTypes() => new List<FaunaType> { FaunaType.Int, FaunaType.Null };
196}
197
198internal class UIntSerializer : BaseSerializer<uint>
199{
200 public override uint Deserialize(MappingContext ctx, ref Utf8FaunaReader reader) =>
201 reader.CurrentTokenType switch
202 {
203 TokenType.Int or TokenType.Long => reader.GetUnsignedInt(),
204 _ => throw new SerializationException(UnexpectedTypeDecodingMessage(reader.CurrentTokenType.GetFaunaType()))
205 };
206
207 public override void Serialize(MappingContext context, Utf8FaunaWriter writer, object? o)
208 {
209 switch (o)
210 {
211 case null:
212 writer.WriteNullValue();
213 break;
214 case uint i:
215 writer.WriteLongValue(i);
216 break;
217 default:
219 }
220 }
221
222 public override List<FaunaType> GetSupportedTypes() => new List<FaunaType> { FaunaType.Int, FaunaType.Long, FaunaType.Null };
223}
224
225internal class LongSerializer : BaseSerializer<long>
226{
227 public override long Deserialize(MappingContext ctx, ref Utf8FaunaReader reader) =>
228 reader.CurrentTokenType switch
229 {
230 TokenType.Int or TokenType.Long => reader.GetLong(),
231 _ => throw new SerializationException(UnexpectedTypeDecodingMessage(reader.CurrentTokenType.GetFaunaType()))
232 };
233
234 public override void Serialize(MappingContext context, Utf8FaunaWriter writer, object? o)
235 {
236 switch (o)
237 {
238 case null:
239 writer.WriteNullValue();
240 break;
241 case long i:
242 writer.WriteLongValue(i);
243 break;
244 default:
246 }
247 }
248
249 public override List<FaunaType> GetSupportedTypes() => new List<FaunaType> { FaunaType.Int, FaunaType.Long, FaunaType.Null };
250}
251
252internal class FloatSerializer : BaseSerializer<float>
253{
254 public override float Deserialize(MappingContext ctx, ref Utf8FaunaReader reader) =>
255 reader.CurrentTokenType switch
256 {
257 TokenType.Int or TokenType.Long or TokenType.Double => reader.GetFloat(),
258 _ => throw new SerializationException(UnexpectedTypeDecodingMessage(reader.CurrentTokenType.GetFaunaType()))
259 };
260
261 public override void Serialize(MappingContext context, Utf8FaunaWriter writer, object? o)
262 {
263 switch (o)
264 {
265 case null:
266 writer.WriteNullValue();
267 break;
268 case float i:
269 writer.WriteDoubleValue(i);
270 break;
271 default:
273 }
274 }
275
276
277 public override List<FaunaType> GetSupportedTypes() => new List<FaunaType> { FaunaType.Double, FaunaType.Int, FaunaType.Long, FaunaType.Null };
278}
279
280internal class DoubleSerializer : BaseSerializer<double>
281{
282 public override double Deserialize(MappingContext ctx, ref Utf8FaunaReader reader) =>
283 reader.CurrentTokenType switch
284 {
285 TokenType.Int or TokenType.Long or TokenType.Double => reader.GetDouble(),
286 _ => throw new SerializationException(UnexpectedTypeDecodingMessage(reader.CurrentTokenType.GetFaunaType()))
287 };
288
289 public override void Serialize(MappingContext context, Utf8FaunaWriter writer, object? o)
290 {
291 switch (o)
292 {
293 case null:
294 writer.WriteNullValue();
295 break;
296 case double i:
297 writer.WriteDoubleValue(i);
298 break;
299 default:
301 }
302 }
303
304 public override List<FaunaType> GetSupportedTypes() => new List<FaunaType> { FaunaType.Double, FaunaType.Int, FaunaType.Long, FaunaType.Null };
305}
306
307internal class BooleanSerializer : BaseSerializer<bool>
308{
309 public override bool Deserialize(MappingContext ctx, ref Utf8FaunaReader reader) =>
310 reader.CurrentTokenType switch
311 {
312 TokenType.True or TokenType.False => reader.GetBoolean(),
313 _ => throw new SerializationException(UnexpectedTypeDecodingMessage(reader.CurrentTokenType.GetFaunaType()))
314 };
315
316 public override void Serialize(MappingContext context, Utf8FaunaWriter writer, object? o)
317 {
318 switch (o)
319 {
320 case null:
321 writer.WriteNullValue();
322 break;
323 case bool i:
324 writer.WriteBooleanValue(i);
325 break;
326 default:
328 }
329 }
330
331 public override List<FaunaType> GetSupportedTypes() => new List<FaunaType> { FaunaType.Boolean, FaunaType.Null };
332}
333
334internal class DateOnlySerializer : BaseSerializer<DateOnly>
335{
336 public override DateOnly Deserialize(MappingContext ctx, ref Utf8FaunaReader reader) =>
337 reader.CurrentTokenType switch
338 {
339 TokenType.Date => reader.GetDate(),
340 _ => throw new SerializationException(UnexpectedTypeDecodingMessage(reader.CurrentTokenType.GetFaunaType()))
341 };
342
343 public override void Serialize(MappingContext context, Utf8FaunaWriter writer, object? o)
344 {
345 switch (o)
346 {
347 case null:
348 writer.WriteNullValue();
349 break;
350 case DateOnly i:
351 writer.WriteDateValue(i);
352 break;
353 default:
355 }
356 }
357
358 public override List<FaunaType> GetSupportedTypes() => new List<FaunaType> { FaunaType.Date, FaunaType.Null };
359}
360
361internal class DateTimeSerializer : BaseSerializer<DateTime>
362{
363 public override DateTime Deserialize(MappingContext ctx, ref Utf8FaunaReader reader) =>
364 reader.CurrentTokenType switch
365 {
366 TokenType.Time => reader.GetTime(),
367 _ => throw new SerializationException(UnexpectedTypeDecodingMessage(reader.CurrentTokenType.GetFaunaType()))
368 };
369
370 public override void Serialize(MappingContext context, Utf8FaunaWriter writer, object? o)
371 {
372 switch (o)
373 {
374 case null:
375 writer.WriteNullValue();
376 break;
377 case DateTime i:
378 writer.WriteTimeValue(i);
379 break;
380 default:
382 }
383 }
384
385 public override List<FaunaType> GetSupportedTypes() => new List<FaunaType> { FaunaType.Null, FaunaType.Time };
386}
387
388internal class DateTimeOffsetSerializer : BaseSerializer<DateTimeOffset>
389{
390 public override DateTimeOffset Deserialize(MappingContext ctx, ref Utf8FaunaReader reader) =>
391 reader.CurrentTokenType switch
392 {
393 TokenType.Time => reader.GetTime(),
394 _ => throw new SerializationException(UnexpectedTypeDecodingMessage(reader.CurrentTokenType.GetFaunaType()))
395 };
396
397 public override void Serialize(MappingContext context, Utf8FaunaWriter writer, object? o)
398 {
399 switch (o)
400 {
401 case null:
402 writer.WriteNullValue();
403 break;
404 case DateTimeOffset i:
405 writer.WriteTimeValue(i);
406 break;
407 default:
409 }
410 }
411
412 public override List<FaunaType> GetSupportedTypes() => new List<FaunaType> { FaunaType.Null, FaunaType.Time };
413}
Represents error that occur during serialization and deserialization of Fauna data.
A class representing the mapping context to be used during serialization and deserialization.
string UnsupportedSerializationTypeMessage(Type type)
A helper to build an unsupported serialization type exception message.
object? ISerializer. Deserialize(MappingContext ctx, ref Utf8FaunaReader reader)
Consumes or partially consumes the provided reader and deserializes into a result.
void ISerializer. Serialize(MappingContext context, Utf8FaunaWriter writer, object? o)
Serializes the provided object onto the Utf8FaunaWriter
FaunaType
An enum representing possible Fauna types.
Definition FaunaType.cs:7