Fauna v10 .NET/C# Driver 0.2.0-beta
 
Loading...
Searching...
No Matches
NullableDocumentSerializer.cs
Go to the documentation of this file.
2using Fauna.Mapping;
3using Fauna.Types;
4
5namespace Fauna.Serialization;
6
7internal class NullableDocumentSerializer<T> : BaseSerializer<NullableDocument<T>> where T : class
8{
9 private static readonly DocumentSerializer<NullableDocument<Document>> _nullableDoc = new();
10 private static readonly DocumentSerializer<NullableDocument<NamedDocument>> _nullableNamedDoc = new();
11 private static readonly DocumentSerializer<NullableDocument<Ref>> _nullabelDocRef = new();
12 private static readonly DocumentSerializer<NullableDocument<NamedRef>> _nullabelNamedDocRef = new();
13
14 public NullableDocumentSerializer()
15 {
16 }
17
18 public override NullableDocument<T> Deserialize(MappingContext context, ref Utf8FaunaReader reader)
19 {
20 if (reader.CurrentTokenType is not (TokenType.StartObject or TokenType.StartRef or TokenType.StartDocument))
21 throw new SerializationException(
22 $"Unexpected token while deserializing into {typeof(NullableDocument<T>)}: {reader.CurrentTokenType}");
23
24 if (typeof(T) == typeof(Document)) return (_nullableDoc.Deserialize(context, ref reader) as NullableDocument<T>)!;
25 if (typeof(T) == typeof(NamedDocument)) return (_nullableNamedDoc.Deserialize(context, ref reader) as NullableDocument<T>)!;
26 if (typeof(T) == typeof(Ref)) return (_nullabelDocRef.Deserialize(context, ref reader) as NullableDocument<T>)!;
27 if (typeof(T) == typeof(NamedRef)) return (_nullabelNamedDocRef.Deserialize(context, ref reader) as NullableDocument<T>)!;
28
29 var info = context.GetInfo(typeof(T));
30 try
31 {
32 var v = info.ClassSerializer.Deserialize(context, ref reader);
33 return new NonNullDocument<T>((v as T)!);
34 }
35 catch (NullDocumentException e)
36 {
37 return new NullDocument<T>(e.Id, e.Collection, e.Cause);
38 }
39 }
40
41 public override void Serialize(MappingContext context, Utf8FaunaWriter writer, object? o)
42 {
43 DynamicSerializer.Singleton.Serialize(context, writer, o);
44 }
45}
A class representing the mapping context to be used during serialization and deserialization.
MappingInfo GetInfo(Type ty, string? colName=null)
Gets the MappingInfo for a given Type.
Represents a document.
Definition Document.cs:7
Represents a document that has a "name" instead of an "id". For example, a Role document is represent...
Represents a document ref that has a "name" instead of an "id". For example, a Role document referenc...
Definition NamedRef.cs:8
A class wrapping a non-null document returned by Fauna.
A class representing a null document returned by Fauna.
A wrapper class that allows Document and user-defined classes to be null references.
Represents a document ref.
Definition Ref.cs:7
TokenType
Enumerates the types of tokens used in Fauna serialization.
Definition TokenType.cs:7