C#Serialize / DeSerialize自定义类的数组

时间:2013-12-08 19:51:51

标签: c# arrays class serialization

我有:

Stereo [] noteBuffer = new noteBuffer[ 128 ];

{ ... fill it.... }

其中:

public class Stereo
{
    public int Samples;
    public float [] L,R;

    public Stereo( int N )
    {
        Samples = N;
        L = new float[ N ];
        R = new float[ N ];
    }   
}

我如何{保存到磁盘} / {从磁盘加载}?

(注意:不同的音符将有不同的长度缓冲区)

4 个答案:

答案 0 :(得分:1)

您可以随时使用Protobuf非常快。

有关此系统性能的详细信息和实施,请参阅http://code.google.com/p/protobuf-net/wiki/Performance

但是如果你想要一个更标准和跨平台的方式来做,这是我的答案(我不认为30 MB会导致问题)

 public void test()
    {
        Stereo [] noteBuffer = new noteBuffer[ 128 ];
       SerializeToXml<List<Stereo>>(noteBuffer.ToList(), outfile);
    }



    public static T DeserializeFromXml<T>(string inputFile)
    {
        XmlSerializer s = new XmlSerializer(typeof(T));
        T deserializedObject = default(T);

        using (TextReader textReader = new StreamReader(inputFile))
        {
            deserializedObject = (T)s.Deserialize(textReader);
            textReader.Close();
        }

        return deserializedObject;
    }

    public static void SerializeToXml<T>(T objToSerialize, string outputFile)
    {

        XmlSerializer s = new XmlSerializer(objToSerialize.GetType());

        using (TextWriter textWriter = new StreamWriter(outputFile))
        {
            s.Serialize(textWriter, objToSerialize);
            textWriter.Close();
        }
    }

答案 1 :(得分:1)

对于一个简单的类,我只想写一个静态函数,它可以将Stereo数组写入流中,而另一个可以将它们再次读出来。

internal class Program
{
    public static void Main()
    {
        Stereo[] stereos = FunctionThatCreatesTheArray();

        using (var writeStream = File.OpenWrite(@"C:\Example\MultiChannelSong.bin"))
        {
            Stereo.Serialize(stereos, writeStream);
        }

        Stereo[] newStereos;
        using (var readStream = File.OpenRead(@"C:\Example\MultiChannelSong.bin"))
        {
           newStereos = Stereo.Deseralize(readStream);
        }

    }
}

public class Stereo
{
    public int Samples;
    public float[] L, R;

    public Stereo(int N)
    {
        Samples = N;
        L = new float[N];
        R = new float[N];
    }

    public static void Serialize(Stereo[] stereos, Stream destination)
    {
        using (BinaryWriter writer = new BinaryWriter(destination, Encoding.Default, true))
        {
            //Write the size of the array to the file
            writer.Write(stereos.LongLength);
            foreach (Stereo stereo in stereos)
            {
                //Write the number of samples there are
                writer.Write(stereo.Samples);

                //Write out the L channel
                foreach (var l in stereo.L)
                {
                    writer.Write(l);
                }

                //Write out the R channel
                foreach (var r in stereo.R)
                {
                    writer.Write(r);
                }
            }
        }
    }

    public static Stereo[] Deseralize(Stream source)
    {
        using (BinaryReader reader = new BinaryReader(source, Encoding.Default, true))
        {
            //Read in the number of records
            var records = reader.ReadInt64();
            Stereo[] stereos = new Stereo[records];

            for (long i = 0; i < records; i++)
            {
                //Read in the number of samples
                var samples = reader.ReadInt32();
                var stereo = new Stereo(samples);

                //Read in the L channel
                for (int j = 0; j < samples; j++)
                {
                    stereo.L[j] = reader.ReadSingle();
                }

                //Read in the R channel
                for (int j = 0; j < samples; j++)
                {
                    stereo.R[j] = reader.ReadSingle();
                }

                //Set the sterieo object we created in to the array.
                stereos[i] = stereo;
            }
            return stereos;
        }
    }
}

如果你想要时髦,你可以压缩输出和输入,以帮助节省空间。

public static void Serialize(Stereo[] stereos, Stream destination)
{
    using (GZipStream compressionStream = new GZipStream(destination, CompressionMode.Compress, true))
    using (BinaryWriter writer = new BinaryWriter(compressionStream, Encoding.Default, true))
    {
        //No changes here required
    }
}

public static Stereo[] Deseralize(Stream source)
{
    using (GZipStream decompressionStream = new GZipStream(source, CompressionMode.Decompress, true))
    using (BinaryReader reader = new BinaryReader(decompressionStream, Encoding.Default, true))
    {
        //No changes here required
    }
}

答案 2 :(得分:1)

至少可以使用以下序列化技术:

可以在this answerXmlSerializer here

中找到XmlSerializer的使用情况

有效性分析在this answer

中提供

答案 3 :(得分:0)

我通常使用Json.NET。请参阅:http://james.newtonking.com/archive/2009/02/14/writing-json-to-a-file-using-json-net

虽然它不像BinaryFormatter那样具有空间效果,但在更改对象的签名方面要好几英里。