Exemple #1
0
 public int Serialize(ref byte[] bytes, int offset, Instant value, IFormatterResolver formatterResolver)
 {
     return(MessagePackBinary.WriteDateTime(ref bytes, offset, value.ToDateTimeUtc()));
 }
Exemple #2
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.");
            }
        }
 public int Serialize(ref byte[] bytes, int offset, UInt32 value, IFormatterResolver formatterResolver)
 {
     return(MessagePackBinary.WriteUInt32ForceUInt32Block(ref bytes, offset, value));
 }
 public int Serialize(ref byte[] bytes, int offset, int value, IFormatterResolver formatterResolver)
 {
     return(MessagePackBinary.WriteInt32(ref bytes, offset, value * 10));
 }
 public int Serialize(ref byte[] bytes, int offset, string value, IFormatterResolver formatterResolver)
 {
     return(MessagePackBinary.WriteString(ref bytes, offset, value + value));
 }
 public int Serialize(ref byte[] bytes, int offset, TimeSpan value, IFormatterResolver formatterResolver)
 {
     return(MessagePackBinary.WriteInt64(ref bytes, offset, value.Ticks));
 }
        public Guid Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            var segment = MessagePackBinary.ReadStringSegment(bytes, offset, out readSize);

            return(new GuidBits(segment).Value);
        }
 private static void WriteMetric(ref byte[] bytes, ref int offset, string key, double value)
 {
     offset += MessagePackBinary.WriteString(ref bytes, offset, key);
     offset += MessagePackBinary.WriteDouble(ref bytes, offset, value);
 }
Exemple #9
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));
        }
Exemple #10
0
        private int SerializeInternal(ref byte[] bytes, int offset, object value, IFormatterResolver formatterResolver)
        {
            var startOffset = offset;

            if (value == null)
            {
                offset += NilFormatter.Instance.Serialize(ref bytes, offset, Nil.Default, formatterResolver);
            }
            else
            {
                var type     = value.GetType();
                var typeInfo = type.GetTypeInfo();

                if (value is DateTime dateTime)
                {
                    offset += UnixTimestampDateTimeFormatter.Instance.Serialize(ref bytes, offset, dateTime, formatterResolver);
                }
                else if (value is DateTime?) // Nullable-type can't use pattern-matching.
                {
                    offset += _nullableDateTimeFormatter.Serialize(ref bytes, offset, (DateTime?)value, formatterResolver);
                }
                else if (value is DateTime[] dateTimes)
                {
                    offset += _dateTimeArrayFormatter.Serialize(ref bytes, offset, dateTimes, formatterResolver);
                }
                else if (value is DateTimeOffset dateTimeOffset)
                {
                    offset += UnixTimestampDateTimeOffsetFormatter.Instance.Serialize(ref bytes, offset, dateTimeOffset, formatterResolver);
                }
                else if (value is DateTimeOffset?) // Nullable-type can't use pattern-matching.
                {
                    offset += _nullableDateTimeOffsetFormatter.Serialize(ref bytes, offset, (DateTimeOffset?)value, formatterResolver);
                }
                else if (value is DateTimeOffset[] dateTimeOffsets)
                {
                    offset += _dateTimeOffsetArrayFormatter.Serialize(ref bytes, offset, dateTimeOffsets, formatterResolver);
                }
                else if (value is IDictionary <string, object> dictionary) // before PrimitiveObjectFormatter
                {
                    offset += MessagePackBinary.WriteMapHeader(ref bytes, offset, dictionary.Count);

                    foreach (var item in dictionary)
                    {
                        offset += MessagePackBinary.WriteString(ref bytes, offset, item.Key);
                        offset += SerializeInternal(ref bytes, offset, item.Value, formatterResolver);
                    }
                }
                else if (PrimitiveObjectFormatter.IsSupportedType(type, typeInfo, value))
                {
                    offset += PrimitiveObjectFormatter.Instance.Serialize(ref bytes, offset, value, formatterResolver);
                }
                else if (typeInfo.IsAnonymous())
                {
                    var properties = type.GetProperties(); // heavy

                    dictionary = properties.ToDictionary(x => x.Name, x => x.GetValue(value));

                    offset += MessagePackBinary.WriteMapHeader(ref bytes, offset, dictionary.Count);

                    foreach (var item in dictionary)
                    {
                        offset += MessagePackBinary.WriteString(ref bytes, offset, item.Key);
                        offset += SerializeInternal(ref bytes, offset, item.Value, formatterResolver);
                    }
                }
                else
                {
                    offset += DynamicObjectTypeFallbackFormatter.Instance.Serialize(ref bytes, offset, value, formatterResolver);
                }
            }

            return(offset - startOffset);
        }
 private static void WriteTag(ref byte[] bytes, ref int offset, string key, string value)
 {
     offset += MessagePackBinary.WriteString(ref bytes, offset, key);
     offset += MessagePackBinary.WriteString(ref bytes, offset, value);
 }
