Skip to content

Instantly share code, notes, and snippets.

@kucheruk
Last active January 25, 2017 13:42
Show Gist options
  • Save kucheruk/8262e73a2613fc5f5a7f1d5bdbf28010 to your computer and use it in GitHub Desktop.
Save kucheruk/8262e73a2613fc5f5a7f1d5bdbf28010 to your computer and use it in GitHub Desktop.
Сериализируем сообщения из Akka.NET Cluster Sharding в Akka.Persistence.MongoDB #2
internal class ClusterShardingMongoSerializer : IBsonSerializer
{
private const string ManifestField = "manifest";
private const string BodyField = "body";
private readonly ClusterShardingMessageSerializer _serializer;
public ClusterShardingMongoSerializer(ExtendedActorSystem system)
{
_serializer = new ClusterShardingMessageSerializer(system);
}
public object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
{
var r = context.Reader;
r.ReadStartDocument();
r.FindElement(ManifestField);
var manifest = r.ReadString();
r.FindElement(BodyField);
var bytes = r.ReadBytes();
var ret = _serializer.FromBinary(bytes, manifest);
r.ReadEndDocument();
return ret;
}
public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value)
{
var w = context.Writer;
w.WriteStartDocument();
w.WriteName(ManifestField);
var manifest = _serializer.Manifest(value);
w.WriteString(manifest);
w.WriteName(BodyField);
w.WriteBytes(_serializer.ToBinary(value));
w.WriteEndDocument();
}
public Type ValueType => typeof(object);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment