0

I'm about serialize a paket with information, which act as an answer on a incoming udp paket. The incoming UDP paket is very compact and contains exactly the information I want to see.

When I send my reply, the packet is nearly 200 times bigger (1036 byte against 57 byte..) and contains a lot of white spaces, name of the class instance and it's structure (like nullable and so on).

The question may sounds wide but what are the problem here? I understand that my solution is serialize the whole object, including it's class name, namespace and so on. The recipient don't need, won't and Can't take this info. They need to go directly into the data structure.

4
  • 1
    You could have a look at Protobuf - it's much more compact serialization than the built-in stuff. Aug 22, 2011 at 9:58
  • @Jonathan, Protobuf.. your probably right about that. I try to not involve third party because they also take time and effort to understand and use effectively. How would this FW make different to the use I try above? Aug 22, 2011 at 11:28
  • 1
    I have used it a limited amount, but from my experience it is quite easy to pick up and Marc Gravell wrote it (implying it is quite stable). Aug 22, 2011 at 11:30
  • @Jonathan, indeed on that implying. I suggest you to put the comment to an answer, Aug 23, 2011 at 6:23

2 Answers 2

1

You should probably try out Protobuf. It supports the attributed serialization model (in fact, it supports several attributed models, including the DataContract WCF model), and is significantly faster than built-in .Net binary serialization (faster than even DataContract serialization). Protobuf is also designed to be as compact as possible - so you should have fewer problems with packet size.

Edit: You could also implement your own serialization paradigm, by using an interface. For example:

public interface IBinarySerializable
{
    void Serialize(BinaryWriter writer);
    void Deserialize(BinaryReader reader);
}

public static class BinaryReaderWriterExtensions
{
    public static void Write(this BinaryWriter writer, IBinarySerializable value)
    {
        value.Serialize(writer);
    }

    public static T Read<T>(this BinaryReader reader)
        where T : IBinarySerializable, new()
    {
        var val = new T();
        val.Deserialize(reader);
        return val;
    }

    public static void ReadInto(this BinaryReader reader, IBinarySerializable value)
    {
        value.Deserialize(reader);
    }

    public static void WriteList<T>(this BinaryWriter writer, IList<T> list, Action<BinaryWriter, T> singleValueWriter)
    {
        writer.Write(list.Count);
        foreach (var item in list)
            singleValueWriter(writer, item);
    }

    public static IEnumerable<T> ReadList<T>(this BinaryReader reader, Func<BinaryReader, T> singleValueReader)
    {
        var ct = reader.ReadInt32();
        for (var i = 0; i < ct; i++)
            yield return singleValueReader(reader);
    }
}

public class WantsToBeSerialized : IBinarySerializable
{
    public int ID;
    public string CustomerName;
    public List<string> Nicknames;
    public SomeOtherSerializableObject Thing;

    void IBinarySerializable.Serialize(BinaryWriter writer)
    {
        writer.Write(ID);
        writer.Write(CustomerName);
        writer.WriteList(Nicknames, (w, value) => w.Write(value));
        writer.Write(Thing);
    }

    void IBinarySerializable.Deserialize(BinaryReader reader)
    {
        ID = reader.ReadInt32();
        CustomerName = reader.ReadString();
        Nicknames = new List<string>(reader.ReadList(x => x.ReadString()));
        Thing = reader.Read<SomeOtherSerializableObject>();
    }
}
3
  • Thank's. I think this is the best answer even if it don't helped me. Protobuf is a framework and the biggest approach to me was the formatter. Which mean, make the packet that will be serialized. The remote system has of a very-very low level of communication. Aug 26, 2011 at 8:34
  • @Jonas I made another alternative for you. Aug 26, 2011 at 9:02
  • Aah that was a very handy and super-green way to go! I will dig into that when I reached the end of the protocol spec. Aug 26, 2011 at 9:05
0

The problem here is, that your counterpart is expecting a flat, xml-like structure, which may look like XML, but is not valid/wellformed according the xml specifications.

You can tweak the .net serializers (DataContract or XMLSerializer), as long as you have in mind that what you´re outputting might no longer be a valid or wellformed. Check the following two links, they should point you in the right direction.

EDIT: Added BinaryFormatting using XMLSerializer, not sure about your output format but maybe this is a help.

class Program
{
    static void Main(string[] args)
    {
        TestObject data = new TestObject() { Name = "Claus", Firstname = "Santa"};

        MemoryStream stream = new MemoryStream();

        XmlSerializerNamespaces xsn = new XmlSerializerNamespaces();
        xsn.Add(String.Empty, String.Empty);

        XmlSerializer serializer = new XmlSerializer(typeof(TestObject));

        XmlDictionaryWriter binaryDictionaryWriter = XmlDictionaryWriter.CreateBinaryWriter(stream);

        serializer.Serialize(binaryDictionaryWriter, data,xsn);

        binaryDictionaryWriter.Flush();

        stream.Seek(0, SeekOrigin.Begin);

        StreamReader reader = new StreamReader(stream);

        string s = reader.ReadToEnd();
    }
}

[Serializable()]
public class TestObject
{
    [XmlAttribute]
    public string Name { get; set; }

    [XmlIgnore]
    public string Firstname { get; set; }

}
4
  • Thank's. This is a pure binary protocol. No XML is involved here, whch are a layer beyond from this. Aug 22, 2011 at 11:27
  • .NET BinaryReader / BinaryFormatter Aug 23, 2011 at 6:21
  • @Jonas I edited my answer, as mentioned I´m not sure about your output format but above shows how to tweak the XML serializer to omit selected properties, given you have access to the object to be serialized.
    – Dominik
    Aug 23, 2011 at 8:44
  • Thank's dominik. It looks okay but it's still like creating Iphone-Apps for Android. This is an xml-serialized output and i develope for pure binary stream. Which means i do a byte[] and tell the stream exactly what I want and when (first byte can be a begin-switch, second byte could contain the length of a following string) and so on. You can have a look at this: stackoverflow.com/questions/1092020/… Aug 23, 2011 at 17:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.