public void CanSerializeInterfaceField()
        {
            var b = new Bar
            {
                Foo = new Foo()
                {
                    A = 123,
                    B = "hello"
                }
            };
            var bytes = MessagePackSerializer.Serialize(b, DefaultResolver.Instance);
            var json  = MessagePackSerializer.ToJson(bytes);

            Assert.Equal(@"{""Foo"":[0,[123,""hello""]]}", json);
            var copy = MessagePackSerializer.DeepCopy(b, DefaultResolver.Instance);

            Assert.NotNull(copy);
            Assert.IsAssignableFrom <IFoo>(copy.Foo);
            Assert.Equal(b.Foo.A, copy.Foo.A);
            Assert.Equal(((Foo)b.Foo).B, ((Foo)copy.Foo).B);
            bytes = MessagePackSerializer.Typeless.Serialize(b);
            json  = MessagePackSerializer.ToJson(bytes);
            Assert.Equal(@"{""$type"":""MessagePack.Tests.ExtensionTests.Bar, MessagePack.Tests"",""Foo"":[0,[123,""hello""]]}", json);
            copy = (Bar)MessagePackSerializer.Typeless.DeepCopy(b);
            Assert.NotNull(copy);
            Assert.IsAssignableFrom <IFoo>(copy.Foo);
            Assert.Equal(b.Foo.A, copy.Foo.A);
            Assert.Equal(((Foo)b.Foo).B, ((Foo)copy.Foo).B);
        }
        public void CanSerializeObjectField()
        {
            var b = new Bar1
            {
                Foo = new Foo()
                {
                    A = 123,
                    B = "hello"
                }
            };
            var bytes = MessagePackSerializer.Serialize(b, DefaultResolver.Instance);
            var json  = MessagePackSerializer.ToJson(bytes);

            Assert.Equal(@"{""Foo"":[""MessagePack.Tests.ExtensionTests.Foo, MessagePack.Tests"",123,""hello""]}", json);

            var copy = MessagePackSerializer.DeepCopy(b, DefaultResolver.Instance);

            Assert.NotNull(copy);
            Assert.IsType <Foo>(copy.Foo);
            Assert.NotNull(copy);
            Assert.IsType <Foo>(copy.Foo);
            var foo = (Foo)copy.Foo;

            Assert.Equal(123, foo.A);
            Assert.Equal("hello", foo.B);

            bytes = MessagePackSerializer.Serialize(b, ContractlessStandardResolverAllowPrivate.Instance);
            copy  = MessagePackSerializer.Deserialize <Bar1>(bytes, ContractlessStandardResolverAllowPrivate.Instance);
            Assert.NotNull(copy);
            Assert.IsType <Foo>(copy.Foo);
            Assert.NotNull(copy);
            Assert.IsType <Foo>(copy.Foo);
            foo = (Foo)copy.Foo;
            Assert.Equal(123, foo.A);
            Assert.Equal("hello", foo.B);


            copy = MessagePackSerializer.DeepCopy(b, DefaultResolver.Instance);
            Assert.NotNull(copy);
            Assert.IsType <Foo>(copy.Foo);
            Assert.NotNull(copy);
            Assert.IsType <Foo>(copy.Foo);
            foo = (Foo)copy.Foo;
            Assert.Equal(123, foo.A);
            Assert.Equal("hello", foo.B);

            bytes = MessagePackSerializer.Typeless.Serialize(b);
            json  = MessagePackSerializer.ToJson(bytes);
            Assert.Equal(@"{""$type"":""MessagePack.Tests.ExtensionTests.Bar1, MessagePack.Tests"",""Foo"":[""MessagePack.Tests.ExtensionTests.Foo, MessagePack.Tests"",123,""hello""]}", json);
            copy = (Bar1)MessagePackSerializer.Typeless.DeepCopy(b);
            Assert.NotNull(copy);
            Assert.IsType <Foo>(copy.Foo);
            foo = (Foo)copy.Foo;
            Assert.Equal(123, foo.A);
            Assert.Equal("hello", foo.B);
        }
        public void CanSerializeCultureInfo()
        {
            var culture = CultureInfo.InvariantCulture;
            var bytes   = MessagePackSerializer.Serialize(culture);

            Assert.Equal(culture, MessagePackSerializer.Deserialize <CultureInfo>(bytes));
            var copy = (CultureInfo)MessagePackSerializer.DeepCopy <object>(culture);

            Assert.Equal(culture, copy);
        }
        public void CanSerializeType()
        {
            var fooType = typeof(FooClass);
            var bytes   = MessagePackSerializer.Serialize(fooType);

            Assert.Equal(fooType, MessagePackSerializer.Deserialize <Type>(bytes));
            var copy = (Type)MessagePackSerializer.DeepCopy <object>(fooType);

            Assert.Equal(fooType, copy);
        }
Exemple #5
0
        public void CanSerializePropertyInfo()
        {
            var propertyInfo = typeof(Dummy).GetProperty("TestProperty");
            var bytes        = MessagePackSerializer.Serialize(propertyInfo);

            Assert.Equal(propertyInfo, MessagePackSerializer.Deserialize <PropertyInfo>(bytes));
            var copy = MessagePackSerializer.DeepCopy(propertyInfo);

            Assert.Equal(propertyInfo, copy);
        }
Exemple #6
0
        public void CanSerializeFieldInfo()
        {
            var fieldInfo = typeof(Dummy).GetField("TestField");
            var bytes     = MessagePackSerializer.Serialize(fieldInfo);

            Assert.Equal(fieldInfo, MessagePackSerializer.Deserialize <FieldInfo>(bytes));
            var copy = MessagePackSerializer.DeepCopy(fieldInfo);

            Assert.Equal(fieldInfo, copy);
        }
        public void CanSerializeIPAddress()
        {
            var ip    = IPAddress.Parse("192.168.0.108");
            var bytes = MessagePackSerializer.Serialize(ip);

            Assert.Equal(ip, MessagePackSerializer.Deserialize <IPAddress>(bytes));
            var copy = (IPAddress)MessagePackSerializer.DeepCopy <object>(ip);

            Assert.Equal(ip, copy);

            var endPoint = new IPEndPoint(ip, 8080);

            bytes = MessagePackSerializer.Serialize(endPoint);
            Assert.Equal(endPoint, MessagePackSerializer.Deserialize <IPEndPoint>(bytes));
            var copy1 = (IPEndPoint)MessagePackSerializer.DeepCopy <object>(endPoint);

            Assert.Equal(endPoint, copy1);
        }
        public void CanSerializeMemberMethod()
        {
            Func <string> a     = 123.ToString;
            var           bytes = MessagePackSerializer.Serialize(a);
            var           json  = MessagePackSerializer.ToJson(bytes);
            var           res   = MessagePackSerializer.Deserialize <Func <string> >(bytes);

            Assert.NotNull(res);
            var actual = res();

            Assert.Equal("123", actual);

            res = MessagePackSerializer.DeepCopy(a);
            Assert.NotNull(res);
            actual = res();
            Assert.Equal("123", actual);

            res = (Func <string>)MessagePackSerializer.Typeless.DeepCopy(a);
            Assert.NotNull(res);
            actual = res();
            Assert.Equal("123", actual);
        }