Last active
January 25, 2017 13:42
-
-
Save kucheruk/8262e73a2613fc5f5a7f1d5bdbf28010 to your computer and use it in GitHub Desktop.
Сериализируем сообщения из Akka.NET Cluster Sharding в Akka.Persistence.MongoDB #2
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
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