Serialize Similar Classes

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; } } 

I am trying to make sure that the old AddressData is still deserialized in the future, despite being serialized with a slightly different class.

Basically, fields that are empty (not one existing) should be drowned out, and those that were deleted should be forgotten.

I am serializing from Object to Byte [] (and vice versa). Not for 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 that I'm 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 a way to do this and still use bytearray instead of JSON or XML?

+4
source share
1 answer

I agree with James. This is not just an architectural thing (DRY principle) if the old class and the new class are inherited from the same parent, and you can do the job. Make a serializer of the parent class that will output the "null" values ​​for missing values ​​from one class and simply does not even contain a field from another that you want to exclude. Then call the serializer of the parent class, not the derived class, and everything will look the same.

On child classes, you still have to figure out what you want to do with values ​​that are not contained in the new serialization format (i.e., the rest of your code will be happy to get the class with this field empty?).

+1
source

All Articles