Newtonsoft.Json-for-Unity icon indicating copy to clipboard operation
Newtonsoft.Json-for-Unity copied to clipboard

Bug: Deserializing HashSet<T> fails

Open klootas opened this issue 4 years ago • 3 comments

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.

klootas avatar Jan 27 '21 16:01 klootas

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.

applejag avatar Jan 28 '21 06:01 applejag

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.

fahall avatar Feb 23 '21 19:02 fahall

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]);
        });
    }
}

applejag avatar Feb 27 '21 19:02 applejag