Exemple #12
0
        public override int ExportBinary(ref byte[] bytes, int offset)
        {
            int length = MessagePackBinary.WriteInt64(ref bytes, offset, Value);

            return(length);
        }
Exemple #13
0
 int IInternalMemory.Serialize(ref byte[] bytes, int offset, IFormatterResolver resolver)
 {
     MessagePackBinary.EnsureCapacity(ref bytes, offset, RawMemory.Length);
     Buffer.BlockCopy(RawMemory, 0, bytes, offset, RawMemory.Length);
     return(RawMemory.Length);
 }
        public static byte[] ToLZ4Binary(ArraySegment <byte> messagePackBinary)
        {
            var buffer = ToLZ4BinaryCore(messagePackBinary);

            return(MessagePackBinary.FastCloneWithResize(buffer.Array, buffer.Count));
        }
 public int Serialize(ref byte[] bytes, int offset, decimal value, IFormatterResolver formatterResolver)
 {
     return(MessagePackBinary.WriteString(ref bytes, offset, value.ToString(CultureInfo.InvariantCulture)));
 }
Exemple #16
0
 public int Serialize(ref byte[] bytes, int offset, LocalDateTime value, IFormatterResolver formatterResolver)
 {
     return(MessagePackBinary.WriteDateTime(ref bytes, offset, DateTime.SpecifyKind(value.ToDateTimeUnspecified(), DateTimeKind.Utc)));
 }
 public decimal Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
 {
     return(decimal.Parse(MessagePackBinary.ReadString(bytes, offset, out readSize), CultureInfo.InvariantCulture));
 }
