Fauna v10 .NET/C# Driver 0.2.0-beta
 
Loading...
Searching...
No Matches
Utf8FaunaWriter.cs
Go to the documentation of this file.
1using System.Buffers;
2using System.Globalization;
3using System.Text.Json;
4using Fauna.Types;
5using Stream = System.IO.Stream;
6
7namespace Fauna.Serialization;
8
12public sealed class Utf8FaunaWriter : IAsyncDisposable, IDisposable
13{
14 private readonly Utf8JsonWriter _writer;
15
20 public Utf8FaunaWriter(IBufferWriter<byte> bufferWriter)
21 {
22 _writer = new Utf8JsonWriter(bufferWriter);
23 }
24
29 public Utf8FaunaWriter(Stream stream)
30 {
31 _writer = new Utf8JsonWriter(stream);
32 }
33
37 public void Flush()
38 {
39 _writer.Flush();
40 }
41
45 public async ValueTask FlushAsync()
46 {
47 await _writer.FlushAsync();
48 }
49
53 public void Dispose()
54 {
55 _writer.Dispose();
56 }
57
61 public async ValueTask DisposeAsync()
62 {
63 await _writer.DisposeAsync();
64 }
65
69 public void WriteStartObject()
70 {
71 _writer.WriteStartObject();
72 }
73
77 public void WriteEndObject()
78 {
79 _writer.WriteEndObject();
80 }
81
86 {
87 _writer.WriteStartObject();
88 WriteFieldName("@object");
89 _writer.WriteStartObject();
90 }
91
96 {
97 _writer.WriteEndObject();
98 _writer.WriteEndObject();
99 }
100
104 public void WriteStartArray()
105 {
106 _writer.WriteStartArray();
107 }
108
112 public void WriteEndArray()
113 {
114 _writer.WriteEndArray();
115 }
116
120 public void WriteStartRef()
121 {
122 _writer.WriteStartObject();
123 WriteFieldName("@ref");
124 _writer.WriteStartObject();
125 }
126
130 public void WriteEndRef()
131 {
132 _writer.WriteEndObject();
133 _writer.WriteEndObject();
134 }
135
141 public void WriteDouble(string fieldName, decimal value)
142 {
143 WriteFieldName(fieldName);
144 WriteDoubleValue(value);
145 }
146
152 public void WriteDouble(string fieldName, double value)
153 {
154 WriteFieldName(fieldName);
155 WriteDoubleValue(value);
156 }
157
163 public void WriteInt(string fieldName, int value)
164 {
165 WriteFieldName(fieldName);
166 WriteIntValue(value);
167 }
168
174 public void WriteLong(string fieldName, long value)
175 {
176 WriteFieldName(fieldName);
177 WriteLongValue(value);
178 }
179
185 public void WriteString(string fieldName, string value)
186 {
187 WriteFieldName(fieldName);
188 WriteStringValue(value);
189 }
190
196 public void WriteDate(string fieldName, DateTime value)
197 {
198 WriteFieldName(fieldName);
199 WriteDateValue(value);
200 }
201
207 public void WriteTime(string fieldName, DateTime value)
208 {
209 WriteFieldName(fieldName);
210 WriteTimeValue(value);
211 }
212
218 public void WriteBoolean(string fieldName, bool value)
219 {
220 WriteFieldName(fieldName);
221 WriteBooleanValue(value);
222
223 }
224
229 public void WriteNull(string fieldName)
230 {
231 WriteFieldName(fieldName);
233 }
234
240 public void WriteModule(string fieldName, Module value)
241 {
242 WriteFieldName(fieldName);
243 WriteModuleValue(value);
244 }
245
250 public void WriteFieldName(string value)
251 {
252 _writer.WritePropertyName(value);
253 }
254
260 public void WriteTaggedValue(string tag, string value)
261 {
263 WriteString(tag, value);
265 }
266
271 public void WriteDoubleValue(decimal value)
272 {
273 WriteTaggedValue("@double", value.ToString(CultureInfo.InvariantCulture));
274 }
275
280 public void WriteDoubleValue(double value)
281 {
282 WriteTaggedValue("@double", value.ToString(CultureInfo.InvariantCulture));
283 }
284
289 public void WriteIntValue(int value)
290 {
291 WriteTaggedValue("@int", value.ToString());
292 }
293
298 public void WriteLongValue(long value)
299 {
300 WriteTaggedValue("@long", value.ToString());
301 }
302
307 public void WriteStringValue(string value)
308 {
309 _writer.WriteStringValue(value);
310 }
311
316 public void WriteDateValue(DateTime value)
317 {
318 var str = value.ToString("yyyy-MM-dd");
319 WriteTaggedValue("@date", str);
320 }
321
326 public void WriteDateValue(DateOnly value)
327 {
328 var str = value.ToString("yyyy-MM-dd");
329 WriteTaggedValue("@date", str);
330 }
331
336 public void WriteDateValue(DateTimeOffset value)
337 {
338 var str = value.ToString("yyyy-MM-dd");
339 WriteTaggedValue("@date", str);
340 }
341
346 public void WriteTimeValue(DateTime value)
347 {
348 var str = value.ToUniversalTime().ToString("o", CultureInfo.InvariantCulture);
349 WriteTaggedValue("@time", str);
350 }
351
356 public void WriteTimeValue(DateTimeOffset value)
357 {
358 var str = value.ToUniversalTime().ToString("o", CultureInfo.InvariantCulture);
359 WriteTaggedValue("@time", str);
360 }
361
366 public void WriteBooleanValue(bool value)
367 {
368 _writer.WriteBooleanValue(value);
369 }
370
374 public void WriteNullValue()
375 {
376 _writer.WriteNullValue();
377 }
378
383 public void WriteModuleValue(Module value)
384 {
385 WriteTaggedValue("@mod", value.Name);
386 }
387}
Provides functionality for writing data in a streaming manner to a buffer or a stream.
void WriteNull(string fieldName)
Writes a null value with a specific field name.
void WriteNullValue()
Writes a null value to the stream.
void WriteEndEscapedObject()
Writes the end of a specially tagged object.
void WriteDateValue(DateTimeOffset value)
Writes a date value as a tagged element.
void WriteIntValue(int value)
Writes an integer value as a tagged element.
void WriteTaggedValue(string tag, string value)
Writes a tagged value in an object.
Utf8FaunaWriter(Stream stream)
Initializes a new instance of the Utf8FaunaWriter class with a specified stream.
void Dispose()
Disposes the underlying writer.
void WriteStartRef()
Writes the beginning of a reference object.
void WriteBooleanValue(bool value)
Writes a boolean value to the stream.
void WriteDate(string fieldName, DateTime value)
Writes a date value with a specific field name.
void WriteDateValue(DateOnly value)
Writes a date value as a tagged element.
void WriteFieldName(string value)
Writes a field name for the next value.
void WriteDouble(string fieldName, decimal value)
Writes a double value with a specific field name.
void WriteLong(string fieldName, long value)
Writes a long integer value with a specific field name.
void WriteString(string fieldName, string value)
Writes a string value with a specific field name.
void WriteDoubleValue(decimal value)
Writes a double value as a tagged element.
async ValueTask FlushAsync()
Asynchronously flushes the written data to the underlying buffer or stream.
void WriteTime(string fieldName, DateTime value)
Writes a time value with a specific field name.
void WriteStartArray()
Writes the beginning of an array.
void WriteEndArray()
Writes the end of an array.
void WriteLongValue(long value)
Writes a long integer value as a tagged element.
void WriteStartEscapedObject()
Writes the beginning of a specially tagged object.
void WriteDateValue(DateTime value)
Writes a date value as a tagged element.
void WriteDoubleValue(double value)
Writes a double value as a tagged element.
void WriteTimeValue(DateTime value)
Writes a date value as a tagged element.
void WriteModule(string fieldName, Module value)
Writes a module value with a specific field name.
void WriteStringValue(string value)
Writes a string value as a tagged element.
void WriteBoolean(string fieldName, bool value)
Writes a boolean value with a specific field name.
void WriteDouble(string fieldName, double value)
Writes a double value with a specific field name.
void WriteEndObject()
Writes the end of an object.
void WriteModuleValue(Module value)
Writes a module value as a tagged element.
void Flush()
Flushes the written data to the underlying buffer or stream.
async ValueTask DisposeAsync()
Asynchronously disposes the underlying writer.
void WriteInt(string fieldName, int value)
Writes an integer value with a specific field name.
Utf8FaunaWriter(IBufferWriter< byte > bufferWriter)
Initializes a new instance of the Utf8FaunaWriter class with a specified buffer writer.
void WriteTimeValue(DateTimeOffset value)
Writes a date value as a tagged element.
void WriteEndRef()
Writes the end of a reference object.
void WriteStartObject()
Writes the beginning of an object.
Represents a module, a singleton object grouping related functionalities. Modules are serialized as @...
Definition Module.cs:8
string Name
Gets the name of the module. The name is used to identify and reference the module.
Definition Module.cs:12
Represents a Fauna stream token.
Definition Stream.cs:7
Stream(string token)
Definition Stream.cs:8