Matthew Maravillas
Matthew Maravillas

Reputation: 3182

Deserialization validation

I'm working with a list of fonts that I serialize and deserialize using DataContractSerializer. In between the two steps, it's conceivable that the user has removed a font from their machine. I'd like to check a font name as it's being deserialized to ensure that it still exists on the system. If it doesn't exist, that element is not included in the collection returned by DataContractSerializer.ReadObject().

Specifically, I'm storing a FontFamily and serializing a property that gets FontFamily.Name. In this property's set accessor, I convert the string back into a FontFamily.

The only reasonable alternative to validation that I can think of would be having the property's set accessor ignore invalid values, and filter out the invalid deserialized objects later. I don't like this option, however - is there a more proper way?

Upvotes: 2

Views: 2100

Answers (2)

Factor Mystic
Factor Mystic

Reputation: 26770

You could also implement IXmlSerializable for you class, which would include your own implementation of ReadXml, allowing you to do whatever validation you wanted as the object was being deserialized.

Upvotes: 0

Ants
Ants

Reputation: 2668

Why not take advantage on the OnDeserializedAttribute? Have your callback do the validation and removal of items that are not valid for the client environment.

http://msdn.microsoft.com/en-us/library/ms733734.aspx

I do have some concerns about how you would go about round tripping the data if you remove or modify the data under the covers.

(For example: I remember being particularly frustrated by older versions of MS Publisher as I was working on a document on two different machines hooked up to two different printers. Whenever I modified the file on one machine, Publisher would be reformat the document to target the printer attached to that machine. When I went back to the other machine where I was going to do the actual printing, Publisher would reformat again, but the margins would not be quite right and so I needed to tweak some more.)

Upvotes: 3

Related Questions