10public static partial class Serializer
13 internal static readonly HashSet<string> Tags =
new()
15 "@int",
"@long",
"@double",
"@date",
"@time",
"@mod",
"@ref",
"@doc",
"@set",
"@object"
25 public static void Serialize(
MappingContext ctx, Utf8FaunaWriter w,
object? o)
27 SerializeValueInternal(ctx, w, o);
30 private static void SerializeValueInternal(
MappingContext ctx, Utf8FaunaWriter w,
object? o,
FaunaType? ty =
null)
34 if (o is
null)
throw new ArgumentNullException(nameof(o));
39 if (o is
byte or sbyte or
short or ushort or
int)
41 var int32 = Convert.ToInt32(o);
42 w.WriteIntValue(int32);
46 throw new SerializationException($
"Unsupported Int conversion. Provided value must be a byte, sbyte, short, ushort, or int but was a {o.GetType()}");
50 if (o is
byte or sbyte or
short or ushort or
int or uint or
long)
52 var int64 = Convert.ToInt64(o);
53 w.WriteLongValue(int64);
57 throw new SerializationException($
"Unsupported Long conversion. Provided value must be a byte, sbyte, short, ushort, int, uint, or long but was a {o.GetType()}");
63 case float or
double or
short or
int or long:
65 var dub = Convert.ToDouble(o);
66 w.WriteDoubleValue(dub);
70 throw new SerializationException($
"Unsupported Double conversion. Provided value must be a short, int, long, float, or double, but was a {o.GetType()}");
74 w.WriteStringValue(o.ToString() ??
string.Empty);
85 case DateTimeOffset v:
89 throw new SerializationException($
"Unsupported Date conversion. Provided value must be a DateTime, DateTimeOffset, or DateOnly but was a {o.GetType()}");
98 case DateTimeOffset v:
102 throw new SerializationException($
"Unsupported Time conversion. Provided value must be a DateTime or DateTimeOffset but was a {o.GetType()}");
108 w.WriteBooleanValue(b);
112 throw new SerializationException($
"Unsupported Boolean conversion. Provided value must be a bool but was a {o.GetType()}");
116 throw new ArgumentOutOfRangeException(nameof(ty), ty,
null);
148 w.WriteDoubleValue(v);
151 w.WriteDoubleValue(v);
154 throw new SerializationException(
"Decimals are unsupported due to potential loss of precision.");
156 w.WriteBooleanValue(v);
159 w.WriteStringValue(v);
162 w.WriteModuleValue(v);
167 case DateTimeOffset v:
174 SerializeObjectInternal(w, o, ctx);
180 private static void SerializeObjectInternal(Utf8FaunaWriter writer,
object obj,
MappingContext context)
184 case Dictionary<string, object> d:
185 SerializeIDictionaryInternal(writer, d, context);
187 case IEnumerable<object> e:
188 writer.WriteStartArray();
191 SerializeValueInternal(context, writer, o);
193 writer.WriteEndArray();
196 SerializeClassInternal(writer, obj, context);
201 private static void SerializeIDictionaryInternal<T>(Utf8FaunaWriter writer, IDictionary<string, T> d,
204 var shouldEscape = Tags.Overlaps(d.Keys);
205 if (shouldEscape) writer.WriteStartEscapedObject();
else writer.WriteStartObject();
206 foreach (var (key, value) in d)
208 writer.WriteFieldName(key);
209 Serialize(context, writer, value);
211 if (shouldEscape) writer.WriteEndEscapedObject();
else writer.WriteEndObject();
214 private static void SerializeClassInternal(Utf8FaunaWriter writer,
object obj,
MappingContext context)
216 var t = obj.GetType();
217 var mapping = context.
GetInfo(t);
218 var shouldEscape = mapping.ShouldEscapeObject;
220 if (shouldEscape) writer.WriteStartEscapedObject();
else writer.WriteStartObject();
221 foreach (var field
in mapping.Fields)
223 writer.WriteFieldName(field.Name!);
224 var v = field.Property.GetValue(obj);
225 SerializeValueInternal(context, writer, v, field.FaunaTypeHint);
227 if (shouldEscape) writer.WriteEndEscapedObject();
else writer.WriteEndObject();
Fauna.Types.Module Module
MappingInfo GetInfo(Type ty)
Represents a module, a singleton object grouping related functionalities. Modules are serialized as @...
FaunaType
Enumerates the different types of data that can be stored in Fauna.