Exemple #1
0
    public long GetLong()
    {
        long value = MessagePackBinary.ReadInt64(_bytes, _offset, out int length);

        _offset += length;
        return(value);
    }
        public DateTime[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            if (MessagePackBinary.IsNil(bytes, offset))
            {
                readSize = 1;
                return(null);
            }

            var startOffset = offset;

            var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);

            offset += readSize;
            var array = new DateTime[len];

            for (var i = 0; i < array.Length; i++)
            {
                var dateData = MessagePackBinary.ReadInt64(bytes, offset, out readSize);
                array[i] = DateTime.FromBinary(dateData);
                offset  += readSize;
            }

            readSize = offset - startOffset;
            return(array);
        }
Exemple #3
0
        public Int64[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            if (MessagePackBinary.IsNil(bytes, offset))
            {
                readSize = 1;
                return(null);
            }
            else
            {
                var startOffset = offset;

                var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
                offset += readSize;
                var array = new Int64[len];
                using (MessagePackSecurity.DepthStep())
                {
                    for (int i = 0; i < array.Length; i++)
                    {
                        array[i] = MessagePackBinary.ReadInt64(bytes, offset, out readSize);
                        offset  += readSize;
                    }
                }

                readSize = offset - startOffset;
                return(array);
            }
        }
Exemple #4
0
        public long ReadInt64()
        {
            long value = MessagePackBinary.ReadInt64(Buffer, Position, out int readSize);

            Position += readSize;

            return(value);
        }
        public long?Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            if (MessagePackBinary.IsNil(bytes, offset))
            {
                readSize = 1;
                return(null);
            }

            return(MessagePackBinary.ReadInt64(bytes, offset, out readSize));
        }
        public DateTime Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            if (MessagePackBinary.GetMessagePackType(bytes, offset) == MessagePackType.Extension)
            {
                return(DateTimeFormatter.Instance.Deserialize(bytes, offset, formatterResolver, out readSize));
            }

            var dateData = MessagePackBinary.ReadInt64(bytes, offset, out readSize);

            return(DateTime.FromBinary(dateData));
        }
Exemple #7
0
        /// <inheritdoc cref="IMessagePackFormatter{T}.Deserialize(byte[], int, IFormatterResolver, out int)" />
        public DateTimeOffset Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            if (MessagePackBinary.GetMessagePackType(bytes, offset) == MessagePackType.Extension)
            {
                return(DateTimeFormatter.Instance.Deserialize(bytes, offset, formatterResolver, out readSize));
            }

            var unixTimestamp = MessagePackBinary.ReadInt64(bytes, offset, out readSize);

            return(DateTimeOffset.FromUnixTimeSeconds(unixTimestamp));
        }
 public Int64?Deserialize(byte[] bytes, int offset, out int readSize)
 {
     if (MessagePackBinary.IsNil(bytes, offset))
     {
         readSize = 1;
         return(null);
     }
     else
     {
         return(MessagePackBinary.ReadInt64(bytes, offset, out readSize));
     }
 }
Exemple #9
0
        public global::PlayerState Deserialize(byte[] bytes, int offset, global::MessagePack.IFormatterResolver formatterResolver, out int readSize)
        {
            if (global::MessagePack.MessagePackBinary.IsNil(bytes, offset))
            {
                throw new InvalidOperationException("typecode is null, struct not supported");
            }

            var startOffset = offset;
            var length      = global::MessagePack.MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);

            offset += readSize;

            var __Id__       = default(int);
            var __Position__ = default(global::Vector3Sim);
            var __Index__    = default(long);

            for (int i = 0; i < length; i++)
            {
                var key = i;

                switch (key)
                {
                case 0:
                    __Id__ = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
                    break;

                case 1:
                    __Position__ = formatterResolver.GetFormatterWithVerify <global::Vector3Sim>().Deserialize(bytes, offset, formatterResolver, out readSize);
                    break;

                case 2:
                    __Index__ = MessagePackBinary.ReadInt64(bytes, offset, out readSize);
                    break;

                default:
                    readSize = global::MessagePack.MessagePackBinary.ReadNextBlock(bytes, offset);
                    break;
                }
                offset += readSize;
            }

            readSize = offset - startOffset;

            var ____result = new global::PlayerState();

            ____result.Id       = __Id__;
            ____result.Position = __Position__;
            ____result.Index    = __Index__;
            return(____result);
        }
Exemple #10
0
        public DateTimeOffset Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            var startOffset = offset;
            var count       = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);

            offset += readSize;

            if (count != 2)
            {
                throw new InvalidOperationException("Invalid DateTimeOffset format.");
            }

            var utc = MessagePackBinary.ReadDateTime(bytes, offset, out readSize);

            offset += readSize;

            var dtOffsetTicks = MessagePackBinary.ReadInt64(bytes, offset, out readSize);

            offset += readSize;

            readSize = offset - startOffset;

            return(new DateTimeOffset(utc, new TimeSpan(dtOffsetTicks)));
        }