Exemple #18
0
        public int Serialize(ref byte[] bytes, int offset, object value, IFormatterResolver formatterResolver)
        {
            if (value == null)
            {
                return(MessagePackBinary.WriteNil(ref bytes, offset));
            }

            var type = value.GetType();

            byte[] typeName;
            if (!typeNameCache.TryGetValue(type, out typeName))
            {
                if (blacklistCheck.Contains(type.FullName))
                {
                    throw new InvalidOperationException("Type is in blacklist:" + type.FullName);
                }

                var ti = type.GetTypeInfo();
                if (ti.IsAnonymous() || useBuiltinTypes.Contains(type))
                {
                    typeName = null;
                }
                else
                {
                    typeName = StringEncoding.UTF8.GetBytes(BuildTypeName(type));
                }
                typeNameCache.TryAdd(type, typeName);
            }

            if (typeName == null)
            {
                return(Resolvers.TypelessFormatterFallbackResolver.Instance.GetFormatter <object>().Serialize(ref bytes, offset, value, formatterResolver));
            }

            // don't use GetOrAdd for avoid closure capture.
            KeyValuePair <object, SerializeMethod> formatterAndDelegate;

            if (!serializers.TryGetValue(type, out formatterAndDelegate))
            {
                lock (serializers) // double check locking...
                {
                    if (!serializers.TryGetValue(type, out formatterAndDelegate))
                    {
                        var ti = type.GetTypeInfo();

                        var formatter = formatterResolver.GetFormatterDynamic(type);
                        if (formatter == null)
                        {
                            throw new FormatterNotRegisteredException(type.FullName + " is not registered in this resolver. resolver:" + formatterResolver.GetType().Name);
                        }

                        var formatterType = typeof(IMessagePackFormatter <>).MakeGenericType(type);
                        var param0        = Expression.Parameter(typeof(object), "formatter");
                        var param1        = Expression.Parameter(typeof(byte[]).MakeByRefType(), "bytes");
                        var param2        = Expression.Parameter(typeof(int), "offset");
                        var param3        = Expression.Parameter(typeof(object), "value");
                        var param4        = Expression.Parameter(typeof(IFormatterResolver), "formatterResolver");

                        var serializeMethodInfo = formatterType.GetRuntimeMethod("Serialize", new[] { typeof(byte[]).MakeByRefType(), typeof(int), type, typeof(IFormatterResolver) });

                        var body = Expression.Call(
                            Expression.Convert(param0, formatterType),
                            serializeMethodInfo,
                            param1,
                            param2,
                            ti.IsValueType ? Expression.Unbox(param3, type) : Expression.Convert(param3, type),
                            param4);

                        var lambda = Expression.Lambda <SerializeMethod>(body, param0, param1, param2, param3, param4).Compile();

                        formatterAndDelegate = new KeyValuePair <object, SerializeMethod>(formatter, lambda);
                        serializers.TryAdd(type, formatterAndDelegate);
                    }
                }
            }

            // mark as extension with code 100
            var startOffset = offset;

            offset += 6; // mark will be written at the end, when size is known
            offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, typeName);
            offset += formatterAndDelegate.Value(formatterAndDelegate.Key, ref bytes, offset, value, formatterResolver);
            MessagePackBinary.WriteExtensionFormatHeaderForceExt32Block(ref bytes, startOffset, (sbyte)TypelessFormatter.ExtensionTypeCode, offset - startOffset - 6);
            return(offset - startOffset);
        }
 public TimeSpan Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
 {
     return(new TimeSpan(MessagePackBinary.ReadInt64(bytes, offset, out readSize)));
 }
        public int Serialize(ref byte[] bytes, int offset, TCollection value, IFormatterResolver formatterResolver)
        {
            if (value == null)
            {
                return(MessagePackBinary.WriteNil(ref bytes, offset));
            }
            else
            {
                // Optimize iteration(array is fastest)
                var array = value as TElement[];
                if (array != null)
                {
                    var startOffset = offset;
                    var formatter   = formatterResolver.GetFormatterWithVerify <TElement>();

                    offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, array.Length);

                    foreach (var item in array)
                    {
                        offset += formatter.Serialize(ref bytes, offset, item, formatterResolver);
                    }

                    return(offset - startOffset);
                }
                else
                {
                    var startOffset = offset;
                    var formatter   = formatterResolver.GetFormatterWithVerify <TElement>();

                    // knows count or not.
                    var seqCount = GetCount(value);
                    if (seqCount != null)
                    {
                        offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, seqCount.Value);

                        // Unity's foreach struct enumerator causes boxing so iterate manually.
                        var e = GetSourceEnumerator(value);
                        try
                        {
                            while (e.MoveNext())
                            {
#if NETSTANDARD1_4
                                offset += formatter.Serialize(ref bytes, offset, e.Current, formatterResolver);
#else
                                offset += formatter.Serialize(ref bytes, (int)offset, (TElement)e.Current, (IFormatterResolver)formatterResolver);
#endif
                            }
                        }
                        finally
                        {
                            e.Dispose();
                        }

                        return(offset - startOffset);
                    }
                    else
                    {
                        // write message first -> open header space -> write header
                        var writeStarOffset = offset;

                        var count     = 0;
                        var moveCount = 0;

                        // count = 16 <= 65535, header len is "3" so choose default space.
                        offset += 3;

                        var e = GetSourceEnumerator(value);
                        try
                        {
                            while (e.MoveNext())
                            {
                                count++;
#if NETSTANDARD1_4
                                var writeSize = formatter.Serialize(ref bytes, offset, e.Current, formatterResolver);
#else
                                var writeSize = formatter.Serialize(ref bytes, (int)offset, (TElement)e.Current, (IFormatterResolver)formatterResolver);
#endif
                                moveCount += writeSize;
                                offset    += writeSize;
                            }
                        }
                        finally
                        {
                            e.Dispose();
                        }

                        var headerLength = MessagePackBinary.GetArrayHeaderLength(count);
                        if (headerLength != 3)
                        {
                            if (headerLength == 1)
                            {
                                offset -= 2;                    // 1
                            }
                            else
                            {
                                offset += 2;  // 5
                            }
                            MessagePackBinary.EnsureCapacity(ref bytes, offset, headerLength);
                            Buffer.BlockCopy(bytes, writeStarOffset + 3, bytes, writeStarOffset + headerLength, moveCount);
                        }
                        MessagePackBinary.WriteArrayHeader(ref bytes, writeStarOffset, count);

                        return(offset - startOffset);
                    }
                }
            }
        }
 public String Deserialize(byte[] bytes, int offset, IFormatterResolver typeResolver, out int readSize)
 {
     return(MessagePackBinary.ReadString(bytes, offset, out readSize));
 }
