7internal class DynamicSerializer : BaseSerializer<object?>
9 public static DynamicSerializer Singleton {
get; } =
new();
11 private readonly ListSerializer<object?> _list;
12 private readonly PageSerializer<object?> _page;
13 private readonly DictionarySerializer<object?> _dict;
14 private readonly BaseRefSerializer<Dictionary<string, object>> _docref;
17 private DynamicSerializer()
19 _list =
new ListSerializer<object?>(
this);
20 _page =
new PageSerializer<object?>(
this);
21 _dict =
new DictionarySerializer<object?>(
this);
22 _docref =
new BaseRefSerializer<Dictionary<string, object>>(_dict);
25 public override List<FaunaType> GetSupportedTypes() =>
new List<FaunaType> {
45 reader.CurrentTokenType
switch
47 TokenType.StartObject => _dict.
Deserialize(ctx, ref reader),
48 TokenType.StartArray => _list.Deserialize(ctx, ref reader),
49 TokenType.StartPage => _page.Deserialize(ctx, ref reader),
50 TokenType.StartRef => _docref.Deserialize(ctx, ref reader),
51 TokenType.StartDocument => DeserializeDocumentInternal(ctx, ref reader),
52 TokenType.String => reader.GetString(),
53 TokenType.Int => reader.GetInt(),
54 TokenType.Long => reader.GetLong(),
55 TokenType.Double => reader.GetDouble(),
56 TokenType.Date => reader.GetDate(),
57 TokenType.Time => reader.GetTime(),
58 TokenType.True or TokenType.False => reader.GetBoolean(),
59 TokenType.Module => reader.GetModule(),
60 TokenType.Bytes => reader.GetBytes(),
61 TokenType.Null =>
null,
62 TokenType.EventSource => reader.GetEventSource(),
64 $
"Unexpected token while deserializing: {reader.CurrentTokenType}"),
67 private object DeserializeDocumentInternal(
MappingContext context, ref Utf8FaunaReader reader)
69 var builder =
new BaseRefBuilder<Dictionary<string, object>>();
70 while (reader.Read() && reader.CurrentTokenType !=
TokenType.EndDocument)
72 if (reader.CurrentTokenType !=
TokenType.FieldName)
74 $
"Unexpected token while deserializing @doc: {reader.CurrentTokenType}");
76 string fieldName = reader.GetString()!;
84 builder.Id = reader.GetString();
87 builder.Name = reader.GetString();
90 builder.Collection = reader.GetModule();
94 if (context.
TryGetCollection(builder.Collection.Name, out var info) && info.ClassSerializer is IPartialDocumentSerializer ser)
96 return new BaseRefBuilder<object>
100 Collection = builder.Collection,
101 Doc = ser.DeserializeDocument(context, builder.Id, builder.Name, builder.Collection,
106 builder.Doc = (Dictionary<string, object>?)_dict.DeserializeDocument(context, builder.Id, builder.Name, builder.Collection, ref reader);
111 if (reader.CurrentTokenType ==
TokenType.EndDocument)
break;
114 return builder.Build();
132 var ser = Serializer.Generate(ctx, o.GetType());
Represents error that occur during serialization and deserialization of Fauna data.
A class representing the mapping context to be used during serialization and deserialization.
bool TryGetCollection(string col, [NotNullWhen(true)] out MappingInfo? ret)
Gets the MappingInfo for a given collection name.
void Serialize(MappingContext ctx, Utf8FaunaWriter writer, object? o)
Serializes the provided object into the Utf8FaunaWriter.
new T Deserialize(MappingContext ctx, ref Utf8FaunaReader reader)
Consumes all or some of a Utf8FaunaReader and returns an instance of T .
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.