8internal class DictionarySerializer<T> : BaseSerializer<Dictionary<string, T>>, IPartialDocumentSerializer
10 private readonly ISerializer<T> _elemSerializer;
12 public DictionarySerializer(ISerializer<T> elemSerializer)
14 _elemSerializer = elemSerializer;
17 public override List<FaunaType> GetSupportedTypes() =>
new List<FaunaType> {
FaunaType.Null, FaunaType.Object };
21 switch (reader.CurrentTokenType)
24 return DeserializeInternal(
new Dictionary<string, T>(),
TokenType.EndObject, ctx, ref reader);
26 return DeserializeInternal(
new Dictionary<string, T>(),
TokenType.EndDocument, ctx, ref reader);
29 $
"Unexpected token while deserializing into {typeof(Dictionary<string, T>)}: {reader.CurrentTokenType}");
38 writer.WriteNullValue();
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)
46 writer.WriteFieldName(key);
47 _elemSerializer.
Serialize(context, writer, value);
50 if (shouldEscape) writer.WriteEndEscapedObject();
51 else writer.WriteEndObject();
54 throw new NotImplementedException();
58 public object DeserializeDocument(
MappingContext context,
string?
id,
string? name,
Module? coll, ref Utf8FaunaReader reader)
60 var dict =
new Dictionary<string, T>();
61 if (typeof(T) == typeof(
object))
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);
68 return DeserializeInternal(dict,
TokenType.EndDocument, context, ref reader);
71 private Dictionary<string, T> DeserializeInternal(
72 Dictionary<string, T> dict,
75 ref Utf8FaunaReader reader)
78 while (reader.Read() && reader.CurrentTokenType != endToken)
80 if (reader.CurrentTokenType !=
TokenType.FieldName)
82 $
"Unexpected token while deserializing field of {typeof(Dictionary<string, T>)}: {reader.CurrentTokenType}");
84 string fieldName = reader.GetString()!;
86 dict.Add(fieldName, _elemSerializer.Deserialize(context, ref reader));
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 @...
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.
TokenType
Enumerates the types of tokens used in Fauna serialization.