Fauna v10 .NET/C# Driver 1.0.1
 
Loading...
Searching...
No Matches
ISerializer.cs
Go to the documentation of this file.
2using Fauna.Mapping;
3
4namespace Fauna.Serialization;
5
10public interface ISerializer<out T> : ISerializer
11{
19}
20
24public interface ISerializer
25{
32 object? Deserialize(MappingContext ctx, ref Utf8FaunaReader reader);
33
40 void Serialize(MappingContext ctx, Utf8FaunaWriter writer, object? o);
41
46 List<FaunaType> GetSupportedTypes();
47}
48
53public abstract class BaseSerializer<T> : ISerializer<T>
54{
59 public virtual List<FaunaType> GetSupportedTypes() => new List<FaunaType>();
60
67 protected static TokenType EndTokenFor(TokenType start)
68 {
69 return start switch
70 {
71 TokenType.StartObject => TokenType.EndObject,
72 TokenType.StartArray => TokenType.EndArray,
73 TokenType.StartPage => TokenType.EndPage,
74 TokenType.StartRef => TokenType.EndRef,
75 TokenType.StartDocument => TokenType.EndDocument,
76 _ => throw new ArgumentOutOfRangeException(nameof(start), start, null)
77 };
78 }
79
85 protected string UnsupportedSerializationTypeMessage(Type type) => $"Cannot serialize `{type}` with `{GetType()}`";
86
92 protected string UnexpectedTypeDecodingMessage(FaunaType faunaType) =>
93 $"Unable to deserialize `{faunaType.GetType().Name}.{faunaType}` with `{GetType().Name}`. " +
94 $"Supported types are [{string.Join(", ", GetSupportedTypes().Select(x => $"{x.GetType().Name}.{x}"))}]";
95
97 Deserialize(ctx, ref reader);
98
105 public abstract T Deserialize(MappingContext ctx, ref Utf8FaunaReader reader);
106
107 void ISerializer.Serialize(MappingContext context, Utf8FaunaWriter writer, object? o) =>
108 Serialize(context, writer, o);
109
116 public abstract void Serialize(MappingContext context, Utf8FaunaWriter writer, object? o);
117
123 protected static SerializationException UnexpectedToken(TokenType token) =>
124 new($"Unexpected token while deserializing: {token}");
125}
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.
static TokenType EndTokenFor(TokenType start)
Gets the end token for a given start token.
virtual List< FaunaType > GetSupportedTypes()
Supported types for the serializer.
Provides functionality for writing data in a streaming manner to a buffer or a stream.
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? Deserialize(MappingContext ctx, ref Utf8FaunaReader reader)
Consumes all or some of a Utf8FaunaReader and returns an object or null.
List< FaunaType > GetSupportedTypes()
A list of types to which the ISerializer applies.
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.