我已经创建了一个简单的数据序列化器来序列化我的数据,有这么简单的方法来序列化和反序列化数据,我有两种方法可以做到这一点:
public string Serialize()
{
SerializedData data = new SerializedData(this);
DataContractSerializer serializer = new DataContractSerializer(typeof(SerializedData));
System.IO.MemoryStream s = new System.IO.MemoryStream();
serializer.WriteObject(s, data);
return Convert.ToBase64String(s.ToArray());
}
public static MessageBlock Deserialize(string name, string sdata, Map map)
{
byte[] bdata = Convert.FromBase64String(sdata);
MessageBlock MessageBlock = new MessageBlock();
DataContractSerializer serializer = new DataContractSerializer(typeof(SerializedData));
System.IO.MemoryStream s = new System.IO.MemoryStream(bdata);
SerializedData data = (SerializedData)serializer.ReadObject(s);
data.UpdateMessageBlock(MessageBlock);
return MessageBlock;
}
[DataContract]
private class SerializedData
{
[DataMember]
public String Name;
[DataMember]
public String Creator;
[DataMember]
public DateTime Created;
[DataMember]
public String World;
[DataMember]
public Vector3I AffectedBlock;
[DataMember]
public int XMin;
[DataMember]
public int XMax;
[DataMember]
public int YMin;
[DataMember]
public int YMax;
[DataMember]
public int ZMin;
[DataMember]
public int ZMax;
[DataMember]
public String Message;
public SerializedData(MessageBlock MessageBlock)
{
lock (MessageBlock)
{
Name = MessageBlock.Name;
Creator = MessageBlock.Creator;
Created = MessageBlock.Created;
World = MessageBlock.World;
AffectedBlock = MessageBlock.AffectedBlock;
XMin = MessageBlock.Range.Xmin;
XMax = MessageBlock.Range.Xmax;
YMin = MessageBlock.Range.Ymin;
YMax = MessageBlock.Range.Ymax;
ZMin = MessageBlock.Range.Zmin;
ZMax = MessageBlock.Range.Zmax;
Message = MessageBlock.Message;
}
}
public void UpdateMessageBlock(MessageBlock MessageBlock)
{
MessageBlock.Name = Name;
MessageBlock.Creator = Creator;
MessageBlock.Created = Created;
MessageBlock.World = World;
MessageBlock.AffectedBlock = AffectedBlock;
MessageBlock.Range = new MessageBlockRange(XMin, XMax, YMin, YMax, ZMin, ZMax);
MessageBlock.Message = Message;
}
}
服务器我运行代码是ubuntu使用单声道3.2.1,没有问题的窗口,我可以很容易地运行代码没有任何错误的Windows NET 4,但它似乎单声道不支持DataContractSerializer,错误我上单声道是:
09:04:19 > ERROR: Map failed to save: System.Runtime.Serialization.SerializationException: Circular reference of an object in the object graph was found: '(0,0,-1)' of type fCraft.Vector3I
at System.Runtime.Serialization.SerializationMap.Serialize (System.Object graph, System.Runtime.Serialization.XmlFormatterSerializer serializer) [0x00000] in :0
at System.Runtime.Serialization.XmlFormatterSerializer.Serialize (System.Type type, System.Object graph) [0x00000] in :0
at System.Runtime.Serialization.SerializationMap.SerializeNonReference (System.Object graph, System.Runtime.Serialization.XmlFormatterSerializer serializer) [0x00000] in :0
at System.Runtime.Serialization.SerializationMap.Serialize (System.Object graph, System.Runtime.Serialization.XmlFormatterSerializer serializer) [0x00000] in :0
at System.Runtime.Serialization.XmlFormatterSerializer.Serialize (System.Type type, System.Object graph) [0x00000] in :0
at System.Runtime.Serialization.SerializationMap.SerializeNonReference (System.Object graph, System.Runtime.Serialization.XmlFormatterSerializer serializer) [0x00000] in :0
at System.Runtime.Serialization.SerializationMap.Serialize (System.Object graph, System.Runtime.Serialization.XmlFormatterSerializer serializer) [0x00000] in :0
at System.Runtime.Serialization.XmlFormatterSerializer.Serialize (System.Type type, System.Object graph) [0x00000] in :0
at System.Runtime.Serialization.SerializationMap.SerializeNonReference (System.Object graph, System.Runtime.Serialization.XmlFormatterSerializer serializer) [0x00000] in :0
at System.Runtime.Serialization.SerializationMap.Serialize (System.Object graph, System.Runtime.Serialization.XmlFormatterSerializer serializer) [0x00000] in :0
at System.Runtime.Serialization.XmlFormatterSerializer.Serialize (System.Type type, System.Object graph) [0x00000] in :0
at System.Runtime.Serialization.XmlFormatterSerializer.Serialize (System.Xml.XmlDictionaryWriter writer, System.Object graph, System.Type declaredType, System.Runtime.Serialization.KnownTypeCollection types, Boolean ignoreUnknown, Int32 maxItems, System.String root_ns, Boolean preserveObjectReferences, System.Runtime.Serialization.DataContractResolver resolver, System.Runtime.Serialization.DataContractResolver defaultResolver) [0x00000] in :0
at System.Runtime.Serialization.DataContractSerializer.WriteObjectContent (System.Xml.XmlDictionaryWriter writer, System.Object graph) [0x00000] in :0
at System.Runtime.Serialization.XmlObjectSerializer.WriteObject (System.Xml.XmlDictionaryWriter writer, System.Object graph) [0x00000] in :0
at System.Runtime.Serialization.DataContractSerializer.WriteObject (System.Xml.XmlWriter writer, System.Object graph) [0x00000] in :0
at System.Runtime.Serialization.XmlObjectSerializer.WriteObject (System.IO.Stream stream, System.Object graph) [0x00000] in :0
at fCraft.MessageBlock.Serialize () [0x00000] in :0
at fCraft.MessageBlockSerialization.Serialize (fCraft.Map map, System.IO.Stream stream, IMapConverterEx converter) [0x00000] in :0
at fCraft.MapConversion.MapFCMv3+<>c_DisplayClass4.b_2 (IConverterExtension ex) [0x00000] in :0
at System.Linq.Enumerable.Sum[IConverterExtension] (IEnumerable
1 source, System.Func
2 selector) [0x00000] in :0
at fCraft.MapConversion.MapFCMv3.WriteMetadata (System.IO.Stream stream, fCraft.Map map) [0x00000] in :0
at fCraft.MapConversion.MapFCMv3.Save (fCraft.Map mapToSave, System.String fileName) [0x00000] in :0
at fCraft.MapConversion.MapUtility.TrySave (fCraft.Map mapToSave, System.String fileName, MapFormat format) [0x00000] in :0
09:04:19 > Warning: Logger.SubmitCrashReport: System.UriFormatException: Uri is longer than the maximum 32766 characters.
at System.Uri.EscapeDataString (System.String stringToEscape) [0x00000] in :0
at fCraft.Logger.LogAndReportCrashInner (System.String message, System.String assembly, System.Exception exception) [0x00000] in :0