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 dtOffsetMinutes = MessagePackBinary.ReadInt16(bytes, offset, out readSize);

                offset += readSize;

                readSize = offset - startOffset;

                return(new DateTimeOffset(utc.Ticks, TimeSpan.FromMinutes(dtOffsetMinutes)));
            }
예제 #2
0
        /// <inheritdoc />
        public Recording Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, 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;
            // NOTE: We can use this to distinguish between future versions of a Recording.
            if (count != 6)
            {
                throw new InvalidOperationException("Request format invalid.");
            }

            string hash = MessagePackBinary.ReadString(bytes, offset, out readSize);

            offset += readSize;

            string keyGeneratorName = MessagePackBinary.ReadString(bytes, offset, out readSize);

            offset += readSize;

            DateTime recordedUtc = MessagePackBinary.ReadDateTime(bytes, offset, out readSize);

            offset += readSize;

            int durationMs = MessagePackBinary.ReadInt32(bytes, offset, out readSize);

            offset += readSize;

            byte[] responseData = MessagePackBinary.ReadBytes(bytes, offset, out readSize);
            offset += readSize;

            byte[] requestData;
            if (MessagePackBinary.IsNil(bytes, offset))
            {
                requestData = null;
                offset++;
            }
            else
            {
                requestData = MessagePackBinary.ReadBytes(bytes, offset, out readSize);
                offset     += readSize;
            }
            readSize = offset - startOffset;

            return(new Recording(
                       hash,
                       keyGeneratorName,
                       recordedUtc,
                       durationMs,
                       responseData,
                       requestData));
        }
예제 #3
0
        public DateTime ReadDateTime()
        {
            DateTime value = MessagePackBinary.ReadDateTime(Buffer, Position, out int readSize);

            Position += readSize;

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

            return(MessagePackBinary.ReadDateTime(bytes, offset, out readSize));
        }
        public LockoutEndDateChanged Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            if (MessagePackBinary.IsNil(bytes, offset))
            {
                readSize = 1;
                return(new LockoutEndDateChanged(null));
            }
            var dt = MessagePackBinary.ReadDateTime(bytes, offset, out readSize);

            return(new LockoutEndDateChanged(new DateTimeOffset(dt)));
        }
예제 #6
0
        public LocalDate Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            var           dt  = MessagePackBinary.ReadDateTime(bytes, offset, out readSize);
            LocalDateTime ldt = LocalDateTime.FromDateTime(dt);

            if (ldt.TimeOfDay != LocalTime.Midnight)
            {
                throw new InvalidOperationException($"code is invalid. code:{bytes[offset]} format:{MessagePackCode.ToFormatName(bytes[offset])}");
            }

            return(LocalDate.FromDateTime(dt));
        }
예제 #7
0
 public DateTime Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
 {
     if (MessagePackBinary.GetMessagePackType(bytes, offset) == MessagePackType.String)
     {
         var str = MessagePackBinary.ReadString(bytes, offset, out readSize);
         return(DateTime.Parse(str, CultureInfo.CurrentCulture));
     }
     else
     {
         return(new DateTime(MessagePackBinary.ReadDateTime(bytes, offset, out readSize).ToLocalTime().Ticks));
     }
 }
예제 #8
0
 public DateTime Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
 {
     if (MessagePackBinary.GetMessagePackType(bytes, offset) == MessagePackType.String)
     {
         var str = MessagePackBinary.ReadString(bytes, offset, out readSize);
         return(DateTime.Parse(str));
     }
     else
     {
         return(MessagePackBinary.ReadDateTime(bytes, offset, out readSize));
     }
 }
 public DateTime?Deserialize(byte[] bytes, int offset, out int readSize)
 {
     if (MessagePackBinary.IsNil(bytes, offset))
     {
         readSize = 1;
         return(null);
     }
     else
     {
         return(MessagePackBinary.ReadDateTime(bytes, offset, out readSize));
     }
 }
예제 #10
0
        public DateTime[] 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 DateTime[len];
                for (int i = 0; i < array.Length; i++)
                {
                    array[i] = MessagePackBinary.ReadDateTime(bytes, offset, out readSize);
                    offset  += readSize;
                }
                readSize = offset - startOffset;
                return(array);
            }
        }
예제 #11
0
 public DateTime Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
 {
     return(MessagePackBinary.ReadDateTime(bytes, offset, out readSize));
 }
예제 #12
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.");
            }
        }
예제 #13
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);
        }
