public object ReadInto(object instance, BinarySerialisationReader reader, BinarySerialisationDataType nextEntryType, bool ignoreAnyInvalidTypes)
        {
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            foreach (var field in _fields)
            {
                if (nextEntryType != BinarySerialisationDataType.FieldName)
                {
                    throw new InvalidSerialisationDataFormatException("Unexpected data type encountered while processing fields in BinarySerialisationReaderTypeReader: " + nextEntryType);
                }
                if (reader.ReadNextNameReferenceID(reader.ReadNextDataType()) != field.FieldNameReferenceID)
                {
                    throw new InvalidSerialisationDataFormatException($"Fields appeared out of order while being processed by BinarySerialisationReaderTypeReader");
                }

                var value = reader.Read(ignoreAnyInvalidTypes, field.FieldType);
                foreach (var setter in field.Setters)
                {
                    setter(ref instance, value);
                }
                nextEntryType = reader.ReadNextDataType();
            }
            if (nextEntryType != BinarySerialisationDataType.ObjectEnd)
            {
                throw new InvalidSerialisationDataFormatException("Unexpected data type encountered after processed fields in BinarySerialisationReaderTypeReader: " + nextEntryType);
            }
            return(instance);
        }
Esempio n. 2
0
 public byte[] GetLittleEndianBytesWithDataType(BinarySerialisationDataType int32)
 {
     if (BitConverter.IsLittleEndian)
     {
         return new[] { (byte)int32, Byte0, Byte1, Byte2, Byte3 }
     }
     ;
     else
     {
         return new[] { (byte)int32, Byte3, Byte2, Byte1, Byte0 }
     };
 }
        public object ReadInto(object instance, BinarySerialisationReader reader, BinarySerialisationDataType nextEntryType, bool ignoreAnyInvalidTypes)
        {
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            var numberOfFieldsSet = 0;

            while (numberOfFieldsSet < _fields.Length)
            {
                var field = _fields[numberOfFieldsSet];
                if (nextEntryType != BinarySerialisationDataType.FieldName)
                {
                    throw new InvalidSerialisationDataFormatException("Unexpected data type encountered while processing fields in BinarySerialisationReaderTypeReader: " + nextEntryType);
                }

                if (reader.ReadNextNameReferenceID(reader.ReadNextDataType()) != field.FieldNameReferenceID)
                {
                    // 2020-12-03 DWR: If this isn't the FieldNameReferenceID that we were expecting next then it must be a field that doesn't exist on the source type in this version of the assembly, suggesting that we're deserialising
                    // data from a different version of the type that has this additional field. We want to just skip over this data. We know that we won't end up missing out setting any fields that we DO want on the current version of
                    // the entity because the while loop we're in would try to read past the end of the data and it would throw.
                    reader.Read(ignoreAnyInvalidTypes: true, targetTypeIfAvailable: null);
                }
                else
                {
                    var value = reader.Read(ignoreAnyInvalidTypes, field.FieldType);
                    foreach (var setter in field.Setters)
                    {
                        setter(ref instance, value);
                    }
                    numberOfFieldsSet++;
                }

                nextEntryType = reader.ReadNextDataType();
            }
            while (nextEntryType == BinarySerialisationDataType.FieldName)             // 2020-12-03 DWR: This corresponds with the change above - if there are new properties AFTER all of the expected fields then we need to skip over those as well
            {
                reader.Read(ignoreAnyInvalidTypes: true, targetTypeIfAvailable: null);
                nextEntryType = reader.ReadNextDataType();
            }
            if (nextEntryType != BinarySerialisationDataType.ObjectEnd)
            {
                throw new InvalidSerialisationDataFormatException("Unexpected data type encountered after processed fields in BinarySerialisationReaderTypeReader: " + nextEntryType);
            }
            return(instance);
        }
