public void TestUnpackObject()
        {
            // Just verify it is OK ... the method always should behave as MessagePackSerializer.Get<MessagePackObject>( new SerializationContext() ).Unpack(stream)
            var result =
                MessagePackSerializer.UnpackMessagePackObject(
                    new MemoryStream(
                        new byte[]
            {
                MessagePackCode.MinimumFixedArray | 5,                   // Root (array)
                MessagePackCode.NilValue, MessagePackCode.FalseValue, 0, // Scalars
                MessagePackCode.MinimumFixedRaw | 1, ( byte )'a',        // Raw
                MessagePackCode.MinimumFixedMap | 1,                     // Map
                MessagePackCode.MinimumFixedRaw | 1, ( byte )'k',        // Key
                1                                                        //  Value of map
            }
                        )
                    );

            Assert.That(result.IsArray);
            Assert.That(result.AsList().Count, Is.EqualTo(5));
            Assert.That(result.AsList()[0].IsNil);
            Assert.That(result.AsList()[1], Is.EqualTo(new MessagePackObject(false)));
            Assert.That(result.AsList()[2], Is.EqualTo(new MessagePackObject(0)));
            Assert.That(result.AsList()[3], Is.EqualTo(new MessagePackObject("a")));
            Assert.That(result.AsList()[4].AsDictionary().Count, Is.EqualTo(1));
            Assert.That(result.AsList()[4].AsDictionary().First().Key, Is.EqualTo(new MessagePackObject("k")));
            Assert.That(result.AsList()[4].AsDictionary().First().Value, Is.EqualTo(new MessagePackObject(1)));
        }
Example #2
0
        public void TestIssue269()
        {
            var input  = new MessagePackObject(Timestamp.UtcNow.Encode());
            var target = MessagePackSerializer.UnpackMessagePackObject(MessagePackSerializer.Get <MessagePackObject>().PackSingleObject(input));

            Assert.That(target.UnderlyingType, Is.EqualTo(typeof(MessagePackExtendedTypeObject)));
            Assert.That(target.IsTypeOf <byte[]>(), Is.False);
            Assert.That(target.IsTypeOf <MessagePackExtendedTypeObject>(), Is.True);

            var forBinary = Assert.Throws <InvalidOperationException>(() => target.AsBinary());

            Assert.That(forBinary.Message, Is.EqualTo("Do not convert MsgPack.MessagePackExtendedTypeObject MessagePackObject to System.Byte[]."));

            var forString = Assert.Throws <InvalidOperationException>(() => target.AsString());

            Assert.That(forString.Message, Is.EqualTo("Do not convert MsgPack.MessagePackExtendedTypeObject MessagePackObject to System.String."));
        }
