Careful With Deserialization On Windows Phone – Encoding Matters
Make sure that you are aware of some gotchas when it comes to handling deserialization routines.
By Den in Programming
December 30, 2012
Serialization is a process that is prone to errors, especially with a poorly structured data layer. However, that is not always the case and a seemingly normal serialization/deserialization scenario might turn out to produce unexepected results. As I was working on a Windows Phone application, I had this standard routine:
|
|
The deserialization would occur like this:
|
|
But this caused an exception to be thrown, and not just a regular one, but one that would crash the application and break the debugging process.
Looking at the snippet above, it is really hard to tell what the issue is, especially if the XML that is being retrieved from the file is valid. However, upon deserialization there is no place where the string encoding is actually specified, which results in the unexpected behavior that we are seeing. By default, the serialization engine sets the encoding to be UTF-8, which should also be respected when deserializing.
So instead of the deserialization snippet above, you need to use this:
|
|
Feedback
Have any thoughts? Let me know over email by sending a note to hi followed by the domain of this website.