-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from aksio-insurtech:feature/type-serializer
Adding a Type serializer
- Loading branch information
Showing
6 changed files
with
61 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |