Decoding from AMQP decimal to C# decimal can lose precision and can also cause OverflowException.
Inheritance: EncodingBase
Ejemplo n.º 1
0
        public static decimal?Decode(ByteBuffer buffer, FormatCode formatCode)
        {
            if (formatCode == 0 && (formatCode = AmqpEncoding.ReadFormatCode(buffer)) == FormatCode.Null)
            {
                return(null);
            }

            return(DecimalEncoding.DecodeValue(buffer, formatCode));
        }
Ejemplo n.º 2
0
 public override void EncodeObject(object value, bool arrayEncoding, ByteBuffer buffer)
 {
     if (arrayEncoding)
     {
         DecimalEncoding.EncodeValue((decimal)value, buffer);
     }
     else
     {
         DecimalEncoding.Encode((decimal)value, buffer);
     }
 }
Ejemplo n.º 3
0
 public static void Encode(decimal?value, ByteBuffer buffer)
 {
     if (value.HasValue)
     {
         AmqpBitConverter.WriteUByte(buffer, FormatCode.Decimal128);
         DecimalEncoding.EncodeValue(value.Value, buffer);
     }
     else
     {
         AmqpEncoding.EncodeNull(buffer);
     }
 }
Ejemplo n.º 4
0
        internal static decimal DecodeValue(ByteBuffer buffer, FormatCode formatCode)
        {
            decimal value = 0;

            switch (formatCode)
            {
            case FormatCode.Decimal32:
                value = DecimalEncoding.DecodeDecimal32(buffer);
                break;

            case FormatCode.Decimal64:
                value = DecimalEncoding.DecodeDecimal64(buffer);
                break;

            case FormatCode.Decimal128:
                value = DecimalEncoding.DecodeDecimal128(buffer);
                break;

            default:
                throw AmqpEncoding.GetEncodingException(AmqpResources.GetString(AmqpResources.AmqpInvalidFormatCode, formatCode, buffer.Offset));
            }

            return(value);
        }
Ejemplo n.º 5
0
        static T[] Decode <T>(ByteBuffer buffer, int size, int count, FormatCode formatCode)
        {
            object descriptor = null;

            if (formatCode == FormatCode.Described)
            {
                descriptor = AmqpEncoding.DecodeObject(buffer);
                formatCode = AmqpEncoding.ReadFormatCode(buffer);
            }

            // Special cases for primitive types
            switch (formatCode)
            {
            case FormatCode.UInt0:
            case FormatCode.SmallUInt:
                return((T[])DecodeArray <uint>(buffer, formatCode, count, (b, f) => UIntEncoding.Decode(b, f).Value));

            case FormatCode.UInt:
                return((T[])DecodeArray <uint>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadUInt(b)));

            case FormatCode.SmallInt:
                return((T[])DecodeArray <int>(buffer, formatCode, count, (b, f) => IntEncoding.Decode(b, f).Value));

            case FormatCode.Int:
                return((T[])DecodeArray <int>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadInt(b)));

            case FormatCode.ULong0:
            case FormatCode.SmallULong:
                return((T[])DecodeArray <ulong>(buffer, formatCode, count, (b, f) => ULongEncoding.Decode(b, f).Value));

            case FormatCode.ULong:
                return((T[])DecodeArray <ulong>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadULong(b)));

            case FormatCode.SmallLong:
                return((T[])DecodeArray <long>(buffer, formatCode, count, (b, f) => LongEncoding.Decode(b, f).Value));

            case FormatCode.Long:
                return((T[])DecodeArray <long>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadLong(b)));

            case FormatCode.Symbol8:
            case FormatCode.Symbol32:
                return((T[])DecodeArray <AmqpSymbol>(buffer, formatCode, count, (b, f) => SymbolEncoding.Decode(b, f)));

            case FormatCode.Boolean:
                return((T[])DecodeArray <bool>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadUByte(b) > 0));

            case FormatCode.UByte:
                return((T[])DecodeArray <byte>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadUByte(b)));

            case FormatCode.Byte:
                return((T[])DecodeArray <sbyte>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadByte(b)));

            case FormatCode.UShort:
                return((T[])DecodeArray <ushort>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadUShort(b)));

            case FormatCode.Short:
                return((T[])DecodeArray <short>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadShort(b)));

            case FormatCode.Float:
                return((T[])DecodeArray <float>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadFloat(b)));

            case FormatCode.Double:
                return((T[])DecodeArray <double>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadDouble(b)));

            case FormatCode.Decimal32:
            case FormatCode.Decimal64:
            case FormatCode.Decimal128:
                return((T[])DecodeArray <decimal>(buffer, formatCode, count, (b, f) => DecimalEncoding.DecodeValue(b, f)));

            case FormatCode.Char:
                return((T[])DecodeArray <char>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadChar(b)));

            case FormatCode.TimeStamp:
                return((T[])DecodeArray <DateTime>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadTimestamp(b)));

            case FormatCode.Uuid:
                return((T[])DecodeArray <Guid>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadUuid(b)));

            default:
                break;
            }

            // General path
            EncodingBase encoding = AmqpEncoding.GetEncoding(formatCode);

            T[] array = new T[count];
            for (int i = 0; i < count; ++i)
            {
                object value = encoding.DecodeObject(buffer, formatCode);
                if (descriptor != null)
                {
                    value = new DescribedType(descriptor, value);
                }

                array[i] = (T)value;
            }

            return(array);
        }