Exemple #11
0
        public Period Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            if (MessagePackBinary.IsNil(bytes, offset))
            {
                readSize = 1;
                return(null);
            }
            var startOffset = offset;

            var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);

            offset += readSize;
            if (count != 10)
            {
                throw new InvalidOperationException("Invalid array count");
            }

            var years = MessagePackBinary.ReadInt32(bytes, offset, out readSize);

            offset += readSize;
            var months = MessagePackBinary.ReadInt32(bytes, offset, out readSize);

            offset += readSize;
            var weeks = MessagePackBinary.ReadInt32(bytes, offset, out readSize);

            offset += readSize;
            var days = MessagePackBinary.ReadInt32(bytes, offset, out readSize);

            offset += readSize;
            var hours = MessagePackBinary.ReadInt64(bytes, offset, out readSize);

            offset += readSize;
            var minutes = MessagePackBinary.ReadInt64(bytes, offset, out readSize);

            offset += readSize;
            var seconds = MessagePackBinary.ReadInt64(bytes, offset, out readSize);

            offset += readSize;
            var milliseconds = MessagePackBinary.ReadInt64(bytes, offset, out readSize);

            offset += readSize;
            var ticks = MessagePackBinary.ReadInt64(bytes, offset, out readSize);

            offset += readSize;
            var nano = MessagePackBinary.ReadInt64(bytes, offset, out readSize);

            offset += readSize;


            readSize = offset - startOffset;

            return(new PeriodBuilder()
            {
                Years = years,
                Months = months,
                Weeks = weeks,
                Days = days,
                Hours = hours,
                Minutes = minutes,
                Seconds = seconds,
                Milliseconds = milliseconds,
                Ticks = ticks,
                Nanoseconds = nano,
            }.Build());
        }
Exemple #12
0
        public static object ReadObject(byte[] input, ref int offset)
        {
            var type = MessagePackBinary.GetMessagePackType(input, offset);
            int size;

            switch (type)
            {
            case MessagePackType.Integer:
                var intValue = MessagePackBinary.ReadInt64(input, offset, out size);
                offset += size;
                return(intValue);

            case MessagePackType.Nil:
                MessagePackBinary.ReadNil(input, offset, out size);
                offset += size;
                return(null);

            case MessagePackType.Boolean:
                var boolValue = MessagePackBinary.ReadBoolean(input, offset, out size);
                offset += size;
                return(boolValue);

            case MessagePackType.Float:
                var doubleValue = MessagePackBinary.ReadDouble(input, offset, out size);
                offset += size;
                return(doubleValue);

            case MessagePackType.String:
                var textValue = MessagePackBinary.ReadString(input, offset, out size);
                offset += size;
                return(textValue);

            case MessagePackType.Binary:
                var binaryValue = MessagePackBinary.ReadBytes(input, offset, out size);
                offset += size;
                return(binaryValue);

            case MessagePackType.Array:
                var argumentCount = ReadArrayLength(input, ref offset, "arguments");
                var array         = new object[argumentCount];
                for (int i = 0; i < argumentCount; i++)
                {
                    array[i] = ReadObject(input, ref offset);
                }
                return(array);

            case MessagePackType.Map:
                var propertyCount = MessagePackBinary.ReadMapHeader(input, offset, out size);
                offset += size;
                var map = new Dictionary <string, object>();
                for (int i = 0; i < propertyCount; i++)
                {
                    textValue = MessagePackBinary.ReadString(input, offset, out size);
                    offset   += size;
                    var value = ReadObject(input, ref offset);
                    map[textValue] = value;
                }
                return(map);

            case MessagePackType.Extension:
            case MessagePackType.Unknown:
            default:
                return(null);
            }
        }
