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

Discussion and help for Easy Save 3
Post Reply
silkmoon18
Posts: 13
Joined: Sun Oct 01, 2023 11:04 am

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

Post 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?
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

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

Post 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
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
silkmoon18
Posts: 13
Joined: Sun Oct 01, 2023 11:04 am

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

Post 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?
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

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

Post 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
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
silkmoon18
Posts: 13
Joined: Sun Oct 01, 2023 11:04 am

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

Post 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.
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

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

Post 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
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
silkmoon18
Posts: 13
Joined: Sun Oct 01, 2023 11:04 am

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

Post 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!
Post Reply