Fauna v10 .NET/C# Driver 1.0.1
 
Loading...
Searching...
No Matches
ListSerializer.cs
Go to the documentation of this file.
1using System.Collections;
3using Fauna.Mapping;
4using ArgumentException = System.ArgumentException;
5
6namespace Fauna.Serialization;
7
8internal class ListSerializer<T> : BaseSerializer<List<T>>
9{
10 private readonly ISerializer<T> _elemSerializer;
11
12 public ListSerializer(ISerializer<T> elemSerializer)
13 {
14 _elemSerializer = elemSerializer;
15 }
16
17 public override List<FaunaType> GetSupportedTypes() => new List<FaunaType> { FaunaType.Array, FaunaType.Null };
18
19 public override List<T> Deserialize(MappingContext ctx, ref Utf8FaunaReader reader)
20 {
21 if (reader.CurrentTokenType == TokenType.StartPage)
22 throw new SerializationException(
23 $"Unexpected token while deserializing into {typeof(List<T>)}: {reader.CurrentTokenType}");
24
25 var wrapInList = reader.CurrentTokenType != TokenType.StartArray;
26
27 var lst = new List<T>();
28
29 if (wrapInList)
30 {
31 lst.Add(_elemSerializer.Deserialize(ctx, ref reader));
32 }
33 else
34 {
35 while (reader.Read() && reader.CurrentTokenType != TokenType.EndArray)
36 {
37 lst.Add(_elemSerializer.Deserialize(ctx, ref reader));
38 }
39 }
40
41 return lst;
42 }
43
44 public override void Serialize(MappingContext ctx, Utf8FaunaWriter w, object? o)
45 {
46 if (o is null)
47 {
48 w.WriteNullValue();
49 return;
50 }
51
52 if (o.GetType().IsGenericType &&
53 o.GetType().GetGenericTypeDefinition() == typeof(List<>) ||
54 o.GetType().GetGenericTypeDefinition() == typeof(IEnumerable))
55 {
56 w.WriteStartArray();
57 foreach (object? elem in (IEnumerable)o)
58 {
59 _elemSerializer.Serialize(ctx, w, elem);
60 }
61 w.WriteEndArray();
62 return;
63 }
64
66 }
67}
System.ArgumentException ArgumentException
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.
void Serialize(MappingContext ctx, Utf8FaunaWriter writer, object? o)
Serializes the provided object into the Utf8FaunaWriter.
FaunaType
An enum representing possible Fauna types.
Definition FaunaType.cs:7
TokenType
Enumerates the types of tokens used in Fauna serialization.
Definition TokenType.cs:7