private SwimMember DeserializeMember(byte[] bytes, int offset, out int readSize)
        {
            readSize = 0;

            var host = DeserializeHost(bytes, offset + readSize, out var r);

            readSize += r;

            var metaCount = MessagePackBinary.ReadMapHeader(bytes, offset + readSize, out r);

            readSize += r;
            var meta = new Dictionary <string, string>();

            for (var i = 0; i < metaCount; i++)
            {
                var key = MessagePackBinary.ReadString(bytes, offset + readSize, out r);
                readSize += r;
                var value = MessagePackBinary.ReadString(bytes, offset + readSize, out r);
                readSize += r;

                meta.Add(key, value);
            }

            var state = (SwimMemberState)MessagePackBinary.ReadByte(bytes, offset + readSize, out r);

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

            readSize += r;

            return(new SwimMember(host, new SwimMeta(meta), state, incarnation));
        }
        public void Standard()
        {
            var o = new SimpleIntKeyData()
            {
                Prop1 = 100,
                Prop2 = ByteEnum.C,
                Prop3 = "abcde",
                Prop4 = new SimlpeStringKeyData
                {
                    Prop1 = 99999,
                    Prop2 = ByteEnum.E,
                    Prop3 = 3
                },
                Prop5 = new SimpleStructIntKeyData
                {
                    X            = 100,
                    Y            = 300,
                    BytesSpecial = new byte[] { 9, 99, 122 }
                },
                Prop6 = new SimpleStructStringKeyData
                {
                    X = 9999,
                    Y = new[] { 1, 10, 100 }
                },
                BytesSpecial = new byte[] { 1, 4, 6 }
            };


            var bytes = MessagePackSerializer.Serialize(o);
            var ms    = new MemoryStream(bytes);

            MessagePackBinary.ReadArrayHeader(ms).Is(7);
            MessagePackBinary.ReadInt32(ms).Is(100);
            MessagePackBinary.ReadByte(ms).Is((byte)ByteEnum.C);
            MessagePackBinary.ReadString(ms).Is("abcde");

            MessagePackBinary.ReadMapHeader(ms).Is(3);
            MessagePackBinary.ReadString(ms).Is("Prop1");
            MessagePackBinary.ReadInt32(ms).Is(99999);
            MessagePackBinary.ReadString(ms).Is("Prop2");
            MessagePackBinary.ReadByte(ms).Is((byte)ByteEnum.E);
            MessagePackBinary.ReadString(ms).Is("Prop3");
            MessagePackBinary.ReadInt32(ms).Is(3);

            MessagePackBinary.ReadArrayHeader(ms).Is(3);
            MessagePackBinary.ReadInt32(ms).Is(100);
            MessagePackBinary.ReadInt32(ms).Is(300);
            MessagePackBinary.ReadBytes(ms).Is(new byte[] { 9, 99, 122 });

            MessagePackBinary.ReadMapHeader(ms).Is(2);
            MessagePackBinary.ReadString(ms).Is("key-X");
            MessagePackBinary.ReadInt32(ms).Is(9999);
            MessagePackBinary.ReadString(ms).Is("key-Y");
            MessagePackBinary.ReadArrayHeader(ms).Is(3);
            MessagePackBinary.ReadInt32(ms).Is(1);
            MessagePackBinary.ReadInt32(ms).Is(10);
            MessagePackBinary.ReadInt32(ms).Is(100);

            MessagePackBinary.ReadBytes(ms).Is(new byte[] { 1, 4, 6 });
        }
예제 #3
0
        public static byte ReadByte(ref ReadOnlyMemory <byte> data)
        {
            var arr = GetArray(data);
            var val = MessagePackBinary.ReadByte(arr.Array, arr.Offset, out var readSize);

            data = data.Slice(readSize);
            return(val);
        }
예제 #4
0
        public byte ReadByte()
        {
            byte value = MessagePackBinary.ReadByte(Buffer, Position, out int readSize);

            Position += readSize;

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

            return(MessagePackBinary.ReadByte(bytes, offset, out readSize));
        }
 public Byte?Deserialize(byte[] bytes, int offset, out int readSize)
 {
     if (MessagePackBinary.IsNil(bytes, offset))
     {
         readSize = 1;
         return(null);
     }
     else
     {
         return(MessagePackBinary.ReadByte(bytes, offset, out readSize));
     }
 }
        public SwimMessage Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            readSize = 0;

            var type = (SwimMessageType)MessagePackBinary.ReadByte(bytes, offset, out var r);

            readSize += r;

            switch (type)
            {
            case SwimMessageType.Ping:
                var pingSeq = MessagePackBinary.ReadUInt64(bytes, offset + readSize, out r);
                readSize += r;

                return(new PingMessage(pingSeq));

            case SwimMessageType.PingReq:
                var pingReqSeq = MessagePackBinary.ReadUInt64(bytes, offset + readSize, out r);
                readSize += r;
                var pingReqDest = DeserializeHost(bytes, offset + readSize, out r);
                readSize += r;

                return(new PingReqMessage(pingReqSeq, pingReqDest));

            case SwimMessageType.Ack:
                var ackSeq = MessagePackBinary.ReadUInt64(bytes, offset + readSize, out r);
                readSize += r;

                return(new AckMessage(ackSeq));

            case SwimMessageType.Sync:
                var syncMember = DeserializeMember(bytes, offset + readSize, out r);
                readSize += r;

                return(new SyncMessage(syncMember));

            case SwimMessageType.Update:
                var updateMember = DeserializeMember(bytes, offset + readSize, out r);
                readSize += r;

                return(new UpdateMessage(updateMember));

            default: throw new ArgumentOutOfRangeException();
            }
        }
