9public static class Deserializer
14 public static IDeserializer<object?> Dynamic {
get; } = DynamicDeserializer.Singleton;
16 private static readonly CheckedDeserializer<object> _object =
new();
17 private static readonly CheckedDeserializer<string> _string =
new();
18 private static readonly CheckedDeserializer<int> _int =
new();
19 private static readonly LongDeserializer _long =
new();
20 private static readonly CheckedDeserializer<double> _double =
new();
21 private static readonly CheckedDeserializer<DateOnly> _dateOnly =
new();
22 private static readonly CheckedDeserializer<DateTime> _dateTime =
new();
23 private static readonly CheckedDeserializer<bool> _bool =
new();
24 private static readonly CheckedDeserializer<Module> _module =
new();
25 private static readonly CheckedDeserializer<Document> _doc =
new();
26 private static readonly CheckedDeserializer<NamedDocument> _namedDoc =
new();
27 private static readonly CheckedDeserializer<DocumentRef> _docRef =
new();
28 private static readonly CheckedDeserializer<NullDocumentRef> _nullDocRef =
new();
29 private static readonly CheckedDeserializer<NamedDocumentRef> _namedDocRef =
new();
30 private static readonly CheckedDeserializer<NullNamedDocumentRef> _nullNamedDocRef =
new();
38 public static IDeserializer<T> Generate<T>(
MappingContext context) where T : notnull
40 var targetType = typeof(T);
41 var deser = (IDeserializer<T>)Generate(context, targetType);
51 public static IDeserializer Generate(
MappingContext context, Type targetType)
53 if (targetType == typeof(
object))
return _object;
54 if (targetType == typeof(
string))
return _string;
55 if (targetType == typeof(
int))
return _int;
56 if (targetType == typeof(
long))
return _long;
57 if (targetType == typeof(
double))
return _double;
58 if (targetType == typeof(DateOnly))
return _dateOnly;
59 if (targetType == typeof(DateTime))
return _dateTime;
60 if (targetType == typeof(
bool))
return _bool;
61 if (targetType == typeof(
Module))
return _module;
62 if (targetType == typeof(
Document))
return _doc;
64 if (targetType == typeof(
DocumentRef))
return _docRef;
69 if (targetType.IsGenericType && targetType.GetGenericTypeDefinition() == typeof(Dictionary<,>))
71 var argTypes = targetType.GetGenericArguments();
72 var keyType = argTypes[0];
73 var valueType = argTypes[1];
75 if (keyType != typeof(
string))
76 throw new ArgumentException(
77 $
"Unsupported Dictionary key type. Key must be of type string, but was a {keyType}");
79 var valueDeserializer = Generate(context, valueType);
81 var deserType = typeof(DictionaryDeserializer<>).MakeGenericType(
new[] { valueType });
82 var deser = Activator.CreateInstance(deserType,
new[] { valueDeserializer });
84 return (IDeserializer)deser!;
87 if (targetType.IsGenericType && targetType.GetGenericTypeDefinition() == typeof(List<>))
89 var elemType = targetType.GetGenericArguments()[0];
90 var elemDeserializer = Generate(context, elemType);
92 var deserType = typeof(ListDeserializer<>).MakeGenericType(
new[] { elemType });
93 var deser = Activator.CreateInstance(deserType,
new[] { elemDeserializer });
95 return (IDeserializer)deser!;
98 if (targetType.IsGenericType && targetType.GetGenericTypeDefinition() == typeof(Page<>))
100 var elemType = targetType.GetGenericArguments()[0];
101 var elemDeserializer = Generate(context, elemType);
103 var deserType = typeof(PageDeserializer<>).MakeGenericType(
new[] { elemType });
104 var deser = Activator.CreateInstance(deserType,
new[] { elemDeserializer });
106 return (IDeserializer)deser!;
109 if (targetType.IsClass && !targetType.IsGenericType)
111 var info = context.
GetInfo(targetType);
112 return info.Deserializer;
115 throw new ArgumentException($
"Unsupported deserialization target type {targetType}");
124 public static IDeserializer<T?> GenerateNullable<T>(
MappingContext context)
126 var targetType = typeof(T);
127 var deser = (IDeserializer<T>)Generate(context, targetType);
128 return new NullableDeserializer<T>(deser);
137 public static IDeserializer GenerateNullable(
MappingContext context, Type targetType)
139 var inner = (IDeserializer)Generate(context, targetType);
140 var deserType = typeof(NullableDeserializer<>).MakeGenericType(
new[] { targetType });
141 var deser = Activator.CreateInstance(deserType,
new[] { inner });
143 return (IDeserializer)deser!;
MappingInfo GetInfo(Type ty)
Represents a document ref.
Represents a module, a singleton object grouping related functionalities. Modules are serialized as @...
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....