Fauna v10 .NET/C# Driver 1.0.1
 
Loading...
Searching...
No Matches
BaseRefSerializer.cs
Go to the documentation of this file.
2using Fauna.Mapping;
3using Fauna.Types;
4
6
7
8internal class RefSerializer<T> : BaseSerializer<Ref<T>> where T : notnull
9{
10 private readonly BaseRefSerializer<T> _baseRefSerializer;
11
12 public RefSerializer(ISerializer docSerializer)
13 {
14 _baseRefSerializer = new BaseRefSerializer<T>(docSerializer);
15 }
16
17 public override List<FaunaType> GetSupportedTypes() => new List<FaunaType> { FaunaType.Null, FaunaType.Ref };
18
19 public override Ref<T> Deserialize(MappingContext ctx, ref Utf8FaunaReader reader)
20 {
21 return (Ref<T>)_baseRefSerializer.Deserialize(ctx, ref reader);
22 }
23
24 public override void Serialize(MappingContext context, Utf8FaunaWriter writer, object? o)
25 {
26 _baseRefSerializer.Serialize(context, writer, o);
27 }
28}
29
30internal class NamedRefSerializer<T> : BaseSerializer<NamedRef<T>> where T : notnull
31{
32 private readonly BaseRefSerializer<T> _baseRefSerializer;
33
34 public NamedRefSerializer(ISerializer docSerializer)
35 {
36 _baseRefSerializer = new BaseRefSerializer<T>(docSerializer);
37 }
38
39 public override List<FaunaType> GetSupportedTypes() => new List<FaunaType> { FaunaType.Null, FaunaType.Ref };
40
41 public override NamedRef<T> Deserialize(MappingContext ctx, ref Utf8FaunaReader reader)
42 {
43 return (NamedRef<T>)_baseRefSerializer.Deserialize(ctx, ref reader);
44 }
45
46 public override void Serialize(MappingContext context, Utf8FaunaWriter writer, object? o)
47 {
48 _baseRefSerializer.Serialize(context, writer, o);
49 }
50}
51
52internal class BaseRefSerializer<T> : BaseSerializer<BaseRef<T>> where T : notnull
53{
54 private readonly ISerializer _docSerializer;
55
56 public BaseRefSerializer(ISerializer docSerializer)
57 {
58 _docSerializer = docSerializer;
59 }
60
61 public override List<FaunaType> GetSupportedTypes() => new List<FaunaType> { FaunaType.Null, FaunaType.Ref };
62
63 public override BaseRef<T> Deserialize(MappingContext ctx, ref Utf8FaunaReader reader)
64 {
65 return reader.CurrentTokenType switch
66 {
67 TokenType.StartRef => DeserializeRefInternal(new BaseRefBuilder<T>(), ctx, ref reader),
68 TokenType.StartDocument => DeserializeDocument(new BaseRefBuilder<T>(), ctx, ref reader),
69 _ => throw new SerializationException(
70 $"Unexpected token while deserializing into Ref: {reader.CurrentTokenType}")
71 };
72 }
73
74 public override void Serialize(MappingContext context, Utf8FaunaWriter writer, object? o)
75 {
76 switch (o)
77 {
78 case null:
79 writer.WriteNullValue();
80 break;
81 case Ref<T> r:
82 writer.WriteStartRef();
83 writer.WriteString("id", r.Id);
84 writer.WriteModule("coll", r.Collection);
85 writer.WriteEndRef();
86 break;
87 case NamedRef<T> r:
88 writer.WriteStartRef();
89 writer.WriteString("name", r.Name);
90 writer.WriteModule("coll", r.Collection);
91 writer.WriteEndRef();
92 break;
93 default:
95 }
96 }
97
98 private static BaseRef<T> DeserializeRefInternal(BaseRefBuilder<T> builder, MappingContext context, ref Utf8FaunaReader reader)
99 {
100
101 while (reader.Read() && reader.CurrentTokenType != TokenType.EndRef)
102 {
103 if (reader.CurrentTokenType != TokenType.FieldName)
104 throw new SerializationException(
105 $"Unexpected token while deserializing into NamedRef: {reader.CurrentTokenType}");
106
107 string fieldName = reader.GetString()!;
108 reader.Read();
109 switch (fieldName)
110 {
111 case "id":
112 builder.Id = reader.GetString();
113 break;
114 case "name":
115 builder.Name = reader.GetString();
116 break;
117 case "coll":
118 builder.Collection = reader.GetModule();
119 break;
120 case "cause":
121 builder.Cause = reader.GetString();
122 break;
123 case "exists":
124 builder.Exists = reader.GetBoolean();
125 break;
126 default:
127 throw new SerializationException(
128 $"Unexpected field while deserializing into Ref: {fieldName}");
129 }
130 }
131
132 return builder.Build();
133 }
134
135 public BaseRef<T> DeserializeDocument(BaseRefBuilder<T> builder, MappingContext context, ref Utf8FaunaReader reader)
136 {
137
138 while (reader.Read() && reader.CurrentTokenType != TokenType.EndDocument)
139 {
140 if (reader.CurrentTokenType != TokenType.FieldName)
141 throw new SerializationException(
142 $"Unexpected token while deserializing into NamedRef: {reader.CurrentTokenType}");
143
144 string fieldName = reader.GetString()!;
145 reader.Read();
146 switch (fieldName)
147 {
148 case "id":
149 builder.Id = reader.GetString();
150 break;
151 case "name":
152 builder.Name = reader.GetString();
153 break;
154 case "coll":
155 builder.Collection = reader.GetModule();
156
157 if (_docSerializer is not IPartialDocumentSerializer cs)
158 {
159 throw new SerializationException($"Serializer {_docSerializer.GetType().Name} must implement IPartialDocumentSerializer interface.");
160 }
161
162 // This assumes ordering on the wire. If name is not null and we're here, then it's a named document so name is a string.
163 builder.Doc = (T?)cs.DeserializeDocument(context, builder.Id, builder.Name, builder.Collection, ref reader);
164 break;
165 }
166
167 // After we deserialize into a doc, we end on the EndDocument a token and do not want to read again
168 if (reader.CurrentTokenType == TokenType.EndDocument) break;
169 }
170
171 return builder.Build();
172 }
173}
Represents error that occur during serialization and deserialization of Fauna data.
A class representing the mapping context to be used during serialization and deserialization.
An abstract class encapsulating common serialization and deserialization logic.
string UnsupportedSerializationTypeMessage(Type type)
A helper to build an unsupported serialization type exception message.
Provides functionality for writing data in a streaming manner to a buffer or a stream.
void WriteNullValue()
Writes a null value to the stream.
void WriteStartRef()
Writes the beginning of a reference object.
void WriteString(string fieldName, string value)
Writes a string value with a specific field name.
void WriteModule(string fieldName, Module value)
Writes a module value with a specific field name.
void WriteEndRef()
Writes the end of a reference object.
An abstract class representing a reference that can wrap an instance of the referenced document.
Definition BaseRef.cs:10
Represents a document ref that has a "name" instead of an "id". For example, a Role document referenc...
Definition NamedRef.cs:12
Represents a document ref.
Definition Ref.cs:11
A generic interface that defines serialize and deserialize behavior for a specific type,...
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.
Definition FaunaType.cs:7
TokenType
Enumerates the types of tokens used in Fauna serialization.
Definition TokenType.cs:7
Represents a reader that provides fast, non-cached, forward-only access to serialized data.