-1

In Unity project I have some class WsMessage for WebSocket interaction. This class located in my own library WebSocketModels.

namespace WebSocketModels
{
    [Serializable]
    public enum WsMessageType 
    { 
        System, Player, Challenge, DeclineChallenge, RemoveChallenge, Game, Move, Moves, Chat,
        Players, Challenges, Games, Clock                        
    }    
    
    [Serializable]
    public class WsMessage
    {
        public WsMessageType type { get; set; }
        public string data { get; set; }

        public WsMessage() { }

        public WsMessage(WsMessageType type, string data)
        {
            this.type = type;
            this.data = data;
        }
    }
}

By some reason it cannot be deserialized. I didn't see any errors. If i move this class from library directly to Unity project object of WsMessage creating normally. I use this simple command for get an object of WsMessage:

WsMessage message = JsonConvert.DeserializeObject<WsMessage>(inputWsMessage);

I've met this problem after change my Unity player Scripting Backend to IL2CPP. On Mono everything was OK.

Example of JSON content

{"type":10,"data":"[{\"id\":\"0d8648e4-ce15-4084-87f9-f3de2b5a9b32\",\"fromPlayer\":{\"id\":\"af76e7c3-27b2-4d05-bcd3-f4b41c3bb7ba\",\"name\":\"Aydar\",\"rating\":1600.0,\"isOnline\":false},\"color\":0,\"timeControl\":{\"time_range\":10,\"time_increment\":5,\"control_type\":0},\"toPlayer\":null}]"}
  • 2
    You should show the json content, maybe it does not match the WsMessage – Everts Jul 14 '20 at 19:20
  • If `public WsMessageType type` is represented by a string in the JSON you need to use [`StringEnumConverter`](https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Converters_StringEnumConverter.htm), see [How to tell Json.Net globally to apply the StringEnumConverter to all enums](https://stackoverflow.com/q/7427909/3744182) and [parsing an enumeration in JSON.net](https://stackoverflow.com/q/7799769/3744182). Beyond that we need a [mcve] to help you, specifically the JSON. – dbc Jul 14 '20 at 19:36
  • Added example of JSON. But it is not problem by serialization by itself, in Mono scripting everything was OK. Also if my class located directly in Unity project it is also can be deserialized normally. Problem happening when i try deserialize object of class from imported library WebSocketModels. – Айдар Ахметшин Jul 15 '20 at 07:47

1 Answers1

0

So, seems like the problem is here;

public WsMessageType type { get; set; }
public string data { get; set; }

Why? because { get; set; } is syntactic sugar for getter and setter methods.

So, in other words, your code above is 'equivalent' to;

public void WsMessageType_SetValue(WsMessageType value)
{
    WsMessageType = value;    
}

public WsMessageType WsMessageType_GetValue()
{
    return WsMessageType;
}

And the same for 'data'.

The problem arises when you try to serialize some data into some function, it doesn't make it sense, and the { get; set; } shortcut makes it harder to see.

If you use variables instead of getter/setter it should work!

ie;

public WsMessageType type;
public string data;
mayo
  • 3,845
  • 1
  • 32
  • 42