applejag / Newtonsoft.Json-for-Unity

Newtonsoft.Json (Json.NET) 10.0.3, 11.0.2, 12.0.3, & 13.0.1 for Unity IL2CPP builds, available via Unity Package Manager
https://github.com/jilleJr/Newtonsoft.Json-for-Unity
MIT License
1.16k stars 131 forks source link

Bug: Deserializing HashSet<T> fails #79

Open klootas opened 3 years ago

klootas commented 3 years ago

It seems that deserializing a class with a property of type HashSet yields the following runtime exception in WebGL:

ArgumentNullException value cannot be null parameter name: method

Changing it to a List works though.

applejag commented 3 years ago

Hi @klootas, thanks for reporting this. I haven't really considered HashSets.

I'll have to look into this, but in the meantime you might find some answers here: https://github.com/jilleJr/Newtonsoft.Json-for-Unity/discussions/51#discussioncomment-185183, I believe it's related.

fahall commented 3 years ago

In my project, I (de)serialized HashSets successfully in Engine, Windows, MacOS, and iOS. But, when I build for Android with IL2CPP, I get the error above.

applejag commented 3 years ago

Hi @fahall! Do you have access to the errors you receive?

You may be able to workaround it by adding such a script to your project: (does not need to be added to any GameObject)

using System;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json.Utilities;

public class AotTypeEnforcer : MonoBehaviour
{
    public void Awake()
    {
        AotHelper.EnsureList<MyHashSetType>();
        AotHelper.Ensure(() =>
        {
            _ = new HashSet<MyHashSetType>(new MyHashSetType[0]);
        });
    }
}