Fauna .NET Driver 0.1.0-beta
 
Loading...
Searching...
No Matches
Deserializer.cs
Go to the documentation of this file.
1using Fauna.Mapping;
2using Fauna.Types;
3
4namespace Fauna.Serialization;
5
9public static class Deserializer
10{
14 public static IDeserializer<object?> Dynamic { get; } = DynamicDeserializer.Singleton;
15
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();
31
38 public static IDeserializer<T> Generate<T>(MappingContext context) where T : notnull
39 {
40 var targetType = typeof(T);
41 var deser = (IDeserializer<T>)Generate(context, targetType);
42 return deser;
43 }
44
51 public static IDeserializer Generate(MappingContext context, Type targetType)
52 {
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;
63 if (targetType == typeof(NamedDocument)) return _namedDoc;
64 if (targetType == typeof(DocumentRef)) return _docRef;
65 if (targetType == typeof(NullDocumentRef)) return _nullDocRef;
66 if (targetType == typeof(NamedDocumentRef)) return _namedDocRef;
67 if (targetType == typeof(NullNamedDocumentRef)) return _nullNamedDocRef;
68
69 if (targetType.IsGenericType && targetType.GetGenericTypeDefinition() == typeof(Dictionary<,>))
70 {
71 var argTypes = targetType.GetGenericArguments();
72 var keyType = argTypes[0];
73 var valueType = argTypes[1];
74
75 if (keyType != typeof(string))
76 throw new ArgumentException(
77 $"Unsupported Dictionary key type. Key must be of type string, but was a {keyType}");
78
79 var valueDeserializer = Generate(context, valueType);
80
81 var deserType = typeof(DictionaryDeserializer<>).MakeGenericType(new[] { valueType });
82 var deser = Activator.CreateInstance(deserType, new[] { valueDeserializer });
83
84 return (IDeserializer)deser!;
85 }
86
87 if (targetType.IsGenericType && targetType.GetGenericTypeDefinition() == typeof(List<>))
88 {
89 var elemType = targetType.GetGenericArguments()[0];
90 var elemDeserializer = Generate(context, elemType);
91
92 var deserType = typeof(ListDeserializer<>).MakeGenericType(new[] { elemType });
93 var deser = Activator.CreateInstance(deserType, new[] { elemDeserializer });
94
95 return (IDeserializer)deser!;
96 }
97
98 if (targetType.IsGenericType && targetType.GetGenericTypeDefinition() == typeof(Page<>))
99 {
100 var elemType = targetType.GetGenericArguments()[0];
101 var elemDeserializer = Generate(context, elemType);
102
103 var deserType = typeof(PageDeserializer<>).MakeGenericType(new[] { elemType });
104 var deser = Activator.CreateInstance(deserType, new[] { elemDeserializer });
105
106 return (IDeserializer)deser!;
107 }
108
109 if (targetType.IsClass && !targetType.IsGenericType)
110 {
111 var info = context.GetInfo(targetType);
112 return info.Deserializer;
113 }
114
115 throw new ArgumentException($"Unsupported deserialization target type {targetType}");
116 }
117
124 public static IDeserializer<T?> GenerateNullable<T>(MappingContext context)
125 {
126 var targetType = typeof(T);
127 var deser = (IDeserializer<T>)Generate(context, targetType);
128 return new NullableDeserializer<T>(deser);
129 }
130
137 public static IDeserializer GenerateNullable(MappingContext context, Type targetType)
138 {
139 var inner = (IDeserializer)Generate(context, targetType);
140 var deserType = typeof(NullableDeserializer<>).MakeGenericType(new[] { targetType });
141 var deser = Activator.CreateInstance(deserType, new[] { inner });
142
143 return (IDeserializer)deser!;
144 }
145}
MappingInfo GetInfo(Type ty)
Represents a document.
Definition Document.cs:7
Represents a document ref.
Definition DocumentRef.cs:7
Represents a module, a singleton object grouping related functionalities. Modules are serialized as @...
Definition Module.cs:8
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....