Serialize network objects .NetCore (Win 10 Store / WinRT)

Hello everyone,
as i am now moving from full .NET Game Development into Windows 10 Store compatible Projects i am facing an serialization issue, and would love to know how you handle your Network communication.

As of today, i serialized my communication classes (Player Object, FireEvent Object, StatsSummary Object etc. etc.) using BinaryFormatter into Byte[] and send it with Lidgren.
Now, thanks to WinRT compatibility, BinaryFormatter is not available. I am left with the XmlSerializer, which does not seem to be able to deserialize without knowing the source type. Do you have any hints for my

object Deserialize(byte[] objectData)
byte[] Serialize(object obj)

Approach? How do you send objects across Systems?

Regards

The most widely used is JSON:

I am left with the XmlSerializer, which does not seem to be able to deserialize without knowing the source type

It’s best to provide what type you want to deserialize into. If you need dynamic behavior you can send classes with a wrapper that helps you decide.

1 Like

Thank you for that hint!

I crafted this, to use for anyone to come and ask the same question:

 public class ObjectHolder
    {
        public string SerializedObject { get; set; }
        public string SerializeHints { get; set; }
    }

    public static class Serializer
    {
        public static object DeserializeFromString(string objectData)
        {
            ObjectHolder holder = JsonConvert.DeserializeObject<ObjectHolder>(objectData);
            Type objType = Type.GetType(holder.SerializeHints);
            var obj = JsonConvert.DeserializeObject(holder.SerializedObject, objType);
            return obj;
        }

        public static string SerializeToString(object obj)
        {
            string sobj = JsonConvert.SerializeObject(obj);

            string holder = JsonConvert.SerializeObject(new ObjectHolder
            {
                SerializedObject = sobj,
                SerializeHints = obj.GetType().ToString()
            });

            return holder;
        }

}

Add newtonsoft JSON to make it work.