question

Petter Vernersson avatar image
Petter Vernersson asked

What is the intended way to deserialize custom data from an api call.,Intended way to deserialize custom data from api call.

I am using the Unity SDK and have made an call to the api function "PlayFabMultiplayerAPI.CreateMatchmakingTicket"

The request accept custom data through "MatchmakingPlayerAttributes"

in this tutorial https://api.playfab.com/docs/tutorials/landing-tournaments/matchmaking-unity-quickstart

they have just entered the data like this.

...
		// Here we specify the creator's attributes.
		Attributes = new MatchmakingPlayerAttributes
		{
			DataObject = new
        		{
        			Skill = 24.4
        		},
		},
...

I have however made a class for my player attributes and entered the data like this.

PlayerAttributes myPlayerAttributes = new PlayerAttributes
{
	HostAdress = GetMyHostAdress()
};

...

                Attributes = new MatchmakingPlayerAttributes {
                    DataObject = myPlayerAttributes
                }
      
...

This is all fine from what I can see. But what is the intended way to get the data back again when retrieving it from another api call.

For example when using this api call.

PlayFabMultiplayerAPI.GetMatch(
	new GetMatchRequest 
	{
            MatchId = matchId,
            QueueName = "queuetest",
            ReturnMemberAttributes = true
        },

The tutorial never shows this.

I can get the DataObject and convert it back to my PlayerAttribute class structure by doing ToString on the DataObject and deserelize it from json to my PlayerAttribute class, but it feels like that isn't the intended way since the DataObject already is a PlayFab.Json.JsonObject.

How is it intended to convert "PlayFab.Json.JsonObject" to your own types?

,

I am using the Unity SDK and have made an call to the api function "PlayFabMultiplayerAPI.CreateMatchmakingTicket"
The request accept custom data through "MatchmakingPlayerAttributes"

in this tutorial https://api.playfab.com/docs/tutorials/landing-tournaments/matchmaking-unity-quickstart

they have just entered the data like this.

...
		// Here we specify the creator's attributes.
		Attributes = new MatchmakingPlayerAttributes
		{
			DataObject = new
        		{
        			Skill = 24.4
        		},
		},
...

I have however made a class for my player attributes and entered the data like this.

PlayerAttributes myPlayerAttributes = new PlayerAttributes
{
	HostAdress = GetMyHostAdress()
};

...

                Attributes = new MatchmakingPlayerAttributes {
                    DataObject = myPlayerAttributes
                }
      
...

This is all fine from what I can see. But what is the intended way to get the data back again when retrieving it from another api call.

For example when using this api call.

PlayFabMultiplayerAPI.GetMatch(
	new GetMatchRequest 
	{
            MatchId = matchId,
            QueueName = "queuetest",
            ReturnMemberAttributes = true
        },

The tutorial never shows this.

I can get the DataObject and convert it back to my PlayerAttribute class structure by doing ToString on the DataObject and deserelize it from json to my PlayerAttribute class, but it feels like that isn't the intended way since the DataObject already is a PlayFab.Json.JsonObject.

How is it intended to convert "PlayFab.Json.JsonObject" to your own types?

apisunity3dsdks
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.

Petter Vernersson avatar image Petter Vernersson commented ·

For some reason the post content and title got duplicated. And I don't seem to be able to edit it.

0 Likes 0 ·

1 Answer

·
Sarah Zhang avatar image
Sarah Zhang answered

There is no built-in SDK to convert this DataObject to the custom type object. We should deserialize it to JSON string then convert it to the custom type object. A possible workaround is to use Newtonsoft.Json to convert it. You can refer to the following code. You can download the unity package on the Github page. It may be similar to your original way. In a word, converting DataObject to JSONString is needed.

//AttributesDataModel.cs
public class AttributesDataModel
{
//should be correspond to your JSON path, in your code, your path is HostAdress, it should be HostAdress too
public string HostAddress { get; set; }
}
//use this function to convert Json to custom type
using Newtonsoft.Json;
…
var itemAttribute = JsonConvert.DeserializeObject<AttributesDataModel>(member.Attributes.DataObject.ToString());
Debug.Log("itemAttribute.HostAddress----->" + itemAttribute.HostAddress);
…
2 comments
10 |1200

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

Petter Vernersson avatar image Petter Vernersson commented ·

Ye, your suggestion is how I currently do it except I use the Json converter available in the playfab unity sdk.

And yes DataObject is by its definition of type object. But that is not was it is constructed as. If you use GetType on DataObject when retrieved from the api call you see that it is a PlayFab.Json.JsonObject.

I just thought it was weird that there had been an effort to construct a PlayFab.Json.JsonObject when I can't use that object to serialize it to my own type and instead has to do a third conversion to string first.

I did however found this forum post now.

https://community.playfab.com/questions/4521/executecloudscript-functionresult-frustration.html

Which explains the concerns I had.

1 Like 1 ·
Sarah Zhang avatar image Sarah Zhang Petter Vernersson commented ·

Yeah, it looks like I missed something thank you for sharing it.

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.