Search Unity

Bug JsonUtility.FromJson freezes app when updating to Unity 2019.4

Discussion in 'Scripting' started by MartinHerbethSpectralTMS, Aug 11, 2021.

  1. MartinHerbethSpectralTMS

    MartinHerbethSpectralTMS

    Joined:
    Jan 17, 2019
    Posts:
    7
    Hello everyone and thank you in advance for your help !

    We recently had to update from Unity 2019.3 (2019.3.9f1) to Unity 2019.4 (2019.4.24f1). After the update we noticed that the deserialization of a json into a certain class takes way more time (from about 0.1 seconds to about 3 seconds).
    Here is the json we deserialize:
    Code (CSharp):
    1. {"zones":[{"id":1,"label":"Site XXX","parentId":null,"zoneLevel":{"id":2,"label":"Site","rank":1},"useQRCode":false},{"id":2,"label":"Site YYY","parentId":null,"zoneLevel":{"id":2,"label":"Site","rank":1},"useQRCode":false},{"id":3,"label":"Site ZZZ","parentId":null,"zoneLevel":{"id":2,"label":"Site","rank":1},"useQRCode":false},{"id":51,"label":"Site AAA","parentId":null,"zoneLevel":{"id":2,"label":"Site","rank":1},"useQRCode":false}]}
    So it's probably not a json size issue.

    We want to deserialize this json into a ShortZoneCollection, which contains a List of ShortZone as presented here:
    Code (CSharp):
    1. [Serializable]
    2. public class ShortZoneCollection : Data<ShortZoneCollection>
    3. {
    4.     public List<ShortZone> zones = new List<ShortZone>();
    5. }
    6.  
    7. [Serializable]
    8. public class ShortZone : Data<ShortZone>
    9. {
    10.     public string label;
    11.     public ZoneLevel zoneLevel;
    12.     public bool useQRCode;
    13.     public List<Zone> childsZone = new List<Zone>();
    14. }
    However, if the childsZone field is commented, the deserialization takes the normal amount of time.

    To clarify: Zone is a class which contains recursive serialization. I know ot's not really supported, however, the jsons we receive from our API never contain any recursive serialization, the fields in our Zone class that are recursive are only there for ease of access, but they are set later, not thanks to the jsons we receive. The Zone class contains a dozen fields and several methods, but nothing too fancy.

    Is it normal that deserialization with JsonUtiliy.FromJson takes (a long) time for fields that are not even present in the json ?
    Was there some kind of change I'm not aware of in JsonUtility.FromJson from 2019.3 to 2019.4 ?

    Thanks in advance for any help provided !
     
  2. kaarloew

    kaarloew

    Joined:
    Nov 1, 2018
    Posts:
    360