question

Kaiwen Yu avatar image
Kaiwen Yu asked

PlayFab unity SDK json convert dictionary to list

PlayFab.SimpleJson seems to convert dictionary to a list of key value pairs, which causes deserialization error in AzureFunctions. I was always using Newtonsoft.Json.JsonConvert on my end and it worked fine.

Class definition:

 public class Equipment
 {
     public EquipmentRarity rarity { get; set; }
     public Dictionary<int, int> randomAffixes { get; set; } <- this field is incorrectly converted into a list
     public Tuple<int, int> influenceAffix { get; }
     public int equipmentId { get; }
     public string itemInstanceId { get; set; }
     public int equipmentLevel { get; }
     public int dropGameNumber { get; }
 }

Using JsonConvert (In Unity):

 {"rarity":2,"randomAffixes":{"1":20,"8":20,"11":20},"equipmentId":1,"equipmentLevel":3,"equippedHeroId":0,"influenceAffix":null,"dropGameNumber":30,"itemInstanceId":"FakeInstanceIdPlaceholder","itemId":"Equipment","itemClass":0}.

Using SimpleJson (In Unity):

 {"rarity":2,"randomAffixes":[{"Key":1,"Value":20},{"Key":8,"Value":20},{"Key":11,"Value":20}],"equipmentId":1,"equipmentLevel":3,"equippedHeroId":0,"influenceAffix":null,"dropGameNumber":30,"itemInstanceId":"FakeInstanceIdPlaceholder","itemId":"Equipment","itemClass":0}.

I have also tried changed my deserializer in Azure Function from (I changed < to - to avoid web formatting issue):

         var context =
             JsonConvert.DeserializeObject-FunctionExecutionContext-GrantUserCheatEquipmentRequest>>(
                 await req.ReadAsStringAsync());

To:

         var context =
             PlayFabSimpleJson.DeserializeObject-FunctionExecutionContext-GrantUserCheatEquipmentRequest>>(
                 await req.ReadAsStringAsync());

and lead to a weird error in PlayFabSimpleJson

 [2023-01-11T06:12:37.783Z] Unexpected error calling GrantUserCheatEquipment. [Object reference not set to an instance of an object.]
    at PlayFab.Json.PocoJsonSerializerStrategy.DeserializeObject(Object value, Type type)
 [2023-01-11T06:12:37.784Z]    at PlayFab.Json.PocoJsonSerializerStrategy.DeserializeObject(Object value, Type type)
 [2023-01-11T06:12:37.785Z]    at PlayFab.Json.PlayFabSimpleJson.DeserializeObject(String json, Type type, IJsonSerializerStrategy jsonSerializerStrategy)
 [2023-01-11T06:12:37.787Z]    at PlayFab.Json.PlayFabSimpleJson.DeserializeObject[T](String json)
 [2023-01-11T06:12:37.789Z]    at Kykystudio.FunctionApp.Functions.GrantUserCheatEquipment.Run(HttpRequest req, ILogger log) in H:\workspace\Unity\AutoCard\AutoCardAzure\Kykystudio.FunctionApp\Functions\GrantUserCheatEquipment.cs:line 30

Could you please help me out on this problem? I don't think Dictionary need to be converted into List, but if that is not changable, please let me know why I cannot use PlayFabSimpleJson in Azure Funtions.

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

Rick Chen avatar image Rick Chen ♦ commented ·

I will do some test about that and let you know the update. Thank you for your patience.

1 Like 1 ·

1 Answer

·
Rick Chen avatar image
Rick Chen answered

Currently there is some issue in PlayFabSimpleJson when deserializing a key as an int. You may use more professional Json converter instead.

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

Kaiwen Yu avatar image Kaiwen Yu commented ·

I didn't manually deserialize the json myself. I am using the ExecuteFunctionRequest from PlayFabSDK. Is there a way to change the default json serializer when using PlayFabCloudScriptAPI.ExecuteFunction()?

ExecuteFunctionRequest class: https://pastebin.com/TN9pJ9c0

0 Likes 0 ·
Kaiwen Yu avatar image Kaiwen Yu commented ·

Hi Rick, do you mind check me message above and help me on it? Thanks!

0 Likes 0 ·
Kaiwen Yu avatar image Kaiwen Yu Kaiwen Yu commented ·

Hi Rick, any update on this?

0 Likes 0 ·
Rick Chen avatar image Rick Chen ♦ Kaiwen Yu commented ·

Sorry for the late reply.

Currently, the way that PlayFabSimpleJson serialize object is by design. To work around that, you may either change the type Dictionary to Dictionary or convert the result on your own.

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.