Beispiel #1
0
        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.AreEqual(foo.Bar, actual.Bar);
            Assert.IsTrue(surrogateHasBeenInvoked);
        }
Beispiel #2
0
        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.Equal(foo.Bar, actual.Bar);
            Assert.True(surrogateHasBeenInvoked);
        }
        public void Initialize(Logger logger)
        {
            var surogates = new[]
            {
                Surrogate.Create <ActorPath, ActorPathSurrogate>(ActorPathSurrogate.From, x => x.Original()),
                Surrogate.Create <StreamPath, StreamPathSurrogate>(StreamPathSurrogate.From, x => x.Original()),
                Surrogate.Create <ActorRef, ActorRefSurrogate>(ActorRefSurrogate.From, x => x.Original(this)),
                Surrogate.Create <StreamRef, StreamRefSurrogate>(StreamRefSurrogate.From, x => x.Original(this)),
                Surrogate.Create <ClientRef, ClientRefSurrogate>(ClientRefSurrogate.From, x => x.Original(this)),
            };

            var options = new SerializerOptions(
                versionTolerance: true,
                preserveObjectReferences: true,
                surrogates: surogates);

            serializer = new Hyperion.Serializer(options);

            options = new SerializerOptions(
                versionTolerance: false,
                preserveObjectReferences: true,
                surrogates: surogates);

            copier = new Hyperion.Serializer(options);
        }
Beispiel #4
0
        public void Issue263_CanSerializeArrayOfSurrogate_WhenPreservingObjectReference()
        {
            var invoked    = new List <SurrogatedClass.ClassSurrogate>();
            var serializer = new Serializer(new SerializerOptions(
                                                preserveObjectReferences: true,
                                                surrogates: new []
            {
                Surrogate.Create <SurrogatedClass, SurrogatedClass.ClassSurrogate>(
                    to => to.ToSurrogate(),
                    from => {
                    invoked.Add(from);
                    return(from.FromSurrogate());
                }),
            }));

            var objectRef = new SurrogatedClass(5);
            var expected  = new List <SurrogatedClass> {
                objectRef, objectRef
            };

            using (var stream = new MemoryStream())
            {
                serializer.Serialize(expected, stream);
                stream.Position = 0;
                var deserialized = serializer.Deserialize <List <SurrogatedClass> >(stream);
                deserialized.Count.Should().Be(2);
                invoked.Count.Should().Be(1);
                ReferenceEquals(deserialized[0], deserialized[1]).Should().BeTrue();
            }
        }
Beispiel #5
0
        public void CanSerializeWithSurrogateInCollection()
        {
            var invoked    = new List <ISurrogate>();
            var surrogates = new[]
            {
                Surrogate.Create <IOriginal, ISurrogate>(from => from.ToSurrogate(), surrogate =>
                {
                    invoked.Add(surrogate);
                    return(surrogate.FromSurrogate());
                })
            };
            var stream     = new MemoryStream();
            var serializer = new Serializer(new SerializerOptions(surrogates: surrogates));
            var key        = new SurrogatedKey("test key");
            var foo        = new Foo
            {
                Bar = "I will be replaced!"
            };

            var dictionary = new Dictionary <SurrogatedKey, Foo>
            {
                [key] = foo
            };

            serializer.Serialize(dictionary, stream);
            stream.Position = 0;

            var actual = serializer.Deserialize <Dictionary <SurrogatedKey, Foo> > (stream);

            Assert.Equal(key, actual.Keys.First());
            Assert.Equal(foo.Bar, actual[key].Bar);
            Assert.Equal(2, invoked.Count);
        }
Beispiel #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HyperionSerializer"/> class.
        /// </summary>
        /// <param name="system">The actor system to associate with this serializer.</param>
        /// <param name="settings">Serializer settings.</param>
        public HyperionSerializer(ExtendedActorSystem system, HyperionSerializerSettings settings)
            : base(system)
        {
            Settings = settings;
            var akkaSurrogate =
                Surrogate
                .Create <ISurrogated, ISurrogate>(
                    from => from.ToSurrogate(system),
                    to => to.FromSurrogate(system));

            var provider = CreateKnownTypesProvider(system, settings.KnownTypesProvider);

            if (system != null)
            {
                var settingsSetup = system.Settings.Setup.Get <HyperionSerializerSetup>()
                                    .GetOrElse(HyperionSerializerSetup.Empty);

                settingsSetup.ApplySettings(Settings);
            }

            _serializer =
                new HySerializer(new SerializerOptions(
                                     versionTolerance: settings.VersionTolerance,
                                     preserveObjectReferences: settings.PreserveObjectReferences,
                                     surrogates: new[] { akkaSurrogate },
                                     serializerFactories: null,
                                     knownTypes: provider.GetKnownTypes(),
                                     ignoreISerializable: true,
                                     packageNameOverrides: settings.PackageNameOverrides));
        }
