コード例 #1
0
        public override MsgPackItem Read(MsgPackTypeId typeId, Stream data)
        {
            long len;

            if (!IsMasked(MsgPackTypeId.MpMap4, typeId, 0x0F, out len))
            {
                switch (typeId)
                {
                case MsgPackTypeId.MpMap16: len = ReadLen(data, 2); break;

                case MsgPackTypeId.MpMap32: len = ReadLen(data, 4); break;

                default: throw new MsgPackException(string.Concat("MpMap does not support a type ID of ", GetOfficialTypeName(typeId), "."), data.Position - 1, typeId);
                }
            }

            value = new KeyValuePair <object, object> [len];

            for (int t = 0; t < len; t++)
            {
                MsgPackItem key = MsgPackItem.Unpack(data);
                MsgPackItem val = MsgPackItem.Unpack(data);
                value[t] = new KeyValuePair <object, object>(key.Value, val.Value);
            }

            return(this);
        }
コード例 #2
0
        public static T Deserialize <T>(Stream stream)
        {
            Type tType = typeof(T);

            if (MsgPackSerializer.NativelySupportedTypes.Contains(tType))
            {
                return(MsgPackItem.Unpack(stream).GetTypedValue <T>());
            }
            MpMap map    = (MpMap)MsgPackItem.Unpack(stream);
            T     result = (T)Materialize(tType, map);

            return(result);
        }
コード例 #3
0
ファイル: MpMap.cs プロジェクト: rafallange/LsMsgPack
        public override MsgPackItem Read(MsgPackTypeId typeId, Stream data)
        {
            long len;

            if (!IsMasked(MsgPackTypeId.MpMap4, typeId, 0x0F, out len))
            {
                switch (typeId)
                {
                case MsgPackTypeId.MpMap16: len = ReadLen(data, 2); break;

                case MsgPackTypeId.MpMap32: len = ReadLen(data, 4); break;

                default: throw new MsgPackException(string.Concat("MpMap does not support a type ID of ", GetOfficialTypeName(typeId), "."), data.Position - 1, typeId);
                }
            }

            value = new KeyValuePair <object, object> [len];

            packedItems = new KeyValuePair <MsgPackItem, MsgPackItem> [len];
            bool errorOccurred = false;

            for (int t = 0; t < len; t++)
            {
                MsgPackItem key = MsgPackItem.Unpack(data, _settings);
                MsgPackItem val;
                if (key is MpError)
                {
                    if (_settings.ContinueProcessingOnBreakingError)
                    {
                        _settings.FileContainsErrors = true;
                        errorOccurred = true;
                        if (data.Position >= data.Length)
                        {
                            val = new MpNull(_settings);
                        }
                        else
                        {
                            val = MsgPackItem.Unpack(data, _settings);
                        }
                    }
                    else
                    {
                        val = new MpNull(_settings);
                    }
                }
                else
                {
                    val = MsgPackItem.Unpack(data, _settings);
                }
                if (_settings._preservePackages)
                {
                    packedItems[t] = new KeyValuePair <MsgPackItem, MsgPackItem>(key, val);
                }
                value[t] = new KeyValuePair <object, object>(key.Value, val.Value);
                if (!_settings.ContinueProcessingOnBreakingError && (key is MpError || val is MpError))
                {
                    return(new MpError(_settings, this));
                }
                if (val is MpError)
                {
                    _settings.FileContainsErrors = true;
                    errorOccurred = true;
                    if (data.Position >= data.Length)
                    {
                        return(new MpError(_settings, this));
                    }
                }
            }
            if (errorOccurred)
            {
                return(new MpError(_settings, this));
            }
            return(this);
        }