Skip to content

Commit

Permalink
✨ Implement type navigator for binary serialization with ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
pleonex committed Jan 30, 2024
1 parent 9da7e72 commit 794cc76
Show file tree
Hide file tree
Showing 10 changed files with 522 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void DeserializeByGenericType()
stream.Write(data);

stream.Position = 0;
var deserializer = new BinaryDeserializer(stream);
var deserializer = new BinaryDeserializer(stream, new DefaultTypePropertyNavigator());
SimpleType obj = deserializer.Deserialize<SimpleType>();

_ = obj.Should().BeEquivalentTo(expected);
Expand Down Expand Up @@ -70,7 +70,7 @@ public void DeserializeStaticByTypeArg()
[Test]
public void DeserializeIncludesInheritedFields()
{
byte[] data = { 0xFE, 0xCA, 0x0A, 0x00, 0x00, 0x00 };
byte[] data = { 0x0A, 0x00, 0x00, 0x00, 0xFE, 0xCA };
var obj = new InheritedType { Value = 0x0A, NewValue = 0xCAFE };

AssertDeserialization(data, obj);
Expand Down
59 changes: 59 additions & 0 deletions src/Yarhl.UnitTests/IO/Serialization/BinarySerializableTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,31 @@

public class SimpleType
{
[BinaryOrder(0)]
public int Value { get; set; }
}

public class InheritedType : SimpleType
{
[BinaryOrder(1)]
public ushort NewValue { get; set; }
}

public struct MultiPropertyStruct
{
[BinaryOrder(0)]
public int IntegerValue { get; set; }

[BinaryOrder(1)]
public long LongValue { get; set; }

[BinaryOrder(2)]
public string TextValue { get; set; }
}

public class TypeWithIgnoredProperties
{
[BinaryOrder(0)]
public long LongValue { get; set; }

[BinaryIgnore]
Expand All @@ -37,65 +43,85 @@ public class TypeWithIgnoredProperties

public class TypeWithNestedObject
{
[BinaryOrder(0)]
public int IntegerValue { get; set; }

[BinaryOrder(1)]
public NestedType ComplexValue { get; set; }

[BinaryOrder(2)]
public int AnotherIntegerValue { get; set; }

public sealed class NestedType
{
[BinaryOrder(0)]
public int NestedValue { get; set; }
}
}

public class TypeWithEndiannessChanges
{
[BinaryOrder(0)]
[BinaryEndianness(EndiannessMode.LittleEndian)]
public int LittleEndianInteger { get; set; }

[BinaryOrder(1)]
[BinaryEndianness(EndiannessMode.BigEndian)]
public int BigEndianInteger { get; set; }

[BinaryOrder(2)]
public int DefaultEndianInteger { get; set; }
}

public class TypeWithNullable
{
[BinaryOrder(0)]
public int? NullValue { get; set; }
}

#region Integer types
public class TypeWithIntegers
{
[BinaryOrder(0)]
public char CharValue { get; set; }

[BinaryOrder(1)]
public byte ByteValue { get; set; }

[BinaryOrder(2)]
public sbyte SByteValue { get; set; }

[BinaryOrder(3)]
public ushort UShortValue { get; set; }

[BinaryOrder(4)]
public short ShortValue { get; set; }

[BinaryOrder(5)]
public uint UIntValue { get; set; }

[BinaryOrder(6)]
public int IntegerValue { get; set; }

[BinaryOrder(7)]
public ulong ULongValue { get; set; }

[BinaryOrder(8)]
public long LongValue { get; set; }
}

public class TypeWithDecimals
{
[BinaryOrder(0)]
public float SingleValue { get; set; }

[BinaryOrder(1)]
public double DoubleValue { get; set; }
}

public class TypeWithInt24
{
[BinaryOrder(0)]
[BinaryInt24]
public int Int24Value { get; set; }
}
Expand All @@ -104,119 +130,152 @@ public class TypeWithInt24
#region Boolean types
public class TypeWithBooleanDefaultAttribute
{
[BinaryOrder(0)]
public int BeforeValue { get; set; }

[BinaryOrder(1)]
[BinaryBoolean]
public bool BooleanValue { get; set; }

[BinaryOrder(2)]
public int AfterValue { get; set; }
}

public class TypeWithBooleanWithoutAttribute
{
[BinaryOrder(0)]
public int BeforeValue { get; set; }

[BinaryOrder(1)]
public bool BooleanValue { get; set; }

[BinaryOrder(2)]
public int AfterValue { get; set; }
}

public class TypeWithBooleanDefinedValue
{
[BinaryOrder(0)]
public int BeforeValue { get; set; }

[BinaryOrder(1)]
[BinaryBoolean(UnderlyingType = typeof(short), TrueValue = (short)42, FalseValue = (short)-42)]
public bool BooleanValue { get; set; }

[BinaryOrder(2)]
public int AfterValue { get; set; }
}

public class TypeWithBooleanTextValue
{
[BinaryOrder(0)]
public int BeforeValue { get; set; }

[BinaryOrder(1)]
[BinaryBoolean(UnderlyingType = typeof(string), TrueValue = "true", FalseValue = "false")]
public bool BooleanValue { get; set; }

[BinaryOrder(2)]
public int AfterValue { get; set; }
}
#endregion

#region String types
public class TypeWithStringDefaultAttribute
{
[BinaryOrder(0)]
public int BeforeValue { get; set; }

[BinaryOrder(1)]
[BinaryString]
public string StringValue { get; set; }

[BinaryOrder(2)]
public int AfterValue { get; set; }
}

public class TypeWithStringWithoutAttribute
{
[BinaryOrder(0)]
public int BeforeValue { get; set; }

[BinaryOrder(1)]
public string StringValue { get; set; }

[BinaryOrder(2)]
public int AfterValue { get; set; }
}

public class TypeWithStringVariableSize
{
[BinaryOrder(0)]
public int BeforeValue { get; set; }

[BinaryOrder(1)]
[BinaryString(SizeType = typeof(ushort), Terminator = "")]
public string StringValue { get; set; }

[BinaryOrder(2)]
public int AfterValue { get; set; }
}

public class TypeWithStringFixedSize
{
[BinaryOrder(0)]
public int BeforeValue { get; set; }

[BinaryOrder(1)]
[BinaryString(FixedSize = 3, Terminator = "")]
public string StringValue { get; set; }

[BinaryOrder(2)]
public int AfterValue { get; set; }
}

public class TypeWithStringDefinedEncoding
{
[BinaryOrder(0)]
public int BeforeValue { get; set; }

[BinaryOrder(1)]
[BinaryString(CodePage = 932)]
public string StringValue { get; set; }

[BinaryOrder(2)]
public int AfterValue { get; set; }
}

public class TypeWithStringInvalidEncoding
{
[BinaryOrder(0)]
public int BeforeValue { get; set; }

[BinaryOrder(1)]
[BinaryString(CodePage = 666)]
public string StringValue { get; set; }

[BinaryOrder(2)]
public int AfterValue { get; set; }
}
#endregion

#region Enum types
public class TypeWithEnumNoAttribute
{
[BinaryOrder(0)]
public SerializableEnum EnumValue { get; set; }
}

public class TypeWithEnumDefaultAttribute
{
[BinaryOrder(0)]
[BinaryEnum]
public SerializableEnum EnumValue { get; set; }
}

public class TypeWithEnumWithOverwrittenType
{
[BinaryOrder(0)]
[BinaryEnum(UnderlyingType = typeof(uint))]
public SerializableEnum EnumValue { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void SerializeByGenericType()
var obj = new SimpleType { Value = 0x0A, };

using var stream = new DataStream();
var serializer = new BinarySerializer(stream);
var serializer = new BinarySerializer(stream, new DefaultTypePropertyNavigator());
serializer.Serialize<SimpleType>(obj);

AssertBinary(stream, data);
Expand Down
Loading

0 comments on commit 794cc76

Please sign in to comment.