Search Unity

Question com.unity.nuget.newtonsoft-json. deserialize complex object stuck webgl app

Discussion in 'Scripting' started by Sarono, Mar 16, 2023.

  1. Sarono

    Sarono

    Joined:
    Apr 12, 2018
    Posts:
    8
    com.unity.nuget.newtonsoft-json 3.1.0
    unity 2021.3.19f1
    webgl

    In Unity WebGL platform, under certain circumstances, deserializing some complex objects may cause the program to freeze, and there is no error message (refer to the code and Effect.json.text below). After deleting "var configs = JsonHelper.FromJson(json.text);", the program can run normally.

    After testing, it runs correctly on Android and PC with il2cpp build.

    The above situation seems to be caused by the program getting stuck in a while(true) loop.

    Does anyone know what the reason might be?


    Code (CSharp):
    1.  
    2.     public class TestLoadEffect : MonoBehaviour
    3.     {
    4.         public TextAsset json;
    5.  
    6.         [ContextMenu("Load")]
    7.         private void Start()
    8.         {
    9.             Log.ERROR("fromjson start");
    10.  
    11.             //var configs = JsonHelper.FromJson<List<SpecialTable>>(json.text); // this is ok
    12.             var configs = JsonHelper.FromJson<List<Effect>>(json.text); // webgl stuck here!
    13.  
    14.             Log.ERROR($"fromjson end. {(configs != null ? configs.Count.ToString() : "null")} \n{(configs != null ? string.Join("\n", configs.Select(c => c.id)) : "null")}");
    15.         }
    16.     }
    17.  
    18.     public class JsonHelper
    19.     {
    20.         static JsonSerializerSettings s_DefaultSettings = new()
    21.         {
    22.             TypeNameHandling = TypeNameHandling.Auto,
    23.             ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
    24.             NullValueHandling = NullValueHandling.Ignore,
    25.             DefaultValueHandling = DefaultValueHandling.Ignore,
    26.             MissingMemberHandling = MissingMemberHandling.Ignore,
    27.             Converters = IAutoJsonConverter.GetJsonConverters(),
    28.         };
    29.        
    30.         public static T FromJson<T>(string json, JsonSerializerSettings settings = null)
    31.         {
    32.             return JsonConvert.DeserializeObject<T>(json, settings ?? s_DefaultSettings);
    33.         }
    34.     }
    35.  
     

    Attached Files: