Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

XML Serialization has stopped working on iOS since Unity 3.5.1

Discussion in 'iOS and tvOS' started by DaveLloyd, Apr 24, 2012.

  1. DaveLloyd

    DaveLloyd

    Joined:
    Nov 15, 2011
    Posts:
    21
    We've been using XML Serialization to save our application's data but it seems to be broken in Unity 3.5.1.

    The problem seems to be with building the necessary methods for the serializer ahead-of-time. I have a couple of test classes like the following:
    Code (csharp):
    1.  
    2. [XmlInclude( typeof( TextElement ) )]
    3. public class Element
    4. {
    5.     public String name;
    6. }
    7.  
    8. public class TextElement : Element
    9. {
    10.     public String text;
    11. }
    12.  
    13. public class Container
    14. {
    15.     public List< Element > elements;
    16. }
    17.  
    18.  
    When I try to write a Container object I get
    This worked before but doesn't any longer although it should if my understanding of the AOT rules with Mono and iOS are correct. I think there was a specific assembly created to do this before. Anyone know what I can do to enable this again?

    PS: Curiously reading such objects back still seems to work...
     
  2. gulgi

    gulgi

    Joined:
    Apr 18, 2012
    Posts:
    9
    I have a similar issue..

    Serialization has stopped working.
    Using JSON though... and the error I have seen is this:
    System.MissingMethodException: Method not found: 'Default constructor not found...ctor() of System.ComponentModel.Int32Converter'. when trying to deserialize the data.

    I am reverting to 3.5.0 and hope that "just works".

    If anyone has a solution/fix, please post.
     
  3. DaveLloyd

    DaveLloyd

    Joined:
    Nov 15, 2011
    Posts:
    21
    I can definitely confirm that my trivial test example works with Unity 3.5.0 but not with 3.5.1.
    Looks like I'll have to revert my main project back to 3.5.0.
     
  4. gulgi

    gulgi

    Joined:
    Apr 18, 2012
    Posts:
    9
    Thx for that confirm.

    My problem remains although I reverted back to 3.5.0.
    I'll see if XML might solve my issues.. even thoiugh JSON is easier on the eyes.
     
  5. techmage

    techmage

    Joined:
    Oct 31, 2009
    Posts:
    2,133
    Are you using iOS Pro? Have you submitted a bug report already?
     
  6. techmage

    techmage

    Joined:
    Oct 31, 2009
    Posts:
    2,133
    I'm using xml serialization and deserialization no issues... I have all the latest unity and Xcode versions, both unity pro and iOS advanced
     
  7. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    dito
     
  8. spree

    spree

    Joined:
    Apr 20, 2009
    Posts:
    20
    Have you checked if API compatibility in build settings is set to ".net2.0 subset"? It seems that only that allows you to use generics in combination with serialization/reflection. I thought I had the same problem as you (stopped working with 3.5.1 was working in 3.5.0) but finally realized that when I switched to 3.5.1 the api compatibility setting in the project settings changed. No idea why it did, but it did and that caused all the problems for me.
     
  9. pahe

    pahe

    Joined:
    May 10, 2011
    Posts:
    541
    This problem is occuring in my project right now. I'm using Unity 3.5.2 and get this strange xml serialization problem on my iOS device. Does anyone know how to fix this, but reverting to 3.5.0?
     
  10. DaveLloyd

    DaveLloyd

    Joined:
    Nov 15, 2011
    Posts:
    21
    Do check your build settings as spree recommended - this made the difference for me.
     
  11. pahe

    pahe

    Joined:
    May 10, 2011
    Posts:
    541
    Double checked it. .NET 2.0 and only normal "assembly stripping" option. Tried disabled assembly stripping too but no difference.
     
  12. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    are you trying to serialize / deserialize generics of generics?
    that will generally not work without extra steps
     
  13. pahe

    pahe

    Joined:
    May 10, 2011
    Posts:
    541
    Yes. I have lists in lists in a new xml file. What do I have to change to make it work?

    Code (csharp):
    1.  
    2. public class ActionLevelDatas
    3. {
    4.     public List<ActionLevelData> LevelDatas = new List<ActionLevelData>();
    5. }
    6.  
    7. public class ActionLevelData
    8. {
    9.     public string LevelName;
    10.     public List<EnemyData> Enemies = new List<EnemyData>();
    11. }
    12.  
    13. public class EnemyData
    14. {
    15.     public string EnemyName;
    16. }
    I want to deserialize the ActionLevelDatas with standard .NET xml deserializer:

    Code (csharp):
    1.  
    2.             XmlSerializer ser = new XmlSerializer(typeof(ActionLevelDatas));
    3.             StringReader reader;
    4.  
    5.             TextAsset ta = Resources.Load("ActionStageDatas/ActionStageDatas") as TextAsset;
    6.             reader = new StringReader(ta.text);
    7.  
    8.             if (ser != null  reader != null)
    9.             {
    10.                 ActionLevelDatas = (ActionLevelDatas)ser.Deserialize(reader);
    11.                 reader.Close();
    12.             }
    13.  
     
  14. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Thats not directly a generic of generic as you have it inside a class.

    None the less I would suspect it to be the same as above might apply and the solution then would be to implement the serialize and deserialize functions xml uses and extending it from the corresponding XML interface.
     
  15. DaveLloyd

    DaveLloyd

    Joined:
    Nov 15, 2011
    Posts:
    21
    Your code should work - I have similar. Normally you only need to do extra work for subclasses (XmlInclude).
    You say you have .NET 2.0. I could not get that to work. Only .NET 2.0 Subset works for me. (Which was v confusing!)
     
  16. pahe

    pahe

    Joined:
    May 10, 2011
    Posts:
    541
    Wtf... that worked! Thanks man! Couldn't explained neither. Many, many thanks! I was about to change my serializer to JSON or somewhat ever just to make it work.
     
  17. handsomePATT

    handsomePATT

    Joined:
    Nov 30, 2010
    Posts:
    574
    ah changing it to subset worked for me also! that could have been a huge waste of time haha.
     
  18. FatWednesday

    FatWednesday

    Joined:
    Jul 4, 2012
    Posts:
    50
    I'm getting the same problem for my code. It used to work fine, but has recently been throwing up errors when we deploy to device.

    I've tried using both .Net 2.0 and .Net 2.0 Subset API targets, neither seem to work.

    I thought at first it might be a problem with the SerializableDictionary I was using, but a new class were serializing that just contains standard value types seems to cause problems as well.

    Unity version being used is 3.5.5f2 (Pro, and iPhone Pro) Any suggestions that don't involve rolling back to an old unity build?
     
  19. PAHeartBeat

    PAHeartBeat

    Joined:
    Jul 11, 2012
    Posts:
    76
    Hi bro

    @techmage

    I am working with Unity 3.5.4f pro and iOS Pro with XCOde 4.5 but as other said, it's throw exception of initializing the configuration system
    it working fine on MAC os but throws error on iOS Device.... :(
     
  20. handsomePATT

    handsomePATT

    Joined:
    Nov 30, 2010
    Posts:
    574
    I have latest version and it is working for me on ios and android devices, as well as mac.
     
  21. PAHeartBeat

    PAHeartBeat

    Joined:
    Jul 11, 2012
    Posts:
    76
    Hi HandsomePATT

    I have tried a lot on iOS Device to test it;s crash / throw exception every time for me :(

    Code (csharp):
    1. public static string Serialize(List<PlayerPrefebs> tData) {
    2.             XmlSerializer serializer = new XmlSerializer(typeof(List<PlayerPrefebs>));
    3.             TextWriter writer = new StringWriter();
    4.             serializer.Serialize(writer, tData);
    5.             return writer.ToString();
    6.         }
    7.         public static string Serialize(PlayerPrefebs tData) {
    8.             XmlSerializer serializer = new XmlSerializer(typeof(PlayerPrefebs));
    9.             TextWriter writer = new StringWriter();
    10.             serializer.Serialize(writer, tData);
    11.             return writer.ToString();
    12.         }
    I have load my code to serialize a data. it does not works for me. and yes I used Generics list in my class to maintain arrays.

    thanks in advance
     
  22. PAHeartBeat

    PAHeartBeat

    Joined:
    Jul 11, 2012
    Posts:
    76
    Hi Frineds


    it's working now I have to change My code little bit and yes it's little confusing the solution is working with only .Net 2.0 Subset

    I just include
    Code (csharp):
    1. [XmlInclude(typeof(Bike))]
    to my class for all sub class or class type which are used in my class which i want to serialize.

    And iOS can't XML Serialize Generics List Directly but it can serialize generics list if your class. so i have change my code it's looks now like this
    Code (csharp):
    1.  
    2.         public static string Serialize(PlayerPrefebs tData) {
    3.             XmlSerializer serializer = new XmlSerializer(typeof(PlayerPrefebs));
    4.             TextWriter writer = new StringWriter();
    5.             serializer.Serialize(writer, tData);
    6.             return writer.ToString();
    7.         }
    8.        
    9.         public static PlayerPrefebs Deserialize(string tData) {
    10.             XmlSerializer serializer = new XmlSerializer(typeof(PlayerPrefebs));
    11.             TextReader reader = new StringReader(tData);
    12.             return (PlayerPrefebs)serializer.Deserialize(reader);
    13.         }
    14.  
    thanks to all of who indirectly contributes their idea to solve the issue
     
  23. ev3d

    ev3d

    Joined:
    Apr 19, 2013
    Posts:
    327
    does anyone know WHY it stopped working? Is this a bug?
     
  24. FatWednesday

    FatWednesday

    Joined:
    Jul 4, 2012
    Posts:
    50
    As far as I'm aware this issue was fixed several versions ago. We use XmlSerialization of custom classes in our projects and have been doing so for a while. We also are serializing lists and arrays of these types. The Dictionary needed changing, so we have a SerializableDictionary class that we found online to handle that but otherwise I see no current problems with XmlSerialization on iOS.
     
  25. Maldita

    Maldita

    Joined:
    Oct 4, 2012
    Posts:
    2
    umm hello, i have a similar problem to this.. You see i need to serialize some data to an XML file, and then email it as an attachment. I have found out that the XMLSerializer works fine in Net 2.0 subset in the Editor, but upon building it for iOS, it sends an error about JIT and is unable to serialize. Switching to Net 2.0 on player prefs solves the serialization problem, but then the smtp method of sending the mail fails on authentication errors. The smtp method works fine on Net 2.0 on both Editor and iOS device. I've tried Application.OpenURL(mailto ) but it doesn't support attachments. Any idea as to how to serialize on Net 2.0 on iOS or to send emails with attachments on 2.0 Subset? I found U3DXT and Prime[31] plug ins, but I don't want to purchase plug ins if there is just another way..