Exemplo n.º 1
0
        public static T Deserialize <T>([NotNull] this ISerializer <T> s, ArraySegment <byte> bytes)
        {
            if (bytes.Array == null)
            {
                ThrowHelpers.ThrowArgumentNullException(nameof(bytes));
#pragma warning disable CS8603   // Possible null reference return.
                return(default); // never reached
        public byte[] Serialize(byte[] obj)
        {
            if (obj == null)
            {
                ThrowHelpers.ThrowArgumentNullException(nameof(obj));
            }

            // ReSharper disable once PossibleNullReferenceException
#pragma warning disable CS8602 // Dereference of a possibly null reference.
            if (obj.Length != Size)
            {
#pragma warning restore CS8602 // Dereference of a possibly null reference.
                ThrowHelpers.ThrowArgumentOutOfRangeException(nameof(obj));
            }

            return(obj.AsSpan().ToArray());
        }
Exemplo n.º 3
0
        public byte[] Serialize(T[] obj)
        {
            if (obj == null)
            {
                ThrowHelpers.ThrowArgumentNullException(nameof(obj));
            }

            // ReSharper disable once PossibleNullReferenceException
#pragma warning disable CS8602 // Dereference of a possibly null reference.
            if (obj.Length == 0)
            {
#pragma warning restore CS8602 // Dereference of a possibly null reference.
                return(new byte[1]);
            }

            using var ms = new MemoryStream();
            ms.WritePackedLength(obj.Length);
            var underlyingSize = underlying.Size;
            if (underlyingSize > 0)
            {
                foreach (var o in obj.Select(e => underlying.Serialize(e)))
                {
                    if (o.Length != underlyingSize)
                    {
                        ThrowHelpers.ThrowArgumentOutOfRangeException(nameof(obj));
                    }

                    ms.Write(o, 0, underlyingSize);
                }
            }
            else
            {
                foreach (var o in obj.Select(e => underlying.Serialize(e)))
                {
                    ms.WritePackedLength(o.Length);
                    if (o.Length > 0)
                    {
                        ms.Write(o, 0, o.Length);
                    }
                }
            }

            return(ms.ToArray());
        }
        public byte[] Serialize(byte[] obj)
        {
            if (obj == null)
            {
                ThrowHelpers.ThrowArgumentNullException(nameof(obj));
            }

            // ReSharper disable once PossibleNullReferenceException
#pragma warning disable CS8602 // Dereference of a possibly null reference.
            if (obj.Length == 0)
            {
#pragma warning restore CS8602 // Dereference of a possibly null reference.
                return(new byte[sizeof(ushort)]);
            }

            using var ms = new MemoryStream();
            ms.WritePackedLength(obj.Length);
            ms.Write(obj, 0, obj.Length);
            return(ms.ToArray());
        }
Exemplo n.º 5
0
        public byte[] Serialize(string obj)
        {
            if (obj == null)
            {
                ThrowHelpers.ThrowArgumentNullException("obj");
            }

            if (string.IsNullOrEmpty(obj))
            {
                return(new byte[1]);
            }

            byte[]      bytes   = encoding.GetBytes(obj);
            Span <byte> packed2 = stackalloc byte[8];

            packed2 = packed2.Slice(0, packed2.WritePackedLength(bytes.Length));
            byte[] rv = new byte[bytes.Length + packed2.Length];
            packed2.CopyTo(rv);
            bytes.AsSpan().CopyTo(rv.AsSpan(packed2.Length));
            return(rv);
        }