public static FooSurrogate FromFoo(Foo foo) { return new FooSurrogate { Bar = foo.Bar }; }
public void CanSerializeWithSurrogate() { var surrogateHasBeenInvoked = false; var surrogates = new[] { Surrogate.Create<Foo, FooSurrogate>(FooSurrogate.FromFoo, surrogate => { surrogateHasBeenInvoked = true; return surrogate.Restore(); }) }; var stream = new MemoryStream(); var serializer = new Serializer(new SerializerOptions(surrogates: surrogates)); var foo = new Foo { Bar = "I will be replaced!" }; serializer.Serialize(foo, stream); stream.Position = 0; var actual = serializer.Deserialize<Foo>(stream); Assert.AreEqual(foo.Bar, actual.Bar); Assert.IsTrue(surrogateHasBeenInvoked); }
public void CanSerializeWithInterfaceSurrogate() { var surrogateHasBeenInvoked = false; var surrogates = new[] { Surrogate.Create<IOriginal, ISurrogate>(from => from.ToSurrogate(), surrogate => { surrogateHasBeenInvoked = true; return surrogate.FromSurrogate(); }) }; var stream = new MemoryStream(); var serializer = new Serializer(new SerializerOptions(surrogates: surrogates)); var foo = new Foo { Bar = "I will be replaced!" }; serializer.Serialize(foo, stream); stream.Position = 0; var actual = serializer.Deserialize<Foo>(stream); Assert.Equal(foo.Bar, actual.Bar); Assert.True(surrogateHasBeenInvoked); }