Beispiel #7
0
 public TestMessageFormatter() : base(
         new SerializerOptions(
             versionTolerance: false,
             preserveObjectReferences: true,
             surrogates: new[]
 {
     Surrogate.Create <ITestMessage, TestMessageSurrogate>(TestMessageSurrogate.ToSurrogate, TestMessageSurrogate.FromSurrogate),
 }
             ))
 {
 }
Beispiel #8
0
 public ClunkerApp(ResourceLoader resourceLoader, Scene initialScene)
 {
     Resources  = resourceLoader;
     Serializer = new Serializer(new SerializerOptions(true, true,
                                                       new[]
     {
         Surrogate.Create <Resource <Image <Rgba32> >, ResourceSurrogate <Image <Rgba32> > >(r => new ResourceSurrogate <Image <Rgba32> >()
         {
             Id = r.Id
         }, s => resourceLoader.LoadImage(s.Id))
     }));
     NextScene            = initialScene;
     _renderers           = new List <IRenderer>();
     WorkQueue            = new RoundRobinWorkQueue(new ThreadedWorkQueue(), new ThreadedWorkQueue(), new ThreadedWorkQueue(), new ThreadedWorkQueue(), new ThreadedWorkQueue(), new ThreadedWorkQueue());
     BestEffortFrameQueue = new DrivenWorkQueue();
 }
Beispiel #9
0
        /// <summary>
        /// Initializes a new instance of the <see cref="WireSerializer"/> class.
        /// </summary>
        /// <param name="system">The actor system to associate with this serializer.</param>
        public WireSerializer(ExtendedActorSystem system) : base(system)
        {
            var akkaSurrogate =
                Surrogate
                .Create <ISurrogated, ISurrogate>(
                    from => from.ToSurrogate(system),
                    to => to.FromSurrogate(system));

            _serializer =
                new Hyperion.Serializer(new SerializerOptions(
                                            preserveObjectReferences: true,
                                            versionTolerance: true,
                                            surrogates: new[]
            {
                akkaSurrogate
            }));
        }
Beispiel #10
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HyperionSerializer"/> class.
        /// </summary>
        /// <param name="system">The actor system to associate with this serializer.</param>
        /// <param name="settings">Serializer settings.</param>
        public HyperionSerializer(ExtendedActorSystem system, HyperionSerializerSettings settings)
            : base(system)
        {
            this.Settings = settings;
            var akkaSurrogate =
                Surrogate
                .Create <ISurrogated, ISurrogate>(
                    from => from.ToSurrogate(system),
                    to => to.FromSurrogate(system));

            var provider = CreateKnownTypesProvider(system, settings.KnownTypesProvider);

            _serializer =
                new Hyperion.Serializer(new SerializerOptions(
                                            preserveObjectReferences: settings.PreserveObjectReferences,
                                            versionTolerance: settings.VersionTolerance,
                                            surrogates: new[] { akkaSurrogate },
                                            knownTypes: provider.GetKnownTypes()));
        }
Beispiel #11
0
        public void Setup_surrogate_should_work()
        {
            var surrogated = new List <Foo>();
            var setup      = HyperionSerializerSetup.Empty
                             .WithSurrogates(new [] { Surrogate.Create <Foo, FooSurrogate>(
                                                          foo =>
                {
                    surrogated.Add(foo);
                    return(new FooSurrogate(foo.Bar + "."));
                },
                                                          surrogate => new Foo(surrogate.Bar)) });
            var settings   = setup.ApplySettings(HyperionSerializerSettings.Default);
            var serializer = new HyperionSerializer((ExtendedActorSystem)Sys, settings);

            var expected     = new Foo("bar");
            var serialized   = serializer.ToBinary(expected);
            var deserialized = serializer.FromBinary <Foo>(serialized);

            deserialized.Bar.Should().Be("bar.");
            surrogated.Count.Should().Be(1);
            surrogated[0].Should().BeEquivalentTo(expected);
        }