Deserialize Custom Class

Hello
I have added a Serialize and Deserialize method for a customType and Register them.

Serialize is well called but not Deserialize.

Then when I get message array I have an objet with Code and Data parameters. Data contains my customType as byte[]. But because I am developping all messages are note as there. Previous one was only string. Maybe it is why Deserialize is not called?

Comments

  • I have reset my history on my webhooks and move Deserialize function into my custom class (because parameter of RegisterType say constructor).
    And it is working now.

    Now I must managed to convert my custom Class that contains string whose length is not know.

    [Serializable]
    public class sTchatDatas
    {
    public bool unreadMessage = false;
    public long timestamp;
    public int level;
    public int characterId;
    public string message;
    public string gamerId;
    public string nickname;
    }

    Maybe I must add public int messageLength ?

    or use char[] ou byte[] instead of string message ?
  • I finally found how to do.

    [Serializable]
    public class sTchatDatas
    {
    public bool unreadMessage = false;
    public long timestamp;
    public int level;
    public int characterId;
    public string message;
    public string gamerId;
    public string nickname;


    public static object DeserializeTchatDatas(byte[] data)
    {
    return ByteArrayToObject(data);
    }

    //public static readonly byte[] memTchatDatas = new byte[2 * 4];
    public static byte[] SerializeTchatDatas(object customType)
    {
    return ObjectToByteArray(customType);
    }
    }




    // Convert an object to a byte array
    public static byte[] ObjectToByteArray(object obj)
    {
    BinaryFormatter bf = new BinaryFormatter();
    using (var ms = new MemoryStream())
    {
    bf.Serialize(ms, obj);
    return ms.ToArray();
    }
    }

    // Convert a byte array to an Object
    public static object ByteArrayToObject(byte[] arrBytes)
    {
    using (var memStream = new MemoryStream())
    {
    var binForm = new BinaryFormatter();
    memStream.Write(arrBytes, 0, arrBytes.Length);
    memStream.Seek(0, SeekOrigin.Begin);
    var obj = binForm.Deserialize(memStream);
    return obj;
    }
    }
  • And last how to deserialize in a javascript WebHooks.
    My game is made with C# Unity. All is ok but I need to deserialize in my js webhooks to read message.

    It seems to be a base64 encode string. right?
  • JohnTube
    JohnTube ✭✭✭✭✭
    Hi @TobyKaos,

    Thank you for choosing Photon!

    I assume you mean Chat WebHooks and you want to deserialize messages, but for what purpose?