Exemple #13
0
        static int ToJsonCore(byte[] bytes, int offset, StringBuilder builder)
        {
            var readSize = 0;
            var type     = MessagePackBinary.GetMessagePackType(bytes, offset);

            switch (type)
            {
            case MessagePackType.Integer:
                var code = bytes[offset];
                if (MessagePackCode.MinNegativeFixInt <= code && code <= MessagePackCode.MaxNegativeFixInt)
                {
                    builder.Append(MessagePackBinary.ReadSByte(bytes, offset, out readSize));
                }
                else if (MessagePackCode.MinFixInt <= code && code <= MessagePackCode.MaxFixInt)
                {
                    builder.Append(MessagePackBinary.ReadByte(bytes, offset, out readSize));
                }
                else if (code == MessagePackCode.Int8)
                {
                    builder.Append(MessagePackBinary.ReadSByte(bytes, offset, out readSize));
                }
                else if (code == MessagePackCode.Int16)
                {
                    builder.Append(MessagePackBinary.ReadInt16(bytes, offset, out readSize));
                }
                else if (code == MessagePackCode.Int32)
                {
                    builder.Append(MessagePackBinary.ReadInt32(bytes, offset, out readSize));
                }
                else if (code == MessagePackCode.Int64)
                {
                    builder.Append(MessagePackBinary.ReadInt64(bytes, offset, out readSize));
                }
                else if (code == MessagePackCode.UInt8)
                {
                    builder.Append(MessagePackBinary.ReadByte(bytes, offset, out readSize));
                }
                else if (code == MessagePackCode.UInt16)
                {
                    builder.Append(MessagePackBinary.ReadUInt16(bytes, offset, out readSize));
                }
                else if (code == MessagePackCode.UInt32)
                {
                    builder.Append(MessagePackBinary.ReadUInt32(bytes, offset, out readSize));
                }
                else if (code == MessagePackCode.UInt64)
                {
                    builder.Append(MessagePackBinary.ReadUInt64(bytes, offset, out readSize));
                }
                break;

            case MessagePackType.Boolean:
                builder.Append(MessagePackBinary.ReadBoolean(bytes, offset, out readSize) ? "true" : "false");
                break;

            case MessagePackType.Float:
                var floatCode = bytes[offset];
                if (floatCode == MessagePackCode.Float32)
                {
                    builder.Append(MessagePackBinary.ReadSingle(bytes, offset, out readSize).ToString(System.Globalization.CultureInfo.InvariantCulture));
                }
                else
                {
                    builder.Append(MessagePackBinary.ReadDouble(bytes, offset, out readSize).ToString(System.Globalization.CultureInfo.InvariantCulture));
                }
                break;

            case MessagePackType.String:
                WriteJsonString(MessagePackBinary.ReadString(bytes, offset, out readSize), builder);
                break;

            case MessagePackType.Binary:
                builder.Append("\"" + Convert.ToBase64String(MessagePackBinary.ReadBytes(bytes, offset, out readSize)) + "\"");
                break;

            case MessagePackType.Array:
            {
                var length        = MessagePackBinary.ReadArrayHeaderRaw(bytes, offset, out readSize);
                var totalReadSize = readSize;
                offset += readSize;
                builder.Append("[");
                for (int i = 0; i < length; i++)
                {
                    readSize       = ToJsonCore(bytes, offset, builder);
                    offset        += readSize;
                    totalReadSize += readSize;

                    if (i != length - 1)
                    {
                        builder.Append(",");
                    }
                }
                builder.Append("]");

                return(totalReadSize);
            }

            case MessagePackType.Map:
            {
                var length        = MessagePackBinary.ReadMapHeaderRaw(bytes, offset, out readSize);
                var totalReadSize = readSize;
                offset += readSize;
                builder.Append("{");
                for (int i = 0; i < length; i++)
                {
                    // write key
                    {
                        var keyType = MessagePackBinary.GetMessagePackType(bytes, offset);
                        if (keyType == MessagePackType.String || keyType == MessagePackType.Binary)
                        {
                            readSize = ToJsonCore(bytes, offset, builder);
                        }
                        else
                        {
                            builder.Append("\"");
                            readSize = ToJsonCore(bytes, offset, builder);
                            builder.Append("\"");
                        }
                        offset        += readSize;
                        totalReadSize += readSize;
                    }

                    builder.Append(":");

                    // write body
                    {
                        readSize       = ToJsonCore(bytes, offset, builder);
                        offset        += readSize;
                        totalReadSize += readSize;
                    }

                    if (i != length - 1)
                    {
                        builder.Append(",");
                    }
                }
                builder.Append("}");

                return(totalReadSize);
            }

            case MessagePackType.Extension:
                var extHeader = MessagePackBinary.ReadExtensionFormatHeader(bytes, offset, out readSize);
                if (extHeader.TypeCode == ReservedMessagePackExtensionTypeCode.DateTime)
                {
                    var dt = MessagePackBinary.ReadDateTime(bytes, offset, out readSize);
                    builder.Append("\"");
                    builder.Append(dt.ToString("o", CultureInfo.InvariantCulture));
                    builder.Append("\"");
                }
                else
                {
                    var ext = MessagePackBinary.ReadExtensionFormat(bytes, offset, out readSize);
                    builder.Append("[");
                    builder.Append(ext.TypeCode);
                    builder.Append(",");
                    builder.Append("\"");
                    builder.Append(Convert.ToBase64String(ext.Data));
                    builder.Append("\"");
                    builder.Append("]");
                }
                break;

            case MessagePackType.Unknown:
            case MessagePackType.Nil:
            default:
                readSize = 1;
                builder.Append("null");
                break;
            }

            return(readSize);
        }
