2

データをシリアル化するための単純なデータシリアライザーを作成しました。データをシリアル化および逆シリアル化するための非常に簡単な方法があります。それを行うには2つの方法があります。

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;
        }
    }

コードを実行しているサーバーは mono 3.2.1 を使用した ubuntu です。Windows では問題ありません。Windows NET 4 ではエラーなく簡単にコードを実行できます。しかし、mono は DataContractSerializer をサポートしていないようです。mono でエラーが発生しました。は:

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] (IEnumerable1 source, System.Func2 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

4

0 に答える 0