Fauna .NET Driver 0.1.0-beta
 
Loading...
Searching...
No Matches
DictionaryDeserializer.cs
Go to the documentation of this file.
1using Fauna.Mapping;
2
3namespace Fauna.Serialization;
4
5internal class DictionaryDeserializer<T> : BaseDeserializer<Dictionary<string, T>>
6{
7 private IDeserializer<T> _elemDeserializer;
8
9 public DictionaryDeserializer(IDeserializer<T> elemDeserializer)
10 {
11 _elemDeserializer = elemDeserializer;
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, _elemDeserializer.Deserialize(context, ref reader));
31 }
32
33 return dict;
34 }
35}
T Deserialize(MappingContext context, ref Utf8FaunaReader reader)
TokenType
Enumerates the types of tokens used in Fauna serialization.
Definition TokenType.cs:7