question

dominikpielak avatar image
dominikpielak asked

Problems with deserialization of classes implementing IDictionary

PlayFabSimpleJson can't correctly deserialize custom classes which implement IDictionary / IDictionary interfaces.

For example:

 public class SerializedDictionary< TKey, TValue > : IDictionary< TKey, TValue >, IDictionary

Serialization is ok, it gives the same results as Dictionary, however problem is with deserialization. PlayFabSimpleJson.DeserializeObject returns empty collection.

I have digged a bit into the problem and in my opinion ReflectionUtils.IsTypeDictionary is the root of the problem. It can't correctly recognize that class is implementing IDictionary< TKey, TValue >.

https://github.com/PlayFab/PlayFab-Samples/blob/master/Recipes/PrizeWheel/Example-Unity3d/PrizeWheel/Assets/PlayFabSdk/Shared/Internal/SimpleJson.cs

Not sure why IsAssignableFrom doesn't work. It also returns false for standard Dictionary, so maybe it's because of generics. Standard Dictionary it's detected by direct checking of type:

 return genericDefinition == typeof(IDictionary<,>) || genericDefinition == typeof(Dictionary<,>);

For sure there is some solution to handle also custom classes by reflection

sdks
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

·
Xiao Zha avatar image
Xiao Zha answered

Could you please share the custom classes with us so we can reproduce the issue? The SimpleJson.cs in PlayFab SDK is used to handle data from the PlayFab APIs, and it may not be very suitable for you to process custom complex classes. As a workaround, you could use the Newtonsoft.Json library to handle custom complex JSON serialization and deserialization needs.

10 |1200

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

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.