1using System.Diagnostics;
8internal class ClassSerializer<T> : BaseSerializer<T>, IPartialDocumentSerializer
10 private const string IdField =
"id";
11 private const string NameField =
"name";
12 private const string CollField =
"coll";
17 Debug.Assert(info.
Type == typeof(T));
21 public override List<FaunaType> GetSupportedTypes() =>
new List<FaunaType> {
FaunaType.Document,
FaunaType.Null,
FaunaType.Object, FaunaType.Ref };
23 public object DeserializeDocument(
MappingContext context,
string?
id,
string? name,
Module? coll, ref Utf8FaunaReader reader)
25 object instance = CreateInstance();
26 if (
id is not
null) TrySetId(instance,
id);
27 if (name is not
null) TrySetName(instance, name);
28 if (coll is not
null) TrySetColl(instance, coll);
29 SetFields(instance, context, ref reader,
TokenType.EndDocument);
35 var endToken = reader.CurrentTokenType
switch
37 TokenType.StartDocument =>
TokenType.EndDocument,
38 TokenType.StartObject =>
TokenType.EndObject,
40 var other =>
throw UnexpectedToken(other),
51 while (reader.Read() && reader.CurrentTokenType !=
TokenType.EndRef)
53 if (reader.CurrentTokenType !=
TokenType.FieldName)
55 $
"Unexpected token while deserializing into Ref: {reader.CurrentTokenType}");
57 string fieldName = reader.GetString()!;
62 id = reader.GetString();
65 name = reader.GetString();
68 coll = reader.GetModule();
71 cause = reader.GetString();
74 exists = reader.GetBoolean();
79 if ((
id !=
null || name !=
null) && coll !=
null && exists)
92 object instance = CreateInstance();
93 SetFields(instance, ctx, ref reader, endToken);
99 SerializeInternal(ctx, writer, o);
102 private static void SerializeInternal(
MappingContext ctx, Utf8FaunaWriter w,
object? o)
112 bool shouldEscape = info.ShouldEscapeObject;
114 if (shouldEscape) w.WriteStartEscapedObject();
else w.WriteStartObject();
115 foreach (var field
in info.Fields)
122 object? v = field.Property.GetValue(o);
123 if (field.FieldType is
FieldType.ClientGeneratedId && v ==
null)
130 w.WriteFieldName(field.Name);
131 field.Serializer.Serialize(ctx, w, v);
133 if (shouldEscape) w.WriteEndEscapedObject();
else w.WriteEndObject();
136 private object CreateInstance() => Activator.CreateInstance(_info.Type)!;
138 private void SetFields(
object instance,
MappingContext context, ref Utf8FaunaReader reader,
TokenType endToken)
140 while (reader.Read() && reader.CurrentTokenType != endToken)
142 if (reader.CurrentTokenType !=
TokenType.FieldName)
143 throw UnexpectedToken(reader.CurrentTokenType);
145 string fieldName = reader.GetString()!;
148 if (fieldName == IdField && reader.CurrentTokenType ==
TokenType.String)
150 TrySetId(instance, reader.GetString()!);
152 else if (fieldName == NameField && reader.CurrentTokenType ==
TokenType.String)
154 TrySetName(instance, reader.GetString()!);
156 else if (_info.FieldsByName.TryGetValue(fieldName, out var field))
158 field.Property.SetValue(instance, field.Serializer.Deserialize(context, ref reader));
167 private void TrySetId(
object instance,
string id)
169 if (!_info.FieldsByName.TryGetValue(IdField, out var field))
174 if (field.Type == typeof(
long))
176 field.Property.SetValue(instance,
long.Parse(
id));
178 else if (field.Type == typeof(
string))
180 field.Property.SetValue(instance,
id);
188 private void TrySetName(
object instance,
string name)
190 if (_info.FieldsByName.TryGetValue(NameField, out var field))
192 if (field.Type == typeof(
string))
194 field.Property.SetValue(instance, name);
203 private void TrySetColl(
object instance,
Module coll)
205 if (_info.FieldsByName.TryGetValue(CollField, out var field))
207 if (field.Type == typeof(
Module))
209 field.Property.SetValue(instance, coll);
219 new($
"Unexpected token while deserializing into class {_info.Type.Name}: {tokenType}");
An exception representing a case when a document cannot be materialized because it does not exist.
Represents error that occur during serialization and deserialization of Fauna data.
A class representing the mapping context to be used during serialization and deserialization.
MappingInfo GetInfo(Type ty, string? colName=null)
Gets the MappingInfo for a given Type.
A class that encapsulates the class mapping, serialization, and deserialization of a Fauna object,...
Type Type
The associated type.
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.
new T Deserialize(MappingContext ctx, ref Utf8FaunaReader reader)
Consumes all or some of a Utf8FaunaReader and returns an instance of T .
FieldType
An enum for the field type, used with concrete implementations of BaseFieldAttribute.
FaunaType
An enum representing possible Fauna types.
TokenType
Enumerates the types of tokens used in Fauna serialization.