7internal class DocumentSerializer<T> : BaseSerializer<T> where T : class
10 public override T Deserialize(
MappingContext context, ref Utf8FaunaReader reader)
12 return reader.CurrentTokenType
switch
14 TokenType.StartDocument => DeserializeDocument(context, ref reader),
15 TokenType.StartRef => DeserializeRef(context, ref reader),
16 _ =>
throw new SerializationException(
17 $
"Unexpected token while deserializing into {typeof(NullableDocument<T>)}: {reader.CurrentTokenType}")
21 public override void Serialize(
MappingContext context, Utf8FaunaWriter writer,
object? o)
23 DynamicSerializer.Singleton.Serialize(context, writer, o);
26 private T DeserializeDocument(
MappingContext context, ref Utf8FaunaReader reader)
28 var data =
new Dictionary<string, object?>();
34 while (reader.Read() && reader.CurrentTokenType !=
TokenType.EndDocument)
36 if (reader.CurrentTokenType !=
TokenType.FieldName)
37 throw new SerializationException(
38 $
"Unexpected token while deserializing into Document: {reader.CurrentTokenType}");
40 var fieldName = reader.GetString()!;
45 id = reader.GetString();
48 name = DynamicSerializer.Singleton.Deserialize(context, ref reader);
51 coll = reader.GetModule();
59 if (info.Type == typeof(T) || typeof(
object) == typeof(T))
61 if (info.ClassSerializer is IClassDocumentSerializer ser)
64 string? nm = name !=
null ? (string)name : null;
65 return (ser.DeserializeDocument(context,
id, nm, ref reader) as T)!;
72 ts = reader.GetTime();
75 data[fieldName] = DynamicSerializer.Singleton.Deserialize(context, ref reader);
80 if (
id !=
null && coll !=
null && ts !=
null)
83 if (typeof(
Ref) == typeof(T))
85 return (
new Ref(
id, coll) as T)!;
91 var docRef =
new Ref(
id, coll);
95 if (name !=
null) data[
"name"] = name;
97 var doc =
new Document(
id, coll, ts.GetValueOrDefault(), data);
106 if (name !=
null && coll !=
null && ts !=
null)
109 var nameAsString = (string)name;
110 var r =
new NamedDocument(nameAsString, coll, ts.GetValueOrDefault(), data);
111 if (r is T d)
return d;
113 if (nr is T nnd)
return nnd;
118 return (
new NamedRef(nameAsString, coll) as T)!;
124 var docRef =
new NamedRef(nameAsString, coll);
128 var doc =
new NamedDocument(nameAsString, coll, ts.GetValueOrDefault(), data);
137 throw new SerializationException(
"Unsupported document type.");
140 private T DeserializeRef(
MappingContext context, ref Utf8FaunaReader reader)
145 string? cause =
null;
148 while (reader.Read() && reader.CurrentTokenType !=
TokenType.EndRef)
150 if (reader.CurrentTokenType !=
TokenType.FieldName)
151 throw new SerializationException(
152 $
"Unexpected token while deserializing into DocumentRef: {reader.CurrentTokenType}");
154 var fieldName = reader.GetString()!;
159 id = reader.GetString();
162 name = reader.GetString();
165 coll = reader.GetModule();
168 cause = reader.GetString();
171 exists = reader.GetBoolean();
176 if (
id !=
null && coll !=
null && exists)
178 var docRef =
new Ref(
id, coll);
184 return (docRef as T)!;
187 if (name !=
null && coll !=
null && exists)
189 var docRef =
new NamedRef(name, coll);
195 return (docRef as T)!;
198 if (
id !=
null && coll !=
null && !exists)
200 if (typeof(
Ref) == typeof(T) || typeof(
Document) == typeof(T))
202 throw new NullDocumentException(
id, coll, cause!);
213 if (name !=
null && coll !=
null && !exists)
217 throw new NullDocumentException(name, coll, cause!);
229 throw new SerializationException(
"Unsupported reference type");
A class representing the mapping context to be used during serialization and deserialization.
bool TryGetCollection(string col, [NotNullWhen(true)] out MappingInfo? ret)
Gets the MappingInfo for a given collection name.
Represents a module, a singleton object grouping related functionalities. Modules are serialized as @...
string Name
Gets the name of the module. The name is used to identify and reference the module.
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...
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.
TokenType
Enumerates the types of tokens used in Fauna serialization.