Пример #1
0
        public static int SerializeDecimal(decimal value, SerializationContext context, bool lengthPrefixed)
        {
            int[] bits = decimal.GetBits(value);
            ulong a = ((ulong)bits[1]) << 32, b = ((ulong)bits[0]) & 0xFFFFFFFFL;
            ulong low       = a | b;
            uint  high      = (uint)bits[2];
            uint  signScale = (uint)(((bits[3] >> 15) & 0x01FE) | ((bits[3] >> 31) & 0x0001));

            int len = 0;

            if (lengthPrefixed)
            {
                if (low != 0)
                {
                    len += 1 + SerializationContext.GetLength((long)low);
                }
                if (high != 0)
                {
                    len += 1 + SerializationContext.GetLength((long)high);
                }
                if (signScale != 0)
                {
                    len += 2;
                }
                len = context.EncodeInt32(len);
            }
            if (low != 0)
            {
                context.WriteByte((FieldDecimalLow << 3) | (int)WireType.Variant);
                len += 1 + context.EncodeInt64((long)low);
            }
            if (high != 0)
            { // note encode as long to avoid high sign issues
                context.WriteByte((FieldDecimalHigh << 3) | (int)WireType.Variant);
                len += 1 + context.EncodeInt64((long)high);
            }
            if (signScale != 0)
            {
                context.WriteByte((FieldDecimalSignScale << 3) | (int)WireType.Variant);
                context.WriteByte((byte)signScale);
                len += 2;
            }
            return(len);
        }
Пример #2
0
        public override int Serialize(TSource source, SerializationContext context)
        {
            long value = GetValue(source);

            if (IsOptional && value == DefaultValue)
            {
                return(0);
            }
            return(WritePrefix(context)
                   + context.EncodeInt64(value));
        }
Пример #3
0
        public static int SerializeDecimal(decimal value, SerializationContext context, bool lengthPrefixed)
        {
            int[] bits = decimal.GetBits(value);
            ulong a = ((ulong)bits[1]) << 32, b = ((ulong)bits[0]) & 0xFFFFFFFFL;
            ulong low = a | b;
            uint high = (uint) bits[2];
            uint signScale = (uint)(((bits[3] >> 15) & 0x01FE) | ((bits[3] >> 31) & 0x0001));

            int len = 0;
            if (lengthPrefixed)
            {
                if (low != 0) len += 1 + SerializationContext.GetLength((long)low);
                if (high != 0) len += 1 + SerializationContext.GetLength((long)high);
                if (signScale!=0) len += 2;
                len = context.EncodeInt32(len);
            }
            if (low != 0)
            {
                context.WriteByte((FieldDecimalLow << 3) | (int)WireType.Variant);
                len += 1 + context.EncodeInt64((long)low);
            }
            if (high != 0)
            { // note encode as long to avoid high sign issues
                context.WriteByte((FieldDecimalHigh << 3) | (int)WireType.Variant);
                len += 1 + context.EncodeInt64((long)high);
            }
            if (signScale != 0)
            {
                context.WriteByte((FieldDecimalSignScale << 3) | (int)WireType.Variant);
                context.WriteByte((byte)signScale);
                len += 2;
            }
            return len;
        }