public Result<T,TE> Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
        {
            if (reader.IsNil)
                return default;

            var count = reader.ReadArrayHeader();
            if (count != HeaderCount)
                throw new InvalidOperationException();

            if (!reader.ReadBoolean())
            {
                reader.ReadNil();
                
                var formatter = options.Resolver.GetFormatterWithVerify<T>();
                var value = formatter.Deserialize(ref reader, options);

                return Result.Success<T,TE>(value);
            }
            else
            {
                var formatter = options.Resolver.GetFormatterWithVerify<TE>();
                var error = formatter.Deserialize(ref reader, options);
                return Result.Failure<T,TE>(error);
            }
        }
            private static (FrameHeader Header, ReadOnlySequence <byte> Payload) DeserializeFrame(ReadOnlySequence <byte> frameSequence)
            {
                var reader             = new MessagePackReader(frameSequence);
                int headerElementCount = reader.ReadArrayHeader();
                var header             = default(FrameHeader);

                if (headerElementCount < 1)
                {
                    throw new MultiplexingProtocolException("Not enough elements in frame header.");
                }

                header.Code = (ControlCode)reader.ReadInt32();
                if (headerElementCount > 1)
                {
                    if (reader.IsNil)
                    {
                        reader.ReadNil();
                    }
                    else
                    {
                        header.ChannelId = reader.ReadInt32();
                    }

                    if (headerElementCount > 2)
                    {
                        var payload = reader.ReadBytes() ?? default;
                        return(header, payload);
                    }
                }

                return(header, default);
            }
Пример #3
0
 public static int?ReadNullableInt(ref MessagePackReader reader)
 {
     if (!reader.IsNil)
     {
         return(reader.ReadInt32());
     }
     else
     {
         reader.ReadNil();
         return(null);
     }
 }
 public T?Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
 {
     if (reader.IsNil)
     {
         reader.ReadNil();
         return(null);
     }
     else
     {
         return(resolver.GetFormatterWithVerify <T>().Deserialize(ref reader, resolver));
     }
 }
 public T?Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
 {
     if (reader.IsNil)
     {
         reader.ReadNil();
         return(null);
     }
     else
     {
         return(options.Resolver.GetFormatterWithVerify <T>().Deserialize(ref reader, options));
     }
 }
Пример #6
0
 public static bool?ReadNullableBool(ref MessagePackReader reader)
 {
     if (!reader.IsNil)
     {
         return(reader.ReadBoolean());
     }
     else
     {
         reader.ReadNil();
         return(null);
     }
 }
Пример #7
0
 public ConnectionAckTypeData Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
 {
     if (reader.NextMessagePackType.Equals(MessagePackType.Array))
     {
         return(new ConnectionAckTypeData(
                    MessagePackSerializer.Deserialize <ConnectionData>(ref reader, options)));
     }
     else if (reader.NextMessagePackType.Equals(MessagePackType.Map))
     {
         return(new ConnectionAckTypeData(MessagePackSerializer.Deserialize <Dictionary <int, string> >(ref reader)));
     }
     else
     {
         reader.ReadNil();
         return(null);
     }
 }
Пример #8
0
        private static string ReadString(ref MessagePackReader reader, string field)
        {
            if (reader.End)
            {
                ThrowInvalidDataException(field, "String");
            }

            if (reader.IsNil)
            {
                reader.ReadNil();
                return(null);
            }
            else if (reader.NextMessagePackType == MessagePackType.String)
            {
                return(reader.ReadString());
            }

            ThrowInvalidDataException(field, "String");
            return(null); //This should never be reached.
        }
Пример #9
0
        public void NilTest()
        {
            (MemoryStream stream, MsgPack.Packer packer) = this.CreateReferencePacker();

            var sequence = new Sequence <byte>();
            var writer   = new MessagePackWriter(sequence);

            writer.WriteNil();
            writer.Flush();
            sequence.Length.Is(1);

            packer.PackNull().Position.Is(sequence.Length);
            stream.ToArray().SequenceEqual(sequence.AsReadOnlySequence.ToArray()).IsTrue();

            var sequenceReader = new MessagePackReader(sequence.AsReadOnlySequence);

            sequenceReader.ReadNil().Is(Nil.Default);
            sequenceReader.End.IsTrue();

            this.CreateUnpackedReference(sequence).IsNil.IsTrue();
        }
            protected virtual (FrameHeader Header, ReadOnlySequence <byte> Payload) DeserializeFrame(ReadOnlySequence <byte> frameSequence)
            {
                var reader             = new MessagePackReader(frameSequence);
                int headerElementCount = reader.ReadArrayHeader();

                if (headerElementCount < 1)
                {
                    throw new MultiplexingProtocolException("Not enough elements in frame header.");
                }

                FrameHeader header;
                var         code      = (ControlCode)reader.ReadInt32();
                ulong?      channelId = null;

                if (headerElementCount > 1)
                {
                    if (reader.IsNil)
                    {
                        reader.ReadNil();
                    }
                    else
                    {
                        channelId = reader.ReadUInt64();
                    }

                    if (headerElementCount > 2)
                    {
                        var payload = reader.ReadBytes() ?? default;
                        header = this.CreateFrameHeader(code, channelId, null);
                        return(header, payload);
                    }
                }

                header = this.CreateFrameHeader(code, channelId, null);
                return(header, default);
            }
 public Nil?Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
 {
     return(reader.ReadNil());
 }
