Page 1 of 1

FormatException: Expected '{' or "null", found '['.

Posted: Mon Oct 30, 2023 1:23 pm
by silkmoon18
Dictionary<string, object> data;
data["Key"] = some list;

When I save and load a dictionary like this, this exception is thrown. Is there a way to solve or work around this?

Re: FormatException: Expected '{' or "null", found '['.

Posted: Mon Oct 30, 2023 2:49 pm
by Joel
Hi there,

This happens because the value of your Dictionary is object which provides no information about the type of data, and it's not possible to store type information with a List. In this case it tries to load it as an object rather than a List, which causes your error.

So that it's possible to determine the type of the data you would need to either use a strictly-typed Dictionary (i.e. Dictionary<string, List<string>>), or
wrap your List in another class before adding it to the Dictionary. I.e.

Code: Select all

public class WrappedStringList
{
	public List<string> data;
	
	public WrappedStringList(List<string> data)
	{
		this.data = data;
	}
}
And then instead of:

Code: Select all

data["key"] = myStringList;
You would do:

Code: Select all

data["key"] = new WrappedStringList(myStringList);
All the best,
Joel

Re: FormatException: Expected '{' or "null", found '['.

Posted: Tue Oct 31, 2023 1:32 am
by silkmoon18
Joel wrote: Mon Oct 30, 2023 2:49 pm Hi there,

This happens because the value of your Dictionary is object which provides no information about the type of data, and it's not possible to store type information with a List. In this case it tries to load it as an object rather than a List, which causes your error.

So that it's possible to determine the type of the data you would need to either use a strictly-typed Dictionary (i.e. Dictionary<string, List<string>>), or
wrap your List in another class before adding it to the Dictionary. I.e.

Code: Select all

public class WrappedStringList
{
	public List<string> data;
	
	public WrappedStringList(List<string> data)
	{
		this.data = data;
	}
}
And then instead of:

Code: Select all

data["key"] = myStringList;
You would do:

Code: Select all

data["key"] = new WrappedStringList(myStringList);
All the best,
Joel
Thank you Joel. Is there a way to check if a type is supported by ES3 in scripts?

Re: FormatException: Expected '{' or "null", found '['.

Posted: Tue Oct 31, 2023 9:11 am
by Joel
The only way for your particular Dictionary would be to attempt to save and load it and catch the exception as it's not possible to determine what types of data your Dictionary contains from the type alone.

All the best,
Joel

Re: FormatException: Expected '{' or "null", found '['.

Posted: Tue Oct 31, 2023 11:28 am
by silkmoon18
Joel wrote: Tue Oct 31, 2023 9:11 am The only way for your particular Dictionary would be to attempt to save and load it and catch the exception as it's not possible to determine what types of data your Dictionary contains from the type alone.

All the best,
Joel
I see the problem. I am wondering if there is a method like IsSupported(typeof(obj)) to check if a specific type is supported.

Re: FormatException: Expected '{' or "null", found '['.

Posted: Tue Oct 31, 2023 1:08 pm
by Joel
You can try the following undocumented internal method:

Code: Select all

try
{
	ES3Internal.ES3TypeMgr.GetOrCreateES3Type(typeof(YourType));
}
catch
{
	Debug.Log("Not supported");
}
However, as mentioned above this won't work for the situation you've described above because Dictionary<string, object> is a supported type and List<string> is a supported type, but a Dictionary<string, object> containing a List<string> can't be deserialized because it's not possible to deserialize a List<string> as an object.

All the best,
Joel

Re: FormatException: Expected '{' or "null", found '['.

Posted: Tue Oct 31, 2023 1:21 pm
by silkmoon18
Joel wrote: Tue Oct 31, 2023 1:08 pm You can try the following undocumented internal method:

Code: Select all

try
{
	ES3Internal.ES3TypeMgr.GetOrCreateES3Type(typeof(YourType));
}
catch
{
	Debug.Log("Not supported");
}
However, as mentioned above this won't work for the situation you've described above because Dictionary<string, object> is a supported type and List<string> is a supported type, but a Dictionary<string, object> containing a List<string> can't be deserialized because it's not possible to deserialize a List<string> as an object.

All the best,
Joel
Thanks!