Exemple #22
0
 public TEnum Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
 {
     if (MessagePackBinary.IsNil(bytes, offset))
     {
         readSize = 1;
         return(default);
            public string Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
            {
                var s = MessagePackBinary.ReadString(bytes, offset, out readSize);

                return(s + s);
            }
Exemple #24
0
        public DynamicArgumentTuple <T1, T2, T3, T4, T5, T6, T7, T8, T9> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            var startOffset = offset;

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

            offset += readSize;

            var item1 = default1;
            var item2 = default2;
            var item3 = default3;
            var item4 = default4;
            var item5 = default5;
            var item6 = default6;
            var item7 = default7;
            var item8 = default8;
            var item9 = default9;

            for (var i = 0; i < length; i++)
            {
                switch (i)
                {
                case 0:
                    item1 = formatterResolver.GetFormatterWithVerify <T1>().Deserialize(bytes, offset, formatterResolver, out readSize);
                    break;

                case 1:
                    item2 = formatterResolver.GetFormatterWithVerify <T2>().Deserialize(bytes, offset, formatterResolver, out readSize);
                    break;

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

                case 3:
                    item4 = formatterResolver.GetFormatterWithVerify <T4>().Deserialize(bytes, offset, formatterResolver, out readSize);
                    break;

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

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

                case 6:
                    item7 = formatterResolver.GetFormatterWithVerify <T7>().Deserialize(bytes, offset, formatterResolver, out readSize);
                    break;

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

                case 8:
                    item9 = formatterResolver.GetFormatterWithVerify <T9>().Deserialize(bytes, offset, formatterResolver, out readSize);
                    break;

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

                offset += readSize;
            }

            readSize = offset - startOffset;
            return(new DynamicArgumentTuple <T1, T2, T3, T4, T5, T6, T7, T8, T9>(item1, item2, item3, item4, item5, item6, item7, item8, item9));
        }
Exemple #25
0
        /// <inheritdoc cref="IMessagePackFormatter{T}.Serialize(ref byte[], int, T, IFormatterResolver)" />
        public int Serialize(ref byte[] bytes, int offset, DateTimeOffset value, IFormatterResolver formatterResolver)
        {
            var unixTimestamp = value.GetUnixTimestamp().TotalSeconds;

            return(MessagePackBinary.WriteDouble(ref bytes, offset, unixTimestamp));
        }
Exemple #26
0
        public unsafe Dup 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.ReadMapHeader(bytes, offset, out readSize);

            offset += readSize;


            int aBCDEFGH     = 0;
            int aBCDEFGHIJKL = 0;
            int aBCDEFGHIJKO = 0;

            // ---isStringKey

            ulong key;
            ArraySegment <byte> arraySegment;
            byte *p;
            int   rest;

            fixed(byte *buffer = &bytes[0])
            {
                for (int i = 0; i < len; i++)
                {
                    arraySegment = MessagePackBinary.ReadStringSegment(bytes, offset, out readSize);
                    offset      += readSize;

                    p    = buffer + arraySegment.Offset;
                    rest = arraySegment.Count;

                    if (rest == 0)
                    {
                        goto LOOP_END;
                    }

                    key = AutomataKeyGen.GetKey(ref p, ref rest);
                    if (rest == 0)
                    {
                        if (key == 5208208757389214273L)
                        {
                            aBCDEFGH = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
                            goto LOOP_END;
                        }

                        goto READ_NEXT;
                    }

                    if (key == 5208208757389214273L)
                    {
                        key = AutomataKeyGen.GetKey(ref p, ref rest);
                        if (rest == 0)
                        {
                            if (key == 1280002633L)
                            {
                                aBCDEFGHIJKL = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
                                goto LOOP_END;
                            }

                            if (key == 1330334281L)
                            {
                                aBCDEFGHIJKO = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
                                goto LOOP_END;
                            }
                        }
                    }

READ_NEXT:
                    readSize = MessagePackBinary.ReadNextBlock(bytes, offset);

LOOP_END:
                    offset += readSize;
                    continue;
                }
            }

            // --- end

            return(new Dup
            {
                ABCDEFGH = aBCDEFGH,
                ABCDEFGHIJKL = aBCDEFGHIJKL,
                ABCDEFGHIJKO = aBCDEFGHIJKO
            });
        }
Exemple #27
0
        public int Serialize(ref byte[] bytes, int offset, object value, IFormatterResolver formatterResolver)
        {
            if (value == null)
            {
                return(MessagePackBinary.WriteNil(ref bytes, offset));
            }

            var t = value.GetType();

            int code;

            if (typeToJumpCode.TryGetValue(t, out code))
            {
                switch (code)
                {
                case 0:
                    return(MessagePackBinary.WriteBoolean(ref bytes, offset, (bool)value));

                case 1:
                    return(MessagePackBinary.WriteChar(ref bytes, offset, (char)value));

                case 2:
                    return(MessagePackBinary.WriteSByteForceSByteBlock(ref bytes, offset, (sbyte)value));

                case 3:
                    return(MessagePackBinary.WriteByteForceByteBlock(ref bytes, offset, (byte)value));

                case 4:
                    return(MessagePackBinary.WriteInt16ForceInt16Block(ref bytes, offset, (Int16)value));

                case 5:
                    return(MessagePackBinary.WriteUInt16ForceUInt16Block(ref bytes, offset, (UInt16)value));

                case 6:
                    return(MessagePackBinary.WriteInt32ForceInt32Block(ref bytes, offset, (Int32)value));

                case 7:
                    return(MessagePackBinary.WriteUInt32ForceUInt32Block(ref bytes, offset, (UInt32)value));

                case 8:
                    return(MessagePackBinary.WriteInt64ForceInt64Block(ref bytes, offset, (Int64)value));

                case 9:
                    return(MessagePackBinary.WriteUInt64ForceUInt64Block(ref bytes, offset, (UInt64)value));

                case 10:
                    return(MessagePackBinary.WriteSingle(ref bytes, offset, (Single)value));

                case 11:
                    return(MessagePackBinary.WriteDouble(ref bytes, offset, (double)value));

                case 12:
                    return(MessagePackBinary.WriteDateTime(ref bytes, offset, (DateTime)value));

                case 13:
                    return(MessagePackBinary.WriteString(ref bytes, offset, (string)value));

                case 14:
                    return(MessagePackBinary.WriteBytes(ref bytes, offset, (byte[])value));

                default:
                    throw new InvalidOperationException("Not supported primitive object resolver. type:" + t.Name);
                }
            }
            else
            {
#if UNITY_WSA && !NETFX_CORE
                if (t.IsEnum)
#else
                if (t.GetTypeInfo().IsEnum)
#endif
                {
                    var underlyingType = Enum.GetUnderlyingType(t);
                    var code2          = typeToJumpCode[underlyingType];
                    switch (code2)
                    {
                    case 2:
                        return(MessagePackBinary.WriteSByteForceSByteBlock(ref bytes, offset, (sbyte)value));

                    case 3:
                        return(MessagePackBinary.WriteByteForceByteBlock(ref bytes, offset, (byte)value));

                    case 4:
                        return(MessagePackBinary.WriteInt16ForceInt16Block(ref bytes, offset, (Int16)value));

                    case 5:
                        return(MessagePackBinary.WriteUInt16ForceUInt16Block(ref bytes, offset, (UInt16)value));

                    case 6:
                        return(MessagePackBinary.WriteInt32ForceInt32Block(ref bytes, offset, (Int32)value));

                    case 7:
                        return(MessagePackBinary.WriteUInt32ForceUInt32Block(ref bytes, offset, (UInt32)value));

                    case 8:
                        return(MessagePackBinary.WriteInt64ForceInt64Block(ref bytes, offset, (Int64)value));

                    case 9:
                        return(MessagePackBinary.WriteUInt64ForceUInt64Block(ref bytes, offset, (UInt64)value));

                    default:
                        break;
                    }
                }
                else if (value is System.Collections.IDictionary) // check IDictionary first
                {
                    var d           = value as System.Collections.IDictionary;
                    var startOffset = offset;
                    offset += MessagePackBinary.WriteMapHeader(ref bytes, offset, d.Count);
                    foreach (System.Collections.DictionaryEntry item in d)
                    {
                        offset += Serialize(ref bytes, offset, item.Key, formatterResolver);
                        offset += Serialize(ref bytes, offset, item.Value, formatterResolver);
                    }
                    return(offset - startOffset);
                }
                else if (value is System.Collections.ICollection)
                {
                    var c           = value as System.Collections.ICollection;
                    var startOffset = offset;
                    offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, c.Count);
                    foreach (var item in c)
                    {
                        offset += Serialize(ref bytes, offset, item, formatterResolver);
                    }
                    return(offset - startOffset);
                }
            }

            throw new InvalidOperationException("Not supported primitive object resolver. type:" + t.Name);
        }
