简体   繁体   中英

Best practice/way to serialize the object using c#

我正在进行协作绘图(套接字编程),其中我必须发送和接收100到1000 Point of C# Class ,所以我想知道发送和接收这些点的最佳方法是什么。我有两个选择。 。一个是List<Point> ,另一个是使用BinaryFormatterJSON Point[] ,但是我已经读过JSON用于发送少量数据,我不知道它是否可以与C# window applications或者不适用于任何救命

There are so many ways to serialize your data.

If you want to transfer a lot of objects, don't use JSON - it's a text format, every unnecessary character is a byte waste of space. If your object uses, say, 20 bytes, and it's textual representation uses 100 bytes (eg because of the field names) than it's a bad choice for large collections, especially with network transfer.

Unless you need the serialized output to be readable, of course. That's, I believe, the only reason to use JSON.

Binary serialization is totally a different matter. There are many serializers out there: BinaryFormatter , protobuf-net by Marc Gravell, Migrant (which I co-author), many many others.

The choice is hard and domain-specific. Do you need graph dependency preserved? Do you have many different objects or large collections? Different libraries will give you different results.

As your data set is not very big (I assume we're talking about a small class/struct Point ), I'd focus on readability. You don't want to design your code to be easily serializable, and you seldom want to write wrappers (because they have to be maintained).

Use datatypes that are meaningful in your context.

Do you need random access to your Points? Then probably you need an array. Do you need to create a resizable collection and iterate over it? List might be better.

I've ran a simple test, using 1000000 System.Windows.Point instances, both on BinaryFormatter and Migrant . There was no real difference whether I've used Point[] or List<Point> .

The choice is yours.

Here is a snippet of the test I've done. The code is not very pimped-up, but you'll change it to List<Point> with no effort. If you need a simple framework to serialize data, I may recommend Migrant . Please notice the one-liner usage: result = Serializer.DeepClone (source); ;-)

using System;
using Antmicro.Migrant;
using System.Diagnostics;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Windows;

namespace test15
{
class MainClass
{
    private static void EnsureEqual(Point[] source, Point[] result)
    {
        for (var i = 0; i < result.Length; ++i) {
            if (source [i] != result [i]) {
                throw new Exception ();
            }
        }
    }

    public static void Main (string[] args)
    {
        var source = new Point[1000000];
        Point[] result, binResult;
        var timer = new Stopwatch ();
        for (var i = 0; i < source.Length; ++i) {
            source [i] = new Point (i, i);
        }

        //Migrant
        timer.Start ();
        result = Serializer.DeepClone (source);
        timer.Stop ();
        EnsureEqual (source, result);
        Console.WriteLine ("Migrant time: {0}", timer.Elapsed);

        timer.Reset ();

        //Binary formatter
        var binaryForm = new BinaryFormatter ();
        using (var ms = new MemoryStream ()) {
            timer.Start ();
            binaryForm.Serialize (ms, source);
            ms.Position = 0;
            binResult = binaryForm.Deserialize(ms) as Point[];
            timer.Stop ();
        }
        Console.WriteLine ("Binary formatter time: {0}", timer.Elapsed);
        EnsureEqual (source, binResult);
    }
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM