×

Discussion Board

Results 1 to 13 of 13
  1. #1
    Registered User
    Join Date
    Mar 2013
    Location
    Goa, India
    Posts
    32

    Exclamation Custom serialization support in windows phone

    I want to use custom serialization for my windows phone 8 application. I know we have an XmlSerializer class available for data serialization but it do not fullfill my needs.

    Why i need custom serialization.
    I need to use custom serialization for maintaining compatibility between multiple revisions of my application. Say i have a model class, which contains 3 member variables in revision 1.0 but i add one more member variable to the class in revision 1.1. So i want data stored by 1.0 app to be compatible with revision 1.1 too. For which i will add version number to each of my model class and depending on version number i will process every field during serialization and desrialization process.

    What i tried.?
    I searched for a custom serialization API for c# and came through ISerializable Interface which completely fullfill my needs but unfortunately it is not suppoted on Windows Phone. Searching further i came across Data Contract Serialization. But Data Contract Serialization does not support processing every member variable separately.


    So how can i achieve custom serialization on windows phone? Please help. Unable to proceed because of this problem. Quick response will be appreciated.

    Regards

  2. #2
    Registered User
    Join Date
    Dec 2012
    Location
    Zagreb, Croatia
    Posts
    73

    Re: Custom serialization support in windows phone

    Hi,

    Take a look at OnDeserializedAttribute. From the documentation:
    When applied to a method, specifies that the method is called immediately after deserialization of an object in an object graph. The order of deserialization relative to other objects in the graph is non-deterministic.
    You could do some post-initialization code here.

    If you really want a clean and robust solution, then you should build one for yourself. You can try binary serialization/deserialization with optional "version" property on the beginning of the entire block which will inform your deserialization method that some additional stuff may be necessary.

  3. #3
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,751

    Re: Custom serialization support in windows phone

    You may want something like "Version Tolerant Serialization" http://msdn.microsoft.com/en-us/libr...v=vs.100).aspx. I do not actually know it, just found with a simple search. Given that it mentions some "SoapFormatter", it may be available for XML serialization too.
    If there is no built-in support, any XML parser can do the job: create the object with default values, then traverse the XML and set the fields of the object accordingly. Fields not mentioned in the XML will remain to contain their default values.

  4. #4
    Registered User
    Join Date
    Mar 2013
    Location
    Goa, India
    Posts
    32

    Re: Custom serialization support in windows phone

    Quote Originally Posted by wizard_hu_ View Post
    You may want something like "Version Tolerant Serialization" http://msdn.microsoft.com/en-us/libr...v=vs.100).aspx.
    This looks useful. I have no idea how i missed it. Will look into it.

    Quote Originally Posted by wizard_hu_ View Post
    If there is no built-in support, any XML parser can do the job: create the object with default values, then traverse the XML and set the fields of the object accordingly. Fields not mentioned in the XML will remain to contain their default values.
    This is what XmlSerializer do right.? So according to you if i initialize my new field to its default value then XmlSerializer will still deserialize old data without throwing exceptions??

  5. #5
    Registered User
    Join Date
    Mar 2013
    Location
    Goa, India
    Posts
    32

    Re: Custom serialization support in windows phone

    Quote Originally Posted by to_pe View Post
    If you really want a clean and robust solution, then you should build one for yourself. You can try binary serialization/deserialization with optional "version" property on the beginning of the entire block which will inform your deserialization method that some additional stuff may be necessary.
    I guess robust solution is what i want. As wizard_hu_ pointed, will look into Binary Serialization (Version Tolerant Serialization)..

    Regards

  6. #6
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,751

    Re: Custom serialization support in windows phone

    Ehm, sorry. When searching I have not checked WP availability. BinaryFormatter is reportedly unavailable for WP7. If you develop for WP8, that may be different.

  7. #7
    Registered User
    Join Date
    Mar 2013
    Location
    Goa, India
    Posts
    32

    Re: Custom serialization support in windows phone

    Hi wizard_hu_ ,
    Quote Originally Posted by wizard_hu_ View Post
    You may want something like "Version Tolerant Serialization" http://msdn.microsoft.com/en-us/libr...v=vs.100).aspx.
    Unable to use BinaryFormatter or SoapFormatter. I guess like ISerializable these API's too are removed from windows phone API bundle.

  8. #8
    Registered User
    Join Date
    Mar 2013
    Location
    Goa, India
    Posts
    32

    Re: Custom serialization support in windows phone

    Quote Originally Posted by wizard_hu_ View Post
    Ehm, sorry. When searching I have not checked WP availability. BinaryFormatter is reportedly unavailable for WP7. If you develop for WP8, that may be different.
    Does not works for Windows phone 8 too.. Will check on Xml stuff and update soon.

  9. #9
    Nokia Developer Champion
    Join Date
    Sep 2012
    Location
    Morocco
    Posts
    297

    Re: Custom serialization support in windows phone

    Have you tryed NewtonSoft's Json.NET librairy ? (you can get it from nuget), that's not XML as the name shows, but there is a JsonConvert.Serialization(objet) method which may help you do the job ( didn't try it, I only use Deserialization one)

  10. #10
    Registered User
    Join Date
    Mar 2013
    Location
    Goa, India
    Posts
    32

    Re: Custom serialization support in windows phone

    Hi all,
    I was using XmlSerializer earlier but as wizard_hu_ pointed out XmlSerializer can be used to support revision changes. i.e you can create parser extension methodes while deserializing you Xml to initilize missing fields with default values. Dont even need to store revision number(atleast in my case) and since XmlSerializer is a first party API its good to use it over any other third party library.

    //gameList is your xml
    var doc = XDocument.Parse(gameList);

    var items = doc.Descendants(XNameConstants.Game).Select(entryElement => new Game()
    {
    Name = entryElement.Descendants(XNameConstants.First).Single().Value;
    Points = entryElement.Descendants(XNameConstants.DriverPin).GetPointsInt32OrDefault();
    });

    //parser extension method
    public Int32 GetPointsInt32OrDefault(this XElement element)
    {
    Int32 parsedValue = 10; //default value
    return (element == null || !Int32.TryParse(element.Value, out parsedValue)) ? 0 : parsedValue;
    }
    Regards

  11. #11
    Registered User
    Join Date
    Mar 2013
    Location
    Goa, India
    Posts
    32

    Re: Custom serialization support in windows phone

    Hi Loukt,
    Quote Originally Posted by Loukt View Post
    Have you tryed NewtonSoft's Json.NET librairy ? (you can get it from nuget), that's not XML as the name shows, but there is a JsonConvert.Serialization(objet) method which may help you do the job ( didn't try it, I only use Deserialization one)
    This library seems much powerful then XmlSerializer. But since i had used XmlSerializer earlier using the same thing is easy for me than using a completely different stuff. If at all i will find any problems with XmlSerializer i will report. Thanks for you suggestions.

    Regards

  12. #12
    Nokia Developer Champion
    Join Date
    Feb 2013
    Location
    Dublin, Ireland
    Posts
    572

    Re: Custom serialization support in windows phone

    For future reference, there's also ServiceStack.Text which is an incredibly fast JSON serialiser...

    https://github.com/ServiceStack/ServiceStack.Text or via NuGet.

  13. #13
    Registered User
    Join Date
    Mar 2013
    Location
    Goa, India
    Posts
    32

    Re: Custom serialization support in windows phone

    Quote Originally Posted by theothernt View Post
    For future reference, there's also ServiceStack.Text which is an incredibly fast JSON serialiser...

    https://github.com/ServiceStack/ServiceStack.Text or via NuGet.
    Thank you theothernt. I will try it with a sample application.

Similar Threads

  1. Replies: 1
    Last Post: 2013-02-14, 17:47
  2. rSAP - Remote SIM Access Profile support in Windows Phone 8
    By Cosima16 in forum Windows Phone General
    Replies: 1
    Last Post: 2012-11-19, 10:57
  3. Nokia Ad Exchange Does not support Windows Phone 8
    By Beep2Bleep in forum Windows Phone General
    Replies: 4
    Last Post: 2012-11-18, 15:26
  4. D211: new Windows Drivers/SDK & Support for Windows 2003?
    By bsmuehmer in forum Multimodecards
    Replies: 1
    Last Post: 2003-08-08, 12:21
  5. Replies: 1
    Last Post: 2002-07-16, 06:44

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×