Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

WebGL - Can no longer serialize properties within classes using newtonsoft.json

Discussion in 'Web' started by DamnICantFly, Jun 5, 2018.

  1. DamnICantFly

    DamnICantFly

    Joined:
    Sep 27, 2013
    Posts:
    12
    WebGL - Can no longer serialize properties within classes using newtonsoft.json

    In our project we used the official Newtonsoft.Json DLL to serialize and deserialize objects.
    Building our project for WebGL, resulted in a perfectly fine working game.
    About 1-2 weeks ago, 'something' changed and errors are thrown when serializing and deserializing objects.
    Initially we thought it was because of a Unity beta update, however, after reverting our entire project back to it's previously working state. And using the editor that was used to build that previous build, we get the same results.

    Reading up on it, apparently Newtonsoft.Json is not officially supported for WebGL, however, it's strange that it did work in the past.

    Now. Using the JsonDotNet Unity package from the asset store, we also get issues with serializing objects which have properties.

    From what I understand, this has something to do with code stripping, however, again, this all used to work in the past, and suddenly it's broken.
    Tried this with Unity version 2018.2.0b6,m 2018.2.0b5, 2018.2.0b4, 2018.2.0b3, 2018.2.0b1, 2018.1.0b13 and 2017.2.3f1

    Basically to test this out. Create a new Unity project. Add the JsonDotNet Unity package, create a class with a public get-set-property ("public float SomeProperty {get; set;}"), try to serialize an instance of the object. In the editor this works fine. Creating a WebGL build (WebAssembly) however, it throws the error: "ExecutionEngineException: Attempting to call method 'System.Reflection.MonoProperty::GetterAdapterFrame<JsonEnumConverterTest+AClassToSerializeWithProperty,System.Single>' for which no ahead of time (AOT) code was generated."

    I created a bug report here: https://fogbugz.unity3d.com/default.asp?1045937_dc036s520rrgk6cc

    Maybe anyone else has a clue as to what's going on here.
     
  2. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    This is actually a Unity bug that they are aware of. I'm not sure if it's been fixed yet.. Are you using .NET 4.6? If you are, there's an updated package of Json .NET for Unity that works around the issue. Right now it only supports .NET 4.6 Profile. It'll work in the editor, standalone, consoles, ios, android, WebGL, etc. It'll also work in UWP as long as you're using IL2CPP.
     
  3. Jeff-Kesselman

    Jeff-Kesselman

    Joined:
    Apr 5, 2010
    Posts:
    99
    This is from 2 years ago and Im having this exact same problem in Unity2020? Is there a recommended fix? Do I have to run the API in 4.x?
     
  4. mteixeira_unity

    mteixeira_unity

    Joined:
    Jul 15, 2020
    Posts:
    1
    I had the same problem. I "Fixed" this JsonDotNet Unity package, and adding link.xml do keep this assembly code, making it to not being stripped
     
  5. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    953
    @Jeff-Kesselman Looking at our bug tracker, the original bug is marked fixed. We are not aware of a current issue here. If you still have a problem that persists in latest Unity release, it would be much appreciated if you can create a small test case and report a bug.
     
  6. dblaw

    dblaw

    Joined:
    Oct 16, 2020
    Posts:
    9
    I'm having similar problems, but no error. Just trying to deserialize JSON, an array of strings under one key name, using an object type with one list of strings. Works just fine in the editor. Fails silently when rendered out as a WebGL project.

    Using Unity 2019.4.1f1, and Newtonsoft JSON downloaded just yesterday.

    Can someone explain how they have fixed this?
     
  7. dblaw

    dblaw

    Joined:
    Oct 16, 2020
    Posts:
    9
    I've solved my problem, and coming back for any intrepid coders googling a similar headache.

    In my Unity JSON desperation, I had come across this website for creating C# objects from given JSON
    https://json2csharp.com

    It's a very useful tool! However, it seems to create objects in a very specific way. I was working with JSON that had an array of text values labeled as "body". This gave me a data object like this:

    Code (CSharp):
    1. [Serializable]
    2.     public class data
    3.     {
    4.         public List<string> body { get; set; }
    5.     }
    Using Newtonsoft JSON, this worked out just fine... in the editor. When running the same code on WebGL, it failed silently. But changing it to this:

    Code (CSharp):
    1. [Serializable]
    2.     public class data
    3.     {
    4.         public List<string> body;
    5.     }
    Worked on both platforms. Hopefully this helps someone in the future.
     
    uatarONE, mdrunk and swgmax like this.
  8. xujinm

    xujinm

    Joined:
    Dec 11, 2018
    Posts:
    13
    It works in my issue
     
    mdrunk, swgmax and dblaw like this.
  9. mdrunk

    mdrunk

    Joined:
    Apr 26, 2014
    Posts:
    11
    I can replicate this in 2020.3.35f1 and 2019.4.24f1 in WebGL builds, but it goes a step further than that. If I examine every property in a model with Debug.Log, and nothing else, it will serialize the model properly. Whatever Debug.Log statements that I comment out, those properties will serialize as null. It's a little crazy.

     
  10. simpan83

    simpan83

    Joined:
    Jun 17, 2022
    Posts:
    3