Exemple #1
0
        internal static object Deserialize(ref MessagePackReader reader)
        {
            if (reader.TryReadNil())
            {
                return(null);
            }

            MessagePackType type = reader.NextMessagePackType;

            return(type switch
            {
                MessagePackType.Integer =>
                reader.NextCode switch
                {
        public object Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
        {
            MessagePackType    type     = reader.NextMessagePackType;
            IFormatterResolver resolver = options.Resolver;

            switch (type)
            {
            case MessagePackType.Integer:
                var code = reader.NextCode;
                if (code >= MessagePackCode.MinNegativeFixInt && code <= MessagePackCode.MaxNegativeFixInt)
                {
                    return(reader.ReadSByte());
                }
                else if (code >= MessagePackCode.MinFixInt && code <= MessagePackCode.MaxFixInt)
                {
                    return(reader.ReadByte());
                }
                else if (code == MessagePackCode.Int8)
                {
                    return(reader.ReadSByte());
                }
                else if (code == MessagePackCode.Int16)
                {
                    return(reader.ReadInt16());
                }
                else if (code == MessagePackCode.Int32)
                {
                    return(reader.ReadInt32());
                }
                else if (code == MessagePackCode.Int64)
                {
                    return(reader.ReadInt64());
                }
                else if (code == MessagePackCode.UInt8)
                {
                    return(reader.ReadByte());
                }
                else if (code == MessagePackCode.UInt16)
                {
                    return(reader.ReadUInt16());
                }
                else if (code == MessagePackCode.UInt32)
                {
                    return(reader.ReadUInt32());
                }
                else if (code == MessagePackCode.UInt64)
                {
                    return(reader.ReadUInt64());
                }

                throw new MessagePackSerializationException("Invalid primitive bytes.");

            case MessagePackType.Boolean:
                return(reader.ReadBoolean());

            case MessagePackType.Float:
                if (reader.NextCode == MessagePackCode.Float32)
                {
                    return(reader.ReadSingle());
                }
                else
                {
                    return(reader.ReadDouble());
                }

            case MessagePackType.String:
                return(reader.ReadString());

            case MessagePackType.Binary:
                // We must copy the sequence returned by ReadBytes since the reader's sequence is only valid during deserialization.
                return(reader.ReadBytes()?.ToArray());

            case MessagePackType.Extension:
                ExtensionHeader ext = reader.ReadExtensionFormatHeader();
                if (ext.TypeCode == ReservedMessagePackExtensionTypeCode.DateTime)
                {
                    return(reader.ReadDateTime(ext));
                }

                throw new MessagePackSerializationException("Invalid primitive bytes.");

            case MessagePackType.Array:
            {
                var length = reader.ReadArrayHeader();
                if (length == 0)
                {
                    return(Array.Empty <object>());
                }

                IMessagePackFormatter <object> objectFormatter = resolver.GetFormatter <object>();
                var array = new object[length];
                options.Security.DepthStep(ref reader);
                try
                {
                    for (int i = 0; i < length; i++)
                    {
                        array[i] = objectFormatter.Deserialize(ref reader, options);
                    }
                }
                finally
                {
                    reader.Depth--;
                }

                return(array);
            }

            case MessagePackType.Map:
            {
                var length = reader.ReadMapHeader();

                IMessagePackFormatter <object> objectFormatter = resolver.GetFormatter <object>();
                var hash = new Dictionary <object, object>(length, options.Security.GetEqualityComparer <object>());
                options.Security.DepthStep(ref reader);
                try
                {
                    for (int i = 0; i < length; i++)
                    {
                        var key = objectFormatter.Deserialize(ref reader, options);

                        var value = objectFormatter.Deserialize(ref reader, options);

                        hash.Add(key, value);
                    }
                }
                finally
                {
                    reader.Depth--;
                }

                return(hash);
            }

            case MessagePackType.Nil:
                reader.ReadNil();
                return(null);

            default:
                throw new MessagePackSerializationException("Invalid primitive bytes.");
            }
        }
 private void ReadElement()
 {
     mCurrentElementType =
         MessagePackCode.ToMessagePackType((byte)PeekByte());
 }
 public ContainerContext(MessagePackType type)
 {
     Type = type;
 }
        private void ReadType(MessagePackType type)
        {
            int readSize = 0;

            switch (type)
            {
            case MessagePackType.Float:
            {
                double d;

                if (MessagePackCode.Float32 == PeekByte())
                {
                    d = MessagePackBinary.ReadSingle(mStream);
                }
                else
                {
                    d = MessagePackBinary.ReadDouble(mStream);
                }

                if (FloatParseHandling == FloatParseHandling.Decimal)
                {
                    SetToken(JsonToken.Float, Convert.ToDecimal(d, CultureInfo.InvariantCulture));
                }
                else
                {
                    SetToken(JsonToken.Float, d);
                }

                break;
            }

            case MessagePackType.String:
            {
                string str = MessagePackBinary.ReadString(mStream);
                SetToken(JsonToken.String, str);
                break;
            }

            case MessagePackType.Map:
            {
                SetToken(JsonToken.StartObject);

                ContainerContext newContext = new ContainerContext(MessagePackType.Map);
                PushContext(newContext);
                newContext.Length = MessagePackBinary.ReadMapHeader(mStream);
                break;
            }

            case MessagePackType.Array:
            {
                SetToken(JsonToken.StartArray);

                ContainerContext newContext = new ContainerContext(MessagePackType.Array);
                PushContext(newContext);
                newContext.Length = MessagePackBinary.ReadArrayHeader(mStream);
                break;
            }

            case MessagePackType.Binary:
                byte[] data = MessagePackBinary.ReadBytes(mStream);

                SetToken(JsonToken.Bytes, data);
                break;

            case MessagePackType.Unknown:
                SetToken(JsonToken.Undefined);
                break;

            case MessagePackType.Boolean:
                bool b = MessagePackBinary.ReadBoolean(mStream);
                SetToken(JsonToken.Boolean, b);
                break;

            case MessagePackType.Extension:
                long            streamPosition = mStream.Position;
                ExtensionHeader ext            = MessagePackBinary.ReadExtensionFormatHeader(mStream);
                mStream.Position = streamPosition;

                if (ext.TypeCode != ReservedMessagePackExtensionTypeCode.DateTime)
                {
                    throw new InvalidOperationException("Invalid primitive bytes.");
                }

                DateTime dateTime = MessagePackBinary.ReadDateTime(mStream);
                SetToken(JsonToken.Date, dateTime);
                break;

            case MessagePackType.Nil:
                MessagePackBinary.ReadNil(mStream);
                SetToken(JsonToken.Null);
                break;

            case MessagePackType.Integer:
                object integer = ReadInteger();
                SetToken(JsonToken.Integer, integer);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(type), "Unexpected MessagePackType value: " + type);
            }
        }
        public object Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
        {
            MessagePackType    type     = reader.NextMessagePackType;
            IFormatterResolver resolver = options.Resolver;

            switch (type)
            {
            case MessagePackType.Integer:
                var code = reader.NextCode;
                if (code >= MessagePackCode.MinNegativeFixInt && code <= MessagePackCode.MaxNegativeFixInt)
                {
                    return(reader.ReadSByte());
                }
                else if (code >= MessagePackCode.MinFixInt && code <= MessagePackCode.MaxFixInt)
                {
                    return(reader.ReadByte());
                }
                else if (code == MessagePackCode.Int8)
                {
                    return(reader.ReadSByte());
                }
                else if (code == MessagePackCode.Int16)
                {
                    return(reader.ReadInt16());
                }
                else if (code == MessagePackCode.Int32)
                {
                    return(reader.ReadInt32());
                }
                else if (code == MessagePackCode.Int64)
                {
                    return(reader.ReadInt64());
                }
                else if (code == MessagePackCode.UInt8)
                {
                    return(reader.ReadByte());
                }
                else if (code == MessagePackCode.UInt16)
                {
                    return(reader.ReadUInt16());
                }
                else if (code == MessagePackCode.UInt32)
                {
                    return(reader.ReadUInt32());
                }
                else if (code == MessagePackCode.UInt64)
                {
                    return(reader.ReadUInt64());
                }

                throw new InvalidOperationException("Invalid primitive bytes.");

            case MessagePackType.Boolean:
                return(reader.ReadBoolean());

            case MessagePackType.Float:
                if (reader.NextCode == MessagePackCode.Float32)
                {
                    return(reader.ReadSingle());
                }
                else
                {
                    return(reader.ReadDouble());
                }

            case MessagePackType.String:
                return(reader.ReadString());

            case MessagePackType.Binary:
                return(reader.ReadBytes());

            case MessagePackType.Extension:
                ExtensionHeader ext = reader.ReadExtensionFormatHeader();
                if (ext.TypeCode == ReservedMessagePackExtensionTypeCode.DateTime)
                {
                    return(reader.ReadDateTime(ext));
                }

                throw new InvalidOperationException("Invalid primitive bytes.");

            case MessagePackType.Array:
            {
                var length = reader.ReadArrayHeader();

                IMessagePackFormatter <object> objectFormatter = resolver.GetFormatter <object>();
                var array = new object[length];
                for (int i = 0; i < length; i++)
                {
                    array[i] = objectFormatter.Deserialize(ref reader, options);
                }

                return(array);
            }

            case MessagePackType.Map:
            {
                var length = reader.ReadMapHeader();

                IMessagePackFormatter <object> objectFormatter = resolver.GetFormatter <object>();
                var hash = new Dictionary <object, object>(length);
                for (int i = 0; i < length; i++)
                {
                    var key = objectFormatter.Deserialize(ref reader, options);

                    var value = objectFormatter.Deserialize(ref reader, options);

                    hash.Add(key, value);
                }

                return(hash);
            }

            case MessagePackType.Nil:
                reader.ReadNil();
                return(null);

            default:
                throw new InvalidOperationException("Invalid primitive bytes.");
            }
        }