Esempio n. 4
0
 internal void VariableLengthInt32(int value, BinarySerialisationDataType int8, BinarySerialisationDataType int16, BinarySerialisationDataType int24, BinarySerialisationDataType int32)
 {
     if ((value >= byte.MinValue) && (value <= byte.MaxValue))
     {
         WriteBytes((byte)int8, (byte)value);
     }
     else if ((value >= short.MinValue) && (value <= short.MaxValue))
     {
         WriteBytes((new Int16Bytes((short)value)).GetLittleEndianBytesWithDataType(int16));
     }
     else if ((value >= _int24Min) && (value <= _int24Max))
     {
         WriteBytes((new Int24Bytes(value)).GetLittleEndianBytesWithDataType(int24));
     }
     else
     {
         WriteBytes((new Int32Bytes(value)).GetLittleEndianBytesWithDataType(int32));
     }
 }
Esempio n. 5
0
 internal int ReadNextNameReferenceID(BinarySerialisationDataType specifiedDataType)
 {
     if (specifiedDataType == BinarySerialisationDataType.NameReferenceID32)
     {
         return(ReadNextInt());
     }
     else if (specifiedDataType == BinarySerialisationDataType.NameReferenceID24)
     {
         return(ReadNextInt24());
     }
     else if (specifiedDataType == BinarySerialisationDataType.NameReferenceID16)
     {
         return(ReadNextInt16());
     }
     else if (specifiedDataType == BinarySerialisationDataType.NameReferenceID8)
     {
         return(ReadNext());
     }
     else
     {
         throw new InvalidSerialisationDataFormatException("Unexpected " + specifiedDataType + " (expected a Name Reference ID)");
     }
 }
Esempio n. 6
0
        private object ReadBeforeApplyingAnyTransforms(BinarySerialisationDataType dataType, bool ignoreAnyInvalidTypes)
        {
            switch (dataType)
            {
            default:
                throw new InvalidSerialisationDataFormatException("Unexpected BinarySerialisationDataType: " + dataType);

            case BinarySerialisationDataType.Null:
                return(null);

            case BinarySerialisationDataType.Boolean:
                return(ReadNext() != 0);

            case BinarySerialisationDataType.Byte:
                return(ReadNext());

            case BinarySerialisationDataType.SByte:
                return((sbyte)ReadNext());

            case BinarySerialisationDataType.Int16:
                return(ReadNextInt16());

            case BinarySerialisationDataType.Int32_8:
                return((int)ReadNext());

            case BinarySerialisationDataType.Int32_16:
                return((int)ReadNextInt16());

            case BinarySerialisationDataType.Int32_24:
                return(ReadNextInt24());

            case BinarySerialisationDataType.Int32:
                return(ReadNextInt());

            case BinarySerialisationDataType.Int64:
                return(ReadNextInt64());

            case BinarySerialisationDataType.UInt16:
                return((new UInt16Bytes(ReadNext(Int16Bytes.BytesRequired))).Value);

            case BinarySerialisationDataType.UInt32:
                return((new UInt32Bytes(ReadNext(Int32Bytes.BytesRequired))).Value);

            case BinarySerialisationDataType.UInt64:
                return((new UInt64Bytes(ReadNext(Int64Bytes.BytesRequired))).Value);

            case BinarySerialisationDataType.Single:
                return((new SingleBytes(ReadNext(SingleBytes.BytesRequired))).Value);

            case BinarySerialisationDataType.Double:
                return((new DoubleBytes(ReadNext(DoubleBytes.BytesRequired))).Value);

            case BinarySerialisationDataType.Decimal:
                return((new DecimalBytes(ReadNext(DecimalBytes.BytesRequired))).Value);

            case BinarySerialisationDataType.Char:
                return((new CharBytes(ReadNext(CharBytes.BytesRequired))).Value);

            case BinarySerialisationDataType.String:
                return(ReadNextString());

            case BinarySerialisationDataType.DateTime:
                return(ReadNextDateTime());

            case BinarySerialisationDataType.TimeSpan:
                return(ReadNextTimeSpan());

            case BinarySerialisationDataType.Guid:
                return(ReadNextGuid());

            case BinarySerialisationDataType.ArrayStart:
                return(ReadNextArray(ignoreAnyInvalidTypes));

            case BinarySerialisationDataType.ObjectStart:
                return(ReadNextObject(ignoreAnyInvalidTypes, toPopulateDeferredInstance: false));
            }
        }