Fauna v10 .NET/C# Driver 0.2.0-beta
 
Loading...
Searching...
No Matches
DictionarySerializer.cs
Go to the documentation of this file.
1using Fauna.Mapping;
2
3namespace Fauna.Serialization;
4
5internal class DictionarySerializer<T> : BaseSerializer<Dictionary<string, T>>
6{
7 private readonly ISerializer<T> _elemSerializer;
8
9 public DictionarySerializer(ISerializer<T> elemSerializer)
10 {
11 _elemSerializer = elemSerializer;
12 }
13
14 public override Dictionary<string, T> Deserialize(MappingContext context, ref Utf8FaunaReader reader)
15 {
16 if (reader.CurrentTokenType != TokenType.StartObject)
17 throw new SerializationException(
18 $"Unexpected token while deserializing into {typeof(Dictionary<string, T>)}: {reader.CurrentTokenType}");
19
20 var dict = new Dictionary<string, T>();
21
22 while (reader.Read() && reader.CurrentTokenType != TokenType.EndObject)
23 {
24 if (reader.CurrentTokenType != TokenType.FieldName)
25 throw new SerializationException(
26 $"Unexpected token while deserializing field of {typeof(Dictionary<string, T>)}: {reader.CurrentTokenType}");
27
28 var fieldName = reader.GetString()!;
29 reader.Read();
30 dict.Add(fieldName, _elemSerializer.Deserialize(context, ref reader));
31 }
32
33 return dict;
34 }
35
36 public override void Serialize(MappingContext context, Utf8FaunaWriter writer, object? o)
37 {
38 DynamicSerializer.Singleton.Serialize(context, writer, o);
39 }
40}
A class representing the mapping context to be used during serialization and deserialization.
TokenType
Enumerates the types of tokens used in Fauna serialization.
Definition TokenType.cs:7