I have the following classes:
[Serializable]
public class ExtendedAddressData
{
public String AddressLine1 { get; set; }
public String AddressLine2 { get; set; }
public String Country { get; set; }
public String City { get; set; }
public String Firstname { get; set; }
public String Surname { get; set; }
public String FakeField { get; set; }
}
[Serializable]
public class AddressData
{
public String AddressLine1 { get; set; }
public String AddressLine2 { get; set; }
public String Country { get; set; }
public String City { get; set; }
public String ZipCode { get; set; }
public String Firstname { get; set; }
public String Surname { get; set; }
}
Im trying to make sure that old AddressData is still deserializable in the future despite being serialized with a slightly different class.
Basically the fields that are empty (none existing) should be blanked out and those that were removed should be forgotten.
I am serializing from Object to Byte[] (and back). Not to XML or JSON
private static byte[] ObjectToByteArray(object _Object)
{
try
{
var memoryStream = new MemoryStream();
new BinaryFormatter().Serialize(memoryStream, _Object);
return memoryStream.ToArray();
}
catch (Exception e)
{
Console.WriteLine("Exception caught in process: {0}", e);
}
return null;
}
private static object ByteArrayToObject(byte[] aBytes)
{
try
{
var memoryStream = new MemoryStream(aBytes);
var serializedObject = new BinaryFormatter().Deserialize(memoryStream);
return serializedObject;
}
catch (Exception e)
{
Console.WriteLine("Exception caught in process: {0}", e);
}
return null;
}
here is a simplified UnitTest that probably explains what im trying better than i can
public void LegacySerializationSupportTest()
{
var realData = new AddressData()
{
AddressLine1 = "AddressLine1",
AddressLine2 = "AddressLine2",
City = "City",
Country = "Country",
Firstname = "Firstname",
Surname = "Surname",
ZipCode = "ZipCode"
};
var bytearray = AddressRepository_Accessor.ObjectToByteArray(realData);
AddressData realObject = (AddressData) AddressRepository_Accessor.ByteArrayToObject(bytearray);
ExtendedAddressData fakeObject = (ExtendedAddressData) AddressRepository_Accessor.ByteArrayToObject(bytearray);
Assert.AreEqual(realObject.AddressLine1,fakeObject.AddressLine1);
}
Is there any way i can do this and still use bytearray instead of JSON or XML?
I agree with James. It isn’t just an architectual thing (DRY principle) if the old class and the new class are both inherit from the same parent you can get the job done. Make a serializer on the parent class that will output the “null” values for the missing values from the one class and simply doesn’t even contain the fields from the other one you want to exclude. Then call the serializer on the parent class rather than the derived class and everything will end up coming out looking the same.
On the child classes you’ll still have to figure out what you want to do with values that aren’t contained in the new serialization format though (ie will the rest of your code be happy getting a class with that field empty?).