Fauna v10 .NET/C# Driver 1.0.1
 
Loading...
Searching...
No Matches
PageSerializer.cs
Go to the documentation of this file.
1using Fauna.Mapping;
2using Fauna.Types;
3
4namespace Fauna.Serialization;
5
6internal class PageSerializer<T> : BaseSerializer<Page<T>>
7{
8 private readonly ISerializer<List<T>> _dataSerializer;
9
10 public PageSerializer(ISerializer<T> elemSerializer)
11 {
12 _dataSerializer = new ListSerializer<T>(elemSerializer);
13 }
14
15 public override List<FaunaType> GetSupportedTypes() => [FaunaType.Null, FaunaType.Set];
16
17 public override Page<T> Deserialize(MappingContext ctx, ref Utf8FaunaReader reader)
18 {
19 bool wrapInPage = false;
20 var endToken = TokenType.None;
21 switch (reader.CurrentTokenType)
22 {
23 case TokenType.StartPage:
24 endToken = TokenType.EndPage;
25 break;
26 case TokenType.StartObject:
27 endToken = TokenType.EndObject;
28 break;
29 default:
30 wrapInPage = true;
31 break;
32 }
33
34 if (wrapInPage)
35 {
36 var data = _dataSerializer.Deserialize(ctx, ref reader);
37 return new Page<T>(data, null);
38 }
39
40 reader.Read();
41 return reader.CurrentTokenType == TokenType.String
42 ? HandleUnmaterialized(ctx, ref reader, endToken)
43 : HandleMaterialized(ctx, ref reader, endToken);
44 }
45
46 private Page<T> HandleUnmaterialized(MappingContext ctx, ref Utf8FaunaReader reader, TokenType endToken)
47 {
48 string after = reader.GetString()!;
49 reader.Read();
50 if (reader.CurrentTokenType != endToken)
51 {
52 throw UnexpectedToken(reader.CurrentTokenType);
53 }
54
55 return new Page<T>([], after);
56 }
57
58 private Page<T> HandleMaterialized(MappingContext ctx, ref Utf8FaunaReader reader, TokenType endToken)
59 {
60 List<T> data = [];
61 string? after = null;
62
63 do
64 {
65 string fieldName = reader.GetString()!;
66 reader.Read();
67
68 switch (fieldName)
69 {
70 case "data":
71 data = _dataSerializer.Deserialize(ctx, ref reader);
72 break;
73 case "after":
74 after = reader.GetString()!;
75 break;
76 }
77 } while (reader.Read() && reader.CurrentTokenType != endToken);
78
79 return new Page<T>(data, after);
80 }
81
82 public override void Serialize(MappingContext context, Utf8FaunaWriter writer, object? o)
83 {
84 throw new NotImplementedException();
85 }
86}
A class representing the mapping context to be used during serialization and deserialization.
new T Deserialize(MappingContext ctx, ref Utf8FaunaReader reader)
Consumes all or some of a Utf8FaunaReader and returns an instance of T .
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
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.