Fauna .NET Driver 0.1.0-beta
 
Loading...
Searching...
No Matches
Deserializers.cs
Go to the documentation of this file.
1using Fauna.Mapping;
3
4namespace Fauna.Linq;
5
6internal class MappedDeserializer<I, O> : BaseDeserializer<O>
7{
8 private IDeserializer<I> _inner;
9 private Func<I, O> _mapper;
10
11 public MappedDeserializer(IDeserializer<I> inner, Func<I, O> mapper)
12 {
13 _inner = inner;
14 _mapper = mapper;
15 }
16
17 public override O Deserialize(MappingContext context, ref Utf8FaunaReader reader) =>
18 _mapper(_inner.Deserialize(context, ref reader));
19}
20
21internal class ProjectionDeserializer : BaseDeserializer<object?[]>
22{
23 private IDeserializer[] _fields;
24
25 public ProjectionDeserializer(IEnumerable<IDeserializer> fields)
26 {
27 _fields = fields.ToArray();
28 }
29
30 public override object?[] Deserialize(MappingContext context, ref Utf8FaunaReader reader)
31 {
32 if (reader.CurrentTokenType != TokenType.StartArray)
33 throw UnexpectedToken(reader.CurrentTokenType);
34
35 var values = new object?[_fields.Length];
36
37 for (var i = 0; i < _fields.Length; i++)
38 {
39 if (!reader.Read()) throw new SerializationException("Unexpected end of stream");
40 if (reader.CurrentTokenType == TokenType.EndArray) throw UnexpectedToken(reader.CurrentTokenType);
41
42 values[i] = _fields[i].Deserialize(context, ref reader);
43 }
44
45 if (!reader.Read()) throw new SerializationException("Unexpected end of stream");
46 if (reader.CurrentTokenType != TokenType.EndArray) throw UnexpectedToken(reader.CurrentTokenType);
47
48 return values;
49 }
50
51 private SerializationException UnexpectedToken(TokenType tokenType) =>
52 new SerializationException($"Unexpected token while deserializing LINQ element: {tokenType}");
53}
T Deserialize(MappingContext context, ref Utf8FaunaReader reader)
Represents error that occur during serialization and deserialization of Fauna data.
new T Deserialize(MappingContext context, ref Utf8FaunaReader reader)
TokenType
Enumerates the types of tokens used in Fauna serialization.
Definition TokenType.cs:7
Represents a reader that provides fast, non-cached, forward-only access to serialized data.