Exemple #28
0
        /// <summary>
        /// Open the database.
        /// </summary>
        public static Database Open(byte[] bytes)
        {
            var offset = 0;
            int readSize;
            var memoryCount = MessagePackBinary.ReadArrayHeader(bytes, 0, out readSize);

            offset += readSize;

            var memories = new KeyValuePair <string, IInternalMemory> [memoryCount];

            for (int i = 0; i < memoryCount; i++)
            {
                var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
                offset += readSize;

                if (len != 2)
                {
                    throw new InvalidOperationException("Invalid MsgPack Binary of Database.");
                }

                var keyName = MessagePackBinary.ReadString(bytes, offset, out readSize);
                offset += readSize;

                // is LZ4 or plain msgpack?
                byte[] rawData;
                var    type = MessagePackBinary.GetMessagePackType(bytes, offset);
                if (type == MessagePackType.Extension)
                {
                    var extensionOffset = offset;
                    var ext             = MessagePackBinary.ReadExtensionFormatHeader(bytes, offset, out readSize);
                    if (ext.TypeCode == LZ4MessagePackSerializer.ExtensionTypeCode)
                    {
                        offset += readSize;
                        offset += (int)ext.Length;

                        rawData = new byte[offset - extensionOffset];
                        Buffer.BlockCopy(bytes, extensionOffset, rawData, 0, rawData.Length);
                        goto END;
                    }
                }

                {
                    var beginOffset = offset;
                    var arrayLen    = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
                    offset += readSize;
                    for (int j = 0; j < arrayLen; j++)
                    {
                        offset += MessagePackBinary.ReadNextBlock(bytes, offset);
                    }
                    readSize = offset - beginOffset;
                    rawData  = new byte[readSize];
                    Buffer.BlockCopy(bytes, beginOffset, rawData, 0, readSize);
                }

END:
                var memory  = new InternalRawMemory(rawData);
                memories[i] = new KeyValuePair <string, IInternalMemory>(keyName, memory);
            }

            return(new Database(memories));
        }
 public UInt32 Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
 {
     return(MessagePackBinary.ReadUInt32(bytes, offset, out readSize));
 }
        public int Serialize(ref byte[] bytes, int offset, DateTime value, IFormatterResolver formatterResolver)
        {
            var dateData = value.ToBinary();

            return(MessagePackBinary.WriteInt64(ref bytes, offset, dateData));
        }