Hi all,
I am sharing data between a python server and a unity C# client over a ZeroMQ socket. The idea is to send python objects like dictionary, list, tuple, etc. by first serializing them into a JSON string on python and then deserializing the JSON string into a serializable class on unity C# side. The communication works fine and I can verify the data sharing over the debug logs on both sides.
The problem is that I would like to deserialize the JSON string directly into a class and I am struggling with this part. For now, the JSON string I am receiving on unity C# is a simple python dict<string, string> i.e.
{"p1": "player1", "p2": "player2"}
Below is the class I use for storing the deserialized data in C#.
public class ParseReceivedData
{
public Dictionary<string, string> player_dict;
//public List<string> p1_list;
//public Tuple<string> p2_tuple;
//public string actions_str;
//public int states_num;
public static ParseReceivedData CreateFromJSON(string jsonString)
{
return JsonUtility.FromJson<ParseReceivedData>(jsonString);
}
public void PrintOutput()
{
foreach (KeyValuePair<String, String> kvp in this.player_dict)
{
Debug.Log("Dict = {0} : {1}" + kvp.Key.ToString() + kvp.Value.ToString());
}
}
}
Below is the part of the code where I have the deserialization.
string message = null;
bool gotMessage = false;
while (Running)
{
gotMessage = client.TryReceiveFrameString(out message); // this returns true if it's successful
if (gotMessage) break;
}
if (gotMessage)
{
Debug.Log("Received " + message);
ParseReceivedData myParser = ParseReceivedData.CreateFromJSON(message);
myParser.PrintOutput();
}
I attached the entire code file.
The error message is:
NullReferenceException: Object reference not set to an instance of an object
The error points to the PrintOutput() function call. Could you kindly help me understand what it is that I am doing wrong and how do I correct this?
Thank you!
6095013–662412–HelloRequester.cs (3.09 KB)