question

shreyasram avatar image
shreyasram asked

SerializationException: Invalid JSON string

Stack Trace: SerializationException: Invalid JSON string PlayFab.Json.PlayFabSimpleJson.DeserializeObject (System.String json) (at <00000000000000000000000000000000>:0) PlayFab.Json.PlayFabSimpleJson.DeserializeObject (System.String json, System.Type type, PlayFab.Json.IJsonSerializerStrategy jsonSerializerStrategy) (at <00000000000000000000000000000000>:0) PlayFab.Json.PlayFabSimpleJson.DeserializeObject[T] (System.String json, PlayFab.Json.IJsonSerializerStrategy jsonSerializerStrategy) (at <00000000000000000000000000000000>:0) PlayFab.Internal.PlayFabUnityHttp.OnResponse (System.String response, PlayFab.Internal.CallRequestContainer reqContainer) (at <00000000000000000000000000000000>:0) PlayFab.Internal.PlayFabUnityHttp+<Post>d_20.MoveNext () (at <00000000000000000000000000000000>:0) UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <00000000000000000000000000000000>:0) PlayFab.Internal.PlayFabUnityHttp:OnResponse(String, CallRequestContainer) PlayFab.Internal.<Post>d_20:MoveNext() UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)

Code:

     public void OnResponse(string response, CallRequestContainer reqContainer)
     {
             try
             {
 #if PLAYFAB_REQUEST_TIMING
                 var startTime = DateTime.UtcNow;
 #endif
                    
                 /////////////////////////////////////
                 // Sometimes playfab sends extra bytes at the start of the response causing 
                 // 'System.Runtime.Serialization.SerializationException: Invalid JSON string' on ios
                 // This is a quick hack to fix that exception
                 /////////////////////////////////////
                 var cleanRseponse = "";
                 for (var i = 0; i &lt; response.Length; i++)
                     if (response[i] == '{')
                     {
                         cleanRseponse = response.Substring(i);
                         break;
                     }
                 response = cleanRseponse;
                 /////////////////////////////////////
                    
                 var serializer = PluginManager.GetPlugin&lt;ISerializerPlugin&gt;(PluginContract.PlayFab_Serializer);
                 var httpResult = serializer.DeserializeObject&lt;HttpResponseObject&gt;(response);
    
                 if (httpResult.code == 200)
                 {
                     // We have a good response from the server
                     reqContainer.JsonResponse = serializer.SerializeObject(httpResult.data);
                     reqContainer.DeserializeResultJson();
                     reqContainer.ApiResult.Request = reqContainer.ApiRequest;
                     reqContainer.ApiResult.CustomData = reqContainer.CustomData;
    
                     PlayFabHttp.instance.OnPlayFabApiResult(reqContainer.ApiResult);
 #if !DISABLE_PLAYFABCLIENT_API
                     PlayFabDeviceUtil.OnPlayFabLogin(reqContainer.ApiResult);
 #endif
                     try
                     {
                         PlayFabHttp.SendEvent(reqContainer.ApiEndpoint, reqContainer.ApiRequest, reqContainer.ApiResult,
                             ApiProcessingEventType.Post);
                     }
                     catch (Exception e)
                     {
                         Debug.LogException(e);
                     }
    
                     try
                     {
                         reqContainer.InvokeSuccessCallback();
                     }
                     catch (Exception e)
                     {
                         Debug.LogException(e);
                     }
                 }
                 else
                 {
                     if (reqContainer.ErrorCallback != null)
                     {
                         reqContainer.Error = PlayFabHttp.GeneratePlayFabError(reqContainer.ApiEndpoint, response, reqContainer.CustomData);
                         PlayFabHttp.SendErrorEvent(reqContainer.ApiRequest, reqContainer.Error);
                         reqContainer.ErrorCallback(reqContainer.Error);
                     }
                 }
             }
             catch (Exception e)
             {
                 Debug.LogException(e);
             }
         }
apisPlayer Data
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.

juergenoswald avatar image juergenoswald commented ·

This forum is still buggy so I need to comment here.

If I'm not mistaken, you mean that sometimes on iOS devices, the response string looks like "{ {JSONDATA}" with an extra "{" at the beginning.

The way I understand the code pasted above is that it's not an extra { at the start, but instead any other random characters except a {. The above, which the user mentions fixes the issue would clean the following for example: "random123{JSONDATA}" by cutting away the "random123" part.

0 Likes 0 ·
juergenoswald avatar image juergenoswald juergenoswald commented ·

NOTE: This comment was supposed to be an answer to "Gosen Gao"

0 Likes 0 ·

1 Answer

·
Gosen Gao avatar image
Gosen Gao answered

Thanks for the information you shared.

If I'm not mistaken, you mean that sometimes on iOS devices, the response string looks like "{ {JSONDATA}" with an extra "{" at the beginning. Can you share more information about this issue? like how to reproduce it?

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

shreyasram avatar image shreyasram commented ·

I am unable to reproduce this issue. The fix was made for an older version but since last week we have been getting lot of crashes. Can updating the sdk fix this? current version is v2.58.181218

0 Likes 0 ·
Gosen Gao avatar image Gosen Gao shreyasram commented ·

We need more info about this issue to find out the cause. Can you add some debug code to collect the origin response string when this exception occurs and share it to us? Also, may I know your title Id?

0 Likes 0 ·
juergenoswald avatar image juergenoswald Gosen Gao commented ·

A few people have mentioned (including me) that this got introduced around June 6th to June 8th. Could that help to narrow down enough the search on your end? Like, what changes got deployed to live around that time that might cause the client-side json deserializer to fail?

1 Like 1 ·
Show more comments

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.