Fauna .NET Driver 0.1.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;
5
6namespace Fauna.Serialization;
7
11public sealed class Utf8FaunaWriter : IAsyncDisposable, IDisposable
12{
13 private readonly Utf8JsonWriter _writer;
14
19 public Utf8FaunaWriter(IBufferWriter<byte> bufferWriter)
20 {
21 _writer = new Utf8JsonWriter(bufferWriter);
22 }
23
28 public Utf8FaunaWriter(Stream stream)
29 {
30 _writer = new Utf8JsonWriter(stream);
31 }
32
36 public void Flush()
37 {
38 _writer.Flush();
39 }
40
44 public async ValueTask FlushAsync()
45 {
46 await _writer.FlushAsync();
47 }
48
52 public void Dispose()
53 {
54 _writer.Dispose();
55 }
56
60 public async ValueTask DisposeAsync()
61 {
62 await _writer.DisposeAsync();
63 }
64
68 public void WriteStartObject()
69 {
70 _writer.WriteStartObject();
71 }
72
76 public void WriteEndObject()
77 {
78 _writer.WriteEndObject();
79 }
80
85 {
86 _writer.WriteStartObject();
87 WriteFieldName("@object");
88 _writer.WriteStartObject();
89 }
90
95 {
96 _writer.WriteEndObject();
97 _writer.WriteEndObject();
98 }
99
103 public void WriteStartArray()
104 {
105 _writer.WriteStartArray();
106 }
107
111 public void WriteEndArray()
112 {
113 _writer.WriteEndArray();
114 }
115
119 public void WriteStartRef()
120 {
121 _writer.WriteStartObject();
122 WriteFieldName("@ref");
123 _writer.WriteStartObject();
124 }
125
129 public void WriteEndRef()
130 {
131 _writer.WriteEndObject();
132 _writer.WriteEndObject();
133 }
134
140 public void WriteDouble(string fieldName, decimal value)
141 {
142 WriteFieldName(fieldName);
143 WriteDoubleValue(value);
144 }
145
151 public void WriteDouble(string fieldName, double value)
152 {
153 WriteFieldName(fieldName);
154 WriteDoubleValue(value);
155 }
156
162 public void WriteInt(string fieldName, int value)
163 {
164 WriteFieldName(fieldName);
165 WriteIntValue(value);
166 }
167
173 public void WriteLong(string fieldName, long value)
174 {
175 WriteFieldName(fieldName);
176 WriteLongValue(value);
177 }
178
184 public void WriteString(string fieldName, string value)
185 {
186 WriteFieldName(fieldName);
187 WriteStringValue(value);
188 }
189
195 public void WriteDate(string fieldName, DateTime value)
196 {
197 WriteFieldName(fieldName);
198 WriteDateValue(value);
199 }
200
206 public void WriteTime(string fieldName, DateTime value)
207 {
208 WriteFieldName(fieldName);
209 WriteTimeValue(value);
210 }
211
217 public void WriteBoolean(string fieldName, bool value)
218 {
219 WriteFieldName(fieldName);
220 WriteBooleanValue(value);
221
222 }
223
228 public void WriteNull(string fieldName)
229 {
230 WriteFieldName(fieldName);
232 }
233
239 public void WriteModule(string fieldName, Module value)
240 {
241 WriteFieldName(fieldName);
242 WriteModuleValue(value);
243 }
244
249 public void WriteFieldName(string value)
250 {
251 _writer.WritePropertyName(value);
252 }
253
259 public void WriteTaggedValue(string tag, string value)
260 {
262 WriteString(tag, value);
264 }
265
270 public void WriteDoubleValue(decimal value)
271 {
272 WriteTaggedValue("@double", value.ToString(CultureInfo.InvariantCulture));
273 }
274
279 public void WriteDoubleValue(double value)
280 {
281 WriteTaggedValue("@double", value.ToString(CultureInfo.InvariantCulture));
282 }
283
288 public void WriteIntValue(int value)
289 {
290 WriteTaggedValue("@int", value.ToString());
291 }
292
297 public void WriteLongValue(long value)
298 {
299 WriteTaggedValue("@long", value.ToString());
300 }
301
306 public void WriteStringValue(string value)
307 {
308 _writer.WriteStringValue(value);
309 }
310
315 public void WriteDateValue(DateTime value)
316 {
317 var str = value.ToString("yyyy-MM-dd");
318 WriteTaggedValue("@date", str);
319 }
320
325 public void WriteDateValue(DateOnly value)
326 {
327 var str = value.ToString("yyyy-MM-dd");
328 WriteTaggedValue("@date", str);
329 }
330
335 public void WriteDateValue(DateTimeOffset value)
336 {
337 var str = value.ToString("yyyy-MM-dd");
338 WriteTaggedValue("@date", str);
339 }
340
345 public void WriteTimeValue(DateTime value)
346 {
347 var str = value.ToUniversalTime().ToString("o", CultureInfo.InvariantCulture);
348 WriteTaggedValue("@time", str);
349 }
350
355 public void WriteTimeValue(DateTimeOffset value)
356 {
357 var str = value.ToUniversalTime().ToString("o", CultureInfo.InvariantCulture);
358 WriteTaggedValue("@time", str);
359 }
360
365 public void WriteBooleanValue(bool value)
366 {
367 _writer.WriteBooleanValue(value);
368 }
369
373 public void WriteNullValue()
374 {
375 _writer.WriteNullValue();
376 }
377
382 public void WriteModuleValue(Module value)
383 {
384 WriteTaggedValue("@mod", value.Name);
385 }
386}
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