예제 #14
0
        public LocalDateTime Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            var dt = MessagePackBinary.ReadDateTime(bytes, offset, out readSize);

            return(LocalDateTime.FromDateTime(dt));
        }
예제 #15
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);
            }
        }
        public UserCreated <TKey, TUser> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            var keyFormatter   = formatterResolver.GetFormatter <TKey>();
            var claimFormatter = formatterResolver.GetFormatter <Claim>();

            var startOffset = offset;

            var key = keyFormatter.Deserialize(bytes, offset, formatterResolver, out readSize);
            var usr = (TUser)Activator.CreateInstance(typeof(TUser), key);

            offset                  += readSize;
            usr.UserName             = MessagePackBinary.ReadString(bytes, offset, out readSize);
            offset                  += readSize;
            usr.NormalizedUserName   = MessagePackBinary.ReadString(bytes, offset, out readSize);
            offset                  += readSize;
            usr.Email                = MessagePackBinary.ReadString(bytes, offset, out readSize);
            offset                  += readSize;
            usr.NormalizedEmail      = MessagePackBinary.ReadString(bytes, offset, out readSize);
            offset                  += readSize;
            usr.EmailConfirmed       = MessagePackBinary.ReadBoolean(bytes, offset, out readSize);
            offset                  += readSize;
            usr.PasswordHash         = MessagePackBinary.ReadString(bytes, offset, out readSize);
            offset                  += readSize;
            usr.SecurityStamp        = MessagePackBinary.ReadString(bytes, offset, out readSize);
            offset                  += readSize;
            usr.PhoneNumber          = MessagePackBinary.ReadString(bytes, offset, out readSize);
            offset                  += readSize;
            usr.PhoneNumberConfirmed = MessagePackBinary.ReadBoolean(bytes, offset, out readSize);
            offset                  += readSize;
            usr.TwoFactorEnabled     = MessagePackBinary.ReadBoolean(bytes, offset, out readSize);
            offset                  += readSize;
            usr.LockoutEnd           = null;
            if (MessagePackBinary.IsNil(bytes, offset))
            {
                offset++;
            }
            else
            {
                usr.LockoutEnd = MessagePackBinary.ReadDateTime(bytes, offset, out readSize);
                offset        += readSize;
            }
            usr.LockoutEnabled    = MessagePackBinary.ReadBoolean(bytes, offset, out readSize);
            offset               += readSize;
            usr.AccessFailedCount = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
            offset               += readSize;

            usr.Claims = null;
            if (MessagePackBinary.IsNil(bytes, offset))
            {
                offset++;
            }
            else
            {
                var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
                offset    += readSize;
                usr.Claims = new List <Claim>(len);
                for (int i = 0; i < len; i++)
                {
                    var claim = claimFormatter.Deserialize(bytes, offset, formatterResolver, out readSize);
                    offset += readSize;
                    usr.Claims.Add(claim);
                }
            }

            usr.Logins = null;
            if (MessagePackBinary.IsNil(bytes, offset))
            {
                offset++;
            }
            else
            {
                var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
                offset    += readSize;
                usr.Logins = new List <ImmutableUserLoginInfo>(len);
                for (int i = 0; i < len; i++)
                {
                    var loginProvider = MessagePackBinary.ReadString(bytes, offset, out readSize);
                    offset += readSize;
                    var providerKey = MessagePackBinary.ReadString(bytes, offset, out readSize);
                    offset += readSize;
                    var displayName = MessagePackBinary.ReadString(bytes, offset, out readSize);
                    offset += readSize;

                    usr.Logins.Add(new ImmutableUserLoginInfo(loginProvider, providerKey, displayName));
                }
            }

            usr.Tokens = null;
            if (MessagePackBinary.IsNil(bytes, offset))
            {
                offset++;
            }
            else
            {
                var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
                offset    += readSize;
                usr.Tokens = new List <ImmutableIdentityUserToken>(len);
                for (int i = 0; i < len; i++)
                {
                    var loginProvider = MessagePackBinary.ReadString(bytes, offset, out readSize);
                    offset += readSize;
                    var name = MessagePackBinary.ReadString(bytes, offset, out readSize);
                    offset += readSize;
                    var value = MessagePackBinary.ReadString(bytes, offset, out readSize);
                    offset += readSize;

                    usr.Tokens.Add(new ImmutableIdentityUserToken(loginProvider, name, value));
                }
            }
            readSize = offset - startOffset;
            return(new UserCreated <TKey, TUser>(usr));
        }
        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);
        }