Fauna v10 .NET/C# Driver 1.0.1
 
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 WriteBytes(string fieldName, byte[] value)
186 {
187 WriteFieldName(fieldName);
188 WriteBytesValue(value);
189 }
190
196 public void WriteString(string fieldName, string value)
197 {
198 WriteFieldName(fieldName);
199 WriteStringValue(value);
200 }
201
207 public void WriteDate(string fieldName, DateTime value)
208 {
209 WriteFieldName(fieldName);
210 WriteDateValue(value);
211 }
212
218 public void WriteTime(string fieldName, DateTime value)
219 {
220 WriteFieldName(fieldName);
221 WriteTimeValue(value);
222 }
223
229 public void WriteBoolean(string fieldName, bool value)
230 {
231 WriteFieldName(fieldName);
232 WriteBooleanValue(value);
233
234 }
235
240 public void WriteNull(string fieldName)
241 {
242 WriteFieldName(fieldName);
244 }
245
251 public void WriteModule(string fieldName, Module value)
252 {
253 WriteFieldName(fieldName);
254 WriteModuleValue(value);
255 }
256
261 public void WriteFieldName(string value)
262 {
263 _writer.WritePropertyName(value);
264 }
265
271 public void WriteTaggedValue(string tag, string value)
272 {
274 WriteString(tag, value);
276 }
277
282 public void WriteDoubleValue(decimal value)
283 {
284 WriteTaggedValue("@double", value.ToString(CultureInfo.InvariantCulture));
285 }
286
291 public void WriteDoubleValue(double value)
292 {
293 WriteTaggedValue("@double", value.ToString(CultureInfo.InvariantCulture));
294 }
295
300 public void WriteIntValue(int value)
301 {
302 WriteTaggedValue("@int", value.ToString());
303 }
304
309 public void WriteBytesValue(byte[] value)
310 {
311 WriteTaggedValue("@bytes", Convert.ToBase64String(value));
312 }
313
318 public void WriteLongValue(long value)
319 {
320 WriteTaggedValue("@long", value.ToString());
321 }
322
327 public void WriteStringValue(string value)
328 {
329 _writer.WriteStringValue(value);
330 }
331
336 public void WriteDateValue(DateTime value)
337 {
338 var str = value.ToString("yyyy-MM-dd");
339 WriteTaggedValue("@date", str);
340 }
341
346 public void WriteDateValue(DateOnly value)
347 {
348 var str = value.ToString("yyyy-MM-dd");
349 WriteTaggedValue("@date", str);
350 }
351
356 public void WriteDateValue(DateTimeOffset value)
357 {
358 var str = value.ToString("yyyy-MM-dd");
359 WriteTaggedValue("@date", str);
360 }
361
366 public void WriteTimeValue(DateTime value)
367 {
368 var str = value.ToUniversalTime().ToString("o", CultureInfo.InvariantCulture);
369 WriteTaggedValue("@time", str);
370 }
371
376 public void WriteTimeValue(DateTimeOffset value)
377 {
378 var str = value.ToUniversalTime().ToString("o", CultureInfo.InvariantCulture);
379 WriteTaggedValue("@time", str);
380 }
381
386 public void WriteBooleanValue(bool value)
387 {
388 _writer.WriteBooleanValue(value);
389 }
390
394 public void WriteNullValue()
395 {
396 _writer.WriteNullValue();
397 }
398
403 public void WriteModuleValue(Module value)
404 {
405 WriteTaggedValue("@mod", value.Name);
406 }
407}
System.IO.Stream Stream
Definition Client.cs:8
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 WriteBytesValue(byte[] value)
Writes a byte array value as a tagged element.
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 WriteBytes(string fieldName, byte[] value)
Writes a byte array value with a specific field name.
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