Fauna v10 .NET/C# Driver 1.0.1
 
Loading...
Searching...
No Matches
DictionarySerializer.cs
Go to the documentation of this file.
1using System.Text.Json;
3using Fauna.Mapping;
4using Fauna.Types;
5
6namespace Fauna.Serialization;
7
8internal class DictionarySerializer<T> : BaseSerializer<Dictionary<string, T>>, IPartialDocumentSerializer
9{
10 private readonly ISerializer<T> _elemSerializer;
11
12 public DictionarySerializer(ISerializer<T> elemSerializer)
13 {
14 _elemSerializer = elemSerializer;
15 }
16
17 public override List<FaunaType> GetSupportedTypes() => new List<FaunaType> { FaunaType.Null, FaunaType.Object };
18
19 public override Dictionary<string, T> Deserialize(MappingContext ctx, ref Utf8FaunaReader reader)
20 {
21 switch (reader.CurrentTokenType)
22 {
23 case TokenType.StartObject:
24 return DeserializeInternal(new Dictionary<string, T>(), TokenType.EndObject, ctx, ref reader);
25 case TokenType.StartDocument:
26 return DeserializeInternal(new Dictionary<string, T>(), TokenType.EndDocument, ctx, ref reader);
27 default:
28 throw new SerializationException(
29 $"Unexpected token while deserializing into {typeof(Dictionary<string, T>)}: {reader.CurrentTokenType}");
30 }
31 }
32
33 public override void Serialize(MappingContext context, Utf8FaunaWriter writer, object? o)
34 {
35 switch (o)
36 {
37 case null:
38 writer.WriteNullValue();
39 break;
40 case Dictionary<string, T> d:
41 bool shouldEscape = Serializer.Tags.Overlaps(d.Keys);
42 if (shouldEscape) writer.WriteStartEscapedObject();
43 else writer.WriteStartObject();
44 foreach (var (key, value) in d)
45 {
46 writer.WriteFieldName(key);
47 _elemSerializer.Serialize(context, writer, value);
48 }
49
50 if (shouldEscape) writer.WriteEndEscapedObject();
51 else writer.WriteEndObject();
52 break;
53 default:
54 throw new NotImplementedException();
55 }
56 }
57
58 public object DeserializeDocument(MappingContext context, string? id, string? name, Module? coll, ref Utf8FaunaReader reader)
59 {
60 var dict = new Dictionary<string, T>();
61 if (typeof(T) == typeof(object))
62 {
63 if (id != null) dict.Add("id", (T)(object)id);
64 if (name != null) dict.Add("name", (T)(object)name);
65 if (coll != null) dict.Add("coll", (T)(object)coll);
66 }
67
68 return DeserializeInternal(dict, TokenType.EndDocument, context, ref reader);
69 }
70
71 private Dictionary<string, T> DeserializeInternal(
72 Dictionary<string, T> dict,
73 TokenType endToken,
74 MappingContext context,
75 ref Utf8FaunaReader reader)
76 {
77
78 while (reader.Read() && reader.CurrentTokenType != endToken)
79 {
80 if (reader.CurrentTokenType != TokenType.FieldName)
81 throw new SerializationException(
82 $"Unexpected token while deserializing field of {typeof(Dictionary<string, T>)}: {reader.CurrentTokenType}");
83
84 string fieldName = reader.GetString()!;
85 reader.Read();
86 dict.Add(fieldName, _elemSerializer.Deserialize(context, ref reader));
87 }
88
89 return dict;
90 }
91}
Represents error that occur during serialization and deserialization of Fauna data.
A class representing the mapping context to be used during serialization and deserialization.
Represents a module, a singleton object grouping related functionalities. Modules are serialized as @...
Definition Module.cs:8
void Serialize(MappingContext ctx, Utf8FaunaWriter writer, object? o)
Serializes the provided object into the Utf8FaunaWriter.
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