Exemple #14
0
        public global::eventDesc Deserialize(byte[] bytes, int offset, global::MessagePack.IFormatterResolver formatterResolver, out int readSize)
        {
            if (global::MessagePack.MessagePackBinary.IsNil(bytes, offset))
            {
                readSize = 1;
                return(null);
            }

            var startOffset = offset;
            var length      = global::MessagePack.MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);

            offset += readSize;

            var __run__       = default(long);
            var __evn__       = default(long);
            var __baseDesc__  = default(string);
            var __energy__    = default(double);
            var __eventDate__ = default(string);
            var __humName__   = default(string);
            var __track__     = default(global::trackData);
            var __comment__   = default(string);

            for (int i = 0; i < length; i++)
            {
                var key = i;

                switch (key)
                {
                case 0:
                    __run__ = MessagePackBinary.ReadInt64(bytes, offset, out readSize);
                    break;

                case 1:
                    __evn__ = MessagePackBinary.ReadInt64(bytes, offset, out readSize);
                    break;

                case 2:
                    __baseDesc__ = formatterResolver.GetFormatterWithVerify <string>().Deserialize(bytes, offset, formatterResolver, out readSize);
                    break;

                case 3:
                    __energy__ = MessagePackBinary.ReadDouble(bytes, offset, out readSize);
                    break;

                case 4:
                    __eventDate__ = formatterResolver.GetFormatterWithVerify <string>().Deserialize(bytes, offset, formatterResolver, out readSize);
                    break;

                case 5:
                    __humName__ = formatterResolver.GetFormatterWithVerify <string>().Deserialize(bytes, offset, formatterResolver, out readSize);
                    break;

                case 6:
                    __track__ = formatterResolver.GetFormatterWithVerify <global::trackData>().Deserialize(bytes, offset, formatterResolver, out readSize);
                    break;

                case 7:
                    __comment__ = formatterResolver.GetFormatterWithVerify <string>().Deserialize(bytes, offset, formatterResolver, out readSize);
                    break;

                default:
                    readSize = global::MessagePack.MessagePackBinary.ReadNextBlock(bytes, offset);
                    break;
                }
                offset += readSize;
            }

            readSize = offset - startOffset;

            var ____result = new global::eventDesc();

            ____result.run       = __run__;
            ____result.evn       = __evn__;
            ____result.baseDesc  = __baseDesc__;
            ____result.energy    = __energy__;
            ____result.eventDate = __eventDate__;
            ____result.humName   = __humName__;
            ____result.track     = __track__;
            ____result.comment   = __comment__;
            return(____result);
        }
        public void Read()
        {
            void Check1 <T, T2>(T data, T2 result, Func <Stream, T2> streamRead)
            {
                const int CheckOffset = 10;

                byte[] bytes = null;
                var    len   = MessagePack.Resolvers.StandardResolver.Instance.GetFormatter <T>().Serialize(ref bytes, CheckOffset, data, MessagePack.Resolvers.StandardResolver.Instance);

                MessagePackBinary.FastResize(ref bytes, CheckOffset + len);


                var ms = new MemoryStream(bytes);

                ms.Position = CheckOffset;

                streamRead(ms).Is(result);
            }

            void Check2 <T>(T data, Func <Stream, T> streamRead)
            {
                const int CheckOffset = 10;

                byte[] bytes = null;
                var    len   = MessagePack.Resolvers.StandardResolver.Instance.GetFormatter <T>().Serialize(ref bytes, CheckOffset, data, MessagePack.Resolvers.StandardResolver.Instance);

                MessagePackBinary.FastResize(ref bytes, CheckOffset + len);


                var ms = new MemoryStream(bytes);

                ms.Position = CheckOffset;

                streamRead(ms).Is(data);
            }

            Check1(new[] { 1, 10, 100, 1000, 10000, short.MaxValue, int.MaxValue }, 7, x => MessagePackBinary.ReadArrayHeader(x));
            Check1(new[] { 1, 10, 100, 1000, 10000, short.MaxValue, int.MaxValue }, (uint)7, x => MessagePackBinary.ReadArrayHeaderRaw(x));
            Check1(Nil.Default, true, x => MessagePackBinary.IsNil((x)));
            Check2(true, x => MessagePackBinary.ReadBoolean(x));
            Check2((byte)100, x => MessagePackBinary.ReadByte(x));
            Check2(new byte[] { 1, 10, 100, 245 }, x => MessagePackBinary.ReadBytes(x));
            Check2('あ', x => MessagePackBinary.ReadChar(x));
            Check2(DateTime.UtcNow, x => MessagePackBinary.ReadDateTime(x));
            Check2(132, x => MessagePackBinary.ReadInt16(x));
            Check2(423, x => MessagePackBinary.ReadInt32(x));
            Check2(64332, x => MessagePackBinary.ReadInt64(x));
            Check2(Nil.Default, x => MessagePackBinary.ReadNil(x));
            Check2(11, x => MessagePackBinary.ReadSByte(x));
            Check2(10.31231f, x => MessagePackBinary.ReadSingle(x));
            Check2("foobar", x => MessagePackBinary.ReadString(x));
            Check2(124, x => MessagePackBinary.ReadUInt16(x));
            Check2((uint)432, x => MessagePackBinary.ReadUInt32(x));
            Check2((ulong)432, x => MessagePackBinary.ReadUInt64(x));


            Check1(new Dictionary <int, int>()
            {
                { 1, 2 }
            }, 1, x => MessagePackBinary.ReadMapHeader(x));
            Check1(new Dictionary <int, int>()
            {
                { 1, 2 }
            }, (uint)1, x => MessagePackBinary.ReadMapHeaderRaw(x));

            {
                var block  = new object[] { 1, new[] { 1, 10, 100 }, 100 };
                var bytes  = MessagePackSerializer.Serialize(block);
                var stream = new MemoryStream(bytes);
                MessagePackBinary.ReadNext(stream);      // array(first)
                MessagePackBinary.ReadNext(stream);      // int
                MessagePackBinary.ReadNextBlock(stream); // skip array
                MessagePackBinary.ReadInt32(stream).Is(100);
            }
            {
                var block = new object[] { 1, new Dictionary <int, int> {
                                               { 1, 10 }, { 111, 200 }
                                           }, 100 };
                var bytes  = MessagePackSerializer.Serialize(block);
                var stream = new MemoryStream(bytes);
                MessagePackBinary.ReadNext(stream);
                MessagePackBinary.ReadNext(stream);
                MessagePackBinary.ReadNextBlock(stream);
                MessagePackBinary.ReadInt32(stream).Is(100);
            }
        }
