Example #1
0
        void Check <T>(T value, Action <T, T> assertTEqual = null)
        {
            var wrap = new SimpleClass <T>();

            wrap.Set(value);

            var sb     = new StringBuilder();
            var writer = new Writer(new PrimitiveTextWriter(new StringWriter(sb)));

            writer.Write(wrap);
            var text = sb.ToString();

            var tokens  = new TokenPrimitiveWriter();
            var _tokens = tokens.TokenStream;

            new Writer(tokens).Write(wrap);
            var reader = new Reader(new TokenPrimitiveReader(tokens.TokenStream));
            var clone  = reader.Read();

            Assert.IsType <SimpleClass <T> >(clone);
            var tclone = (SimpleClass <T>)clone;

            tclone.AssertClonningSuccess(value, assertTEqual);


            // this is a tad different, it uses BinaryWriter and an internal flag that write less TypeSurrogate info
            var clone2 = Serializer.Clone(wrap);

            clone2.AssertClonningSuccess(value, assertTEqual);
        }
Example #2
0
        /// <summary>
        /// Perform a deep clone operation by serializing then deserializing an object.
        /// </summary>
        /// <typeparam name="T">The type of the object to clone.</typeparam>
        /// <param name="instance">The instance to clone.</param>
        /// <param name="settings">The settings to use with the <see cref="ObjectWriter"/></param>
        /// <returns>A deep clone of the <paramref name="instance"/>.</returns>
        public static T Clone <T>(T instance, SerializationSettings settings = null)
        {
            var ms = new List <object>(256);

            var pw = new TokenPrimitiveWriter(ms);
            var ow = new ObjectWriter(pw)
            {
                Settings = settings,
            };

            ow.Write(instance);

            var pr = new TokenPrimitiveReader(ms);
            var or = new ObjectReader(pr);

            var result = or.Read();

            return((T)result);
        }
Example #3
0
        public void CheckPartialGeneric()
        {
            var g = new GFoo <string>();

            g.Values[0] = "hello";
            g.Values[1] = "bye";

            var g2 = Serializer.Clone(g);

            Assert.Equal(2, g2.Values.Count);
            Assert.Equal(g.Values[0], g2.Values[0]);
            Assert.Equal(g.Values[1], g2.Values[1]);

            var store = new TokenPrimitiveWriter();

            Serializer.Serialize(g, store);
            var g3  = Serializer.Deserialize <GFoo2 <string> >(new TokenPrimitiveReader(store.TokenStream));
            var g3v = (Dictionary <int, string>)g3.Values;

            Assert.Equal(2, g3v.Count);
            Assert.Equal(g.Values[0], g3v[0]);
            Assert.Equal(g.Values[1], g3v[1]);
        }
Example #4
0
        /// <summary>
        /// Perform a deep clone operation by serializing then deserializing an object.
        /// </summary>
        /// <typeparam name="T">The type of the object to clone.</typeparam>
        /// <param name="instance">The instance to clone.</param>
        /// <param name="settings">The settings to use with the <see cref="ObjectWriter"/></param>
        /// <returns>A deep clone of the <paramref name="instance"/>.</returns>
        public static T Clone <T>(T instance, SerializationSettings settings = null)
        {
            var ms = new List <object>(256);

            var pw = new TokenPrimitiveWriter(ms);
            var ow = new Writer(pw)
            {
                Settings = new SerializationSettings
                {
                    IgnoreISerializable = settings?.IgnoreISerializable ?? true,
                    IgnoreTypeConverter = settings?.IgnoreTypeConverter ?? true,
                },
            };

            ow.Write(instance);

            var pr = new TokenPrimitiveReader(ms);
            var or = new Reader(pr);

            var result = or.Read();

            return((T)result);
        }
Example #5
0
        public void CheckReshape()
        {
            var a = new CRA1()
            {
                ID    = 42,
                Tag   = "Ping",
                Child = new CRA2()
                {
                    Name = "hello",
                },
            };
            var ac = (CRA2)a.Child;

            ac.CRA1 = a;

            var stream = new TokenPrimitiveWriter();

            Serializer.Serialize(a, stream);
            var b = Serializer.Deserialize <CRB1>(new TokenPrimitiveReader(stream.TokenStream));

            Assert.Equal(a.ID, b.ID);
            Assert.Equal(b.Child.CRA1, b);
            Assert.Equal(ac.Name, b.Child.Name);
        }