question

Kaiwen Yu avatar image
Kaiwen Yu asked

PlayFab ExecuteFunction Json Serilization issue. ,PlayFab CloudScript with Azure Functions deserialization issue

Hi all,

I am recently working on a new PlayFabCloudScriptFunction with Azure Functions. I have encountered an issue with Json Deserialization of my request. On the Unity client side, I have some thing like this:

PlayFabCloudScriptAPI.ExecuteFunction(
    GetRequest(CloudScriptConstants.CompleteSingleplayerGameFunctionName,
               new CompleteSingleplayerGameRequest(gameState)),
    GetCallback(resultCallback, errorCallback),
    error => errorCallback.Invoke());

where gameState is like this:

public class GameState
{
    public Dictionary<int, int> playerRelics { get; private set; }

// ... Other fields are removed and they are probably not an issue. 
}

On Azure Functions side, I did this (this pattern worked fine for all other types including nested object and lists):

var context =
    JsonConvert.DeserializeObject<FunctionExecutionContext<CompleteSingleplayerGameRequest>>(
        await req.ReadAsStringAsync());

and it fails because the playerRelics is like this in json:

"playerRelics": [
  {
    "Key": 29,
    "Value": 1
  },
  {
    "Key": 9,
    "Value": 1
  }
],

I think JsonConvert is looking for this:

"playerRelics": {
    "29: 1,

    "9":1
},

The issue might caused by the dictionary key is an int but not a string. But I doubt changing it to string will change the serialization format. The Key, Value seems to be a certain serialization format that is coming with PlayFab in Unity.

BTW I have Odin Inspecter & Serializer installed. Will this be the cause?

I tried PlayFabSimpleJson on Azure side but it will throw this:

Object reference not set to an instance of an object.


Thanks!

Kevin

,

Hi all,

Recently I have created a PlayFab Execute Function with request containing objects like this:

public class GameState
{
    public Dictionary<int, int> playerRelics { get; private set; }

//... Removed other fields
}

Usually what I do it at Unity client side, I call

PlayFabCloudScriptAPI.ExecuteFunction(
    GetRequest(CloudScriptConstants.CompleteSingleplayerGameFunctionName,
               new CompleteSingleplayerGameRequest(gameState)),
    GetCallback(resultCallback, errorCallback),
    error => errorCallback.Invoke());

Then on the Azure Function side I do:

var context = JsonConvert.DeserializeObject<FunctionExecutionContext<CompleteSingleplayerGameRequest>>(await req.ReadAsStringAsync());

This pattern worked fine until I have put that GameState object into the request. And the issue is with the dictionary. Somehow, unity client side serialization converted it into this:

"playerRelics": [
  {
    "Key": 29,
    "Value": 1
  },
  {
    "Key": 9,
    "Value": 1
  }
],

Which is rather questionable as I thought Dictionary should be converted into this:

"playerRelics": {
"29":1,
"9":1
},

I am not sure if it is caused by the dictionary key is an int instead of a string so it cannot automatically serialize it as I thought it would.

Anyway, I tried to use PlayFabSimpleJson.DeserializeObject<xxx> but it will get this: Object reference not set to an instance of an object.

Please help me take a look. Thanks!

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Hernando avatar image
Hernando answered

I try to test your gameState class model with Json.NET, but I get an error when reproducing your issue. According to the error message, your 'playerRelics' type must implement IDictionary<string, JToken>.', therefore we should modify the GameState class to be like to the following:

class GameState  {  
	[JsonProperty]  
	public IDictionary<string, JToken> playerRelics { get; set; }  

	[JsonProperty]  
	public string playerName;  
}

For PlayFabSimpleJson issue, maybe you had set "Managed Stripping Level" to High in "Player Settings...". It only works if this value is set to Low or Disabled. this thread is worth reading through https://community.playfab.com/questions/28390/nullreferenceexception-object-reference-not-set-to.html

1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kaiwen Yu avatar image Kaiwen Yu commented ·

Thanks for taking a look. I am not sure if you are receiving the error at unity client side or CloudScript Azure Functions side.

For now I did a change to make this Dictionary to a list of new class I created for relics and it worked just fine. I don't really need the Dictionary functionality here as I am just using this class to hold some data.

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.