Serializing cyclic object references using DataContractSerializer not working

戏子无情 提交于 2019-12-10 10:17:11

问题


I'm building an XNA game and I'm trying to save game/map etc. state completely, and then be able to load and resume from exactly the same state.

My game logic consists of fairly complex elements (for serializing) such as references, delegates etc. I've done hours of research and decided that it's the best to use a DataContractSerializer that preserves the object references. (I also got around for delegates but that's another topic) I have no problem serializing and deserializing the state, re-creating the objects, the fields, lists, and even object references correctly and completely. But I've got a problem with cyclic references. Consider this scenario:

class X{
   public X another;
}

//from code:
X first = new X();
X second = new X();
first.another = second;
second.another = first;

Trying to serialize X will result in an exception complaining about cyclic references. If I comment out the last line it works fine. Well, I can imagine WHY it is happening, but I have no idea HOW to solve it. I've read somewhere that I can use the DataContract attribute with IsReference set to true, but it didn't change anything for me -- still got the error. (I want to avoid it anyway since the code I'm working on is portable code and may someday run on Xbox too, and portable library for Xbox doesn't support the assembly that DataContract is in.)

Here is the code to serialize:

class DataContractContentWriterBase<T> where T : GameObject
{
    internal void Write(Stream output, T objectToWrite, Type[] extraTypes = null)
    {
        if (extraTypes == null) { extraTypes = new Type[0]; }
        DataContractSerializer serializer = new DataContractSerializer(typeof(T), extraTypes, int.MaxValue, false, true, null);
        serializer.WriteObject(output, objectToWrite);
    }
}

and I'm calling this code from this class:

[ContentTypeWriter]
public class PlatformObjectTemplateWriter : ContentTypeWriter<TWrite>
(... lots of code ...)
    DataContractContentWriterBase<TWrite> writer = new DataContractContentWriterBase<TWrite>();
    protected override void Write(ContentWriter output, TWrite value)
    {
        writer.Write(output.BaseStream, value, GetExtraTypes());
    }

and for deserialization:

class DataContractContentReaderBase<T> where T: GameObject
{
    internal T Read(Stream input, Type[] extraTypes = null)
    {
        if (extraTypes == null) { extraTypes = new Type[0]; }
        DataContractSerializer serializer = new DataContractSerializer(typeof(T), extraTypes, int.MaxValue, false, true, null);
        T obj = serializer.ReadObject(input) as T;
        //return obj.Clone() as T; //clone falan.. bi bak iste.
        return obj;
    }
}

and it's being called by:

public class PlatformObjectTemplateReader : ContentTypeReader<TRead>
(lots of code...)
    DataContractContentReaderBase<TRead> reader = new DataContractContentReaderBase<TRead>();
    protected override TRead Read(ContentReader input, TRead existingInstance)
    {
        return reader.Read(input.BaseStream, GetExtraTypes());
    }

where:

PlatformObjectTemplate was my type to write.

Any suggestions?

SOLUTION: Just a few minutes ago, I've realized that I wasn't marking the fields with DataMember attribute, and before I added the DataContract attribute, the XNA serializer was somehow acting as the "default" serializer. Now, I've marked all the objects, and things are working perfectly now. I now have cyclic references with no problem in my model.


回答1:


If you don't want to use [DataContract(IsReference=true)] then DataContractSerializer won't help you, because this attribute is the thing that does the trick with references.

So, you should either look for alternative serializers, or write some serialization code that transforms your graphs into some conventional representation (like a list of nodes + a list of links between them) and back, and then serialize that simple structure.

In case you decide to use DataContract(IsReference=true), here's a sample that serializes your graph:

[DataContract(IsReference = true)]
class X{
  [DataMember]
  public X another;
}

static void Main()
{
  //from code:
  var first = new X();
  var second = new X();
  first.another = second;
  second.another = first;

  byte[] data;
  using (var stream = new MemoryStream())
  {
    var serializer = new DataContractSerializer(typeof(X));

    serializer.WriteObject(stream, first);
    data = stream.ToArray();
  }
  var str = Encoding.UTF8.GetString(data2);
}

The str will contain the following XML:

<X z:Id="i1" xmlns="http://schemas.datacontract.org/2004/07/GraphXmlSerialization"
   xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
  <another z:Id="i2">
    <another z:Ref="i1"/>
  </another>
</X>


来源:https://stackoverflow.com/questions/8290456/serializing-cyclic-object-references-using-datacontractserializer-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!