Exemple #16
0
        public DateTime Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            var ticks = MessagePackBinary.ReadInt64(bytes, offset, out readSize);

            return(new DateTime(ticks));
        }
Exemple #17
0
        public LocalTime Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            var nanos = MessagePackBinary.ReadInt64(bytes, offset, out readSize);

            return(LocalTime.Midnight.PlusNanoseconds(nanos));
        }
Exemple #18
0
        public object Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            var type = MessagePackBinary.GetMessagePackType(bytes, offset);

            switch (type)
            {
            case MessagePackType.Integer:
                var code = bytes[offset];
                if (MessagePackCode.MinNegativeFixInt <= code && code <= MessagePackCode.MaxNegativeFixInt)
                {
                    return(MessagePackBinary.ReadSByte(bytes, offset, out readSize));
                }
                else if (MessagePackCode.MinFixInt <= code && code <= MessagePackCode.MaxFixInt)
                {
                    return(MessagePackBinary.ReadByte(bytes, offset, out readSize));
                }
                else if (code == MessagePackCode.Int8)
                {
                    return(MessagePackBinary.ReadSByte(bytes, offset, out readSize));
                }
                else if (code == MessagePackCode.Int16)
                {
                    return(MessagePackBinary.ReadInt16(bytes, offset, out readSize));
                }
                else if (code == MessagePackCode.Int32)
                {
                    return(MessagePackBinary.ReadInt32(bytes, offset, out readSize));
                }
                else if (code == MessagePackCode.Int64)
                {
                    return(MessagePackBinary.ReadInt64(bytes, offset, out readSize));
                }
                else if (code == MessagePackCode.UInt8)
                {
                    return(MessagePackBinary.ReadByte(bytes, offset, out readSize));
                }
                else if (code == MessagePackCode.UInt16)
                {
                    return(MessagePackBinary.ReadUInt16(bytes, offset, out readSize));
                }
                else if (code == MessagePackCode.UInt32)
                {
                    return(MessagePackBinary.ReadUInt32(bytes, offset, out readSize));
                }
                else if (code == MessagePackCode.UInt64)
                {
                    return(MessagePackBinary.ReadUInt64(bytes, offset, out readSize));
                }
                throw new InvalidOperationException("Invalid primitive bytes.");

            case MessagePackType.Boolean:
                return(MessagePackBinary.ReadBoolean(bytes, offset, out readSize));

            case MessagePackType.Float:
                if (MessagePackCode.Float32 == bytes[offset])
                {
                    return(MessagePackBinary.ReadSingle(bytes, offset, out readSize));
                }
                else
                {
                    return(MessagePackBinary.ReadDouble(bytes, offset, out readSize));
                }

            case MessagePackType.String:
                return(MessagePackBinary.ReadString(bytes, offset, out readSize));

            case MessagePackType.Binary:
                return(MessagePackBinary.ReadBytes(bytes, offset, out readSize));

            case MessagePackType.Extension:
                var ext = MessagePackBinary.ReadExtensionFormatHeader(bytes, offset, out readSize);
                if (ext.TypeCode == ReservedMessagePackExtensionTypeCode.DateTime)
                {
                    return(MessagePackBinary.ReadDateTime(bytes, offset, out readSize));
                }
                throw new InvalidOperationException("Invalid primitive bytes.");

            case MessagePackType.Array:
            {
                var length      = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
                var startOffset = offset;
                offset += readSize;

                var objectFormatter = formatterResolver.GetFormatter <object>();
                var array           = new object[length];
                for (int i = 0; i < length; i++)
                {
                    array[i] = objectFormatter.Deserialize(bytes, offset, formatterResolver, out readSize);
                    offset  += readSize;
                }

                readSize = offset - startOffset;
                return(array);
            }

            case MessagePackType.Map:
            {
                var length      = MessagePackBinary.ReadMapHeader(bytes, offset, out readSize);
                var startOffset = offset;
                offset += readSize;

                var objectFormatter = formatterResolver.GetFormatter <object>();
                var hash            = new Dictionary <object, object>(length);
                for (int i = 0; i < length; i++)
                {
                    var key = objectFormatter.Deserialize(bytes, offset, formatterResolver, out readSize);
                    offset += readSize;

                    var value = objectFormatter.Deserialize(bytes, offset, formatterResolver, out readSize);
                    offset += readSize;

                    hash.Add(key, value);
                }

                readSize = offset - startOffset;
                return(hash);
            }

            case MessagePackType.Nil:
                readSize = 1;
                return(null);

            default:
                throw new InvalidOperationException("Invalid primitive bytes.");
            }
        }
