Skip to content

Commit

Permalink
Fixing #8: Deseriealize problem when child object is null.
Browse files Browse the repository at this point in the history
This change breaks backward compatibility.
  • Loading branch information
salarcode committed Aug 19, 2016
1 parent 8fb24d1 commit 4f10e24
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 29 deletions.
1 change: 0 additions & 1 deletion Salar.Bois.Tests/ObjectsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@ public void HierarchyObjects2Test()
AssertionHelper.AssertMembersAreEqual(init.KidNull, final.KidNull);
AssertionHelper.AssertMembersAreEqual(init.KidValue, final.KidValue);
init.KidNull.Should().Be.EqualTo(final.KidNull);
init.KidValue.Should().Be.EqualTo(final.KidValue);
}

[TestMethod]
Expand Down
45 changes: 17 additions & 28 deletions Salar.Bois/BoisSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,37 +176,26 @@ private void WriteObject(BinaryWriter writer, BoisMemberInfo boisMemInfo, object
{
if (obj == null)
{
// null indicator
WriteValue(writer, true);
return;
// null indicator
WriteNullableType(writer, true);
return;
}

_serializeDepth++;
var type = obj.GetType();

var boisType = _typeCache.GetTypeInfo(type, true);
var boisTypeInfo = boisType as BoisTypeInfo;

// Use this member info if avaiable. it is more accurate because it came from the object holder,
// not the object itseld.
if (boisMemInfo != null)
{
if (boisMemInfo.IsContainerObject && boisMemInfo.IsNullable)
{
//This is a nullable struct and is not null
WriteValue(writer, false);
}
}
else
{
if (boisType.IsContainerObject && boisType.IsNullable)
{
//This is a nullable struct and is not null
WriteValue(writer, false);
}
}

if (boisTypeInfo != null)
_serializeDepth++;
// Use this member info if avaiable. it is more accurate because it came from the object holder,
// not the object itseld.
if (boisMemInfo.IsContainerObject && boisMemInfo.IsNullable)
{
//This is a nullable struct and is not null
WriteNullableType(writer, false);
}

if (boisTypeInfo != null)
{
// writing the members
for (int i = 0; i < boisTypeInfo.Members.Length; i++)
Expand Down Expand Up @@ -857,10 +846,10 @@ private object ReadMember(BinaryReader reader, Type memType)

private object ReadMember(BinaryReader reader, BoisMemberInfo memInfo, Type memType)
{
if ((memInfo.IsNullable && memInfo.IsContainerObject) ||
(memInfo.IsNullable && !memInfo.IsSupportedPrimitive && (!memInfo.IsContainerObject || memInfo.IsStruct)))
{
bool isNull = reader.ReadByte() != 0;
if ((memInfo.IsNullable && memInfo.IsContainerObject) ||
(memInfo.IsNullable && !memInfo.IsSupportedPrimitive && (!memInfo.IsContainerObject || memInfo.IsStruct)))
{
bool isNull = reader.ReadByte() != 0;

if (isNull)
{
Expand Down

0 comments on commit 4f10e24

Please sign in to comment.