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

Home Page:https://github.com/jilleJr/Newtonsoft.Json-for-Unity

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bug: Deserializing HashSet<T> fails

klootas opened this issue · 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.

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: #51 (comment), I believe it's related.

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.

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