Search Unity

Unity does not support SerializeReference in JsonUtility

Discussion in 'Editor & General Support' started by jerome-lacoste, Sep 23, 2020.

  1. jerome-lacoste

    jerome-lacoste

    Joined:
    Jan 7, 2012
    Posts:
    206
    Hello.

    We have been in contact with support since April with issues around SerializeReference (1240887, 1245749, 1275317, 1277193).

    Then we got the following message yesterday as part of case "1275317" (originally reported here).

    "I just got a reply from the developer. IT turns out that this behavior is actually by design. We do not support SerializeReference in JsonUtility."

    We have invested on this setup and it works 99.999% of the time. Until now it looked more like a bug than a lack of support.

    So we would appreciate some further explanation before we rip it out.

    1. In the documentation I didn't find a thing explicitly says that SerializeReference is not supported by JsonUtility:
    2. In the 4 years or more that JsonUtility has existed, there has to my knowledge not been a difference in support between what is supported by the unity serializer and JsonUtility itself. So it's a bit weird to add this now.

    3. in all our conversations with support or on the forum in the 4-5 months we have been reporting issues, no-one has ever raised an issue with the potential incompatibility.

    All in all this seems to come against the Principle of Least Astonishment and also a bit late...

    If SerializeReference is indeed not supported in JsonUtiity, the consequences are serious for us. We have to change our serialization layer to something more stable and force an update to 8 products on 5 platforms, affecting tens of thousand of users. This is not a small task and we are not taking it lightly.

    Can someone with knowledge of the internals
    • confirm that JsonUtility doesn't support SerializeReference
    • let us know why
    • let us know if there are ways to work-around this issue (given that it works most of the time)
    • bonus point: let us know if it will be supported one day
    Thanks,

    Note: we have also asked for paid support several weeks ago and we are still waiting for someone to take contact...
     
  2. DiegoDePalacio

    DiegoDePalacio

    Unity Technologies

    Joined:
    Oct 28, 2009
    Posts:
    507
    Hello Jerome,

    I'm sorry for the inconvenience.

    I contacted the developer in charge of solving your ticket and this is what he expressed about the current status of your issue:

    "With regards to JsonUtility support. We expect that serialization should not be a concern, there are edge cases for deserialization which could be problematic as you have reported before. This code path has not been as heavily tested, we are looking at improving the coverage there.

    To answer you on whether or not 1245749 is related, I suspect it is not as the other case was related to a Serializable type containing a SerializeReference with an instance itself containing a SerializeReference. In this case, I have inspected all types which are serialized and they do not contain themselves SerializeReference.

    I have tried recreating a unit test to validate your use case using the same types, ca, without success.
    You are witnessing this solely in the player or are you able to reproduce this in the editor as well? Did you try to change the order/location where DataPersistenceObjectModel is accessed/kept at runtime to see if it impacted the _data values?"


    This same information was shared for you on the ticket 1275317, but I'm adding it here for your convenience.

    The mentioned developer is available for a call with you if you want. In such a case, please send me a PM to arrange such call.

    Thank you for your comprehension and sorry again for the bad experience that you're having with this specific functionality.


    Diego.
     
  3. kufra1

    kufra1

    Joined:
    Oct 6, 2020
    Posts:
    1
    Hi,

    Is there any news on those topics?
    Tried looking into the issues posted above:
    (1240887, 1245749, 1275317, 1277193). but only 1245749 seem to exist (or is public?).

    Using JSONUtility to create savefiles based on serialized SO containing my savedata. Some of the more complex SO contains ScriptableReferences. Having some issues with loading the save file after reload of the editor. It actually works to save/load in the same session of unity.
     
  4. bilalakil

    bilalakil

    Joined:
    Jan 28, 2018
    Posts:
    77
    Hello from the future!

    Just chiming in to say this now appears to work Yay!!

    Input:

    Code (CSharp):
    1.     public interface Y { }
    2.  
    3.     [Serializable]
    4.     public class X
    5.     {
    6.         [SerializeReference] public List<Y> Ys;
    7.     }
    8.     [Serializable]
    9.     public class Z : Y { }
    10.     [Serializable]
    11.     public class W : Y { }
    12.  
    13.     ...
    14.  
    15.         var x = new X
    16.         {
    17.             Ys = new List<Y>
    18.             {
    19.                 new Z(),
    20.                 new W(),
    21.             },
    22.         };
    23.         var json = JsonUtility.ToJson(x);
    24.         UnityEngine.Debug.Log(json);
    25.  
    26.         var xFromJson = JsonUtility.FromJson<X>(json);
    27.         foreach (var y in xFromJson.Ys)
    28.             UnityEngine.Debug.Log(y);
    Output logs:

    Code (CSharp):
    1. {"Ys":[{"rid":1000},{"rid":1001}],"references":{"version":2,"RefIds":[{"rid":1000,"type":{"class":"Test/Z","ns":"","asm":"Assembly-CSharp"},"data":{}},{"rid":1001,"type":{"class":"Test/W","ns":"","asm":"Assembly-CSharp"},"data":{}}]}}
    2.  
    3. Test+Z
    4.  
    5. Test+W