Rehan Manzoor
Rehan Manzoor

Reputation: 2753

Best practice/way to serialize the object using c#

im working on collaborative drawing (socket-programming) in which i have to send and recieve 100 to 1000 Point of C# Class, so i wanted to know that what is the best way to send and recieve these points.. i have two options.. one is List<Point> and other is Point[] using either BinaryFormatter or JSON, but i have read that JSON is used to send small amount of data, and i dont know will it work with C# window applications or not thanx for any help

Upvotes: 0

Views: 1930

Answers (1)

Piotr Zierhoffer
Piotr Zierhoffer

Reputation: 5151

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 (e.g. 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);
    }
}
}

Upvotes: 3

Related Questions