Exemple #19
0
        public global::InputData Deserialize(byte[] bytes, int offset, global::MessagePack.IFormatterResolver formatterResolver, out int readSize)
        {
            if (global::MessagePack.MessagePackBinary.IsNil(bytes, offset))
            {
                throw new InvalidOperationException("typecode is null, struct not supported");
            }

            var startOffset = offset;
            var length      = global::MessagePack.MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);

            offset += readSize;

            var __Index__ = default(long);
            var __Right__ = default(bool);
            var __Left__  = default(bool);
            var __Up__    = default(bool);
            var __Down__  = default(bool);

            for (int i = 0; i < length; i++)
            {
                var key = i;

                switch (key)
                {
                case 0:
                    __Index__ = MessagePackBinary.ReadInt64(bytes, offset, out readSize);
                    break;

                case 1:
                    __Right__ = MessagePackBinary.ReadBoolean(bytes, offset, out readSize);
                    break;

                case 2:
                    __Left__ = MessagePackBinary.ReadBoolean(bytes, offset, out readSize);
                    break;

                case 3:
                    __Up__ = MessagePackBinary.ReadBoolean(bytes, offset, out readSize);
                    break;

                case 4:
                    __Down__ = MessagePackBinary.ReadBoolean(bytes, offset, out readSize);
                    break;

                default:
                    readSize = global::MessagePack.MessagePackBinary.ReadNextBlock(bytes, offset);
                    break;
                }
                offset += readSize;
            }

            readSize = offset - startOffset;

            var ____result = new global::InputData();

            ____result.Index = __Index__;
            ____result.Right = __Right__;
            ____result.Left  = __Left__;
            ____result.Up    = __Up__;
            ____result.Down  = __Down__;
            return(____result);
        }