Example #3
0
        private static void TestEnumKeyCore(SerializationContext context, string expected, bool isAsync)
        {
            var serializer = context.GetSerializer <MidpointRounding>();
            var obj        = MidpointRounding.ToEven;

            using (var buffer = new MemoryStream())
            {
#if FEATURE_TAP
                if (isAsync)
                {
                    serializer.PackAsync(buffer, obj, CancellationToken.None).Wait();
                }
                else
                {
#endif // FEATURE_TAP
                serializer.Pack(buffer, obj);
#if FEATURE_TAP
            }
#endif // FEATURE_TAP

                buffer.Position = 0;
                var stringValue = MessagePackSerializer.UnpackMessagePackObject(buffer).AsString();

                Assert.That(stringValue, Is.EqualTo(expected));

                buffer.Position = 0;

                MidpointRounding deserialized;
#if FEATURE_TAP
                if (isAsync)
                {
                    deserialized = serializer.UnpackAsync(buffer, CancellationToken.None).Result;
                }
                else
                {
#endif // FEATURE_TAP
                deserialized = serializer.Unpack(buffer);
#if FEATURE_TAP
            }
#endif // FEATURE_TAP

                Assert.That(deserialized, Is.EqualTo(obj));
            }
        }
        private static void TestSerializationCore(SerializationContext context, DateTimeConversionMethod expected)
        {
            Assert.That(context.DefaultDateTimeConversionMethod, Is.EqualTo(expected));

            var now         = Timestamp.UtcNow;
            var dateTimeNow = now.ToDateTimeOffset();
            var source      =
                new ClassHasTimestamp
            {
                Timestamp      = now,
                DateTime       = dateTimeNow.AddSeconds(1).UtcDateTime,
                DateTimeOffset = dateTimeNow.AddSeconds(2),
#if (!SILVERLIGHT || WINDOWS_PHONE) && !XAMARIN && !UNITY && !UNITY
                FileTime = now.Add(TimeSpan.FromSeconds(3)).ToDateTime().ToWin32FileTimeUtc()
#endif // ( !SILVERLIGHT || WINDOWS_PHONE ) && !XAMARIN && !UNITY && !UNITY
            };

            using (var buffer = new MemoryStream())
            {
                var serializer = context.GetSerializer <ClassHasTimestamp>();
                serializer.Pack(
                    buffer,
                    source
                    );

                // Test representation
                buffer.Position = 0;
                var native = MessagePackSerializer.UnpackMessagePackObject(buffer);
                Assert.That(native.IsArray, Is.True);
                var nativeMembers = native.AsList();
                Assert.That(nativeMembers.Count, Is.EqualTo(ClassHasTimestamp.MemberCount));

                for (var i = 0; i < ClassHasTimestamp.MemberCount; i++)
                {
                    switch (expected)
                    {
                    case DateTimeConversionMethod.Native:
                    {
                        if (i == 2)
                        {
                            // DateTimeOffset -> [long, shourt]
                            Assert.That(nativeMembers[i].IsArray, Is.True);
                            var dtoComponents = nativeMembers[i].AsList();
                            Assert.That(dtoComponents.Count, Is.EqualTo(2));
                            Assert.That(dtoComponents[0].IsTypeOf <long>(), Is.True);
                            Assert.That(dtoComponents[1].IsTypeOf <short>(), Is.True);
                        }
                        else
                        {
                            Assert.That(nativeMembers[i].IsTypeOf <long>(), Is.True);
                        }
                        break;
                    }

                    case DateTimeConversionMethod.UnixEpoc:
                    {
                        Assert.That(nativeMembers[i].IsTypeOf <long>(), Is.True);
                        break;
                    }

                    case DateTimeConversionMethod.Timestamp:
                    {
                        Assert.That(nativeMembers[i].IsTypeOf <MessagePackExtendedTypeObject>(), Is.True);
                        var asExt = nativeMembers[i].AsMessagePackExtendedTypeObject();
                        Assert.That(asExt.TypeCode, Is.EqualTo(0xFF));
                        // Actual encoding should be tested in Timestamp tests.
                        break;
                    }
                    }
                }

                // Test round trip
                buffer.Position = 0;

                var result = serializer.Unpack(buffer);
                switch (expected)
                {
                case DateTimeConversionMethod.Native:
                {
                    Assert.That(result.Timestamp, Is.EqualTo(new Timestamp(now.UnixEpochSecondsPart, now.NanosecondsPart / 100 * 100)));
                    Assert.That(result.DateTime, Is.EqualTo(now.ToDateTime().AddSeconds(1)));
                    Assert.That(result.DateTimeOffset, Is.EqualTo(now.ToDateTimeOffset().AddSeconds(2)));
#if (!SILVERLIGHT || WINDOWS_PHONE) && !XAMARIN && !UNITY && !UNITY
                    Assert.That(result.FileTime, Is.EqualTo(now.Add(TimeSpan.FromSeconds(3).Subtract(TimeSpan.FromTicks(now.NanosecondsPart % 100))).ToDateTime().ToWin32FileTimeUtc()));
#endif // ( !SILVERLIGHT || WINDOWS_PHONE ) && !XAMARIN && !UNITY && !UNITY
                    break;
                }

                case DateTimeConversionMethod.UnixEpoc:
                {
                    Assert.That(result.Timestamp, Is.EqualTo(new Timestamp(now.UnixEpochSecondsPart, now.NanosecondsPart / 1000000 * 1000000)));
                    Assert.That(result.DateTime, Is.EqualTo(now.ToDateTime().AddSeconds(1).Subtract(TimeSpan.FromTicks(now.NanosecondsPart / 100 % 10000))));
                    Assert.That(result.DateTimeOffset, Is.EqualTo(now.ToDateTimeOffset().AddSeconds(2).Subtract(TimeSpan.FromTicks(now.NanosecondsPart / 100 % 10000))));
#if (!SILVERLIGHT || WINDOWS_PHONE) && !XAMARIN && !UNITY && !UNITY
                    Assert.That(result.FileTime, Is.EqualTo(now.Add(TimeSpan.FromSeconds(3).Subtract(TimeSpan.FromTicks(now.NanosecondsPart / 100 % 10000))).ToDateTime().ToWin32FileTimeUtc()));
#endif // ( !SILVERLIGHT || WINDOWS_PHONE ) && !XAMARIN && !UNITY && !UNITY
                    break;
                }

                case DateTimeConversionMethod.Timestamp:
                {
                    Assert.That(result.Timestamp, Is.EqualTo(now));
                    Assert.That(result.DateTime, Is.EqualTo(now.ToDateTime().AddSeconds(1)));
                    Assert.That(result.DateTimeOffset, Is.EqualTo(now.ToDateTimeOffset().AddSeconds(2)));
#if (!SILVERLIGHT || WINDOWS_PHONE) && !XAMARIN && !UNITY && !UNITY
                    Assert.That(result.FileTime, Is.EqualTo(now.Add(TimeSpan.FromSeconds(3)).ToDateTime().ToWin32FileTimeUtc()));
#endif // ( !SILVERLIGHT || WINDOWS_PHONE ) && !XAMARIN && !UNITY && !UNITY
                    break;
                }
                }
            }
        }