Fauna v10 .NET/C# Driver 1.0.0
 
Loading...
Searching...
No Matches
Deserializers.cs
Go to the documentation of this file.
2using Fauna.Mapping;
4
5namespace Fauna.Linq;
6
7internal class MappedDeserializer<I, O> : BaseSerializer<O>
8{
9 private ISerializer<I> _inner;
10 private Func<I, O> _mapper;
11
12 public MappedDeserializer(ISerializer<I> inner, Func<I, O> mapper)
13 {
14 _inner = inner;
15 _mapper = mapper;
16 }
17
18 public override List<FaunaType> GetSupportedTypes() => _inner.GetSupportedTypes();
19
20 public override O Deserialize(MappingContext ctx, ref Utf8FaunaReader reader) =>
21 _mapper(_inner.Deserialize(ctx, ref reader));
22
23 public override void Serialize(MappingContext context, Utf8FaunaWriter writer, object? o)
24 {
25 throw new NotImplementedException();
26 }
27}
28
29internal class ProjectionDeserializer : BaseSerializer<object?[]>
30{
31 private ISerializer[] _fields;
32
33 public ProjectionDeserializer(IEnumerable<ISerializer> fields)
34 {
35 _fields = fields.ToArray();
36 }
37
38 public override List<FaunaType> GetSupportedTypes() => _fields.SelectMany(x => x.GetSupportedTypes()).Distinct().ToList();
39
40 public override object?[] Deserialize(MappingContext ctx, ref Utf8FaunaReader reader)
41 {
42 if (reader.CurrentTokenType != TokenType.StartArray)
43 throw UnexpectedToken(reader.CurrentTokenType);
44
45 var values = new object?[_fields.Length];
46
47 for (var i = 0; i < _fields.Length; i++)
48 {
49 if (!reader.Read()) throw new SerializationException("Unexpected end of stream");
50 if (reader.CurrentTokenType == TokenType.EndArray) throw UnexpectedToken(reader.CurrentTokenType);
51
52 values[i] = _fields[i].Deserialize(ctx, ref reader);
53 }
54
55 if (!reader.Read()) throw new SerializationException("Unexpected end of stream");
56 if (reader.CurrentTokenType != TokenType.EndArray) throw UnexpectedToken(reader.CurrentTokenType);
57
58 return values;
59 }
60
61 public override void Serialize(MappingContext context, Utf8FaunaWriter writer, object? o)
62 {
63 throw new NotImplementedException();
64 }
65
66 private new static SerializationException UnexpectedToken(TokenType tokenType) =>
67 new($"Unexpected token while deserializing LINQ element: {tokenType}");
68}
Represents error that occur during serialization and deserialization of Fauna data.
A class representing the mapping context to be used during serialization and deserialization.
An abstract class encapsulating common serialization and deserialization logic.
Provides functionality for writing data in a streaming manner to a buffer or a stream.
A generic interface that defines serialize and deserialize behavior for a specific type,...
new T Deserialize(MappingContext ctx, ref Utf8FaunaReader reader)
Consumes all or some of a Utf8FaunaReader and returns an instance of T .
List< FaunaType > GetSupportedTypes()
A list of types to which the ISerializer applies.
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
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.