예제 #8
0
        public global::FTServer.ClientInstance.Packet.IPacket 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 __OperationCode__ = default(byte);
            var __Parameters__    = default(global::System.Collections.Generic.Dictionary <byte, object>);

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

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

                case 1:
                    __Parameters__ = formatterResolver.GetFormatterWithVerify <global::System.Collections.Generic.Dictionary <byte, object> >().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::FTServer.ClientInstance.Packet.IPacket(__OperationCode__, __Parameters__);

            ____result.OperationCode = __OperationCode__;
            ____result.Parameters    = __Parameters__;
            return(____result);
        }
예제 #9
0
        /// <summary>
        /// Deserializes the content into the specified response.
        /// </summary>
        /// <param name="bytes">The bytes.</param>
        /// <param name="offset">The offset.</param>
        /// <param name="readSize">Size of the read.</param>
        /// <returns></returns>
        /// <exception cref="InvalidOperationException">Content format invalid.</exception>
        public static HttpContent Deserialize(byte[] bytes, int offset, out int readSize)
        {
            if (MessagePackBinary.IsNil(bytes, offset))
            {
                readSize = 1;
                return(null);
            }

            int startOffset = offset;

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

            offset += readSize;
            if (count != 3)
            {
                throw new InvalidOperationException("Content format invalid.");
            }

            // Type
            byte typeByte = MessagePackBinary.ReadByte(bytes, offset, out readSize);

            offset += readSize;

            // Headers
            Dictionary <string, string[]> headers = new Dictionary <string, string[]>(StringComparer.OrdinalIgnoreCase);

            if (MessagePackBinary.IsNil(bytes, offset))
            {
                readSize++;
            }
            else
            {
                int headersCount = MessagePackBinary.ReadMapHeader(bytes, offset, out readSize);
                offset += readSize;

                if (headersCount > 0)
                {
                    while (headersCount-- > 0)
                    {
                        string key = MessagePackBinary.ReadString(bytes, offset, out readSize);
                        offset += readSize;


                        int valuesCount = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
                        offset += readSize;

                        string[] values = new string[valuesCount];

                        // Note, we can't have an empty array, and if we tried adding one it would be ignored anyway
                        // therefore it's safe to just start looping and call add for each entry.
                        for (int i = 0; i < valuesCount; i++)
                        {
                            string headerValue = MessagePackBinary.ReadString(bytes, offset, out readSize);
                            offset += readSize;

                            values[i] = headerValue;
                        }

                        headers[key] = values;
                    }
                }
            }

            // Get data
            byte[]      data = MessagePackBinary.ReadBytes(bytes, offset, out readSize);
            HttpContent content;

            switch (typeByte)
            {
            // StreamContent
            case 1:
#pragma warning disable DF0000 // Marks undisposed anonymous objects from object creations.
                content = new StreamContent(new MemoryStream(data));
#pragma warning restore DF0000 // Marks undisposed anonymous objects from object creations.
                break;

            /*
             *  TODO
             * case 2:
             *  // See https://github.com/dotnet/corefx/blob/master/src/System.Net.Http/src/System/Net/Http/FormUrlEncodedContent.cs
             *  string encoded = Encoding.UTF8.GetString(data).Replace("+", "%20");
             *  List<KeyValuePair<string, string>> nvc = new List<KeyValuePair<string, string>>();
             *  int start = 0;
             *  do
             *  {
             *
             *  } while ()
             *
             *
             *  break;
             */

            case 3:
                // See https://github.com/dotnet/corefx/blob/master/src/System.Net.Http/src/System/Net/Http/StringContent.cs
                // Get encoding from headers if possible
                Encoding encoding;
                string   charSet;
                if (headers.TryGetValue("content-type", out string[] ctValues) &&
예제 #10
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);
        }
        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);
            }
        }
        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);
        }
예제 #13
0
 public global::MessagePack.MessagePackType Deserialize(byte[] bytes, int offset, global::MessagePack.IFormatterResolver formatterResolver, out int readSize)
 {
     return((global::MessagePack.MessagePackType)MessagePackBinary.ReadByte(bytes, offset, out readSize));
 }
 public override byte ReadByteValue()
 {
     return(MessagePackBinary.ReadByte(this.DecoderStream));
 }
예제 #15
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.");
            }
        }
예제 #16
0
 public Byte Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
 {
     return(MessagePackBinary.ReadByte(bytes, offset, out readSize));
 }
예제 #17
0
 public Byte Deserialize(byte[] bytes, int offset, out int readSize)
 {
     return(MessagePackBinary.ReadByte(bytes, offset, out readSize));
 }