Search Unity

JsonUtility Issue in WebGL Release

Discussion in 'Web' started by threedinteractive, Feb 20, 2019.

  1. threedinteractive

    threedinteractive

    Joined:
    Jul 18, 2018
    Posts:
    8
    Hi,
    Most recently (maybe since Unity version 2018.3.x, i didn't notice this issue immediately), the JsonUtility serializer no longer works in the WebGL release build.

    While the serialized and deserialized data are identical in the editor and the Windows standalone build, an empty JSON file is created under WebGL.

    Code (CSharp):
    1. //Classes
    2. [Serializable]
    3. public class objectList
    4. {
    5.     public long pID;
    6.     public List<listObject> children = new List<listObject>();
    7. }
    8.  
    9. [Serializable]
    10. public class listObject
    11. {
    12.     public string name;
    13.     public long oID;
    14.     public uint childCount;
    15.     public bool first;
    16.     public bool last;
    17. }
    18.  
    19. //example Code as i called in my build
    20. Debug.Log(text);
    21. var json = JsonUtility.FromJson<objectList>(text);
    22. Debug.Log(JsonUtility.ToJson(json));
    23.  
    Hope someone can help with this issue.
     
    Last edited: Feb 20, 2019
  2. UDN_08a62c30-cdb1-4e59-bb48-acda29f9e582

    UDN_08a62c30-cdb1-4e59-bb48-acda29f9e582

    Joined:
    Jun 19, 2018
    Posts:
    23
    Just checked my code, using latest 2018.3.6.
    I had no problem with JsonUtility.

    There is only thing I noticed. Why are you setting default value? Maybe this is a reason
    public List<listObject> children;
     
    dblaw likes this.
  3. threedinteractive

    threedinteractive

    Joined:
    Jul 18, 2018
    Posts:
    8
    Hi, I tried your suggestion and tested a little bit more and now it works again.
    I needed to set default values, because after i removed the new List<listObject>(), my JSON was complete empty.
    Now i have set for all variables default values and it started to work again.
    This behavior occurs only in WebGL and I think that in previous versions it was not neccessarry to set default values (like in the editor or my windows build)

    thanks for the help and best wishes
     
  4. threedinteractive

    threedinteractive

    Joined:
    Jul 18, 2018
    Posts:
    8
    Hi, I have to adjust my statement from yesterday again.
    JSON serialization still does not work correctly in the release build of WebGL for me.
    However, it works in the "Development Build".
    The tested Classes are:
    Code (CSharp):
    1. [Serializable]
    2. public class objectList
    3. {
    4.     [SerializeField]
    5.     private long pID = 0;
    6.     public long Parent { get { return pID; } set { pID = value; } }
    7.  
    8.     [SerializeField]
    9.     private List<listObject> children = new List<listObject>();
    10.     public List<listObject> Children { get { return children; } }
    11. }
    12.  
    13. [Serializable]
    14. public class listObject
    15. {
    16.     [SerializeField]
    17.     private string name = "";
    18.     public string Name { get { return name; } }
    19.  
    20.     [SerializeField]
    21.     private long oID = 0;
    22.     public long ID { get { return oID; } }
    23.  
    24.     [SerializeField]
    25.     private uint childCount = 0;
    26.     public uint ChildCount { get { return childCount; } }
    27.  
    28.     [SerializeField]
    29.     private bool first = true;
    30.     public bool isFirst { get { return first; } }
    31.  
    32.     [SerializeField]
    33.     private bool last = true;
    34.     public bool isLast { get { return last; } }
    35. }
    and

    Code (CSharp):
    1. [Serializable]
    2. public class objectList
    3. {
    4.     public long pID = 0;
    5.     public List<listObject> children = new List<listObject>();
    6. }
    7.  
    8. [Serializable]
    9. public class listObject
    10. {
    11.     public string name = "";
    12.     public long oID = 0;
    13.     public uint childCount = 0;
    14.     public bool first = true;
    15.     public bool last = true;
    16. }
    The Log from Development Build:
    The Log from Release Build, all numbers are stripped
     
  5. TheRoccoB

    TheRoccoB

    Joined:
    Jun 29, 2017
    Posts:
    54
    This is sort of a non-answer, but I'd suggest using MiniJson available free here https://gist.github.com/darktable/1411710

    It's the library that Facebook itself includes in their Facebook unity API, and the facebook peeps know web data well. I've used it successfully several times.
     
  6. forcepusher

    forcepusher

    Joined:
    Jun 25, 2012
    Posts:
    227
    Just in case someone comes across this thread via search engines.
    Most likely this is caused by code stripping. Use [field: Preserve] attribute on every serialized field to avoid issues.
     
  7. OxDEADFACE

    OxDEADFACE

    Joined:
    Jul 6, 2017
    Posts:
    33
    I see a pattern: it stripped out all the `long` and `uint` fields.

    I wonder if they would have been stripped if `int` was used instead?

    Old post I know. But it seems this issue (or something like it) might be happening to us over here in 2020 LTS.