Пример #12
0
 public Nil?Deserialize(ref MessagePackReader reader, IFormatterResolver typeResolver)
 {
     return(reader.ReadNil());
 }
        public object Deserialize(ref MessagePackReader reader, IFormatterResolver resolver)
        {
            var type = reader.NextMessagePackType;

            switch (type)
            {
            case MessagePackType.Integer:
                var code = reader.NextCode;
                if (MessagePackCode.MinNegativeFixInt <= code && code <= MessagePackCode.MaxNegativeFixInt)
                {
                    return(reader.ReadSByte());
                }
                else if (MessagePackCode.MinFixInt <= code && 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 (MessagePackCode.Float32 == reader.NextCode)
                {
                    return(reader.ReadSingle());
                }
                else
                {
                    return(reader.ReadDouble());
                }

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

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

            case MessagePackType.Extension:
                var 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();

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

                return(array);
            }

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

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

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

                    hash.Add(key, value);
                }

                return(hash);
            }

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

            default:
                throw new InvalidOperationException("Invalid primitive bytes.");
            }
        }
Пример #14
0
        public ConnectionData Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
        {
            if (reader.IsNil)
            {
                reader.ReadNil();
                return(null);
            }
            int size = reader.ReadArrayHeader();

            if (size < 4 || 6 < size)
            {
                throw new FormatException(string.Format("ConnectionData expected 4, 5 or 6 elements but found {0} instead!", size));
            }

            bool   clusterIncluded = false;
            string clusterName     = null;

            if (reader.NextMessagePackType == MessagePackType.String)
            {
                clusterName     = reader.ReadString();
                clusterIncluded = true;
            }
            else if (reader.IsNil)
            {
                clusterIncluded = true;
                reader.ReadNil();
            }

            bool serveOnSame = reader.ReadBoolean();
            int  protocol    = reader.ReadInt32();
            bool fragment    = reader.ReadBoolean();

            long?fragTransUnit = null;

            if (reader.IsNil)
            {
                reader.ReadNil();
            }
            else
            {
                fragTransUnit = reader.ReadInt64();
            }

            string[] reservedFields = null;
            if ((size == 5 && !clusterIncluded) || size == 6)
            {
                if (reader.IsNil)
                {
                    reader.ReadNil();
                }
                else
                {
                    int innerSize = reader.ReadArrayHeader();
                    reservedFields = new string[innerSize];
                    for (int i = 0; i < innerSize; ++i)
                    {
                        if (reader.IsNil)
                        {
                            reader.ReadNil();
                        }
                        else
                        {
                            reservedFields[i] = reader.ReadString();
                        }
                    }
                }
            }
            return(new ConnectionData(clusterName, serveOnSame, protocol, fragment, fragTransUnit, reservedFields));
        }
        public StackItem Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
        {
            var count = reader.ReadArrayHeader();

            if (count != 2)
            {
                throw new MessagePackSerializationException($"Invalid StackItem Array Header {count}");
            }

            var type = options.Resolver.GetFormatterWithVerify <StackItemType>().Deserialize(ref reader, options);

            switch (type)
            {
            case StackItemType.Any:
                reader.ReadNil();
                return(StackItem.Null);

            case StackItemType.Boolean:
                return(reader.ReadBoolean() ? StackItem.True : StackItem.False);

            case StackItemType.Buffer:
            {
                var bytes = options.Resolver.GetFormatter <byte[]>().Deserialize(ref reader, options);
                return(new NeoBuffer(bytes));
            }

            case StackItemType.ByteString:
            {
                var bytes = options.Resolver.GetFormatter <byte[]>().Deserialize(ref reader, options);
                return(new NeoByteString(bytes));
            }

            case StackItemType.Integer:
            {
                var integer = options.Resolver.GetFormatterWithVerify <BigInteger>().Deserialize(ref reader, options);
                return(new NeoInteger(integer));
            }

            case StackItemType.InteropInterface:
            {
                var typeName = options.Resolver.GetFormatterWithVerify <string>().Deserialize(ref reader, options);
                return(new TraceInteropInterface(typeName));
            }

            case StackItemType.Pointer:
                reader.ReadNil();
                return(new NeoPointer(Array.Empty <byte>(), 0));

            case StackItemType.Map:
            {
                var map      = new NeoMap();
                var mapCount = reader.ReadMapHeader();
                for (int i = 0; i < mapCount; i++)
                {
                    var key = (PrimitiveType)Deserialize(ref reader, options);
                    map[key] = Deserialize(ref reader, options);
                }
                return(map);
            }

            case StackItemType.Array:
            case StackItemType.Struct:
            {
                var array = type == StackItemType.Array
                            ? new NeoArray()
                            : new NeoStruct();
                var arrayCount = reader.ReadArrayHeader();
                for (int i = 0; i < arrayCount; i++)
                {
                    array.Add(Deserialize(ref reader, options));
                }
                return(array);
            }
            }

            throw new MessagePackSerializationException($"Invalid StackItem {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 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.");
            }
        }
Пример #17
0
 public Unit Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
 {
     reader.ReadNil();
     return(null);
 }