Ejemplo n.º 6
0
        internal static void EncodeValue(IList value, int count, Type itemType, ByteBuffer buffer)
        {
            if (count == 0)
            {
                return;
            }

            EncodingBase encoding = AmqpEncoding.GetEncoding(itemType);

            switch (encoding.FormatCode)
            {
            case FormatCode.UInt:
                EncodeArray <uint>(buffer, (IList <uint>)value, count, FormatCode.UInt, (b, v, i) => AmqpBitConverter.WriteUInt(b, v));
                return;

            case FormatCode.Int:
                buffer.Validate(true, FixedWidth.Int * count);
                EncodeArray <int>(buffer, (IList <int>)value, count, FormatCode.Int, (b, v, i) =>
                {
                    AmqpBitConverter.WriteInt(b.Buffer, b.WritePos + i * FixedWidth.Int, v);
                });
                buffer.Append(FixedWidth.Int * count);
                return;

            case FormatCode.ULong:
                EncodeArray <ulong>(buffer, (IList <ulong>)value, count, FormatCode.ULong, (b, v, i) => AmqpBitConverter.WriteULong(b, v));
                return;

            case FormatCode.Long:
                EncodeArray <long>(buffer, (IList <long>)value, count, FormatCode.Long, (b, v, i) => AmqpBitConverter.WriteLong(b, v));
                return;

            case FormatCode.Symbol32:
                EncodeArray <AmqpSymbol>(buffer, (IList <AmqpSymbol>)value, count, FormatCode.Symbol32, (b, v, i) => SymbolEncoding.EncodeValue(v, FormatCode.Symbol32, b));
                return;

            case FormatCode.Boolean:
                EncodeArray <bool>(buffer, (IList <bool>)value, count, FormatCode.Boolean, (b, v, i) => AmqpBitConverter.WriteUByte(b, (byte)(v ? 1 : 0)));
                return;

            case FormatCode.UByte:
                EncodeArray <byte>(buffer, (IList <byte>)value, count, FormatCode.UByte, (b, v, i) => AmqpBitConverter.WriteUByte(b, v));
                return;

            case FormatCode.Byte:
                EncodeArray <sbyte>(buffer, (IList <sbyte>)value, count, FormatCode.Byte, (b, v, i) => AmqpBitConverter.WriteByte(b, v));
                return;

            case FormatCode.UShort:
                EncodeArray <ushort>(buffer, (IList <ushort>)value, count, FormatCode.UShort, (b, v, i) => AmqpBitConverter.WriteUShort(b, v));
                return;

            case FormatCode.Short:
                EncodeArray <short>(buffer, (IList <short>)value, count, FormatCode.Short, (b, v, i) => AmqpBitConverter.WriteShort(b, v));
                return;

            case FormatCode.Float:
                EncodeArray <float>(buffer, (IList <float>)value, count, FormatCode.Float, (b, v, i) => AmqpBitConverter.WriteFloat(b, v));
                return;

            case FormatCode.Double:
                EncodeArray <double>(buffer, (IList <double>)value, count, FormatCode.Double, (b, v, i) => AmqpBitConverter.WriteDouble(b, v));
                return;

            case FormatCode.Decimal128:
                EncodeArray <decimal>(buffer, (IList <decimal>)value, count, FormatCode.Decimal128, (b, v, i) => DecimalEncoding.EncodeValue(v, b));
                return;

            case FormatCode.Char:
                EncodeArray <char>(buffer, (IList <char>)value, count, FormatCode.Char, (b, v, i) => AmqpBitConverter.WriteChar(b, v));
                return;

            case FormatCode.TimeStamp:
                EncodeArray <DateTime>(buffer, (IList <DateTime>)value, count, FormatCode.TimeStamp, (b, v, i) => AmqpBitConverter.WriteTimestamp(b, v));
                return;

            case FormatCode.Uuid:
                EncodeArray <Guid>(buffer, (IList <Guid>)value, count, FormatCode.Uuid, (b, v, i) => AmqpBitConverter.WriteUuid(b, v));
                return;

            default:
                break;
            }

            object firstItem = value[0];

            AmqpBitConverter.WriteUByte(buffer, encoding.FormatCode);
            if (encoding.FormatCode == FormatCode.Described)
            {
                DescribedType describedValue = (DescribedType)firstItem;
                AmqpEncoding.EncodeObject(describedValue.Descriptor, buffer);
                AmqpBitConverter.WriteUByte(buffer, AmqpEncoding.GetEncoding(describedValue.Value).FormatCode);
            }

            for (int i = 0; i < count; i++)
            {
                encoding.EncodeObject(value[i], true, buffer);
            }
        }
Ejemplo n.º 7
0
 public override object DecodeObject(ByteBuffer buffer, FormatCode formatCode)
 {
     return(DecimalEncoding.Decode(buffer, formatCode));
 }