Exemple #20
0
        public Product Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            int     startOffset = offset;
            int     size        = 0;
            Product p           = new Product();

            _       = MessagePackBinary.ReadMapHeader(bytes, offset, out size);
            offset += size;

            string fieldName = MessagePackBinary.ReadString(bytes, offset, out size);

            offset += size;

            p.Id    = MessagePackBinary.ReadInt64(bytes, offset, out size);
            offset += size;

            fieldName = MessagePackBinary.ReadString(bytes, offset, out size);
            offset   += size;

            p.Name  = MessagePackBinary.ReadString(bytes, offset, out size);
            offset += size;

            fieldName     = MessagePackBinary.ReadString(bytes, offset, out size);
            offset       += size;
            p.Description = MessagePackBinary.ReadString(bytes, offset, out size);
            offset       += size;

            fieldName = MessagePackBinary.ReadString(bytes, offset, out size);
            offset   += size;
            p.Seller  = MessagePackBinary.ReadString(bytes, offset, out size);
            offset   += size;

            fieldName = MessagePackBinary.ReadString(bytes, offset, out size);
            offset   += size;
            formatterResolver.GetFormatter <Category>().Deserialize(bytes, offset, formatterResolver, out size);
            offset += size;

            fieldName = MessagePackBinary.ReadString(bytes, offset, out size);
            offset   += size;
            p.Rating  = formatterResolver.GetFormatter <Rate>().Deserialize(bytes, offset, formatterResolver, out size);
            offset   += size;

            fieldName = MessagePackBinary.ReadString(bytes, offset, out size);
            offset   += size;
            if (bytes[offset] != 0xc0)
            {
                string  price = MessagePackBinary.ReadString(bytes, offset, out size);
                decimal priceResult;
                if (Decimal.TryParse(price, out priceResult))
                {
                    p.Price = priceResult;
                }
                ;
            }
            else
            {
                MessagePackBinary.ReadNil(bytes, offset, out size);
            }
            offset += size;

            fieldName  = MessagePackBinary.ReadString(bytes, offset, out size);
            offset    += size;
            p.Currency = MessagePackBinary.ReadString(bytes, offset, out size);
            offset    += size;

            fieldName = MessagePackBinary.ReadString(bytes, offset, out size);
            offset   += size;
            p.Created = MessagePackBinary.ReadDateTime(bytes, offset, out size);
            offset   += size;

            fieldName = MessagePackBinary.ReadString(bytes, offset, out size);
            offset   += size;
            if (bytes[offset] != 0xc0)
            {
                p.Updated = MessagePackBinary.ReadDateTime(bytes, offset, out size);
            }
            else
            {
                MessagePackBinary.ReadNil(bytes, offset, out size);
            }
            offset += size;

            fieldName = MessagePackBinary.ReadString(bytes, offset, out size);
            offset   += size;
            p.Labels  = formatterResolver.GetFormatter <ICollection <string> >().Deserialize(bytes, offset, formatterResolver, out size);
            offset   += size;

            fieldName = MessagePackBinary.ReadString(bytes, offset, out size);
            offset   += size;
            Uri uriStr = formatterResolver.GetFormatter <Uri>().Deserialize(bytes, offset, formatterResolver, out size);

            p.Location = uriStr;
            offset    += size;

            fieldName = MessagePackBinary.ReadString(bytes, offset, out size);
            offset   += size;
            p.Image   = MessagePackBinary.ReadBytes(bytes, offset, out size);
            offset   += size;

            fieldName = MessagePackBinary.ReadString(bytes, offset, out size);
            offset   += size;

            p.Details = formatterResolver.GetFormatter <Dictionary <string, string> >().Deserialize(bytes, offset, formatterResolver, out size);
            offset   += size;

            readSize = offset - startOffset;
            return(p);
        }
 public override long ReadLongValue()
 {
     return(MessagePackBinary.ReadInt64(this.DecoderStream));
 }
 public Int64 Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
 {
     return(MessagePackBinary.ReadInt64(bytes, offset, out readSize));
 }
        static int ToJsonCore(byte[] bytes, int offset, StringBuilder builder)
        {
            var readSize = 0;
            var type     = MessagePackBinary.GetMessagePackType(bytes, offset);

            switch (type)
            {
            case MessagePackType.Integer:
                var code = bytes[offset];
                if (MessagePackCode.MinNegativeFixInt <= code && code <= MessagePackCode.MaxNegativeFixInt)
                {
                    builder.Append(MessagePackBinary.ReadSByte(bytes, offset, out readSize));
                }
                else if (MessagePackCode.MinFixInt <= code && code <= MessagePackCode.MaxFixInt)
                {
                    builder.Append(MessagePackBinary.ReadByte(bytes, offset, out readSize));
                }
                else if (code == MessagePackCode.Int8)
                {
                    builder.Append(MessagePackBinary.ReadSByte(bytes, offset, out readSize));
                }
                else if (code == MessagePackCode.Int16)
                {
                    builder.Append(MessagePackBinary.ReadInt16(bytes, offset, out readSize));
                }
                else if (code == MessagePackCode.Int32)
                {
                    builder.Append(MessagePackBinary.ReadInt32(bytes, offset, out readSize));
                }
                else if (code == MessagePackCode.Int64)
                {
                    builder.Append(MessagePackBinary.ReadInt64(bytes, offset, out readSize));
                }
                else if (code == MessagePackCode.UInt8)
                {
                    builder.Append(MessagePackBinary.ReadByte(bytes, offset, out readSize));
                }
                else if (code == MessagePackCode.UInt16)
                {
                    builder.Append(MessagePackBinary.ReadUInt16(bytes, offset, out readSize));
                }
                else if (code == MessagePackCode.UInt32)
                {
                    builder.Append(MessagePackBinary.ReadUInt32(bytes, offset, out readSize));
                }
                else if (code == MessagePackCode.UInt64)
                {
                    builder.Append(MessagePackBinary.ReadUInt64(bytes, offset, out readSize));
                }
                break;

            case MessagePackType.Boolean:
                builder.Append(MessagePackBinary.ReadBoolean(bytes, offset, out readSize) ? "true" : "false");
                break;

            case MessagePackType.Float:
                var floatCode = bytes[offset];
                if (floatCode == MessagePackCode.Float32)
                {
                    builder.Append(MessagePackBinary.ReadSingle(bytes, offset, out readSize).ToString(System.Globalization.CultureInfo.InvariantCulture));
                }
                else
                {
                    builder.Append(MessagePackBinary.ReadDouble(bytes, offset, out readSize).ToString(System.Globalization.CultureInfo.InvariantCulture));
                }
                break;

            case MessagePackType.String:
                WriteJsonString(MessagePackBinary.ReadString(bytes, offset, out readSize), builder);
                break;

            case MessagePackType.Binary:
                builder.Append("\"" + Convert.ToBase64String(MessagePackBinary.ReadBytes(bytes, offset, out readSize)) + "\"");
                break;

            case MessagePackType.Array:
            {
                var length        = MessagePackBinary.ReadArrayHeaderRaw(bytes, offset, out readSize);
                var totalReadSize = readSize;
                offset += readSize;
                builder.Append("[");
                for (int i = 0; i < length; i++)
                {
                    readSize       = ToJsonCore(bytes, offset, builder);
                    offset        += readSize;
                    totalReadSize += readSize;

                    if (i != length - 1)
                    {
                        builder.Append(",");
                    }
                }
                builder.Append("]");

                return(totalReadSize);
            }

            case MessagePackType.Map:
            {
                var length        = MessagePackBinary.ReadMapHeaderRaw(bytes, offset, out readSize);
                var totalReadSize = readSize;
                offset += readSize;
                builder.Append("{");
                for (int i = 0; i < length; i++)
                {
                    // write key
                    {
                        var keyType = MessagePackBinary.GetMessagePackType(bytes, offset);
                        if (keyType == MessagePackType.String || keyType == MessagePackType.Binary)
                        {
                            readSize = ToJsonCore(bytes, offset, builder);
                        }
                        else
                        {
                            builder.Append("\"");
                            readSize = ToJsonCore(bytes, offset, builder);
                            builder.Append("\"");
                        }
                        offset        += readSize;
                        totalReadSize += readSize;
                    }

                    builder.Append(":");

                    // write body
                    {
                        readSize       = ToJsonCore(bytes, offset, builder);
                        offset        += readSize;
                        totalReadSize += readSize;
                    }

                    if (i != length - 1)
                    {
                        builder.Append(",");
                    }
                }
                builder.Append("}");

                return(totalReadSize);
            }

            case MessagePackType.Extension:
                var extHeader = MessagePackBinary.ReadExtensionFormatHeader(bytes, offset, out readSize);
                if (extHeader.TypeCode == ReservedMessagePackExtensionTypeCode.DateTime)
                {
                    var dt = MessagePackBinary.ReadDateTime(bytes, offset, out readSize);
                    builder.Append("\"");
                    builder.Append(dt.ToString("o", CultureInfo.InvariantCulture));
                    builder.Append("\"");
                }
#if NETSTANDARD || NETFRAMEWORK
                else if (extHeader.TypeCode == TypelessFormatter.ExtensionTypeCode)
                {
                    int startOffset = offset;
                    // prepare type name token
                    offset += 6;
                    var typeNameToken    = new StringBuilder();
                    var typeNameReadSize = ToJsonCore(bytes, offset, typeNameToken);
                    offset += typeNameReadSize;
                    int startBuilderLength = builder.Length;
                    if (extHeader.Length > typeNameReadSize)
                    {
                        // object map or array
                        var typeInside = MessagePackBinary.GetMessagePackType(bytes, offset);
                        if (typeInside != MessagePackType.Array && typeInside != MessagePackType.Map)
                        {
                            builder.Append("{");
                        }
                        offset += ToJsonCore(bytes, offset, builder);
                        // insert type name token to start of object map or array
                        if (typeInside != MessagePackType.Array)
                        {
                            typeNameToken.Insert(0, "\"$type\":");
                        }
                        if (typeInside != MessagePackType.Array && typeInside != MessagePackType.Map)
                        {
                            builder.Append("}");
                        }
                        if (builder.Length - startBuilderLength > 2)
                        {
                            typeNameToken.Append(",");
                        }
                        builder.Insert(startBuilderLength + 1, typeNameToken.ToString());
                    }
                    else
                    {
                        builder.Append("{\"$type\":\"" + typeNameToken.ToString() + "}");
                    }
                    readSize = offset - startOffset;
                }
#endif
                else
                {
                    var ext = MessagePackBinary.ReadExtensionFormat(bytes, offset, out readSize);
                    builder.Append("[");
                    builder.Append(ext.TypeCode);
                    builder.Append(",");
                    builder.Append("\"");
                    builder.Append(Convert.ToBase64String(ext.Data));
                    builder.Append("\"");
                    builder.Append("]");
                }
                break;

            case MessagePackType.Unknown:
            case MessagePackType.Nil:
            default:
                readSize = 1;
                builder.Append("null");
                break;
            }

            return(readSize);
        }