Ejemplo n.º 1
0
		public readonly ushort Reserved; // field kept for completeness sake
#pragma warning restore CS0649

		/// <summary>
		/// Order: SetKey, GetExtra, GetBody
		/// </summary>
		private void AllocateExtras()
		{
			if (extra.Length == 0 && extraLength > 0)
				extra = bodyBuilder.Request(extraLength);

#if DEBUG
			didExtra = true;
#endif
		}
Ejemplo n.º 2
0
        public void Serialize(SequenceBuilder target, string key)
        {
            KeyFormatter.ThrowIfInvalidKey(key);

            var keyLength = utf8.GetByteCount(key);
            var buffer    = target.Request(keyLength).Span;

            utf8.GetBytes(key, buffer);
        }
        public void Serialize(SequenceBuilder target, string key)
        {
            KeyFormatter.ThrowIfInvalidKey(key);

            var keyLength = prefix.Length + utf8.GetByteCount(key);
            var buffer    = target.Request(keyLength).Span;

            prefix.Span.CopyTo(buffer);
            utf8.GetBytes(key, buffer.Slice(prefix.Length));
        }
Ejemplo n.º 4
0
        private uint DoSerialize(SequenceBuilder output, object?value)
        {
            if (value is byte[] tmpByteArray)
            {
#pragma warning disable IDE0067 // nothing to dispose
                new SegmentedStream(output).Write(tmpByteArray, 0, tmpByteArray.Length);
#pragma warning restore IDE0067

                return(RawDataFlag);
            }

            TypeCode code;

            if (value == null)
            {
                code = TypeCode.DBNull;
            }
            else
            {
                code = Type.GetTypeCode(value.GetType());

#pragma warning disable IDE0049 // readability

                switch (code)
                {
                case TypeCode.Empty:
                case TypeCode.DBNull: break;

                case TypeCode.Object: new BinaryFormatter().Serialize(new SegmentedStream(output), value); break;

                case TypeCode.String:
                    var theString = (string)value;
                    if (theString.Length > 0)
                    {
                        Utf8NoBom.GetBytes(theString.AsSpan(), output.Request(Utf8NoBom.GetByteCount(theString)).Span);
                    }
                    break;

                case TypeCode.SByte: output.Request(sizeof(SByte)).Span[0] = (Byte)(SByte)value; break;

                case TypeCode.Byte: output.Request(sizeof(Byte)).Span[0] = (Byte)value; break;

                case TypeCode.Boolean: output.Request(sizeof(Byte)).Span[0] = (Boolean)value ? TRUE : FALSE; break;

                case TypeCode.Char: BinaryPrimitives.WriteUInt16LittleEndian(output.Request(sizeof(Char)).Span, (Char)value); break;

                case TypeCode.Int16: BinaryPrimitives.WriteInt16LittleEndian(output.Request(sizeof(Int16)).Span, (Int16)value); break;

                case TypeCode.Int32: BinaryPrimitives.WriteInt32LittleEndian(output.Request(sizeof(Int32)).Span, (Int32)value); break;

                case TypeCode.Int64: BinaryPrimitives.WriteInt64LittleEndian(output.Request(sizeof(Int64)).Span, (Int64)value); break;

                case TypeCode.UInt16: BinaryPrimitives.WriteUInt16LittleEndian(output.Request(sizeof(UInt16)).Span, (UInt16)value); break;

                case TypeCode.UInt32: BinaryPrimitives.WriteUInt32LittleEndian(output.Request(sizeof(UInt32)).Span, (UInt32)value); break;

                case TypeCode.UInt64: BinaryPrimitives.WriteUInt64LittleEndian(output.Request(sizeof(UInt64)).Span, (UInt64)value); break;

                case TypeCode.DateTime: BinaryPrimitives.WriteInt64LittleEndian(output.Request(sizeof(Int64)).Span, ((DateTime)value).ToBinary()); break;

                case TypeCode.Single:
#if !(NETSTANDARD2_0 || NET471 || NET472 || NET48)
                    BitConverter.TryWriteBytes(output.Request(sizeof(Single)).Span, (Single)value);
#else
                    var float_bytes = BitConverter.GetBytes((Single)value);
                    output.Append(float_bytes.AsSpan());
#endif
                    break;

                case TypeCode.Double:
#if !(NETSTANDARD2_0 || NET471 || NET472 || NET48)
                    BitConverter.TryWriteBytes(output.Request(sizeof(Double)).Span, (Double)value);
#else
                    var double_bytes = BitConverter.GetBytes((Double)value);
                    output.Append(double_bytes.AsSpan());
#endif

                    break;

                case TypeCode.Decimal:
                    var src    = Decimal.GetBits((Decimal)value);
                    var target = output.Request(sizeof(Int32) * 4).Span;

                    BinaryPrimitives.WriteInt32LittleEndian(target, src[0]);
                    BinaryPrimitives.WriteInt32LittleEndian(target.Slice(sizeof(Int32)), src[1]);
                    BinaryPrimitives.WriteInt32LittleEndian(target.Slice(sizeof(Int32) + sizeof(Int32)), src[2]);
                    BinaryPrimitives.WriteInt32LittleEndian(target.Slice(sizeof(Int32) + sizeof(Int32) + sizeof(Int32)), src[3]);

                    break;

                default:
                    throw new SerializationException($"Cannot serialize '{value}', unknown TypeCode: {code}");
                }

#pragma warning restore IDE0049
            }

            return(FlagPrefix | (uint)code);
        }