Skip to content

Commit

Permalink
Merge pull request #24 from aksio-insurtech:feature/type-serializer
Browse files Browse the repository at this point in the history
Adding a Type serializer
  • Loading branch information
einari authored Dec 11, 2023
2 parents d517a87 + abc1cda commit 1de7db9
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Samples/Bar.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace Samples;

public record Bar(string Something);
public record Bar(string Something, Type Type);
6 changes: 3 additions & 3 deletions Samples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
db.DropCollection("blahs");
var collection = db.GetCollection<Blah>();

var b = new Blah("Hello", 42, new Bar("aasd"));
var b = new Blah("Hello", 42, new Bar("aasd", typeof(string)));
var doc = b.ToBsonDocument();

// collection.InsertOne(new Blah("Hello", 42, "Something"));
collection.InsertOne(new Blah("Hello", 42, new Bar("aasd")));
collection.InsertOne(new Blah("Hello", 42, new Bar("aasd", typeof(string))));
collection.InsertOne(new Blah("Hello", 42, new Foo("Cat", "Horse", "Bar")));
collection.InsertOne(new Blah("Hello", 42, new Foo("Cat", "Horse", new Bar("Something"))));
collection.InsertOne(new Blah("Hello", 42, new Foo("Cat", "Horse", new Bar("Something", typeof(string)))));


// var items = collection.Find(_ => true).ToList();
Expand Down
3 changes: 1 addition & 2 deletions Source/CustomObjectDiscriminatorConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,5 @@ public Type GetActualType(IBsonReader bsonReader, Type nominalType)
}

/// <inheritdoc/>
public BsonValue GetDiscriminator(Type nominalType, Type actualType) =>
$"{actualType.FullName}, {actualType.Assembly.GetName().Name}";
public BsonValue GetDiscriminator(Type nominalType, Type actualType) => actualType.GetTypeString();
}
2 changes: 2 additions & 0 deletions Source/MongoDBDefaults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public static void Initialize(IMongoDBArtifacts? mongoDBArtifacts = default, IDe
.RegisterSerializer(new DateOnlySerializer());
BsonSerializer
.RegisterSerializer(new TimeOnlySerializer());
BsonSerializer
.RegisterSerializer(new TypeSerializer());

#pragma warning disable CS0618

Expand Down
20 changes: 20 additions & 0 deletions Source/TypeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Aksio Insurtech. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Aksio.MongoDB;

/// <summary>
/// Extension methods for <see cref="Type"/>.
/// </summary>
public static class TypeExtensions
{
/// <summary>
/// Get the type string for a <see cref="Type"/>.
/// </summary>
/// <param name="type">Type to get from.</param>
/// <returns>A type string.</returns>
public static string GetTypeString(this Type type) =>
(type.Namespace?.StartsWith("System") ?? false) ?
(type.FullName ?? type.Name) :
$"{type.FullName ?? type.Name}, {type.Assembly.GetName().Name}";
}
34 changes: 34 additions & 0 deletions Source/TypeSerializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) Aksio Insurtech. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Serializers;

namespace Aksio.MongoDB;

/// <summary>
/// Represents a serializer for <see cref="Type"/>.
/// </summary>
public class TypeSerializer : SerializerBase<Type>
{
/// <inheritdoc/>
public override Type Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
{
var bsonType = context.Reader.GetCurrentBsonType();
switch (bsonType)
{
case BsonType.String:
return Type.GetType(context.Reader.ReadString()) ?? throw new InvalidOperationException("Could not deserialize type.");

default:
throw new NotSupportedException($"Cannot deserialize a {bsonType} to a {nameof(Type)}.");
}
}

/// <inheritdoc/>
public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, Type value)
{
context.Writer.WriteString(value.GetTypeString());
}
}

0 comments on commit 1de7db9

Please sign in to comment.