Fauna v10 .NET/C# Driver 0.3.0-beta
 
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 Ref<T> Deserialize(MappingContext context, ref Utf8FaunaReader reader)
18 {
19 return (Ref<T>)_baseRefSerializer.Deserialize(context, ref reader);
20 }
21
22 public override void Serialize(MappingContext context, Utf8FaunaWriter writer, object? o)
23 {
24 _baseRefSerializer.Serialize(context, writer, o);
25 }
26}
27
28internal class NamedRefSerializer<T> : BaseSerializer<NamedRef<T>> where T : notnull
29{
30 private readonly BaseRefSerializer<T> _baseRefSerializer;
31
32 public NamedRefSerializer(ISerializer docSerializer)
33 {
34 _baseRefSerializer = new BaseRefSerializer<T>(docSerializer);
35 }
36
37 public override NamedRef<T> Deserialize(MappingContext context, ref Utf8FaunaReader reader)
38 {
39 return (NamedRef<T>)_baseRefSerializer.Deserialize(context, ref reader);
40 }
41
42 public override void Serialize(MappingContext context, Utf8FaunaWriter writer, object? o)
43 {
44 _baseRefSerializer.Serialize(context, writer, o);
45 }
46}
47
48internal class BaseRefSerializer<T> : BaseSerializer<BaseRef<T>> where T : notnull
49{
50 private readonly ISerializer _docSerializer;
51
52 public BaseRefSerializer(ISerializer docSerializer)
53 {
54 _docSerializer = docSerializer;
55 }
56
57 public override BaseRef<T> Deserialize(MappingContext context, ref Utf8FaunaReader reader)
58 {
59 return reader.CurrentTokenType switch
60 {
61 TokenType.StartRef => DeserializeRefInternal(new BaseRefBuilder<T>(), context, ref reader),
62 TokenType.StartDocument => DeserializeDocument(new BaseRefBuilder<T>(), context, ref reader),
63 _ => throw new SerializationException(
64 $"Unexpected token while deserializing into Ref: {reader.CurrentTokenType}")
65 };
66 }
67
68 public override void Serialize(MappingContext context, Utf8FaunaWriter writer, object? o)
69 {
70 switch (o)
71 {
72 case null:
73 writer.WriteNullValue();
74 break;
75 case Ref<T> r:
76 writer.WriteStartRef();
77 writer.WriteString("id", r.Id);
78 writer.WriteModule("coll", r.Collection);
79 writer.WriteEndRef();
80 break;
81 case NamedRef<T> r:
82 writer.WriteStartRef();
83 writer.WriteString("name", r.Name);
84 writer.WriteModule("coll", r.Collection);
85 writer.WriteEndRef();
86 break;
87 default:
89 }
90 }
91
92 private static BaseRef<T> DeserializeRefInternal(BaseRefBuilder<T> builder, MappingContext context, ref Utf8FaunaReader reader)
93 {
94
95 while (reader.Read() && reader.CurrentTokenType != TokenType.EndRef)
96 {
97 if (reader.CurrentTokenType != TokenType.FieldName)
98 throw new SerializationException(
99 $"Unexpected token while deserializing into NamedRef: {reader.CurrentTokenType}");
100
101 string fieldName = reader.GetString()!;
102 reader.Read();
103 switch (fieldName)
104 {
105 case "id":
106 builder.Id = reader.GetString();
107 break;
108 case "name":
109 builder.Name = reader.GetString();
110 break;
111 case "coll":
112 builder.Collection = reader.GetModule();
113 break;
114 case "cause":
115 builder.Cause = reader.GetString();
116 break;
117 case "exists":
118 builder.Exists = reader.GetBoolean();
119 break;
120 default:
121 throw new SerializationException(
122 $"Unexpected field while deserializing into Ref: {fieldName}");
123 }
124 }
125
126 return builder.Build();
127 }
128
129 public BaseRef<T> DeserializeDocument(BaseRefBuilder<T> builder, MappingContext context, ref Utf8FaunaReader reader)
130 {
131
132 while (reader.Read() && reader.CurrentTokenType != TokenType.EndDocument)
133 {
134 if (reader.CurrentTokenType != TokenType.FieldName)
135 throw new SerializationException(
136 $"Unexpected token while deserializing into NamedRef: {reader.CurrentTokenType}");
137
138 string fieldName = reader.GetString()!;
139 reader.Read();
140 switch (fieldName)
141 {
142 case "id":
143 builder.Id = reader.GetString();
144 break;
145 case "name":
146 builder.Name = reader.GetString();
147 break;
148 case "coll":
149 builder.Collection = reader.GetModule();
150
151 if (_docSerializer is not IPartialDocumentSerializer cs)
152 {
153 throw new SerializationException($"Serializer {_docSerializer.GetType().Name} must implement IPartialDocumentSerializer interface.");
154 }
155
156 // 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.
157 builder.Doc = (T?)cs.DeserializeDocument(context, builder.Id, builder.Name, builder.Collection, ref reader);
158 break;
159 }
160
161 // After we deserialize into a doc, we end on the EndDocument a token and do not want to read again
162 if (reader.CurrentTokenType == TokenType.EndDocument) break;
163 }
164
165 return builder.Build();
166 }
167}
Represents error that occur during serialization and deserialization of Fauna data.
A class representing the mapping context to be used during serialization and deserialization.
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.
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
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.