Fauna .NET Driver 0.1.0-beta
 
Loading...
Searching...
No Matches
PageDeserializer.cs
Go to the documentation of this file.
1using Fauna.Mapping;
2using Fauna.Types;
3
4namespace Fauna.Serialization;
5
6internal class PageDeserializer<T> : BaseDeserializer<Page<T>>
7{
8 private IDeserializer<List<T>> _dataDeserializer;
9
10 public PageDeserializer(IDeserializer<T> elemDeserializer)
11 {
12 _dataDeserializer = new ListDeserializer<T>(elemDeserializer);
13 }
14
15 public override Page<T> Deserialize(MappingContext context, ref Utf8FaunaReader reader)
16 {
17 var endToken = reader.CurrentTokenType switch
18 {
19 TokenType.StartPage => TokenType.EndPage,
20 TokenType.StartObject => TokenType.EndObject,
21 var other =>
22 throw new SerializationException(
23 $"Unexpected token while deserializing into {typeof(Page<T>)}: {other}"),
24 };
25
26 List<T>? data = null;
27 string? after = null;
28
29 while (reader.Read() && reader.CurrentTokenType != endToken)
30 {
31 var fieldName = reader.GetString()!;
32 reader.Read();
33
34 switch (fieldName)
35 {
36 case "data":
37 data = _dataDeserializer.Deserialize(context, ref reader);
38 break;
39 case "after":
40 after = reader.GetString()!;
41 break;
42 }
43 }
44
45 if (data is null)
46 throw new SerializationException($"No page data found while deserializing into {typeof(Page<T>)}");
47
48 return new Page<T>(data!, after);
49 }
50}
T Deserialize(MappingContext context, ref Utf8FaunaReader reader)
TokenType
Enumerates the types of tokens used in Fauna serialization.
Definition TokenType.cs:7
record Page< T >(IReadOnlyList< T > Data, string? After)
Represents a page in a dataset for pagination.