6internal class DynamicDeserializer : BaseDeserializer<object?>
8 public static DynamicDeserializer Singleton {
get; } =
new();
10 private readonly ListDeserializer<object?> _list;
11 private readonly PageDeserializer<object?> _page;
12 private readonly DictionaryDeserializer<object?> _dict;
14 private DynamicDeserializer()
16 _list =
new ListDeserializer<object?>(
this);
17 _page =
new PageDeserializer<object?>(
this);
18 _dict =
new DictionaryDeserializer<object?>(
this);
22 reader.CurrentTokenType
switch
24 TokenType.StartObject => _dict.Deserialize(context, ref reader),
25 TokenType.StartArray => _list.Deserialize(context, ref reader),
26 TokenType.StartPage => _page.Deserialize(context, ref reader),
27 TokenType.StartRef => DeserializeRef(context, ref reader),
28 TokenType.StartDocument => DeserializeDocument(context, ref reader),
29 TokenType.String => reader.GetString(),
30 TokenType.Int => reader.GetInt(),
31 TokenType.Long => reader.GetLong(),
32 TokenType.Double => reader.GetDouble(),
33 TokenType.Date => reader.GetDate(),
34 TokenType.Time => reader.GetTime(),
35 TokenType.True or TokenType.False => reader.GetBoolean(),
36 TokenType.Module => reader.GetModule(),
37 TokenType.Null =>
null,
38 _ =>
throw new SerializationException(
39 $
"Unexpected token while deserializing: {reader.CurrentTokenType}"),
42 private object DeserializeRef(
MappingContext context, ref Utf8FaunaReader reader)
49 var allProps =
new Dictionary<string, object?>();
51 while (reader.Read() && reader.CurrentTokenType !=
TokenType.EndRef)
53 if (reader.CurrentTokenType !=
TokenType.FieldName)
54 throw new SerializationException(
55 $
"Unexpected token while deserializing into DocumentRef: {reader.CurrentTokenType}");
57 var fieldName = reader.GetString()!;
62 id = reader.GetString();
66 name = reader.GetString();
67 allProps[
"name"] = name;
70 coll = reader.GetModule();
71 allProps[
"coll"] = coll;
74 exists = reader.GetBoolean();
75 allProps[
"exists"] = exists;
78 cause = reader.GetString();
79 allProps[
"cause"] = cause;
82 allProps[fieldName] = DynamicDeserializer.Singleton.Deserialize(context, ref reader);
87 if (
id !=
null && coll !=
null)
97 if (name !=
null && coll !=
null)
111 private object DeserializeDocument(
MappingContext context, ref Utf8FaunaReader reader)
113 var data =
new Dictionary<string, object?>();
119 while (reader.Read() && reader.CurrentTokenType !=
TokenType.EndDocument)
121 if (reader.CurrentTokenType !=
TokenType.FieldName)
122 throw new SerializationException(
123 $
"Unexpected token while deserializing into Document: {reader.CurrentTokenType}");
125 var fieldName = reader.GetString()!;
130 id = reader.GetString();
133 name = reader.GetString();
136 coll = reader.GetModule();
143 return collInfo.Deserializer.DeserializeDocument(context,
id, name, ref reader);
148 ts = reader.GetTime();
151 data[fieldName] = DynamicDeserializer.Singleton.Deserialize(context, ref reader);
156 if (
id !=
null && coll !=
null && ts !=
null)
158 if (name !=
null) data[
"name"] = name;
159 return new Document(
id, coll, ts.GetValueOrDefault(), data);
162 if (name !=
null && coll !=
null && ts !=
null)
164 return new NamedDocument(name, coll, ts.GetValueOrDefault(), data);
168 if (
id !=
null) data[
"id"] = id;
169 if (name !=
null) data[
"name"] = name;
170 if (coll !=
null) data[
"coll"] = coll;
171 if (ts !=
null) data[
"ts"] = ts;
bool TryGetCollection(string col, [NotNullWhen(true)] out MappingInfo? ret)
T Deserialize(MappingContext context, ref Utf8FaunaReader reader)
Represents a document ref.
Represents a module, a singleton object grouping related functionalities. Modules are serialized as @...
string Name
Gets the name of the module. The name is used to identify and reference the module.
Represents a document that has a "name" instead of an "id". For example, a Role document is represent...
Represents a document ref that has a "name" instead of an "id". For example, a Role document referenc...
Represents a null reference to a document, including a reason for its null state.
Represents a reference to a named document that is null, including a reason for its null state....
TokenType
Enumerates the types of tokens used in Fauna serialization.