- Home /
DataContract serialization not compatible with Unity?
Hi everyone,
I've ran into couple of problems using DataContractSerializer within Unity. After many hours of debugging I've decided to copy the classes I'm trying to serialize/deserialize to Visual Studio solution and try it without Unity. I used target framework "Unity .Net 3.5 Subset Base Class Libraries".
These are things I found not working well within Unity while Visual Studio solution worked perfectly fine:
Visual Studio had no problems deserializing empty array while Unity kept throwing
ArgumentException: Object type System.Collections.ArrayList cannot be converted to target type: MyNamespace.MyType[]
Circular references where handled nicely within Visual Studio with
DataContract(IsReference=true)
. Unity kept throwingSerializationException: Deserialized object with reference Id 'i1' was not found
Methods with attached attributes
OnDeserialized
,OnSerialized
,OnSerializing
,OnDeserializing
were not called within Unity
I was hoping to convert empty list to null before serialization and set circular references after deserialization in callback functions but it turned out to be a problem itself.
I chose DataContractSerializer instead of XmlSerializer because DataContractSerializer can cope with circular references and dictionaries which XmlSerializer supposedly can't.
So, my question is simply this: Are there known difficulties with using DataContractSerializer within Unity or is this just me not knowing something simple? Which serializer is preferred in Unity to save data to Xml? What's with these callback methods not being called? I'm quite new in serialization so any information would be helpful.
I'm definitely not an expert, but i'm using the DataContractSerializer too (for a dialog system). I also had many problems, but most were either related to deserializing unity types which i had to replace or fixed by using the $$anonymous$$nownTypes option. i only use the generic list, which works for me and had no problems with circular references. Apart from that you could also have a look at unity's ISerializationCallbackReceiver interface, depending on what you want to do.
Thank you for your comment.
I also used $$anonymous$$nownTypes to specify concrete types in my class hierarchy and I was serializing only classes that I wrote. Interesting point is that my code worked fine in Visual Studio so I can't say I was doing something wrong. It just didn't work in Unity and kept throwing errors I wrote above.
What just came to my $$anonymous$$d now is that I tried to serialize a hierarchy that was not derived from $$anonymous$$onoBehaviour nor ScriptableObject. Of course Visual Studio had no problems with that, but maybe Unity had. $$anonymous$$aybe Unity can serialize/deserialize a single class not derived from $$anonymous$$onoBehaviour/ScriptableObject but has a problem with polymorphic serialization of hierarchy that was not derived from $$anonymous$$onoBehaviour or ScriptableObject... Do you have any experience with that kind of serialization in Unity?
ISerializationCallback is called on every attempt of serialization or just serialization with Unitys built-in serializer?
Have you had any luck with this? I have a similar problem. I wrote a very simple test script for the serialiser:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
public class SerializerTest : $$anonymous$$onoBehaviour
{
public string added = "";
[Context$$anonymous$$enu("Save")]
void Save()
{
TestData data = new TestData();
for (int i = 0; i < 2; i++) { data.test[i] = new TestClass(); }
data.test[0][0] = data.test[1];
data.test[1][0] = data.test[0];
Serializer.Save<TestData>(gameObject.name+added, data);
}
[Context$$anonymous$$enu("Load")]
void Load() { TestData data = Serializer.Load<TestData>(gameObject.name+added); }
}
[DataContract(IsReference = true)]
public class TestClass
{
public TestClass this[int i] { get { return test[i]; } set { test[i] = value; } }
[Data$$anonymous$$ember] public TestClass[] test = new TestClass[2];
}
[DataContract]
public class TestData
{
[Data$$anonymous$$ember] public TestClass test = new TestClass();
}
For some reason it serialises and deserialises perfectly on windows within unity. But when I try exactly the same code on a mac within unity I get an exception on deserialisation:
SerializationException: Deserialized object with reference Id 'i1' was not found
It feels like an issue with unity's $$anonymous$$ono/.NET implementation.
Your answer
Follow this Question
Related Questions
Problems with the XML Serializer 1 Answer
"Root Element Missing: XML Exception" when trying to load data from XML file 2 Answers
Xml Serialization of "sub classes" 0 Answers
URGENT - Cannot be serialized because it does not have a default public constructor.... 1 Answer
Unity Serialization with XML Root problem: 'Does Not Denote Valid Type' 0 Answers